| 1 | # $Id: PrimarySeqI.pm,v 1.26.2.1 2001/03/02 22:47:54 heikki Exp $ | ||
| 2 | # | ||
| 3 | # BioPerl module for FAST::Bio::PrimarySeqI | ||
| 4 | # | ||
| 5 | # Cared for by Ewan Birney <birney@sanger.ac.uk> | ||
| 6 | # | ||
| 7 | # Copyright Ewan Birney | ||
| 8 | # | ||
| 9 | # You may distribute this module under the same terms as perl itself | ||
| 10 | |||
| 11 | # POD documentation - main docs before the code | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | FAST::Bio::PrimarySeqI - Interface definition for a FAST::Bio::PrimarySeq | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | # get a FAST::Bio::PrimarySeqI compliant object somehow | ||
| 20 | |||
| 21 | # to test this is a seq object | ||
| 22 | |||
| 23 | $obj->isa("FAST::Bio::PrimarySeqI") || $obj->throw("$obj does not implement the FAST::Bio::PrimarySeqI interface"); | ||
| 24 | |||
| 25 | # accessors | ||
| 26 | |||
| 27 | $string = $obj->seq(); | ||
| 28 | $substring = $obj->subseq(12,50); | ||
| 29 | $display = $obj->display_id(); # for human display | ||
| 30 | $id = $obj->primary_id(); # unique id for this object, implementation defined | ||
| 31 | $unique_key= $obj->accession_number(); | ||
| 32 | # unique biological id | ||
| 33 | |||
| 34 | # object manipulation | ||
| 35 | |||
| 36 | eval { | ||
| 37 | $rev = $obj->revcom(); | ||
| 38 | }; | ||
| 39 | if( $@ ) { | ||
| 40 | $obj->throw("Could not reverse complement. Probably not DNA. Actual exception\n$@\n"); | ||
| 41 | } | ||
| 42 | |||
| 43 | $trunc = $obj->trunc(12,50); | ||
| 44 | |||
| 45 | # $rev and $trunc are FAST::Bio::PrimarySeqI compliant objects | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This object defines an abstract interface to basic sequence | ||
| 52 | information. PrimarySeq is an object just for the sequence and its | ||
| 53 | name(s), nothing more. Seq is the larger object complete with | ||
| 54 | features. There is a pure perl implementation of this in | ||
| 55 | FAST::Bio::PrimarySeq. If you just want to use FAST::Bio::PrimarySeq objects, then | ||
| 56 | please read that module first. This module defines the interface, and | ||
| 57 | is of more interest to people who want to wrap their own Perl | ||
| 58 | Objects/RDBs/FileSystems etc in way that they "are" bioperl sequence | ||
| 59 | objects, even though it is not using Perl to store the sequence etc. | ||
| 60 | |||
| 61 | |||
| 62 | This interface defines what bioperl consideres necessary to "be" a | ||
| 63 | sequence, without providing an implementation of this. (An | ||
| 64 | implementation is provided in FAST::Bio::PrimarySeq). If you want to provide | ||
| 65 | a FAST::Bio::PrimarySeq 'compliant' object which in fact wraps another | ||
| 66 | object/database/out-of-perl experience, then this is the correct thing | ||
| 67 | to wrap, generally by providing a wrapper class which would inheriet | ||
| 68 | from your object and this FAST::Bio::PrimarySeqI interface. The wrapper class | ||
| 69 | then would have methods lists in the "Implementation Specific | ||
| 70 | Functions" which would provide these methods for your object. | ||
| 71 | |||
| 72 | |||
| 73 | =head1 FEEDBACK | ||
| 74 | |||
| 75 | =head2 Mailing Lists | ||
| 76 | |||
| 77 | User feedback is an integral part of the evolution of this and other | ||
| 78 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 79 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 80 | |||
| 81 | bioperl-l@bioperl.org - General discussion | ||
| 82 | http://bio.perl.org/MailList.html - About the mailing lists | ||
| 83 | |||
| 84 | =head2 Reporting Bugs | ||
| 85 | |||
| 86 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 87 | the bugs and their resolution. Bug reports can be submitted via email | ||
| 88 | or the web: | ||
| 89 | |||
| 90 | bioperl-bugs@bio.perl.org | ||
| 91 | http://bio.perl.org/bioperl-bugs/ | ||
| 92 | |||
| 93 | =head1 AUTHOR - Ewan Birney | ||
| 94 | |||
| 95 | Email birney@sanger.ac.uk | ||
| 96 | |||
| 97 | Describe contact details here | ||
| 98 | |||
| 99 | =head1 APPENDIX | ||
| 100 | |||
| 101 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | # Let the code begin... | ||
| 107 | |||
| 108 | package FAST::Bio::MyPrimarySeqI; | ||
| 109 | use vars qw(@ISA); | ||
| 110 | use strict; | ||
| 111 | use Carp; | ||
| 112 | use FAST::Bio::Tools::CodonTable; | ||
| 113 | |||
| 114 | =head1 Implementation Specific Functions | ||
| 115 | |||
| 116 | These functions are the ones that a specific implementation must | ||
| 117 | define. | ||
| 118 | |||
| 119 | =head2 seq | ||
| 120 | |||
| 121 | Title : seq | ||
| 122 | Usage : $string = $obj->seq() | ||
| 123 | Function: Returns the sequence as a string of letters. The | ||
| 124 | case of the letters is left up to the implementer. | ||
| 125 | Suggested cases are upper case for proteins and lower case for | ||
| 126 | DNA sequence (IUPAC standard), | ||
| 127 | but implementations are suggested to keep an open mind about | ||
| 128 | case (some users... want mixed case!) | ||
| 129 | Returns : A scalar | ||
| 130 | Status : Virtual | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub seq { | ||
| 135 | my ($self) = @_; | ||
| 136 | |||
| 137 | if( $self->can('throw') ) { | ||
| 138 | $self->throw("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 139 | } else { | ||
| 140 | confess("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | =head2 subseq | ||
| 145 | |||
| 146 | Title : subseq | ||
| 147 | Usage : $substring = $obj->subseq(10,40); | ||
| 148 | Function: returns the subseq from start to end, where the first base | ||
| 149 | is 1 and the number is inclusive, ie 1-2 are the first two | ||
| 150 | bases of the sequence | ||
| 151 | |||
| 152 | Start cannot be larger than end but can be equal | ||
| 153 | |||
| 154 | Returns : a string | ||
| 155 | Args : | ||
| 156 | Status : Virtual | ||
| 157 | |||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub subseq{ | ||
| 162 | my ($self) = @_; | ||
| 163 | |||
| 164 | if( $self->can('throw') ) { | ||
| 165 | $self->throw("FAST::Bio::PrimarySeqI definition of subseq - implementing class did not provide this method"); | ||
| 166 | } else { | ||
| 167 | confess("FAST::Bio::PrimarySeqI definition of subseq - implementing class did not provide this method"); | ||
| 168 | } | ||
| 169 | |||
| 170 | } | ||
| 171 | |||
| 172 | =head2 display_id | ||
| 173 | |||
| 174 | Title : display_id | ||
| 175 | Usage : $id_string = $obj->display_id(); | ||
| 176 | Function: returns the display id, aka the common name of the Sequence object. | ||
| 177 | |||
| 178 | The semantics of this is that it is the most likely string | ||
| 179 | to be used as an identifier of the sequence, and likely to | ||
| 180 | have "human" readability. The id is equivalent to the ID | ||
| 181 | field of the GenBank/EMBL databanks and the id field of the | ||
| 182 | Swissprot/sptrembl database. In fasta format, the >(\S+) is | ||
| 183 | presumed to be the id, though some people overload the id | ||
| 184 | to embed other information. Bioperl does not use any | ||
| 185 | embedded information in the ID field, and people are | ||
| 186 | encouraged to use other mechanisms (accession field for | ||
| 187 | example, or extending the sequence object) to solve this. | ||
| 188 | |||
| 189 | Notice that $seq->id() maps to this function, mainly for | ||
| 190 | legacy/convience issues | ||
| 191 | Returns : A string | ||
| 192 | Args : None | ||
| 193 | Status : Virtual | ||
| 194 | |||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub display_id { | ||
| 199 | my ($self) = @_; | ||
| 200 | |||
| 201 | if( $self->can('throw') ) { | ||
| 202 | $self->throw("FAST::Bio::PrimarySeqI definition of id - implementing class did not provide this method"); | ||
| 203 | } else { | ||
| 204 | confess("FAST::Bio::PrimarySeqI definition of id - implementing class did not provide this method"); | ||
| 205 | } | ||
| 206 | |||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | =head2 accession_number | ||
| 211 | |||
| 212 | Title : accession_number | ||
| 213 | Usage : $unique_biological_key = $obj->accession_number; | ||
| 214 | Function: Returns the unique biological id for a sequence, commonly | ||
| 215 | called the accession_number. For sequences from established | ||
| 216 | databases, the implementors should try to use the correct | ||
| 217 | accession number. Notice that primary_id() provides the | ||
| 218 | unique id for the implemetation, allowing multiple objects | ||
| 219 | to have the same accession number in a particular implementation. | ||
| 220 | |||
| 221 | For sequences with no accession number, this method should return | ||
| 222 | "unknown". | ||
| 223 | Returns : A string | ||
| 224 | Args : None | ||
| 225 | Status : Virtual | ||
| 226 | |||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub accession_number { | ||
| 231 | my ($self,@args) = @_; | ||
| 232 | |||
| 233 | if( $self->can('throw') ) { | ||
| 234 | $self->throw("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 235 | } else { | ||
| 236 | confess("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 237 | } | ||
| 238 | |||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | =head2 primary_id | ||
| 244 | |||
| 245 | Title : primary_id | ||
| 246 | Usage : $unique_implementation_key = $obj->primary_id; | ||
| 247 | Function: Returns the unique id for this object in this | ||
| 248 | implementation. This allows implementations to manage | ||
| 249 | their own object ids in a way the implementaiton can control | ||
| 250 | clients can expect one id to map to one object. | ||
| 251 | |||
| 252 | For sequences with no accession number, this method should return | ||
| 253 | a stringified memory location. | ||
| 254 | Returns : A string | ||
| 255 | Args : None | ||
| 256 | Status : Virtual | ||
| 257 | |||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub primary_id { | ||
| 262 | my ($self,@args) = @_; | ||
| 263 | |||
| 264 | if( $self->can('throw') ) { | ||
| 265 | $self->throw("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 266 | } else { | ||
| 267 | confess("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 268 | } | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | =head2 can_call_new | ||
| 274 | |||
| 275 | Title : can_call_new | ||
| 276 | Usage : if( $obj->can_call_new ) { | ||
| 277 | $newobj = $obj->new( %param ); | ||
| 278 | } | ||
| 279 | Function: can_call_new returns 1 or 0 depending | ||
| 280 | on whether an implementation allows new | ||
| 281 | constructor to be called. If a new constructor | ||
| 282 | is allowed, then it should take the followed hashed | ||
| 283 | constructor list. | ||
| 284 | |||
| 285 | $myobject->new( -seq => $sequence_as_string, | ||
| 286 | -display_id => $id | ||
| 287 | -accession_number => $accession | ||
| 288 | -alphabet => 'dna', | ||
| 289 | ); | ||
| 290 | Example : | ||
| 291 | Returns : 1 or 0 | ||
| 292 | Args : | ||
| 293 | |||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | sub can_call_new{ | ||
| 298 | my ($self,@args) = @_; | ||
| 299 | |||
| 300 | # we default to 0 here | ||
| 301 | |||
| 302 | return 0; | ||
| 303 | } | ||
| 304 | |||
| 305 | =head2 alphabet | ||
| 306 | |||
| 307 | Title : alphabet | ||
| 308 | Usage : if( $obj->alphabet eq 'dna' ) { /Do Something/ } | ||
| 309 | Function: Returns the type of sequence being one of | ||
| 310 | 'dna', 'rna' or 'protein'. This is case sensitive. | ||
| 311 | |||
| 312 | This is not called <type> because this would cause | ||
| 313 | upgrade problems from the 0.5 and earlier Seq objects. | ||
| 314 | |||
| 315 | Returns : a string either 'dna','rna','protein'. NB - the object must | ||
| 316 | make a call of the type - if there is no type specified it | ||
| 317 | has to guess. | ||
| 318 | Args : none | ||
| 319 | Status : Virtual | ||
| 320 | |||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | sub alphabet{ | ||
| 325 | my ($self,@args) = @_; | ||
| 326 | |||
| 327 | if( $self->can('throw') ) { | ||
| 328 | $self->throw("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 329 | } else { | ||
| 330 | confess("FAST::Bio::PrimarySeqI definition of seq - implementing class did not provide this method"); | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | } | ||
| 335 | |||
| 336 | =head1 Optional Implementation Functions | ||
| 337 | |||
| 338 | The following functions rely on the above functions. A implementing | ||
| 339 | class does not need to provide these functions, as they will be | ||
| 340 | provided by this class, but is free to override these functions. | ||
| 341 | |||
| 342 | All of revcom(), trunc(), and translate() create new sequence | ||
| 343 | objects. They will call new() on the class of the sequence object | ||
| 344 | instance passed as argument, unless can_call_new() returns FALSE. In | ||
| 345 | the latter case a FAST::Bio::PrimarySeq object will be created. Implementors | ||
| 346 | which really want to control how objects are created (eg, for object | ||
| 347 | persistence over a database, or objects in a CORBA framework), they | ||
| 348 | are encouraged to override these methods | ||
| 349 | |||
| 350 | =head2 revcom | ||
| 351 | |||
| 352 | Title : revcom | ||
| 353 | Usage : $rev = $seq->revcom() | ||
| 354 | Function: Produces a new FAST::Bio::PrimarySeqI implementing object which | ||
| 355 | is the reversed complement of the sequence. For protein | ||
| 356 | sequences this throws an exception of "Sequence is a protein. Cannot revcom" | ||
| 357 | |||
| 358 | The id is the same id as the orginal sequence, and the accession number | ||
| 359 | is also indentical. If someone wants to track that this sequence has be | ||
| 360 | reversed, it needs to define its own extensions | ||
| 361 | |||
| 362 | To do an inplace edit of an object you can go: | ||
| 363 | |||
| 364 | $seq = $seq->revcom(); | ||
| 365 | |||
| 366 | This of course, causes Perl to handle the garbage collection of the old | ||
| 367 | object, but it is roughly speaking as efficient as an inplace edit. | ||
| 368 | |||
| 369 | Returns : A new (fresh) FAST::Bio::PrimarySeqI object | ||
| 370 | Args : none | ||
| 371 | |||
| 372 | |||
| 373 | =cut | ||
| 374 | |||
| 375 | sub revcom{ | ||
| 376 | my ($self) = @_; | ||
| 377 | |||
| 378 | |||
| 379 | # check the type is good first. | ||
| 380 | my $t = $self->alphabet; | ||
| 381 | |||
| 382 | if( $t eq 'protein' ) { | ||
| 383 | if( $self->can('throw') ) { | ||
| 384 | $self->throw("Sequence is a protein. Cannot revcom"); | ||
| 385 | } else { | ||
| 386 | confess("[$self] Sequence is a protein. Cannot revcom"); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | if( $t ne 'dna' && $t ne 'rna' ) { | ||
| 391 | if( $self->can('warn') ) { | ||
| 392 | $self->warn("Sequence is not dna or rna, but [$t]. Attempting to revcom, but unsure if this is right"); | ||
| 393 | } else { | ||
| 394 | warn("[$self] Sequence is not dna or rna, but [$t]. Attempting to revcom, but unsure if this is right"); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | # yank out the sequence string | ||
| 399 | |||
| 400 | my $str = $self->seq(); | ||
| 401 | |||
| 402 | # if is RNA - map to DNA then map back | ||
| 403 | |||
| 404 | if( $t eq 'rna' ) { | ||
| 405 | $str =~ tr/uU/tT/; | ||
| 406 | } | ||
| 407 | |||
| 408 | # revcom etc... | ||
| 409 | |||
| 410 | $str =~ tr/acgtrymkswhbvdnxACGTRYMKSWHBVDNX/tgcayrkmswdvbhnxTGCAYRKMSWDVBHNX/; | ||
| 411 | my $revseq = CORE::reverse $str; | ||
| 412 | |||
| 413 | if( $t eq 'rna' ) { | ||
| 414 | $revseq =~ tr/tT/uU/; | ||
| 415 | } | ||
| 416 | |||
| 417 | my $seqclass; | ||
| 418 | if($self->can_call_new()) { | ||
| 419 | $seqclass = ref($self); | ||
| 420 | } else { | ||
| 421 | $seqclass = 'FAST::Bio::PrimarySeq'; | ||
| 422 | $self->_attempt_to_load_Seq(); | ||
| 423 | } | ||
| 424 | my $out = $seqclass->new( '-seq' => $revseq, | ||
| 425 | '-display_id' => $self->display_id, | ||
| 426 | '-accession_number' => $self->accession_number, | ||
| 427 | '-alphabet' => $self->alphabet, | ||
| 428 | '-desc' => $self->desc() | ||
| 429 | ); | ||
| 430 | return $out; | ||
| 431 | |||
| 432 | } | ||
| 433 | |||
| 434 | =head2 trunc | ||
| 435 | |||
| 436 | Title : trunc | ||
| 437 | Usage : $subseq = $myseq->trunc(10,100); | ||
| 438 | Function: Provides a truncation of a sequence, | ||
| 439 | |||
| 440 | Example : | ||
| 441 | Returns : a fresh FAST::Bio::PrimarySeqI implementing object | ||
| 442 | Args : Two integers denoting first and last base of the sub-sequence. | ||
| 443 | |||
| 444 | |||
| 445 | =cut | ||
| 446 | |||
| 447 | sub trunc{ | ||
| 448 | my ($self,$start,$end) = @_; | ||
| 449 | |||
| 450 | if( !$end ) { | ||
| 451 | if( $self->can('throw') ) { | ||
| 452 | $self->throw("trunc start,end"); | ||
| 453 | } else { | ||
| 454 | confess("[$self] trunc start,end"); | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | if( $end < $start ) { | ||
| 459 | if( $self->can('throw') ) { | ||
| 460 | $self->throw("$end is smaller than $start. if you want to truncated and reverse complement, you must call trunc followed by revcom. Sorry."); | ||
| 461 | } else { | ||
| 462 | confess("[$self] $end is smaller than $start. If you want to truncated and reverse complement, you must call trunc followed by revcom. Sorry."); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | my $str = $self->subseq($start,$end); | ||
| 467 | |||
| 468 | my $seqclass; | ||
| 469 | if($self->can_call_new()) { | ||
| 470 | $seqclass = ref($self); | ||
| 471 | } else { | ||
| 472 | $seqclass = 'FAST::Bio::PrimarySeq'; | ||
| 473 | $self->_attempt_to_load_Seq(); | ||
| 474 | } | ||
| 475 | my $out = $seqclass->new( '-seq' => $str, | ||
| 476 | '-display_id' => $self->display_id, | ||
| 477 | '-accession_number' => $self->accession_number, | ||
| 478 | '-alphabet' => $self->alphabet, | ||
| 479 | '-desc' => $self->desc() | ||
| 480 | ); | ||
| 481 | return $out; | ||
| 482 | } | ||
| 483 | |||
| 484 | |||
| 485 | =head2 translate | ||
| 486 | |||
| 487 | Title : translate | ||
| 488 | Usage : $protein_seq_obj = $dna_seq_obj->translate | ||
| 489 | #if full CDS expected: | ||
| 490 | $protein_seq_obj = $cds_seq_obj->translate(undef,undef,undef,undef,1); | ||
| 491 | Function: | ||
| 492 | |||
| 493 | Provides the translation of the DNA sequence using full | ||
| 494 | IUPAC ambiguities in DNA/RNA and amino acid codes. | ||
| 495 | |||
| 496 | The full CDS translation is identical to EMBL/TREMBL | ||
| 497 | database translation. Note that the trailing terminator | ||
| 498 | character is removed before returning the translation | ||
| 499 | object. | ||
| 500 | |||
| 501 | Note: if you set $dna_seq_obj->verbose(1) you will get a | ||
| 502 | warning if the first codon is not a valid initiator. | ||
| 503 | |||
| 504 | |||
| 505 | Returns : A FAST::Bio::PrimarySeqI implementing object | ||
| 506 | Args : character for terminator (optional) defaults to '*' | ||
| 507 | character for unknown amino acid (optional) defaults to 'X' | ||
| 508 | frame (optional) valid values 0, 1, 2, defaults to 0 | ||
| 509 | codon table id (optional) defaults to 1 | ||
| 510 | complete coding sequence expected, defaults to 0 (false) | ||
| 511 | boolean, throw exception if not complete CDS (true) or defaults to warning (false) | ||
| 512 | |||
| 513 | =cut | ||
| 514 | |||
| 515 | |||
| 516 | sub translate { | ||
| 517 | my($self) = shift; | ||
| 518 | my($stop, $unknown, $frame, $tableid, $fullCDS, $throw) = @_; | ||
| 519 | my($i, $len, $output) = (0,0,''); | ||
| 520 | my($codon) = ""; | ||
| 521 | my $aa; | ||
| 522 | |||
| 523 | |||
| 524 | ## User can pass in symbol for stop and unknown codons | ||
| 525 | unless(defined($stop) and $stop ne '') { $stop = "*"; } | ||
| 526 | unless(defined($unknown) and $unknown ne '') { $unknown = "X"; } | ||
| 527 | unless(defined($frame) and $frame ne '') { $frame = 0; } | ||
| 528 | |||
| 529 | ## the codon table ID | ||
| 530 | unless(defined($tableid) and $tableid ne '') { $tableid = 1; } | ||
| 531 | |||
| 532 | ##Error if monomer is "Amino" | ||
| 533 | $self->throw("Can't translate an amino acid sequence.") if | ||
| 534 | ($self->alphabet eq 'protein'); | ||
| 535 | |||
| 536 | ##Error if frame is not 0, 1 or 2 | ||
| 537 | $self->throw("Valid values for frame are 0, 1, 2, not [$frame].") unless | ||
| 538 | ($frame == 0 or $frame == 1 or $frame == 2); | ||
| 539 | |||
| 540 | #thows a warning if ID is invalid | ||
| 541 | my $codonTable = FAST::Bio::Tools::CodonTable->new( -id => $tableid); | ||
| 542 | |||
| 543 | my ($seq) = $self->seq(); | ||
| 544 | |||
| 545 | # deal with frame offset. | ||
| 546 | if( $frame ) { | ||
| 547 | $seq = substr ($seq,$frame); | ||
| 548 | } | ||
| 549 | $seq .= 'n'; | ||
| 550 | |||
| 551 | my $length = (length $seq) - 2; | ||
| 552 | for ($i = 0 ; $i < $length ; $i += 3) { | ||
| 553 | my $codon = substr($seq, $i, 3); | ||
| 554 | my $aa = $codonTable->translate($codon); | ||
| 555 | if ($aa eq '*') { | ||
| 556 | $output .= $stop; | ||
| 557 | } | ||
| 558 | elsif ($aa eq 'X') { | ||
| 559 | $output .= $unknown; | ||
| 560 | } | ||
| 561 | else { | ||
| 562 | $output .= $aa ; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | # only if we are expecting to translate a complete coding region | ||
| 566 | if ($fullCDS) { | ||
| 567 | my $id = $self->display_id; | ||
| 568 | #remove the stop character | ||
| 569 | if( substr($output,-1,1) eq $stop ) { | ||
| 570 | chop $output; | ||
| 571 | } else { | ||
| 572 | $throw && $self->throw("Seq [$id]: Not using a valid terminator codon!"); | ||
| 573 | $self->warn("Seq [$id]: Not using a valid terminator codon!"); | ||
| 574 | } | ||
| 575 | # test if there are terminator characters inside the protein sequence! | ||
| 576 | if ($output =~ /\*/) { | ||
| 577 | $throw && $self->throw("Seq [$id]: Terminator codon inside CDS!"); | ||
| 578 | $self->warn("Seq [$id]: Terminator codon inside CDS!"); | ||
| 579 | } | ||
| 580 | # if the initiator codon is not ATG, the amino acid needs to changed into M | ||
| 581 | if ( substr($output,0,1) ne 'M' ) { | ||
| 582 | if ($codonTable->is_start_codon(substr($seq, 0, 3)) ) { | ||
| 583 | $output = 'M'. substr($output,1); | ||
| 584 | } | ||
| 585 | elsif ($throw) { | ||
| 586 | $self->warn("Seq [$id]: Not using a valid initiator codon!"); | ||
| 587 | } else { | ||
| 588 | $self->throw("Seq [$id]: Not using a valid initiator codon!"); | ||
| 589 | } | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | my $seqclass; | ||
| 594 | if($self->can_call_new()) { | ||
| 595 | $seqclass = ref($self); | ||
| 596 | } else { | ||
| 597 | $seqclass = 'FAST::Bio::PrimarySeq'; | ||
| 598 | $self->_attempt_to_load_Seq(); | ||
| 599 | } | ||
| 600 | my $out = $seqclass->new( '-seq' => $output, | ||
| 601 | '-display_id' => $self->display_id, | ||
| 602 | '-accession_number' => $self->accession_number, | ||
| 603 | # is there anything wrong with retaining the | ||
| 604 | # description? | ||
| 605 | '-desc' => $self->desc(), | ||
| 606 | '-alphabet' => 'protein' | ||
| 607 | ); | ||
| 608 | return $out; | ||
| 609 | |||
| 610 | } | ||
| 611 | |||
| 612 | |||
| 613 | =head2 id | ||
| 614 | |||
| 615 | Title : id | ||
| 616 | Usage : $id = $seq->id() | ||
| 617 | Function: ID of the sequence. This should normally be (and actually is in | ||
| 618 | the implementation provided here) just a synonym for display_id(). | ||
| 619 | Example : | ||
| 620 | Returns : A string. | ||
| 621 | Args : | ||
| 622 | |||
| 623 | |||
| 624 | =cut | ||
| 625 | |||
| 626 | sub id { | ||
| 627 | my ($self)= @_; | ||
| 628 | |||
| 629 | return $self->display_id(); | ||
| 630 | } | ||
| 631 | |||
| 632 | |||
| 633 | =head2 length | ||
| 634 | |||
| 635 | Title : length | ||
| 636 | Usage : $len = $seq->length() | ||
| 637 | Function: | ||
| 638 | Example : | ||
| 639 | Returns : integer representing the length of the sequence. | ||
| 640 | Args : | ||
| 641 | |||
| 642 | |||
| 643 | =cut | ||
| 644 | |||
| 645 | sub length { | ||
| 646 | my ($self)= @_; | ||
| 647 | |||
| 648 | if( $self->can('throw') ) { | ||
| 649 | $self->throw("FAST::Bio::PrimarySeqI definition of length - implementing class did not provide this method"); | ||
| 650 | } else { | ||
| 651 | confess("FAST::Bio::PrimarySeqI definition of length - implementing class did not provide this method"); | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | =head2 desc | ||
| 656 | |||
| 657 | Title : desc | ||
| 658 | Usage : $seq->desc($newval); | ||
| 659 | $description = $seq->desc(); | ||
| 660 | Function: Get/set description text for a seq object | ||
| 661 | Example : | ||
| 662 | Returns : value of desc | ||
| 663 | Args : newvalue (optional) | ||
| 664 | |||
| 665 | |||
| 666 | =cut | ||
| 667 | |||
| 668 | sub desc { | ||
| 669 | my ($self,$value) = @_; | ||
| 670 | if( $self->can('warn') ) { | ||
| 671 | $self->warn("FAST::Bio::PrimarySeqI definition of desc - implementing class did not provide this method"); | ||
| 672 | } else { | ||
| 673 | warn("FAST::Bio::PrimarySeqI definition of desc - implementing class did not provide this method"); | ||
| 674 | } | ||
| 675 | return ''; | ||
| 676 | } | ||
| 677 | |||
| 678 | # These methods are here for backward compatibility with the old, 0.5 | ||
| 679 | # Seq objects. They all throw warnings that someone is using a | ||
| 680 | # deprecated method, and may eventually be removed completely from | ||
| 681 | # this object. However, they are important to ease the transition from | ||
| 682 | # the old system. | ||
| 683 | |||
| 684 | sub str{ | ||
| 685 | my ($self,$start,$end) = @_; | ||
| 686 | |||
| 687 | # we assumme anyone using this is using vanilla bioperl object | ||
| 688 | my ($p,$f,$l) = caller; | ||
| 689 | $self->warn("$f:$l Seq::str - deprecated method. You should use \$obj->seq in preference"); | ||
| 690 | |||
| 691 | if( defined $end ) { | ||
| 692 | return $self->subseq($start,$end); | ||
| 693 | } else { | ||
| 694 | return $self->seq(); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | sub ary{ | ||
| 699 | my ($self,$start,$end) = @_; | ||
| 700 | |||
| 701 | # we assumme anyone using this is using vanilla bioperl object | ||
| 702 | my ($p,$f,$l) = caller; | ||
| 703 | $self->warn("$f:$l Seq::ary - deprecated method. You should use \$obj->seq in preference, followed by your split to an array"); | ||
| 704 | |||
| 705 | my $str; | ||
| 706 | if( defined $end ) { | ||
| 707 | $str = $self->subseq($start,$end); | ||
| 708 | } else { | ||
| 709 | $str = $self->seq(); | ||
| 710 | } | ||
| 711 | |||
| 712 | return split(//,$str); | ||
| 713 | } | ||
| 714 | |||
| 715 | sub getseq{ | ||
| 716 | my ($self,@args) = @_; | ||
| 717 | |||
| 718 | if( wantarray ) { | ||
| 719 | return $self->ary(@args); | ||
| 720 | } else { | ||
| 721 | return $self->str(@args); | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | sub setseq { | ||
| 726 | my ($self,$seq) = @_; | ||
| 727 | |||
| 728 | # we assumme anyone using this is using vanilla bioperl object | ||
| 729 | my ($p,$f,$l) = caller; | ||
| 730 | $self->warn("$f:$l Seq::setseq - deprecated method. You should use \$obj->seq in preference, followed by your split to an array"); | ||
| 731 | |||
| 732 | return $self->seq($seq); | ||
| 733 | } | ||
| 734 | |||
| 735 | sub type{ | ||
| 736 | my ($self) = @_; | ||
| 737 | |||
| 738 | # we assumme anyone using this is using vanilla bioperl object | ||
| 739 | my ($p,$f,$l) = caller; | ||
| 740 | $self->warn("$f:$l Seq::type - deprecated method. You should use \$obj->alphabet in preference (notice that alphabet returns lowercase strings)"); | ||
| 741 | |||
| 742 | my $t = $self->alphabet; | ||
| 743 | $t eq "dna" && return "DNA"; | ||
| 744 | $t eq "rna" && return "RNA"; | ||
| 745 | $t eq "protein" && return "PROTEIN"; | ||
| 746 | return "UNKNOWN"; | ||
| 747 | } | ||
| 748 | |||
| 749 | sub seq_len { | ||
| 750 | my $self = shift; | ||
| 751 | # we assumme anyone using this is using vanilla bioperl object | ||
| 752 | my ($p,$f,$l) = caller; | ||
| 753 | $self->warn("$f:$l Seq::seq_len - deprecated method. You should use \$obj->length in preference"); | ||
| 754 | return $self->length(); | ||
| 755 | } | ||
| 756 | |||
| 757 | sub out_fasta{ | ||
| 758 | my ($self,@args) = @_; | ||
| 759 | |||
| 760 | my ($p,$f,$l) = caller; | ||
| 761 | $self->warn("$f:$l Seq::out_fasta - deprecated method. You should use the SeqIO package in preference"); | ||
| 762 | |||
| 763 | my $str = $self->seq; | ||
| 764 | $str =~ tr/a-z/A-Z/; | ||
| 765 | $str=~ s/(.{1,60})/$1\n/g; | ||
| 766 | # return ">". $self->id(). " ".$self->desc()."\n"; | ||
| 767 | return ">". $self->id(). "\n" . $str . "\n"; #ps 3/25/00 | ||
| 768 | } | ||
| 769 | |||
| 770 | =head1 Private functions | ||
| 771 | |||
| 772 | These are some private functions for the PrimarySeqI interface. You do not | ||
| 773 | need to implement these functions | ||
| 774 | |||
| 775 | =head2 _attempt_to_load_Seq | ||
| 776 | |||
| 777 | Title : _attempt_to_load_Seq | ||
| 778 | Usage : | ||
| 779 | Function: | ||
| 780 | Example : | ||
| 781 | Returns : | ||
| 782 | Args : | ||
| 783 | |||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub _attempt_to_load_Seq{ | ||
| 788 | my ($self) = @_; | ||
| 789 | |||
| 790 | if( $main::{'FAST::Bio::PrimarySeq'} ) { | ||
| 791 | return 1; | ||
| 792 | } else { | ||
| 793 | eval { | ||
| 794 | require FAST::Bio::PrimarySeq; | ||
| 795 | }; | ||
| 796 | if( $@ ) { | ||
| 797 | if( $self->can('throw') ) { | ||
| 798 | $self->throw("FAST::Bio::PrimarySeq could not be loaded for $self\nThis indicates that you are usnig FAST::Bio::PrimarySeqI without FAST::Bio::PrimarySeq loaded and without providing a complete solution\nThe most likely problem is that there has been a misconfiguration of the bioperl environment\nActual exception\n\n$@\n"); | ||
| 799 | } else { | ||
| 800 | confess("FAST::Bio::PrimarySeq could not be loaded for $self\nThis indicates that you are usnig FAST::Bio::PrimarySeqI without FAST::Bio::PrimarySeq loaded and without providing a complete solution\nThe most likely problem is that there has been a misconfiguration of the bioperl environment\nActual exception\n\n$@\n"); | ||
| 801 | } | ||
| 802 | return 0; | ||
| 803 | } | ||
| 804 | return 1; | ||
| 805 | } | ||
| 806 | |||
| 807 | } | ||
| 808 | |||
| 809 | |||
| 810 | |||
| 811 | |||
| 812 | 1; | ||
| 813 | |||
| 814 |