-
Notifications
You must be signed in to change notification settings - Fork 5
/
ribotyper
executable file
·5032 lines (4595 loc) · 249 KB
/
ribotyper
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
#
use strict;
use warnings;
use Getopt::Long;
use Time::HiRes qw(gettimeofday);
# ribotyper :: detect and classify ribosomal RNA sequences
# Usage: ribotyper [-options] <fasta file to annotate> <output directory>
require "ribo.pm";
# make sure required environment variables are set
my $env_riboscripts_dir = utl_DirEnvVarValid("RIBOSCRIPTSDIR");
my $env_riboinfernal_dir = utl_DirEnvVarValid("RIBOINFERNALDIR");
my $env_riboeasel_dir = utl_DirEnvVarValid("RIBOEASELDIR");
my $df_model_dir = $env_riboscripts_dir . "/models/";
my %execs_H = (); # hash with paths to all required executables
$execs_H{"cmsearch"} = $env_riboinfernal_dir . "/cmsearch";
$execs_H{"cmalign"} = $env_riboinfernal_dir . "/cmalign";
$execs_H{"esl-seqstat"} = $env_riboeasel_dir . "/esl-seqstat";
$execs_H{"esl-sfetch"} = $env_riboeasel_dir . "/esl-sfetch";
# deal with 'time' differently because we can deal if it doesn't exist and
# we only use it if it does exist and -p is used
my $env_ribotime_dir = ribo_CheckForTimeExecutable(); # this will return undef if time doesn't exist or is undef
if(defined $env_ribotime_dir) {
$execs_H{"time"} = $env_ribotime_dir . "/time";
}
utl_ExecHValidate(\%execs_H, undef);
#########################################################
# Command line and option processing using sqp_opts.pm
#
# opt_HH: 2D hash:
# 1D key: option name (e.g. "-h")
# 2D key: string denoting type of information
# (one of "type", "default", "group", "requires", "incompatible", "preamble", "help")
# value: string explaining 2D key:
# "type": "boolean", "string", "integer" or "real"
# "default": default value for option
# "group": integer denoting group number this option belongs to
# "requires": string of 0 or more other options this option requires to work, each separated by a ','
# "incompatible": string of 0 or more other options this option is incompatible with, each separated by a ','
# "preamble": string describing option for preamble section (beginning of output from script)
# "help": string describing option for help section (printed if -h used)
# "setby": '1' if option set by user, else 'undef'
# "value": value for option, can be undef if default is undef
#
# opt_order_A: array of options in the order they should be processed
#
# opt_group_desc_H: key: group number (integer), value: description of group for help output
my %opt_HH = ();
my @opt_order_A = ();
my %opt_group_desc_H = ();
# Add all options to %opt_HH and @opt_order_A.
# This section needs to be kept in sync (manually) with the &GetOptions call below
$opt_group_desc_H{"1"} = "basic options";
# option type default group requires incompat preamble-output help-output
opt_Add("-h", "boolean", 0, 0, undef, undef, undef, "display this help", \%opt_HH, \@opt_order_A);
opt_Add("-f", "boolean", 0, 1, undef, undef, "forcing directory overwrite", "force; if <output directory> exists, overwrite it", \%opt_HH, \@opt_order_A);
opt_Add("-v", "boolean", 0, 1, undef, undef, "be verbose", "be verbose; output commands to stdout as they're run", \%opt_HH, \@opt_order_A);
opt_Add("-n", "integer", 0, 1, undef, "-p", "use <n> CPUs", "use <n> CPUs", \%opt_HH, \@opt_order_A);
opt_Add("-i", "string", undef, 1, undef, undef, "use model info file <s> instead of default", "use model info file <s> instead of default", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"2"} = "options for controlling the first round search algorithm";
# option type default group requires incompat preamble-output help-output
opt_Add("--1hmm", "boolean", 0, 2, undef, undef, "run first round in slower HMM mode", "run first round in slower HMM mode", \%opt_HH, \@opt_order_A);
opt_Add("--1slow", "boolean", 0, 2, undef, undef, "run first round in slow CM mode", "run first round in slow CM mode that scores structure+sequence", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"3"} = "options for controlling the second round search algorithm";
# option type default group requires incompat preamble-output help-output
opt_Add("--2slow", "boolean", 0, 3, undef, "--1slow", "run second round in slow CM mode", "run second round in slow CM mode that scores structure+sequence", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"4"} = "options related to bit score REPORTING thresholds";
# option type default group requires incompat preamble-output help-output
opt_Add("--minpsc", "real", "20.", 4, undef, undef, "set minimum bit score cutoff for primary hits to <x>", "set minimum bit score cutoff for primary hits to include to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--minssc", "real", "10.", 4, undef, undef, "set minimum bit score cutoff for secondary hits to <x>", "set minimum bit score cutoff for secondary hits to include to <x> bits", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"5"} = "options for controlling which sequences PASS/FAIL (turning on optional failure criteria)";
# option type default group requires incompat preamble-output help-output
opt_Add("--minusfail", "boolean", 0, 5, undef, undef, "hits on negative (minus) strand FAIL", "hits on negative (minus) strand defined as FAILures", \%opt_HH, \@opt_order_A);
opt_Add("--scfail", "boolean", 0, 5, undef, undef, "seqs that fall below low score threshold FAIL", "seqs that fall below low score threshold FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--difffail", "boolean", 0, 5, undef, undef, "seqs that fall below low score diff threshold FAIL", "seqs that fall below low score difference threshold FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--covfail", "boolean", 0, 5, undef, undef, "seqs that fall below low coverage threshold FAIL", "seqs that fall below low coverage threshold FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--multfail", "boolean", 0, 5, undef, undef, "seqs that have more than one hit to best model FAIL", "seqs that have more than one hit to best model FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--questfail", "boolean", 0, 5,"--inaccept",undef, "seqs that score best to questionable models FAIL", "seqs that score best to questionable models FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--shortfail", "integer", 0, 5, undef, undef, "seqs that are shorter than <n> nucleotides FAIL", "seqs that are shorter than <n> nucleotides FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--longfail", "integer", 0, 5, undef, undef, "seqs that are longer than <n> nucleotides FAIL", "seqs that are longer than <n> nucleotides FAIL", \%opt_HH, \@opt_order_A);
opt_Add("--esdfail", "boolean", 0, 5,"--evalues",undef, "E-value/score discrepancies FAIL", "seqs in which second best hit by E-value has better bit score above threshold FAIL", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"6"} = "options for controlling thresholds for failure/warning criteria";
# option type default group requires incompat preamble-output help-output
opt_Add("--lowppossc", "real", "0.5", 6, undef, undef, "set minimum bit per position threshold to <x>", "set minimum bit per position threshold for reporting suspiciously low scores to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--tcov", "real", "0.86", 6, undef, undef, "set low total coverage threshold to <x>", "set low total coverage threshold to <x> fraction of target sequence", \%opt_HH, \@opt_order_A);
opt_Add("--tshortcov", "real", undef, 6,"--tshortlen",undef, "set low total coverage for short seqs threshold to <x>", "set low total coverage threshold for short seqs to <x> fraction of target sequence", \%opt_HH, \@opt_order_A);
opt_Add("--tshortlen", "integer", undef, 6,"--tshortcov",undef, "set maximum length for short seqs coverage calc to <n>", "set maximum length for short seq coverage threshold to <n> nucleotides", \%opt_HH, \@opt_order_A);
opt_Add("--lowpdiff", "real", "0.10", 6, undef, "--absdiff","set low per-posn score difference threshold to <x>", "set 'low' per-posn score difference threshold to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--vlowpdiff", "real", "0.04", 6, undef, "--absdiff","set very low per-posn score difference threshold to <x>", "set 'very low' per-posn score difference threshold to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--absdiff", "boolean", 0, 6, undef, undef, "use total score diff threshold, not per-posn", "use total score difference thresholds instead of per-posn", \%opt_HH, \@opt_order_A);
opt_Add("--lowadiff", "real", "100.", 6,"--absdiff",undef, "set 'low' total sc diff threshold to <x>", "set 'low' total score difference threshold to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--vlowadiff", "real", "40.", 6,"--absdiff",undef, "set 'very low' total sc diff threshold to <x>", "set 'very low' total score difference threshold to <x> bits", \%opt_HH, \@opt_order_A);
opt_Add("--maxoverlap", "integer", "20", 6, undef, undef, "set maximum allowed model position overlap to <n>", "set maximum allowed number of model positions to overlap b/t 2 hits before failure to <n>", \%opt_HH, \@opt_order_A);
opt_Add("--esdmaxsc", "real", "0.001", 6,"--evalues",undef, "set max allowed bit sc diff for Evalue/score discrepancies to <x>", "set maximum allowed bit score difference for E-value/score discrepancies to <x>", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"7"} = "optional input files";
# option type default group requires incompat preamble-output help-output
opt_Add("--inaccept", "string", undef, 7, undef, undef, "read acceptable/questionable models from <s>", "read acceptable/questionable domains/models from file <s>", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"8"} = "options that modify the behavior of --1slow or --2slow";
# option type default group requires incompat preamble-output help-output
opt_Add("--mid", "boolean", 0, 8, undef, "--max", "use --mid instead of --rfam", "with --1slow/--2slow use cmsearch --mid option instead of --rfam", \%opt_HH, \@opt_order_A);
opt_Add("--max", "boolean", 0, 8, undef, "--mid", "use --max instead of --rfam", "with --1slow/--2slow use cmsearch --max option instead of --rfam", \%opt_HH, \@opt_order_A);
opt_Add("--smxsize", "real", undef, 8,"--max", undef, "with --max, use --smxsize <x>", "with --max also use cmsearch --smxsize <x>", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"9"} = "options for parallelizing cmsearch on a compute farm";
# option type default group requires incompat preamble-output help-output
opt_Add("-p", "boolean", 0, 9, undef, undef, "parallelize cmsearch on a compute farm", "parallelize cmsearch on a compute farm", \%opt_HH, \@opt_order_A);
opt_Add("-q", "string", undef, 9, "-p", undef, "use qsub info file <s> instead of default", "use qsub info file <s> instead of default", \%opt_HH, \@opt_order_A);
opt_Add("-s", "integer", 181, 9, "-p", undef, "seed for random number generator is <n>", "seed for random number generator is <n>", \%opt_HH, \@opt_order_A);
opt_Add("--nkb", "integer", 100, 9, "-p", undef, "number of KB of seq for each cmsearch farm job is <n>", "number of KB of sequence for each cmsearch farm job is <n>", \%opt_HH, \@opt_order_A);
opt_Add("--wait", "integer", 500, 9, "-p", undef, "allow <n> minutes for cmsearch jobs on farm", "allow <n> wall-clock minutes for cmsearch jobs on farm to finish, including queueing time", \%opt_HH, \@opt_order_A);
opt_Add("--errcheck", "boolean", 0, 9, "-p", undef, "consider any farm stderr output as indicating a job failure", "consider any farm stderr output as indicating a job failure", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"10"} = "options for controlling gap type definitions";
opt_Add("--mgap", "integer", 10, 10, undef, undef, "maximum size of a 'small' gap in model coordinates is <n>", "maximum size of a 'small' gap in model coordinates is <n>", \%opt_HH, \@opt_order_A);
opt_Add("--sgap", "integer", 10, 10, undef, undef, "maximum size of a 'small' gap in sequence coordinates is <n>", "maximum size of a 'small' gap in sequence coordinates is <n>", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"11"} = "options for creating additional output files";
opt_Add("--outseqs", "boolean", 0, 11, undef, undef, "save per-model PASS/FAIL sequences to files", "save per-model PASS/FAIL sequences to files", \%opt_HH, \@opt_order_A);
opt_Add("--outhits", "boolean", 0, 11, undef, undef, "save per-model PASS/FAIL best hits to files", "save per-model PASS/FAIL sequences to files", \%opt_HH, \@opt_order_A);
opt_Add("--outgaps", "boolean", 0, 11, undef, undef, "save gap sequences between hits to a file", "save gap sequences between hits to a file", \%opt_HH, \@opt_order_A);
opt_Add("--outxgaps", "integer", 20, 11, undef, undef, "save gap sequence file with <n> added nts", "save gap sequence file with <n> added nts", \%opt_HH, \@opt_order_A);
opt_Add("--keep", "boolean", 0, 11, undef, undef, "keep all intermediate files", "keep all intermediate files that are removed by default", \%opt_HH, \@opt_order_A);
$opt_group_desc_H{"12"} = "advanced options";
# option type default group requires incompat preamble-output help-output
opt_Add("--evalues", "boolean", 0, 12, undef, undef, "rank by E-values, not bit scores", "rank hits by E-values, not bit scores", \%opt_HH, \@opt_order_A);
opt_Add("--skipsearch", "boolean", 0, 12, undef, "-f", "skip search stage", "skip search stage, use results from earlier run", \%opt_HH, \@opt_order_A);
opt_Add("--noali", "boolean", 0, 12,"--keep", "--skipsearch", "no alignments in output", "no alignments in output, requires --keep", \%opt_HH, \@opt_order_A);
opt_Add("--samedomain", "boolean", 0, 12, undef, undef, "top two hits can be same domain", "top two hits can be to models in the same domain", \%opt_HH, \@opt_order_A);
opt_Add("--skipval", "boolean", 0, 12, undef, undef, "skip validation of CM and model info files", "skip validation of CM and model info files", \%opt_HH, \@opt_order_A);
opt_Add("--onlyval", "boolean", 0, 12, undef, "--skipval", "validate CM and model info files and exit", "validate CM and model info files and exit", \%opt_HH, \@opt_order_A);
# This section needs to be kept in sync (manually) with the opt_Add() section above
my %GetOptions_H = ();
my $usage = "Usage: ribotyper [-options] <fasta file to annotate> <output directory>\n";
my $synopsis = "ribotyper :: detect and classify ribosomal RNA sequences";
my $options_okay =
&GetOptions('h' => \$GetOptions_H{"-h"},
'f' => \$GetOptions_H{"-f"},
'v' => \$GetOptions_H{"-v"},
'n=s' => \$GetOptions_H{"-n"},
'i=s' => \$GetOptions_H{"-i"},
# first round algorithm options
'1hmm' => \$GetOptions_H{"--1hmm"},
'1slow' => \$GetOptions_H{"--1slow"},
# first round algorithm options
'2slow' => \$GetOptions_H{"--2slow"},
# options controlling minimum bit score cutoff
'minpsc=s' => \$GetOptions_H{"--minpsc"},
'minssc=s' => \$GetOptions_H{"--minssc"},
# options controlling which sequences PASS/FAIL
'minusfail' => \$GetOptions_H{"--minusfail"},
'scfail' => \$GetOptions_H{"--scfail"},
'difffail' => \$GetOptions_H{"--difffail"},
'covfail' => \$GetOptions_H{"--covfail"},
'multfail' => \$GetOptions_H{"--multfail"},
'questfail' => \$GetOptions_H{"--questfail"},
'shortfail=s' => \$GetOptions_H{"--shortfail"},
'longfail=s' => \$GetOptions_H{"--longfail"},
'esdfail' => \$GetOptions_H{"--esdfail"},
# options controlling thresholds for warnings and failures
'lowppossc=s' => \$GetOptions_H{"--lowppossc"},
'tcov=s' => \$GetOptions_H{"--tcov"},
'tshortcov=s' => \$GetOptions_H{"--tshortcov"},
'tshortlen=s' => \$GetOptions_H{"--tshortlen"},
'lowpdiff=s' => \$GetOptions_H{"--lowpdiff"},
'vlowpdiff=s' => \$GetOptions_H{"--vlowpdiff"},
'absdiff' => \$GetOptions_H{"--absdiff"},
'lowadiff=s' => \$GetOptions_H{"--lowadiff"},
'vlowadiff=s' => \$GetOptions_H{"--vlowadiff"},
'maxoverlap=s' => \$GetOptions_H{"--maxoverlap"},
'esdmaxsc=s' => \$GetOptions_H{"--esdmaxsc"},
# optional input files
'inaccept=s' => \$GetOptions_H{"--inaccept"},
# options that affect --1slow and --2slow
'mid' => \$GetOptions_H{"--mid"},
'max' => \$GetOptions_H{"--max"},
'smxsize=s' => \$GetOptions_H{"--smxsize"},
# options for parallelization
'p' => \$GetOptions_H{"-p"},
'q=s' => \$GetOptions_H{"-q"},
's=s' => \$GetOptions_H{"-s"},
'nkb=s' => \$GetOptions_H{"--nkb"},
'wait=s' => \$GetOptions_H{"--wait"},
'errcheck' => \$GetOptions_H{"--errcheck"},
# options related to gap types
'mgap=s' => \$GetOptions_H{"--mgap"},
'sgap=s' => \$GetOptions_H{"--sgap"},
# advanced options
'evalues' => \$GetOptions_H{"--evalues"},
'skipsearch' => \$GetOptions_H{"--skipsearch"},
'noali' => \$GetOptions_H{"--noali"},
'keep' => \$GetOptions_H{"--keep"},
'outseqs' => \$GetOptions_H{"--outseqs"},
'outhits' => \$GetOptions_H{"--outhits"},
'outgaps' => \$GetOptions_H{"--outgaps"},
'outxgaps=s' => \$GetOptions_H{"--outxgaps"},
'samedomain' => \$GetOptions_H{"--samedomain"},
'skipval' => \$GetOptions_H{"--skipval"},
'onlyval' => \$GetOptions_H{"--onlyval"});
my $total_seconds = -1 * ofile_SecondsSinceEpoch(); # by multiplying by -1, we can just add another ofile_SecondsSinceEpoch call at end to get total time
my $executable = $0;
my $date = scalar localtime();
my $version = "1.0.5";
my $releasedate = "Sep 2023";
my $package_name = "Ribovore";
# make *STDOUT file handle 'hot' so it automatically flushes whenever we print to it
select *STDOUT;
$| = 1;
# print help and exit if necessary
if((! $options_okay) || ($GetOptions_H{"-h"})) {
ofile_OutputBanner(*STDOUT, $package_name, $version, $releasedate, $synopsis, $date, undef);
opt_OutputHelp(*STDOUT, $usage, \%opt_HH, \@opt_order_A, \%opt_group_desc_H);
if(! $options_okay) { die "ERROR, unrecognized option;"; }
else { exit 0; } # -h, exit with 0 status
}
# check that number of command line args is correct
if(scalar(@ARGV) != 2) {
print "Incorrect number of command line arguments.\n";
print $usage;
print "\nTo see more help on available options, enter 'ribotyper -h'\n\n";
exit(1);
}
my ($seq_file, $dir_out) = (@ARGV);
# set options in opt_HH
opt_SetFromUserHash(\%GetOptions_H, \%opt_HH);
# validate options (check for conflicts)
opt_ValidateSet(\%opt_HH, \@opt_order_A);
# do some final option checks that are currently too sophisticated for sqp_opts
if(opt_Get("--evalues", \%opt_HH)) {
if((! opt_Get("--1hmm", \%opt_HH)) &&
(! opt_Get("--1slow", \%opt_HH)) &&
(! opt_Get("--2slow", \%opt_HH))) {
die "ERROR, --evalues requires one of --1hmm, --1slow or --2slow";
}
}
if(opt_Get("--mid", \%opt_HH)) {
if((! opt_Get("--1slow", \%opt_HH)) &&
(! opt_Get("--2slow", \%opt_HH))) {
die "ERROR, --mid requires one of --1slow or --2slow";
}
}
if(opt_Get("--max", \%opt_HH)) {
if((! opt_Get("--1slow", \%opt_HH)) &&
(! opt_Get("--2slow", \%opt_HH))) {
die "ERROR, --max requires one of --1slow or --2slow";
}
}
if(opt_IsUsed("--lowpdiff",\%opt_HH) || opt_IsUsed("--vlowpdiff",\%opt_HH)) {
if(opt_Get("--lowpdiff",\%opt_HH) < opt_Get("--vlowpdiff",\%opt_HH)) {
die sprintf("ERROR, with --lowpdiff <x> and --vlowpdiff <y>, <x> must be less than <y> (got <x>: %f, <y>: %f)\n",
opt_Get("--lowpdiff",\%opt_HH), opt_Get("--vlowpdiff",\%opt_HH));
}
}
if(opt_IsUsed("--lowadiff",\%opt_HH) || opt_IsUsed("--vlowadiff",\%opt_HH)) {
if(opt_Get("--lowadiff",\%opt_HH) < opt_Get("--vlowadiff",\%opt_HH)) {
die sprintf("ERROR, with --lowadiff <x> and --vlowadiff <y>, <x> must be less than <y> (got <x>: %f, <y>: %f)\n",
opt_Get("--lowadiff",\%opt_HH), opt_Get("--vlowadiff",\%opt_HH));
}
}
if((opt_IsUsed("--shortfail",\%opt_HH) && opt_IsUsed("--longfail",\%opt_HH)) &&
(opt_Get("--shortfail",\%opt_HH) >= opt_Get("--longfail",\%opt_HH))) {
die sprintf("ERROR, with --shortfail <n1> and --longfail <n2>, <n1> must be less than <n2> (got <n1>: %f, <n2>: %f)\n",
opt_Get("--shortfail",\%opt_HH), opt_Get("--longfail",\%opt_HH));
}
if(opt_IsUsed("--outxgaps", \%opt_HH)) {
if(opt_Get("--outxgaps", \%opt_HH) < 1) {
die "ERROR with --outxgaps <n>, <n> must be >= 1";
}
}
my $min_primary_sc = opt_Get("--minpsc", \%opt_HH);
my $min_secondary_sc = opt_Get("--minssc", \%opt_HH);
if($min_secondary_sc > $min_primary_sc) {
if((opt_IsUsed("--minpsc", \%opt_HH)) && (opt_IsUsed("--minssc", \%opt_HH))) {
die sprintf("ERROR, with --minpsc <x> and --minssc <y>, <x> must be less than or equal to <y> (got <x>: %f, <y>: %f)\n",
opt_Get("--minpsc",\%opt_HH), opt_Get("--minssc",\%opt_HH));
}
elsif(opt_IsUsed("--minpsc", \%opt_HH)) {
die sprintf("ERROR, with --minpsc <x>, <x> must be less than or equal to <y>=%d (default value for --minssc)\nOr you must lower <y> with the --minssc option too.\n",
opt_Get("--minpsc",\%opt_HH), opt_Get("--minssc",\%opt_HH));
}
elsif(opt_IsUsed("--minssc", \%opt_HH)) {
die sprintf("ERROR, with --minssc <y>, <x> must be greater or equal to <x>=%d (default value for --minpsc)\nOr you must lower <x> with the --minpsc option too.\n",
opt_Get("--minssc",\%opt_HH), opt_Get("--minpsc",\%opt_HH));
}
else {
die "ERROR default values for --minpsc and --minssc seem to have been altered so that the minpsc default is less than the minssc default, which is illogical."
# this will only happen if the default values in this file are changed so that --minpsc default is less than --minssc default
}
}
my $cmd = undef; # a command to be run by utl_RunCommand()
my @early_cmd_A = (); # array of commands we run before our log file is opened
my @to_remove_A = (); # array of files to remove at end
my $r1_secs = undef; # number of seconds required for round 1 search
my $r2_secs = undef; # number of seconds required for round 2 search
my $ncpu = opt_Get("-n" , \%opt_HH); # number of CPUs to use with search command (default 0: --cpu 0)
if($ncpu == 1) { $ncpu = 0; } # prefer --cpu 0 to --cpu 1
# the way we handle the $dir_out differs markedly if we have --skipsearch enabled
# so we handle that separately
if(opt_Get("--skipsearch", \%opt_HH)) {
if(-d $dir_out) {
# this is what we expect, do nothing
}
elsif(-e $dir_out) {
die "ERROR with --skipsearch, $dir_out must already exist as a directory, but it exists as a file, delete it first, then run without --skipsearch";
}
else {
die "ERROR with --skipsearch, $dir_out must already exist as a directory, but it does not. Run without --skipsearch";
}
}
else { # --skipsearch not used, normal case
# if $dir_out already exists remove it only if -f also used
if(-d $dir_out) {
$cmd = "rm -rf $dir_out";
if(opt_Get("-f", \%opt_HH)) { utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, undef); push(@early_cmd_A, $cmd); }
else { die "ERROR intended output directory named $dir_out already exists. Remove it, or use -f to overwrite it."; }
}
elsif(-e $dir_out) {
$cmd = "rm $dir_out";
if(opt_Get("-f", \%opt_HH)) { utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, undef); push(@early_cmd_A, $cmd); }
else { die "ERROR a file matching the name of the intended output directory $dir_out already exists. Remove the file, or use -f to overwrite it."; }
}
# if $dir_out does not exist, create it
if(! -d $dir_out) {
$cmd = "mkdir $dir_out";
utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, undef); push(@early_cmd_A, $cmd);
}
}
# if -p used and "time" exists (we checked above), then leave execs_H{"time"} alone, else undefine it so we won't use it
my $use_time_program = ((opt_Get("-p", \%opt_HH)) && (defined $execs_H{"time"})) ? 1 : 0;
if(! $use_time_program) { $execs_H{"time"} = undef; }
my $dir_out_tail = $dir_out;
$dir_out_tail =~ s/^.+\///; # remove all but last dir
my $out_root = $dir_out . "/" . $dir_out_tail . ".ribotyper";
#############################################
# output program banner and open output files
#############################################
# output preamble
my @arg_desc_A = ();
my @arg_A = ();
push(@arg_desc_A, "target sequence input file");
push(@arg_A, $seq_file);
push(@arg_desc_A, "output directory name");
push(@arg_A, $dir_out);
my %extra_H = ();
$extra_H{"\$RIBOSCRIPTSDIR"} = $env_riboscripts_dir;
$extra_H{"\$RIBOINFERNALDIR"} = $env_riboinfernal_dir;
$extra_H{"\$RIBOEASELDIR"} = $env_riboeasel_dir;
if($use_time_program) {
$extra_H{"\$RIBOTIMEDIR"} = $env_ribotime_dir;
}
ofile_OutputBanner(*STDOUT, $package_name, $version, $releasedate, $synopsis, $date, \%extra_H);
opt_OutputPreamble(*STDOUT, \@arg_desc_A, \@arg_A, \%opt_HH, \@opt_order_A);
# open the log and command files:
# set output file names and file handles, and open those file handles
my %ofile_info_HH = (); # hash of information on output files we created,
# 1D keys:
# "fullpath": full path to the file
# "nodirpath": file name, full path minus all directories
# "desc": short description of the file
# "FH": file handle to output to for this file, maybe undef
# 2D keys:
# "log": log file of what's output to stdout
# "cmd": command file with list of all commands executed
# open the list, log and command files
ofile_OpenAndAddFileToOutputInfo(\%ofile_info_HH, "list", $out_root . ".list", 1, 1, "List and description of all output files");
ofile_OpenAndAddFileToOutputInfo(\%ofile_info_HH, "log", $out_root . ".log", 1, 1, "Output printed to screen");
ofile_OpenAndAddFileToOutputInfo(\%ofile_info_HH, "cmd", $out_root . ".cmd", 1, 1, "List of executed commands");
my $log_FH = $ofile_info_HH{"FH"}{"log"};
my $cmd_FH = $ofile_info_HH{"FH"}{"cmd"};
# output files are all open, if we exit after this point, we'll need
# to close these first.
# now we have the log file open, output the banner there too
ofile_OutputBanner($log_FH, $package_name, $version, $releasedate, $synopsis, $date, \%extra_H);
opt_OutputPreamble($log_FH, \@arg_desc_A, \@arg_A, \%opt_HH, \@opt_order_A);
# output any commands we already executed to $log_FH
foreach $cmd (@early_cmd_A) {
print $cmd_FH $cmd . "\n";
}
# make sure the sequence, qsubinfo and modelinfo files exist
my $df_modelinfo_file = $df_model_dir . "ribotyper.modelinfo";
my $modelinfo_file = undef;
if(! opt_IsUsed("-i", \%opt_HH)) { $modelinfo_file = $df_modelinfo_file; }
else { $modelinfo_file = opt_Get("-i", \%opt_HH); }
utl_FileValidateExistsAndNonEmpty($seq_file, "sequence file", undef, 1, $ofile_info_HH{"FH"}); # '1' says: die if it doesn't exist or is empty
if(! opt_IsUsed("-i", \%opt_HH)) {
utl_FileValidateExistsAndNonEmpty($modelinfo_file, "default model info file", undef, 1, $ofile_info_HH{"FH"}); # '1' says: die if it doesn't exist or is empty
}
else { # -i used on the command line
utl_FileValidateExistsAndNonEmpty($modelinfo_file, "model info file specified with -i", undef, 1, $ofile_info_HH{"FH"}); # '1' says: die if it doesn't exist or is empty
}
# if -p, check for existence of qsub info file
my $df_qsubinfo_file = $df_model_dir . "ribo.qsubinfo";
my $qsubinfo_file = undef;
if(opt_IsUsed("-p", \%opt_HH)) {
if(! opt_IsUsed("-q", \%opt_HH)) { $qsubinfo_file = $df_qsubinfo_file; }
else { $qsubinfo_file = opt_Get("-q", \%opt_HH); }
if(! opt_IsUsed("-q", \%opt_HH)) {
utl_FileValidateExistsAndNonEmpty($qsubinfo_file, "default qsub info file", undef, 1, $ofile_info_HH{"FH"}); # '1' says: die if it doesn't exist or is empty
}
else { # -q used on the command line
utl_FileValidateExistsAndNonEmpty($qsubinfo_file, "qsub info file specified with -q", undef, 1, $ofile_info_HH{"FH"}); # 1 says: die if it doesn't exist or is empty
}
}
# we check for the existence of model file after we parse the model info file
##############################################
# determine search methods for rounds 1 and 2
##############################################
my $alg1 = undef; # can be any of "fast", "hmmonly", or "slow"
if (opt_Get("--1hmm", \%opt_HH)) { $alg1 = "hmmonly"; }
elsif(opt_Get("--1slow", \%opt_HH)) { $alg1 = "slow"; }
else { $alg1 = "fast"; }
my $alg2 = undef; # can be "hmmonly" or "slow"
if(! opt_Get("--1slow", \%opt_HH)) {
if(opt_Get("--2slow", \%opt_HH)) {
$alg2 = "slow";
}
elsif(! opt_Get("--1hmm", \%opt_HH)) {
$alg2 = "hmmonly";
}
}
##############################
# define and open output files
##############################
my $r1_unsrt_long_out_file = $out_root . ".r1.unsrt.long.out";
my $r1_unsrt_short_out_file = $out_root . ".r1.unsrt.short.out";
my $r1_srt_long_out_file = undef;
my $r1_srt_short_out_file = undef;
my $r2_unsrt_long_out_file = undef;
my $r2_unsrt_short_out_file = undef;
my $r2_srt_long_out_file = undef;
my $r2_srt_short_out_file = undef;
my $final_long_out_file = $out_root . ".long.out"; # the final long output file, created combining r1 and r2 files (or copying r1 file, if r2 is skipped)
my $final_short_out_file = $out_root . ".short.out"; # the final short output file, created combining r1 and r2 files (or copying r1 file, if r2 is skipped)
if(defined $alg2) {
$r1_srt_long_out_file = $out_root . ".r1.long.out";
$r1_srt_short_out_file = $out_root . ".r1.short.out";
$r2_unsrt_long_out_file = $out_root . ".r2.unsrt.long.out";
$r2_unsrt_short_out_file = $out_root . ".r2.unsrt.short.out";
$r2_srt_long_out_file = $out_root . ".r2.long.out";
$r2_srt_short_out_file = $out_root . ".r2.short.out";
}
else {
$r1_srt_long_out_file = $out_root . ".long.out"; # same name as $final_long_out_file, which is ok, the r1 file is the final file
$r1_srt_short_out_file = $out_root . ".short.out"; # same name as $final_short_out_file, which is ok, the r1 file is the final file
}
my $r1_unsrt_long_out_FH = undef; # output file handle for unsorted long output file for round 1
my $r1_unsrt_short_out_FH = undef; # output file handle for unsorted short output file for round 1
my $r1_srt_long_out_FH = undef; # output file handle for sorted long output file for round 1
my $r1_srt_short_out_FH = undef; # output file handle for sorted short output file for round 1
my $r2_unsrt_long_out_FH = undef; # output file handle for unsorted long output file for round 2
my $r2_unsrt_short_out_FH = undef; # output file handle for unsorted short output file for round 2
my $r2_srt_long_out_FH = undef; # output file handle for unsorted long output file for round 2
my $r2_srt_short_out_FH = undef; # output file handle for unsorted short output file for round 2
my $final_long_out_FH = undef; # output file handle for final long output file
my $final_short_out_FH = undef; # output file handle for final short output file
open($r1_unsrt_long_out_FH, ">", $r1_unsrt_long_out_file) || ofile_FileOpenFailure($r1_unsrt_long_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r1_unsrt_short_out_FH, ">", $r1_unsrt_short_out_file) || ofile_FileOpenFailure($r1_unsrt_short_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r1_srt_long_out_FH, ">", $r1_srt_long_out_file) || ofile_FileOpenFailure($r1_srt_long_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r1_srt_short_out_FH, ">", $r1_srt_short_out_file) || ofile_FileOpenFailure($r1_srt_short_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
if(defined $alg2) {
open($r2_unsrt_long_out_FH, ">", $r2_unsrt_long_out_file) || ofile_FileOpenFailure($r1_unsrt_long_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r2_unsrt_short_out_FH, ">", $r2_unsrt_short_out_file) || ofile_FileOpenFailure($r1_unsrt_short_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r2_srt_long_out_FH, ">", $r2_srt_long_out_file) || ofile_FileOpenFailure($r1_srt_long_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($r2_srt_short_out_FH, ">", $r2_srt_short_out_file) || ofile_FileOpenFailure($r1_srt_short_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($final_long_out_FH, ">", $final_long_out_file) || ofile_FileOpenFailure($final_long_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
open($final_short_out_FH, ">", $final_short_out_file) || ofile_FileOpenFailure($final_short_out_FH, "ribotyper::Main", $!, "writing", $ofile_info_HH{"FH"});
}
my $have_evalues_r1 = determine_if_we_have_evalues(1, \%opt_HH, $ofile_info_HH{"FH"});
my $have_evalues_r2 = determine_if_we_have_evalues(2, \%opt_HH, $ofile_info_HH{"FH"});
my @ufeature_A = (); # array of unexpected feature strings
my %ufeature_ct_H = (); # hash of counts of unexpected features (keys are elements of @{$ufeature_A})
initialize_ufeature_stats(\@ufeature_A, \%ufeature_ct_H, \%opt_HH);
###########################################################################
# Step 1: Parse/validate input files
###########################################################################
my $progress_w = 80; # the width of the left hand column in our progress output, hard-coded
my $start_secs = ofile_OutputProgressPrior("Validating input files", $progress_w, $log_FH, *STDOUT);
# parse model info file, which checks that all CM files exist
# variables related to fams and domains
my %family_H = (); # hash of fams, key: model name, value: name of family model belongs to (e.g. SSU)
my %domain_H = (); # hash of domains, key: model name, value: name of domain model belongs to (e.g. Archaea)
my %indi_cmfile_H = (); # hash of individual CM files; key model name: path to individual CM file for this model
my %sfetchfile_H = (); # key is model, value is name of sfetch input file to use to create $seqfile_H{$model}
my %seqfile_H = (); # key is model, value is name of sequence file search this model with for round 2, undef if none
my %nseq_H = (); # key is model, value is total number of sequences in $seqfile_H{$model}
my %totseqlen_H = (); # key is model, value is total number of nucleotides in all sequences in $seqfile_H{$model}
my $qsub_prefix = undef; # qsub prefix for submitting jobs to the farm
my $qsub_suffix = undef; # qsub suffix for submitting jobs to the farm
my $master_model_file = parse_modelinfo_file($modelinfo_file, $df_model_dir, \%family_H, \%domain_H, \%indi_cmfile_H, \%opt_HH, $ofile_info_HH{"FH"});
# parse the model file and make sure that there is a 1:1 correspondence between
# models in the models file and models listed in the model info file
if(! opt_Get("--skipval", \%opt_HH)) {
parse_and_validate_model_files($master_model_file, \%family_H, \%indi_cmfile_H, $ofile_info_HH{"FH"});
}
if(opt_Get("--onlyval", \%opt_HH)) {
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
$total_seconds += ofile_SecondsSinceEpoch();
ofile_OutputConclusionAndCloseFilesOk($total_seconds, $dir_out, \%ofile_info_HH);
exit(0);
}
# parse qsub file
if(opt_IsUsed("-p", \%opt_HH)) {
($qsub_prefix, $qsub_suffix) = ribo_ParseQsubFile($qsubinfo_file, $ofile_info_HH{"FH"});
}
# determine max width of domain, family, and classification (formed as family.domain)
my %width_H = (); # hash, key is "model" or "target", value is maximum length of any model/target
$width_H{"model"} = length("model");
$width_H{"domain"} = length("domain");
$width_H{"family"} = length("fam");
$width_H{"classification"} = length("classification");
my $model;
foreach $model (keys %domain_H) {
my $model_len = length($model);
my $domain_len = length($domain_H{$model});
my $family_len = length($family_H{$model});
my $class_len = $domain_len + $family_len + 1; # +1 is for the '.' separator
if($model_len > $width_H{"model"}) { $width_H{"model"} = $model_len; }
if($domain_len > $width_H{"domain"}) { $width_H{"domain"} = $domain_len; }
if($family_len > $width_H{"family"}) { $width_H{"family"} = $family_len; }
if($class_len > $width_H{"classification"}) { $width_H{"classification"} = $class_len; }
}
# parse input file of acceptable models, if the option --inaccept is used
my %accept_H = ();
my %question_H = ();
if(opt_IsUsed("--inaccept", \%opt_HH)) {
foreach $model (keys %domain_H) {
$accept_H{$model} = 0;
$question_H{$model} = 0;
}
parse_inaccept_file(opt_Get("--inaccept", \%opt_HH), \%accept_H, \%question_H, $ofile_info_HH{"FH"});
}
else { # --inaccept not used, all models are acceptable
foreach $model (keys %domain_H) {
$accept_H{$model} = 1;
$question_H{$model} = 0;
}
}
# create SSI index file for the sequence file
# if it already exists, delete it and create a new one, just to make sure it's valid
my $ssi_file = $seq_file . ".ssi";
if(utl_FileValidateExistsAndNonEmpty($ssi_file, undef, undef, 0, $ofile_info_HH{"FH"})) {
unlink $ssi_file;
}
utl_RunCommand($execs_H{"esl-sfetch"} . " --index $seq_file > /dev/null", opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
if(utl_FileValidateExistsAndNonEmpty($ssi_file, undef, undef, 0, $ofile_info_HH{"FH"}) != 1) {
ofile_FAIL("ERROR, tried to create $ssi_file, but failed", 1, $ofile_info_HH{"FH"});
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
###########################################################################
# Step 2: Use esl-seqstat to determine sequence lengths of all target seqs
###########################################################################
$start_secs = ofile_OutputProgressPrior("Determining target sequence lengths", $progress_w, $log_FH, *STDOUT);
my $tot_nnt = 0; # total number of nucleotides in target sequence file (summed length of all seqs)
my $nseq = 0; # total number of sequences in target sequence file
my $Z_value = 0; # total number of Mb of search space in target sequence file,
# this is (2*$tot_nnt)/1000000 (the 2 is because we search both strands)
my @seqorder_A = (); # array of sequences in order they appear in input file
my %seqidx_H = (); # key: sequence name, value: index of sequence in original input sequence file (1..$nseq)
my %seqlen_H = (); # key: sequence name, value: length of sequence,
# value multiplied by -1 after we output info for this sequence
# in round 1. Multiplied by -1 again after we output info
# for this sequence in round 2. We do this so that we know
# that 'we output this sequence already', so if we
# see it again before the next round, then we know the
# tbl file was not sorted properly. That shouldn't happen,
# but if somehow it does then we want to know about it.
my %seqgap_HHA = (); # 2D hash of arrays, 1D key is CM model name, 2K key is sequence name,
# value is array of sequence start/stop regions of gaps
# to sfetch for researching with intron models
# use esl-seqstat to determine sequence lengths
my $seqstat_file = $out_root . ".seqstat";
$tot_nnt = ribo_ProcessSequenceFile($execs_H{"esl-seqstat"}, $seq_file, $seqstat_file, \@seqorder_A, \%seqidx_H, \%seqlen_H, \%width_H, \%opt_HH, \%ofile_info_HH);
$Z_value = sprintf("%.6f", (2 * $tot_nnt) / 1000000.);
$nseq = scalar(keys %seqlen_H);
# now that we know the max sequence name length, we can output headers to the output files
output_long_headers($r1_srt_long_out_FH, 1, \%opt_HH, \%width_H, $ofile_info_HH{"FH"});
output_short_headers($r1_srt_short_out_FH, \%width_H, $ofile_info_HH{"FH"});
if(defined $alg2) {
output_long_headers($r2_srt_long_out_FH, 2, \%opt_HH, \%width_H, $ofile_info_HH{"FH"});
output_short_headers($r2_srt_short_out_FH, \%width_H, $ofile_info_HH{"FH"});
output_long_headers($final_long_out_FH, "final", \%opt_HH, \%width_H, $ofile_info_HH{"FH"});
output_short_headers($final_short_out_FH, \%width_H, $ofile_info_HH{"FH"});
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
###########################################################################
# Step 3: classify sequences using round 1 search algorithm
# determine which algorithm to use and options to use as well
# as the command for sorting the output and parsing the output
###########################################################################
# set up defaults
my $r1_searchout_file = (opt_Get("--keep", \%opt_HH)) ? $out_root . ".r1.cmsearch.out" : "/dev/null";
my $r1_tblout_file = $out_root . ".r1.cmsearch.tbl";
my $alg1_opts = determine_cmsearch_opts($alg1, \%opt_HH, $ofile_info_HH{"FH"}) . " -T $min_secondary_sc -Z $Z_value --cpu $ncpu";
my $sum_cpu_secs = undef; # if -p: summed number of elapsed CPU secs all cmsearch jobs required to finish, '0' if -p was not used
my $r1_opt_p_sum_cpu_secs = 0.;
if(! opt_Get("--skipsearch", \%opt_HH)) {
$start_secs = ofile_OutputProgressPrior(sprintf("Classifying sequences%s", (opt_Get("-p", \%opt_HH)) ? " in parallel across multiple jobs" : ""), $progress_w, $log_FH, *STDOUT);
my %info_H = ();
$info_H{"IN:seqfile"} = $seq_file;
$info_H{"IN:modelfile"} = $master_model_file;
$info_H{"OUT-NAME:tblout"} = $r1_tblout_file;
$info_H{"OUT-NAME:stdout"} = $r1_searchout_file;
$info_H{"OUT-NAME:time"} = $r1_tblout_file . ".time";
$info_H{"OUT-NAME:stderr"} = $r1_tblout_file . ".err";
$info_H{"OUT-NAME:qcmd"} = $r1_tblout_file . ".qcmd";
ribo_RunCmsearchOrCmalignOrRRnaSensorWrapper(\%execs_H, "cmsearch", $qsub_prefix, $qsub_suffix, \%seqlen_H, $progress_w, $out_root, $nseq, $tot_nnt, $alg1_opts, \%info_H, \%opt_HH, \%ofile_info_HH);
if($use_time_program) {
$r1_opt_p_sum_cpu_secs = ribo_ParseUnixTimeOutput($r1_tblout_file . ".time", $ofile_info_HH{"FH"});
}
}
else {
$start_secs = ofile_OutputProgressPrior("Skipping sequence classification (using results from previous run)", $progress_w, $log_FH, *STDOUT);
if(! -s $r1_tblout_file) {
ofile_FAIL("ERROR with --skipsearch, tblout file ($r1_tblout_file) should exist and be non-empty but it's not", 1, $ofile_info_HH{"FH"});
}
}
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r1_tblout_file);
push(@to_remove_A, $r1_tblout_file . ".time");
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1tblout", $r1_tblout_file, 0, 1, ".tblout file for round 1");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1searchout", $r1_searchout_file, 0, 1, "cmsearch output file for round 1");
}
my $extra_desc = opt_Get("-p", \%opt_HH) ? sprintf("(%.1f summed elapsed seconds for all jobs)", $r1_opt_p_sum_cpu_secs) : undef;
$r1_secs = ofile_OutputProgressComplete($start_secs, $extra_desc, $log_FH, *STDOUT);
###########################################################################
# Step 4: Sort round 1 output
###########################################################################
my $r1_sorted_tblout_file = $r1_tblout_file . ".sorted";
my $r1_sort_cmd = determine_sort_cmd($alg1, $r1_tblout_file, $r1_sorted_tblout_file, \%opt_HH, $ofile_info_HH{"FH"});
$start_secs = ofile_OutputProgressPrior("Sorting classification results", $progress_w, $log_FH, *STDOUT);
utl_RunCommand($r1_sort_cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r1_sorted_tblout_file);
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "sortedr1tblout", $r1_sorted_tblout_file, 0, 1, "sorted .tblout file for round 1");
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
###########################################################################
###########################################################################
# Step 5: Parse round 1 sorted output
###########################################################################
$start_secs = ofile_OutputProgressPrior("Processing classification results", $progress_w, $log_FH, *STDOUT);
parse_sorted_tbl_file($r1_sorted_tblout_file, $alg1, 1, \%opt_HH, \%width_H, \%seqidx_H, \%seqlen_H,
\%family_H, \%domain_H, \%accept_H, \%question_H, undef, $r1_unsrt_long_out_FH, $r1_unsrt_short_out_FH, $ofile_info_HH{"FH"});
# add data for sequences with 0 hits and then sort the output files
# based on sequence index from original input file.
output_all_hitless_targets($r1_unsrt_long_out_FH, $r1_unsrt_short_out_FH, 1, \%opt_HH, \%width_H, \%seqidx_H, \%seqlen_H, $ofile_info_HH{"FH"}); # 1: round 1
# now close the unsorted file handles (we're done with these)
# and also the sorted file handles (so we can output directly to them using system())
# Remember, we already output the headers to these files above
close($r1_unsrt_long_out_FH);
close($r1_unsrt_short_out_FH);
close($r1_srt_long_out_FH);
close($r1_srt_short_out_FH);
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r1_unsrt_long_out_file);
push(@to_remove_A, $r1_unsrt_short_out_file);
if(defined $alg2) {
push(@to_remove_A, $r1_srt_long_out_file);
push(@to_remove_A, $r1_srt_short_out_file);
}
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1_unsrt_long", $r1_unsrt_long_out_file, 0, 1, "round 1 unsorted long output file");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1_unsrt_short", $r1_unsrt_short_out_file, 0, 1, "round 1 unsorted short output file");
if(defined $alg2) {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1_srt_long", $r1_srt_long_out_file, 0, 1, "round 1 sorted long output file");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r1_srt_short", $r1_srt_short_out_file, 0, 1, "round 1 sorted short output file");
}
}
$cmd = "sort -n $r1_unsrt_short_out_file >> $r1_srt_short_out_file";
utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
$cmd = "sort -n $r1_unsrt_long_out_file >> $r1_srt_long_out_file";
utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
# reopen them, and add tails to the output files
open($r1_srt_long_out_FH, ">>", $r1_srt_long_out_file) || ofile_FileOpenFailure($r1_srt_long_out_FH, "ribotyper::Main", $!, "appending", $ofile_info_HH{"FH"});
open($r1_srt_short_out_FH, ">>", $r1_srt_short_out_file) || ofile_FileOpenFailure($r1_srt_short_out_FH, "ribotyper::Main", $!, "appending", $ofile_info_HH{"FH"});
output_long_tail($r1_srt_long_out_FH, 1, \@ufeature_A, \%opt_HH, $ofile_info_HH{"FH"}); # 1: round 1 of searching
output_short_tail($r1_srt_short_out_FH, \@ufeature_A, \%opt_HH);
close($r1_srt_short_out_FH);
close($r1_srt_long_out_FH);
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
###########################################################################
# Step 6: Parse the round 1 output to create sfetch files for fetching
# sequence sets for each model.
###########################################################################
if(defined $alg2) { # only do this if we're doing a second round of searching
if(! opt_Get("--skipsearch", \%opt_HH)) {
$start_secs = ofile_OutputProgressPrior("Fetching per-model sequence sets", $progress_w, $log_FH, *STDOUT);
}
else {
$start_secs = ofile_OutputProgressPrior("Skipping per-model sequence set fetching", $progress_w, $log_FH, *STDOUT);
}
my %seqsub_HA = (); # key is model, value is an array of all sequences to fetch to research with that model
# first initialize all arrays to empty
foreach $model (keys %family_H) {
@{$seqsub_HA{$model}} = ();
}
# fill the arrays with sequence names for each model
parse_round1_long_file($r1_srt_long_out_file, \%seqsub_HA, $ofile_info_HH{"FH"});
# create the sfetch files with sequence names
foreach $model (sort keys %family_H) {
$sfetchfile_H{$model} = undef;
if(scalar(@{$seqsub_HA{$model}}) > 0) {
$sfetchfile_H{$model} = $out_root . ".$model.sfetch";
utl_AToFile($seqsub_HA{$model}, $sfetchfile_H{$model}, 1, $ofile_info_HH{"FH"});
$totseqlen_H{$model} = ribo_SumSeqlenGivenArray($seqsub_HA{$model}, \%seqlen_H, $ofile_info_HH{"FH"});
$nseq_H{$model} = scalar(@{$seqsub_HA{$model}});
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $sfetchfile_H{$model});
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "sfetch" . $model, $sfetchfile_H{$model}, 0, 1, "sfetch file for $model");
}
}
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
foreach $model (sort keys %family_H) {
if(defined $sfetchfile_H{$model}) {
$seqfile_H{$model} = $out_root . ".$model.fa";
if(! opt_Get("--skipsearch", \%opt_HH)) {
utl_RunCommand($execs_H{"esl-sfetch"} . " -f $seq_file " . $sfetchfile_H{$model} . " > " . $seqfile_H{$model}, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $seqfile_H{$model});
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "seqfile" . $model, $seqfile_H{$model}, 0, 1, "seqfile file for $model");
}
}
}
}
}
###########################################################################
# Step 7: Do round 2 of searches, one model at a time.
###########################################################################
my $alg2_opts; # algorithm 2 cmsearch options
my @r2_model_A = (); # models we performed round 2 for
my @r2_tblout_file_A = (); # tblout files for each model we performed round 2 for
my @r2_searchout_file_A = (); # search output file for each model we performed round 2 for
my @r2_search_cmd_A = (); # search command for each model we performed round 2 for
my $r2_all_tblout_file; # file that is concatenation of all files in @r2_tblout_file_A
my $r2_all_sorted_tblout_file; # single sorted tblout file for all models we performed round 2 for
my $r2_all_sort_cmd; # sort command for $r2_all_tblout_file to create $r2_tblout_file
my $midx = 0; # counter of models in round 2
my $nr2 = 0; # number of models we run round 2 searches for
my $r2_opt_p_sum_cpu_secs = 0.;
if(defined $alg2) {
$alg2_opts = determine_cmsearch_opts($alg2, \%opt_HH, $ofile_info_HH{"FH"}) . " -T $min_secondary_sc -Z $Z_value --cpu $ncpu";;
if(! opt_Get("--skipsearch", \%opt_HH)) {
$start_secs = ofile_OutputProgressPrior(sprintf("Searching sequences against best-matching models%s", (opt_Get("-p", \%opt_HH)) ? " in parallel across multiple jobs" : ""), $progress_w, $log_FH, *STDOUT);
}
else {
$start_secs = ofile_OutputProgressPrior("Skipping sequence search (using results from previous run)", $progress_w, $log_FH, *STDOUT);
}
my $cmd = undef;
foreach $model (sort keys %family_H) {
if(defined $sfetchfile_H{$model}) {
push(@r2_model_A, $model);
push(@r2_tblout_file_A, $out_root . ".r2.$model.cmsearch.tbl");
push(@r2_searchout_file_A, ((opt_Get("--keep", \%opt_HH)) ? $out_root . ".r2.cmsearch.out" : "/dev/null"));
push(@r2_search_cmd_A, $execs_H{"cmsearch"} . " -T $min_secondary_sc -Z $Z_value --cpu $ncpu");
if(! opt_Get("--skipsearch", \%opt_HH)) {
my %info_H = ();
$info_H{"IN:seqfile"} = $seqfile_H{$model};
$info_H{"IN:modelfile"} = $indi_cmfile_H{$model};
$info_H{"OUT-NAME:tblout"} = $r2_tblout_file_A[$midx];
$info_H{"OUT-NAME:stdout"} = $r2_searchout_file_A[$midx];
$info_H{"OUT-NAME:time"} = $r2_tblout_file_A[$midx] . ".time";
$info_H{"OUT-NAME:stderr"} = $r2_tblout_file_A[$midx] . ".err";
$info_H{"OUT-NAME:qcmd"} = $r2_tblout_file_A[$midx] . ".qcmd";
ribo_RunCmsearchOrCmalignOrRRnaSensorWrapper(\%execs_H, "cmsearch", $qsub_prefix, $qsub_suffix, \%seqlen_H, $progress_w, $out_root, $nseq_H{$model}, $totseqlen_H{$model}, $alg2_opts, \%info_H, \%opt_HH, \%ofile_info_HH);
if($use_time_program) {
$r2_opt_p_sum_cpu_secs += ribo_ParseUnixTimeOutput($r2_tblout_file_A[$midx] . ".time", $ofile_info_HH{"FH"});
}
}
elsif(! -s $r2_tblout_file_A[$midx]) {
ofile_FAIL("ERROR with --skipsearch, tblout file " . $r2_tblout_file_A[$midx] . " should exist and be non-empty but it's not", 1, $ofile_info_HH{"FH"});
}
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r2_tblout_file_A[$midx]);
push(@to_remove_A, $r2_tblout_file_A[$midx] . ".time");
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2tblout" . $model, $r2_tblout_file_A[$midx], 0, 1, "$model .tblout file for round 2");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2searchout" . $model, $r2_searchout_file_A[$midx], 0, 1, "$model .tblout file for round 2");
}
$midx++;
}
}
$nr2 = $midx;
$extra_desc = opt_Get("-p", \%opt_HH) ? sprintf("(%.1f summed elapsed seconds for all jobs)", $r2_opt_p_sum_cpu_secs) : undef;
$r2_secs = ofile_OutputProgressComplete($start_secs, $extra_desc, $log_FH, *STDOUT);
# concatenate round 2 tblout files
my $cat_cmd = ""; # command used to concatenate tabular output from all round 2 searches
$r2_all_tblout_file = $out_root . ".r2.all.cmsearch.tbl";
if(defined $alg2) {
if($nr2 >= 1) {
$start_secs = ofile_OutputProgressPrior("Concatenating tabular round 2 search results", $progress_w, $log_FH, *STDOUT);
$cat_cmd = "cat $r2_tblout_file_A[0] ";
for($midx = 1; $midx < $nr2; $midx++) {
$cat_cmd .= "$r2_tblout_file_A[$midx] ";
}
$cat_cmd .= "> " . $r2_all_tblout_file;
utl_RunCommand($cat_cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r2_all_tblout_file);
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2alltblout", $r2_all_tblout_file, 0, 1, "concatenated tblout file for round 2");
}
}
}
}
###########################################################################
# Step 8: Sort round 2 output
###########################################################################
if((defined $alg2) && ($nr2 > 0)) {
$start_secs = ofile_OutputProgressPrior("Sorting search results", $progress_w, $log_FH, *STDOUT);
$r2_all_sorted_tblout_file = $r2_all_tblout_file . ".sorted";
$r2_all_sort_cmd = determine_sort_cmd($alg2, $r2_all_tblout_file, $r2_all_sorted_tblout_file, \%opt_HH, $ofile_info_HH{"FH"});
utl_RunCommand($r2_all_sort_cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r2_all_sorted_tblout_file);
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "sortedr2alltblout", $r2_all_sorted_tblout_file, 0, 1, "sorted concatenated tblout file for round 2");
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
}
###########################################################################
# Step 9: Parse round 2 sorted output
###########################################################################
if(defined $alg2) {
$start_secs = ofile_OutputProgressPrior("Processing tabular round 2 search results", $progress_w, $log_FH, *STDOUT);
if($nr2 > 0) {
parse_sorted_tbl_file($r2_all_sorted_tblout_file, $alg2, 2, \%opt_HH, \%width_H, \%seqidx_H, \%seqlen_H,
\%family_H, \%domain_H, \%accept_H, \%question_H, \%seqgap_HHA, $r2_unsrt_long_out_FH, $r2_unsrt_short_out_FH, $ofile_info_HH{"FH"});
}
# add data for sequences with 0 hits and then sort the output files
# based on sequence index from original input file.
output_all_hitless_targets($r2_unsrt_long_out_FH, $r2_unsrt_short_out_FH, 2, \%opt_HH, \%width_H, \%seqidx_H, \%seqlen_H, $ofile_info_HH{"FH"}); # 2: round 2
# now close the unsorted file handles (we're done with these)
# and also the sorted file handles (so we can output directly to them using system())
# Remember, we already output the headers to these files above
close($r2_unsrt_long_out_FH);
close($r2_unsrt_short_out_FH);
close($r2_srt_long_out_FH);
close($r2_srt_short_out_FH);
$cmd = "sort -n $r2_unsrt_short_out_file >> $r2_srt_short_out_file";
utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
$cmd = "sort -n $r2_unsrt_long_out_file >> $r2_srt_long_out_file";
utl_RunCommand($cmd, opt_Get("-v", \%opt_HH), 0, $ofile_info_HH{"FH"});
# reopen them, and add tails to the output files
# now that we know the max sequence name length, we can output headers to the output files
open($r2_srt_long_out_FH, ">>", $r2_srt_long_out_file) || ofile_FileOpenFailure($r2_srt_long_out_FH, "ribotyper::Main", $!, "appending", $ofile_info_HH{"FH"});
open($r2_srt_short_out_FH, ">>", $r2_srt_short_out_file) || ofile_FileOpenFailure($r2_srt_short_out_FH, "ribotyper::Main", $!, "appending", $ofile_info_HH{"FH"});
output_long_tail($r2_srt_long_out_FH, 2, \@ufeature_A, \%opt_HH, $ofile_info_HH{"FH"}); # 2: round 2 of searching
output_short_tail($r2_srt_short_out_FH, \@ufeature_A, \%opt_HH);
close($r2_srt_short_out_FH);
close($r2_srt_long_out_FH);
if(! opt_Get("--keep", \%opt_HH)) {
push(@to_remove_A, $r2_unsrt_long_out_file);
push(@to_remove_A, $r2_unsrt_short_out_file);
push(@to_remove_A, $r2_srt_long_out_file);
push(@to_remove_A, $r2_srt_short_out_file);
}
else {
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2_unsrt_long", $r2_unsrt_long_out_file, 0, 1, "round 2 unsorted long output file");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2_unsrt_short", $r2_unsrt_short_out_file, 0, 1, "round 2 unsorted short output file");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2_srt_long", $r2_srt_long_out_file, 0, 1, "round 2 sorted long output file");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "r2_srt_short", $r2_srt_short_out_file, 0, 1, "round 2 sorted short output file");
}
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
}
###########################################################################
# Step 10: Combine the round 1 and round 2 output files to create the
# final output file.
###########################################################################
my %class_stats_HH = (); # hash of hashes with summary statistics
# 1D key: class name (e.g. "SSU.Bacteria") or "*all*" or "*none*" or "*input*"
# 2D key: "nseq", "nnt_cov", "nnt_tot"
if(defined $alg2) {
$start_secs = ofile_OutputProgressPrior("Creating final output files", $progress_w, $log_FH, *STDOUT);
open($r1_srt_long_out_FH, $r1_srt_long_out_file) || ofile_FileOpenFailure($r1_srt_long_out_FH, "ribotyper::Main", $!, "reading", $ofile_info_HH{"FH"});
open($r1_srt_short_out_FH, $r1_srt_short_out_file) || ofile_FileOpenFailure($r1_srt_short_out_FH, "ribotyper::Main", $!, "reading", $ofile_info_HH{"FH"});
open($r2_srt_long_out_FH, $r2_srt_long_out_file) || ofile_FileOpenFailure($r2_srt_long_out_FH, "ribotyper::Main", $!, "reading", $ofile_info_HH{"FH"});
open($r2_srt_short_out_FH, $r2_srt_short_out_file) || ofile_FileOpenFailure($r2_srt_short_out_FH, "ribotyper::Main", $!, "reading", $ofile_info_HH{"FH"});
output_combined_short_or_long_file($final_short_out_FH, $r1_srt_short_out_FH, $r2_srt_short_out_FH, 1, # 1: $do_short = TRUE
undef, undef, \%width_H, \%opt_HH, $ofile_info_HH{"FH"});
output_combined_short_or_long_file($final_long_out_FH, $r1_srt_long_out_FH, $r2_srt_long_out_FH, 0, # 0: $do_short = FALSE
\%class_stats_HH, \%ufeature_ct_H, \%width_H, \%opt_HH, $ofile_info_HH{"FH"});
output_short_tail($final_short_out_FH, \@ufeature_A, \%opt_HH);
output_long_tail($final_long_out_FH, "final", \@ufeature_A, \%opt_HH, $ofile_info_HH{"FH"});
close($final_short_out_FH);
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "short_out", $final_short_out_file, 1, 1, "Short (6 column) output");
ofile_AddClosedFileToOutputInfo(\%ofile_info_HH, "long_out", $final_long_out_file, 1, 1, sprintf("Long (%d column) output", determine_number_of_columns_in_long_output_file("final", \%opt_HH, $ofile_info_HH{"FH"})));
ofile_OutputProgressComplete($start_secs, undef, $log_FH, *STDOUT);
}
else {
# no $alg2, so we only did one round of searching, fill the %class_stats_HH and %ufeature_ct_H stats hashes
open($r1_srt_long_out_FH, $r1_srt_long_out_file) || ofile_FileOpenFailure($r1_srt_long_out_FH, "ribotyper::Main", $!, "reading", $ofile_info_HH{"FH"});
get_stats_from_long_file_for_sole_round($r1_srt_long_out_FH, \%class_stats_HH, \%ufeature_ct_H, \%opt_HH, $ofile_info_HH{"FH"});
}
##################################################################
# Step 11: Fetch sequences that match to each model, if necessary.
##################################################################
my $do_outseq = opt_Get("--outseqs", \%opt_HH);
my $do_outhit = opt_Get("--outhits", \%opt_HH);
my $do_gapseq = opt_Get("--outgaps", \%opt_HH);
my $do_xgapseq = opt_IsUsed("--outxgaps", \%opt_HH);
my @pass_A = (); # array 0..$i..nseq-1, '1' if sequence with index $i+1 PASSes, '0' if it FAILs