-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepareGBFiles.pl
executable file
·416 lines (282 loc) · 9.57 KB
/
prepareGBFiles.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/perl
###########################################################
=pod
=cut
###########################################################
use Connection;
use Utils;
use strict;
my $dbh = Connection->getDBHandleFromConfigFile();
my $params = {};
main();
sub main
{
initialize();
Utils->usage($params, ['algorithm']);
my @algorithms = split(/\s/, $params->{algorithm});
my %format = ();
my @formats = split(/\s/, $params->{format});
foreach my $form(@formats){ $format{$form} = 1};
my $genome = "\'". $params->{genome} ."\'";
if ($params->{genome_list}){
open(LIST, $params->{genome_list}) or die "Can't open genome_list file!!\n";
$genome = "";
while (my $entry = <LIST>){
chomp $entry;
$entry =~s/'/''/g;
$genome .= "\'". $entry ."\', " ;
}
$genome=~s/, $//;
}
my @genomes = getGenomes($genome);
foreach my $genome (@genomes){
my($genome_name, $common_name)=$genome=~/(.*)\t(.*)/;
#$genome_name=~s/'/''/g;
print "Preparing files for $genome_name...\n";
`mkdir $common_name`;
foreach my $algorithm(@algorithms){
prepareFSA($genome_name, $common_name, $algorithm);
prepareTBL($genome_name, $common_name, $algorithm);
prepareGB($genome_name, $common_name, $algorithm);
#prepareGFF($genome_name, $common_name, $algorithm);
`rm $common_name\/*.ecn $common_name\/*.f* $common_name\/*.sqn $common_name\/*.tbl`;
}
}
}
sub initialize{
$params->{algo_file} = { "PATRIC" => "PATRIC", "RefSeq" => "RefSeq", "BRC" => "BRC"};
$params->{algo_name} = { "PATRIC" => "RAST", "RefSeq" => "RefSeq", "BRC" => "Curation"};
}
sub getGenomes {
my ($genome) = @_;
my @genomes;
#$genome=~s/^'|'$//g; ####
my $sql = "
select distinct gi.genome_name, gi.common_name
from cas.genomeinfo gi, cas.sequenceinfo si
where gi.genome_info_id = si.genome_info_id ";
$sql.="and genome_name in ($genome) " unless($genome eq "\'all\'");
#$sql.="and genome_name like '%$genome%' ";
$sql.="order by genome_name";
#print "$sql\n";
my $st = $dbh->prepare($sql);
$st->execute;
while (my ($genome_name, $common_name) = $st->fetchrow_array){
push(@genomes, "$genome_name\t$common_name");
}
return @genomes;
}
sub prepareFSA {
my ($genome_name, $common_name, $algorithm) = @_;
my $outFile = "$common_name/$common_name.".$params->{algo_file}->{$algorithm}.".fsa";
open OUT, ">$outFile" or die "Sorry, could not open output file $outFile !\n";
print "\t$outFile\n";
my $count = 0;
my $sql = "
select si.sequence_info_id, si.accession, ns.description, gi.genome_name, ns.sequence
from cas.genomeinfo gi, cas.sequenceinfo si, dots.nasequence ns
where gi.genome_info_id = si.genome_info_id
and si.na_sequence_id = ns.na_sequence_id
and ns.sequence is not null
and genome_name = ?
order by accession
";
my $sth = $dbh->prepare($sql);
$sth->execute($genome_name);
my $rows = 0;
while (my($sequence_info_id, $accession, $description, $genome_name, $sequence) = $sth->fetchrow_array){
$sequence=createFastaSequence($sequence);
print OUT ">$accession [organism=$genome_name] $description\n$sequence\n" if ($sequence ne "");
$rows++;
}
close OUT;
`rm $outFile` if ($rows == 0);
}
sub prepareTBL {
my ($genome_name, $common_name, $algorithm) = @_;
my $outFile = "$common_name/$common_name.".$params->{algo_file}->{$algorithm}.".tbl";
open OUT, ">$outFile" or die "Sorry, could not open output file $outFile !\n";
print "\t$outFile\n";
my @features = prepareFeatures($genome_name, $algorithm);
my $go = prepareGO($genome_name, $algorithm);
my $ec = prepareEC($genome_name, $algorithm);
my $prevAccession = "";
foreach my $feature (@features){
my ($genome_name, $accession, $annotation, $feature_type, $na_feature_id, $locus_tag, $start, $end, $strand, $na_length, $gene, $product, $label, $is_pseudo, $bound_moiety, $anticodon, $protein_id, $pseed_id)
= @$feature;
if ($accession ne $prevAccession){
print OUT ">Feature $accession\n";
$prevAccession = $accession;
}
print OUT "$start\t$end\t$feature_type\n" if ($strand eq '+');
print OUT "$end\t$start\t$feature_type\n" if ($strand eq '-');
print OUT "\t\t\tlocus_tag\t$locus_tag\n" if $locus_tag;
print OUT "\t\t\tproduct\t$product\n" if $product;
print OUT "\t\t\tgene\t$gene\n" if $gene;
print OUT "\t\t\tlabel\t$label\n" if $label;
print OUT "\t\t\tpseudo\t \n" if $is_pseudo;
print OUT "\t\t\tbound_moiety\t$bound_moiety\n" if $bound_moiety;
print OUT "\t\t\tanticodon\t$anticodon\n" if $anticodon;
print OUT "\t\t\tprotein_id\t$protein_id\n" if $protein_id;
print OUT "\t\t\tdb_xref\tSEED:$pseed_id\n" if $pseed_id;
print OUT "\t\t\tdb_xref\tPATRIC:$na_feature_id\n" if $pseed_id;
foreach my $go_term (@{$go->{$na_feature_id}}){
print OUT "\t\t\tgo_function\t$go_term\n";
}
foreach my $ec_no (@{$ec->{$na_feature_id}}){
print OUT "\t\t\tEC_number\t$ec_no\n";
}
}
close OUT;
}
sub prepareFeatures(){
my ($genome_name, $algorithm) = @_;
my @features;
my $sql = "
select genome_name, accession,
decode(df.algorithm,'Curation','BRC','RAST','PATRIC','RefSeq') as annotation, name as feature_type,
na_feature_id, source_id as locus_tag,
start_max, end_min, decode(is_reversed, 1, '-', '+') as strand,
na_length, gene, product,
label, is_pseudo, bound_moiety, anticodon,
protein_id, pseed_id
from app.dnafeature df
where df.genome_name = ?
and df.algorithm = ?
order by df.accession, start_max
";
my $sth = $dbh->prepare($sql);
$sth->execute($genome_name, $params->{algo_name}->{$algorithm});
while (my @row=$sth->fetchrow_array){
push @features, [@row];
}
return @features;
}
sub prepareGO(){
my ($genome_name, $algorithm) = @_;
my %go = ();
my $sql = "
select distinct df.na_feature_id, go_id, go_term
from app.dnafeature df, app.gosummary gs
where df.na_feature_id = gs.na_feature_id
and df.name = 'CDS'
and df.genome_name = ?
and df.algorithm = ?
order by df.na_feature_id
";
my $sth = $dbh->prepare($sql);
$sth->execute($genome_name, $params->{algo_name}->{$algorithm});
my $prevFID;
my @go;
while (my @row = $sth->fetchrow_array){
my ($na_feature_id, $go_id, $go_term) = @row;
if ($na_feature_id != $prevFID){
$go{$prevFID} = [@go];
@go=();
}
$prevFID = $na_feature_id;
$go_id=~s/GO://;
push @go, "$go_term|$go_id";
}
return \%go;
}
sub prepareEC(){
my ($genome_name, $algorithm) = @_;
my %ec = ();
my $sql = "
select distinct df.na_feature_id, es.ec_number
from app.dnafeature df, app.ecsummary es
where df.na_feature_id = es.na_feature_id
and df.name = 'CDS'
and df.genome_name = ?
and df.algorithm = ?
order by df.na_feature_id
";
my $sth = $dbh->prepare($sql);
$sth->execute($genome_name, $params->{algo_name}->{$algorithm});
my $prevFID;
my @ec;
while (my @row = $sth->fetchrow_array){
my ($na_feature_id, $ec_no) = @row;
if ($na_feature_id != $prevFID){
$ec{$prevFID} = [@ec];
@ec=();
}
$prevFID = $na_feature_id;
push @ec, "$ec_no";
}
return \%ec;
}
sub prepareGB {
my ($genome_name, $common_name, $algorithm) = @_;
`tbl2asn -p $common_name -V b -a s`;
my $gbFile = "$common_name/$common_name.".$params->{algo_file}->{$algorithm}.".gbf";
open GB, "$gbFile";
open TMP, ">tmp.gbf";
my $accession;
while ( my $line = <GB>){
($accession) = $line=~/^LOCUS\s+(\S+)\s+/ if $line=~/^LOCUS/;
print TMP $line unless($line=~/LOCUS|ACCESSION|VERSION|ORGANISM|SOURCE|\/organism/);
if ($line=~/^LOCUS/){
($accession) = $line=~/^LOCUS\s+(\S+)\s+/;
$line =~s/ bp DNA circular / bp DNA circular BCT /;
$line =~s/ bp DNA linear / bp DNA linear BCT /;
print TMP $line;
}
print TMP "ACCESSION $accession\n" if ($line=~/^ACCESSION/);
print TMP "VERSION $accession\n" if ($line=~/^VERSION/);
print TMP "SOURCE $genome_name\n" if ($line=~/^SOURCE/);
print TMP " ORGANISM $genome_name\n" if ($line=~/^ ORGANISM/);
print TMP " /organism=\"$genome_name\"\n" if ($line=~/^ .organism=.unknown./);
=pod
if ($line=~/^SOURCE/){
formline <<END, $genome_name;
SOURCE ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
END
}
print $^A; $^A = "";
if ($line=~/ORGANISM/){
formline <<END, $genome_name;
ORGANISM ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
END
}
print $^A; $^A = "";
if ($line=~/^ *\/organism/){
my $tmp = "/organism=\"$genome_name\"";
formline <<END, $tmp;
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
END
}
print $^A; $^A = "";
=cut
}
close GB,
close TMP;
`mv tmp.gbf $gbFile`;
}
sub prepareGFF {
my ($genome_name, $common_name, $algorithm) = @_;
my $gbFile = "$common_name.".$params->{algo_file}->{$algorithm}.".gbf";
my $gffFile = "$common_name.".$params->{algo_file}->{$algorithm}.".gff";
`mv $common_name/$gbFile ./`;
`bp_genbank2gff3.pl $gbFile`;
`mv $gbFile.gff $gffFile`;
`perl -pi -e 's/\tGenBank\t/\tPATRIC\t/' $gffFile`;
`perl -pi -e 's/.*\tPATRIC\texon\t.*\n//' $gffFile`;
`perl -pi -e 's/.*\tPATRIC\tmRNA\t.*\n//' $gffFile`;
`perl -pi -e 's/.t01;/;/' $gffFile`;
`mv $common_name\.* $common_name\/.`;
}
sub createFastaSequence {
my ($seq) = @_;
my $fasta = "";
my $seqLength = length($seq);
while ($seqLength > 60){
my $seqTemp = substr ($seq, 0, 60);
$fasta .= $seqTemp."\n";
$seq = substr ($seq, 60);
$seqLength = length($seq);
}
$fasta .= $seq."\n";
return $fasta;
}