WGS2NCBI.pm

filter: Statementstmt 19.1% 51 / 266 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 59.0% 13 / 22 Podpod 0.0% 0 / 7 totaltotal 14.4% 64 / 443 CC 71 SCAR 55.7
CC
71
Cov
13%
prune
420.0
process
342.0
prepare
306.0
CRAP 263.2
« WGS2NCBI/FeatureSet.pm ↑ Summary WGS2NCBI/TableReader.pm »
1 package Bio::WGS2NCBI;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Bio::WGS2NCBI::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Bio::WGS2NCBI::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Bio::WGS2NCBI::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Bio::WGS2NCBI::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Bio::WGS2NCBI::FeatureSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Bio::WGS2NCBI::TableReader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # version number for the whole release is defined here
16 4 our $VERSION = 'v0.1.2';
17  
18 # export the run function
19 4 require Exporter;
20 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 our @EXPORT = qw(process prepare compress convert prune);
22  
23 # GFF3 column numbers
24 4 my $chr_idx = 0;
25 4 my $source_idx = 1;
26 4 my $type_idx = 2;
27 4 my $start_idx = 3;
28 4 my $end_idx = 4;
29 4 my $strand_idx = 6;
30 4 my $codon_idx = 7;
31 4 my $meta_idx = 8;
32  
33 =pod
34  
35 =head1 NAME
36  
37 Bio::WGS2NCBI - module to assist in submitting whole genome sequencing projects to NCBI
38  
39 =head1 DESCRIPTION
40  
41 This module documents the four actions (prepare, process, convert and compress) that are
42 available to users of the L<wgs2ncbi> script. Each of these steps is configured by one or
43 more configuration files. In the documentation below, the relevant fields from these
44 configuration files are listed. To understand how the configuration system itself works,
45 consult the documentation at L<Bio::WGS2NCBI::Config>.
46  
47 =head1 prepare
48  
49 The C<prepare> action takes the annotation file (in GFF3 format) and extracts the relevant
50 information out of it, writing it to a potentially large set of files. This is done
51 because GFF3 annotation files can become quite large, so that finding the annotations
52 for any particular scaffold might take a long time if this is done by scanning through
53 the whole file. Instead, the set of annotations is reduced, by taking the following steps:
54  
55 =over
56  
57 =item B<Remove all sequence data from the file> - Embedded FASTA data is permissible
58 according to the GFF3 standard, but this makes files needlessly bulky if the FASTA data
59 are available separately as well.
60  
61 =item B<Remove all annotations from unrecognized sources> - GFF3 files can contain
62 annotations from sources you don't particularly trust and want to ignore in your
63 submission. This is configurable.
64  
65 =item B<Remove all irrelevant features> - any sequence features in GFF3 that are not
66 recognized in NCBI feature tables are discarded. This is configurable.
67  
68 =back
69  
70 Subsequently, the annotations are split such that there is a separate file for each
71 scaffold (or chromosome, if that is how your annotations are organized). This way, the
72 relevant information for any scaffold can be found much more quickly through the file
73 system rather than by scanning through a large file.
74  
75 In order for this action to succeed, the following configuration values need to be
76 provided:
77  
78 =over
79  
80 =item C<gff3file>
81  
82 The location of the input annotation file in GFF3 format.
83  
84 =item C<gff3dir>
85  
86 The location of the output directory for the split annotation files.
87  
88 =item C<source>
89  
90 Which annotation source to trust.
91  
92 =item C<feature>
93  
94 Which features to retain.
95  
96 =back
97  
98 =cut
99  
100 0 ▶sub prepare {
Subroutine prepare: not called CC=17 SCAR=57.2
Pod undocumented
101 0 my $config = Bio::WGS2NCBI::Config->new;
102 0 my %types = map { $_ => 1 } $config->feature;
103 0 my $gff3file = $config->gff3file;
104 0 my $gff3dir = $config->gff3dir;
105 0 my $source = $config->source; # XXX maybe comma separated list?
106 0 my $chr_idx = 0;
107 0 my $src_idx = 1;
108 0 my $type_idx = 2;
109 0 my @queue;
110 my %handles;
111
112 # check the configuration
113 0 my $quit;
114 0 ▶ if ( not $gff3file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $gff3file) { } 0 0 0
elsif (not -e $gff3file) { } 0 0 0
115 0 ERROR "No GFF3 file provided.";
116 0 $quit = 1;
117 }
118 elsif ( not -e $gff3file ) {
119 0 ERROR "No GFF3 file at location '$gff3file'";
120 0 $quit = 1;
121 }
122 0 ▶ if ( not $gff3dir ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $gff3dir) { } 0 0 0
elsif (not -d $gff3dir) { } 0 0 0
123 0 ERROR "No GFF3 directory provided.";
124 0 $quit = 1;
125 }
126 elsif ( not -d $gff3dir ) {
127 0 ERROR "No GFF3 directory at location '$gff3dir'";
128 0 $quit = 1;
129 }
130 0 ▶ if ( $quit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quit) 0 0 0
131 0 ERROR "Quitting. Try 'wgs2ncbi help' for more info.";
132 0 exit(1);
133 }
134  
135 # open file handle
136 0 INFO "Going to pre-process annotations in $gff3file";
137 0 ▶ open my $fh, '<', $gff3file or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $gff3file or die 0 0 0
138
139 # iterate over lines
140 0 my $line = 0;
141 0 ▶ LINE: while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
142 0 ▶ next LINE if /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ 0 0 0
143 0 my @line = split /\t/, $_;
144
145 # we will automatically skip over sequence lines as they won't have columns
146 0 ▶ next LINE if not $line[$src_idx] or $line[$src_idx] ne $source;
Branch: 1 branch
BranchTrueFalseTotal
if not $line[$src_idx] or $line[$src_idx] ne $source 0 0 0
Condition0%0/3
not $line[$src_idx] or $line[$src_idx] ne $source
l!l&&r!l&&!r
000
MC/DC0%0/2
not $line[$src_idx] or $line[$src_idx] ne $source
not $line[$src_idx] $line[$src_idx] ne $source
Truth table0%0/3
not(A) or B
A= not $line[$src_idx]B= $line[$src_idx] ne $source
ABresult
1X1
011
000
147 0 ▶ next LINE if not $line[$type_idx] or not $types{$line[$type_idx]};
Branch: 1 branch
BranchTrueFalseTotal
unless $line[$type_idx] and $types{$line[$type_idx]} 0 0 0
Condition0%0/3
$line[$type_idx] and $types{$line[$type_idx]}
!ll&&!rl&&r
000
MC/DC0%0/2
$line[$type_idx] and $types{$line[$type_idx]}
$line[$type_idx] $types{$line[$type_idx]}
Truth table0%0/3
A and B
A= $line[$type_idx]B= $types{$line[$type_idx]}
ABresult
0X0
100
111
148 0 my $chr = $line[$chr_idx];
149
150 # we keep up to 100 file handles (which is max on POSIX). once we
151 # reach that number we close existing ones on a first in, first out basis
152 0 ▶ if ( not $handles{$chr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handles{$chr}) 0 0 0
153 0 ▶ if ( scalar @queue == 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue == 100) 0 0 0
154 0 my $fifo = shift @queue;
155 0 close $handles{$fifo};
156 0 delete $handles{$fifo};
157 }
158 0 ▶ open $handles{$chr}, '>', "${gff3dir}/${chr}.gff3" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $handles{$chr}, ">", "$gff3dir/$chr.gff3" or die $! 0 0 0
159 0 push @queue, $chr;
160 }
161 0 my $fh = $handles{$chr};
162 0 print $fh $_;
163
164 # log line #
165 0 ▶ if ( not ++$line % 10000 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (++$line % 10000) 0 0 0
166 0 INFO "Processed line $line";
167 }
168 }
169 0 INFO "Done. Files written to $gff3dir";
170 }
171  
172 =head1 process
173  
174 The C<process> action takes the sequence file (in FASTA format, with a record for each
175 scaffold or chromosome) and the pre-processed annotations and converts these into feature
176 table files and (masked) sequence files.
177  
178 In order for this action to succeed, the following configuration values need to be
179 provided:
180  
181 =over
182  
183 =item C<datafile>
184  
185 The location of the input FASTA file, with a record for each scaffold or chromosome.
186  
187 =item C<info>
188  
189 An INI-style configuration file that contains key/value pairs that will be embedded in the
190 FASTA sequence headers of the produced output files. This is typically used for metadata
191 about the sampled organism, such as its sex, collection locality, collected cell type,
192 etc.
193  
194 =item C<masks>
195  
196 An INI-style configuration file that contains the coordinates of sequence segments to
197 mask. This may be needed because NCBI will do a strict screen to check for unclipped
198 adaptor sequences or contaminants. In the report that is returned by NCBI it will state
199 the sequence coordinates of segments that NCBI will not accept in a submission. By putting
200 these coordinate in this file the offending segments will be replaced with NNNs.
201  
202 =item C<products>
203  
204 An INI-style configuration file that contains the corrected names for protein products.
205 The rationale is that your genome annotation process may introduce protein names that NCBI
206 would like to deprecate, such as names that include molecular weights, database
207 identifiers, references to 'homology', and so on. The discrepancy report that is produced
208 in the L<Bio::WGS2NCBI/convert> step will be a first guide in composing corrected names,
209 but the validation that NCBI will perform will likely point out additional errors.
210  
211 =item C<gff3dir>
212  
213 The location of the pre-processed GFF3 files as produced by L<Bio::WGS2NCBI/prepare>.
214  
215 =item C<datadir>
216  
217 The location of the output dir where the (potentially 'chunked', see below) sequence files
218 and feature tables will be written.
219  
220 =item C<prefix>
221  
222 A short character sequence that is prefixed to every sequence record identifier that is
223 generated. NCBI will provide submitters with this prefix when the submission is
224 initialized.
225  
226 =item C<authority>
227  
228 This is a naming authority that will be applied to all sequence record identifiers. A
229 reasonable value for this could be the name of the lab or institution that leads the
230 project resulting in the submission. NCBI intends this authority, in combination with the
231 C<prefix> as a way to ensure that sequences are globally uniquely identifiable.
232  
233 =item C<minlength>
234  
235 The minimum length of a scaffold to be retained in a submission. This should be 200 or
236 above.
237  
238 =item C<minintron>
239  
240 The minimum length of an intron to be retained in a submission. Introns shorter than this
241 are interpreted (by NCBI) to be spurious and should therefore by discarded. As a
242 consequence, the gene that contains such an intron will be annotated as a pseudogene. This
243 value must be 10 or above.
244  
245 =item C<chunksize>
246  
247 The output that is produced can be combined into chunks of more than one scaffold per
248 file. To keep the number of files manageable it is convenient to set this to a large
249 value, but less than or equal to 10,000.
250  
251 =item C<limit>
252  
253 This parameter allows you to run the process on only a limited set of scaffolds. This is
254 provided for testing, "dry run" purposes. For real usage this value must be set to 0.
255  
256 =back
257  
258 =cut
259  
260 0 ▶sub process {
Subroutine process: not called CC=18 SCAR=58.3
Pod undocumented
261 0 my $config = Bio::WGS2NCBI::Config->new;
262
263 # read the info file, if any
264 0 my %info = $config->read_ini( $config->info );
265 0 my $desc = join ' ', map { sprintf('[%s=%s]',$_,$info{$_}) } keys %info;
266
267 # read the masks, if any
268 0 my %masks;
269 0 ▶ if ( my $file = $config->masks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $config->masks) 0 0 0
270
271 # read the masks file, make values uniformly array refs
272 0 %masks = $config->read_ini( $file );
273 0 for my $key ( keys %masks ) {
274 0 ▶ $masks{$key} = [ $masks{$key} ] if not ref $masks{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $masks{$key} 0 0 0
275 }
276 0 INFO "Read masks for ".scalar(keys(%masks))." sequences";
277 }
278
279 # open the fasta file handle
280 0 ▶ open my $fastaFH, '<', $config->datafile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fastaFH, "<", $config->datafile or die $! 0 0 0
281 0 my $fastaPos = 0;
282
283 # counter to generate autoincrementing IDs for gene, mrna and protein
284 0 my $counter = {};
285 0 my $seq_counter;
286
287 # we will re-use these handles
288 0 my ( $tblFH, $scaffoldFH );
289
290 # iterate over the genome
291 0 ▶ SEQ: while( not eof $fastaFH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not eof $fastaFH) 0 0 0
292 0 ++$seq_counter;
293 0 ▶ last if $config->limit and $seq_counter == $config->limit;
Branch: 1 branch
BranchTrueFalseTotal
if $config->limit and $seq_counter == $config->limit 0 0 0
Condition0%0/3
$config->limit and $seq_counter == $config->limit
!ll&&!rl&&r
000
MC/DC0%0/2
$config->limit and $seq_counter == $config->limit
$config->limit $seq_counter == $config->limit
Truth table0%0/3
A and B
A= $config->limitB= $seq_counter == $config->limit
ABresult
0X0
100
111
294
295 # advance to the next scaffold/chromosome
296 0 my ( $offset, $seq ) = ( 0 );
297 0 ( $fastaPos, $seq ) = Bio::WGS2NCBI::Seq->read_fasta( $fastaFH, $fastaPos );
298 0 $seq->desc($desc);
299 0 my $length = $seq->length;
300 0 my $chr = $seq->id;
301
302 # compute offset at beginning, if any. e.g. if NNCGTNN, $offset is 2
303 0 ▶ if ( $offset = $seq->get_non_missing_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset = $seq->get_non_missing_index) 0 0 0
304 0 INFO "Leading ${offset}bp gap in $chr, will strip this and apply offset";
305 }
306
307 # check what we have left, here if NNCGTNN, $lnmi == 4
308 0 my $last_non_missing_index = $seq->get_non_missing_index('reverse');
309 0 $length -= ( $length - 1 - $last_non_missing_index ) + $offset;
310 0 ▶ if ( $length < $config->minlength ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < $config->minlength) 0 0 0
311 0 WARN "Remaining seq $chr is too short ($length bp), skipping";
312 0 $seq_counter--;
313 0 next SEQ;
314 }
315
316 # open handle to the features table
317 0 ▶ if ( ( $seq_counter % $config->chunksize ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq_counter % $config->chunksize == 1) 0 0 0
318
319 # close if already open
320 0 ▶ if ( $tblFH and $scaffoldFH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tblFH and $scaffoldFH) 0 0 0
Condition0%0/3
$tblFH and $scaffoldFH
!ll&&!rl&&r
000
MC/DC0%0/2
$tblFH and $scaffoldFH
$tblFH $scaffoldFH
Truth table0%0/3
A and B
A= $tblFHB= $scaffoldFH
ABresult
0X0
100
111
321 0 close $tblFH;
322 0 close $scaffoldFH;
323 }
324
325 # generate a new name indicating the range
326 0 my $upper = $seq_counter + $config->chunksize - 1;
327 0 my $stem = "combined_${seq_counter}-${upper}";
328 0 ▶ open $tblFH, '>', $config->datadir . "/${stem}.tbl" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $tblFH, ">", $config->datadir . "/$stem.tbl" or die $! 0 0 0
329 0 ▶ open $scaffoldFH, '>', $config->datadir . "/${stem}.fsa" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $scaffoldFH, ">", $config->datadir . "/$stem.fsa" or die $! 0 0 0
330 }
331
332 # get the features for that scaffold/chromosome, if we have them
333 0 my $gff3 = $config->gff3dir . "/${chr}.gff3";
334 0 ▶ if ( -e $gff3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $gff3) { } 0 0 0
335 0 ▶ open my $gff3FH, '<', $gff3 or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $gff3FH, "<", $gff3 or die $! 0 0 0
336 0 my $features = Bio::WGS2NCBI::FeatureSet->read_features(
337 $gff3FH, # file handle of focal file
338 $counter, # counter for generating locus tags
339 $config, # config object
340 $seq, # sequence object
341 $offset, # start offset
342 $last_non_missing_index, # last true seq character
343 );
344 0 $features->offset($offset);
345 0 $features->write_features( $tblFH );
346 }
347 else {
348 0 print $tblFH '>Features ', $chr, "\n";
349 }
350
351 # truncate, mask, serialize
352 0 my $trunc = $seq->trunc( $offset + 1, $offset + $length );
353 0 ▶ $trunc->mask( @{$masks{$chr}} ) if $masks{$chr};
Branch: 1 branch
BranchTrueFalseTotal
if $masks{$chr} 0 0 0
354 0 $trunc->write_fasta( $scaffoldFH );
355 }
356 }
357  
358 =head1 convert
359  
360 The C<convert> action runs the C<tbl2asn> program provided by NCBI with the right
361 settings. This requires the following configuration settings:
362  
363 =over
364  
365 =item C<datadir>
366  
367 The location of the dir where the (potentially 'chunked', see below) sequence files
368 and feature tables were written by L<Bio::WGS2NCBI/process>.
369  
370 =item C<template>
371  
372 The location of the template file produced with the form at:
373 L<http://www.ncbi.nlm.nih.gov/WebSub/template.cgi>
374  
375 =item C<outdir>
376  
377 The location where to write the resulting ASN.1 files.
378  
379 =item C<discrep>
380  
381 The location where to write the discrepancy report.
382  
383 =item C<tbl2asn>
384  
385 The location where the tbl2asn executable is located.
386  
387 =back
388  
389 =cut
390  
391 0 ▶sub convert {
Subroutine convert: not called CC=1 SCAR=6.9
Pod undocumented
392 0 my $config = Bio::WGS2NCBI::Config->new;
393 0 my $INDIR = $config->datadir;
394 0 my $TMPL = $config->template;
395 0 my $OUTDIR = $config->outdir;
396 0 my $DISCREP = $config->discrep;
397 0 my $TBL2ASN = $config->tbl2asn;
398 0 my $command = "$TBL2ASN -p $INDIR -t $TMPL -M n -a r10k -l paired-ends -r $OUTDIR -Z $DISCREP > /dev/null 2>&1";
399 0 INFO "going to execute command '$command'";
400 0 exec $command;
401 }
402  
403 =head1 trim
404  
405 The C<trim> action trims stretches of leading or trailing NNNs from sequence records, and
406 updates the coordinates in the associated feature tables accordingly. In cases where a
407 feature falls within a trimmed region, the feature is removed entirely.
408  
409 =over
410  
411 =item C<datadir>
412  
413 The location of the dir where the (potentially 'chunked', see below) sequence files
414 and feature tables were written by L<Bio::WGS2NCBI/process>.
415  
416 =back
417  
418 =cut
419  
420 0 ▶sub trim {
Subroutine trim: not called CC=16 SCAR=56.1
Pod undocumented
421 0 my $config = Bio::WGS2NCBI::Config->new;
422 0 my $INDIR = $config->datadir;
423
424 # iterate over files in folder, read FASTA files
425 0 ▶ opendir my $dh, $INDIR or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $INDIR or die $! 0 0 0
426 0 ▶ while( my $file = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 0 0 0
427
428 # have a FASTA file
429 0 ▶ if ( $file =~ /(.+)\.fsa$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /(.+)\.fsa$/) 0 0 0
430 0 my $stem = $1;
431
432 # make backup of FASTA file
433 0 rename "${INDIR}/${file}", "${INDIR}/${file}.bak";
434
435 # read file, look op non-missing residue positions, write truncated
436 0 ▶ open my $fh, '<', "${INDIR}/${file}.bak" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "$INDIR/$file.bak" or die $! 0 0 0
437 0 ▶ open my $out, '>', "${INDIR}/${file}" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", "$INDIR/$file" or die $! 0 0 0
438 0 my ( $pos, $seq, %coord );
439 0 ▶ while( not eof($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
440 0 ( $pos, $seq ) = Bio::WGS2NCBI::Seq->read_fasta( $fh, $pos );
441 0 my $id = $seq->id;
442 0 my $i1 = $seq->get_non_missing_index;
443 0 my $i2 = $seq->get_non_missing_index(1);
444 0 INFO "$id\t$i1 .. $i2";
445 0 $coord{$id} = [ $i1, $i2 ];
446 0 $seq->trunc( $i1 + 1, $i2 + 1 )->write_fasta($out);
447 }
448
449 # make backup of TBL file, open handle for writing
450 0 rename "${INDIR}/${stem}.tbl", "${INDIR}/${stem}.tbl.bak";
451 0 ▶ open my $outtbl, '>', "${INDIR}/${stem}.tbl" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $outtbl, ">", "$INDIR/$stem.tbl" or die $! 0 0 0
452
453 # initialize variables
454 my $tr = Bio::WGS2NCBI::TableReader->new(
455 '-file' => "${INDIR}/${stem}.tbl.bak",
456 0 ▶ '-cb' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
457 0 my $id = shift;
458 0 print $outtbl '>Features ', $id, "\n";
459 }
460 0 );
461
462 # iterate over features
463 0 my ( $oldid, $drop, $id ) = ( '' );
464 0 ▶ while( my $f = $tr->next_feature ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $tr->next_feature) 0 0 0
465 0 $id = $tr->seq;
466 0 ▶ if ( $f->isa('Bio::WGS2NCBI::GeneFeature') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->isa("Bio::WGS2NCBI::GeneFeature")) 0 0 0
467 0 ▶ if ( $f->lies_within( @{ $coord{$id} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->lies_within(@{$coord{$id};})) { } 0 0 0
468 0 $drop = 0;
469 }
470 else {
471 0 $drop = 1;
472 }
473 }
474 0 ▶ if ( not $drop ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($drop) 0 0 0
475
476 # shift features leftward
477 0 ▶ if ( my $diff = $coord{$id}->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $diff = $coord{$id}[0]) 0 0 0
478 0 my @r = $f->range;
479 0 for my $r ( @r ) {
480 0 my @coord;
481 0 for my $coord ( @$r ) {
482 0 ▶ if ( $coord =~ /^([^0-9]*)(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coord =~ /^([^0-9]*)(\d+)$/) 0 0 0
483 0 my $prefix = $1;
484 0 my $number = $2;
485 0 $number -= $diff;
486 0 push @coord, $prefix . $number;
487 }
488 }
489 0 $r->[0] = $coord[0];
490 0 $r->[1] = $coord[1];
491 }
492 }
493 0 print $outtbl $f->to_string;
494 }
495 0 $oldid = $id;
496 }
497 }
498 }
499 }
500  
501 =head1 prune
502  
503 The C<prune> action reads a discrepancy file as supplied by NCBI, parses out errors that
504 have locations in them, which are then pruned from the table files in $config->datadir.
505  
506 This requires the following configuration settings:
507  
508 =over
509  
510 =item C<datadir>
511  
512 The location of the dir where the (potentially 'chunked', see below) sequence files
513 and feature tables were written by L<Bio::WGS2NCBI/process>.
514  
515 =item C<validation>
516  
517 The location where to read the validation report from NCBI.
518  
519 =item C<prefix>
520  
521 The ID prefix that was assigned to you by NCBI when you created your submission, something
522 like 'CR513_'
523  
524 =item C<authority>
525  
526 The naming authority prefix that you chose for your identifiers, something like
527 'gnl|aceprd|'
528  
529 The
530  
531 =back
532  
533 =cut
534  
535 0 ▶sub prune {
Subroutine prune: not called CC=20 SCAR=60.4
Pod undocumented
536 0 my $config = Bio::WGS2NCBI::Config->new;
537 0 my $datadir = $config->datadir;
538 0 my $discrep = $config->validation;
539 0 my $auth = $config->authority; # gnl|aceprd|
540 0 my $prefix = $config->prefix; # CR513_
541 0 my %locations;
542 my %objects;
543
544 # read locations from coordinates in report
545 0 ▶ open my $fh, '<', $discrep or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $discrep or die $! 0 0 0
546 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
547
548 # [(lcl|contig_3124:c<739-1821, 2000-2254)]
549 0 my $coord = qr/c?[<>]?\d+/;
550 0 my $seq = qr/[^:]+/;
551 0 ▶ if ( /lcl\|($seq):($coord-$coord)((?:, $coord-coord)*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/lcl\|($seq):($coord-$coord)((?:, $coord-coord)*)/) 0 0 0
552 0 my ( $seq, $first, $remainder ) = ( $1, $2, $3 );
553 0 ▶ $locations{$seq} = [] if not $locations{$seq};
Branch: 1 branch
BranchTrueFalseTotal
unless $locations{$seq} 0 0 0
554 0 push @{ $locations{$seq} }, [ split /-/, $first ];
555 0 INFO "Going to prune annotations on $seq:$first";
556
557 0 for my $r ( split /, /, $remainder ) {
558 0 ▶ if ( $r =~ /($coord)-($coord)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /($coord)-($coord)/) 0 0 0
559 0 my ( $start, $stop ) = ( $1, $2 );
560 0 push @{ $locations{$seq} }, [ $start => $stop ];
561 0 INFO "Extending range to prune $start => $stop";
562 }
563 }
564 }
565 0 ▶ if ( / \Q$auth\E($prefix\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/ \Q$auth\E($prefix\d+)/) 0 0 0
566 0 my $id = $1;
567 0 INFO "Going to prune annotations identified by $id";
568 0 $objects{$id}++;
569 }
570 }
571
572 # start reading feature tables
573 0 ▶ opendir my $dh, $datadir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $datadir or die $! 0 0 0
574 0 ▶ while( my $entry = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
575 0 ▶ if ( $entry =~ /\.tbl$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\.tbl$/) 0 0 0
576
577 # make pruned copy
578 0 INFO "Going to start writing to $datadir/$entry.pruned";
579 0 ▶ open my $fh, '>', "$datadir/$entry.pruned" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$datadir/$entry.pruned" or die $! 0 0 0
580
581 # instantiate reader and start scanning
582 0 INFO "Going to start reading $datadir/$entry";
583 my $read = Bio::WGS2NCBI::TableReader->new(
584 '-file' => "$datadir/$entry",
585 0 ▶ '-cb' => sub { print $fh ">Features @_\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
586 0 );
587
588 # iterate over features, write out with sequence separators
589 0 my $seq = '';
590 0 ▶ FEAT: while( my $feat = $read->next_feature ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feat = $read->next_feature) 0 0 0
591
592 # scan all the ranges
593 0 $seq = $read->seq;
594 0 ▶ if ( $locations{$seq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locations{$seq}) 0 0 0
595 0 for my $loc ( @{ $locations{$seq} } ) {
596 0 ▶ if ( $feat->lies_within(@$loc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feat->lies_within(@$loc)) 0 0 0
597 0 WARN "Pruning from location $seq:".$loc->[0].'-'.$loc->[1];
598 0 next FEAT;
599 }
600 }
601 }
602
603 # check object ids
604 0 ▶ if ( $feat->can('locus_tag') and $objects{$feat->locus_tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feat->can("locus_tag") and $objects{$feat->locus_tag}) 0 0 0
Condition0%0/3
$feat->can("locus_tag") and $objects{$feat->locus_tag}
!ll&&!rl&&r
000
MC/DC0%0/2
$feat->can("locus_tag") and $objects{$feat->locus_tag}
$feat->can("locus_tag") $objects{$feat->locus_tag}
Truth table0%0/3
A and B
A= $feat->can("locus_tag")B= $objects{$feat->locus_tag}
ABresult
0X0
100
111
605 0 WARN "Pruning $feat ".$feat->locus_tag;
606 0 next FEAT;
607 }
608 0 ▶ if ( $feat->can('protein_id') and $objects{$auth . $feat->protein_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feat->can("protein_id") and $objects{$auth . $feat->protein_id}) 0 0 0
Condition0%0/3
$feat->can("protein_id") and $objects{$auth . $feat->protein_id}
!ll&&!rl&&r
000
MC/DC0%0/2
$feat->can("protein_id") and $objects{$auth . $feat->protein_id}
$feat->can("protein_id") $objects{$auth . $feat->protein_id}
Truth table0%0/3
A and B
A= $feat->can("protein_id")B= $objects{$auth . $feat->protein_id}
ABresult
0X0
100
111
609 0 WARN "Pruning $feat ".$feat->protein_id;
610 0 next FEAT;
611 }
612
613 # still here? then print the feature
614 0 print $fh $feat->to_string;
615 }
616 }
617 }
618 }
619  
620 =head1 compress
621  
622 The C<compress> action bundles the ASN.1 files produced by C<Bio::WGS2NCBI/convert> into
623 a .tar.gz archive that can be uploaded to NCBI. This requires the following configuration
624 settings:
625  
626 =over
627  
628 =item C<outdir>
629  
630 The location where the ASN.1 files were written.
631  
632 =item C<archive>
633  
634 The name and location of the archive to produce.
635  
636 =back
637  
638 =cut
639  
640 0 ▶sub compress {
Subroutine compress: not called CC=4 SCAR=30.0
Pod undocumented
641 0 my $config = Bio::WGS2NCBI::Config->new;
642 0 my $tar = Archive::Tar->new;
643 0 my $sqndir = $config->outdir;
644 0 my $archive = $config->archive;
645
646 # open directory handle
647 0 INFO "going to add *.sqn files from $sqndir to $archive";
648 0 ▶ opendir my $dh, $sqndir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $sqndir or die $! 0 0 0
649
650 # iterate over files
651 0 my $counter;
652 0 ▶ while( my $entry = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $dh)) 0 0 0
653 0 ▶ if ( $entry =~ /\.sqn$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\.sqn$/) 0 0 0
654 0 $counter++;
655 0 $tar->add_files( "${sqndir}/${entry}" );
656 }
657 }
658 0 INFO "added $counter file(s)";
659
660 # compress results
661 0 INFO "going to compress $archive";
662 0 $tar->write( $archive, COMPRESS_GZIP );
663 }
664  
665 =head1 help
666  
667 Displays module documentation (which you are reading now).
668  
669 =cut
670  
671 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod undocumented
672 0 pod2usage({
673 '-verbose' => 2,
674 '-exitval' => 0,
675 '-noperldoc' => 1,
676 });
677 }
678  
679 4 1;
680  
« WGS2NCBI/FeatureSet.pm ↑ Summary WGS2NCBI/TableReader.pm »