-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathprokka
More file actions
executable file
·1466 lines (1293 loc) · 47.6 KB
/
Copy pathprokka
File metadata and controls
executable file
·1466 lines (1293 loc) · 47.6 KB
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
# prokka - Rapid bacterial genome annotation
#
# Copyright (C) 2012- Torsten Seemann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use File::Copy;
use Time::Piece;
use Time::Seconds;
use XML::Simple;
use List::Util qw(min max sum);
use Scalar::Util qw(openhandle);
use Data::Dumper;
use Bio::Root::Version;
use Bio::SeqIO;
use Bio::SearchIO;
use Bio::Seq;
use Bio::SeqFeature::Generic;
use Bio::Tools::GFF;
use FindBin;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# global variables
my @CMDLINE = @ARGV;
my $OPSYS = $^O;
my $BINDIR = "$FindBin::RealBin/../binaries/$OPSYS";
my $EXE = $FindBin::RealScript;
my $VERSION = "1.9.2-testing";
my $AUTHOR = 'Torsten Seemann <torsten.seemann@monash.edu>';
my $URL = 'http://www.vicbioinformatics.com';
my $DBDIR = "$FindBin::RealBin/../db";
my $HYPO = 'hypothetical protein';
my $UNANN = 'unannotated protein';
my $MAXCONTIGIDLEN = 38; # Genbank rule
# these should accept .faa on STDIN and write report to STDOUT
my $BLASTPCMD = "blastp -query - -db %d -evalue %e -num_threads 1 -num_descriptions 1 -num_alignments 1";
my $HMMER3CMD = "hmmscan --noali --notextw --acc -E %e --cpu 1 %d /dev/stdin";
my $rnammer_mode = 'bac';
my $barrnap_mode = 'bac';
my $aragorn_opt = '';
# debian package broke compatibility so have to force it now *grumble*
my $PARALLELCMD = "parallel --gnu";
# not used anymore
#my $INFERNALCMD = "cmscan --noali --notextw --acc -E %e --cpu 1 -o %o %d %i 2>/dev/null";
my $starttime = localtime;
my %seq;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# table of tools we need/optional and min versions
# yes - this should be in a json/yaml config file :-/
my $BIDEC = '(\d+\.\d+)'; # pattern of NN.NN for versions that can be compared
my %tools = (
'parallel' => {
GETVER => "parallel --version | grep '^GNU parallel 2'",
REGEXP => qr/GNU parallel (\d+)/,
MINVER => "20130422",
NEEDED => 1,
},
'aragorn' => {
GETVER => "aragorn -h 2>&1 | grep -i '^ARAGORN v'",
REGEXP => qr/($BIDEC)/,
MINVER => "1.2",
NEEDED => 1,
},
'rnammer' => {
GETVER => "rnammer -V 2>&1 | grep -i 'rnammer [0-9]'",
REGEXP => qr/($BIDEC)/,
MINVER => "1.2",
NEEDED => 0,
},
'barrnap' => {
GETVER => "barrnap --version 2>&1",
REGEXP => qr/($BIDEC)/,
MINVER => "0.4",
NEEDED => 0,
},
'prodigal' => {
GETVER => "prodigal -v 2>&1 | grep -i '^Prodigal V'",
REGEXP => qr/($BIDEC)/,
MINVER => "2.6",
NEEDED => 1,
},
'signalp' => {
# this is so long-winded as -v changed meaning (3.0=version, 4.0=verbose !?)
GETVER => "signalp -v < /dev/null 2>&1 | egrep ',|# SignalP' | sed 's/^# SignalP-//'",
REGEXP => qr/^($BIDEC)/,
MINVER => "3.0",
NEEDED => 0, # only if --gram used
},
'minced' => {
GETVER => "minced --version | sed -n '1p'",
REGEXP => qr/minced\s+\d+\.(\d+\.\d+)/,
MINVER => "1.3",
NEEDED => 0,
},
'cmscan' => {
GETVER => "cmscan -h | grep '^# INFERNAL'",
REGEXP => qr/INFERNAL\s+($BIDEC)/,
MINVER => "1.1",
NEEDED => 0, # only if --rfam used
},
'cmpress' => {
GETVER => "cmpress -h | grep '^# INFERNAL'",
REGEXP => qr/INFERNAL\s+($BIDEC)/,
MINVER => "1.1",
NEEDED => 0,
},
'hmmscan' => {
GETVER => "hmmscan -h | grep '^# HMMER'",
REGEXP => qr/HMMER\s+($BIDEC)/,
MINVER => "3.1",
NEEDED => 1,
},
'hmmpress' => {
GETVER => "hmmpress -h | grep '^# HMMER'",
REGEXP => qr/HMMER\s+($BIDEC)/,
MINVER => "3.1",
NEEDED => 1,
},
'blastp' => {
GETVER => "blastp -version",
REGEXP => qr/blastp:\s+($BIDEC)/,
MINVER => "2.2",
NEEDED => 1,
},
'makeblastdb' => {
GETVER => "makeblastdb -version",
REGEXP => qr/makeblastdb:\s+($BIDEC)/,
MINVER => "2.2",
NEEDED => 0, # only if --proteins used
},
'tbl2asn' => {
GETVER => "tbl2asn - | grep '^tbl2asn'",
REGEXP => qr/tbl2asn\s+($BIDEC)/,
MINVER => "21.9",
NEEDED => 1,
},
# now just the standard unix tools we need
'less' => { NEEDED=>1 },
'grep' => { NEEDED=>1 }, # yes, we need this before we can test versions :-/
'egrep' => { NEEDED=>1 },
'sed' => { NEEDED=>1 },
'find' => { NEEDED=>1 },
);
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# get (and check) versions of tools
sub check_tool {
my($toolname) = @_;
my $t = $tools{$toolname};
my $fp = find_exe($toolname);
err("Can't find required '$toolname' in your \$PATH") if !$fp and $t->{NEEDED};
if ($fp) {
$t->{HAVE} = $fp;
msg("Looking for '$toolname' - found $fp");
if ($t->{GETVER}) {
my($s) = qx($t->{GETVER});
if (defined $s) {
$s =~ $t->{REGEXP};
$t->{VERSION} = $1 if defined $1;
msg("Determined $toolname version is $t->{VERSION}");
if ($t->{VERSION} < $t->{MINVER}) {
err("Prokka needs $toolname $t->{MINVER} or higher. Please install it and try again.");
}
}
else {
err("Could not determine version of $toolname - please install version",
$t->{MINVER}, "or higher");
}
}
}
}
sub check_all_tools {
for my $toolname (sort keys %tools) {
check_tool($toolname);
}
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# command line options
my(@Options, $quiet, $kingdom, $fast, $force, $outdir, $prefix, $cpus, $addgenes,
$gcode, $gram, $gffver, $locustag, $increment, $mincontiglen, $evalue, $coverage,
$genus, $species, $strain, $plasmid,
$usegenus, $proteins, $centre, $scaffolds,
$rfam, $norrna, $notrna,
$metagenome, $compliant, $listdb, $citation);
setOptions();
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# welcome message
msg("This is $EXE $VERSION");
msg("Written by $AUTHOR");
msg("Victorian Bioinformatics Consortium - $URL");
msg("Local time is $starttime");
msg("You are", $ENV{USER} || 'not telling me who you are!');
msg("Operating system is $OPSYS");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# check BioPerl version
my $minbpver = "1.006002"; # for Bio::SearcIO::hmmer3
my $bpver = $Bio::Root::Version::VERSION;
msg("You have BioPerl $bpver");
err("Please install BioPerl $minbpver or higher") if $bpver < $minbpver;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Determine CPU cores available
my $num_cores = num_cpu();
msg("System has $num_cores cores.");
if (!defined $cpus or $cpus < 0) {
$cpus = 1;
}
elsif ($cpus == 0) {
$cpus = $num_cores;
}
elsif ($cpus > $num_cores) {
msg("Option --cpu asked for $cpus cores, but system only has $num_cores");
$cpus = $num_cores;
}
msg("Will use maximum of $cpus cores.");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# set up options based on --mode
if ($kingdom =~ m/bac|prok/i) {
$kingdom = 'Bacteria';
$gcode ||= 11;
$rnammer_mode = 'bac';
$barrnap_mode = 'bac';
}
elsif ($kingdom =~ m/arch/i) {
$kingdom = 'Archaea';
$gcode ||= 11;
$gram = '';
$rnammer_mode = 'arc';
$barrnap_mode = 'bac';
}
elsif ($kingdom =~ m/vir/i) {
$kingdom = 'Viruses';
$gcode ||= 1; # std
$gram = '';
$rnammer_mode = '';
$barrnap_mode = '';
}
elsif ($kingdom =~ m/mito|mt/i) {
$kingdom = 'Mitochondria';
$gcode ||= 5; # metazoa
$aragorn_opt = '-mt';
$gram = '';
$rnammer_mode = 'euk';
$barrnap_mode = 'mito';
}
else {
err("Can't parse --mode '$kingdom'. Choose from: Bacteria Archaea Virus Mitochondria");
}
msg("Annotating as >>> $kingdom <<<");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# check if --setupdb has been run
if ( ! -r "$DBDIR/kingdom/$kingdom/sprot.pin" or ! "$DBDIR/hmm/HAMAP.hmm.h3i") {
err("The sequence databases have not been indexed. Please run 'prokka --setupdb' first.");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# check options
if ($compliant) {
msg("Enabling options to ensure Genbank/ENA/DDJB submission compliance.");
$addgenes = 1;
$centre ||= 'Prokka';
$mincontiglen = 200;
}
#$centre or err("You must set --centre or the NCBI tbl2asn tool won't work properly, sorry.");
($gcode < 1 or $gcode > 24) and err("Invalid genetic code, must be 1..24");
$evalue >= 0 or err("Invalid --evalue, must be >= 0");
#($coverage >= 0 and $coverage <= 100) or err("Invalid --coverage, must be 0..100");
$increment >= 1 or err("Invalid --increment, must be >= 1");
$locustag ||= uc($EXE);
# http://www.ncbi.nlm.nih.gov/genomes/static/Annotation_pipeline_README.txt
$prefix ||= $locustag.'_'.(localtime->mdy('')); # NCBI wants US format, ech.
$outdir ||= $prefix;
if (-d $outdir) {
if ($force) {
msg("Re-using existing --outdir $outdir")
}
else {
err("Folder '$outdir' already exists! Please change --outdir or use --force");
}
}
else {
msg("Creating new output folder: $outdir");
runcmd("mkdir -p \Q$outdir\E");
}
msg("Using filename prefix: $prefix.XXX");
# canonical names
$genus = ucfirst(lc($genus)) if $genus;
$species = lc($species) if $strain;
msg("Setting HMMER_NCPU=1");
$ENV{HMMER_NCPU} = 1;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# set up log file
my $logfile = "$outdir/$prefix.log";
msg("Writing log to: $logfile");
open LOG, '>', $logfile or err("Can't open logfile");
msg("This is $EXE $VERSION");
msg("Command: @CMDLINE");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# add our included binaries to the END of the PATH
if (-d $BINDIR) {
msg("Extending PATH: $BINDIR");
$ENV{PATH} .= ":$BINDIR:$BINDIR/../common";
}
check_all_tools();
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# check if optional tools are installed if the option was enabled
# (this should be in the hash above, but we do it manually for this release)
if ($proteins and ! $tools{'makeblastdb'}->{HAVE}) {
err("You need to install 'makeblastdb' to use the --proteins option.");
}
if ($rfam and ! $tools{'cmscan'}->{HAVE}) {
err("You need to install 'cmscan' to use the --rfam option.");
}
if ($gram and ! $tools{'signalp'}->{HAVE}) {
err("You need to install 'signalp' to use the --gram option.");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# not sure why this is this far down, will leave for now
$gcode ||= 1;
msg("Using genetic code table $gcode.");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# read in sequences; remove small contigs; replace ambig with N
my $in = shift @ARGV or err("Please supply a contig fasta file on the command line.");
(-r $in and !-d _ and -s _) or err("'$in' is not a readable non-empty FASTA file");
msg("Loading and checking input file: $in");
my $fin = Bio::SeqIO->new(-file=>$in, -format=>'fasta');
my $fout = Bio::SeqIO->new(-file=>">$outdir/$prefix.fna", -format=>'fasta');
my $ncontig = 0;
my $contigprefix = $locustag || $prefix || $outdir || $strain || '';
$contigprefix .= '_' if $contigprefix;
my $contig_name_len = length($centre) + 1 + length($contigprefix) + 6;
if ($compliant and $contig_name_len > $MAXCONTIGIDLEN) {
err("Genbank contig IDs are $contig_name_len chars, must be <= $MAXCONTIGIDLEN. Prefix is: $contigprefix");
}
while (my $seq = $fin->next_seq) {
if ($seq->length < $mincontiglen) {
msg("Skipping short (<$mincontiglen bp) contig:",$seq->display_id);
next;
}
$ncontig++;
# http://www.ncbi.nlm.nih.gov/genomes/static/Annotation_pipeline_README.txt
# leave contigs names as-is unless they are in --compliant mode or want --centre set
if ($centre) {
$seq->id( sprintf "gnl|$centre|${contigprefix}contig%06d", $ncontig );
}
if (length($seq->id) > $MAXCONTIGIDLEN) {
msg("WARNING: Contig IDs must be less than 38 characters for Genbank compliance")
}
my $s = $seq->seq;
$s = uc($s);
$s =~ s/[*-]//g; # replace pads/gaps with nothing
$s =~ s/[^ACTG]/N/g; # replace wacky IUPAC with N
$seq->seq($s);
$seq->desc(undef);
$fout->write_seq($seq);
if (exists $seq{$seq->id}) {
err("Uh oh! Sequence file '$in' contains duplicate sequence ID:", $seq->id);
}
$seq{ $seq->id }{DNA} = $seq;
}
$ncontig > 0 or err("FASTA file '$in' contains no suitable sequence entries");
msg("Wrote $ncontig contigs");
#msg(sort keys %seq); exit;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# tRNA + tmRNA
if ($notrna) {
msg("Skipping tRNA search at user request.");
}
else {
msg("Predicting tRNAs and tmRNAs");
my $cmd = "aragorn -gc$gcode $aragorn_opt -w $outdir/$prefix.fna"; # -t/-m
msg("Running: $cmd");
my $num_trna=0;
open TRNA, "$cmd |";
my $sid;
while (<TRNA>) {
chomp;
if (m/^>(\S+)/) {
$sid = $1;
next;
}
my @x = split m/\s+/;
next unless @x == 5 and $x[0] =~ m/^\d+$/;
# and $x[4] =~ m/^\([ATCG]{3}\)$/i;
#msg($_);
msg("@x");
$x[2] =~ m/(c)?\[(\d+),(\d+)\]/;
my($revcom, $start, $end) = ($1,$2,$3);
# bug fix for aragorn when revcom trna ends at start of contig!
# if (defined $revcom and $start > $end) {
# msg("Activating kludge for Aragorn bug for tRNA end at contig start");
# $start = 1;
# }
if ($start > $end) {
msg("tRNA $x[2] has start($start) > end ($end) - skipping.");
next;
}
if (abs($end-$start) > 500) {
msg("tRNA/tmRNA $x[2] is too big (>500bp) - skipping.");
next;
}
# end kludge
$num_trna++;
my $ftype = 'tRNA';
my $product = $x[1].$x[4];
my @gene = ();
if ($x[1] =~ m/^(tmRNA)/) {
$ftype = $1;
$product = "transfer-messenger RNA, SsrA";
@gene = ('gene' => 'ssrA')
}
my $tool = "Aragorn:".$tools{aragorn}->{VERSION};
push @{$seq{$sid}{FEATURE}}, Bio::SeqFeature::Generic->new(
-primary => $ftype, # tRNA or tmRNA
-seq_id => $sid,
-source => $tool,
-start => $start,
-end => $end,
-strand => (defined $revcom ? -1 : +1),
-score => undef,
-frame => 0,
-tag => {
'product' => $product,
'inference' => "COORDINATES:profile:$tool",
@gene,
}
);
}
msg("Found $num_trna tRNAs");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# rRNA
if ($kingdom ne 'Viruses' and !$norrna) {
msg("Predicting Ribosomal RNAs");
if ($tools{'barrnap'}->{HAVE}) {
msg("Running Barrnap with $cpus threads");
my $num_rrna=0;
my $gff = Bio::Tools::GFF->new(
-file => "barrnap --kingdom $barrnap_mode --threads $cpus --quiet $outdir/$prefix.fna |",
-gff_version => 3,
);
while (my $feat = $gff->next_feature) {
$feat->remove_tag('Name'); # only want /product
push @{$seq{$feat->seq_id}{FEATURE}}, $feat;
$num_rrna++;
msg("$num_rrna", $feat->seq_id, $feat->start, $feat->get_tag_values('product'));
}
msg("Found $num_rrna rRNAs");
}
elsif ($tools{'rnammer'}->{HAVE}) {
msg("Running RNAmmer");
my $rnammerfn = "$outdir/rnammer.xml";
my $num_rrna = 0;
my $rnammer_opt = $cpus != 1 ? "-multi" : "";
runcmd("rnammer -S $rnammer_mode $rnammer_opt -xml $rnammerfn $outdir/$prefix.fna");
my $xml = XML::Simple->new(ForceArray => 1);
my $data = $xml->XMLin($rnammerfn);
for my $entry (@{$data->{entries}[0]->{entry}}) {
my $sid = $entry->{sequenceEntry}[0];
next unless exists $seq{$sid};
my $desc = $entry->{mol}[0];
$desc =~ s/s_r/S ribosomal /i; # make it English '23S_rRNA => 23S ribosomal RNA'
$num_rrna++;
my $tool = "RNAmmer:".$tools{rnammer}->{VERSION};
push @{$seq{$sid}{FEATURE}}, Bio::SeqFeature::Generic->new(
-primary => 'rRNA',
-seq_id => $sid,
-source => $tool, # $data->{predictor}[0]
-start => $entry->{start}[0],
-end => $entry->{stop}[0],
-strand => $entry->{direction}[0],
-score => undef, # $entry->{score}[0],
-frame => 0,
-tag => {
'product' => $desc,
'inference' => "COORDINATES:profile:$tool", # FIXME version ?
}
);
msg(join "\t", $num_rrna, $desc, $sid, $entry->{start}[0], $entry->{stop}[0], $entry->{direction}[0]);
}
delfile($rnammerfn);
msg("Found $num_rrna rRNAs");
}
else {
msg("You need either Barrnap or RNAmmer installed to predict rRNAs!");
}
}
else {
msg("Disabling rRNA search: --kingdom=$kingdom or --norrna=$norrna");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# ncRNA via Rfam + Infernal
my $cmdb = "$DBDIR/cm/$kingdom";
if ($rfam) {
if (-r "$cmdb.i1m") {
msg("Scanning for ncRNAs... please be patient.");
my $num_ncrna = 0;
my $tool = "Infernal:".$tools{'cmscan'}->{VERSION};
my $icpu = $cpus || 1;
open INFERNAL, "cmscan --cpu $icpu -E $evalue --tblout /dev/stdout -o /dev/null --noali $cmdb $outdir/$prefix.fna |";
while (<INFERNAL>) {
next if m/^#/; # ignore comments
my @x = split ' '; # magic Perl whitespace splitter
# msg("DEBUG: ", join("~~~", @x) );
next unless @x > 9; # avoid incorrect lines
next unless defined $x[1] and $x[1] =~ m/^RF\d/;
my $sid = $x[2];
next unless exists $seq{$sid};
push @{$seq{$sid}{FEATURE}}, Bio::SeqFeature::Generic->new(
-primary => 'misc_RNA',
-seq_id => $sid,
-source => $tool,
-start => min($x[7], $x[8]),
-end => max($x[7], $x[8]),
-strand => ($x[9] eq '-' ? -1 : +1),
-score => undef, # possibly x[16] but had problems here with '!'
-frame => 0,
-tag => {
'product' => $x[0],
'inference' => "COORDINATES:profile:$tool",
}
);
$num_ncrna++;
msg("$num_ncrna ncRNA $x[0] $sid $x[7]..$x[8]");
}
msg("Found $num_ncrna ncRNAs.");
}
else {
msg("Disabling ncRNA search, can't find $cmdb index file.");
}
}
else {
msg("Skipping ncRNA search, enable with --rfam if desired.");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Tally all the RNA features __ which we want to exclude overlaps with CDS __
my @allrna;
for my $sid (sort keys %seq) {
push @allrna, (grep { $_->primary_tag =~ m/[tr]RNA/ } @{ $seq{$sid}{FEATURE} });
}
msg("Total of", scalar(@allrna), "tRNA + rRNA features");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# CRISPRs
if ($tools{'minced'}->{HAVE}) {
msg("Searching for CRISPR repeats");
my $num_crispr=0;
my $gff = Bio::Tools::GFF->new(
-file => "minced -gff '$outdir/$prefix.fna' |",
-gff_version => 3,
);
while (my $feat = $gff->next_feature) {
# format it properly for NCBI
$feat->primary_tag("repeat_region");
$feat->remove_tag('ID');
$feat->add_tag_value('rpt_family', 'CRISPR');
push @{$seq{$feat->seq_id}{FEATURE}}, $feat;
# there should be no CDS features overlapping with CRISPRs, but prodigal
# will occasionally create ORFs in these regions. Got to stop that.
push @allrna, $feat;
$num_crispr++;
msg("CRISPR$num_crispr", $feat->seq_id, $feat->start, "with", $feat->score, "spacers");
}
msg("Found $num_crispr CRISPRs");
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# CDS
msg("Predicting coding sequences");
my $totalbp = sum( map { $seq{$_}{DNA}->length } keys %seq);
my $prodigal_mode = ($totalbp >= 100000 && !$metagenome) ? 'single' : 'meta';
msg("Contigs total $totalbp bp, so using $prodigal_mode mode");
my $num_cds=0;
my $cmd = "prodigal -i $outdir/$prefix.fna -c -m -g $gcode -p $prodigal_mode -f sco -q";
msg("Running: $cmd");
open CDS, "$cmd |";
my $sid;
while (<CDS>) {
if (m/seqhdr="([^\s\"]+)"/) {
$sid = $1;
# msg("CDS $sid");
next;
}
elsif (m/^>\d+_(\d+)_(\d+)_([+-])$/) {
my $tool = "Prodigal:".$tools{prodigal}->{VERSION}; # FIXME: why inner loop?
my $cds = Bio::SeqFeature::Generic->new(
-primary => 'CDS',
-seq_id => $sid,
-source => $tool,
-start => $1,
-end => $2,
-strand => ($3 eq '+' ? +1 : -1),
-score => undef,
-frame => 0,
-tag => {
'inference' => "ab initio prediction:$tool",
}
);
my $overlap;
for my $rna (@allrna) {
# same contig, overlapping (could check same strand too? not sure)
if ($rna->seq_id eq $sid and $cds->overlaps($rna)) {
$overlap = $rna;
last;
}
}
# mitochondria are highly packed, so don't exclude as CDS/tRNA often overlap.
if ($overlap and $kingdom ne 'Mitochondria') {
my $type = $overlap->primary_tag;
msg("Excluding CDS which overlaps existing RNA ($type) at $sid:$1..$2 on $3 strand");
}
else {
$num_cds++;
push @{$seq{$sid}{FEATURE}}, $cds;
## BUG James Doonan - ensure no odd features extending beyond contig
if ($cds->end > $seq{$cds->seq_id}{DNA}->length ) {
err("CDS end", $cds->end, "is beyond length", $seq{$sid}{DNA}->length, "in contig $sid")
}
}
}
}
msg("Found $num_cds CDS");
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Connect features to their parent sequences
msg("Connecting features back to sequences");
for my $sid (sort keys %seq) {
for my $f (@{ $seq{$sid}{FEATURE} }) {
$f->attach_seq( $seq{$sid}{DNA} );
}
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Find signal peptide leader sequences
if ($tools{signalp}->{HAVE}) {
my $sigpver = substr $tools{signalp}{VERSION}, 0, 1; # first char, expect 3 or 4
if ($kingdom eq 'Bacteria' and $sigpver==3 || $sigpver==4) {
if ($gram) {
$gram = $gram =~ m/\+|[posl]/i ? 'gram+' : 'gram-';
msg("Looking for signal peptides at start of predicted proteins");
msg("Treating $kingdom as $gram");
my $spoutfn = "$outdir/signalp.faa";
my $spout = Bio::SeqIO->new(-file=>">$spoutfn", -format=>'fasta');
my %cds;
my $count=0;
for my $sid (sort keys %seq) {
for my $f (@{ $seq{$sid}{FEATURE} }) {
next unless $f->primary_tag eq 'CDS';
$cds{++$count} = $f;
my $seq = $f->seq->translate(-codontable_id=>$gcode, -complete => 1);
$seq->display_id($count);
$spout->write_seq($seq);
}
}
my $opts = $sigpver==3 ? '-m hmm' : '';
my $cmd = "signalp -t $gram -f short $opts $spoutfn 2> /dev/null";
msg("Running: $cmd");
my $tool = "SignalP:".$tools{signalp}->{VERSION};
my $num_sigpep = 0;
open SIGNALP, "$cmd |";
while (<SIGNALP>) {
my @x = split m/\s+/;
if ($sigpver == 3) {
next unless @x == 7 and $x[6] eq 'Y'; # has sig_pep
my $parent = $cds{ $x[0] };
my $prob = $x[5];
my $cleave = $x[3];
my $start = $parent->strand > 0 ? $parent->start : $parent->end;
my $end = $start + $parent->strand * ($cleave - 1);
my $sigpep = Bio::SeqFeature::Generic->new(
-seq_id => $parent->seq_id,
-source_tag => $tool,
-primary => 'sig_peptide',
-start => min($start, $end),
-end => max($start, $end),
-strand => $parent->strand,
-frame => 0, # PHASE: compulsory for peptides, can't be '.'
-tag => {
# 'ID' => $ID,
# 'Parent' => $x[0], # don't have proper IDs yet....
'product' => "putative signal peptide",
'inference' => "ab initio prediction:$tool",
'note' => "predicted cleavage at residue $x[3] with probability $prob",
}
);
push @{$seq{$parent->seq_id}{FEATURE}}, $sigpep;
$num_sigpep++;
}
else {
# msg("sigp$sigpver: @x");
next unless @x==12 and $x[9] eq 'Y'; # has sig_pep
my $parent = $cds{ $x[0] };
my $cleave = $x[2];
my $start = $parent->strand > 0 ? $parent->start : $parent->end;
my $end = $start + $parent->strand * ($cleave - 1);
my $sigpep = Bio::SeqFeature::Generic->new(
-seq_id => $parent->seq_id,
-source_tag => $tool,
-primary => 'sig_peptide',
-start => min($start, $end),
-end => max($start, $end),
-strand => $parent->strand,
-frame => 0, # PHASE: compulsory for peptides, can't be '.'
-tag => {
# 'ID' => $ID,
# 'Parent' => $x[0], # don't have proper IDs yet....
'product' => "putative signal peptide",
'inference' => "ab initio prediction:$tool",
'note' => "predicted cleavage at residue $x[2]",
}
);
push @{$seq{$parent->seq_id}{FEATURE}}, $sigpep;
$num_sigpep++;
}
}
msg("Found $num_sigpep signal peptides");
delfile($spoutfn);
}
else {
msg("Option --gram not specified, will NOT check for signal peptides.");
}
}
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Annotate CDS
# primary data source is a curated subset of uniprot (evidence <= 1 per Phylum)
my @database = (
{
DB => "$DBDIR/kingdom/$kingdom/sprot",
SRC => 'similar to AA sequence:UniProtKB:',
FMT => 'blast',
CMD => $BLASTPCMD,
},
);
# secondary sources are a series of HMMs
unless ($kingdom eq 'Viruses') {
for my $name (qw(HAMAP CLUSTERS Pfam)) {
push @database, {
DB => "$DBDIR/hmm/$name.hmm",
SRC => "protein motif:$name:",
FMT => 'hmmer3',
CMD => $HMMER3CMD,
VERSION => 3, # without this, latest Bioperl goes into infinite loop
};
}
}
# if --usegenus is enabled
# AND user supplies a genus, and we have a custom file (from GenBank) do it first!
if ($usegenus) {
if ($genus and -r "$DBDIR/genus/$genus.pin") {
my $blastdb = "$DBDIR/genus/$genus";
msg("Using custom $genus database for annotation");
unshift @database, {
DB => $blastdb,
SRC => 'similar to AA sequence:RefSeq:',
FMT => 'blast',
CMD => $BLASTPCMD,
};
}
else {
msg("Skipping genus-specific proteins as can't see $DBDIR/$genus");
}
}
else {
msg("Not using genus-specific database. Try --usegenus to enable it.");
}
# if user supplies a trusted set of proteins, we try these first!
if (-r $proteins) {
msg("Preparing user-supplied primary annotation source: $proteins");
runcmd("makeblastdb -dbtype prot -in '$proteins' -out '$outdir/proteins' -logfile /dev/null");
my $src = $proteins;
$src =~ s{^.*/}{};
msg("Using /inference source as '$src'");
unshift @database, {
DB => "$outdir/proteins",
SRC => "similar to AA sequence:$src:",
FMT => 'blast',
CMD => $BLASTPCMD,
};
}
if ($fast) {
msg("Option --fast enabled, so skipping CDS similarity searches");
}
else {
msg("Annotating CDS, please be patient.");
msg("Will use", ($cpus > 0 ? $cpus : 'all available'), "CPUs for similarity searching.");
# for each sequence/profile database in order,
for my $db (@database) {
# we write out all the CDS which haven't been annotated yet and then search them
my $faa_name = "$outdir/proteins.faa";
open my $faa, '>', $faa_name;
my %cds;
my $count=0;
for my $sid (sort keys %seq) {
for my $f (@{ $seq{$sid}{FEATURE} }) {
next unless $f->primary_tag eq 'CDS';
next if $f->has_tag('product');
$cds{++$count} = $f;
print $faa ">$count\n",$f->seq->translate(-codontable_id=>$gcode, -complete => 1)->seq,"\n";
}
}
close $faa;
next if $count <= 0;
msg("There are still $count unannotated CDS left (started with $num_cds)");
msg("Will use", $db->{FMT}, "to search against", $db->{DB}, "with $cpus CPUs");
my $cmd = $db->{CMD};
# $cmd =~ s/%i/{}/g;
# $cmd =~ s/%o/{}.out/g;
$cmd =~ s/%e/$evalue/g;
$cmd =~ s,%d,$db->{DB},g;
#
# **** PARALLEL RUN! ****
#
my $faa_bytes = -s $faa_name;
my $bsize = int($faa_bytes / $cpus / 2); # div 2 to allow for slow vs fast subtasks?
my $paropts = $cpus > 0 ? " -j $cpus" : "";
my $bls_name = "$outdir/proteins.bls";
runcmd("cat $faa_name | ${PARALLELCMD}$paropts --block $bsize --recstart '>' --pipe $cmd > $bls_name 2> /dev/null");
my $num_cleaned=0;
my $bls = Bio::SearchIO->new(-file=>$bls_name, -format=>$db->{FMT}, -version=>$db->{VERSION});
while (my $res = $bls->next_result) {
my $hit = $res->next_hit or next;
my($pid,$prod,$gene,$EC,@ec_numbers) = ($res->query_name, $hit->description, '', '', '');
if ($prod =~ m/~~~/) {
($EC,$gene,$prod) = split m/~~~/, $prod;
@ec_numbers = split m/;/, $EC;
foreach (@ec_numbers) {
$_ =~ s/n\d+/-/g; # collapse transitionary EC numbers
}
}
my $cleanprod = cleanup_product($prod);
$cds{$pid}->add_tag_value('product', $cleanprod);
foreach my $ec (@ec_numbers) {
next if ($ec eq ''); # CLUSTERS and Pfam hmm searches introduce empty EC_number tags in output tbl/gff
$cds{$pid}->add_tag_value('EC_number', $ec);
}
$cds{$pid}->add_tag_value('gene', $gene) if $gene;
$cds{$pid}->add_tag_value('inference', $db->{SRC}.$hit->name);
if ($cleanprod ne $prod) {
msg("Modify product: $prod => $cleanprod");
# we remove any special /gene or /EC if the /product is 'hypothetical protein' !
if ($cleanprod eq $HYPO) {
$cds{$pid}->add_tag_value('note', $prod);
# I think I still need to do this to cope with dodgy anno sources
$cds{$pid}->remove_tag('gene') if $cds{$pid}->has_tag('gene');
$cds{$pid}->remove_tag('EC_number') if $cds{$pid}->has_tag('EC_number'); # removes all EC numbers
}
$num_cleaned++;
}
}
msg("Cleaned $num_cleaned /product names") if $num_cleaned > 0;
delfile( $faa_name, $bls_name);
}
}
if ($proteins) {
delfile( map { "$outdir/proteins.$_" } qw(psq phr pin) );
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Label unannotated proteins as 'hypothetical protein'
my $empty_label = $fast ? 'unannotated protein' : $HYPO;
my $num_hypo=0;
for my $sid (sort keys %seq) {
for my $f ( @{ $seq{$sid}{FEATURE} }) {
if ($f->primary_tag eq 'CDS' and not $f->has_tag('product')) {
$f->add_tag_value('product', $empty_label);
$num_hypo++;
}
}
}
msg("Labelling remaining $num_hypo proteins as '$empty_label'") if $num_hypo > 0;
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Look for possible /pseudo genes - adjacent with same annotation
for my $sid (sort keys %seq) {
my $prev = '';
for my $f ( grep { $_->primary_tag eq 'CDS' } @{ $seq{$sid}{FEATURE} } ) {
my $this = TAG($f, 'product');
if ($this eq $prev and $this ne $HYPO and $this ne $UNANN) {
msg("Possible /pseudo '$prev' at", $f->seq_id, 'position', $f->start);
}
$prev = $this;
$this = '';
}
}
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
# Fix colliding /gene names in CDS (before we add 'gene' features)
# (this could be written as such a nice map/map/grep one day...)
my %collide;
for my $sid (sort keys %seq) {
for my $f ( sort { $a->start <=> $b->start } @{ $seq{$sid}{FEATURE} }) {
next unless $f->primary_tag eq 'CDS';
my $gene = TAG($f, 'gene') or next;
push @{ $collide{$gene} }, $f;
}
}
msg("Found", scalar(keys(%collide)), "unique /gene codes.");
my $num_collide=0;
for my $gene (keys %collide) {
my @cds = @{$collide{$gene}};
next unless @cds > 1;
my $n=0;
for my $f (@cds) {
$f->remove_tag('gene');
$n++;