| 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'; | ||
| 118 | use strict; | ||
| 119 | use warnings; | ||
| 120 | |||
| 121 | use Bio::EnsEMBL::Feature; | ||
| 122 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 123 | |||
| 124 | use vars qw(@ISA); | ||
| 125 | |||
| 126 | @ISA = qw(Bio::EnsEMBL::Feature); | ||
| 127 | |||
| 128 | use constant SEQUENCE_ONTOLOGY => { | ||
| 129 | acc => 'SO:0001411', | ||
| 130 | term => 'biological_region', | ||
| 131 | }; | ||
| 132 | |||
| 133 | =head2 new | ||
| 134 | |||
| 135 | Arg [-SLICE]: Bio::EnsEMBL::SLice - Represents the sequence that this | ||
| 136 | feature is on. The coordinates of the created feature are | ||
| 137 | relative to the start of the slice. | ||
| 138 | Arg [-START]: The start coordinate of this feature relative to the start | ||
| 139 | of the slice it is sitting on. Coordinates start at 1 and | ||
| 140 | are inclusive. | ||
| 141 | Arg [-END] : The end coordinate of this feature relative to the start of | ||
| 142 | the slice it is sitting on. Coordinates start at 1 and are | ||
| 143 | inclusive. | ||
| 144 | Arg [-STRAND]: The orientation of this feature. Valid values are 1,-1,0. | ||
| 145 | Arg [-SEQNAME] : A seqname to be used instead of the default name of the | ||
| 146 | of the slice. Useful for features that do not have an | ||
| 147 | attached slice such as protein features. | ||
| 148 | Arg [-dbID] : (optional) internal database id | ||
| 149 | Arg [-ADAPTOR]: (optional) Bio::EnsEMBL::DBSQL::BaseAdaptor | ||
| 150 | Example : $feature = Bio::EnsEMBL::MiscFeature->new(-start => 1, | ||
| 151 | -end => 100, | ||
| 152 | -strand => 1, | ||
| 153 | -slice => $slice, | ||
| 154 | -analysis => $analysis); | ||
| 155 | Description: Constructs a new Bio::EnsEMBL::Feature. Generally subclasses | ||
| 156 | of this method are instantiated, rather than this class itself. | ||
| 157 | Returntype : Bio::EnsEMBL::MiscFeature | ||
| 158 | Exceptions : Thrown on invalid -SLICE, -ANALYSIS, -STRAND ,-ADAPTOR arguments | ||
| 159 | Caller : general, subclass constructors | ||
| 160 | Status : Stable | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | |||
| 165 | sub new { | ||
| 166 | my $class = shift; | ||
| 167 | |||
| 168 | my $self = $class->SUPER::new(@_); | ||
| 169 | |||
| 170 | $self->{'attributes'} = []; | ||
| 171 | |||
| 172 | return $self; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | =head2 add_Attribute | ||
| 178 | |||
| 179 | Arg [1] : Bio::EnsEMBL::Attribute $attribute | ||
| 180 | Example : $misc_feature->add_attribute($attribute); | ||
| 181 | Description: Adds an attribute to this misc. feature | ||
| 182 | Returntype : none | ||
| 183 | Exceptions : throw on wrong argument type | ||
| 184 | Caller : general | ||
| 185 | Status : Stable | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub add_Attribute { | ||
| 190 | my ($self, $attrib) = @_; | ||
| 191 | |||
| 192 | if( ! defined $attrib || ! $attrib->isa( "Bio::EnsEMBL::Attribute" )) { | ||
| 193 | throw( "You have to provide a Bio::EnsEMBL::Attribute, not a [$attrib]" ); | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->{'attributes'} ||= []; | ||
| 197 | push @{$self->{'attributes'}}, $attrib | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | =head2 add_MiscSet | ||
| 203 | |||
| 204 | Arg [1] : Bio::EnsEMBL::MiscSet $set | ||
| 205 | The set to add | ||
| 206 | Example : $misc_feature->add_MiscSet(Bio::EnsEMBL::MiscSet->new(...)); | ||
| 207 | Description: Associates this MiscFeature with a given Set. | ||
| 208 | Returntype : none | ||
| 209 | Exceptions : throw if the set arg is not provided, | ||
| 210 | throw if the set to be added does not have a code | ||
| 211 | Caller : general | ||
| 212 | Status : Stable | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | |||
| 217 | sub add_MiscSet { | ||
| 218 | my $self = shift; | ||
| 219 | my $miscSet = shift; | ||
| 220 | |||
| 221 | if(!$miscSet || !ref($miscSet) || !$miscSet->isa('Bio::EnsEMBL::MiscSet')) { | ||
| 222 | throw('Set argument must be a Bio::EnsEMBL::MiscSet'); | ||
| 223 | } | ||
| 224 | |||
| 225 | $self->{'miscSets'} ||= []; | ||
| 226 | |||
| 227 | push( @{$self->{'miscSets'}}, $miscSet ); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | =head2 get_all_MiscSets | ||
| 233 | |||
| 234 | Arg [1] : optional string $code | ||
| 235 | The code of the set to retrieve | ||
| 236 | Example : $set = $misc_feature->get_all_MiscSets($code); | ||
| 237 | Description: Retrieves a set that this feature is associated with via its | ||
| 238 | code. Can return empty lists. Usually returns about one elements lists. | ||
| 239 | Returntype : listref of Bio::EnsEMBL::MiscSet | ||
| 240 | Exceptions : throw if the code arg is not provided | ||
| 241 | Caller : general | ||
| 242 | Status : Stable | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | |||
| 247 | sub get_all_MiscSets { | ||
| 248 | my $self = shift; | ||
| 249 | my $code = shift; | ||
| 250 | |||
| 251 | $self->{'miscSets'} ||= []; | ||
| 252 | if( defined $code ) { | ||
| 253 | my @results = grep { uc($_->code())eq uc( $code ) } @{$self->{'miscSets'}}; | ||
| 254 | return \@results; | ||
| 255 | } else { | ||
| 256 | return $self->{'miscSets'}; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | =head2 get_all_Attributes | ||
| 262 | |||
| 263 | Arg [1] : optional string $code | ||
| 264 | The code of the Attribute objects to retrieve | ||
| 265 | Example : @attributes = @{ $misc_feature->get_all_Attributes('name') }; | ||
| 266 | Description: Retrieves a list of Attribute objects for given code or all | ||
| 267 | of the associated Attributes. | ||
| 268 | Returntype : listref of Bio::EnsEMBL::Attribute | ||
| 269 | Exceptions : | ||
| 270 | Caller : general | ||
| 271 | Status : Stable | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub get_all_Attributes { | ||
| 276 | my $self = shift; | ||
| 277 | my $code = shift; | ||
| 278 | |||
| 279 | my @results; | ||
| 280 | my $result; | ||
| 281 | |||
| 282 | if( defined $code ) { | ||
| 283 | @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}}; | ||
| 284 | return \@results; | ||
| 285 | } else { | ||
| 286 | return $self->{'attributes'}; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | =head2 get_all_attribute_values | ||
| 291 | |||
| 292 | Arg [1] : string $code | ||
| 293 | The code of the Attribute object values to retrieve | ||
| 294 | Example : @attributes_vals = @{$misc_feature->get_all_attribute_values('name')}; | ||
| 295 | Description: Retrieves a list of Attribute object values for given code or all | ||
| 296 | of the associated Attributes. | ||
| 297 | Returntype : listref of values | ||
| 298 | Exceptions : | ||
| 299 | Caller : general | ||
| 300 | Status : Stable | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | sub get_all_attribute_values { | ||
| 305 | my $self = shift; | ||
| 306 | my $code = shift; | ||
| 307 | my @results = map { uc( $_->code() ) eq uc( $code ) ? $_->value : () } | ||
| 308 | @{$self->{'attributes'}}; | ||
| 309 | return \@results; | ||
| 310 | } | ||
| 311 | |||
| 312 | =head2 get_scalar_attribute | ||
| 313 | |||
| 314 | Arg [1] : string $code | ||
| 315 | The code of the Attribute object values to retrieve | ||
| 316 | Example : $vals = $misc_feature->get_scalar_attribute('name'); | ||
| 317 | Description: Retrieves a value for given code or all | ||
| 318 | of the associated Attributes. | ||
| 319 | Returntype : scalar value | ||
| 320 | Exceptions : | ||
| 321 | Caller : general | ||
| 322 | Status : Stable | ||
| 323 | |||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | |||
| 328 | sub get_scalar_attribute { | ||
| 329 | my $self = shift; | ||
| 330 | my $code = shift; | ||
| 331 | my @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}}; | ||
| 332 | return @results ? $results[0]->value() : ''; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub get_first_scalar_attribute { | ||
| 336 | my $self = shift; | ||
| 337 | foreach my $code ( @_ ) { | ||
| 338 | my @results = grep { uc( $_->code() ) eq uc( $code )} @{$self->{'attributes'}}; | ||
| 339 | return $results[0]->value() if @results; | ||
| 340 | } | ||
| 341 | return ''; | ||
| 342 | } | ||
| 343 | =head2 display_id | ||
| 344 | |||
| 345 | Arg [1] : none | ||
| 346 | Example : print $kb->display_id(); | ||
| 347 | Description: This method returns a string that is considered to be | ||
| 348 | the 'display' identifier. For misc_features this is the first | ||
| 349 | name or synonym attribute or '' if neither are defined. | ||
| 350 | Returntype : string | ||
| 351 | Exceptions : none | ||
| 352 | Caller : web drawing code | ||
| 353 | Status : Stable | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub display_id { | ||
| 358 | my $self = shift; | ||
| 359 | my ($attrib) = @{$self->get_all_Attributes('name')}; | ||
| 360 | ($attrib) = @{$self->get_all_Attributes('synonym')} if(!$attrib); | ||
| 361 | if( defined $attrib ) { | ||
| 362 | return $attrib->value(); | ||
| 363 | } else { | ||
| 364 | return ''; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | |||
| 369 | =head2 summary_as_hash | ||
| 370 | |||
| 371 | Example : my $hash = $misc_feature->summary_as_hash(); | ||
| 372 | Description: Generates a HashRef compatible with GFFSerializer. Adds | ||
| 373 | all attribute key value pairs plus MiscSet codes and names | ||
| 374 | Returntype : Hash | ||
| 375 | Exceptions : none | ||
| 376 | Caller : general | ||
| 377 | Status : Stable | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | sub summary_as_hash { | ||
| 382 | my ($self) = @_; | ||
| 383 | my $hash = $self->SUPER::summary_as_hash(); | ||
| 384 | my $attributes = $self->get_all_Attributes(); | ||
| 385 | foreach my $attr (@{$attributes}) { | ||
| 386 | $hash->{$attr->code()} = $attr->value(); | ||
| 387 | } | ||
| 388 | my $misc_sets = $self->get_all_MiscSets(); | ||
| 389 | foreach my $set (@{$misc_sets}) { | ||
| 390 | push(@{$hash->{misc_set_code}},$set->code()); | ||
| 391 | push(@{$hash->{misc_set_name}},$set->name()); | ||
| 392 | } | ||
| 393 | return $hash; | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | 1; |