annotate PanExplorer_workflow/Perl/ConvertPanacotaMatrix.pl @ 2:97e4e3e818b6 draft

Uploaded
author dereeper
date Thu, 30 May 2024 11:48:09 +0000
parents 032f6b3806a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
1 #!/usr/bin/perl
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
2
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
3 use strict;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
4
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
5 my $indir = $ARGV[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
6 my $matrix = $ARGV[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
7 my $out = $ARGV[2];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
8 my $strain_names = $ARGV[3];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
9
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
10 my %strains_of_gb;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
11 open(F,$strain_names);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
12 while(<F>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
13 my $line = $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
14 $line =~s/\n//g;$line =~s/\r//g;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
15 my ($gb,$strain) = split(/\t/,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
16 $strains_of_gb{$gb} = $strain;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
17 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
18 close(F);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
19
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
20 my @strains;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
21 my %corr;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
22 open(D,"ls $indir/*pep |");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
23 while(<D>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
24 my $file = $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
25 my $prot_num = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
26 my $strain;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
27 if ($file =~/\/([^\/]*).pep/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
28 $strain = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
29 $strain = $strains_of_gb{$strain};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
30 push(@strains,$strain);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
31 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
32 open(F,"$file");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
33 while(<F>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
34 if (/>(.*)/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
35 my $prot_id = $1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
36 $prot_num++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
37 my $new_id = "$strain"."_".$prot_num;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
38 $corr{$new_id} = $prot_id;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
39 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
40 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
41 close(F);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
42 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
43 close(D);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
44
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
45 my $cl_num = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
46 my $nb_strains = 1;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
47 open(O,">$out");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
48 open(U,">$out.upsetr.txt");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
49 open(M,">$out.accessory_01matrix.txt");
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
50 open(F,$matrix);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
51 print O "ClutserID";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
52 print U "ClutserID";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
53 print M "Gene";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
54 my %hash_place_strains;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
55 my $num_cell = 0;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
56 foreach my $strain(@strains){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
57 $num_cell++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
58 my @words = split(/_/,$strain);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
59 my $genus = $words[0];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
60 my $species = $words[1];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
61 my $shortname = substr($genus,0,3) . "_". substr($species,0,2);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
62 for (my $j = 2; $j <= $#words; $j++){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
63 $shortname.="_".$words[$j];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
64 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
65 $shortname = substr($shortname,0,25);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
66 print O "\t".$strain;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
67 print U "\t".$shortname;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
68 print M "\t".$shortname;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
69 $hash_place_strains{$strain} = $num_cell;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
70 $nb_strains++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
71 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
72 print O "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
73 print U "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
74 print M "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
75 while(<F>){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
76 $cl_num++;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
77 my $line = $_;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
78 $line =~s/\n//g;$line =~s/\r//g;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
79 my @infos = split(/ /,$line);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
80 my %cells;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
81 for (my $i = 1; $i <= $#infos; $i++){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
82 my $new_id = $infos[$i];
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
83 my $prot_id = $corr{$new_id};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
84 my $strain;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
85 if ($new_id =~/^(.*)_\d+$/){$strain=$1;}
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
86 my $num_cell = $hash_place_strains{$strain};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
87 #print "$strain $num_cell $prot_id $new_id\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
88 $cells{$strain}.= $prot_id.",";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
89 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
90 print O $cl_num;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
91 print U $cl_num;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
92 my $concat_accessory = "";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
93 foreach my $strain(@strains){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
94 my $val;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
95 if ($cells{$strain}){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
96 $val = $cells{$strain};
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
97 chop($val);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
98 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
99 else{
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
100 $val = "-";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
101 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
102 if ($val =~/\w+/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
103 print U "\t1";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
104 $concat_accessory .= "\t1";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
105 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
106 else{
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
107 print U "\t0";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
108 $concat_accessory .= "\t0";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
109 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
110 my $concat = $val;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
111 print O "\t".$concat;
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
112 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
113 if ($concat_accessory =~/0/){
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
114 print M $cl_num.$concat_accessory."\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
115 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
116 print O "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
117 print U "\n";
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
118 }
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
119 close(F);
032f6b3806a3 Uploaded
dereeper
parents:
diff changeset
120 close(O);