WGS2NCBI/FeatureSet.pm

filter: Statementstmt 12.8% 21 / 164 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 30.7% 4 / 13 Podpod 100% 8 / 8 totaltotal 11.4% 33 / 288 CC 45 SCAR 59.5
CC
45
Cov
8%
_read_gene_line
702.0
read_features
240.0
new
12.0
CRAP 385.1
↑ Summary WGS2NCBI.pm »
1 package Bio::WGS2NCBI::FeatureSet;
2 4 ▶use Bio::WGS2NCBI::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # GFF3 column numbers
8 4 my $chr_idx = 0;
9 4 my $source_idx = 1;
10 4 my $type_idx = 2;
11 4 my $start_idx = 3;
12 4 my $end_idx = 4;
13 4 my $strand_idx = 6;
14 4 my $codon_idx = 7;
15 4 my $meta_idx = 8;
16  
17 =head1 NAME
18  
19 Bio::WGS2NCBI::FeatureSet - container of sets of features
20  
21 =head1 DESCRIPTION
22  
23 Objects from this class maintain an internal set of all
24 sequence features for a given sequence ID. Features can
25 are added (or removed) from the set while L<Bio::WGS2NCBI>
26 is reading a sequence with its annotations. In addition,
27 objects from this class maintain an (integer) offset by
28 which all features are shifted, e.g. in the case where
29 a scaffold starts with an assembly gap that needs to be
30 removed. Lastly, objects from this class implement the
31 logic for serializing all the annotations for a given
32 sequence in feature table format.
33  
34 =head1 METHODS
35  
36 =over
37  
38 =item new()
39  
40 Returns a new Bio::WGS2NCBI::FeatureSet object. This
41 constructor has one required, named argument: 'seqid',
42 which is the identifier of the sequence for which this
43 set holds the features. In addition, optionally,
44 an array reference of 'features' and an 'offset' can be
45 provided. For example:
46  
47 my $set = Bio::WGS2NCBI::FeatureSet->new(
48 seqid => 'scaffold1.1',
49 features => [ $feat1, $feat2 ],
50 offset => 33,
51 );
52  
53 =cut
54  
55 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
56 0 my $class = shift;
57 0 my %args = @_;
58 my $self = {
59 '_seqid' => $args{'seqid'},
60 '_features' => $args{'features'} || [],
61 0 ▶ '_offset' => $args{'offset'} || 0,
Condition0%0/4
$args{'features'} || []
l!l
00
$args{'offset'} || 0
l!l
00
MC/DC0%0/1
$args{'features'} || []
$args{'features'}
MC/DC0%0/1
$args{'offset'} || 0
$args{'offset'}
Truth table0%0/2
A
A= $args{'features'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'offset'}
Aresult
11
00
62 };
63 0 return bless $self, $class;
64 }
65  
66 =item offset()
67  
68 Gets/sets the offset by which all feature coordinates
69 must be shifted.
70  
71 =cut
72  
73 0 ▶sub offset {
Subroutine offset: not called CC=2 SCAR=17.9
Pod documented
74 0 my $self = shift;
75 0 ▶ $self->{'_offset'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
76 0 return $self->{'_offset'};
77 }
78  
79 =item add()
80  
81 Adds one or more features to the set.
82  
83 =cut
84  
85 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
86 0 my ( $self, @feat ) = @_;
87 0 push @{ $self->{'_features'} }, @feat;
88 }
89  
90 =item remove()
91  
92 Removes one or more features from the set.
93  
94 =cut
95  
96 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
97 0 my ( $self, @feat ) = @_;
98 0 my %remove = map { $_->id => 1 } @feat;
99 0 $self->{'_features'} = [ grep { ! $remove{ $_->id } } @{ $self->{'_features'} } ];
100  
101 }
102  
103 =item to_string()
104  
105 Writes the set out in feature table format.
106  
107 =cut
108  
109 0 ▶sub to_string {
Subroutine to_string: not called CC=3 SCAR=24.8
Pod documented
110 0 my $self = shift;
111 0 my $result = '>Features ' . $self->{'_seqid'} . "\n";
112 0 ▶ if ( my $offset = $self->offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $offset = $self->offset) 0 0 0
113 0 $result .= "[offset=-${offset}]\n";
114 }
115 0 for my $feat ( @{ $self->{'_features'} } ) {
116 0 $result .= $feat->to_string;
117 }
118 0 return $result;
119 }
120  
121 =item read_feature_set
122  
123  
124 =cut
125  
126 0 ▶sub read_feature_set {
Subroutine read_feature_set: not called CC=1 SCAR=6.9
Pod documented
127 0 my ( $class, $fh, $pos ) = @_;
128  
129 }
130  
131 =item read_features
132  
133 Reads and returns a FeatureSet. Arguments:
134  
135 - the file handle to read from
136 - a counter hash to generate IDs
137 - the Config object
138 - the focal Seq object
139 - the offset at the start of the sequence (e.g. after leading NNNs)
140 - the index of the last non-N residue in the sequence
141  
142 =cut
143  
144 0 ▶sub read_features {
Subroutine read_features: not called CC=15 SCAR=54.8
Pod documented
145 0 my ( $class, $fh, $counter, $config, $seq, $offset, $last_non_missing_index ) = @_;
146 0 INFO "Reading features for ".$seq->id;
147
148 # instantiate new set
149 0 my $set = $class->new( 'seqid' => $seq->id );
150
151 # this is re-set for every gene
152 0 my ( $gene, $cds, $mrna, $skipgene );
153
154 0 ▶ LINE: while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
155 0 chomp;
156 0 my @line = split /\t/, $_;
157
158 # raw start and end coordinates when reading left-to-right. these need to be
159 # reconciled with the strandedness, after reducing the left-to-right to the
160 # last index for non-missing characters in the raw sequence. if we don't do
161 # that we end up with features that run beyond the end of their sequence.
162 0 my ( $ltrs, $ltre ) = @line[$start_idx,$end_idx];
163
164 # last_non_missing_index is the 0-based index for the last character that is
165 # not an N in the sequence. since feature tables use 1-based coordinates we
166 # add one. No $ltre may exceed this adjusted end.
167 0 my $adjusted_end = $last_non_missing_index + 1;
168 0 ▶ $ltre = $adjusted_end if $ltre > $adjusted_end;
Branch: 1 branch
BranchTrueFalseTotal
if $ltre > $adjusted_end 0 0 0
169 0 ▶ my ( $start, $end ) = $line[$strand_idx] eq '+' ? ($ltrs,$ltre) : ($ltre,$ltrs);
Branch: 1 branch
BranchTrueFalseTotal
$line[$strand_idx] eq '+' ? : 0 0 0
170
171 # initialize shared constructor args
172 0 my %args = ( 'type' => $line[$type_idx] );
173
174 # found a new gene, start creating objects
175 0 ▶ if ( $line[$type_idx] eq 'gene' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line[$type_idx] eq 'gene') { } 0 0 0
elsif ($line[$type_idx] eq 'CDS') { } 0 0 0
elsif ($line[$type_idx] eq 'five_prime_UTR') { } 0 0 0
elsif ($line[$type_idx] eq 'three_prime_UTR') { } 0 0 0
176
177 # ignore genes that fall outside the range because we stripped
178 # leading gaps
179 0 ▶ if ( $line[$start_idx] - $offset <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[$start_idx] - $offset <= 0) { } 0 0 0
180 0 $skipgene = 1;
181 0 WARN "First gene on ".$seq->id()." outside of allowed range, skipping";
182 0 next LINE;
183 }
184 else {
185 0 $skipgene = 0;
186 }
187  
188 # finish the previous gene, if we have any
189 0 ▶ if ( $gene ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gene) 0 0 0
190 0 $gene->annotate_short_introns($cds,$mrna,$set,$config);
191 0 $gene->annotate_partials($cds,$mrna,$seq);
192 }
193
194 # clear the caches
195 0 $gene = $cds = $mrna = undef;
196
197 # add additional constructor args
198 0 $args{'strand'} = $line[$strand_idx];
199 0 $args{'locus_tag'} = $config->prefix . sprintf "%05s", ++$counter->{'gene'};
200 0 $args{'range'} = [ [ $start, $end ] ];
201
202 # read the line
203 0 _read_gene_line(\%args,@line);
204 0 DEBUG "Gene: " . $args{'product'};
205
206 # create the object
207 0 $gene = Bio::WGS2NCBI::Feature->new(%args);
208 0 $set->add($gene);
209 }
210 elsif ( $line[$type_idx] eq 'CDS' ) {
211 0 ▶ next LINE if $skipgene;
Branch: 1 branch
BranchTrueFalseTotal
if $skipgene 0 0 0
212
213 # we only create one CDS and one mRNA object, which both span multiple ranges
214 0 ▶ if ( not $cds and not $mrna ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cds || $mrna) { } 0 0 0
Condition0%0/3
$cds || $mrna
l!l&&r!l&&!r
000
MC/DC0%0/2
$cds || $mrna
$cds $mrna
Truth table0%0/3
A || B
A= $cdsB= $mrna
ABresult
1X1
011
000
215 0 DEBUG "Instantiating new CDS and mRNA";
216 0 my $auth = $config->authority;
217 0 ▶ if ( not $auth ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth) 0 0 0
218 0 WARN "No naming authority specified!";
219 }
220
221 # create the CDS
222 0 $args{'product'} = $gene->product;
223 0 $args{'protein_id'} = $auth . $gene->locus_tag;
224 0 $args{'transcript_id'} = $auth . $gene->locus_tag . '.mrna';
225 0 $cds = Bio::WGS2NCBI::Feature->new(
226 %args,
227 'phase' => $line[$codon_idx],
228 'note' => $gene->note,
229 'db_xref' => $gene->db_xref,
230 'range' => [ [ $start, $end ] ],
231 );
232 0 $set->add($cds);
233
234 # create the mRNA
235 0 $args{'type'} = 'mRNA';
236 0 $mrna = Bio::WGS2NCBI::Feature->new( %args,'range' => [ [ $start, $end ] ]);
237 0 $set->add($mrna);
238 }
239 else {
240 0 DEBUG "growing range for CDS and mRNA ($start..$end)";
241 0 $cds->range( [ $start, $end ] );
242 0 $mrna->range( [ $start, $end ] );
243 }
244 }
245 elsif ( $line[$type_idx] eq 'five_prime_UTR' ) {
246 0 $gene->five_prime_UTR( [ $line[$start_idx], $line[$end_idx] ] );
247 }
248 elsif ( $line[$type_idx] eq 'three_prime_UTR' ) {
249 0 $gene->three_prime_UTR( [ $line[$start_idx], $line[$end_idx] ] );
250 }
251 }
252
253 # finish the final gene, if we have any
254 0 ▶ if ( $gene ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gene) 0 0 0
255 0 $gene->annotate_short_introns($cds,$mrna,$set,$config);
256 0 $gene->annotate_partials($cds,$mrna,$seq);
257 }
258
259 0 return $set;
260 }
261  
262 # not a method, a helper function that is called from read_features
263 0 ▶sub _read_gene_line {
Subroutine _read_gene_line: not called CC=26 SCAR=65.5
264 0 my ( $args, @line ) = @_;
265 0 $args->{'product'} = 'hypothetical protein';
266
267 # here we parse out the whole note and the gene symbol and product
268 0 ▶ if ( $line[$meta_idx] =~ /Note=([^;]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[$meta_idx] =~ /Note=([^;]+)/) 0 0 0
269 0 $args->{'note'} = $1;
270
271 # some GFF3 features are URL-encoded, this needs to be corrected
272 0 ▶ if ( $args->{'note'} =~ /%/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"note"} =~ /%/) 0 0 0
273 0 INFO "[URLENCODED] possible URL encoding in note: ".$args->{'note'};
274 0 $args->{'note'} = uri_unescape($args->{'note'});
275 0 INFO "[URLENCODED] corrected as: ".$args->{'note'};
276 }
277 0 ▶ if ( $args->{'note'} =~ /Similar to ([^:]+):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"note"} =~ /Similar to ([^:]+):/) 0 0 0
278 0 $args->{'gene'} = $1;
279 }
280 0 ▶ if ( $args->{'note'} =~ /: (.+?) \([^\(\)]+\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"note"} =~ /: (.+?) \([^\(\)]+\)$/) 0 0 0
281 0 $args->{'product'} = $1;
282
283 # product should not end with 'domain'
284 0 ▶ if ( $args->{'product'} =~ /domain$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /domain$/i) 0 0 0
285 0 INFO "[DOMAIN] product ends with 'domain': ".$args->{'product'};
286 0 $args->{'product'} .= ' protein';
287 0 INFO "[DOMAIN] corrected as: ".$args->{'product'};
288 }
289
290 # Implies evolutionary relationship; change to -like protein
291 0 ▶ if ( $args->{'product'} =~ /homolog/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /homolog/i) 0 0 0
292 0 INFO "[HOMOLOG] product contains 'homolog': ".$args->{'product'};
293 0 $args->{'product'} =~ s/ homolog/-like/i;
294 0 INFO "[HOMOLOG] corrected as: ".$args->{'product'};
295 }
296
297 # features ends with 'like',
298 # Consider adding 'protein' to the end of the product name
299 0 ▶ if ( $args->{'product'} =~ /like$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /like$/i) 0 0 0
300 0 INFO "[LIKE] product ends with like: ".$args->{'product'};
301 0 $args->{'product'} .= ' protein';
302 0 INFO "[LIKE] corrected as: ".$args->{'product'};
303 }
304
305 # feature contains 'gene'
306 0 ▶ if ( $args->{'product'} =~ /\bgene\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /\bgene\b/i) 0 0 0
307 0 INFO "[GENE] product contains 'gene': ".$args->{'product'};
308 }
309
310 # product may contain database identifier
311 0 ▶ if ( $args->{'product'} =~ /\d{3}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /\d{3}/) 0 0 0
312 0 INFO "[DBID] product may contain database identifier: ".$args->{'product'};
313 }
314
315 # product contains organelle
316 0 ▶ if ( $args->{'product'} =~ /mitochondrial/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /mitochondrial/i) 0 0 0
317 0 INFO "[ORGANELLE] product contains organelle: ".$args->{'product'};
318 }
319
320 # product starts with 'Probable'
321 0 ▶ if ( $args->{'product'} =~ /^Probable\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /^Probable\b/i) 0 0 0
322 0 $args->{'product'} =~ s/^Probable/putative/i;
323 0 INFO "[PROBABLE] product starts with probable, changed to putative";
324 }
325  
326 # product starts with 'Probable'
327 0 ▶ if ( $args->{'product'} =~ /^Uncharacterized\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"product"} =~ /^Uncharacterized\b/i) 0 0 0
328 0 $args->{'product'} =~ s/^Uncharacterized/putative/i;
329 0 INFO "[UNCHARACTERIZED] product starts with Uncharacterized, changed to putative";
330 }
331 }
332 0 ▶ if ( $args->{'gene'} and $args->{'product'} eq 'hypothetical protein' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"gene"} and $args->{"product"} eq "hypothetical protein") 0 0 0
Condition0%0/3
$args->{"gene"} and $args->{"product"} eq "hypothetical protein"
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"gene"} and $args->{"product"} eq "hypothetical protein"
$args->{"gene"} $args->{"product"} eq "hypothetical protein"
Truth table0%0/3
A and B
A= $args->{"gene"}B= $args->{"product"} eq "hypothetical protein"
ABresult
0X0
100
111
333 0 $args->{'product'} = $args->{'gene'};
334 }
335 }
336
337 # there are potentially multiple database cross-references, e.g.
338 # to Pfam and InterPro
339 0 ▶ if ( $line[$meta_idx] =~ /Dbxref=([^;]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[$meta_idx] =~ /Dbxref=([^;]+)/) 0 0 0
340 0 my $refs = $1;
341 0 $refs =~ s/Pfam/PFAM/g;
342 0 $args->{'db_xref'} = [ split /,/, $refs ];
343 }
344
345 # check if we need to replace from INI
346 0 my $config = Bio::WGS2NCBI::Config->new;
347 0 ▶ if ( my $file = $config->products ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $config->products) 0 0 0
348 0 my %map = $config->read_ini( $file );
349 0 ▶ if ( my $fixed = $map{ $args->{'product'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fixed = $map{$args->{"product"}}) 0 0 0
350 0 ▶ if ( ref $fixed and ref $fixed eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fixed and ref $fixed eq "ARRAY") 0 0 0
Condition0%0/3
ref $fixed and ref $fixed eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $fixed and ref $fixed eq "ARRAY"
ref $fixed ref $fixed eq "ARRAY"
Truth table0%0/3
A and B
A= ref $fixedB= ref $fixed eq "ARRAY"
ABresult
0X0
100
111
351 0 $fixed = pop(@{$fixed});
352 WARN "Multiple mappings for the same product, will use last seen: '"
353 0 . $args->{'product'}."' => '".$fixed."'";
354 }
355 0 INFO "Replacing '".$args->{'product'}."' with '$fixed' from '$file'";
356 0 $args->{'product'} = $fixed;
357 }
358 }
359
360 # check to see if we should delete uninformative gene symbols
361 0 ▶ if ( $args->{'gene'} and ( $args->{'gene'} eq 'hypothetical protein' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"gene"} and $args->{"gene"} eq "hypothetical protein") 0 0 0
Condition0%0/3
$args->{"gene"} and $args->{"gene"} eq "hypothetical protein"
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"gene"} and $args->{"gene"} eq "hypothetical protein"
$args->{"gene"} $args->{"gene"} eq "hypothetical protein"
Truth table0%0/3
A and B
A= $args->{"gene"}B= $args->{"gene"} eq "hypothetical protein"
ABresult
0X0
100
111
362 0 WARN "Gene symbol was set to 'hypothetical protein', deleting...";
363 0 delete $args->{'gene'};
364 }
365 0 ▶ if ( $args->{'gene'} and ( $args->{'product'} eq 'hypothetical protein' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"gene"} and $args->{"product"} eq "hypothetical protein") 0 0 0
Condition0%0/3
$args->{"gene"} and $args->{"product"} eq "hypothetical protein"
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"gene"} and $args->{"product"} eq "hypothetical protein"
$args->{"gene"} $args->{"product"} eq "hypothetical protein"
Truth table0%0/3
A and B
A= $args->{"gene"}B= $args->{"product"} eq "hypothetical protein"
ABresult
0X0
100
111
366 0 my $sym = $args->{'gene'};
367 0 WARN "Product was 'hypothetical protein', moving gene name '$sym' to note";
368 0 delete $args->{'gene'};
369 0 ▶ if ( not defined $args->{'note'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $args->{'note'}) { } 0 0 0
elsif (not ref $args->{'note'}) { } 0 0 0
370 0 $args->{'note'} = $sym;
371 }
372 elsif ( not ref $args->{'note'} ) {
373 0 $args->{'note'} = [ $args->{'note'}, $sym ];
374 }
375 else {
376 0 push @{ $args->{'note'} }, $sym;
377 }
378 }
379 }
380  
381 =item write_features
382  
383 Writes the FeatureSet to the provided file handle.
384  
385 =cut
386  
387 0 ▶sub write_features {
Subroutine write_features: not called CC=1 SCAR=6.9
Pod documented
388 0 my ( $features, $fh ) = @_;
389 0 print $fh $features->to_string;
390 }
391  
392 =back
393  
394 =cut
395  
396 4 1;
↑ Summary WGS2NCBI.pm »