blib/lib/Bio/Gonzales/Feat.pm

filter: Statementstmt 75.9% 60 / 79 Branchbran 50.0% 5 / 10 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 70.5% 12 / 17 Podpod 50.0% 4 / 8 totaltotal 68.7% 90 / 131 CC 14 SCAR 23.8
CC
14
Cov
72%
to_gff3
23.1
as_hash
2.0
length
2.0
CRAP 10.8
« blib/lib/Bio/Gonzales/GO/Util.pm ↑ Summary blib/lib/Bio/Gonzales/IDMapIO.pm »
1 package Bio::Gonzales::Feat;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::MoreUtils qw/zip/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Bio::Gonzales::Seq::Util qw/strand_convert/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $QUIET_MODE;
14  
15 4 our $ATTR_ESCAPE_RE = qr/([\x00-\x1F\x7F%&\=;,])/;
16  
17 4 our %FIXED_ATTRIBUTE_NAMES = (
18 ID => 1,
19 Parent => 2,
20 Target => 3,
21 Name => 4,
22 Alias => 5,
23 Gap => 6,
24 Derives_from => 7,
25 Dbxref => 9,
26 Ontology_term => 10,
27 Is_circular => 11,
28 Note => 99,
29 );
30  
31 4 our $VERSION = '0.058'; # VERSION
32  
33 4 extends 'Bio::Gonzales::MiniFeat';
34  
35 4 has [qw/seq_id start end strand/] => ( is => 'rw', required => 1 );
36  
37 4 has [qw/phase score /] => ( is => 'rw' );
38  
39 sub as_hash {
40 0 ▶ my $self = shift;
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
41 return {
42 0 seq_id => $self->seq_id,
43 source => $self->source,
44 type => $self->type,
45 attributes => $self->attributes,
46 start => $self->start,
47 end => $self->end,
48 strand => $self->strand,
49 phase => $self->phase,
50 score => $self->score,
51 };
52 }
53  
54 25 ▶sub scf_id { return shift->seq_id(@_); }
Subroutine scf_id: called CC=1 SCAR=0.0
Pod documented
55  
56 0 ▶sub length { return $_[0]->end - $_[0]->start + 1 }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
57  
58 0 ▶sub begin { return shift->start(@_) }
Subroutine begin: not called CC=1 SCAR=6.9
Pod documented
59  
60 0 ▶sub Convert_strand { strand_convert(@_) }
Subroutine Convert_strand: not called CC=1 SCAR=6.9
Pod documented
61  
62 sub switch_coords {
63 0 ▶ my $self = shift;
Subroutine switch_coords: not called CC=1 SCAR=6.9
Pod undocumented
64  
65 0 my ( $b, $e ) = ( $self->start, $self->end );
66 0 $self->start($e);
67 0 $self->end($b);
68  
69 0 return $self;
70 }
71  
72 sub sort_subfeats {
73 56 ▶ my ($self) = @_;
Subroutine sort_subfeats: called CC=2 SCAR=6.9
Pod undocumented
74  
75 56 ▶ my @sf = sort { ( $a->start <=> $b->start ) || ( $b->end <=> $a->end ) } @{ $self->subfeats };
Branch: 1 branch
BranchTrueFalseTotal
$a->start <=> $b->start or $b->end <=> $a->end 3 42 45
76 56 $self->subfeats( \@sf );
77 }
78  
79 sub to_gff3 {
80 141 ▶ my ( $self, $escape_whitespace_everywhere ) = @_;
Subroutine to_gff3: called CC=13 SCAR=31.4
Pod undocumented
81  
82 141 my $strand = strand_convert( $self->strand );
83  
84 141 my $attributes = $self->attributes;
85 #sort the attributes
86 141 ▶ my @attr_names = sort { ( $FIXED_ATTRIBUTE_NAMES{$a} || 98 ) <=> ( $FIXED_ATTRIBUTE_NAMES{$b} || 98 ) }
Condition100%4/4
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$a} || 98
l!l
134215
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$b} || 98
l!l
74275
MC/DC100%1/1
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$a} || 98
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$a}
MC/DC100%1/1
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$b} || 98
$Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$b}
Truth table100%2/2
A
A= $Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$a}
Aresult
11
00
Truth table100%2/2
A
A= $Bio::Gonzales::Feat::FIXED_ATTRIBUTE_NAMES{$b}
Aresult
11
00
87 keys %$attributes;
88  
89 141 my @groups;
90 141 for my $a (@attr_names) {
91 423 my @escaped_v;
92 423 for my $v ( @{ $attributes->{$a} } ) {
93 423 ▶ unless ( defined($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 423 423
94 0 carp "The attribute " . $a . " of feature " . $self->id . " has uninitialized values";
95 0 $v = '';
96 }
97  
98 423 $v =~ s/$ATTR_ESCAPE_RE/sprintf("%%%02X",ord($1))/ge;
99 423 ▶ $v =~ s/ /%20/g if ( $escape_whitespace_everywhere && $a ne 'Target' );
Branch: 1 branch
BranchTrueFalseTotal
if $escape_whitespace_everywhere and $a ne "Target" 0 423 423
Condition33%1/3
$escape_whitespace_everywhere and $a ne "Target"
!ll&&!rl&&r
42300
MC/DC0%0/2
$escape_whitespace_everywhere and $a ne "Target"
$escape_whitespace_everywhere $a ne "Target"
Truth table33%1/3
A and B
A= $escape_whitespace_everywhereB= $a ne "Target"
ABresult
0X0
100
111
100 423 push @escaped_v, $v;
101 }
102  
103 423 ▶ if ( $a eq 'Target' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a eq "Target") 0 423 423
104 0 for my $v (@escaped_v) {
105 0 ▶ if ( $v =~ /^"?(.*?)\s+(\d+\s+\d+(?:\s+[-.+])?)\s*"?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^"?(.*?)\s+(\d+\s+\d+(?:\s+[-.+])?)\s*"?$/) 0 0 0
106 0 my ( $tid, $rest ) = ( $1, $2 );
107 0 $tid =~ s/ /%20/g;
108 0 $v = join " ", $tid, $rest;
109 }
110 }
111 }
112  
113 423 $a =~ s/$ATTR_ESCAPE_RE/sprintf("%%%02X",ord($1))/ge;
114  
115 423 push @groups, $a . '=' . join( ',', @escaped_v );
116 }
117  
118 141 ▶ return join( "\t",
Condition50%2/4
$self->score // "."
l!l
1410
$self->phase // "."
l!l
0141
MC/DC0%0/1
$self->score // "."
$self->score
MC/DC0%0/1
$self->phase // "."
$self->phase
Truth table50%1/2
A
A= $self->score
Aresult
11
00
Truth table50%1/2
A
A= $self->phase
Aresult
11
00
119 $self->seq_id, $self->source, $self->type,
120 $self->start, $self->end, $self->score // '.',
121 $strand, $self->phase // '.', join( ';', @groups ) )
122 . "\n";
123 }
124 4 __PACKAGE__->meta->make_immutable;
125  
126 4 1;
127  
128 __END__
129  
130 =head1 NAME
131  
132 Bio::Gonzales::Feat - a sequence feature
133  
134 =head1 SYNOPSIS
135  
136 Bio::Gonzales::Feat->new(
137 seq_id => 'chr01',
138 source => 'glimmerhmm',
139 type => 'exon',
140 start => 324,
141 end => 6342,
142 strand => -1,
143 attributes => { ID => [ 'exon01' ], Parent => [ 'gene01', 'gene02' ] },
144 );
145  
146 =head1 DESCRIPTION
147  
148 Represents a sequence feature. The field C<attributes> is not required to
149 create an object of class Bio::Gonzales::Feat. This class is based on the
150 L<Sequence Ontology GFF3 specification|http://www.sequenceontology.org/gff3.shtml>
151  
152 =head1 METHODS
153  
154 =over 4
155  
156 =item B<< \%attributes = $f->attr >>
157  
158 =item B<< \%attributes = $f->attributes >>
159  
160 =item B<< $sequence_id = $f->seq_id >>
161  
162 =item B<< $souce = $f->source >>
163  
164 =item B<< $f->source($new_source) >>
165  
166 Gets and sets the source attribute of the feature.
167  
168 =item B<< $type = $f->type >>
169  
170 =item B<< $f->type($new_type) >>
171  
172 Gets and sets the type attribute of the feature.
173  
174 =item B<< $start_coord = $f->start >>
175  
176 =item B<< $start_coord = $f->begin >>
177  
178 =item B<< $f->start($start_1_based_coord) >>
179  
180 =item B<< $f->begin($start_1_baed_coord) >>
181  
182 Get or set the start coord of the feature.
183  
184 =item B<< $f->end >>
185  
186 The same syntax as C<$f->start>, only for the end coordianate.
187  
188 =item B<< $f->strand($strand) >>
189  
190 =item B<< $strand = $f->strand >>
191  
192 Set or get the strand. The strand can be -1 (minus strand), 0 (strand unknown) or 1 (plus strand).
193  
194 =item B<< $phase = $f->phase >>
195  
196 =item B<< $f->phase($phase) >>
197  
198 Gets or sets the phase.
199  
200 =item B<< $score = $f->score >>
201  
202 =item B<< $f->score($score) >>
203  
204 Gets or sets the score.
205  
206 =item B<< $f->attributes >>
207  
208 =item B<< $f->attr >>
209  
210 Get or set the attributes of the feature. Structure:
211  
212 {
213 ID => [ 'id01' ],
214 Parent => [ 'parent1', 'parent2', ... ]
215 ...
216 }
217  
218 =item B<< $f->subfeats >>
219  
220 Gives access to a general container for subfeature objects. Makes grouping
221 easier, e.g. for BED output format. An example would be an 'mRNA'-object that
222 has several exons as subfeatures.
223  
224 =item B<< $f->parentfeats >>
225  
226 The same as C<$f->subfeats>, only with parent relation. This function is
227 completely unrelated to the C<$f->parent_id> function. C<$f->parent_id> only
228 accesses the attributes, not the parentfeature container.
229  
230 =item B<< $f->scf_id >>
231  
232 This is a synonym for C<$f->seq_id>.
233  
234 =item B<< $first_value = $f->attr_first($attribute_key) >>
235  
236 =item B<< $first_value = $f->first_attr($attribute_key) >>
237  
238 The functions C<attr_first> and C<first_attr> retrieve the value of the first
239 element of the given attribute. An example would be
240  
241 my $id = $f->attr_first("ID");
242  
243 # in case of multiple parents only the first entry/parent will be returned.
244 my $parent = $f->attr_first("Parent");
245  
246  
247 =item B<< $id = $f->id >>
248  
249 Retrieve the value of the "ID" attribute. If a feature has multiple ids, a
250 warning will be printed. Effectively a shortcut for C<$f->attributes->{ID}[0]>.
251  
252 =item B<< @ids = $f->ids >>
253  
254 =item B<< \@ids = $f->ids >>
255  
256 A shortcut for C<$f->attributes->{ID}>. Returns a list of IDs in list context,
257 a reference to the ID list in scalar context.
258  
259 =item B<< @parent_ids = $f->parent_ids >>
260  
261 =item B<< \@parent_ids = $f->parent_ids >>
262  
263 A shortcut for C<$f->attributes->{Parent}>. Returns a list of parent IDs in list context,
264 a reference to the parent ID list in scalar context.
265  
266 =item B<< $parent_id = $f->parent_id >>
267  
268 A shortcut for C<$f->attributes->{Parent}[0]>. Gives a warning if multiple
269 parent ids are present.
270  
271 =item B<< $f->add_attr(%attributes) >>
272  
273 To add an attribute, call C<add_attr> with either a hash of the form
274  
275 %attributes = (
276 ID => "mrna_01",
277 Parent => "gene_01"
278 );
279  
280 or
281  
282 %attributes = (
283 ID => "exon_01",
284 Parent => [ "gene_01", "gene_02" ],
285 );
286  
287 =item B<< \@deleted_attributes = $f->del_attr(@attribute_names) >>
288  
289 =item B<< $deleted_attribute = $f->del_attr($attribute_name) >>
290  
291 Deletes all attributes in C<@attribute_names>.
292  
293 =item B<< Bio::Gonzales::Feat->Convert_strand($strand) >>
294  
295 Convert between numeric and character strand respresentation.
296  
297  
298 =item B<< $cloned_f = $f->clone >>
299  
300 Clone the feature, deeply (incl. subfeatures and parentfeatures).
301  
302 =item B<< $length = $f->length >>
303  
304 The length (end -start +1)
305  
306 =back
307  
308 =head1 SEE ALSO
309  
310 =head1 AUTHOR
311  
312 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
313  
314 =cut
« blib/lib/Bio/Gonzales/GO/Util.pm ↑ Summary blib/lib/Bio/Gonzales/IDMapIO.pm »