-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheckSTORI.pl
425 lines (390 loc) · 9.58 KB
/
checkSTORI.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#command: perl /path/to/checkSTORI.pl /path/to/file-A /path/to/file-B [-q | -c | -org <file>] [-s]
#-q means quiet mode
#-c means concise, ie omit the taxaIds, -1s, and ?s. NB: If this option is used, gis on the same line will not necessarily be from the same taxon.
#-org <file> means organize the output according to a file, which specifies on each line <taxID> <species.name> <phylum>
#-s means print the score for each family member in addition to the family members
use lib '/home/josh/perl5/lib';
use lib '/home/josh/perl5/lib/perl5';
use Data::Dumper;
use List::MoreUtils qw / uniq /;
my $inputA=shift(@ARGV);
my $inputB=shift(@ARGV);
my $quietOrConcise=shift(@ARGV);
my $quietFlag=0; my $conciseFlag=0; my $orgFile=-1; my $orgFlag=0;
if ($quietOrConcise =~ m/-q/) {
$quietFlag = 1;
}
elsif ($quietOrConcise =~ m/\-c/) {
$conciseFlag = 1;
}
elsif ($quietOrConcise =~ m/\-org/) {
$orgFile = shift(@ARGV);
$orgFlag=1;
}
my $score = shift(@ARGV);
my $scoreFlag=0;
$scoreFlag=1 if ($score =~ m/\-s/);
my @taxaArr=(); my $finishedCount=0;
my @temp = @{GetFams($inputA)}; #read the two datasets into memory
my %famsA = %{$temp[0]};
my %scoresA = %{$temp[1]};
my $runtime_a = $temp[2];
#print Dumper \%famsA;
#print Dumper \%scoresA;
my @temp = @{GetFams($inputB)}; #read the two datasets into memory
my %famsB = %{$temp[0]};
my %scoresB = %{$temp[1]};
my $runtime_b = $temp[2];
#print Dumper \%scoresB;
@taxaArr = uniq @taxaArr;
my $taxaCount=($#taxaArr + 1);
#Find families between the two datasets that have common sequences
my @pairScores=();
foreach my $famFromA (keys %famsA) {
my %consensusFam = ();
my %chosenFam = ();
my $chosenFamName=-1;
my $consensusSize = -1;
foreach my $famFromB (keys %famsB) {
my $agreementScore = GetAgreementScore($famsA{$famFromA},$famsB{$famFromB});
my @temp = ($famFromA, $famFromB, $agreementScore);
push @pairScores, [@temp];
}
}
my @pairScores_sorted = sort { $b->[2] <=> $a->[2] } @pairScores; #sort descending
#print Dumper \@pairScores_sorted;
my @famNames=();
my %consensusFams=();
my %consensusScores=();
foreach my $pair_ref (@pairScores_sorted) {
if ($pair_ref->[2] > 0) {
my $famA = $pair_ref->[0];
my $famB = $pair_ref->[1];
if (exists $famsA{$famA}) {
if (exists $famsB{$famB}) {
my @famsAndScores = @{GetConsensusFam($famsA{$famA},$famsB{$famB},$scoresA{$famA},$scoresB{$famB})};
my %consensus = %{$famsAndScores[0]};
my %consensus_s = %{$famsAndScores[1]};
my $consensusName = $famA . $famB;
push @famNames, $consensusName;
delete $famsA{$famA};
delete $famsB{$famB};
#print $consensusName . "\n";
#print Dumper \%consensus;
$consensusFams{$consensusName} = {%consensus};
$consensusScores{$consensusName} = {%consensus_s}
}
}
}
}
my %famAgreement=();
foreach my $family (@famNames) {
my $disagreements=0;
foreach my $taxon (@taxaArr) {
if ($consensusFams{$family}{$taxon} eq "?") {
$disagreements++;
}
}
my $agreement = ($taxaCount - $disagreements);
my $score = ($agreement/$taxaCount);
$famAgreement{$family}=$score;
}
#print "quietFlag= $quietFlag\n";
if ($quietFlag==1) {
#print "finishedCount= $finishedCount\n";
print "runtime_a: $runtime_a\n";
print "runtime_b: $runtime_b\n";
if ($finishedCount==2) { print "bothfinished\n"; }
}
if ($orgFlag==1) {
print "-1 -1 ";
}
print "score ";
foreach my $family (@famNames) {
print $famAgreement{$family} . " ";
}
if ($#famNames == -1) {
print "-1\n"; }
if (($quietFlag==0) && ($conciseFlag==0) && ($orgFlag==0)) {
print "\n";
print "family ";
foreach my $family (@famNames) {
print $family . " ";
}
print "\n";
foreach my $taxon (@taxaArr) {
print $taxon . " ";
foreach my $family (@famNames) {
if (exists $consensusFams{$family}{$taxon}) {
print $consensusFams{$family}{$taxon} . " ";
}
else {
print "-1 ";
}
}
print "\n";
}
print "\n\n";
foreach my $taxon (@taxaArr) {
print $taxon . " " if ($scoreFlag==1);
foreach my $family (@famNames) {
if (exists $consensusScores{$family}{$taxon}) {
print $consensusScores{$family}{$taxon} . " " if ($scoreFlag==1);
}
else {
print "-1 " if ($scoreFlag==1);
}
}
print "\n" if ($scoreFlag==1);
}
}
elsif (($quietFlag==0) && ($conciseFlag==1) && ($orgFlag==0)) {
print "\n";
print "family ";
foreach my $family (@famNames) {
print $family . " ";
}
print "\n";
my @conciseGis=();
my $row=0; my $col=0;
foreach my $family (@famNames) {
my %temp = %{$consensusFams{$family}};
foreach my $taxon (keys %temp) {
if (!($temp{$taxon} =~ m/\?/)) {
$conciseGis[$row][$col] = $temp{$taxon};
$row++;
}
}
$col++;
$row=0;
}
foreach my $row_ref (@conciseGis) {
my @row = @{$row_ref};
my $c=0;
print "taxon_na ";
foreach my $fam (@famNames) {
if (defined $row[$c]) {
print $row[$c] . " "; }
else {
print "na "; }
$c++;
}
print "\n";
}
}
elsif ($orgFlag==1) {
print "\n";
print "taxID name phylum ";
foreach my $family (@famNames) {
print $family . " ";
}
print "\n";
#print "orgFile\= $orgFile\n";
if (!($orgFile eq "-1")) {
open org, $orgFile;
}
my @taxids_ordered=();
my @names_ordered=();
my @phyla_ordered=();
while (<org>) {
my $line = $_;
chomp($line);
if ($line =~ m/(\d+)\t(.+)\t(.+)/) {
push @taxids_ordered, $1;
push @names_ordered, $2;
push @phyla_ordered, $3;
#print "file\: $1 $2 $3\n";
}
}
my $r=0;
foreach my $taxon (@taxids_ordered) {
print $taxon . " " . $names_ordered[$r] . " " . $phyla_ordered[$r] . " ";
foreach my $family (@famNames) {
if (exists $consensusFams{$family}{$taxon}) {
print $consensusFams{$family}{$taxon} . " ";
}
else {
print "-1 ";
}
}
print "\n";
$r++;
}
print "\n\n";
my $r=0;
foreach my $taxon (@taxids_ordered) {
print $taxon . " " . $names_ordered[$r] . " " . $phyla_ordered[$r] . " " if ($scoreFlag==1);
foreach my $family (@famNames) {
if (exists $consensusScores{$family}{$taxon}) {
print $consensusScores{$family}{$taxon} . " " if ($scoreFlag==1);
}
else {
print "-1 " if ($scoreFlag==1);
}
}
print "\n" if ($scoreFlag==1);
$r++;
}
close org;
}
print "\n";
sub GetAgreementScore {
my %fam1 = %{shift(@_)};
my %fam2 = %{shift(@_)};
my $score=0;
foreach my $taxon (keys %fam1) {
if (exists $fam2{$taxon}) {
$fam1{$taxon} =~ m/^(.+)$/;
my $gi1 = $1;
$fam2{$taxon} =~ m/^(.+)$/;
my $gi2 = $1;
if ($gi1 eq $gi2) {
$score++;
}
}
}
return $score;
}
sub GetConsensusFam {
my %fam1 = %{shift(@_)};
my %fam2 = %{shift(@_)};
my %scores1 = %{shift(@_)};
my %scores2 = %{shift(@_)};
my %consensus=();
my %scores=();
foreach my $taxon (keys %fam1) {
if (exists $fam2{$taxon}) {
$fam1{$taxon} =~ m/^(.+)$/;
my $gi1 = $1;
$fam2{$taxon} =~ m/^(.+)$/;
my $gi2 = $1;
if ($gi1 eq $gi2) {
$consensus{$taxon} = $gi1;
$scores{$taxon} = ($scores1{$taxon} + $scores2{$taxon});
}
else {
$consensus{$taxon} = "?";
$scores{$taxon} = "?";
}
}
else {
$consensus{$taxon} = "?";
$scores{$taxon} = "?";
}
}
foreach my $taxon (keys %fam2) {
if (exists $fam1{$taxon}) {
}
else {
$consensus{$taxon} = "?";
$scores{$taxon} = "?";
}
}
my @temp = (\%consensus, \%scores);
return \@temp;
}
sub GetFams {
#print "entered GetFams\n";
my $inputFile = shift(@_);
print "inputFile is $inputFile\n";
open (input, $inputFile);
my @fileArr=<input>;
my $startLine=-1;
my $startLine_score=-1;
my $runtime=-1;
for (my $r=$#fileArr; $r>=0; $r--) {
if ($fileArr[$r] =~ m/final\stable\smain/) {
$startLine = ($r + 2);
$r=-1;
}
elsif ($fileArr[$r] =~ m/score\stable/) {
$startLine_score = ($r + 2);
#needn't set r to -1 because the score table always follows "final table main"
}
}
my $finishedFlag=0;
for (my $r=$#fileArr; $r>=0; $r--) {
if ($fileArr[$r] =~ m/runtime\s(.+)\s\[total-runtime\](.*)/) {
#print "match\n";
$runtime=$1; $theRest = $2;
if ($theRest =~ m/finished/) { $finishedFlag=1; }
$r=-1;
}
}
if ($finishedFlag==1) {
$finishedCount++;
if ($quietFlag==0) {
print "$inputFile has finished.\n";
}
}
my %fam = ();
my @famsArr=();
my %scores = ();
if ($startLine>0) {
#print "startLine is $startLine\n";
for (my $r=$startLine; $r<=$#fileArr; $r++) {
my $line=$fileArr[$r];
chomp($line);
#print $line . "\n";
if ($line=~ m/^taxon/) {
$line =~ m/taxon\s(.+)/;
my $famsTemp = $1;
chomp($famsTemp);
@famsArr = split / /, $famsTemp;
}
elsif ($line =~ m/^\d+/) {
$line =~ m/(\d+)\s\s(.+)/;
my $gisTemp = $2;
chomp($gisTemp);
my @giArr = split / /, $gisTemp;
my $taxon = $1;
push @taxaArr, $taxon;
foreach my $family (@famsArr) {
if ($family =~ m/.+/) {
my $gi = shift(@giArr);
if ($gi ne -1) {
$fam{$family}{$taxon} = $gi;
#print "fam{$family}{$taxon}=$gi\n";
}
}
}
}
else {
$r=($#fileArr + 1);
}
}
}
if ($startLine_score>0) {
for (my $r=$startLine_score; $r<=$#fileArr; $r++) {
my $line=$fileArr[$r];
chomp($line);
if ($line=~ m/^taxon/) {
$line =~ m/taxon\s(.+)/;
my $famsTemp = $1;
chomp($famsTemp);
@famsArr = split / /, $famsTemp;
}
elsif ($line =~ m/^\d+/) {
$line =~ m/(\d+)\s\s(.+)/;
my $scoresTemp = $2;
chomp($scoresTemp);
my @scoresArr = split / /, $scoresTemp;
my $taxon = $1;
foreach my $family (@famsArr) {
if ($family =~ m/.+/) {
my $score = shift(@scoresArr);
if ($score != -1) {
$scores{$family}{$taxon} = $score;
#print "fam{$family}{$taxon}=$gi\n";
}
}
}
}
else {
$r=($#fileArr + 1);
}
}
}
my @temp = (\%fam, \%scores, $runtime);
return \@temp;
}