EnsEMBL/MiscFeature.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 157 CC 10 SCAR 16.9
CC
10
Cov
0%
display_id
12.0
add_Attribute
6.0
add_MiscSet
6.0
CRAP 5.4
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::MiscFeature - A miscelaneous feature with arbitrary features and
34 associations.
35  
36 =head1 SYNOPSIS
37  
38 use Bio::EnsEMBL::MiscFeature;
39 use Bio::EnsEMBL::MiscSet;
40 use Bio::EnsEMBL::Attribute;
41  
42 my $mfeat = Bio::EnsEMBL::MiscFeature->new(
43 -START => 1200,
44 -END => 100_000,
45 -STRAND => 1,
46 -SLICE => $slice
47 );
48  
49 # Can add attributes to the misc feature and associate with various
50 # sets
51 my $clone_set = Bio::EnsEMBL::MiscSet->new(
52 -CODE => 'clone',
53 -NAME => '1MB clone set',
54 -DESCRIPTION => '1MB CloneSet'
55 );
56  
57 my $tiling_path_set = Bio::EnsEMBL::MiscSet->new(
58 -CODE => 'tilingpath',
59 -NAME => 'tiling path set'
60 );
61  
62 my $attrib1 = Bio::EnsEMBL::Attribute->new(
63 -VALUE => 'RLX12451',
64 -CODE => 'name',
65 -NAME => 'name'
66 );
67  
68 my $attrib2 = Bio::EnsEMBL::Attribute->new(
69 -VALUE => '4',
70 -CODE => 'version',
71 -NAME => 'version'
72 );
73  
74 my $attrib3 = Bio::EnsEMBL::Attribute->new(
75 -VALUE => 'AL42131.4',
76 -CODE => 'synonym',
77 -NAME => 'synonym'
78 );
79  
80 # can associate a misc feature with any number of sets
81  
82 $mfeat->add_MiscSet($clone_set);
83 $mfeat->add_MiscSet($tiling_path_set);
84  
85 # can add arbitrary attributes to a misc feature
86  
87 $mfeat->add_Attribute($attrib1);
88 $mfeat->add_Attribute($attrib2);
89 $mfeat->add_Attribute($attrib3);
90  
91 my ($name_attrib) = @{ $mfeat->get_all_Attributes('name') };
92 my @all_attribs = @{ $mfeat->get_all_Attributes() };
93  
94 my @all_sets = @{ $mfeat->get_all_MiscSets() };
95 my ($clone_set) = @{ $mfeat->get_all_CloneSets('clone') };
96  
97  
98 # Can do normal feature operations as well
99 $mfeat = $mfeat->transform('supercontig');
100 print $mfeat->slice->seq_region_name, ' ', $mfeat->start, '-',
101 $mfeat->end;
102  
103  
104 =head1 DESCRIPTION
105  
106 MiscFeatures are extremely general features with a location, and an
107 arbitrary group of attributes. They are grouped with other features of
108 the same 'type' through the use of MiscSets (see Bio::EnsEMBL::MiscSet).
109 Attributes are attached in the fom of Bio::EnsEMBL::Attribute objects.
110 See Bio::EnsEMBL::DBSQL::MiscFeatureAdaptor for ways to fetch or store
111 MiscFeatures.
112  
113 =cut
114  
115  
116 package Bio::EnsEMBL::MiscFeature;
117 $Bio::EnsEMBL::MiscFeature::VERSION = '114.0.0_50'; # TRIAL
118 $Bio::EnsEMBL::MiscFeature::VERSION = '114.0.050';
119 use strict;
120 use warnings;
121  
122 use Bio::EnsEMBL::Feature;
123 use Bio::EnsEMBL::Utils::Exception qw(throw);
124  
125 use vars qw(@ISA);
126  
127 @ISA = qw(Bio::EnsEMBL::Feature);
128  
129 use constant SEQUENCE_ONTOLOGY => {
130 acc => 'SO:0001411',
131 term => 'biological_region',
132 };
133  
134 =head2 new
135  
136 Arg [-SLICE]: Bio::EnsEMBL::SLice - Represents the sequence that this
137 feature is on. The coordinates of the created feature are
138 relative to the start of the slice.
139 Arg [-START]: The start coordinate of this feature relative to the start
140 of the slice it is sitting on. Coordinates start at 1 and
141 are inclusive.
142 Arg [-END] : The end coordinate of this feature relative to the start of
143 the slice it is sitting on. Coordinates start at 1 and are
144 inclusive.
145 Arg [-STRAND]: The orientation of this feature. Valid values are 1,-1,0.
146 Arg [-SEQNAME] : A seqname to be used instead of the default name of the
147 of the slice. Useful for features that do not have an
148 attached slice such as protein features.
149 Arg [-dbID] : (optional) internal database id
150 Arg [-ADAPTOR]: (optional) Bio::EnsEMBL::DBSQL::BaseAdaptor
151 Example : $feature = Bio::EnsEMBL::MiscFeature->new(-start => 1,
152 -end => 100,
153 -strand => 1,
154 -slice => $slice,
155 -analysis => $analysis);
156 Description: Constructs a new Bio::EnsEMBL::Feature. Generally subclasses
157 of this method are instantiated, rather than this class itself.
158 Returntype : Bio::EnsEMBL::MiscFeature
159 Exceptions : Thrown on invalid -SLICE, -ANALYSIS, -STRAND ,-ADAPTOR arguments
160 Caller : general, subclass constructors
161 Status : Stable
162  
163 =cut
164  
165  
166 sub new {
167 my $class = shift;
168  
169 my $self = $class->SUPER::new(@_);
170  
171 $self->{'attributes'} = [];
172  
173 return $self;
174 }
175  
176  
177  
178 =head2 add_Attribute
179  
180 Arg [1] : Bio::EnsEMBL::Attribute $attribute
181 Example : $misc_feature->add_attribute($attribute);
182 Description: Adds an attribute to this misc. feature
183 Returntype : none
184 Exceptions : throw on wrong argument type
185 Caller : general
186 Status : Stable
187  
188 =cut
189  
190 sub add_Attribute {
191 my ($self, $attrib) = @_;
192  
193 if( ! defined $attrib || ! $attrib->isa( "Bio::EnsEMBL::Attribute" )) {
194 throw( "You have to provide a Bio::EnsEMBL::Attribute, not a [$attrib]" );
195 }
196  
197 $self->{'attributes'} ||= [];
198 push @{$self->{'attributes'}}, $attrib
199 }
200  
201  
202  
203 =head2 add_MiscSet
204  
205 Arg [1] : Bio::EnsEMBL::MiscSet $set
206 The set to add
207 Example : $misc_feature->add_MiscSet(Bio::EnsEMBL::MiscSet->new(...));
208 Description: Associates this MiscFeature with a given Set.
209 Returntype : none
210 Exceptions : throw if the set arg is not provided,
211 throw if the set to be added does not have a code
212 Caller : general
213 Status : Stable
214  
215 =cut
216  
217  
218 sub add_MiscSet {
219 my $self = shift;
220 my $miscSet = shift;
221  
222 if(!$miscSet || !ref($miscSet) || !$miscSet->isa('Bio::EnsEMBL::MiscSet')) {
223 throw('Set argument must be a Bio::EnsEMBL::MiscSet');
224 }
225  
226 $self->{'miscSets'} ||= [];
227  
228 push( @{$self->{'miscSets'}}, $miscSet );
229 }
230  
231  
232  
233 =head2 get_all_MiscSets
234  
235 Arg [1] : optional string $code
236 The code of the set to retrieve
237 Example : $set = $misc_feature->get_all_MiscSets($code);
238 Description: Retrieves a set that this feature is associated with via its
239 code. Can return empty lists. Usually returns about one elements lists.
240 Returntype : listref of Bio::EnsEMBL::MiscSet
241 Exceptions : throw if the code arg is not provided
242 Caller : general
243 Status : Stable
244  
245 =cut
246  
247  
248 sub get_all_MiscSets {
249 my $self = shift;
250 my $code = shift;
251  
252 $self->{'miscSets'} ||= [];
253 if( defined $code ) {
254 my @results = grep { uc($_->code())eq uc( $code ) } @{$self->{'miscSets'}};
255 return \@results;
256 } else {
257 return $self->{'miscSets'};
258 }
259 }
260  
261  
262 =head2 get_all_Attributes
263  
264 Arg [1] : optional string $code
265 The code of the Attribute objects to retrieve
266 Example : @attributes = @{ $misc_feature->get_all_Attributes('name') };
267 Description: Retrieves a list of Attribute objects for given code or all
268 of the associated Attributes.
269 Returntype : listref of Bio::EnsEMBL::Attribute
270 Exceptions :
271 Caller : general
272 Status : Stable
273  
274 =cut
275  
276 sub get_all_Attributes {
277 my $self = shift;
278 my $code = shift;
279  
280 my @results;
281 my $result;
282  
283 if( defined $code ) {
284 @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}};
285 return \@results;
286 } else {
287 return $self->{'attributes'};
288 }
289 }
290  
291 =head2 get_all_attribute_values
292  
293 Arg [1] : string $code
294 The code of the Attribute object values to retrieve
295 Example : @attributes_vals = @{$misc_feature->get_all_attribute_values('name')};
296 Description: Retrieves a list of Attribute object values for given code or all
297 of the associated Attributes.
298 Returntype : listref of values
299 Exceptions :
300 Caller : general
301 Status : Stable
302  
303 =cut
304  
305 sub get_all_attribute_values {
306 my $self = shift;
307 my $code = shift;
308 my @results = map { uc( $_->code() ) eq uc( $code ) ? $_->value : () }
309 @{$self->{'attributes'}};
310 return \@results;
311 }
312  
313 =head2 get_scalar_attribute
314  
315 Arg [1] : string $code
316 The code of the Attribute object values to retrieve
317 Example : $vals = $misc_feature->get_scalar_attribute('name');
318 Description: Retrieves a value for given code or all
319 of the associated Attributes.
320 Returntype : scalar value
321 Exceptions :
322 Caller : general
323 Status : Stable
324  
325  
326 =cut
327
328  
329 sub get_scalar_attribute {
330 my $self = shift;
331 my $code = shift;
332 my @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}};
333 return @results ? $results[0]->value() : '';
334 }
335  
336 sub get_first_scalar_attribute {
337 my $self = shift;
338 foreach my $code ( @_ ) {
339 my @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}};
340 return $results[0]->value() if @results;
341 }
342 return '';
343 }
344 =head2 display_id
345  
346 Arg [1] : none
347 Example : print $kb->display_id();
348 Description: This method returns a string that is considered to be
349 the 'display' identifier. For misc_features this is the first
350 name or synonym attribute or '' if neither are defined.
351 Returntype : string
352 Exceptions : none
353 Caller : web drawing code
354 Status : Stable
355  
356 =cut
357  
358 sub display_id {
359 my $self = shift;
360 my ($attrib) = @{$self->get_all_Attributes('name')};
361 ($attrib) = @{$self->get_all_Attributes('synonym')} if(!$attrib);
362 if( defined $attrib ) {
363 return $attrib->value();
364 } else {
365 return '';
366 }
367 }
368  
369  
370 =head2 summary_as_hash
371  
372 Example : my $hash = $misc_feature->summary_as_hash();
373 Description: Generates a HashRef compatible with GFFSerializer. Adds
374 all attribute key value pairs plus MiscSet codes and names
375 Returntype : Hash
376 Exceptions : none
377 Caller : general
378 Status : Stable
379  
380 =cut
381  
382 sub summary_as_hash {
383 my ($self) = @_;
384 my $hash = $self->SUPER::summary_as_hash();
385 my $attributes = $self->get_all_Attributes();
386 foreach my $attr (@{$attributes}) {
387 $hash->{$attr->code()} = $attr->value();
388 }
389 my $misc_sets = $self->get_all_MiscSets();
390 foreach my $set (@{$misc_sets}) {
391 push(@{$hash->{misc_set_code}},$set->code());
392 push(@{$hash->{misc_set_name}},$set->name());
393 }
394 return $hash;
395 }
396  
397  
398 1;