forked from eead-csic-compbio/get_homologues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_homologues.pl
executable file
·3342 lines (2949 loc) · 118 KB
/
get_homologues.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
# 2021 Bruno Contreras-Moreira (1) and Pablo Vinuesa (2):
# 1: http://www.eead.csic.es/compbio (Estacion Experimental Aula Dei-CSIC/Fundacion ARAID, Spain)
# 2: http://www.ccg.unam.mx/~vinuesa (Center for Genomic Sciences, UNAM, Mexico)
# This program uses BLAST (and DIAMOND/HMMER/Pfam) to define clusters of 'orthologous' ORF/intergenic
# sequences and pan/core-genome gene sets. Several algorithms are available and search parameters
# are customizable. It is designed to process (in a HPC computer cluster) files contained in a
# directory (-d), so that new .faa/.gbk files can be added while conserving previous BLAST results.
# In general the program tries to re-use previous results when run with the same input directory.
$|=1;
use strict;
use warnings;
use Getopt::Std;
use Fcntl qw(:flock);
use File::Temp qw(tempfile);
use File::Basename;
use Benchmark;
use Cwd;
use FindBin '$Bin';
use lib "$Bin/lib";
use lib "$Bin/lib/bioperl-1.5.2_102/";
use HPCluster;
use phyTools; #also imports constants SEQ,NAME used as array subindices
use marfil_homology; # includes $FASTAEXTENSION and other global variables set there such as @taxa
my $VERSION = '3.x';
## settings for local batch blast,hmmscan jobs
my $BATCHSIZE = 100;
## global variables that control some algorithmic choices
my $NOFSAMPLESREPORT = 10; # number of genome samples used for the generation of pan/core genomes
my $MAXEVALUEBLASTSEARCH = 0.01; # required to reduce size of blast output files
my $MAXPFAMSEQS = 250; # default for -m cluster jobs, it is updated to 1000 with -m local
my $MININTERGENESIZE = 200; # minimum length (nts) required for intergenic segments to be considered
my $MAXINTERGENESIZE = 700;
my $INTERGENEFLANKORF = 180; # length in nts of intergene flanks borrowed from neighbor ORFs (/3 for amino acids)
my $PRINTCLUSTERSSCREEN = 0; # whether cluster names should be printed to screen
my $KEEPSCNDHSPS = 1; # by default only the first BLAST hsp is kept for further calculations
my $COMPRESSBLAST = 1; # set to 0 if gzip is not available
my $DISSABLEPRND = 1; # dissable paranoid (PRND, -P) options due to no redistribution license;
## list of features/binaries required by this program (do not edit)
my @FEATURES2CHECK = ('EXE_BLASTP','EXE_BLASTN','EXE_FORMATDB','EXE_MCL','EXE_MAKEHASH',
'EXE_READBLAST','EXE_COGLSE','EXE_COGTRI','EXE_HMMPFAM','EXE_DMNDP','EXE_DMNFT',
'EXE_INPARA','EXE_ORTHO','EXE_HOMOL','EXE_SPLITBLAST','EXE_SPLITHMMPFAM');
if(!$DISSABLEPRND){ push(@FEATURES2CHECK,'EXE_MULTIPARANOID'); }
################################################################
my ($newDIR,$output_mask,$pancore_mask,$include_file,$onlyblast,%included_input_files,%opts) = ('','','',0,0);
my ($exclude_inparalogues,$doMCL,$doPARANOID,$doCOG,$do_PFAM,$reference_proteome_string) = (0,0,0,0,0,0);
my ($inputDIR,$input_FASTA_file,$filter_by_length,$cluster_list_file,$do_intergenic,$do_features) = (0,0);
my ($n_of_cpus,$COGmulticluster,$do_minimal_BDBHs,$do_soft) = ($BLAST_NOCPU,0,0,0);
my ($do_ANIb_matrix,$do_POCP_matrix,$do_diamond) = (0,0,0);
my ($min_cluster_size,$runmode,$do_genome_composition,$saveRAM,$ANIb_matrix_file,$POCP_matrix_file);
my ($evalue_cutoff,$pi_cutoff,$pmatch_cutoff) = ($BLAST_PVALUE_CUTOFF_DEFAULT, # see marfil_homology.pm
$PERCENT_IDENTITY_CUTOFF_DEFAULT,$PERCENT_MATCH_CUTOFF_DEFAULT);
my ($MCLinflation,$neighbor_corr_cutoff) = ($MCL_INFLATION_DEFAULT,$MIN_NEIGHBOR_CORR_DEFAULT);
my ($bitscore_cutoff,$segment_cover_cutoff) = ($MIN_BITSCORE_DEFAULT,$MIN_SEGMENT_COVER_DEFAULT);
my $random_number_generator_seed = 0;
my $pwd = getcwd(); $pwd .= '/';
my $start_time = new Benchmark();
getopts('hvbcesgPADGMpXoxza:f:n:m:d:r:t:i:I:E:S:C:F:N:B:O:R:', \%opts);
if(($opts{'h'})||(scalar(keys(%opts))==0))
{
print "\nusage: $0 [options]\n\n";
print "-h this message\n";
print "-v print version, credits and checks installation\n";
print "-d directory with input FASTA files ( .faa / .fna ), (overrides -i,\n";
print " GenBank files ( .gbk ), 1 per genome, or a subdirectory use of pre-clustered sequences\n".
" ( subdir.clusters / subdir_ ) with pre-clustered sequences ignores -c, -g)\n".
" ( .faa / .fna ); allows for new files to be added later; \n".
" creates output folder named 'directory_homologues'\n";
print "-i input amino acid FASTA file with [taxon names] in headers, ".
"(required unless -d is set)\n".
" creates output folder named 'file_homologues'\n";
print "\nOptional parameters:\n";
print "-o only run BLAST/Pfam searches and exit ".
"(useful to pre-compute searches)\n";
print "-c report genome composition analysis ".
"(follows order in -I file if enforced,\n".
" ".
" ignores -r,-t,-e)\n";
print "-R set random seed for genome composition analysis ".
"(optional, requires -c, example -R 1234,\n".
" ".
" required for mixing -c with -c -a runs)\n";
if(eval{ require DB_File } ) # show only if DB_File is available (it should as it is core)
{
print "-s save memory by using BerkeleyDB; default parsing stores\n".
" sequence hits in RAM\n";
}
print "-m runmode [local|cluster|dryrun] ".
"(default local)\n";
print "-n nb of threads for BLAST/HMMER/MCL in 'local' runmode (default=$n_of_cpus)\n";
print "-I file with .faa/.gbk files in -d to be included ".
"(takes all by default, requires -d)\n";
print "\nAlgorithms instead of default bidirectional best-hits (BDBH):\n";
print "-G use COGtriangle algorithm (COGS, PubMed=20439257) ".
"(requires 3+ genomes|taxa)\n";
print "-M use orthoMCL algorithm (OMCL, PubMed=12952885)\n";
print "-p use PARANOID algorithm (PRND, PubMed=16873526)\n" if(!$DISSABLEPRND);
print "\nOptions that control sequence similarity searches:\n";
print "-X use diamond instead of blastp (optional, set threads with -n)\n";
print "-C min \%coverage in BLAST pairwise alignments ";
print "(range [1-100],default=$PERCENT_MATCH_CUTOFF_DEFAULT)\n";
print "-E max E-value ".
"(default=$BLAST_PVALUE_CUTOFF_DEFAULT,max=$MAXEVALUEBLASTSEARCH)\n";
print "-D require equal Pfam domain composition ".
"(best with -m cluster or -n threads)\n";
print " when defining similarity-based orthology\n";
print "-S min \%sequence identity in BLAST query/subj pairs ".
"(range [1-100],default=$PERCENT_IDENTITY_CUTOFF_DEFAULT [BDBH|OMCL])\n";
print "-N min BLAST neighborhood correlation PubMed=18475320 ".
"(range [0,1],default=$MIN_NEIGHBOR_CORR_DEFAULT [BDBH|OMCL])\n";
print "-b compile core-genome with minimum BLAST searches ".
"(ignores -c [BDBH])\n";
print "\nOptions that control clustering:\n";
print "-t report sequence clusters including at least t taxa ".
"(default t=numberOfTaxa,\n".
" ".
" t=0 reports all clusters [OMCL|COGS])\n";
print "-a report clusters of sequence features in GenBank files ".
"(requires -d and .gbk files,\n";
print " instead of default 'CDS' GenBank features ".
" example -a 'tRNA,rRNA',\n".
" ".
" NOTE: uses blastn instead of blastp,\n".
" ".
" ignores -g,-D)\n";
print "-g report clusters of intergenic sequences flanked by ORFs ".
"(requires -d and .gbk files)\n";
print " in addition to default 'CDS' clusters\n";
print "-f filter by \%length difference within clusters ".
"(range [1-100], by default sequence\n".
" ".
" length is not checked)\n";
print "-r reference proteome .faa/.gbk file ".
"(by default takes file with\n".
" ".
" least sequences; with BDBH sets\n".
" ".
" first taxa to start adding genes)\n";
print "-e exclude clusters with inparalogues ".
"(by default inparalogues are\n".
" ".
" included)\n";
print "-x allow sequences in multiple COG clusters ".
"(by default sequences are allocated\n".
" ".
" to single clusters [COGS])\n";
print "-F orthoMCL inflation value ".
"(range [1-5], default=$MCL_INFLATION_DEFAULT [OMCL])\n";
print "-B min bit-score (in bits) ".
"(default=$MIN_BITSCORE_DEFAULT [PRND])\n" if(!$DISSABLEPRND);
print "-A calculate average identity of clustered sequences, ".
"(optional, creates tab-separated matrix,\n";
print " by default uses blastp results but can use blastn with -a ".
" recommended with -t 0 [OMCL|COGS])\n";
print "-P calculate percentage of conserved proteins (POCP), ".
"(optional, creates tab-separated matrix,\n";
print " by default uses blastp results but can use blastn with -a ".
" recommended with -t 0 [OMCL|COGS])\n";
print "-z add soft-core to genome composition analysis ".
"(optional, requires -c [OMCL|COGS])\n";
print "\n".
" This program uses BLAST (and optionally HMMER/Pfam) to define clusters of 'orthologous'\n".
" genomic sequences and pan/core-genome gene sets. Several algorithms are available\n".
" and search parameters are customizable. It is designed to process (in a HPC computer\n".
" cluster) files contained in a directory (-d), so that new .faa/.gbk files can be added\n".
" while conserving previous BLAST results. In general the program will try to re-use\n".
" previous results when run with the same input directory.\n\n";
exit;
}
# read version bumber from CHANGES.txt
open(CHANGES,"$Bin/CHANGES.txt");
while(<CHANGES>)
{
if(eof && /^(\d+):/){ $VERSION = $1 }
}
close(CHANGES);
if(defined($opts{'v'}))
{
print "\n$0 version $VERSION\n";
print "\nProgram written by Bruno Contreras-Moreira (1) and Pablo Vinuesa (2).\n";
print "\n 1: http://www.eead.csic.es/compbio (Estacion Experimental Aula Dei/CSIC/Fundacion ARAID, Spain)\n";
print " 2: http://www.ccg.unam.mx/~vinuesa (Center for Genomic Sciences, UNAM, Mexico)\n";
print "\nPrimary citations:\n\n";
print " Contreras-Moreira B, Vinuesa P (2013) GET_HOMOLOGUES, a versatile software package for scalable and\n".
" robust microbial pangenome analysis. Appl Environ Microbiol 79(24):7696-701. (PubMed:24096415)\n\n";
print " Vinuesa P and Contreras-Moreira B (2015) Robust Identification of Orthologues and Paralogues for \n".
" Microbial Pan-Genomics Using GET_HOMOLOGUES: A Case Study of pIncA/C Plasmids. In Bacterial Pangenomics,\n".
" Methods in Molecular Biology Volume 1231, 203-232, edited by A Mengoni, M Galardini and M Fondi.\n";
print "\nThis software employs code, binaries and data from different authors, please cite them accordingly:\n";
print " OrthoMCL v1.4 (www.orthomcl.org , PubMed:12952885)\n";
print " INPARANOID v3.0 (inparanoid.sbc.su.se , PubMed:16873526)\n" if(!$DISSABLEPRND);
print " COGtriangles v2.1 (sourceforge.net/projects/cogtriangles , PubMed=20439257)\n";
print " NCBI Blast-2.2 (blast.ncbi.nlm.nih.gov , PubMed=9254694,20003500)\n";
print " Bioperl v1.5.2 (www.bioperl.org , PubMed=12368254)\n";
print " HMMER 3.1b2 (hmmer.org)\n";
print " Pfam (http://pfam.xfam.org , PubMed=26673716)\n";
print " Diamond v0.8.25 (https://github.com/bbuchfink/diamond, PubMed=25402007)\n";
# check all binaries and data needed by this program and print diagnostic info
print check_installed_features(@FEATURES2CHECK);
exit(0);
}
if(defined($opts{'d'}))
{
$inputDIR = $opts{'d'};
die "# EXIT : need a valid input directory\n" if(!-e $inputDIR);
if(basename($inputDIR) =~ /([\+])/)
{
die "# EXIT : need a valid input directory name, offending char: '$1'\n";
}
if(substr($inputDIR,length($inputDIR)-1,1) eq '/'){ chop $inputDIR }
$newDIR = $pwd.basename($inputDIR)."_homologues";
}
elsif(defined($opts{'i'}))
{
$input_FASTA_file = $opts{'i'};
die "# EXIT : need a valid input FASTA file\n" if(!-e $input_FASTA_file);
my $root = basename($input_FASTA_file);
if($root =~ /([\+])/)
{
die "# EXIT : need a valid input FASTA file, offending char: '$1'\n";
}
while($root =~ /\./){ $root = substr($root,0,-1) }
$newDIR = $pwd.$root."_homologues";
}
else{ die "# EXIT : need a -d directory with .faa/.gbk files or a -i .faa file as input\n"; }
#if($input_FASTA_file){ $runmode = 'local'; }
if(defined($opts{'m'}))
{
$runmode = $opts{'m'};
if($runmode eq 'pbs'){ $runmode = 'cluster'; } # legacy
elsif($runmode ne 'local' && $runmode ne 'cluster' && $runmode ne 'dryrun'){ $runmode = 'cluster'; }
}
else{ $runmode = 'local'; }
if($runmode eq 'local' && defined($opts{'n'}) && $opts{'n'} > 0)
{
$n_of_cpus = $opts{'n'};
$BLAST_NOCPU = $n_of_cpus;
}
if($runmode eq 'cluster')
{
# check whether file 'cluster.conf' exists & parse it
read_cluster_config( "$Bin/HPC.conf" );
if(!cluster_is_available())
{
print "# EXIT : cluster is not available, please create/edit file cluster.conf\n";
print_cluster_config();
die "# EXIT : or choose runmode -m local\n";
}
}
if(defined($opts{'o'})){ $onlyblast = 1; }
if($opts{'r'}){ $reference_proteome_string = $opts{'r'}; }
else{ $reference_proteome_string = 0; }
check_installed_features(@FEATURES2CHECK);
if(defined($opts{'X'}))
{
if(feature_is_installed('DIAMOND'))
{
$do_diamond = 1;
$output_mask .= "dmd_";
$pancore_mask .= "_dmd";
}
else{ warn_missing_soft('DIAMOND') }
}
if(defined($opts{'f'}) && $opts{'f'} > 0 && $opts{'f'} <= 100)
{
$filter_by_length = $opts{'f'};
$output_mask .= "f$filter_by_length\_";
}
else{ $filter_by_length = 0; $output_mask .= "f0_"; }
if(defined($opts{'t'}) && $opts{'t'} >= 0)
{
$min_cluster_size = $opts{'t'};
$output_mask .= $min_cluster_size."taxa_";
}
else{ $min_cluster_size = 'all'; $output_mask .= "alltaxa_"; }
if($opts{'I'} && $inputDIR)
{
$include_file = $opts{'I'};
$output_mask .= basename($include_file)."_";
$pancore_mask = "_".basename($include_file);
}
if($opts{'a'} && defined($opts{'d'}))
{
if($do_diamond)
{
die "# EXIT : cannot run diamond (-X) with DNA features (-a)\n";
}
$do_features = $opts{'a'};
$do_features =~ s/\s+//g;
my $features = $do_features;
$features =~ s/,//g;
$output_mask .= "$features\_";
$pancore_mask .= "_$features";
rename_blast_outfiles('features');
}
else{ $do_features = 0 }
if(defined($opts{'A'})){ $do_ANIb_matrix = 1 }
if(defined($opts{'P'})){ $do_POCP_matrix = 1 }
if(defined($opts{'g'}) && defined($opts{'d'}) && !$do_features)
{
if($min_cluster_size ne 'all')
{
die "\n# WARNING: intergenic sequences are extracted between syntenic core clusters, ".
"please remove option -t $min_cluster_size\n\n";
}
$do_intergenic = 1;
}
else{ $do_intergenic = 0 }
if(defined($opts{'M'}))
{
if(feature_is_installed('OMCL'))
{
$doMCL = 1;
$output_mask .= "algOMCL_";
$pancore_mask .= "_algOMCL";
}
else{ warn_missing_soft('OMCL') }
}
elsif(defined($opts{'p'}) && !$DISSABLEPRND)
{
if(feature_is_installed('PRND'))
{
$doPARANOID = 1;
$output_mask .= "algPRND_";
$pancore_mask .= "_algPRND";
}
else{ warn_missing_soft('PRND') }
}
elsif(defined($opts{'G'})) # && $inputDIR ne '')
{
if(feature_is_installed('COGS'))
{
$doCOG = 1;
if(defined($opts{'x'}))
{
$COGmulticluster = 1;
$output_mask .= "algCOGx_";
$pancore_mask .= "_algCOGx";
}
else
{
$COGmulticluster = 0;
$output_mask .= "algCOG_";
$pancore_mask .= "_algCOG";
}
}
else{ warn_missing_soft('COGS') }
}
else
{
if(defined($opts{'b'}))
{
if($do_diamond)
{
die "# EXIT : cannot run diamond (-X) with -b option\n";
}
$do_minimal_BDBHs = 1;
$output_mask .= "algBDBHmin_";
$pancore_mask .= "_algBDBHmin";
}
else
{
$output_mask .= "algBDBH_";
$pancore_mask .= "_algBDBH";
}
#if(defined($opts{'D'}) && !$do_features){}
if($min_cluster_size eq '0')
{
die "\n# WARNING: use of the default BDBH algorithm with option -t 0 is not supported ".
"(please check the manual)\n\n";
}
elsif($do_ANIb_matrix)
{
die "\n# WARNING: use of the default BDBH algorithm with option -A is not supported ".
"(please check the manual)\n\n";
}
elsif($do_POCP_matrix)
{
die "\n# WARNING: use of the default BDBH algorithm with option -P is not supported ".
"(please check the manual)\n\n";
}
}
if(defined($opts{'D'}) && !$do_features)
{
if(feature_is_installed('PFAM'))
{
$do_PFAM = 1;
$output_mask .= "Pfam\_";
$pancore_mask .= "_Pfam";
# in local mode increase batch size before calling _split_hmmscan.pl
if($runmode eq 'local'){ $MAXPFAMSEQS = 5000 } # should fit most bacterial proteomes
}
else{ warn_missing_soft('PFAM') }
}
if(defined($opts{'s'}) && eval{ require DB_File } ){ $saveRAM = 1; }
else{ $saveRAM = 0; }
if(defined($opts{'c'}) && !$do_minimal_BDBHs)
{
$do_genome_composition = 1;
if(defined($opts{'z'}) && ($doMCL || $doCOG))
{
$do_soft = 1;
}
if($opts{'R'})
{
$random_number_generator_seed = $opts{'R'};
}
elsif($do_features)
{
die "\n# WARNING: use of -c and -a options usually requires setting -R ".
"(please check the manual)\n\n";
}
srand($random_number_generator_seed);
}
else{ $do_genome_composition = 0; }
if(defined($opts{'e'}))
{
$exclude_inparalogues = 1;
$output_mask .= "e1_";
}
else{ $output_mask .= "e0_"; }
if(defined($opts{'E'}))
{
$evalue_cutoff = $opts{'E'};
if($evalue_cutoff > $MAXEVALUEBLASTSEARCH){ $evalue_cutoff = $MAXEVALUEBLASTSEARCH }
$output_mask .= "E$evalue_cutoff\_"; $pancore_mask .= "_E$evalue_cutoff";
}
if(defined($opts{'C'}))
{
$pmatch_cutoff = $opts{'C'}; # BDBH|OMCL
if($pmatch_cutoff < 1){ $pmatch_cutoff = 1 }
elsif($pmatch_cutoff > 100){ $pmatch_cutoff = 100 }
$segment_cover_cutoff = $pmatch_cutoff; # COGS|PRND
$output_mask .= "C$pmatch_cutoff\_"; $pancore_mask .= "_C$pmatch_cutoff";
}
if(defined($opts{'S'}))
{
$pi_cutoff = $opts{'S'};
if($pi_cutoff < 1){ $pi_cutoff = 1 }
elsif($pi_cutoff > 100){ $pi_cutoff = 100 }
$output_mask .= "S$pi_cutoff\_"; $pancore_mask .= "_S$pi_cutoff";
}
if(defined($opts{'F'}))
{
$MCLinflation = $opts{'F'};
if($MCLinflation < 1){ $MCLinflation = 1 }
elsif($MCLinflation > 5){ $MCLinflation = 5 }
$output_mask .= "F$MCLinflation\_"; $pancore_mask .= "_F$MCLinflation";
}
if(defined($opts{'N'}))
{
$neighbor_corr_cutoff = $opts{'N'};
if($neighbor_corr_cutoff < 0){ $neighbor_corr_cutoff = 0 }
elsif($neighbor_corr_cutoff > 1){ $neighbor_corr_cutoff = 1 }
$output_mask .= "N$neighbor_corr_cutoff\_"; $pancore_mask .= "_N$neighbor_corr_cutoff";
}
if(defined($opts{'B'}) && !$DISSABLEPRND)
{
$bitscore_cutoff = $opts{'B'};
if($bitscore_cutoff < 0){ $bitscore_cutoff = 0 }
$output_mask .= "B$bitscore_cutoff\_"; $pancore_mask .= "_B$bitscore_cutoff";
}
print "# $0 -i $input_FASTA_file -d $inputDIR -o $onlyblast -X $do_diamond -e $exclude_inparalogues -f $filter_by_length -r $reference_proteome_string ".
"-t $min_cluster_size -c $do_genome_composition -z $do_soft -I $include_file -m $runmode -n $n_of_cpus -M $doMCL -G $doCOG -p $doPARANOID ".
"-C $pmatch_cutoff -S $pi_cutoff -E $evalue_cutoff -F $MCLinflation -N $neighbor_corr_cutoff -B $bitscore_cutoff -b $do_minimal_BDBHs ".
"-s $saveRAM -D $do_PFAM -g $do_intergenic -a '$do_features' -x $COGmulticluster -R $random_number_generator_seed -A $do_ANIb_matrix -P $do_POCP_matrix\n\n";
if($runmode eq 'cluster')
{
print "# computer cluster settings\n";
print_cluster_config();
}
###############################################################
## 0) declare most important vars
my ($infile,$new_infile,$total,$total_blast,$p2oinfile,$seq,$comma_input_files,$n_of_residues);
my ($label,%orthologues,$gene,$orth,$orth2,$para,%inparalogues,%paralogues,$FASTAresultsDIR,$order,$minlog);
my ($n_of_similar_length_orthologues,$clusterfile,$dna_clusterfile,$previous_files,$current_files,$inpara);
my ($min_proteome_size,$reference_proteome,$smallest_proteome,$proteome_size,%seq_length,$cluster,$annot);
my ($smallest_proteome_name,$reference_proteome_name,%psize,$taxon,$taxon2,$n_of_clusters,$n_of_taxa);
my ($pname,$n_of_sequences,$refOK,$genbankOK,$cluster_size,$dna_cluster_size,$dna_cluster,$prot_cluster);
my (%orth_registry,%LSE_registry,$LSE_reference,$LSE_t,$redo_inp,$redo_orth,%idclusters,%names,$generef);
my ($reparse_all,$total_genbankOK,$n_of_similar_length_paralogues,$pfam_annot,$dnaOK,$n_of_taxa_cluster) = (0,0);
my ($BDBHdone,$PARANOIDdone,$orthoMCLdone,$COGdone,$n_of_parsed_lines,$n_of_pfam_parsed_lines) = (0,0,0,0,0,0);
my ($diff_BDBH_params,$diff_INP_params,$diff_HOM_params,$diff_OMCL_params,$lockcapableFS,$total_dry) = (0,0,0,0,0,0);
my ($total_clustersOK,$clgene,$clorth,%ANIb_matrix,%POCP_matrix,%GIclusters,$clustersOK,%cluster_ids) = (0);
my (@sorted_clusters,%taxa_clusters,%orth_taxa,@newfiles,%ressize,%intergenic_FNA_files);
constructDirectory($newDIR) || die "# EXIT : cannot create directory $newDIR , check permissions\n";
# 0.1) try to make sure there is only 1 instance writing to $newDIR
# http://www.perlmonks.org/?node_id=590619, flock might fail with NFS filehandles
# http://stackoverflow.com/questions/4502721/in-perl-how-can-i-check-if-a-file-is-locked
# http://perldoc.perl.org/File/Temp.html
my ($fhtest,$testlockfilename) = tempfile( DIR => $newDIR );
if(flock($fhtest,LOCK_EX|LOCK_NB))
{
$lockcapableFS = 1;
}
else
{
print "# WARNING : cannot lock files in $newDIR ,\n".
"# please ensure that no other instance of the program is running at this location\n\n";
}
unlink($testlockfilename);
open(my $fhlock,">$marfil_homology::lockfile") ||
die "# EXIT : cannot create lockfile $marfil_homology::lockfile\n";
if($lockcapableFS)
{
flock($fhlock, LOCK_EX|LOCK_NB) ||
die "# EXIT : cannot run another instance of the program with same input data while previous is running\n";
}
# 0.2) prepare to tie hash/array to db if required : allows massives,slow data structs in disk
my (@sequence_data,@sequence_prot,@sequence_dna);
my $sequence_data_filename = $newDIR."/sequence_data.db";
my $sequence_prot_filename = $newDIR."/sequence_prot.db";
my $sequence_dna_filename = $newDIR."/sequence_dna.db";
my $pfam_data_filename = $newDIR."/pfam.db";# %pfam_hash is global, imported from marfil_homology
unlink($sequence_data_filename,$sequence_prot_filename,$sequence_dna_filename,$pfam_data_filename);
my $input_order_file = $newDIR."/input_order.txt";
my $diamond_file = $newDIR."/diamond.txt";
my $dryrun_file = $newDIR."/dryrun.txt";
my $paranoidDIR = $newDIR."/paranoid_sql_C$pmatch_cutoff\_B$bitscore_cutoff\_O$segment_cover_cutoff/";
print "# version $VERSION\n";
print "# results_directory=$newDIR\n";
print "# parameters: MAXEVALUEBLASTSEARCH=$MAXEVALUEBLASTSEARCH MAXPFAMSEQS=$MAXPFAMSEQS ".
"BATCHSIZE=$BATCHSIZE KEEPSCNDHSPS=$KEEPSCNDHSPS\n";
# 0.3) make sure that DIAMOND jobs are not mixed with BLASTP jobs
if(-s $diamond_file)
{
open(DMDFILE,$diamond_file) || die "# EXIT : cannot read $diamond_file\n";
while(<DMDFILE>)
{
if(/^diamond job:(\d)/)
{
if($1 != $do_diamond)
{
$do_diamond = $1;
print "# diamond job:$do_diamond (forced to match previous jobs)\n";
}
else{ print "# diamond job:$do_diamond\n" }
}
}
close(DMDFILE);
}
else
{
if(-s $input_order_file)
{
# assume this was a BLASTP job to be sure
if($do_diamond == 1)
{
$do_diamond = 0;
print "# diamond job:$do_diamond (forced to match previous jobs)\n";
}
else{ print "# diamond job:$do_diamond\n" }
}
else{ print "# diamond job:$do_diamond\n" }
open(DMDFILE,'>',$diamond_file) || die "# EXIT : cannot create $diamond_file\n";
print DMDFILE "diamond job:$do_diamond\n";
close(DMDFILE);
}
# 0.4) prepare dryrun file if required
if($runmode eq 'dryrun')
{
open(DRYRUNLOG,">",$dryrun_file);
}
################################################################
## 1) read all input files, identify format and generate input FASTA files in temporary directory
print "\n# checking input files...\n";
$min_proteome_size = -1;
$reference_proteome = $refOK = $n_of_sequences = $n_of_taxa = $n_of_residues = 0;
$previous_files = $current_files = '';
# 1.0) instantiate BerkeleyDB if required
if($saveRAM)
{
eval
{
import DB_File;
tie(@sequence_data,'DB_File',$sequence_data_filename,1,0666,$DB_File::DB_RECNO)
|| die "# EXIT : cannot create $sequence_data_filename: $! (BerkeleyDB::Error)\n";
tie(@sequence_prot,'DB_File',$sequence_prot_filename,1,0666,$DB_File::DB_RECNO)
|| die "# EXIT : cannot create $sequence_prot_filename: $! (BerkeleyDB::Error)\n";
tie(@sequence_dna,'DB_File',$sequence_dna_filename,1,0666,$DB_File::DB_RECNO)
|| die "# EXIT : cannot create $sequence_dna_filename: $! (BerkeleyDB::Error)\n";
if($do_PFAM && !$onlyblast)
{
tie(%pfam_hash,'DB_File',$pfam_data_filename,1,0666,$DB_File::DB_HASH)
|| die "# EXIT : cannot create $pfam_data_filename: $! (BerkeleyDB::Error)\n";
}
}
}
# 1.1) directory with input files
if($inputDIR)
{
# 1.1.1) open and read directory, only master .fa/.gb files are considered
opendir(DIR,$inputDIR) || die "# EXIT : cannot list $inputDIR\n";
my @inputfiles = sort grep {/\.fa[a-z]*\S*$/i||/\.gb\S*$/||/_$/||/\.clusters$/} readdir(DIR);
closedir(DIR);
# 1.1.2) sort input files and put new files towards the end of @inputfiles: LILO
if(-s $input_order_file)
{
my (@new_order_input_files,%previous_input_file,$n_of_new_infiles);
open(ORDER,$input_order_file) || die "# EXIT : cannot read $input_order_file\n";
while(<ORDER>)
{
chomp;
($order,$infile) = split(/\t/);
if(!-s $inputDIR."/".$infile)
{ die "# EXIT : cannot find previous input file $infile, please re-run everything\n"; }
$previous_input_file{$infile} = 1;
$new_order_input_files[$order] = $infile;
}
close(ORDER);
$n_of_new_infiles=0;
foreach $infile (@inputfiles)
{
next if($previous_input_file{$infile});
$new_order_input_files[++$order] = $infile;
print "# order of new input file $infile = $order\n";
$n_of_new_infiles++;
}
if($n_of_new_infiles){ print "# updating $input_order_file with $n_of_new_infiles new input files\n"; }
open(ORDER,">$input_order_file") || die "# EXIT : cannot write $input_order_file\n";
$order=0;
foreach $infile (@new_order_input_files)
{
print ORDER "$order\t$infile\n";
$order++;
}
close(ORDER);
@inputfiles = @new_order_input_files;
}
else
{
$order=0;
open(ORDER,">$input_order_file") || die "# EXIT : cannot write $input_order_file\n";
foreach $infile (@inputfiles)
{
print ORDER "$order\t$infile\n";
$order++;
}
close(ORDER);
}
# 1.1.3) check if all input files have GenBank format (-a/-g options)
if($do_intergenic || $do_features)
{
foreach $infile (@inputfiles)
{
if($infile !~ /\.gb\S*$/i)
{
die "\n# EXIT : cannot analyze feature/intergenic sequences when input file is not in GenBank format\n\n";
}
}
}
# 1.1.4) open CSV protein to organism file, id to name (required by COGtriangles)
open(COGCSV,">$p2ofilename") || die "# EXIT : cannot write $p2ofilename\n";
# 1.1.5) iteratively parse input files
foreach $infile (@inputfiles)
{
my ($file_ressize,$intergenic_size,$fasta_ref,$dna_fasta_ref) = (0,0);
($genbankOK,$dnaOK,$clustersOK,$proteome_size) = (0,0,0,-1);
++$n_of_taxa;
# 1.1.5.1) genbank files
if($infile =~ /\.gb\S*$/i)
{
my $prot_tmp_file = $newDIR ."/" .basename($infile) . ".faa";
my $dna_tmp_file = $newDIR ."/" .basename($infile) . ".fna";
my $feature_dna_tmp_file = $newDIR ."/" .basename($infile) . "_features.fna";
my $interg_tmp_file = $newDIR ."/" .basename($infile) .
"_intergenic$MININTERGENESIZE\_$MAXINTERGENESIZE\_$INTERGENEFLANKORF.fna";
if($do_features)
{
if(-s $feature_dna_tmp_file ||
extract_features_from_genbank($inputDIR."/".$infile,
$feature_dna_tmp_file,$do_features) > 0)
{
$fasta_ref = read_FASTA_file_array($feature_dna_tmp_file);
if(!@$fasta_ref)
{
die "# ERROR: could not extract feature sequences from file ".
"$inputDIR/$infile\n";
}
}
else{ die "# ERROR: could not extract feature sequences from file $inputDIR/$infile\n"; }
}
else
{
if(-s $prot_tmp_file ||
extract_CDSs_from_genbank($inputDIR."/".$infile,$prot_tmp_file,$dna_tmp_file) > 0)
{
$fasta_ref = read_FASTA_file_array($prot_tmp_file);
if(-s $dna_tmp_file && @$fasta_ref)
{
$dna_fasta_ref = read_FASTA_file_array($dna_tmp_file);
$genbankOK = 1;
$total_genbankOK++;
}
else
{
print "# WARNING: could not extract nucleotide sequences from file $inputDIR/$infile\n";
}
}
elsif(extract_genes_from_genbank($inputDIR."/".$infile,$prot_tmp_file,$dna_tmp_file) > 0)
{
print "# WARNING: can only extract genes (not CDSs) from file $inputDIR/$infile\n";
# both dna & prot sequences are always generated with extract_genes
$fasta_ref = read_FASTA_file_array($prot_tmp_file);
$dna_fasta_ref = read_FASTA_file_array($dna_tmp_file);
$genbankOK = 1;
$total_genbankOK++;
}
else{ die "# ERROR: could not extract sequences from file $inputDIR/$infile\n"; }
if($do_intergenic)
{
if(!-s $interg_tmp_file)
{
$intergenic_size =
extract_intergenic_from_genbank($inputDIR."/".$infile,
$interg_tmp_file,$MININTERGENESIZE,
$MAXINTERGENESIZE,$INTERGENEFLANKORF);
}
else
{
my $intergfna_ref = read_FASTA_file_array($interg_tmp_file);
$intergenic_size = scalar(@$intergfna_ref);
}
if(!$intergenic_size)
{
print "# WARNING: could not extract intergenic sequences from file $inputDIR/$infile\n";
}
else{ $intergenic_FNA_files{$infile} = $interg_tmp_file }
}
}
}
elsif(($infile =~ /\.clusters$/ || $infile =~ /_$/) && -d $inputDIR.'/'.$infile) # 1.1.5.2) subdirectory with cluster files in FASTA format
{
if($total_clustersOK++)
{
die "\n# WARNING: can only take one folder of pre-clustered sequences\n\n";
}
opendir(CLDIR,$inputDIR.'/'.$infile) || die "# EXIT : cannot list $inputDIR/$infile\n";
my @clusterfiles = sort grep {/\.fa[a-z]*$/i} readdir(CLDIR);
closedir(CLDIR);
my ($cluster_id,$cldnaOK) = (0);
my $clseq = $n_of_sequences + 1; # make sure sequences in clusters are correctly numbered
my (@clusters_fasta,@clusters_dna_fasta);
foreach my $clusterfile (@clusterfiles)
{
$cldnaOK = 0;
my $protfile = $inputDIR.'/'.$infile.'/'.$clusterfile;
$fasta_ref = read_FASTA_file_array($protfile);
# check whether an identical .fna file exists (expects sequences in same order)
my $dnafile = $protfile;
$dnafile =~ s/\.fa+.*?$/\.fna/;
if($dnafile ne $protfile && -s $dnafile )
{
$dna_fasta_ref = read_FASTA_file_array($dnafile);
if($#{$fasta_ref} == $#{$dna_fasta_ref}){ $cldnaOK = 1 }
else
{
print "WARNING: cannot use nucleotide sequences in file $dnafile\n".
"# as they do not match those in file $protfile\n";
}
}
# arbitrarily choose first cluster sequence as representative
$cluster_id = $clseq;
# add sequentially members of pre-processed cluster
foreach $seq ( 0 .. $#{$fasta_ref} )
{
# remember members of this cluster
push(@{$cluster_ids{$cluster_id}},$clseq) if($clseq != $cluster_id);
$clusters_fasta[$clseq] = $fasta_ref->[$seq]; #print "$clseq\n";
if($cldnaOK)
{
$clusters_dna_fasta[$clseq] = $dna_fasta_ref->[$seq];
}
$clseq++;
}
}
$fasta_ref = \@clusters_fasta;
$dna_fasta_ref = \@clusters_dna_fasta;
if($#{$fasta_ref} == $#{$dna_fasta_ref}){ $dnaOK = 1 }
$clustersOK = scalar(@clusterfiles);
$total_clustersOK++;
if($do_genome_composition)
{
die "\n# WARNING: -c genome composition analyses cannot be done with pre-clustered input sequences ".
"(please check the manual)\n\n";
}
}
else # 1.1.5.3) fasta files
{
my $protfile = $inputDIR."/".$infile;
$fasta_ref = read_FASTA_file_array($protfile);
# check whether an identical .fna file exists (expects sequences in same order)
my $dnafile = $protfile;
$dnafile =~ s/\.fa+.*?$/\.fna/;
if($dnafile ne $protfile && -s $dnafile )
{
$dna_fasta_ref = read_FASTA_file_array($dnafile);
if($#{$fasta_ref} == $#{$dna_fasta_ref}){ $dnaOK = 1 }
else
{
print "WARNING: cannot use nucleotide sequences in file $dnafile\n".
"# as they do not match those in file $protfile\n";
}
}
}
if(!defined($fasta_ref)){ die "# ERROR: could not extract sequences from $inputDIR/$infile\n"; }
$proteome_size = scalar(@$fasta_ref);
$psize{$infile} = $proteome_size;
if($do_intergenic){ print "# $infile $proteome_size (intergenes=$intergenic_size)\n"; }
elsif($clustersOK)
{
if($dnaOK){ print "# $infile $proteome_size ($clustersOK clusters, found twin .fna file)\n"; }
else{ print "# $infile $proteome_size ($clustersOK clusters)\n"; }
}
elsif($dnaOK){ print "# $infile $proteome_size (found twin .fna file)\n"; }
else{ print "# $infile $proteome_size\n"; }
$new_infile = basename($infile) . ".$FASTAEXTENSION";
$p2oinfile = basename($infile);
if($do_features)
{
$new_infile = basename($infile) . "_features.$FASTAEXTENSION";
$p2oinfile = basename($infile) . '_features';
}
open(INPUT,">$newDIR/$new_infile") || die "# EXIT : cannot create $newDIR/$new_infile\n";
foreach $seq ( 0 .. $#{$fasta_ref} )
{
if(!$fasta_ref->[$seq][SEQ] || $fasta_ref->[$seq][SEQ] eq '')
{
chomp $fasta_ref->[$seq][NAME];
print "# skipped void sequence ($fasta_ref->[$seq][NAME])\n" if($fasta_ref->[$seq][NAME]);
next;
}
# skip DNA sequences in supposed-to-be amino acid files
if(!$do_features && $fasta_ref->[$seq][SEQ] =~ /^[ACGTUXN\-\s]+$/i)
{
print "# skipped nucleotide sequence ($fasta_ref->[$seq][NAME])\n";
next;
}
# @sequence_... global arrays take large amounts of RAM
# first sequence is [1]
++$n_of_sequences;
$file_ressize += length($fasta_ref->[$seq][SEQ]);
if(!$onlyblast)
{
$sequence_data[$n_of_sequences] = $fasta_ref->[$seq][NAME];
$sequence_prot[$n_of_sequences] = $fasta_ref->[$seq][SEQ]; # with -a actually stores dna
if($fasta_ref->[$seq][SEQ] eq '')
{
printf("# cannot parse protein sequence of %s... (%s)\n",
substr($fasta_ref->[$seq][NAME],0,15),$infile);
}
if($genbankOK || $dnaOK)
{
$sequence_dna[$n_of_sequences] = $dna_fasta_ref->[$seq][SEQ];
if(!$dna_fasta_ref->[$seq][SEQ] || $dna_fasta_ref->[$seq][SEQ] eq '')
{
printf("# cannot parse DNA sequence of %s... (%s)\n",
substr($fasta_ref->[$seq][NAME],0,15),$infile);
}
}
}
# do not print redundant pre-clustered sequences, take only 1/cluster
next if($clustersOK && !$cluster_ids{$seq});
print INPUT ">$n_of_sequences\n$fasta_ref->[$seq][SEQ]\n";
$pname = $fasta_ref->[$seq][NAME];$pname =~ s/#/hash/g; # COG does not like hashes in names
print COGCSV "$n_of_sequences,$p2oinfile,$pname\n";
}
close(INPUT);
$comma_input_files .= "$new_infile,";
push(@newfiles,$new_infile);
$n_of_residues += $file_ressize;
$ressize{$infile} = $ressize{$new_infile} = $file_ressize;
}
close(COGCSV);
if($min_cluster_size eq 'all'){ $min_cluster_size = $n_of_taxa; } # size of clusters in section 4
}
else # 1.2) input single FASTA file with [taxon names] in headers, order not checked
{
print "# $input_FASTA_file\n";
# 1.2.0) open CSV protein to organism file required by COGtriangles
open(COGCSV,">$p2ofilename") || die "# EXIT : cannot write $p2ofilename\n";
my $fasta_ref = read_FASTA_file_array($input_FASTA_file);
# allow taxon/genome name to be a single word (1)
my %taxa_in_headers = find_taxa_FASTA_array_headers($fasta_ref,1);
# 1.2.1) print sequences of the same taxon in separate files
foreach $taxon (sort (keys(%taxa_in_headers)))
{
my $taxon_ressize = 0;