Newer
Older
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
#!/usr/bin/perl
# this script takes a fasta file with contigs and the output of Barrnap runs for different kingdoms to produce a fasta file with rRNA sequences and a .tab file in the style of prodigal
# 6 inputs: - the fasta file of the contigs
# - the .gff files from Barrnap (for eukaryota, bacteria, archaea and mitochondria)
# - the prefix for the output
# 2 outputs: - a fasta file with the rRNA gene sequences, naming is the contig name appended with _r and a continuous number per contig
# - a table with contig name rRNA gene name (see above), the sense, length, start position, end position, completeness and kind (16S, 5S etc)
# Anna Heintz-Buschart, June 2016, based on code written in November 2014
use strict;
use warnings;
use Bio::DB::Fasta;
use List::MoreUtils qw(uniq);
use List::Util qw(min max);
use Getopt::Long;
my ($fastaFile,$eukFile,$arcFile,$bacFile,$mitoFile,$output);
GetOptions('f=s' => \$fastaFile,
'e=s' => \$eukFile,
'a=s' => \$arcFile,
'b=s' => \$bacFile,
'm=s' => \$mitoFile,
'o=s' => \$output);
my $listFile = $output.".tab";
my $geneFile = $output.".fa";
my %rRNAs = (); #0: start, 1: end, 2: sense, 3:kind; 4:eval
open (IN, $bacFile);
while (my $line = <IN>){
unless ($line =~ /^#/) {
chomp $line;
my($contig, undef, undef, $start, $end, $eval, $sense, undef, $attribute) = split "\t", $line;
my $completeness = "";
if ($attribute =~ /partial/){
$completeness = " incomplete"
} else {
$completeness = " complete"
}
my @attributes = split ";", $attribute,2;
my $kind = $attributes[0];
substr($kind, 0 , 5) = "";
$kind .= " bacterial".$completeness;
push @{$rRNAs{$contig}[0]}, $start;
push @{$rRNAs{$contig}[1]}, $end;
push @{$rRNAs{$contig}[2]}, $sense;
push @{$rRNAs{$contig}[3]}, $kind;
push @{$rRNAs{$contig}[4]}, $eval;
}
}
close(IN);
open (IN, $arcFile);
while (my $line = <IN>){
unless ($line =~ /^#/) {
chomp $line;
my($contig, undef, undef, $start, $end, $eval, $sense, undef, $attribute) = split "\t", $line;
my $completeness = "";
if ($attribute =~ /partial/){
$completeness = " incomplete"
} else {
$completeness = " complete"
}
my @attributes = split ";", $attribute,2;
my $kind = $attributes[0];
substr($kind, 0 , 5) = "";
$kind .= " archaeal".$completeness;
push @{$rRNAs{$contig}[0]}, $start;
push @{$rRNAs{$contig}[1]}, $end;
push @{$rRNAs{$contig}[2]}, $sense;
push @{$rRNAs{$contig}[3]}, $kind;
push @{$rRNAs{$contig}[4]}, $eval;
}
}
close(IN);
open (IN, $eukFile);
while (my $line = <IN>){
unless ($line =~ /^#/) {
chomp $line;
my($contig, undef, undef, $start, $end, $eval, $sense, undef, $attribute) = split "\t", $line;
my $completeness = "";
if ($attribute =~ /partial/){
$completeness = " incomplete"
} else {
$completeness = " complete"
}
my @attributes = split ";", $attribute,2;
my $kind = $attributes[0];
substr($kind, 0 , 5) = "";
$kind .= " eukaryotic".$completeness;
push @{$rRNAs{$contig}[0]}, $start;
push @{$rRNAs{$contig}[1]}, $end;
push @{$rRNAs{$contig}[2]}, $sense;
push @{$rRNAs{$contig}[3]}, $kind;
push @{$rRNAs{$contig}[4]}, $eval;
}
}
close(IN);
open (IN, $mitoFile);
while (my $line = <IN>){
unless ($line =~ /^#/) {
chomp $line;
my($contig, undef, undef, $start, $end, $eval, $sense, undef, $attribute) = split "\t", $line;
my $completeness = "";
if ($attribute =~ /partial/){
$completeness = " incomplete"
} else {
$completeness = " complete"
}
my @attributes = split ";", $attribute,2;
my $kind = $attributes[0];
substr($kind, 0 , 5) = "";
$kind .= " mitochondrial".$completeness;
push @{$rRNAs{$contig}[0]}, $start;
push @{$rRNAs{$contig}[1]}, $end;
push @{$rRNAs{$contig}[2]}, $sense;
push @{$rRNAs{$contig}[3]}, $kind;
push @{$rRNAs{$contig}[4]}, $eval;
}
}
close(IN);
my %allContigs = ();
my $db = Bio::DB::Fasta->new( $fastaFile );
open(GEN, ">", $geneFile) or die "cannot open $geneFile \n";
open(TAB, ">", $listFile) or die "cannot open $listFile \n";
print TAB "contig\tgene\tsense\tlength\tstart\tend\teval\tkind\n";
my @ids = keys %rRNAs;
foreach my $contig (@ids){
my @useStarts = ();
my @useEnds = ();
my @useSenses = ();
my @useKinds = ();
my @useEvals = ();
if ($#{$rRNAs{$contig}[0]} > 0) {
my @starts = @{$rRNAs{$contig}[0]};
my @ends = @{$rRNAs{$contig}[1]};
my @senses = @{$rRNAs{$contig}[2]};
my @kinds = @{$rRNAs{$contig}[3]};
my @evals = @{$rRNAs{$contig}[4]};
my @idx = sort { $starts[$a]+0 <=> 0+$starts[$b] } 0 .. $#starts;
@starts = @starts[@idx];
@ends = @ends[@idx];
@senses = @senses[@idx];
@kinds = @kinds[@idx];
@evals = @evals[@idx];
my $currentStart = $starts[0];
my $currentEnd = $ends[0];
my @currentAllStarts = ();
my @currentAllEnds = ();
my @currentSenses = ();
my @currentKinds = ();
my @currentEvals = ();
for (my $i = 0; $i <= $#starts; $i++) {
if ($starts[$i] <= $currentEnd){
$currentEnd = $ends[$i] if ($ends[$i] >= $currentEnd);
push @currentAllStarts, $starts[$i];
push @currentAllEnds, $ends[$i];
push @currentSenses, $senses[$i];
push @currentKinds, $kinds[$i];
push @currentEvals, $evals[$i];
} else {
my @ind = ();
my $testEval = $currentEvals[0];
for (my $m = 0; $m <= $#currentEvals; $m++){
if ($testEval == $currentEvals[$m]) {
push @ind, $m;
} elsif ($testEval > $currentEvals[$m]) {
@ind = ($m);
$testEval = $currentEvals[$m];
}
}
unshift @useEvals, $testEval;
if ($#ind > 0) {
my @uniqueSenses = uniq @currentSenses;
my @uniqueKinds = uniq @currentKinds;
unshift @useSenses, join('',@uniqueSenses);
unshift @useKinds, join(';',@uniqueKinds);
unshift @useStarts, min @currentAllStarts[@ind];
unshift @useEnds, max @currentAllEnds[@ind];
} else {
unshift @useSenses, $currentSenses[$ind[0]];
unshift @useKinds, $currentKinds[$ind[0]];
unshift @useStarts, $currentAllStarts[$ind[0]];
unshift @useEnds, $currentAllEnds[$ind[0]];
}
$currentStart = $starts[$i];
$currentEnd = $ends[$i];
@currentAllStarts = ($starts[$i]);
@currentAllEnds = ($ends[$i]);
@currentSenses = ($senses[$i]);
@currentKinds = ($kinds[$i]);
@currentEvals = ($evals[$i]);
}
}
my @ind = ();
my $testEval = $currentEvals[0];
for (my $m = 0; $m <= $#currentEvals; $m++){
if ($testEval == $currentEvals[$m]) {
push @ind, $m;
} elsif ($testEval > $currentEvals[$m]) {
@ind = ($m);
$testEval = $currentEvals[$m];
}
}
unshift @useEvals, $testEval;
if ($#ind > 0) {
my @uniqueSenses = uniq @currentSenses;
my @uniqueKinds = uniq @currentKinds;
unshift @useSenses, join('',@uniqueSenses);
unshift @useKinds, join(';',@uniqueKinds);
unshift @useStarts, min @currentAllStarts[@ind];
unshift @useEnds, max @currentAllEnds[@ind];
} else {
unshift @useSenses, $currentSenses[$ind[0]];
unshift @useKinds, $currentKinds[$ind[0]];
unshift @useStarts, $currentAllStarts[$ind[0]];
unshift @useEnds, $currentAllEnds[$ind[0]];
}
} else {
push @useStarts, $rRNAs{$contig}[0][0];
push @useEnds, $rRNAs{$contig}[1][0];
push @useSenses, $rRNAs{$contig}[2][0];
push @useKinds, $rRNAs{$contig}[3][0];
push @useEvals, $rRNAs{$contig}[4][0];
}
for (my $j = 0 ; $j <= $#useStarts ; $j++ ) {
if ($useSenses[$j] =~ /\+-|-\+/) {
print STDERR "Ambiguous sense on $contig between $useStarts[$j] and $useEnds[$j]. \n";
next;
}
if (exists $allContigs{$contig}) {
$allContigs{$contig}++
} else {
$allContigs{$contig} = 1
}
my $gene = join("", $contig, "_r", $allContigs{$contig});
my $length = 1 + $useEnds[$j] - $useStarts[$j];
my $sequence = "";
if ($useSenses[$j] eq "+") {
$sequence = $db->seq($contig, $useStarts[$j], $useEnds[$j]);
} else {
$sequence = $db->seq($contig, $useEnds[$j], $useStarts[$j]);
}
if (!defined( $sequence )) {
print STDERR "Sequence $contig not found. \n";
next;
} else {
print GEN ">$gene\n", "$sequence\n";
}
print TAB join("\t",$contig,$gene,$useSenses[$j],$length,$useStarts[$j],$useEnds[$j],$useEvals[$j],$useKinds[$j]), "\n";
}
}
close(GEN);
close(TAB);