« FAST/Bio/Ontology/OBOEngine.pm
↑ Summary
FAST/Bio/OntologyIO/Handlers/InterPro_BioSQL_Handler.pm »
| 1 | # | ||
| 2 | # bioperl module for FAST::Bio::PrimarySeq | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Ewan Birney <birney@ebi.ac.uk> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::PrimarySeq - Bioperl lightweight Sequence Object | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # FAST::Bio::SeqIO for file reading, FAST::Bio::DB::GenBank for | ||
| 21 | # database reading | ||
| 22 | |||
| 23 | use FAST::Bio::Seq; | ||
| 24 | use FAST::Bio::SeqIO; | ||
| 25 | use FAST::Bio::DB::GenBank; | ||
| 26 | |||
| 27 | # make from memory | ||
| 28 | |||
| 29 | $seqobj = FAST::Bio::PrimarySeq->new ( -seq => 'ATGGGGTGGGCGGTGGGTGGTTTG', | ||
| 30 | -id => 'GeneFragment-12', | ||
| 31 | -accession_number => 'X78121', | ||
| 32 | -alphabet => 'dna', | ||
| 33 | -is_circular => 1 ); | ||
| 34 | print "Sequence ", $seqobj->id(), " with accession ", | ||
| 35 | $seqobj->accession_number, "\n"; | ||
| 36 | |||
| 37 | # read from file | ||
| 38 | |||
| 39 | $inputstream = FAST::Bio::SeqIO->new(-file => "myseq.fa", | ||
| 40 | -format => 'Fasta'); | ||
| 41 | $seqobj = $inputstream->next_seq(); | ||
| 42 | print "Sequence ", $seqobj->id(), " and desc ", $seqobj->desc, "\n"; | ||
| 43 | |||
| 44 | # to get out parts of the sequence. | ||
| 45 | |||
| 46 | print "Sequence ", $seqobj->id(), " with accession ", | ||
| 47 | $seqobj->accession_number, " and desc ", $seqobj->desc, "\n"; | ||
| 48 | |||
| 49 | $string = $seqobj->seq(); | ||
| 50 | $string2 = $seqobj->subseq(1,40); | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | PrimarySeq is a lightweight Sequence object, storing the sequence, its | ||
| 55 | name, a computer-useful unique name, and other fundamental attributes. | ||
| 56 | It does not contain sequence features or other information. To have a | ||
| 57 | sequence with sequence features you should use the Seq object which uses | ||
| 58 | this object. | ||
| 59 | |||
| 60 | Although new users will use FAST::Bio::PrimarySeq a lot, in general you will | ||
| 61 | be using it from the FAST::Bio::Seq object. For more information on FAST::Bio::Seq | ||
| 62 | see L<FAST::Bio::Seq>. For interest you might like to know that | ||
| 63 | FAST::Bio::Seq has-a FAST::Bio::PrimarySeq and forwards most of the function calls | ||
| 64 | to do with sequence to it (the has-a relationship lets us get out of a | ||
| 65 | otherwise nasty cyclical reference in Perl which would leak memory). | ||
| 66 | |||
| 67 | Sequence objects are defined by the FAST::Bio::PrimarySeqI interface, and this | ||
| 68 | object is a pure Perl implementation of the interface. If that's | ||
| 69 | gibberish to you, don't worry. The take home message is that this | ||
| 70 | object is the bioperl default sequence object, but other people can | ||
| 71 | use their own objects as sequences if they so wish. If you are | ||
| 72 | interested in wrapping your own objects as compliant Bioperl sequence | ||
| 73 | objects, then you should read the FAST::Bio::PrimarySeqI documentation | ||
| 74 | |||
| 75 | The documentation of this object is a merge of the FAST::Bio::PrimarySeq and | ||
| 76 | FAST::Bio::PrimarySeqI documentation. This allows all the methods which you can | ||
| 77 | call on sequence objects here. | ||
| 78 | |||
| 79 | =head1 FEEDBACK | ||
| 80 | |||
| 81 | =head2 Mailing Lists | ||
| 82 | |||
| 83 | User feedback is an integral part of the evolution of this and other | ||
| 84 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 85 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 86 | |||
| 87 | bioperl-l@bioperl.org - General discussion | ||
| 88 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 89 | |||
| 90 | =head2 Support | ||
| 91 | |||
| 92 | Please direct usage questions or support issues to the mailing list: | ||
| 93 | |||
| 94 | I<bioperl-l@bioperl.org> | ||
| 95 | |||
| 96 | rather than to the module maintainer directly. Many experienced and | ||
| 97 | reponsive experts will be able look at the problem and quickly | ||
| 98 | address it. Please include a thorough description of the problem | ||
| 99 | with code and data examples if at all possible. | ||
| 100 | |||
| 101 | =head2 Reporting Bugs | ||
| 102 | |||
| 103 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 104 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 105 | web: | ||
| 106 | |||
| 107 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 108 | |||
| 109 | =head1 AUTHOR - Ewan Birney | ||
| 110 | |||
| 111 | Email birney@ebi.ac.uk | ||
| 112 | |||
| 113 | =head1 APPENDIX | ||
| 114 | |||
| 115 | The rest of the documentation details each of the object | ||
| 116 | methods. Internal methods are usually preceded with a _ | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | |||
| 121 | # Let the code begin... | ||
| 122 | |||
| 123 | |||
| 124 | package FAST::Bio::PrimarySeq; | ||
| 125 | use vars qw($MATCHPATTERN $GAP_SYMBOLS); | ||
| 126 | use strict; | ||
| 127 | |||
| 128 | $MATCHPATTERN = 'A-Za-z\-\.\*\?=~'; | ||
| 129 | $GAP_SYMBOLS = '-~'; | ||
| 130 | |||
| 131 | use base qw(FAST::Bio::Root::Root FAST::Bio::PrimarySeqI | ||
| 132 | FAST::Bio::IdentifiableI FAST::Bio::DescribableI); | ||
| 133 | |||
| 134 | # | ||
| 135 | # setup the allowed values for alphabet() | ||
| 136 | # | ||
| 137 | |||
| 138 | my %valid_type = map {$_, 1} qw( dna rna protein ); | ||
| 139 | |||
| 140 | =head2 new | ||
| 141 | |||
| 142 | Title : new | ||
| 143 | Usage : $seq = FAST::Bio::PrimarySeq->new( -seq => 'ATGGGGGTGGTGGTACCCT', | ||
| 144 | -id => 'human_id', | ||
| 145 | -accession_number => 'AL000012', | ||
| 146 | ); | ||
| 147 | |||
| 148 | Function: Returns a new primary seq object from | ||
| 149 | basic constructors, being a string for the sequence | ||
| 150 | and strings for id and accession_number. | ||
| 151 | |||
| 152 | Note that you can provide an empty sequence string. However, in | ||
| 153 | this case you MUST specify the type of sequence you wish to | ||
| 154 | initialize by the parameter -alphabet. See alphabet() for possible | ||
| 155 | values. | ||
| 156 | Returns : a new FAST::Bio::PrimarySeq object | ||
| 157 | Args : -seq => sequence string | ||
| 158 | -display_id => display id of the sequence (locus name) | ||
| 159 | -accession_number => accession number | ||
| 160 | -primary_id => primary id (Genbank id) | ||
| 161 | -version => version number | ||
| 162 | -namespace => the namespace for the accession | ||
| 163 | -authority => the authority for the namespace | ||
| 164 | -description => description text | ||
| 165 | -desc => alias for description | ||
| 166 | -alphabet => sequence type (alphabet) (dna|rna|protein) | ||
| 167 | -id => alias for display id | ||
| 168 | -is_circular => boolean field for whether or not sequence is circular | ||
| 169 | -direct => boolean field for directly setting sequence (requires alphabet also set) | ||
| 170 | -ref_to_seq => boolean field indicating the sequence is a reference (?!?) | ||
| 171 | -nowarnonempty => boolean field for whether or not to warn when sequence is empty | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | |||
| 176 | sub new { | ||
| 177 | my ($class, @args) = @_; | ||
| 178 | my $self = $class->SUPER::new(@args); | ||
| 179 | |||
| 180 | my($seq,$id,$acc,$pid,$ns,$auth,$v,$oid, | ||
| 181 | $desc,$description, | ||
| 182 | $alphabet,$given_id,$is_circular,$direct,$ref_to_seq,$len,$nowarnonempty) = | ||
| 183 | $self->_rearrange([qw(SEQ | ||
| 184 | DISPLAY_ID | ||
| 185 | ACCESSION_NUMBER | ||
| 186 | PRIMARY_ID | ||
| 187 | NAMESPACE | ||
| 188 | AUTHORITY | ||
| 189 | VERSION | ||
| 190 | OBJECT_ID | ||
| 191 | DESC | ||
| 192 | DESCRIPTION | ||
| 193 | ALPHABET | ||
| 194 | ID | ||
| 195 | IS_CIRCULAR | ||
| 196 | DIRECT | ||
| 197 | REF_TO_SEQ | ||
| 198 | LENGTH | ||
| 199 | NOWARNONEMPTY | ||
| 200 | )], | ||
| 201 | @args); | ||
| 202 | |||
| 203 | # private var _nowarnonempty, need to be set before calling _guess_alphabet | ||
| 204 | $self->{'_nowarnonempty'} = $nowarnonempty; | ||
| 205 | |||
| 206 | if( defined $id && defined $given_id ) { | ||
| 207 | if( $id ne $given_id ) { | ||
| 208 | $self->throw("Provided both id and display_id constructor ". | ||
| 209 | "functions. [$id] [$given_id]"); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | if( defined $given_id ) { $id = $given_id; } | ||
| 213 | |||
| 214 | # let's set the length before the seq -- if there is one, this length is | ||
| 215 | # going to be invalidated | ||
| 216 | defined $len && $self->length($len); | ||
| 217 | |||
| 218 | # if alphabet is provided we set it first, so that it won't be guessed | ||
| 219 | # when the sequence is set | ||
| 220 | $alphabet && $self->alphabet($alphabet); | ||
| 221 | |||
| 222 | # bernd's idea: define ids so that invalid sequence messages | ||
| 223 | # can be more informative... | ||
| 224 | defined $id && $self->display_id($id); | ||
| 225 | $acc && $self->accession_number($acc); | ||
| 226 | defined $pid && $self->primary_id($pid); | ||
| 227 | |||
| 228 | # if there is an alphabet, and direct is passed in, assume the alphabet | ||
| 229 | # and sequence is ok | ||
| 230 | |||
| 231 | if( $direct && $ref_to_seq) { | ||
| 232 | $self->{'seq'} = $$ref_to_seq; | ||
| 233 | if( ! $alphabet ) { | ||
| 234 | $self->_guess_alphabet(); | ||
| 235 | } # else it has been set already above | ||
| 236 | } else { | ||
| 237 | # print STDERR "DEBUG: setting sequence to [$seq]\n"; | ||
| 238 | # note: the sequence string may be empty | ||
| 239 | $self->seq($seq) if defined($seq); | ||
| 240 | } | ||
| 241 | |||
| 242 | $desc && $self->desc($desc); | ||
| 243 | $description && $self->description($description); | ||
| 244 | $is_circular && $self->is_circular($is_circular); | ||
| 245 | $ns && $self->namespace($ns); | ||
| 246 | $auth && $self->authority($auth); | ||
| 247 | defined($v) && $self->version($v); | ||
| 248 | defined($oid) && $self->object_id($oid); | ||
| 249 | |||
| 250 | |||
| 251 | return $self; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub direct_seq_set { | ||
| 255 | my $obj = shift; | ||
| 256 | return $obj->{'seq'} = shift if @_; | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | =head2 seq | ||
| 262 | |||
| 263 | Title : seq | ||
| 264 | Usage : $string = $obj->seq() | ||
| 265 | Function: Returns the sequence as a string of letters. The | ||
| 266 | case of the letters is left up to the implementer. | ||
| 267 | Suggested cases are upper case for proteins and lower case for | ||
| 268 | DNA sequence (IUPAC standard), but you should not rely on this. | ||
| 269 | Returns : A scalar | ||
| 270 | Args : Optionally on set the new value (a string). An optional second | ||
| 271 | argument presets the alphabet (otherwise it will be guessed). | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub seq { | ||
| 276 | my ($obj,@args) = @_; | ||
| 277 | |||
| 278 | if( scalar(@args) == 0 ) { | ||
| 279 | return $obj->{'seq'}; | ||
| 280 | } | ||
| 281 | |||
| 282 | my ($value,$alphabet) = @args; | ||
| 283 | |||
| 284 | if(@args) { | ||
| 285 | if(defined($value) && (! $obj->validate_seq($value))) { | ||
| 286 | $obj->throw("Attempting to set the sequence '".(defined($obj->id) || | ||
| 287 | "[unidentified sequence]")."' to [$value] which does not look healthy"); | ||
| 288 | } | ||
| 289 | # if a sequence was already set we make sure that we re-adjust the | ||
| 290 | # alphabet, otherwise we skip guessing if alphabet is already set | ||
| 291 | # note: if the new seq is empty or undef, we don't consider that a | ||
| 292 | # change (we wouldn't have anything to guess on anyway) | ||
| 293 | my $is_changed_seq = | ||
| 294 | exists($obj->{'seq'}) && (CORE::length($value || '') > 0); | ||
| 295 | $obj->{'seq'} = $value; | ||
| 296 | # new alphabet overridden by arguments? | ||
| 297 | if($alphabet) { | ||
| 298 | # yes, set it no matter what | ||
| 299 | $obj->alphabet($alphabet); | ||
| 300 | } elsif ($is_changed_seq || (! defined($obj->alphabet()))) { | ||
| 301 | # if we changed a previous sequence to a new one or if there is no | ||
| 302 | # alphabet yet at all, we need to guess the (possibly new) alphabet | ||
| 303 | $obj->_guess_alphabet(); | ||
| 304 | } # else (seq not changed and alphabet was defined) do nothing | ||
| 305 | # if the seq is changed, make sure we unset a possibly set length | ||
| 306 | $obj->length(undef) if $is_changed_seq || $obj->{'seq'}; | ||
| 307 | } | ||
| 308 | return $obj->{'seq'}; | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 validate_seq | ||
| 312 | |||
| 313 | Title : validate_seq | ||
| 314 | Usage : if(! $seq->validate_seq($seq_str) ) { | ||
| 315 | print "sequence $seq_str is not valid for an object of | ||
| 316 | alphabet ",$seq->alphabet, "\n"; | ||
| 317 | } | ||
| 318 | Function: Validates a given sequence string. A validating sequence string | ||
| 319 | must be accepted by seq(). A string that does not validate will | ||
| 320 | lead to an exception if passed to seq(). | ||
| 321 | |||
| 322 | The implementation provided here does not take alphabet() into | ||
| 323 | account. Allowed are all letters (A-Z) and '-','.','*','?','=', | ||
| 324 | and '~'. | ||
| 325 | |||
| 326 | Example : | ||
| 327 | Returns : 1 if the supplied sequence string is valid for the object, and | ||
| 328 | 0 otherwise. | ||
| 329 | Args : The sequence string to be validated. | ||
| 330 | |||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub validate_seq { | ||
| 335 | my ($self,$seqstr) = @_; | ||
| 336 | if( ! defined $seqstr ){ $seqstr = $self->seq(); } | ||
| 337 | return 0 unless( defined $seqstr); | ||
| 338 | if((CORE::length($seqstr) > 0) && | ||
| 339 | ($seqstr !~ /^([$MATCHPATTERN]+)$/)) { | ||
| 340 | $self->warn("sequence '".(defined($self->id) || "[unidentified sequence]"). | ||
| 341 | "' doesn't validate, mismatch is " . | ||
| 342 | join(",",($seqstr =~ /([^$MATCHPATTERN]+)/g))); | ||
| 343 | return 0; | ||
| 344 | } | ||
| 345 | return 1; | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 subseq | ||
| 349 | |||
| 350 | Title : subseq | ||
| 351 | Usage : $substring = $obj->subseq(10,40); | ||
| 352 | $substring = $obj->subseq(10,40,NOGAP) | ||
| 353 | $substring = $obj->subseq(-START=>10,-END=>40,-REPLACE_WITH=>'tga') | ||
| 354 | Function: returns the subseq from start to end, where the first sequence | ||
| 355 | character has coordinate 1 number is inclusive, ie 1-2 are the | ||
| 356 | first two characters of the sequence | ||
| 357 | Returns : a string | ||
| 358 | Args : integer for start position | ||
| 359 | integer for end position | ||
| 360 | OR | ||
| 361 | FAST::Bio::LocationI location for subseq (strand honored) | ||
| 362 | Specify -NOGAP=>1 to return subseq with gap characters removed | ||
| 363 | Specify -REPLACE_WITH=>$new_subseq to replace the subseq returned | ||
| 364 | with $new_subseq in the sequence object | ||
| 365 | |||
| 366 | =cut | ||
| 367 | |||
| 368 | sub subseq { | ||
| 369 | my $self = shift; | ||
| 370 | my @args = @_; | ||
| 371 | my ($start,$end,$nogap,$replace) = $self->_rearrange([qw(START | ||
| 372 | END | ||
| 373 | NOGAP | ||
| 374 | REPLACE_WITH)],@args); | ||
| 375 | |||
| 376 | # if $replace is specified, have the constructor validate it as seq | ||
| 377 | my $dummy = new FAST::Bio::PrimarySeq(-seq=>$replace, -alphabet=>$self->alphabet) if defined($replace); | ||
| 378 | |||
| 379 | if( ref($start) && $start->isa('FAST::Bio::LocationI') ) { | ||
| 380 | my $loc = $start; | ||
| 381 | my $seq = ""; | ||
| 382 | foreach my $subloc ($loc->each_Location()) { | ||
| 383 | my $piece = $self->subseq(-START=>$subloc->start(), | ||
| 384 | '-END'=>$subloc->end(), | ||
| 385 | -REPLACE_WITH=>$replace, | ||
| 386 | -NOGAP=>$nogap); | ||
| 387 | $piece =~ s/[$GAP_SYMBOLS]//g if $nogap; | ||
| 388 | if($subloc->strand() < 0) { | ||
| 389 | $piece = FAST::Bio::PrimarySeq->new('-seq' => $piece)->revcom()->seq(); | ||
| 390 | } | ||
| 391 | $seq .= $piece; | ||
| 392 | } | ||
| 393 | return $seq; | ||
| 394 | } elsif( defined $start && defined $end ) { | ||
| 395 | if( $start > $end ){ | ||
| 396 | $self->throw("Bad start,end parameters. Start [$start] has to be ". | ||
| 397 | "less than end [$end]"); | ||
| 398 | } | ||
| 399 | if( $start <= 0 ) { | ||
| 400 | $self->throw("Bad start parameter ($start). Start must be positive."); | ||
| 401 | } | ||
| 402 | |||
| 403 | # remove one from start, and then length is end-start | ||
| 404 | $start--; | ||
| 405 | my @ss_args = map { eval "defined $_" ? $_ : () } qw( $self->{seq} $start $end-$start $replace); | ||
| 406 | my $seqstr = eval join( '', "substr(", join(',',@ss_args), ")"); | ||
| 407 | |||
| 408 | if( $end > $self->length) { | ||
| 409 | if ($self->is_circular) { | ||
| 410 | my $start = 0; | ||
| 411 | my $end = $end - $self->length; | ||
| 412 | my @ss_args = map { eval "defined $_" ? $_ : () } qw( $self->{seq} $start $end-$start $replace); | ||
| 413 | my $appendstr = eval join( '', "substr(", join(',',@ss_args), ")"); | ||
| 414 | $seqstr .= $appendstr; | ||
| 415 | } else { | ||
| 416 | $self->throw("Bad end parameter ($end). End must be less than the total length of sequence (total=".$self->length.")") | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | $seqstr =~ s/[$GAP_SYMBOLS]//g if ($nogap); | ||
| 421 | return $seqstr; | ||
| 422 | |||
| 423 | } else { | ||
| 424 | $self->warn("Incorrect parameters to subseq - must be two integers or a FAST::Bio::LocationI object. Got:", $self,$start,$end,$replace,$nogap); | ||
| 425 | return; | ||
| 426 | } | ||
| 427 | } | ||
| 428 | |||
| 429 | =head2 length | ||
| 430 | |||
| 431 | Title : length | ||
| 432 | Usage : $len = $seq->length(); | ||
| 433 | Function: Get the length of the sequence in number of symbols (bases | ||
| 434 | or amino acids). | ||
| 435 | |||
| 436 | You can also set this attribute, even to a number that does | ||
| 437 | not match the length of the sequence string. This is useful | ||
| 438 | if you don''t want to set the sequence too, or if you want | ||
| 439 | to free up memory by unsetting the sequence. In the latter | ||
| 440 | case you could do e.g. | ||
| 441 | |||
| 442 | $seq->length($seq->length); | ||
| 443 | $seq->seq(undef); | ||
| 444 | |||
| 445 | Note that if you set the sequence to a value other than | ||
| 446 | undef at any time, the length attribute will be | ||
| 447 | invalidated, and the length of the sequence string will be | ||
| 448 | reported again. Also, we won''t let you lie about the length. | ||
| 449 | |||
| 450 | Example : | ||
| 451 | Returns : integer representing the length of the sequence. | ||
| 452 | Args : Optionally, the value on set | ||
| 453 | |||
| 454 | =cut | ||
| 455 | |||
| 456 | sub length { | ||
| 457 | my $self = shift; | ||
| 458 | my $len = CORE::length($self->seq() || ''); | ||
| 459 | |||
| 460 | if(@_) { | ||
| 461 | my $val = shift; | ||
| 462 | if(defined($val) && $len && ($len != $val)) { | ||
| 463 | $self->throw("You're trying to lie about the length: ". | ||
| 464 | "is $len but you say ".$val); | ||
| 465 | } | ||
| 466 | $self->{'_seq_length'} = $val; | ||
| 467 | } elsif(defined($self->{'_seq_length'})) { | ||
| 468 | return $self->{'_seq_length'}; | ||
| 469 | } | ||
| 470 | return $len; | ||
| 471 | } | ||
| 472 | |||
| 473 | =head2 display_id | ||
| 474 | |||
| 475 | Title : display_id or display_name | ||
| 476 | Usage : $id_string = $obj->display_id(); | ||
| 477 | Function: returns the display id, aka the common name of the Sequence object. | ||
| 478 | |||
| 479 | The semantics of this is that it is the most likely string to | ||
| 480 | be used as an identifier of the sequence, and likely to have | ||
| 481 | "human" readability. The id is equivalent to the ID field of | ||
| 482 | the GenBank/EMBL databanks and the id field of the | ||
| 483 | Swissprot/sptrembl database. In fasta format, the >(\S+) is | ||
| 484 | presumed to be the id, though some people overload the id to | ||
| 485 | embed other information. Bioperl does not use any embedded | ||
| 486 | information in the ID field, and people are encouraged to use | ||
| 487 | other mechanisms (accession field for example, or extending | ||
| 488 | the sequence object) to solve this. | ||
| 489 | |||
| 490 | With the new FAST::Bio::DescribeableI interface, display_name aliases | ||
| 491 | to this method. | ||
| 492 | |||
| 493 | Returns : A string | ||
| 494 | Args : None | ||
| 495 | |||
| 496 | |||
| 497 | =cut | ||
| 498 | |||
| 499 | sub display_id { | ||
| 500 | my ($obj,$value) = @_; | ||
| 501 | if( defined $value) { | ||
| 502 | $obj->{'display_id'} = $value; | ||
| 503 | } | ||
| 504 | return $obj->{'display_id'}; | ||
| 505 | } | ||
| 506 | |||
| 507 | =head2 accession_number | ||
| 508 | |||
| 509 | Title : accession_number or object_id | ||
| 510 | Usage : $unique_key = $obj->accession_number; | ||
| 511 | Function: Returns the unique biological id for a sequence, commonly | ||
| 512 | called the accession_number. For sequences from established | ||
| 513 | databases, the implementors should try to use the correct | ||
| 514 | accession number. Notice that primary_id() provides the | ||
| 515 | unique id for the implemetation, allowing multiple objects | ||
| 516 | to have the same accession number in a particular implementation. | ||
| 517 | |||
| 518 | For sequences with no accession number, this method should | ||
| 519 | return "unknown". | ||
| 520 | |||
| 521 | [Note this method name is likely to change in 1.3] | ||
| 522 | |||
| 523 | With the new FAST::Bio::IdentifiableI interface, this is aliased | ||
| 524 | to object_id | ||
| 525 | |||
| 526 | Returns : A string | ||
| 527 | Args : A string (optional) for setting | ||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | sub accession_number { | ||
| 532 | my( $obj, $acc ) = @_; | ||
| 533 | |||
| 534 | if (defined $acc) { | ||
| 535 | $obj->{'accession_number'} = $acc; | ||
| 536 | } else { | ||
| 537 | $acc = $obj->{'accession_number'}; | ||
| 538 | $acc = 'unknown' unless defined $acc; | ||
| 539 | } | ||
| 540 | return $acc; | ||
| 541 | } | ||
| 542 | |||
| 543 | =head2 primary_id | ||
| 544 | |||
| 545 | Title : primary_id | ||
| 546 | Usage : $unique_key = $obj->primary_id; | ||
| 547 | Function: Returns the unique id for this object in this | ||
| 548 | implementation. This allows implementations to manage their | ||
| 549 | own object ids in a way the implementaiton can control | ||
| 550 | clients can expect one id to map to one object. | ||
| 551 | |||
| 552 | For sequences with no natural primary id, this method | ||
| 553 | should return a stringified memory location. | ||
| 554 | |||
| 555 | Returns : A string | ||
| 556 | Args : A string (optional, for setting) | ||
| 557 | |||
| 558 | =cut | ||
| 559 | |||
| 560 | sub primary_id { | ||
| 561 | my $obj = shift; | ||
| 562 | |||
| 563 | if(@_) { | ||
| 564 | $obj->{'primary_id'} = shift; | ||
| 565 | } | ||
| 566 | if( ! defined($obj->{'primary_id'}) ) { | ||
| 567 | return "$obj"; | ||
| 568 | } | ||
| 569 | return $obj->{'primary_id'}; | ||
| 570 | } | ||
| 571 | |||
| 572 | |||
| 573 | =head2 alphabet | ||
| 574 | |||
| 575 | Title : alphabet | ||
| 576 | Usage : if( $obj->alphabet eq 'dna' ) { /Do Something/ } | ||
| 577 | Function: Get/Set the alphabet of sequence, one of | ||
| 578 | 'dna', 'rna' or 'protein'. This is case sensitive. | ||
| 579 | |||
| 580 | This is not called <type> because this would cause | ||
| 581 | upgrade problems from the 0.5 and earlier Seq objects. | ||
| 582 | |||
| 583 | Returns : a string either 'dna','rna','protein'. NB - the object must | ||
| 584 | make a call of the type - if there is no alphabet specified it | ||
| 585 | has to guess. | ||
| 586 | Args : optional string to set : 'dna' | 'rna' | 'protein' | ||
| 587 | |||
| 588 | |||
| 589 | =cut | ||
| 590 | |||
| 591 | sub alphabet { | ||
| 592 | my ($obj,$value) = @_; | ||
| 593 | if (defined $value) { | ||
| 594 | $value = lc $value; | ||
| 595 | unless ( $valid_type{$value} ) { | ||
| 596 | $obj->throw("Alphabet '$value' is not a valid alphabet (". | ||
| 597 | join(',', map "'$_'", sort keys %valid_type) .") lowercase"); | ||
| 598 | } | ||
| 599 | $obj->{'alphabet'} = $value; | ||
| 600 | } | ||
| 601 | return $obj->{'alphabet'}; | ||
| 602 | } | ||
| 603 | |||
| 604 | =head2 desc | ||
| 605 | |||
| 606 | Title : desc or description | ||
| 607 | Usage : $obj->desc($newval) | ||
| 608 | Function: Get/set description of the sequence. | ||
| 609 | |||
| 610 | 'description' is an alias for this for compliance with the | ||
| 611 | FAST::Bio::DescribeableI interface. | ||
| 612 | |||
| 613 | Example : | ||
| 614 | Returns : value of desc (a string) | ||
| 615 | Args : newvalue (a string or undef, optional) | ||
| 616 | |||
| 617 | |||
| 618 | =cut | ||
| 619 | |||
| 620 | sub desc{ | ||
| 621 | my $self = shift; | ||
| 622 | |||
| 623 | return $self->{'desc'} = shift if @_; | ||
| 624 | return $self->{'desc'}; | ||
| 625 | } | ||
| 626 | |||
| 627 | =head2 can_call_new | ||
| 628 | |||
| 629 | Title : can_call_new | ||
| 630 | Usage : | ||
| 631 | Function: | ||
| 632 | Example : | ||
| 633 | Returns : true | ||
| 634 | Args : | ||
| 635 | |||
| 636 | |||
| 637 | =cut | ||
| 638 | |||
| 639 | sub can_call_new { | ||
| 640 | my ($self) = @_; | ||
| 641 | |||
| 642 | return 1; | ||
| 643 | } | ||
| 644 | |||
| 645 | =head2 id | ||
| 646 | |||
| 647 | Title : id | ||
| 648 | Usage : $id = $seq->id() | ||
| 649 | Function: This is mapped on display_id | ||
| 650 | Example : | ||
| 651 | Returns : | ||
| 652 | Args : | ||
| 653 | |||
| 654 | |||
| 655 | =cut | ||
| 656 | |||
| 657 | sub id { | ||
| 658 | return shift->display_id(@_); | ||
| 659 | } | ||
| 660 | |||
| 661 | =head2 is_circular | ||
| 662 | |||
| 663 | Title : is_circular | ||
| 664 | Usage : if( $obj->is_circular) { /Do Something/ } | ||
| 665 | Function: Returns true if the molecule is circular | ||
| 666 | Returns : Boolean value | ||
| 667 | Args : none | ||
| 668 | |||
| 669 | =cut | ||
| 670 | |||
| 671 | sub is_circular{ | ||
| 672 | my $self = shift; | ||
| 673 | |||
| 674 | return $self->{'is_circular'} = shift if @_; | ||
| 675 | return $self->{'is_circular'}; | ||
| 676 | } | ||
| 677 | |||
| 678 | |||
| 679 | =head1 Methods for FAST::Bio::IdentifiableI compliance | ||
| 680 | |||
| 681 | =cut | ||
| 682 | |||
| 683 | =head2 object_id | ||
| 684 | |||
| 685 | Title : object_id | ||
| 686 | Usage : $string = $obj->object_id() | ||
| 687 | Function: A string which represents the stable primary identifier | ||
| 688 | in this namespace of this object. For DNA sequences this | ||
| 689 | is its accession_number, similarly for protein sequences. | ||
| 690 | |||
| 691 | This is aliased to accession_number(). | ||
| 692 | Returns : A scalar | ||
| 693 | |||
| 694 | |||
| 695 | =cut | ||
| 696 | |||
| 697 | sub object_id { | ||
| 698 | return shift->accession_number(@_); | ||
| 699 | } | ||
| 700 | |||
| 701 | =head2 version | ||
| 702 | |||
| 703 | Title : version | ||
| 704 | Usage : $version = $obj->version() | ||
| 705 | Function: A number which differentiates between versions of | ||
| 706 | the same object. Higher numbers are considered to be | ||
| 707 | later and more relevant, but a single object described | ||
| 708 | the same identifier should represent the same concept. | ||
| 709 | |||
| 710 | Returns : A number | ||
| 711 | |||
| 712 | =cut | ||
| 713 | |||
| 714 | sub version{ | ||
| 715 | my ($self,$value) = @_; | ||
| 716 | if( defined $value) { | ||
| 717 | $self->{'_version'} = $value; | ||
| 718 | } | ||
| 719 | return $self->{'_version'}; | ||
| 720 | } | ||
| 721 | |||
| 722 | |||
| 723 | =head2 authority | ||
| 724 | |||
| 725 | Title : authority | ||
| 726 | Usage : $authority = $obj->authority() | ||
| 727 | Function: A string which represents the organisation which | ||
| 728 | granted the namespace, written as the DNS name for | ||
| 729 | organisation (eg, wormbase.org). | ||
| 730 | |||
| 731 | Returns : A scalar | ||
| 732 | |||
| 733 | =cut | ||
| 734 | |||
| 735 | sub authority { | ||
| 736 | my ($obj,$value) = @_; | ||
| 737 | if( defined $value) { | ||
| 738 | $obj->{'authority'} = $value; | ||
| 739 | } | ||
| 740 | return $obj->{'authority'}; | ||
| 741 | } | ||
| 742 | |||
| 743 | =head2 namespace | ||
| 744 | |||
| 745 | Title : namespace | ||
| 746 | Usage : $string = $obj->namespace() | ||
| 747 | Function: A string representing the name space this identifier | ||
| 748 | is valid in, often the database name or the name | ||
| 749 | describing the collection. | ||
| 750 | |||
| 751 | Returns : A scalar | ||
| 752 | |||
| 753 | |||
| 754 | =cut | ||
| 755 | |||
| 756 | sub namespace{ | ||
| 757 | my ($self,$value) = @_; | ||
| 758 | if( defined $value) { | ||
| 759 | $self->{'namespace'} = $value; | ||
| 760 | } | ||
| 761 | return $self->{'namespace'} || ""; | ||
| 762 | } | ||
| 763 | |||
| 764 | =head1 Methods for FAST::Bio::DescribableI compliance | ||
| 765 | |||
| 766 | This comprises of display_name and description. | ||
| 767 | |||
| 768 | =cut | ||
| 769 | |||
| 770 | =head2 display_name | ||
| 771 | |||
| 772 | Title : display_name | ||
| 773 | Usage : $string = $obj->display_name() | ||
| 774 | Function: A string which is what should be displayed to the user. | ||
| 775 | The string should have no spaces (ideally, though a cautious | ||
| 776 | user of this interface would not assumme this) and should be | ||
| 777 | less than thirty characters (though again, double checking | ||
| 778 | this is a good idea). | ||
| 779 | |||
| 780 | This is aliased to display_id(). | ||
| 781 | Returns : A scalar | ||
| 782 | |||
| 783 | =cut | ||
| 784 | |||
| 785 | sub display_name { | ||
| 786 | return shift->display_id(@_); | ||
| 787 | } | ||
| 788 | |||
| 789 | =head2 description | ||
| 790 | |||
| 791 | Title : description | ||
| 792 | Usage : $string = $obj->description() | ||
| 793 | Function: A text string suitable for displaying to the user a | ||
| 794 | description. This string is likely to have spaces, but | ||
| 795 | should not have any newlines or formatting - just plain | ||
| 796 | text. The string should not be greater than 255 characters | ||
| 797 | and clients can feel justified at truncating strings at 255 | ||
| 798 | characters for the purposes of display. | ||
| 799 | |||
| 800 | This is aliased to desc(). | ||
| 801 | Returns : A scalar | ||
| 802 | |||
| 803 | =cut | ||
| 804 | |||
| 805 | sub description { | ||
| 806 | return shift->desc(@_); | ||
| 807 | } | ||
| 808 | |||
| 809 | =head1 Methods Inherited from FAST::Bio::PrimarySeqI | ||
| 810 | |||
| 811 | These methods are available on FAST::Bio::PrimarySeq, although they are | ||
| 812 | actually implemented on FAST::Bio::PrimarySeqI | ||
| 813 | |||
| 814 | =head2 revcom | ||
| 815 | |||
| 816 | Title : revcom | ||
| 817 | Usage : $rev = $seq->revcom() | ||
| 818 | Function: Produces a new FAST::Bio::SeqI implementing object which | ||
| 819 | is the reversed complement of the sequence. For protein | ||
| 820 | sequences this throws an exception of | ||
| 821 | "Sequence is a protein. Cannot revcom". | ||
| 822 | |||
| 823 | The id is the same id as the orginal sequence, and the | ||
| 824 | accession number is also indentical. If someone wants to | ||
| 825 | track that this sequence has be reversed, it needs to | ||
| 826 | define its own extensions. | ||
| 827 | |||
| 828 | To do an inplace edit of an object you can go: | ||
| 829 | |||
| 830 | $seqobj = $seqobj->revcom(); | ||
| 831 | |||
| 832 | This of course, causes Perl to handle the garbage | ||
| 833 | collection of the old object, but it is roughly speaking as | ||
| 834 | efficient as an inplace edit. | ||
| 835 | |||
| 836 | Returns : A new (fresh) FAST::Bio::SeqI object | ||
| 837 | Args : none | ||
| 838 | |||
| 839 | =cut | ||
| 840 | |||
| 841 | =head2 trunc | ||
| 842 | |||
| 843 | Title : trunc | ||
| 844 | Usage : $subseq = $myseq->trunc(10,100); | ||
| 845 | Function: Provides a truncation of a sequence, | ||
| 846 | |||
| 847 | Example : | ||
| 848 | Returns : A fresh FAST::Bio::SeqI implementing object. | ||
| 849 | Args : | ||
| 850 | |||
| 851 | |||
| 852 | =cut | ||
| 853 | |||
| 854 | =head1 Internal methods | ||
| 855 | |||
| 856 | These are internal methods to PrimarySeq | ||
| 857 | |||
| 858 | =cut | ||
| 859 | |||
| 860 | =head2 _guess_alphabet | ||
| 861 | |||
| 862 | Title : _guess_alphabet | ||
| 863 | Usage : | ||
| 864 | Function: Automatically guess and set the type of sequence: dna, rna or protein | ||
| 865 | Example : | ||
| 866 | Returns : one of strings 'dna', 'rna' or 'protein'. | ||
| 867 | Args : none | ||
| 868 | |||
| 869 | |||
| 870 | =cut | ||
| 871 | |||
| 872 | sub _guess_alphabet { | ||
| 873 | my ($self) = @_; | ||
| 874 | my $type; | ||
| 875 | |||
| 876 | # Remove char's that clearly don't denote nucleic or amino acids | ||
| 877 | my $str = $self->seq(); | ||
| 878 | $str =~ s/[-.?]//gi; | ||
| 879 | |||
| 880 | # Check for sequences without valid letters | ||
| 881 | my $total = CORE::length($str); | ||
| 882 | if( $total == 0 ) { | ||
| 883 | if (!$self->{'_nowarnonempty'}) { | ||
| 884 | $self->warn("Got a sequence with no letters in it ". | ||
| 885 | "cannot guess alphabet"); | ||
| 886 | } | ||
| 887 | return ''; | ||
| 888 | } | ||
| 889 | |||
| 890 | if ($str =~ m/[EFIJLOPQXZ]/i) { | ||
| 891 | # Start with a safe method to find proteins. | ||
| 892 | # Unambiguous IUPAC letters for proteins are: E,F,I,J,L,O,P,Q,X,Z | ||
| 893 | $type = 'protein'; | ||
| 894 | } else { | ||
| 895 | # Alphabet is unsure, could still be DNA, RNA or protein. | ||
| 896 | # DNA and RNA contain mostly A, T, U, G, C and N, but the other letters | ||
| 897 | # they use are also among the 15 valid letters that a protein sequence | ||
| 898 | # can contain at this stage. Make our best guess based on sequence | ||
| 899 | # composition. If it contains over 70% of ACGTUN, it is likely nucleic. | ||
| 900 | if( ($str =~ tr/ATUGCNatugcn//) / $total > 0.7 ) { | ||
| 901 | if ( $str =~ m/U/i ) { | ||
| 902 | $type = 'rna'; | ||
| 903 | } else { | ||
| 904 | $type = 'dna'; | ||
| 905 | } | ||
| 906 | } else { | ||
| 907 | $type = 'protein'; | ||
| 908 | } | ||
| 909 | } | ||
| 910 | |||
| 911 | $self->alphabet($type); | ||
| 912 | return $type; | ||
| 913 | } | ||
| 914 | |||
| 915 | ############################################################################ | ||
| 916 | # aliases due to name changes or to compensate for our lack of consistency # | ||
| 917 | ############################################################################ | ||
| 918 | |||
| 919 | sub accession { | ||
| 920 | my $self = shift; | ||
| 921 | |||
| 922 | $self->warn(ref($self)."::accession is deprecated, ". | ||
| 923 | "use accession_number() instead"); | ||
| 924 | return $self->accession_number(@_); | ||
| 925 | } | ||
| 926 | |||
| 927 | 1; | ||
| 928 |