| 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:SeqEdit - A class representing a post transcriptional edit to a | ||
| 34 | sequence. | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | use Bio::EnsEMBL::SeqEdit; | ||
| 39 | use Bio::EnsEMBL::Attribute; | ||
| 40 | |||
| 41 | # construct a SeqEdit object using a Transcript attribute | ||
| 42 | |||
| 43 | ($attribute) = @{ $translation->get_all_Attributes('_rna_edit') }; | ||
| 44 | |||
| 45 | $seq_edit = Bio::EnsEMBL::SeqEdit( -ATTRIB => $attribute ); | ||
| 46 | |||
| 47 | print $seq_edit->start(), "\n"; | ||
| 48 | print $seq_edit->end(), "\n"; | ||
| 49 | print $seq_edit->alt_seq(), "\n"; | ||
| 50 | |||
| 51 | # apply the edit to some sequence | ||
| 52 | $seq = $transcript->spliced_seq(); | ||
| 53 | print "Before modifiction: $seq\n"; | ||
| 54 | |||
| 55 | $seq_edit->apply_edit( \$seq ); | ||
| 56 | print "After modification: $seq\n"; | ||
| 57 | |||
| 58 | # construct an attribute object from a SeqEdit and add it to a | ||
| 59 | # translation | ||
| 60 | |||
| 61 | $seq_edit = Bio::EnsEMBL::SeqEdit->new( | ||
| 62 | -CODE => '_selenocysteine', | ||
| 63 | -NAME => 'Selenocysteine', | ||
| 64 | -DESC => 'Selenocysteine', | ||
| 65 | -START => 10, | ||
| 66 | -END => 10, | ||
| 67 | -ALT_SEQ => 'U' | ||
| 68 | ); | ||
| 69 | |||
| 70 | $attribute = $seq_edit->get_Attribute(); | ||
| 71 | $translation->add_Attributes($attribute); | ||
| 72 | |||
| 73 | =head1 DESCRIPTION | ||
| 74 | |||
| 75 | This is a class used to represent post transcriptional | ||
| 76 | modifications to sequences. SeqEdit objects are stored as ordinary | ||
| 77 | Bio::EnsEMBL::Attributes with a parseable value and can be used to | ||
| 78 | represent RNA editing, selenocysteines etc. | ||
| 79 | |||
| 80 | Also see B<Bio::EnsEMBL::Attribute> | ||
| 81 | |||
| 82 | =head1 METHODS | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | package Bio::EnsEMBL::SeqEdit; | ||
| 87 | $Bio::EnsEMBL::SeqEdit::VERSION = '114.0.0_50'; # TRIAL | ||
| 88 | $Bio::EnsEMBL::SeqEdit::VERSION = '114.0.050'; | ||
| 89 | use strict; | ||
| 90 | use warnings; | ||
| 91 | |||
| 92 | use Bio::EnsEMBL::Attribute; | ||
| 93 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 94 | use Bio::EnsEMBL::Utils::Exception qw(throw); | ||
| 95 | |||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | Arg [-ATTRIB] : Bio::EnsEMBL::Attribute | ||
| 100 | Constructs a new SeqEdit from an Attribute. | ||
| 101 | Can only be provided if no other constructor arguments | ||
| 102 | are provided. | ||
| 103 | Arg [-START] : The start position of the edit. | ||
| 104 | Arg [-END] : The end position of the edit. | ||
| 105 | Arg [-ALT_SEQ] : The alternate sequence | ||
| 106 | Arg [-CODE] : A code for this SeqEdit | ||
| 107 | Arg [-NAME] : A name for this SeqEdit | ||
| 108 | Arg [-DESCRIPTION] : Arg passed to superclass constructor | ||
| 109 | Example : my $sea = Bio::EnsEMBL::SeqEdit->new(-ATTRIB => $attrib); | ||
| 110 | my $sea = Bio::EnsEMBL::SeqEdit->new | ||
| 111 | (-START => 10, | ||
| 112 | -END => 12, | ||
| 113 | -ALT_SEQ => 'ACG', | ||
| 114 | -CODE => '_rna_edit', | ||
| 115 | -NAME => 'RNA Edit', | ||
| 116 | -DESCRIPTION => 'RNA edit'); | ||
| 117 | Description: Constructs a SeqEdit representing a single edit to a | ||
| 118 | sequence, such as an rna modification or a selenocysteine. | ||
| 119 | Returntype : Bio::EnsEMBL::SeqEdit | ||
| 120 | Exceptions : throws if attribute set and other args aswell | ||
| 121 | throws if start and end not set correctly of attribure not set | ||
| 122 | Caller : general | ||
| 123 | Status : Stable | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub new { | ||
| 128 | my $class = shift; | ||
| 129 | |||
| 130 | my ($attrib, $start, $end, $alt_seq, $name, $desc, $code) = | ||
| 131 | rearrange([qw(ATTRIB START END ALT_SEQ NAME DESCRIPTION CODE)], @_); | ||
| 132 | |||
| 133 | my $self; | ||
| 134 | |||
| 135 | if($attrib) { | ||
| 136 | if(defined($start) || defined($end) || defined($alt_seq) || | ||
| 137 | defined($name) || defined($desc) || defined($code)) { | ||
| 138 | throw("Cannot specify -ATTRIB argument with additional arguments."); | ||
| 139 | } | ||
| 140 | |||
| 141 | if(!ref($attrib) || !$attrib->isa('Bio::EnsEMBL::Attribute')) { | ||
| 142 | throw('Bio::EnsEMBL::Attribute argument expected.'); | ||
| 143 | } | ||
| 144 | |||
| 145 | ($start, $end, $alt_seq) = split(/\s+/, $attrib->value()); | ||
| 146 | |||
| 147 | if($start !~ /\d+/ || $end !~ /\d+/) { | ||
| 148 | throw('Could not parse value of attribute: '.$attrib->value()); | ||
| 149 | } | ||
| 150 | |||
| 151 | $name = $attrib->name(); | ||
| 152 | $code = $attrib->code(); | ||
| 153 | $desc = $attrib->description(); | ||
| 154 | |||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | if(defined($end) && defined($start) && $start > $end+1) { | ||
| 159 | throw("start must be less than or equal to end + 1"); | ||
| 160 | } | ||
| 161 | |||
| 162 | if(defined($start) && $start < 1) { | ||
| 163 | throw("start must be greater than or equal to 1"); | ||
| 164 | } | ||
| 165 | |||
| 166 | if(defined($end) && $end < 0) { | ||
| 167 | throw("end must be greater than or equal to 0"); | ||
| 168 | } | ||
| 169 | |||
| 170 | $alt_seq ||= ''; | ||
| 171 | |||
| 172 | return bless {'start' => $start, | ||
| 173 | 'end' => $end, | ||
| 174 | 'alt_seq' => $alt_seq, | ||
| 175 | 'description' => $desc, | ||
| 176 | 'name' => $name, | ||
| 177 | 'code' => $code}, $class; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | =head2 start | ||
| 183 | |||
| 184 | Arg [1] : (optional) int $start - the new start position | ||
| 185 | Example : $start = $se_attrib->start(); | ||
| 186 | Description: Getter/Setter for the start position of the region replaced | ||
| 187 | by the alt_seq. | ||
| 188 | |||
| 189 | Coordinates are inclusive and one-based, which means that | ||
| 190 | inserts are unusually represented by a start 1bp higher than | ||
| 191 | the end. | ||
| 192 | |||
| 193 | E.g. start = 1, end = 1 is a replacement of the first base but | ||
| 194 | start = 1, end = 0 is an insert BEFORE the first base. | ||
| 195 | Returntype : int | ||
| 196 | Exceptions : none | ||
| 197 | Caller : Transcript, Translation | ||
| 198 | Status : Stable | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub start { | ||
| 203 | my $self = shift; | ||
| 204 | |||
| 205 | if(@_) { | ||
| 206 | my $start = shift; | ||
| 207 | if(defined($start) && $start < 1) { | ||
| 208 | throw("start must be greater than or equal to 1"); | ||
| 209 | } | ||
| 210 | $self->{'start'} = $start; | ||
| 211 | } | ||
| 212 | |||
| 213 | return $self->{'start'}; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | =head2 end | ||
| 218 | |||
| 219 | Arg [1] : (optional) int $end - the new end position | ||
| 220 | Example : $end = $se_attrib->end(); | ||
| 221 | Description: Getter/Setter for the end position of the region replaced | ||
| 222 | by the alt_seq. | ||
| 223 | |||
| 224 | Coordinates are inclusive and one-based, which means that | ||
| 225 | inserts are unusually represented by a start 1bp higher than | ||
| 226 | the end. | ||
| 227 | |||
| 228 | E.g. start = 1, end = 1 is a replacement of the first base but | ||
| 229 | start = 1, end = 0 is an insert BEFORE the first base. | ||
| 230 | Returntype : int | ||
| 231 | Exceptions : throws if end <= 0 | ||
| 232 | Caller : Transcript, Translation | ||
| 233 | Status : Stable | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub end { | ||
| 238 | my $self = shift; | ||
| 239 | |||
| 240 | if(@_) { | ||
| 241 | my $end = shift; | ||
| 242 | if(defined($end) && $end < 0) { | ||
| 243 | throw("end must be greater than or equal to 0"); | ||
| 244 | } | ||
| 245 | $self->{'end'} = $end; | ||
| 246 | } | ||
| 247 | |||
| 248 | return $self->{'end'}; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | =head2 alt_seq | ||
| 253 | |||
| 254 | Arg [1] : (optional) string $alt_seq | ||
| 255 | Example : my $alt_seq = $se_attrib->alt_seq(); | ||
| 256 | Description: Getter/Setter for the replacement sequence used by this edit. | ||
| 257 | The sequence may either be a string of amino acids or | ||
| 258 | nucleotides depending on the context in which this edit is | ||
| 259 | used. | ||
| 260 | |||
| 261 | In the case of a deletion the replacement sequence is an empty | ||
| 262 | string. | ||
| 263 | Returntype : string | ||
| 264 | Exceptions : none | ||
| 265 | Caller : Transcript, Translation | ||
| 266 | Status : Stable | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub alt_seq { | ||
| 271 | my $self = shift; | ||
| 272 | $self->{'alt_seq'} = shift || '' if(@_); | ||
| 273 | return $self->{'alt_seq'}; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | =head2 length_diff | ||
| 278 | |||
| 279 | Arg [1] : none | ||
| 280 | Example : my $diff = $sea->length_diff(); | ||
| 281 | Description: Returns the difference in length caused by applying this | ||
| 282 | edit to a sequence. This may be be negative (deletion), | ||
| 283 | positive (insertion) or 0 (replacement). | ||
| 284 | |||
| 285 | If either start or end are not defined 0 is returned. | ||
| 286 | Returntype : int | ||
| 287 | Exceptions : none | ||
| 288 | Caller : general | ||
| 289 | Status : Stable | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub length_diff { | ||
| 294 | my $self = shift; | ||
| 295 | |||
| 296 | return 0 if(!defined($self->{'end'}) || !defined($self->{'start'})); | ||
| 297 | |||
| 298 | return length($self->{'alt_seq'}) - ($self->{'end'} - $self->{'start'} + 1); | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | |||
| 303 | =head2 name | ||
| 304 | |||
| 305 | Arg [1] : (optional) string $name | ||
| 306 | Example : my $name = $seqedit->name(); | ||
| 307 | Description: Getter/Setter for the name of this SeqEdit | ||
| 308 | Returntype : string | ||
| 309 | Exceptions : none | ||
| 310 | Caller : general | ||
| 311 | Status : Stable | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub name { | ||
| 316 | my $self = shift; | ||
| 317 | $self->{'name'} = shift if(@_); | ||
| 318 | return $self->{'name'}; | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | |||
| 324 | =head2 code | ||
| 325 | |||
| 326 | Arg [1] : (optional) string $code | ||
| 327 | Example : my $code = $seqedit->code(); | ||
| 328 | Description: Getter/Setter for the code of this SeqEdit | ||
| 329 | Returntype : string | ||
| 330 | Exceptions : none | ||
| 331 | Caller : general | ||
| 332 | Status : Stable | ||
| 333 | |||
| 334 | =cut | ||
| 335 | |||
| 336 | sub code { | ||
| 337 | my $self = shift; | ||
| 338 | $self->{'code'} = shift if(@_); | ||
| 339 | return $self->{'code'}; | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | =head2 description | ||
| 345 | |||
| 346 | Arg [1] : (optional) string $desc | ||
| 347 | Example : my $desc = $seqedit->description(); | ||
| 348 | Description: Getter/Setter for the description of this SeqEdit | ||
| 349 | Returntype : string | ||
| 350 | Exceptions : none | ||
| 351 | Caller : general | ||
| 352 | Status : Stable | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | sub description { | ||
| 357 | my $self = shift; | ||
| 358 | $self->{'description'} = shift if(@_); | ||
| 359 | return $self->{'description'}; | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | =head2 get_Attribute | ||
| 365 | |||
| 366 | Arg [1] : none | ||
| 367 | Example : my $attrib = $seqedit->get_Attribute(); | ||
| 368 | $transcript->add_Attributes($attrib); | ||
| 369 | Description: Converts a SeqEdit object into an Attribute object. This | ||
| 370 | allows the SeqEdit to be stored as any other attribute in the | ||
| 371 | ensembl database. The start/end and alt_seq properties | ||
| 372 | should be set before calling this method. | ||
| 373 | Returntype : Bio::EnsEMBL::Attribute | ||
| 374 | Exceptions : warning if start/end or alt_seq properties are not defined | ||
| 375 | Caller : general | ||
| 376 | Status : Stable | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | sub get_Attribute { | ||
| 381 | my $self = shift; | ||
| 382 | |||
| 383 | my $start = $self->start(); | ||
| 384 | my $end = $self->end(); | ||
| 385 | my $alt_seq = $self->alt_seq(); | ||
| 386 | |||
| 387 | my $value; | ||
| 388 | |||
| 389 | if(defined($start) && defined($end) && defined($alt_seq)) { | ||
| 390 | $value = join(' ', $start, $end, $alt_seq); | ||
| 391 | } else { | ||
| 392 | warning('Attribute value cannot be created unless start, end and alt_seq' . | ||
| 393 | 'properties are defined'); | ||
| 394 | $value = ''; | ||
| 395 | } | ||
| 396 | |||
| 397 | return Bio::EnsEMBL::Attribute->new(-CODE => $self->code(), | ||
| 398 | -VALUE => $value, | ||
| 399 | -NAME => $self->name(), | ||
| 400 | -DESCRIPTION => $self->description()); | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | =head2 apply_edit | ||
| 405 | |||
| 406 | Arg [1] : reference to string $seqref | ||
| 407 | Example : $sequence = 'ACTGAATATTTAAGGCA'; | ||
| 408 | $seqedit->apply_edit(\$sequence); | ||
| 409 | print $sequence, "\n"; | ||
| 410 | Description: Applies this edit directly to a sequence which is | ||
| 411 | passed by reference. The coordinates of this SeqEdit | ||
| 412 | are assumed to be relative to the start of the sequence | ||
| 413 | argument. | ||
| 414 | If either the start or end of this SeqEdit are not defined | ||
| 415 | this function will not do anything to the passed sequence. | ||
| 416 | Returntype : reference to the same sequence that was passed in | ||
| 417 | Exceptions : none | ||
| 418 | Caller : Transcript, Translation | ||
| 419 | Status : Stable | ||
| 420 | |||
| 421 | =cut | ||
| 422 | |||
| 423 | sub apply_edit { | ||
| 424 | my $self = shift; | ||
| 425 | my $seqref = shift; | ||
| 426 | |||
| 427 | if(ref($seqref) ne 'SCALAR') { | ||
| 428 | throw("Reference to scalar argument expected"); | ||
| 429 | } | ||
| 430 | |||
| 431 | if(!defined($self->{'start'}) || !defined($self->{'end'})) { | ||
| 432 | return $seqref; | ||
| 433 | } | ||
| 434 | |||
| 435 | my $len = $self->{'end'} - $self->{'start'} + 1; | ||
| 436 | substr($$seqref, $self->{'start'} - 1, $len) = $self->{'alt_seq'}; | ||
| 437 | |||
| 438 | return $seqref; | ||
| 439 | } | ||
| 440 | |||
| 441 | |||
| 442 | 1; |