-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpanoct.pl
executable file
·3798 lines (3557 loc) · 169 KB
/
panoct.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#Copyright (C) 2011-2015 The J. Craig Venter Institute (JCVI). All rights reserved
#Written by Derrick E. Fouts, Ph.D. and Granger Sutton, Ph.D.
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#Revision notes
# Changed shebang line from /usr/local/bin/perl to "/usr/bin/env perl" for increased portablity
# Using cwd() instead of $ENV{'PWD'} for increased portability.
# Made warning for case when best BLAST score > than self score show only in debug mode
# Increased input and output file documentation in README.txt file
# Added options T, C, W, S, U. B, c and e
# Fixed bugs (refer to the svn code repository)
# Match table and other revisions 01/07/2004
# Added NCBI -m9 input option 12/08/2010
# Moved genome identification from feat/locus_name-tag to the gene_att file 12/08/2010
my $commandline = join (" ", @ARGV);
my $prog = $0;
$prog =~ s/.*\///;
use strict;
use warnings;
use Getopt::Std;
use Data::Dumper;
use Scalar::Util qw(looks_like_number);
getopts ('TdhDb:p:t:f:i:I:E:L:M:G:H:V:P:Q:N:g:A:C:F:a:s:W:S:U:B:c:e:R:');#GGS added F, a, s, options F option for frameshift and a option for amount of missing amino acids to still be full length and s option for an evidence threshold for frameshifts
our ($opt_T,$opt_d,$opt_h,$opt_D,$opt_b,$opt_p,$opt_t,$opt_f,$opt_i,$opt_I,$opt_E,$opt_L,$opt_M,$opt_G,$opt_H,$opt_V,$opt_P,$opt_Q,$opt_N,$opt_g,$opt_A,$opt_C,$opt_F,$opt_a,$opt_s,$opt_W,$opt_S,$opt_U, $opt_B, $opt_c, $opt_e, $opt_R);
my ($print_cluster_numbers,$compute_centroids,$basedir,$btabpath,$pep_path,$att_file,$pep_file,$btabfile,$tagfile,$percentid,$fspercentid,$evalue,$min_hit_length,$microarray,$histogramfile,$hitsfile,$vennfile,$normalizefile,$writeparalogs,$writeclusterweights,$DEBUG,$frameshiftfile,$frameshift_length_ratio,$max_missing_aa,$frames_link_thresh,$ANCHOR_window_size,$ANCHOR_cutoff,$CGN_window_size,$strict_orthos,$fragmentfile, $pairwisefile, $compute_adjacency, $cluster_input_file, $process_clusters);#GGS added frameshift variables
#GGS $frameshiftfile is a boolean for detecting and outputting frameshifted or truncated protein fragments true if -F
#GGS -F $frameshift_length_ratio is a value between 1 and 2 (default 1.33) used to determine that we are detecting adjacent protein fragments rather than adjacent paralogs
#GGS -a $max_missing_aa is how many amino acids at either end of a blast match can be missing and still be considered full length
#GGS -s $frames_link_thresh is a threshold for number of blast matches indicating a frameshift to believe it
#GGS -G yes outputs a histograms file
#set defaults
$microarray = 0;
$hitsfile = 0;
$histogramfile = 0;
$strict_orthos = 1;
$normalizefile = 0;
$writeparalogs = 1;
$fragmentfile = 1;
$pairwisefile = 1;
$writeclusterweights = 1;
$vennfile = 1;
$compute_adjacency = 0;
$compute_centroids = 1;
$ANCHOR_window_size = 2;
$ANCHOR_cutoff = (2 * $ANCHOR_window_size) + 1;
$cluster_input_file = "";
$process_clusters = 0;
my @cluster_levels = ();
my @BSR_cluster_levels = ();
## use boolean logic: TRUE = 1, FALSE = 0
my $version = "3.23";
if ($opt_d) {
$compute_centroids = 1;
} else {
$compute_centroids = 1; #default - always compute centroids
}
if ($opt_T) {
$print_cluster_numbers = 1;
} else {
$print_cluster_numbers = 0; #default
}
if ($opt_D) {$DEBUG = 1;} else { $DEBUG = 0; } # Debug mode is off as default.
#GGS process strict ortholog option
if ($opt_S) {
if (($opt_S eq "Y") or ($opt_S eq "y") or ($opt_S eq "Yes") or ($opt_S eq "yes") or ($opt_S eq "YES")) {$strict_orthos = 1;}
elsif (($opt_S eq "N") or ($opt_S eq "n") or ($opt_S eq "No") or ($opt_S eq "no") or ($opt_S eq "NO")) {$strict_orthos = 0;}
elsif (($opt_S eq "M") or ($opt_S eq "m") or ($opt_S eq "More") or ($opt_S eq "more") or ($opt_S eq "MORE")) {$strict_orthos = 2;}
else { $strict_orthos = 1; } # Want to use strict criteria for orthologs 0 = NO, 1 = YES [DEFAULT = YES]
}
#GGS process CGN window size option
if ($opt_W) {
if (($opt_W >= 1) && ($opt_W <= 20)) {#GGS ratioCGN window size must be strictly between 1 and 20
$CGN_window_size = int $opt_W;
} else {
&option_help;
}
} else {
$CGN_window_size = 5; #default CGN window size
}
#GGS process frameshift option
if ($opt_F) {
$frameshiftfile = 1;
if (($opt_F < 2) && ($opt_F > 1)) {#GGS ratio must be strictly between 1 and 2
$frameshift_length_ratio = $opt_F;
} else {
&option_help;
}
} else {
$frameshiftfile = 0;
}
#GGS set max_missing_aa
if ($opt_a) {
if (($opt_a > 0) && ($opt_a <= 100)) {#GGS must be between 0 and 100
$max_missing_aa = $opt_a;
} else {
&option_help;
}
} else {
$max_missing_aa = 20;#default
}
#GGS set frameshift evidence threshold
if ($opt_s) {
if ($opt_s > 0) {#GGS must be > 0
$frames_link_thresh = $opt_s;
} else {
&option_help;
}
} else {
$frames_link_thresh = 1;#default
}
if ($opt_h) { &option_help; } # quit with help menu
if ($opt_b) {$basedir = $opt_b;} else { $basedir = $ENV{'PWD'}; } # if no value for option b (base or working directory) set it to current directory
if ($opt_p) {$btabpath = $opt_p;} else { $btabpath = $basedir; } # if no value given for path to btab file, make default = base directory
if ($opt_Q) {$pep_path = $opt_Q;} else { $pep_path = $basedir; } # if no value given for path to peptide file, make default = base directory
if (($opt_g) && (-s "$opt_g")) {$att_file = $opt_g;} else { print STDERR "Error with -g\n"; &option_help; } # if no value for option g (name of gene_att file), quit with help menu
if (($opt_P) && (-s "$pep_path/$opt_P")) {$pep_file = $opt_P;} else { print STDERR "Error with -P $pep_path/$opt_P\n"; &option_help; } # if no value for option P (pepfile), quit with help menu
if (($opt_f) && (-s "$opt_f")) {$tagfile = $opt_f;} else { print STDERR "Error with -f $opt_f\n"; &option_help; } # must supply the file containing the list of unique genome names to analyze
if ($opt_R) {
if ($opt_t) {
print STDERR "Blast tabular output file: $opt_t is being ignored since previously computed clusters are being used (-R option)\n";
}
if (-s "$opt_R") {
$cluster_input_file = $opt_R;
$process_clusters = 1;
$btabfile = ""; #so outputting the parameters file doesn't complain
} else { #if supplied the -R argument must be a file of PanOCT formatted clusters
print STDERR "Error with -R $opt_R\n";
&option_help;
}
} else {
$process_clusters = 0;
#only need blast data if we are not just processing clusters
if (($opt_t) && (-s "$btabpath/$opt_t")) {$btabfile = $opt_t;} else { print STDERR "Error with -t $btabpath/$opt_t\n"; &option_help; } # if no value for option t (name of btab file), quit with help menu
}
if ($opt_i) {$percentid = $opt_i;} else { $percentid = 35.0; } # the minimum cutoff to use blast scores for possible matches
if ($opt_I) {$fspercentid = $opt_I;} else { $fspercentid = 35.0; } # the minimum cutoff to use blast scores for frameshift detection
if ($opt_E) {$evalue = $opt_E;} else { $evalue = 0.00001; } # if no E-value cut-off given, make default 0.00001
if ($opt_L) {$min_hit_length = $opt_L;} else { $min_hit_length = 1; }
if ($opt_M) {
if (($opt_M eq "Y") or ($opt_M eq "y") or ($opt_M eq "Yes") or ($opt_M eq "yes") or ($opt_M eq "YES")) {$microarray = 1;}
elsif (($opt_M eq "N") or ($opt_M eq "n") or ($opt_M eq "No") or ($opt_M eq "no") or ($opt_M eq "NO")) {$microarray = 0;}
else { $microarray = 0; } # Want to create microarray-like data of normalized BLAST scores 0 = NO, 1 = YES [DEFAULT = NO]
}
if ($opt_G) {
if (($opt_G eq "Y") or ($opt_G eq "y") or ($opt_G eq "Yes") or ($opt_G eq "yes") or ($opt_G eq "YES")) {$histogramfile = 1;}
elsif (($opt_G eq "N") or ($opt_G eq "n") or ($opt_G eq "No") or ($opt_G eq "no") or ($opt_G eq "NO")) {$histogramfile = 0;}
else { $histogramfile = 0; } # Want to create histograms 0 = NO, 1 = YES [DEFAULT = NO]
}
if ($opt_H) {
if (($opt_H eq "Y") or ($opt_H eq "y") or ($opt_H eq "Yes") or ($opt_H eq "yes") or ($opt_H eq "YES")) {$hitsfile = 1;}
elsif (($opt_H eq "N") or ($opt_H eq "n") or ($opt_H eq "No") or ($opt_H eq "no") or ($opt_H eq "NO")) {$hitsfile = 0;}
else { $hitsfile = 0; } # Want to create table of hits 0 = NO, 1 = YES [DEFAULT = NO]
}
if ($opt_V) {
if (($opt_V eq "Y") or ($opt_V eq "y") or ($opt_V eq "Yes") or ($opt_V eq "yes") or ($opt_V eq "YES")) {$vennfile = 1;}
elsif (($opt_V eq "N") or ($opt_V eq "n") or ($opt_V eq "No") or ($opt_V eq "no") or ($opt_V eq "NO")) {$vennfile = 0;}
else { $vennfile = 1; } # Want to create match table file? 0 = NO, 1 = YES [DEFAULT = YES]
}
if ($opt_N) {
if (($opt_N eq "Y") or ($opt_N eq "y") or ($opt_N eq "Yes") or ($opt_N eq "yes") or ($opt_N eq "YES")) {$normalizefile = 1;}
elsif (($opt_N eq "N") or ($opt_N eq "n") or ($opt_N eq "No") or ($opt_N eq "no") or ($opt_N eq "NO")) {$normalizefile = 0;}
else { $normalizefile = 0; } # Want to create normalized BLAST score file? 0 = NO, 1 = YES [DEFAULT = NO]
}
if ($opt_U) {
if (($opt_U eq "Y") or ($opt_U eq "y") or ($opt_U eq "Yes") or ($opt_U eq "yes") or ($opt_U eq "YES")) {$fragmentfile = 1;}
elsif (($opt_U eq "N") or ($opt_U eq "n") or ($opt_U eq "No") or ($opt_U eq "no") or ($opt_U eq "NO")) {$fragmentfile = 0;}
else { $fragmentfile = 1; } # Want to create list of possible protein fragments and fusions? 0 = NO, 1 = YES [DEFAULT = YES]
}
if ($opt_B) {
if (($opt_B eq "Y") or ($opt_B eq "y") or ($opt_B eq "Yes") or ($opt_B eq "yes") or ($opt_B eq "YES")) {$pairwisefile = 1;}
elsif (($opt_B eq "N") or ($opt_B eq "n") or ($opt_B eq "No") or ($opt_B eq "no") or ($opt_B eq "NO")) {$pairwisefile = 0;}
else { $pairwisefile = 1; } # Want to create list of pairwise similarity scores? 0 = NO, 1 = YES [DEFAULT = YES]
}
if ( $opt_A ) {
if(($opt_A eq "Y") or ($opt_A eq "y") or ($opt_A eq "Yes") or ($opt_A eq "yes") or ($opt_A eq "YES")) {$writeparalogs = 1;}
elsif (($opt_A eq "N") or ($opt_A eq "n") or ($opt_A eq "No") or ($opt_A eq "no") or ($opt_A eq "NO")) {$writeparalogs = 0;}
else { $writeparalogs = 1; } # Want to create a list of paralogs? 0 = NO, 1 = YES [DEFAULT = YES]
}
if ( $opt_C ) {
if(($opt_C eq "Y") or ($opt_C eq "y") or ($opt_C eq "Yes") or ($opt_C eq "yes") or ($opt_C eq "YES")) {$writeclusterweights = 1;}
elsif (($opt_C eq "N") or ($opt_C eq "n") or ($opt_C eq "No") or ($opt_C eq "no") or ($opt_C eq "NO")) {$writeclusterweights = 0;}
else { $writeclusterweights = 1; } # Want to create a list of cluster weights? 0 = NO, 1 = YES [DEFAULT = YES]
}
#process percent of representation in cluster for adjacency matrix into cluster_levels array
if ($opt_c) {
$compute_adjacency = 1;
@cluster_levels = split(',', $opt_c);
foreach my $level (@cluster_levels) {
if ($DEBUG) {
print STDERR "calc_adjacency level $level\n";
}
if (!(looks_like_number($level))) {
die ("ERROR: $level is not a number in list of adjacency matrix cluster representation values (-c)!\n");
}
if (($level > 100) || ($level < 0)) {
die ("ERROR: $level is not between 0-100 in list of adjacency matrix cluster representation values (-c)!\n");
}
}
}
#process percent of representation in cluster for BSR distance matrices into BSR_cluster_levels array
if ($opt_e) {
@BSR_cluster_levels = split(',', $opt_c);
foreach my $level (@BSR_cluster_levels) {
if ($DEBUG) {
print STDERR "BSR level $level\n";
}
if (!(looks_like_number($level))) {
die ("ERROR: $level is not a number in list of BSR matrix cluster representation values (-e)!\n");
}
if (($level > 100) || ($level < 0)) {
die ("ERROR: $level is not between 0-100 in list of BSR matrix cluster representation values (-e)!\n");
}
}
} else {
$BSR_cluster_levels[0] = 0;
$BSR_cluster_levels[1] = 90;
$BSR_cluster_levels[2] = 100;
}
my %query_evalue_cutoff = ();# Key = query id, Value = query specific evalue cutoff for low scoring queries
my @paralog_cutoff = (); # Index1 = genome tag index, Index2 = genome tag index, Value = [0,1] normalized blast score cutoff for paralogs between these two genomes
my @ave_per_id = (); # Index1 = genome tag index, Index2 = genome tag index, Value = [0,100] average percent identity for high quality orthologs between these two genomes
my @mean_max_BSR = (); # Index1 = genome tag index, Index2 = genome tag index, Value = [0,1] mean max BSR for high quality orthologs between these two genomes
my %genome_hash = (); # Key1 = genome tag, Key2 = asmbl_id, Value = array of protein ids sorted by end5 and then end3
my %genome_hash_context = ();# Key1 = genome tag, Key2 = asmbl_id, Key3 = positions in genome_hash that are only for context, Value = 1 just to define the key3
my %feat_hash = (); # Key = feat_name Key2 = struct members with their values
my %relationship_hash = (); # Key1 = Query protein, Key2 = Subject protein, Key3 = struct members with their values
my %Qbytaghash = (); # Key1 = Query protein, Key2 = subject genome tag, Key3 = Subject protein, Key4 = same as relationship_hash Key3 (actually is the same pointer)
my %BestTagMatch = (); # Key1 = Query protein, Key2 = subject genome tag, Value = bit score of best match for this genome
my %SecondBestTagMatch = ();# Key1 = Query protein, Key2 = subject genome tag, Value = bit score of best match for this genome
my %orf_counter = (); # Key = genome tag, Value = orf counts
my %Tagbyfeatnamehash = (); # Key1 = genome tag, Key2 = feat_name (query)
my %TagByPointer = (); # Key = feat_name-tag, value = array position location
my @tag_array = (); # array of genome tags
my %FeatnameLookupTag_hash = ();# Key = feat_name, Value = tag. Needed for input lacking tagged feat_names
my %TagIndex = (); # Key = genome tag, Value = index in tag_array of associated genome tag
my %AssemblyLookup_hash = ();# Generated to store the asmbl_id (value) of each feat_name (key) so we can lookup the correct array during synteny searches
my %clusters = (); # Key = feat_name, Value = array of (hashes of feat_name and genome_tag) that are in a cluster with the feat_name key
my %cluster_number = (); # Key = feat_name, Value = cluster number that feat_name is in
my @cluster_size = (); # Index = cluster number, Value = size of the cluster
my @cluster_for_number =(); # Index = cluster number, Value = cluster structure (array)
my @centroids = (); # Index = cluster number, Value = feature id for the centroid of the cluster
my $minSynMatches = 2; # Hard-code the minimum number of matches to be consider syntenous
my $min_synteny_threshold = 0;# minimum synteny score to trust
my $genome_number; # number of genomes defined in tagfile
#my $outprefix = "$basedir/panoct_"; #previous naming convention
my $outprefix = "$basedir/";
my $btab_style = ""; # 1 = WU, 0 = NCBI
#%relationship_hash stores information for the blast matches
###key1 = query
###key2 = subject
###value -> id = percent identity
### eval = e-value
### score = BLAST bits score
### BSR = BLAST Score Ratio # added 12/13/2010 so that the true top matches were chosen, not the Blast score
### best = 1 if best blast match in the subject genome, otherwise 0
### bibest = 1 if best bidirectional blast match in the subject genome, otherwise 0
### min_query = smaller match coordinate on the query protein
### max_query = larger match coordinate on the query protein
### min_sub = smaller match coordinate on the subject protein
### max_sub = larger match coordinate on the subject protein
### clique_top = number in the clique at the top of blast matches
### clique_all = number in the clique for all blast matches
sub print_parameters { # print the parameters input via command line or set as default
my ($outfile) = @_;
print $outfile "$prog $commandline\n";
print $outfile "version = $version\n";
print $outfile "microarray = $microarray\n";
print $outfile "hitsfile = $hitsfile\n";
print $outfile "histogramfile = $histogramfile\n";
print $outfile "strict_orthos = $strict_orthos\n";
print $outfile "normalizefile = $normalizefile\n";
print $outfile "writeparalogs = $writeparalogs\n";
print $outfile "fragmentfile = $fragmentfile\n";
print $outfile "pairwisefile = $pairwisefile\n";
print $outfile "print_cluster_numbers = $print_cluster_numbers\n";
print $outfile "vennfile = $vennfile\n";
print $outfile "compute_centroids = $compute_centroids\n";
print $outfile "ANCHOR_window_size = $ANCHOR_window_size\n";
print $outfile "ANCHOR_cutoff = $ANCHOR_cutoff\n";
print $outfile "DEBUG = $DEBUG\n";
print $outfile "CGN_window_size = $CGN_window_size\n";
print $outfile "frameshiftfile = $frameshiftfile\n";
print $outfile "max_missing_aa = $max_missing_aa\n";
print $outfile "frames_link_thresh = $frames_link_thresh\n";
print $outfile "basedir = $basedir\n";
print $outfile "btabpath = $btabpath\n";
print $outfile "pep_path = $pep_path\n";
print $outfile "att_file = $att_file\n";
print $outfile "pep_file = $pep_file\n";
print $outfile "btabfile = $btabfile\n";;
print $outfile "tagfile = $tagfile\n";
print $outfile "percentid = $percentid\n";
print $outfile "fspercentid = $fspercentid\n";
print $outfile "evalue = $evalue\n";
print $outfile "min_hit_length = $min_hit_length\n";
print $outfile "writeclusterweights $writeclusterweights\n";
print $outfile "compute_adjacency $compute_adjacency\n";
print $outfile "process_clusters $process_clusters\n";
print $outfile "cluster_input_file $cluster_input_file\n";
my $cl_levs = join (" ", @cluster_levels);
print $outfile "compute_adjacency cluster_levels $cl_levs\n";
$cl_levs = join (" ", @BSR_cluster_levels);
print $outfile "BSR_cluster_levels $cl_levs\n";
}
sub get_tags { # obtain list of genomes to compare
my %temp_hash = ();
$genome_number = 0; # total number of genomes to be processed
open (my $infile, "<", "$basedir/$tagfile") || die ("ERROR: can't open file $basedir/$tagfile\n");
while (<$infile>) {
chomp;
if (length($_) > 0) {
my($name,$location,$asmbl_id) = split(/\t/,$_);
$name =~ s/\s+$//;
if (defined $temp_hash{$name}) {
die ("ERROR: You have more than one occurance of $name in $basedir/$tagfile!\n");
} else {
$temp_hash{$name} = 1;
push (@tag_array, $name); # populate the tag_array in the order of the tagfile (1st tag is the reference tag)
$genome_number++;
if ($genome_number == 1) {
print STDERR "$name [Reference]\n";
} else {
print STDERR " $name\n";
}
}
}
}
close($infile);
my $index = 0;
foreach my $tag (@tag_array) {
$TagIndex{$tag} = $index++
}
}
sub get_protein_info { #formerly get_fasta_headers. Added calculation of protein length for ncbi data 12/08/10
my @line = ();
my $id;
my $title = "";
my $sequence = "";
my $length = "";
unless (open (PEPFILE, "<$pep_path/$pep_file") ) {
die ("can't open file $pep_path/$pep_file.\n");
}
my ($save_input_separator) = $/;
$/="\n>";
while (<PEPFILE>) {
($title,$sequence) = /^>?\s*(.*)\n([^>]+)>?/; # split the header line and sequence (very cool)
@line = split(/\s+/, $title); # split the scalar $line on space or tab (to separate the identifier from the header and store in array @fasta
$id = $line[0]; # unique orf identifier is in column 0, com_name is in rest
$id =~ s/>//;
$sequence =~ s/[^a-zA-Z]//g; # remove any non-alphabet letters
$length = length($sequence);
$feat_hash{$id}->{'header'} = join(' ', @line[1..$#line]); # put the identifier into the hash as the "key" and the header as value "header" (joining all columns after first space/tab)
$feat_hash{$id}->{'length'} = $length;
$feat_hash{$id}->{'sequence'} = $sequence;
#print STDERR "$id ($feat_hash{$id}->{'header'}) = $feat_hash{$id}->{'length'}\n";
$title = ""; # clear the title for the next round.
$sequence = ""; #clear out the sequence for the next round.
}
$/ = $save_input_separator; # restore the input separator
close (PEPFILE);
return;
}
sub get_gene_att {
my $pos = "";
my $tag = "";
my $end5 = "";
my $end3 = "";
my $asmbl_id = "";
my $feat_name = "";
my $anno = "";
my $failed = 0;
unless (open (ATTFILE, "<$basedir/$att_file") ) {
die ("ERROR: can not open file $basedir/$att_file.\n");
}
while (<ATTFILE>) {
my @att_line = ();
chomp;
@att_line = split(/\t/, $_); # split the scalar $line on tab
$asmbl_id = $att_line[0];
if ($asmbl_id eq "") {
print STDERR "ERROR: assembly id/contig id must not be empty/null in the gene attribute file\n$_\n";
$failed = 1;
}
$feat_name = $att_line[1];
if (defined $feat_hash{$feat_name}->{'full'}) {
print STDERR "ERROR: $feat_name appears more than once in the gene attribute file!\n";
$failed = 1;
}
$end5 = $att_line[2];
$end3 = $att_line[3];
$anno = $att_line[4];
$tag = $att_line[5];
if (!defined $TagIndex{$tag}) {
print STDERR "ERROR: $tag is a genome tag in the gene attribute file but not in the genome tag file!\n";
$failed = 1;
}
$feat_hash{$feat_name}->{'full'} = 0; # initialize number of full length blast matches to 0
$feat_hash{$feat_name}->{'fragment'} = 0; #initialize fragment blast matches to 0
$feat_hash{$feat_name}->{'fusion'} = 0; #initialize fusion blast matches to 0
$feat_hash{$feat_name}->{'retained'} = 0; #initialize retained frameshift flag to 0/false
$FeatnameLookupTag_hash{$feat_name} = $tag;
push (@{ $genome_hash{$tag}{$asmbl_id} }, $feat_name);
$AssemblyLookup_hash{$feat_name} = $asmbl_id; # new 01/12/2011
$feat_hash{$feat_name}->{'end5'} = $end5;
$feat_hash{$feat_name}->{'end3'} = $end3;
if ($end5 < $end3) {
$feat_hash{$feat_name}->{'orient'} = 1;
} else {
$feat_hash{$feat_name}->{'orient'} = -1;
}
if (($feat_name !~ /^CONTEXT[0-9]+:/) && !defined $feat_hash{$feat_name}->{'header'}) {
print STDERR "ERROR: $feat_name in the gene attribute file ($att_file) was not found in the protein file ($pep_file)!\n";
$failed = 1;
}
if ($anno) { # check if there is any annotation from gene_att file
$feat_hash{$feat_name}->{'header'} = $anno;
}
print STDERR "$feat_name $feat_hash{$feat_name}->{'header'} $feat_hash{$feat_name}->{'end5'} $feat_hash{$feat_name}->{'end3'} $feat_hash{$feat_name}->{'length'} $feat_hash{$feat_name}->{'orient'} $tag $asmbl_id\n" if ($DEBUG);
}
close (ATTFILE);
foreach my $feat_id (keys %feat_hash) {
if ($feat_id =~ /^CONTEXT[0-9]+:(.*)$/) {
my $real_feat_id = $1;
if (!defined $feat_hash{$real_feat_id}) {
print STDERR "ERROR: $real_feat_id did not appear in the gene attribute file but $feat_id did!\n";
$failed = 1;
}
} elsif (!defined $feat_hash{$feat_id}->{'end5'}) {
print STDERR "ERROR: $feat_id appears in the protein file ($pep_file) but not in the gene attribute file ($att_file)!\n";
$failed = 1;
}
}
if ($failed) {
exit(1);
}
foreach $tag (keys %genome_hash) {
foreach $asmbl_id (keys %{ $genome_hash{$tag} }) {
my $sort_by_end5_end3 = sub {
my $end5a = $feat_hash{$a}->{'end5'};
my $end5b = $feat_hash{$b}->{'end5'};
my $end3a = $feat_hash{$a}->{'end3'};
my $end3b = $feat_hash{$b}->{'end3'};
my $mida = ($end5a + $end3a) / 2;
my $midb = ($end5b + $end3b) / 2;
if ($mida <=> $midb) {
return ($mida <=> $midb);
} elsif ($end5a <=> $end5b) {
return ($end5a <=> $end5b);
} elsif ($end3a <=> $end3b) {
return ($end3a <=> $end3b);
} else {
die ("SORRY $a and $b have the same end5 ($end5a) and end3 ($end3a), please correct the gene_att file!\n");
}
};
@{ $genome_hash{$tag}{$asmbl_id} } = sort $sort_by_end5_end3 ( @{ $genome_hash{$tag}{$asmbl_id} } );
for my $index (0 .. $#{ $genome_hash{$tag}{$asmbl_id} }) {
my $feat_name = $genome_hash{$tag}{$asmbl_id}->[$index];
if ($feat_name =~ /^CONTEXT[0-9]+:(.*)$/) {
$genome_hash{$tag}{$asmbl_id}->[$index] = $1;
$genome_hash_context{$tag}{$asmbl_id}->{$index} = 1;
delete $feat_hash{$feat_name};
print STDERR "CONTEXT: $tag $asmbl_id $index = $feat_name ($1)\n" if ($DEBUG);
} else {
$TagByPointer{$feat_name} = $index;
print STDERR "$tag $asmbl_id $index = $feat_name\n" if ($DEBUG);
}
}
}
}
return;
}
sub rebuild_genome_hash {
#rebuild %genome_hash, %genome_hash_context, and %TagByPointer after deleting any features/genes/proteins
foreach my $tag (keys %genome_hash) {
foreach my $asmbl_id (keys %{ $genome_hash{$tag} }) {
my $cur_index = 0;
my $skip_index = 0;
foreach my $feat_id (@{ $genome_hash{$tag}{$asmbl_id} }) {
if (!defined $feat_hash{$feat_id}) {
$skip_index++;
} else {
if ($cur_index < $skip_index) {
if ((defined $genome_hash_context{$tag}{$asmbl_id}->{$cur_index}) && (!defined $genome_hash_context{$tag}{$asmbl_id}->{$skip_index})) {
delete $genome_hash_context{$tag}{$asmbl_id}->{$cur_index};
}
if ((!defined $genome_hash_context{$tag}{$asmbl_id}->{$cur_index}) && (defined $genome_hash_context{$tag}{$asmbl_id}->{$skip_index})) {
$genome_hash_context{$tag}{$asmbl_id}->{$cur_index} = 1;
}
$genome_hash{$tag}{$asmbl_id}->[$cur_index] = $genome_hash{$tag}{$asmbl_id}->[$skip_index];
if (!defined $genome_hash_context{$tag}{$asmbl_id}->{$skip_index}) {
$TagByPointer{$feat_id} = $cur_index;
print STDERR "$tag $asmbl_id $feat_id $cur_index $skip_index\n" if ($DEBUG);
}
}
$cur_index++;
$skip_index++;
}
}
$cur_index--;
$#{ $genome_hash{$tag}{$asmbl_id} } = $cur_index;
$cur_index++;
while ($cur_index < $skip_index) {
if (defined $genome_hash_context{$tag}{$asmbl_id}->{$cur_index}) {
delete $genome_hash_context{$tag}{$asmbl_id}->{$cur_index};
}
$cur_index++;
}
}
}
return;
}
sub select_max_scores_from_btab { # get tab-delimited BLAST results in either WUBLAST or NCBI -m8/-m9 formats
## new code - test for btab format (WUbtab or ncbi m8/m9)
my @btab_line = (); # array variable to store split btab lines
my $qid = ""; # query id
my $sid = ""; # subject id (from database)
my $qtag; # query genome tag
my $stag; # subject genome tag
my $score = ""; # BLAST bit score
open (my $infile, "<", "$btabpath/$btabfile") || die ("ERROR: can't open file $btabpath/$btabfile\n");
# this while look is interrogate the file to determine which tabular output style it is
while (<$infile>) {
chomp;
if (!/^#/) { # don't look at REMs
@btab_line = split(/\t/);
last; # we have out info, break the loop
}
}
close($infile);
if ($#btab_line >= "19") { # adjusted because Badgers btab.pl script only goes to e-value or perl column 19
print STDERR "Detected WUBLAST-style btab file ...\n";
$btab_style = 1; # WU
}
elsif ($#btab_line == "11") {
print STDERR "NCBI BLAST (-m 8 or -m 9) option btab file detected ...\n";
$btab_style = 0; # NCBI
}
else {
die ("ERROR: BLAST data must be either WUBLAST btab or NCBI BLAST -m8 or -m9 formats.\n");
}
### process BLAST results ###
open ($infile, "<", "$btabpath/$btabfile");
while (<$infile>) {
chomp;
@btab_line = split(/\t/);
if ($btab_style) { # WU
$score = $btab_line[12];
$qid = $btab_line[0];
$sid = $btab_line[5];
} else { # NCBI
if ($btab_line[0] =~ /^#/) { next;} # skip the lines beginning with #
$score = $btab_line[11];
$qid = $btab_line[0];
$sid = $btab_line[1];
}
if (!defined($qid) || !defined $feat_hash{$qid}) {print STDERR "WARNING!!! $qid is a feature identifier in the btab file but not in the gene attribute file skipping this entry!\n" if ($DEBUG); next;}
if (!defined($sid) || !defined $feat_hash{$sid}) {print STDERR "WARNING!!! $sid is a feature identifier in the btab file but not in the gene attribute file skipping this entry!\n" if ($DEBUG); next;}
$qtag = $FeatnameLookupTag_hash{$qid};
$stag = $FeatnameLookupTag_hash{$sid};
if (!defined($qtag) || !defined $TagIndex{$qtag}) {print STDERR "WARNING!!! $qtag is a genome tag in the btab file but not in the genome tag file skipping this entry!\n" if ($DEBUG); next;}
if (!defined($stag) || !defined $TagIndex{$stag}) {print STDERR "WARNING!!! $stag is a genome tag in the btab file but not in the genome tag file skipping this entry!\n" if ($DEBUG); next;}
print STDERR "$qtag $qid $stag $sid $score\n" if ($DEBUG);
if (!defined $BestTagMatch{$qid}{$stag}) { # this assumes that the best Blast match for a query in a given subject genome appears first in the file
$BestTagMatch{$qid}->{$stag} = $score;
} else {
if (!defined $SecondBestTagMatch{$qid}{$stag}) { # this assumes that the second best Blast match for a query in a given subject genome appears second in the file
$SecondBestTagMatch{$qid}->{$stag} = $score;
}
if ($score > $BestTagMatch{$qid}->{$stag}) { # correct Best scores if sort assumption is violated
$SecondBestTagMatch{$qid}->{$stag} = $BestTagMatch{$qid}->{$stag};
$BestTagMatch{$qid}->{$stag} = $score;
} elsif ($score > $SecondBestTagMatch{$qid}->{$stag}) {
$SecondBestTagMatch{$qid}->{$stag} = $score;
}
}
#adding in recipricol direction in case blast results lack symmetry as they sometimes do
#this will violate the sort assumptions but we can correct for that
#this may mask problems with the all against all having missing data
if (!defined $BestTagMatch{$sid}{$qtag}) {
$BestTagMatch{$sid}->{$qtag} = $score;
} else {
if (!defined $SecondBestTagMatch{$sid}{$qtag}) {
$SecondBestTagMatch{$sid}->{$qtag} = $score;
}
if ($score > $BestTagMatch{$sid}->{$qtag}) { # correct Best scores if sort assumption is violated
$SecondBestTagMatch{$sid}->{$qtag} = $BestTagMatch{$sid}->{$qtag};
$BestTagMatch{$sid}->{$qtag} = $score;
} elsif ($score > $SecondBestTagMatch{$sid}->{$qtag}) {
$SecondBestTagMatch{$sid}->{$qtag} = $score;
}
}
}
close ($infile);
}
sub select_data_from_btab { # get tab-delimited BLAST results in either WUBLAST or NCBI -m8/-m9 formats
## new code - test for btab format (WUbtab or ncbi m8/m9)
my @btab_line = (); # array variable to store split btab lines
my $qmatch_length = ""; # stores the query length
my $smatch_length = ""; # stores the subject length
my $qid = ""; # query id
my $sid = ""; # subject id (from database)
my $qtag; # query genome tag
my $stag; # subject genome tag
my $qbegin = ""; # start query
my $qend = ""; # end query
my $sbegin = ""; # start subject
my $send = ""; # end subject
my $pid = ""; # percent identity
my $evlu = ""; # e-value
my $score = ""; # BLAST bit score
my $qlength = ""; # size of query protein sequence
my $slength = ""; # size of subject (database match) protein sequence
open (my $infile, "<", "$btabpath/$btabfile") || die ("ERROR: can't open file $btabpath/$btabfile\n");
### process BLAST results ###
open ($infile, "<", "$btabpath/$btabfile");
while (<$infile>) {
chomp;
@btab_line = split(/\t/);
# same variables for both btab styles
$qbegin = $btab_line[6];
$qend = $btab_line[7];
$sbegin = $btab_line[8];
$send = $btab_line[9];
if ($btab_style) { # WU
# adjusted because Badgers WU btab.pl script only goes to e-value or perl column 19
# ========================================================
# btab output for WUBLAST output
# column number Description (for Perl), add 1 for Unix
# 0 Query Sequence Name
# 1 Date of the Analysis
# 2 Query Sequence Length
# 3 Search Method -- Blast family application name
# 4 Database Name
# 5 Subject Sequence Name -- Database entry name
# 6 Start of alignment on query (5' nucleotide match in query)
# 7 End of alignment on query (3' nucleotide match in query)
# 8 Start of alignment on subject (5' nucleotide match in db hit)
# 9 End of alignment on subject (3' nucleotide match in db hit)
# 10 % Identity
# 11 % Similarity
# 12 Score (bits)
# 13 File Offset for Beginning of Alignment
# 14 File Offset for End of Alignment
# 15 Description (annotatioon)
# 16 Frame -- 1 through 6, or NULL
# 17 Query Strand -- Plus, Minus or NULL
# 18 DB sequence length
# 19 Expect -- expected value
# 20 P-Value -- Poisson ratio
# ========================================================
# propigate variables for WUBLAST
$qid = $btab_line[0];
$sid = $btab_line[5];
$pid = $btab_line[10];
$evlu = $btab_line[19];
$score = $btab_line[12];
} else { # NCBI
# ========================================================
# btab output from NCBI blastn (-m 8) option:
# column number Description (for Perl), add 1 for Unix
# 0 Query_id
# 1 subject_id (Hit from db)
# 2 % Identity
# 3 length of alignment
# 4 number or mismatches
# 5 number of gaps
# 6 start of alignment on query (5' nucleotide match in query)
# 7 end of alignment on query (3' nucleotide match in query)
# 8 start of alignment on subject (5' nucleotide match in db hit)
# 9 end of alignment on subject (3' nucleotide match in db hit)
# 10 e-value
# 11 score (bits)
# ========================================================
# propigate variables for NCBI m8/m9
if ($btab_line[0] =~ /^#/) { next;} # skip the lines beginning with #
$qid = $btab_line[0];
$sid = $btab_line[1];
$pid = $btab_line[2];
$evlu = $btab_line[10];
$score = $btab_line[11];
}
### Generic processing ###
print STDERR "$qid $sid $score\n" if ($DEBUG);
if (!defined($qid) || !defined $feat_hash{$qid}) {print STDERR "WARNING!!! $qid is a feature identifier in the btab file but not in the gene attribute file skipping this entry!\n" if ($DEBUG); next;}
if (!defined($sid) || !defined $feat_hash{$sid}) {print STDERR "WARNING!!! $sid is a feature identifier in the btab file but not in the gene attribute file skipping this entry!\n" if ($DEBUG); next;}
$qtag = $FeatnameLookupTag_hash{$qid};
$stag = $FeatnameLookupTag_hash{$sid};
if (!defined($qtag) || !defined $TagIndex{$qtag}) {print STDERR "WARNING!!! $qtag is a genome tag in the btab file but not in the genome tag file skipping this entry!\n" if ($DEBUG); next;}
if (!defined($stag) || !defined $TagIndex{$stag}) {print STDERR "WARNING!!! $stag is a genome tag in the btab file but not in the genome tag file skipping this entry!\n" if ($DEBUG); next;}
$qlength = $feat_hash{$qid}->{'length'};
$slength = $feat_hash{$sid}->{'length'};
$qmatch_length = ((abs($qbegin - $qend) + 1)/$qlength)*100;
$smatch_length = ((abs($sbegin - $send) + 1)/$slength)*100;
if (!$feat_hash{$qid}->{'bit'}) {
$feat_hash{$qid}->{'bit'} = 1; # used to determine number of orfs processed
$orf_counter{$qtag}->{'raw'}++; # increment the total orf counter (we will get the total # of orfs this way)
}
if (!defined $relationship_hash{$qid}) { # this assumes that the best Blast match (presumably to itself) for a query appears first in the file
$query_evalue_cutoff{$qid} = $evlu * 10.0;
}
if ($pid < $percentid) {
print STDERR "Skipping %id $pid < $percentid Query: $qid X Subject $sid\n" if ($DEBUG);
next;
}
if (($evlu > $evalue) && ($evlu > $query_evalue_cutoff{$qid})) {
print STDERR "Skipping $evlu > $evalue Query: $qid X Subject $sid = $pid\n" if ($DEBUG);
next;
}
if (($qmatch_length < $min_hit_length) || ($smatch_length < $min_hit_length)) {
print STDERR "Skipping match length $qmatch_length , $smatch_length < $min_hit_length Query: $qid X Subject $sid = $pid\n" if ($DEBUG);
next;
}
#if (($qmatch_length < 50.0) && ($smatch_length < 50.0)) { # the match length must be greater than 1/2 the length of the shorter protein to ignore domain matches
#print STDERR "Skipping match length $qmatch_length , $smatch_length both < 50.0 Query: $qid X Subject $sid = $pid\n" if ($DEBUG);
#next;
#} Didn't help - but did hurt finding some valid fragments
if (!defined $BestTagMatch{$qid}{$stag}) {
print STDERR "ERROR: $qid and genome $stag had no score the first time through!\n";
exit(1);
}
if ((0.8 * $BestTagMatch{$qid}->{$stag}) > $score) {
print STDERR "Skipping score $score < 80% of max $BestTagMatch{$qid}->{$stag} Query: $qid X Subject $sid = $pid\n" if ($DEBUG);
next; #do not keep matches scoring less than 80% of the best score
}
if (defined $relationship_hash{$qid} && defined $relationship_hash{$qid}{$sid}) {
if ($relationship_hash{$qid}{$sid}->{'score'} >= $score) { # ignore lower scores for the same two proteins
next;
}
} else {
$Qbytaghash{$qid}{$stag}->{$sid} = $relationship_hash{$qid}{$sid} = {}; #have Qbytaghash and relationship_hash reference the same hash
}
# make sure $qbegin is smaller than $qend and $sbegin is smaller than $send
my $temp_val;
if ($qbegin > $qend) { #swap
$temp_val = $qend;
$qend = $qbegin;
$qbegin = $temp_val;
}
if ($sbegin > $send) { #swap
$temp_val = $send;
$send = $sbegin;
$sbegin = $temp_val;
}
$relationship_hash{$qid}{$sid}->{'id'} = $pid;
$relationship_hash{$qid}{$sid}->{'eval'} = $evlu;
$relationship_hash{$qid}{$sid}->{'score'} = $score;
$relationship_hash{$qid}{$sid}->{'min_query'} = $qbegin;
$relationship_hash{$qid}{$sid}->{'max_query'} = $qend;
$relationship_hash{$qid}{$sid}->{'min_sub'} = $sbegin;
$relationship_hash{$qid}{$sid}->{'max_sub'} = $send;
$relationship_hash{$qid}{$sid}->{'best'} = 0;
$relationship_hash{$qid}{$sid}->{'bibest'} = 0;
$relationship_hash{$qid}{$sid}->{'synbest'} = 0;
$relationship_hash{$qid}{$sid}->{'synbibest'} = 0;
$relationship_hash{$qid}{$sid}->{'CGN_bibest'} = 0;
$relationship_hash{$qid}{$sid}->{'full'} = 0;
$relationship_hash{$qid}{$sid}->{'anchor'} = 0;
$relationship_hash{$qid}{$sid}->{'extend'} = 0;
print STDERR "Query: $qid X Subject $sid = $relationship_hash{$qid}{$sid}->{'id'}\n" if ($DEBUG);
#determine full length indicating blast matches
if ($pid >= $fspercentid) { # only use matches above the defined percent identity cutoff
if (($qbegin <= $max_missing_aa + 1) && ($qend + $max_missing_aa >= $feat_hash{$qid}->{'length'}) && ($qmatch_length >= 80)) { # covers full length of query
if (($sbegin <= $max_missing_aa + 1) && ($send + $max_missing_aa >= $feat_hash{$sid}->{'length'}) && ($smatch_length >= 80)) { #covers full length of subject
$feat_hash{$qid}->{'full'}++; #not a protein fragment if almost a full length match
$feat_hash{$sid}->{'full'}++; #this is also true for the subject but we may double count
$relationship_hash{$qid}{$sid}->{'full'} = 1;
}
}
}
if (!defined $Tagbyfeatnamehash{$qtag}{$qid}) {
$Tagbyfeatnamehash{$qtag}{$qid} = 1;
$orf_counter{$qtag}->{'used'}++;
} elsif ($Tagbyfeatnamehash{$qtag}{$qid} == 0) { #if qid had only been seen as a target (sid) before change to seen as a query (qid)
$Tagbyfeatnamehash{$qtag}{$qid} = 1;
}
if (!defined $Tagbyfeatnamehash{$stag}{$sid}) {
$Tagbyfeatnamehash{$stag}{$sid} = 0;
$orf_counter{$stag}->{'used'}++;
}
}
close ($infile);
open (OUTMISSING, ">$outprefix" . "missing_blast_results.txt");
my $changed = 0;
foreach my $qid (keys %feat_hash) { # go through all featnames
my $qtag = $FeatnameLookupTag_hash{$qid};
if (!defined $Tagbyfeatnamehash{$qtag}{$qid}) {#output feat_names in the attribute file but missing from the blast results, and cleanup hashes to ignore these proteins
$changed = 1;
print OUTMISSING "$qtag:$qid\n";
delete $feat_hash{$qid};
delete $FeatnameLookupTag_hash{$qid};
delete $AssemblyLookup_hash{$qid};
delete $TagByPointer{$qid};
} elsif ($Tagbyfeatnamehash{$qtag}{$qid} == 0) { #if qid had only been seen as a target
print OUTMISSING "$qtag:$qid only search result\n";
}
}
if ($changed) {
&rebuild_genome_hash; #need to remove the deleted features/genes/proteins
}
close (OUTMISSING);
print STDERR "check that TagByPointer is consistent\n" if ($DEBUG);
#check that TagByPointer is consistent
foreach $qtag (@tag_array) { # start looping through genomes by order in tag file
foreach $qid (keys %{ $Tagbyfeatnamehash{$qtag} } ) { # go through featnames of each genome to look for matches in other genomes
foreach $stag (keys %{ $Qbytaghash{$qid} } ) {# if query protein matches anything in subject genome, lets drill through each match
foreach $sid (keys %{ $Qbytaghash{$qid}{$stag} } ) {
my $qasmbl_id = $AssemblyLookup_hash{$qid};
if (!defined $qasmbl_id) {
die ("ERROR: asmbl_id undefined for $qtag $qid ($stag $sid)\n");
}
my $sasmbl_id = $AssemblyLookup_hash{$sid};
if (!defined $sasmbl_id) {
die ("ERROR: asmbl_id undefined for ($qtag $qid) $stag $sid\n");
}
my $qindex = $TagByPointer{$qid};
if (!defined $qindex) {
die ("ERROR: TagByPointer undefined for $qtag $qid ($stag $sid)\n");
}
my $sindex = $TagByPointer{$sid};
if (!defined $sindex) {
die ("ERROR: TagByPointer undefined for ($qtag $qid) $stag $sid\n");
}
if ($genome_hash{$qtag}{$qasmbl_id}->[$qindex] ne $qid) {
die ("ERROR: Inconsistent $genome_hash{$qtag}{$qasmbl_id}->[$qindex] $qid for $qtag $qid ($stag $sid)\n");
}
if ($genome_hash{$stag}{$sasmbl_id}->[$sindex] ne $sid) {
die ("ERROR: Inconsistent $genome_hash{$stag}{$sasmbl_id}->[$sindex] $sid for ($qtag $qid) $stag $sid\n");
}
}
}
}
}
#check that BestMatchTag and SecondBestMatchTag are consistent
#also determine if self-hit exists and create self-hit record if not, no score should be above the self-hit so reset them
my $cum_num_qids = 0; #calculate average number of proteins in a genome
foreach my $qtag (@tag_array) { # start looping through genomes by order in tag file
my $num_qids = 0;
my $num_no_selfhits = 0;
foreach my $qid ( keys %{ $Tagbyfeatnamehash{$qtag} } ) { # go through featnames of query genome to look for matches in other genomes
$num_qids++;
my $max_score = 0;
my $selfhit = 1;
my $selfhit_score;
if (defined $relationship_hash{$qid} && defined $relationship_hash{$qid}{$qid}) {
$selfhit_score = $relationship_hash{$qid}{$qid}->{'score'};
if (!defined $BestTagMatch{$qid}{$qtag}) {
die ("ERROR: selfhit was found previously but not here!\n");
}
} else {
print STDERR "WARNING: no self hit Blast score for $qtag : $qid\n" if ($DEBUG);
$selfhit = 0;
$num_no_selfhits++;
}
if (!defined $BestTagMatch{$qid}) {
die ("ERROR: no Blast scores for $qtag : $qid when some were found in previous loop\n");
}
# Find maximum blast score to use for selfhit score if there is no selfhit score
if (!$selfhit) {
foreach my $stag (@tag_array) { # loop through all genomes
if (defined $BestTagMatch{$qid}{$stag} && ($BestTagMatch{$qid}{$stag} > $max_score)) {
$max_score = $BestTagMatch{$qid}{$stag};
}
}
$selfhit_score = $max_score;
}
foreach my $stag (@tag_array) { # loop through all genomes
if (defined $SecondBestTagMatch{$qid}{$stag} && !defined $BestTagMatch{$qid}{$stag}) {
die ("ERROR: Second Best Blast score for $qtag : $qid to genome $stag exists but not Best!\n");
}
if (!defined $BestTagMatch{$qid}{$stag}) {
next;
}
if ($BestTagMatch{$qid}{$stag} > $selfhit_score) {
$BestTagMatch{$qid}{$stag} = $selfhit_score;
if (defined $SecondBestTagMatch{$qid}{$stag} && ($SecondBestTagMatch{$qid}{$stag} > $selfhit_score)) {
$SecondBestTagMatch{$qid}{$stag} = $selfhit_score;
}
}
}
if (!$selfhit) {
$BestTagMatch{$qid}{$qtag} = $max_score;
if ((defined $Qbytaghash{$qid}{$qtag}->{$qid}) || (defined $relationship_hash{$qid}{$qid})) {
die ("ERROR: selfhit defined here ($qtag $qid) but not above\n");
}
$Qbytaghash{$qid}{$qtag}->{$qid} = $relationship_hash{$qid}{$qid} = {}; #have Qbytaghash and relationship_hash reference the same hash
$relationship_hash{$qid}{$qid}->{'score'} = $max_score;
$relationship_hash{$qid}{$qid}->{'id'} = 100;
$relationship_hash{$qid}{$qid}->{'eval'} = 0;
$relationship_hash{$qid}{$qid}->{'min_query'} = 1;
$relationship_hash{$qid}{$qid}->{'max_query'} = $feat_hash{$qid}->{'length'};
$relationship_hash{$qid}{$qid}->{'min_sub'} = 1;
$relationship_hash{$qid}{$qid}->{'max_sub'} = $feat_hash{$qid}->{'length'};
$relationship_hash{$qid}{$qid}->{'best'} = 0;
$relationship_hash{$qid}{$qid}->{'bibest'} = 0;
$relationship_hash{$qid}{$qid}->{'synbest'} = 0;
$relationship_hash{$qid}{$qid}->{'synbibest'} = 0;
$relationship_hash{$qid}{$qid}->{'CGN_bibest'} = 0;
$relationship_hash{$qid}{$qid}->{'full'} = 0;
$relationship_hash{$qid}{$qid}->{'anchor'} = 0;
$relationship_hash{$qid}{$qid}->{'extend'} = 0;
}
}
$cum_num_qids += $num_qids;
if ($num_no_selfhits > 0) {
print STDERR "WARNING: Number of missing Blast selfhits for genome $qtag is $num_no_selfhits\n";
}
}
my $ave_num_qids = 0;
if ($genome_number > 0) {
$ave_num_qids = $cum_num_qids / $genome_number;
}
print STDERR "reduce any scores above self score to self score\n" if ($DEBUG);
#reduce any scores above self score to self score
foreach my $qid (keys %feat_hash) { # go through all featnames
if (!defined $relationship_hash{$qid}) {
print STDERR "WARNING: no matches to any other gene for $qid!\n";
next;
}
if (!defined $relationship_hash{$qid}{$qid}) {
die ("ERROR: $qid is missing a selfhit after forcing one!\n");
}
foreach my $sid (keys %{ $relationship_hash{$qid} }) { # go through all featnames
if ($relationship_hash{$qid}{$sid}->{'score'} > $relationship_hash{$qid}{$qid}->{'score'}) {
print STDERR "WARNING ($qid $sid $relationship_hash{$qid}{$sid}->{'score'}) > selfhit $relationship_hash{$qid}{$qid}->{'score'}\n" if ($DEBUG);
$relationship_hash{$qid}{$sid}->{'score'} = $relationship_hash{$qid}{$qid}->{'score'};
}
}
}
#check that genomes have a reasonable amount of proteins
foreach my $qtag (@tag_array) { # start looping through genomes by order in tag file
my $num_qids = 0;
foreach my $qid ( keys %{ $Tagbyfeatnamehash{$qtag} } ) { # go through featnames of query genome
$num_qids++;
}
print STDERR "genome: $qtag has $num_qids valid genes";
if ($num_qids < (0.75 * $ave_num_qids)) {
print STDERR " WARNING!!! This is much lower than the average number of genes $ave_num_qids";
}
print STDERR "\n";
}
}