FeatureIO/bed.pm

filter: Statementstmt 57.1% 44 / 77 Branchbran 25.0% 9 / 36 Conditioncond 28.5% 6 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 46.6% 76 / 163 CC 30 SCAR 43.1
CC
30
Cov
44%
write_feature
210.0
next_feature
12.3
_initialize
7.9
CRAP 74.3
« FeatureIO/gff.pm ↑ Summary SeqFeature/Annotated.pm »
1 =pod
2  
3 =head1 NAME
4  
5 Bio::FeatureIO::bed - read/write features from UCSC BED format
6  
7 =head1 SYNOPSIS
8  
9 my $in = Bio::FeatureIO(-format => 'bed', -file => 'file.bed');
10 for my $feat ($in->next_feature) {
11 # do something with $feat (a Bio::SeqFeature::Annotated object)
12 }
13  
14 my $out = Bio::FeatureIO(-format=>'bed');
15 for my $feat ($seq->get_seqFeatures) {
16 $out->write_feature($feat);
17 }
18  
19 =head1 DESCRIPTION
20  
21 See L<http://www.genome.ucsc.edu/goldenPath/help/customTrack.html#BED>.
22  
23 Currently for read and write only the first 6 fields (chr, start, end, name,
24 score, strand) are supported.
25  
26 =head1 FEEDBACK
27  
28 =head2 Mailing Lists
29  
30 User feedback is an integral part of the evolution of this and other
31 Bioperl modules. Send your comments and suggestions preferably to
32 the Bioperl mailing list. Your participation is much appreciated.
33  
34 bioperl-l@bioperl.org - General discussion
35 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
36  
37 =head2 Support
38  
39 Please direct usage questions or support issues to the mailing list:
40  
41 I<bioperl-l@bioperl.org>
42  
43 rather than to the module maintainer directly. Many experienced and
44 reponsive experts will be able look at the problem and quickly
45 address it. Please include a thorough description of the problem
46 with code and data examples if at all possible.
47  
48 =head2 Reporting Bugs
49  
50 Report bugs to the Bioperl bug tracking system to help us keep track
51 of the bugs and their resolution. Bug reports can be submitted via
52 the web:
53  
54 http://bugzilla.open-bio.org/
55  
56 =head1 AUTHOR - Allen Day
57  
58 Email allenday@ucla.edu
59  
60 =head1 CONTRIBUTORS
61  
62 Sendu Bala, bix@sendu.me.uk
63  
64 =head1 APPENDIX
65  
66 The rest of the documentation details each of the object methods.
67 Internal methods are usually preceded with a _
68  
69 =cut
70  
71  
72 # Let the code begin...
73  
74  
75 package Bio::FeatureIO::bed;
76 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 $Bio::FeatureIO::bed::AUTHORITY = 'cpan:BIOPERLML';
78 }
79 2 $Bio::FeatureIO::bed::VERSION = '1.6.905';
80 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use base qw(Bio::FeatureIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶use Bio::SeqFeature::Annotated;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶use Bio::Annotation::SimpleValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶use Bio::OntologyIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 =head2 _initialize
88  
89 Title : _initialize
90 Function: initializes BED for reading/writing
91 Args : all optional:
92 name description
93 ----------------------------------------------------------
94 -name the name for the BED track, stored in header
95 name defaults to localtime()
96 -description the description for the BED track, stored in
97 header. defaults to localtime().
98 -use_score whether or not the score attribute of
99 features should be used when rendering them.
100 the higher the score the darker the color.
101 defaults to 0 (false)
102  
103  
104  
105 =cut
106  
107 1 ▶sub _initialize {
Subroutine _initialize: called CC=6 SCAR=20.7
108 1 my($self,%arg) = @_;
109  
110 1 $self->SUPER::_initialize(%arg);
111  
112 1 ▶ $self->name($arg{-name} || scalar(localtime()));
Condition50%1/2
$arg{'-name'} || scalar localtime()
l!l
01
MC/DC0%0/1
$arg{'-name'} || scalar localtime()
$arg{'-name'}
Truth table50%1/2
A
A= $arg{'-name'}
Aresult
11
00
113 1 ▶ $self->description($arg{-description} || scalar(localtime()));
Condition50%1/2
$arg{'-description'} || scalar localtime()
l!l
01
MC/DC0%0/1
$arg{'-description'} || scalar localtime()
$arg{'-description'}
Truth table50%1/2
A
A= $arg{'-description'}
Aresult
11
00
114 1 ▶ $self->use_score($arg{-use_score} || 0);
Condition50%1/2
$arg{'-use_score'} || 0
l!l
01
MC/DC0%0/1
$arg{'-use_score'} || 0
$arg{'-use_score'}
Truth table50%1/2
A
A= $arg{'-use_score'}
Aresult
11
00
115  
116 1 ▶ $self->_print(sprintf('track name="%s" description="%s" useScore=%d',
Branch: 2 branches
BranchTrueFalseTotal
if $self->mode eq "w" 0 1 1
$self->use_score ? : 0 0 0
117 $self->name,
118 $self->description,
119 $self->use_score ? 1 : 0
120 )."\n") if $self->mode eq 'w';
121 }
122  
123 =head2 use_score
124  
125 Title : use_score
126 Usage : $obj->use_score($newval)
127 Function: should score be used to adjust feature color when rendering? set to true if so.
128 Example :
129 Returns : value of use_score (a scalar)
130 Args : on set, new value (a scalar or undef, optional)
131  
132  
133 =cut
134  
135 1 ▶sub use_score{
Subroutine use_score: called CC=2 SCAR=8.1
Pod documented
136 1 my $self = shift;
137  
138 1 ▶ return $self->{'use_score'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
139 0 return $self->{'use_score'};
140 }
141  
142 =head2 name
143  
144 Title : name
145 Usage : $obj->name($newval)
146 Function: name of BED track
147 Example :
148 Returns : value of name (a scalar)
149 Args : on set, new value (a scalar or undef, optional)
150  
151  
152 =cut
153  
154 1 ▶sub name{
Subroutine name: called CC=2 SCAR=8.1
Pod documented
155 1 my $self = shift;
156  
157 1 ▶ return $self->{'name'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
158 0 return $self->{'name'};
159 }
160  
161 =head2 description
162  
163 Title : description
164 Usage : $obj->description($newval)
165 Function: description of BED track
166 Example :
167 Returns : value of description (a scalar)
168 Args : on set, new value (a scalar or undef, optional)
169  
170  
171 =cut
172  
173 1 ▶sub description{
Subroutine description: called CC=2 SCAR=8.1
Pod documented
174 1 my $self = shift;
175  
176 1 ▶ return $self->{'description'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
177 0 return $self->{'description'};
178 }
179  
180  
181 0 ▶sub write_feature {
Subroutine write_feature: not called CC=14 SCAR=53.5
Pod documented
182 0 my($self,$feature) = @_;
183 0 ▶ $self->throw("only Bio::SeqFeature::Annotated objects are writeable") unless $feature->isa('Bio::SeqFeature::Annotated');
Branch: 1 branch
BranchTrueFalseTotal
unless $feature->isa("Bio::SeqFeature::Annotated") 0 0 0
184  
185 0 ▶ my $chrom = $feature->seq_id || '';
Condition0%0/2
$feature->seq_id || ''
l!l
00
MC/DC0%0/1
$feature->seq_id || ''
$feature->seq_id
Truth table0%0/2
A
A= $feature->seq_id
Aresult
11
00
186 0 ▶ my $chrom_start = $feature->start || 0; # output start is supposed to be 0-based
Condition0%0/2
$feature->start || 0
l!l
00
MC/DC0%0/1
$feature->start || 0
$feature->start
Truth table0%0/2
A
A= $feature->start
Aresult
11
00
187 0 ▶ my $chrom_end = ($feature->end + 1) || 1; # output end is supposed to not be part of the feature
Condition0%0/2
$feature->end + 1 || 1
l!l
00
MC/DC0%0/1
$feature->end + 1 || 1
$feature->end + 1
Truth table0%0/2
A
A= $feature->end + 1
Aresult
11
00
188  
189 #try to make a reasonable name
190 0 my $name = undef;
191 0 my @v;
192 0 ▶ if (@v = ($feature->annotation->get_Annotations('Name'))){
Branch: 2 branches
BranchTrueFalseTotal
if (@v = $feature->annotation->get_Annotations('Name')) { } 0 0 0
elsif (@v = $feature->annotation->get_Annotations('ID')) { } 0 0 0
193 0 $name = $v[0];
194 0 ▶ $self->warn("only using first of feature's multiple names: ".join ',', map {$_->value} @v) if scalar(@v) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @v > 1 0 0 0
195 } elsif (@v = ($feature->annotation->get_Annotations('ID'))){
196 0 $name = $v[0];
197 0 ▶ $self->warn("only using first of feature's multiple IDs: ".join ',', map {$_->value} @v) if scalar(@v) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @v > 1 0 0 0
198 } else {
199 0 $name = 'anonymous';
200 }
201
202 0 ▶ if (ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 0 0 0
203 0 $name = $name->value;
204 }
205 0 ▶ if (ref($chrom)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chrom) 0 0 0
206 0 $chrom = $chrom->value;
207 }
208  
209 0 ▶ my $score = $feature->score || 0;
Condition0%0/2
$feature->score || 0
l!l
00
MC/DC0%0/1
$feature->score || 0
$feature->score
Truth table0%0/2
A
A= $feature->score
Aresult
11
00
210 0 ▶ my $strand = $feature->strand == 0 ? '-' : '+'; #default to +
Branch: 1 branch
BranchTrueFalseTotal
$feature->strand == 0 ? : 0 0 0
211 0 my $thick_start = ''; #not implemented, used for CDS
212 0 my $thick_end = ''; #not implemented, used for CDS
213 0 my $reserved = 0;
214 0 my $block_count = ''; #not implemented, used for sub features
215 0 my $block_sizes = ''; #not implemented, used for sub features
216 0 my $block_starts = ''; #not implemented, used for sub features
217  
218 0 $self->_print(join("\t",($chrom,$chrom_start,$chrom_end,$name,$score,$strand,$thick_start,$thick_end,$reserved,$block_count,$block_sizes, $block_starts))."\n");
219 0 $self->write_feature($_) foreach $feature->get_SeqFeatures();
220 }
221  
222 1 ▶sub next_feature {
Subroutine next_feature: called CC=9 SCAR=25.1
Pod documented
223 1 my $self = shift;
224 1 ▶ my $line = $self->_readline || return;
Condition50%1/2
$self->_readline || (return)
l!l
10
MC/DC0%0/1
$self->_readline || (return)
$self->_readline
Truth table50%1/2
A
A= $self->_readline
Aresult
11
00
225
226 1 my ($seq_id, $start, $end, $name, $score, $strand) = split(/\s+/, $line);
227 1 ▶ $strand ||= '+';
Condition50%1/2
$strand ||= "+"
l!l
01
MC/DC0%0/1
$strand ||= "+"
$strand
Truth table50%1/2
A
A= $strand
Aresult
11
00
228
229 1 ▶ unless (looks_like_number($start) && looks_like_number($end)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($start) and &looks_like_number($end)) 0 1 1
Condition33%1/3
&looks_like_number($start) and &looks_like_number($end)
!ll&&!rl&&r
001
MC/DC0%0/2
&looks_like_number($start) and &looks_like_number($end)
&looks_like_number($start) &looks_like_number($end)
Truth table33%1/3
A and B
A= &looks_like_number($start)B= &looks_like_number($end)
ABresult
0X0
100
111
230 # skip what is probably a header line
231 0 return $self->next_feature;
232 }
233
234 # start is 0 based, need it 1-based;
235 # end is one beyond the feature ends and thus already 1-based
236 1 ▶ my $feature = Bio::SeqFeature::Annotated->new(-start => ++$start,
Branch: 3 branches
BranchTrueFalseTotal
$score ? : 0 1 1
$strand ? : 1 0 1
$strand eq '+' ? : 1 0 1
237 -end => $end,
238 $score ? (-score => $score) : (),
239 $strand ? (-strand => $strand eq '+' ? 1 : -1) : ());
240
241 1 $feature->seq_id($seq_id);
242 1 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 1 0 1
243 1 my $sv = Bio::Annotation::SimpleValue->new(-tagname => 'Name', -value => $name);
244 1 $feature->annotation->add_Annotation($sv);
245 }
246
247 1 return $feature;
248 }
249  
250 2 1;
« FeatureIO/gff.pm ↑ Summary SeqFeature/Annotated.pm »