-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathannotateRefSeqSequencesWithUniqueTaxonIDs.pl
More file actions
391 lines (327 loc) · 10.7 KB
/
annotateRefSeqSequencesWithUniqueTaxonIDs.pl
File metadata and controls
391 lines (327 loc) · 10.7 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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use FindBin;
use File::Find;
use lib "$FindBin::Bin/perlLib";
use Cwd qw/abs_path/;
use taxTree;
my $refSeqDirectory;
my $taxonomyInDirectory;
my $taxonomyOutDirectory;
GetOptions (
'refSeqDirectory:s' => \$refSeqDirectory,
'taxonomyInDirectory:s' => \$taxonomyInDirectory,
'taxonomyOutDirectory:s' => \$taxonomyOutDirectory,
);
unless($refSeqDirectory and $taxonomyInDirectory and $taxonomyOutDirectory)
{
print_help();
}
$refSeqDirectory = abs_path($refSeqDirectory);
die "Please specify valid directory for --refSeqDirectory" unless(-d $refSeqDirectory);
die "Please specify valid directory for --taxonomyInDirectory" unless(-d $taxonomyInDirectory);
print "Reading taxonomy from $taxonomyInDirectory ..\n";
my $taxonomy_href = taxTree::readTaxonomy($taxonomyInDirectory);
my $merged_href = taxTree::readMerged($taxonomyInDirectory);
print "\tdone.\n\n";
my @expected_taxonomy_files = taxTree::getTaxonomyFileNames();
unless(-d $taxonomyOutDirectory)
{
mkdir($taxonomyOutDirectory) or die "Can't mkdir $taxonomyOutDirectory (specified via --taxonomyOutDirectory)";
}
foreach my $f (@expected_taxonomy_files)
{
my $cmd_cp_f = qq(cp ${taxonomyInDirectory}/${f} ${taxonomyOutDirectory}/);
system($cmd_cp_f) and die "Cannot execute cp command:\n$cmd_cp_f";
}
my %file_2_taxonID;
my %file_2_organismName;
my %taxonID_2_files;
my %refSeq_categories;
my %assemblyLevels;
my %category_level_combined;
my $included_genome = 0;
my $missed_representative = 0;
my $missed_referenceGenome = 0;
my $missed_reference_and_representative = 0;
print "Scanning $refSeqDirectory for *_assembly_report.txt\n";
find(\&wanted, $refSeqDirectory);
sub wanted {
my $file = $File::Find::name;
{
no warnings;
$file = $File::Find::name;
}
# warn Dumper("Expected file nt existing", $file) unless(-e $file);
if((-e $file) and (not -d $file) and ($file =~ /_assembly_report\.txt$/))
{
my $gzFile = get_gz_for_assemblyReport($file);
if(-e $gzFile)
{
remove_existing_fnaFile_for_assemblyReportFile($file);
my $taxonID;
my $organismName = 'NA';
my $refSeq_category = '';
my $assembly_level = '';
open(REPORT, '<', $file) or die "Cannot open assembly report $file";
while(<REPORT>)
{
if($_ =~ /^# Taxid:\s*(\d+)/)
{
$taxonID = $1;
}
if($_ =~ /^# Organism name:\s*(.+)/)
{
$organismName = $1;
$organismName =~ s/[\n\r]//g;
}
if($_ =~ /^# RefSeq category:\s*(.+)/)
{
$refSeq_category = $1;
$refSeq_category =~ s/[\n\r]//g;
}
if($_ =~ /^# Assembly level:\s*(.+)/)
{
$assembly_level = $1;
$assembly_level =~ s/[\n\r]//g;
}
}
close(REPORT);
unless(defined $taxonID)
{
die "Assembly report $file has not taxon ID";
}
$refSeq_categories{$refSeq_category}++;
$assemblyLevels{$assembly_level}++;
if(not $refSeq_category)
{
$refSeq_category = 'Undefined';
# warn "No RefSeq category info in $file";
}
$category_level_combined{join('_', $refSeq_category, $assembly_level)}++;
next unless($assembly_level eq 'Complete Genome');
$included_genome++;
unless(exists $taxonomy_href->{$taxonID})
{
warn "Taxon ID $taxonID not defined in tree in $taxonomyInDirectory - try recovering from merged nodes.";
$taxonID = taxTree::findCurrentNodeID($taxonomy_href, $merged_href, $taxonID);
}
if(exists $taxonomy_href->{$taxonID})
{
$file_2_taxonID{$file} = $taxonID;
$file_2_organismName{$file} = $organismName;
push(@{$taxonID_2_files{$taxonID}}, $file);
}
else
{
die "Taxon ID $taxonID not defined in tree in $taxonomyInDirectory -- update your taxonomy directory?";
}
# if(1) # or $assembly_level eq 'Complete Genome'
# {
# }
# else
# {
# die Dumper("Weird assembly_level", $assembly_level) unless(($assembly_level eq 'Contig') or ($assembly_level eq 'Scaffold') or ($assembly_level eq 'Chromosome'));
# if(($refSeq_category eq 'Representative Genome') and ($refSeq_category eq 'Reference Genome'))
# {
# $missed_reference_and_representative++;
# }
# elsif($refSeq_category eq 'Representative Genome')
# {
# $missed_representative++;
# }
# elsif($refSeq_category eq 'Reference Genome')
# {
# $missed_referenceGenome++;
# }
# else
# {
# die Dumper("Weird refSeq_category", $refSeq_category) unless(($refSeq_category eq '') or ($refSeq_category eq 'Representative Genome'));
# }
# }
}
else
{
warn "No assembly data file - $gzFile";
}
}
}
print "Summary input data:\n";
print "\tRefseq categories:\n";
foreach my $key (keys %refSeq_categories)
{
print "\t\t", $key, ": ", $refSeq_categories{$key}, "\n";
}
print "\tAssembly levels:\n";
foreach my $key (keys %assemblyLevels)
{
print "\t\t", $key, ": ", $assemblyLevels{$key}, "\n";
}
print "\t{Category} X {Assembly level}:\n";
foreach my $key (keys %category_level_combined)
{
print "\t\t", $key, ": ", $category_level_combined{$key}, "\n";
}
print "Total genomes: $included_genome \n";
# print "\tMissed reference and representative: $missed_reference_and_representative \n";
# print "\tMissed representative: $missed_representative \n";
# print "\tMissed reference: $missed_referenceGenome \n";
my $running_new_ids = 0;
my %new_tree_relationships;
my %new_tree_names;
my %need_taxonID = (1 => 1);
foreach my $taxonID (keys %taxonID_2_files)
{
unless(exists $taxonomy_href->{$taxonID})
{
die "Taxon ID $taxonID not defined in tree in $taxonomyInDirectory -- update your taxonomy directory?";
}
my $current_id = $taxonID;
do {
$need_taxonID{$current_id} = 1;
die unless(defined $taxonomy_href->{$current_id});
$current_id = $taxonomy_href->{$current_id}{parent};
} while($current_id != 1);
if(scalar(@{$taxonID_2_files{$taxonID}}) > 1)
{
my $thisNode_rank = $taxonomy_href->{$taxonID}{rank};
die unless(defined $thisNode_rank);
unless(($thisNode_rank eq 'species') or ($thisNode_rank eq 'no rank') or ($thisNode_rank eq 'subspecies') or ($thisNode_rank eq 'varietas') or ($thisNode_rank eq 'strain') or ($thisNode_rank eq 'isolate'))
{
die Dumper("Unexpected rank", $thisNode_rank, $taxonID, $taxonID_2_files{$taxonID});
}
my @associated_files = @{$taxonID_2_files{$taxonID}};
foreach my $f (@associated_files)
{
$running_new_ids++;
my $newID = 'x' . $running_new_ids;
$file_2_taxonID{$f} = $newID;
$new_tree_relationships{$newID} = $taxonID;
$new_tree_names{$newID} = $file_2_organismName{$f};
$taxonID_2_files{$newID} = [$f];
}
delete $taxonID_2_files{$taxonID};
}
}
print "Introduced $running_new_ids new taxonomic IDs\n";
my $contigCounter = 0;
foreach my $assemblyReportFile (keys %file_2_taxonID)
{
my $taxonID = $file_2_taxonID{$assemblyReportFile};
my $fresh_fnaFile = get_fresh_fnaFile_for_assemblyReportFile($assemblyReportFile);
my $f2 = $fresh_fnaFile . '_';
open(F, '<', $fresh_fnaFile) or die "Cannot open $fresh_fnaFile";
open(F2, '>', $f2) or die "Cannot open $f2";
while(<F>)
{
my $line = $_;
if(substr($line, 0, 1) eq '>')
{
substr($line, 0, 1) = '';
die "File $fresh_fnaFile already conains kraken segment?" if ($line =~ /kraken:taxid\|(\d+)/);
$contigCounter++;
my $line_for_inclusion = $line;
$line_for_inclusion =~ s/\s.*//;
$line_for_inclusion =~ s/[\r\n]//g;
my $newID = 'C' . $contigCounter . '|kraken:taxid|' . $taxonID . '|' . $line_for_inclusion;
die "Invalid ID: $newID" if ($newID =~ /\s/);
print F2 '>', $newID, "\n";
}
else
{
print F2 $line;
}
}
close(F2);
close(F);
my $mv_command = qq(mv $f2 $fresh_fnaFile);
die "Command $mv_command failed" if (system($mv_command));
}
print "Annotated $contigCounter contigs\n";
my $taxonomy_names_f_out = $taxonomyOutDirectory . '/names.dmp';
my $taxonomy_nodes_f_out = $taxonomyOutDirectory . '/nodes.dmp';
die unless(-e $taxonomy_names_f_out);
open(NAMESOUT, '>>', $taxonomy_names_f_out) or die "Cannot open $taxonomy_names_f_out";
foreach my $newID (keys %new_tree_relationships)
{
print NAMESOUT join("\t|\t", $newID, $new_tree_names{$newID}, '', 'scientific name', ''), "\n";
}
close(NAMESOUT);
die unless(-e $taxonomy_nodes_f_out);
open(NODESOUT, '>>', $taxonomy_nodes_f_out) or die "Cannot open $taxonomy_nodes_f_out";
foreach my $newID (keys %new_tree_relationships)
{
print NODESOUT join("\t|\t", $newID, $new_tree_relationships{$newID}, 'pseudospecies', ''), "\n";
}
close(NODESOUT);
print "\nOutput new taxonomy into $taxonomyOutDirectory\n\n";
sub get_gz_for_assemblyReport
{
my $assemblyReport = shift;
die "Weird apparent assembly report file $assemblyReport" unless($assemblyReport =~ /_assembly_report\.txt$/);
my $gzFile = $assemblyReport;
$gzFile =~ s/_assembly_report\.txt/_genomic.fna.gz/;
return $gzFile;
}
sub remove_existing_fnaFile_for_assemblyReportFile
{
my $assemblyReport = shift;
die "Weird apparent assembly report file $assemblyReport" unless($assemblyReport =~ /_assembly_report\.txt$/);
my $gzFile = get_gz_for_assemblyReport($assemblyReport);
unless(-e $gzFile)
{
warn "Expected file $gzFile not existing";
return 0;
}
my $fna_file = $gzFile;
$fna_file =~ s/\.gz$//;
if(-e $fna_file)
{
unlink($fna_file) or die "Cannot unlink $fna_file";
}
return 1;
}
sub get_fresh_fnaFile_for_assemblyReportFile
{
my $assemblyReport = shift;
die "Weird apparent assembly report file $assemblyReport" unless($assemblyReport =~ /_assembly_report\.txt$/);
my $gzFile = $assemblyReport;
$gzFile =~ s/_assembly_report\.txt/_genomic.fna.gz/;
die "Expected file $gzFile not existing" unless(-e $gzFile);
my $fna_file = $gzFile;
$fna_file =~ s/\.gz$//;
if(-e $fna_file)
{
unlink($fna_file) or die "Cannot unlink $fna_file";
}
my $cmd = qq(gunzip -c $gzFile > $fna_file);
if(system($cmd))
{
unlink($fna_file);
die "Command $cmd failed";
}
die unless(-e $fna_file);
return $fna_file;
}
sub print_help
{
print qq(
annotateRefSeqSequencesWithUniqueTaxonIDs.pl
Prepares a RefSeq/GenBank download for MetaMaps.
Usage:
perl annotateRefSeqSequencesWithUniqueTaxonIDs.pl --refSeqDirectory DIR --taxonomyInDirectory DIR --taxonomyOutDirectory DIR
Details:
Reads a directory structure as downloaded from RefSeq / Genbank servers,
unzips assemblies, gets information from *_assembly_report files, and writes
FASTA files with taxonID information in FASTA IDs.
Each sub-directory is treated as a separate 'mappping unit' that will receive a
unique mapping ID - the script will create pseudo taxonomic IDs, prefixed with an
'x', if necessary.
The specified --refSeqDirectory is manipulated in-place, and the amended taxonomy
is written into --taxonomyOutDirectory\n\n
);
exit;
}