File Coverage

blib/lib/Bio/FeatureIO/bed.pm
Criterion Covered Total %
statement 42 75 56.0
branch 9 36 25.0
condition 6 21 28.5
subroutine 12 13 92.3
pod 5 5 100.0
total 74 150 49.3


line stmt bran cond sub pod time code
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.
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
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             BEGIN {
77 1     1   532 $Bio::FeatureIO::bed::AUTHORITY = 'cpan:BIOPERLML';
78             }
79             $Bio::FeatureIO::bed::VERSION = '1.6.905';
80 1     1   7 use strict;
  1         2  
  1         23  
81 1     1   4 use base qw(Bio::FeatureIO);
  1         2  
  1         95  
82 1     1   338 use Bio::SeqFeature::Annotated;
  1         4  
  1         36  
83 1     1   6 use Bio::Annotation::SimpleValue;
  1         2  
  1         20  
84 1     1   5 use Bio::OntologyIO;
  1         2  
  1         21  
85 1     1   5 use Scalar::Util qw(looks_like_number);
  1         2  
  1         673  
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             sub _initialize {
108 1     1   3 my($self,%arg) = @_;
109              
110 1         8 $self->SUPER::_initialize(%arg);
111              
112 1   50     279 $self->name($arg{-name} || scalar(localtime()));
113 1   50     17 $self->description($arg{-description} || scalar(localtime()));
114 1   50     6 $self->use_score($arg{-use_score} || 0);
115              
116 1 0       8 $self->_print(sprintf('track name="%s" description="%s" useScore=%d',
    50          
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             sub use_score{
136 1     1 1 2 my $self = shift;
137              
138 1 50       5 return $self->{'use_score'} = shift if @_;
139 0         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             sub name{
155 1     1 1 3 my $self = shift;
156              
157 1 50       5 return $self->{'name'} = shift if @_;
158 0         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             sub description{
174 1     1 1 2 my $self = shift;
175              
176 1 50       7 return $self->{'description'} = shift if @_;
177 0         0 return $self->{'description'};
178             }
179              
180              
181             sub write_feature {
182 0     0 1 0 my($self,$feature) = @_;
183 0 0       0 $self->throw("only Bio::SeqFeature::Annotated objects are writeable") unless $feature->isa('Bio::SeqFeature::Annotated');
184              
185 0   0     0 my $chrom = $feature->seq_id || '';
186 0   0     0 my $chrom_start = $feature->start || 0; # output start is supposed to be 0-based
187 0   0     0 my $chrom_end = ($feature->end + 1) || 1; # output end is supposed to not be part of the feature
188              
189             #try to make a reasonable name
190 0         0 my $name = undef;
191 0         0 my @v;
192 0 0       0 if (@v = ($feature->annotation->get_Annotations('Name'))){
    0          
193 0         0 $name = $v[0];
194 0 0       0 $self->warn("only using first of feature's multiple names: ".join ',', map {$_->value} @v) if scalar(@v) > 1;
  0         0  
195             } elsif (@v = ($feature->annotation->get_Annotations('ID'))){
196 0         0 $name = $v[0];
197 0 0       0 $self->warn("only using first of feature's multiple IDs: ".join ',', map {$_->value} @v) if scalar(@v) > 1;
  0         0  
198             } else {
199 0         0 $name = 'anonymous';
200             }
201            
202 0 0       0 if (ref($name)) {
203 0         0 $name = $name->value;
204             }
205 0 0       0 if (ref($chrom)) {
206 0         0 $chrom = $chrom->value;
207             }
208              
209 0   0     0 my $score = $feature->score || 0;
210 0 0       0 my $strand = $feature->strand == 0 ? '-' : '+'; #default to +
211 0         0 my $thick_start = ''; #not implemented, used for CDS
212 0         0 my $thick_end = ''; #not implemented, used for CDS
213 0         0 my $reserved = 0;
214 0         0 my $block_count = ''; #not implemented, used for sub features
215 0         0 my $block_sizes = ''; #not implemented, used for sub features
216 0         0 my $block_starts = ''; #not implemented, used for sub features
217              
218 0         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         0 $self->write_feature($_) foreach $feature->get_SeqFeatures();
220             }
221              
222             sub next_feature {
223 1     1 1 3 my $self = shift;
224 1   50     7 my $line = $self->_readline || return;
225            
226 1         49 my ($seq_id, $start, $end, $name, $score, $strand) = split(/\s+/, $line);
227 1   50     10 $strand ||= '+';
228            
229 1 50 33     8 unless (looks_like_number($start) && looks_like_number($end)) {
230             # skip what is probably a header line
231 0         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 50       12 my $feature = Bio::SeqFeature::Annotated->new(-start => ++$start,
    50          
    50          
237             -end => $end,
238             $score ? (-score => $score) : (),
239             $strand ? (-strand => $strand eq '+' ? 1 : -1) : ());
240            
241 1         4 $feature->seq_id($seq_id);
242 1 50       9 if ($name) {
243 1         5 my $sv = Bio::Annotation::SimpleValue->new(-tagname => 'Name', -value => $name);
244 1         66 $feature->annotation->add_Annotation($sv);
245             }
246            
247 1         53 return $feature;
248             }
249              
250             1;