| 1 | # | ||
| 2 | # bioperl module for FAST::Bio::PrimaryQual | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Chad Matsalla <bioinformatics@dieselwurks.com> | ||
| 7 | # | ||
| 8 | # Copyright Chad Matsalla | ||
| 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::Seq::PrimaryQual - Bioperl lightweight Quality Object | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use FAST::Bio::Seq::PrimaryQual; | ||
| 21 | |||
| 22 | # you can use either a space-delimited string for quality | ||
| 23 | |||
| 24 | my $string_quals = "10 20 30 40 50 40 30 20 10"; | ||
| 25 | my $qualobj = FAST::Bio::Seq::PrimaryQual->new | ||
| 26 | ( '-qual' => $string_quals, | ||
| 27 | '-id' => 'QualityFragment-12', | ||
| 28 | '-accession_number' => 'X78121', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # _or_ you can use an array of quality values | ||
| 32 | |||
| 33 | my @q2 = split/ /,$string_quals; | ||
| 34 | $qualobj = FAST::Bio::Seq::PrimaryQual->new( '-qual' => \@q2, | ||
| 35 | '-primary_id' => 'chads primary_id', | ||
| 36 | '-desc' => 'chads desc', | ||
| 37 | '-accession_number' => 'chads accession_number', | ||
| 38 | '-id' => 'chads id' | ||
| 39 | ); | ||
| 40 | |||
| 41 | # to get the quality values out: | ||
| 42 | |||
| 43 | my @quals = @{$qualobj->qual()}; | ||
| 44 | |||
| 45 | # to give _new_ quality values | ||
| 46 | |||
| 47 | my $newqualstring = "50 90 1000 20 12 0 0"; | ||
| 48 | $qualobj->qual($newqualstring); | ||
| 49 | |||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | This module provides a mechanism for storing quality | ||
| 54 | values. Much more useful as part of | ||
| 55 | FAST::Bio::Seq::SeqWithQuality where these quality values | ||
| 56 | are associated with the sequence information. | ||
| 57 | |||
| 58 | =head1 FEEDBACK | ||
| 59 | |||
| 60 | =head2 Mailing Lists | ||
| 61 | |||
| 62 | User feedback is an integral part of the evolution of this and other | ||
| 63 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 64 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 65 | |||
| 66 | bioperl-l@bioperl.org - General discussion | ||
| 67 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 68 | |||
| 69 | =head2 Support | ||
| 70 | |||
| 71 | Please direct usage questions or support issues to the mailing list: | ||
| 72 | |||
| 73 | I<bioperl-l@bioperl.org> | ||
| 74 | |||
| 75 | rather than to the module maintainer directly. Many experienced and | ||
| 76 | reponsive experts will be able look at the problem and quickly | ||
| 77 | address it. Please include a thorough description of the problem | ||
| 78 | with code and data examples if at all possible. | ||
| 79 | |||
| 80 | =head2 Reporting Bugs | ||
| 81 | |||
| 82 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 83 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 84 | web: | ||
| 85 | |||
| 86 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 87 | |||
| 88 | =head1 AUTHOR - Chad Matsalla | ||
| 89 | |||
| 90 | Email bioinformatics@dieselwurks.com | ||
| 91 | |||
| 92 | =head1 APPENDIX | ||
| 93 | |||
| 94 | The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | |||
| 99 | # Let the code begin... | ||
| 100 | |||
| 101 | package FAST::Bio::Seq::PrimaryQual; | ||
| 102 | use vars qw(%valid_type); | ||
| 103 | use strict; | ||
| 104 | |||
| 105 | |||
| 106 | use base qw(FAST::Bio::Root::Root FAST::Bio::Seq::QualI); | ||
| 107 | |||
| 108 | |||
| 109 | =head2 new() | ||
| 110 | |||
| 111 | Title : new() | ||
| 112 | Usage : $qual = FAST::Bio::Seq::PrimaryQual->new | ||
| 113 | ( -qual => '10 20 30 40 50 50 20 10', | ||
| 114 | -id => 'human_id', | ||
| 115 | -accession_number => 'AL000012', | ||
| 116 | ); | ||
| 117 | |||
| 118 | Function: Returns a new FAST::Bio::Seq::PrimaryQual object from basic | ||
| 119 | constructors, being a string _or_ a reference to an array for the | ||
| 120 | sequence and strings for id and accession_number. Note that you | ||
| 121 | can provide an empty quality string. | ||
| 122 | Returns : a new FAST::Bio::Seq::PrimaryQual object | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | sub new { | ||
| 130 | my ($class, @args) = @_; | ||
| 131 | my $self = $class->SUPER::new(@args); | ||
| 132 | |||
| 133 | # default: turn ON the warnings (duh) | ||
| 134 | my($qual,$id,$acc,$pid,$desc,$given_id,$header) = | ||
| 135 | $self->_rearrange([qw(QUAL | ||
| 136 | DISPLAY_ID | ||
| 137 | ACCESSION_NUMBER | ||
| 138 | PRIMARY_ID | ||
| 139 | DESC | ||
| 140 | ID | ||
| 141 | HEADER | ||
| 142 | )], | ||
| 143 | @args); | ||
| 144 | if( defined $id && defined $given_id ) { | ||
| 145 | if( $id ne $given_id ) { | ||
| 146 | $self->throw("Provided both id and display_id constructor functions. [$id] [$given_id]"); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | if( defined $given_id ) { $id = $given_id; } | ||
| 150 | |||
| 151 | # note: the sequence string may be empty | ||
| 152 | $self->qual(defined($qual) ? $qual : []); | ||
| 153 | $header && $self->header($header); | ||
| 154 | $id && $self->display_id($id); | ||
| 155 | $acc && $self->accession_number($acc); | ||
| 156 | $pid && $self->primary_id($pid); | ||
| 157 | $desc && $self->desc($desc); | ||
| 158 | |||
| 159 | return $self; | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 qual() | ||
| 163 | |||
| 164 | Title : qual() | ||
| 165 | Usage : @quality_values = @{$obj->qual()}; | ||
| 166 | Function: Returns the quality as a reference to an array containing the | ||
| 167 | quality values. The individual elements of the quality array are | ||
| 168 | not validated and can be any numeric value. | ||
| 169 | Returns : A reference to an array. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub qual { | ||
| 174 | my ($self,$value) = @_; | ||
| 175 | |||
| 176 | if( ! defined $value || length($value) == 0 ) { | ||
| 177 | $self->{'qual'} ||= []; | ||
| 178 | } elsif( ref($value) =~ /ARRAY/i ) { | ||
| 179 | # if the user passed in a reference to an array | ||
| 180 | $self->{'qual'} = $value; | ||
| 181 | } elsif(! $self->validate_qual($value)){ | ||
| 182 | $self->throw("Attempting to set the quality to [$value] which does not look healthy"); | ||
| 183 | } else { | ||
| 184 | $value =~ s/^\s+//; | ||
| 185 | $self->{'qual'} = [split(/\s+/,$value)]; | ||
| 186 | } | ||
| 187 | |||
| 188 | return $self->{'qual'}; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | =head2 seq() | ||
| 193 | |||
| 194 | Title : seq() | ||
| 195 | Usager : $sequence = $obj->seq(); | ||
| 196 | Function : Returns the quality numbers as a space-separated string. | ||
| 197 | Returns : Single string. | ||
| 198 | Args : None. | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub seq { | ||
| 203 | join ' ', @{ shift->qual }; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | =head2 validate_qual($qualstring) | ||
| 208 | |||
| 209 | Title : validate_qual($qualstring) | ||
| 210 | Usage : print("Valid.") if { &validate_qual($self,$qualities); } | ||
| 211 | Function: Make sure that the quality, if it has length > 0, contains at | ||
| 212 | least one digit. Note that quality strings are parsed into arrays | ||
| 213 | using split/\d+/,$quality_string, so make sure that your quality | ||
| 214 | scalar looks like this if you want it to be parsed properly. | ||
| 215 | Returns : 1 for a valid sequence (WHY? Shouldn\'t it return 0? <boggle>) | ||
| 216 | Args : a scalar (any scalar, why PrimarySeq author?) and a scalar | ||
| 217 | containing the string to validate. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | sub validate_qual { | ||
| 222 | # how do I validate quality values? | ||
| 223 | # \d+\s+\d+..., I suppose | ||
| 224 | my ($self,$qualstr) = @_; | ||
| 225 | # why the CORE?? -- (Because FAST::Bio::PrimarySeqI namespace has a | ||
| 226 | # length method, you have to qualify | ||
| 227 | # which length to use) | ||
| 228 | return 0 if (!defined $qualstr || CORE::length($qualstr) <= 0); | ||
| 229 | return 1 if( $qualstr =~ /\d/); | ||
| 230 | |||
| 231 | return 0; | ||
| 232 | } | ||
| 233 | |||
| 234 | =head2 subqual($start,$end) | ||
| 235 | |||
| 236 | Title : subqual($start,$end) | ||
| 237 | Usage : @subset_of_quality_values = @{$obj->subqual(10,40)}; | ||
| 238 | Function: returns the quality values from $start to $end, where the | ||
| 239 | first value is 1 and the number is inclusive, ie 1-2 are the | ||
| 240 | first two bases of the sequence. Start cannot be larger than | ||
| 241 | end but can be equal. | ||
| 242 | Returns : A reference to an array. | ||
| 243 | Args : a start position and an end position | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | |||
| 248 | sub subqual { | ||
| 249 | my ($self,$start,$end) = @_; | ||
| 250 | |||
| 251 | if( $start > $end ){ | ||
| 252 | $self->throw("in subqual, start [$start] has to be greater than end [$end]"); | ||
| 253 | } | ||
| 254 | |||
| 255 | if( $start <= 0 || $end > $self->length ) { | ||
| 256 | $self->throw("You have to have start positive and length less than the total length of sequence [$start:$end] Total ".$self->length.""); | ||
| 257 | } | ||
| 258 | |||
| 259 | # remove one from start, and then length is end-start | ||
| 260 | |||
| 261 | $start--; | ||
| 262 | $end--; | ||
| 263 | my @sub_qual_array = @{$self->{qual}}[$start..$end]; | ||
| 264 | |||
| 265 | # return substr $self->seq(), $start, ($end-$start); | ||
| 266 | return \@sub_qual_array; | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | =head2 display_id() | ||
| 271 | |||
| 272 | Title : display_id() | ||
| 273 | Usage : $id_string = $obj->display_id(); | ||
| 274 | Function: returns the display id, aka the common name of the Quality | ||
| 275 | object. | ||
| 276 | The semantics of this is that it is the most likely string to be | ||
| 277 | used as an identifier of the quality sequence, and likely to have | ||
| 278 | "human" readability. The id is equivalent to the ID field of the | ||
| 279 | GenBank/EMBL databanks and the id field of the Swissprot/sptrembl | ||
| 280 | database. In fasta format, the >(\S+) is presumed to be the id, | ||
| 281 | though some people overload the id to embed other information. | ||
| 282 | Bioperl does not use any embedded information in the ID field, | ||
| 283 | and people are encouraged to use other mechanisms (accession | ||
| 284 | field for example, or extending the sequence object) to solve | ||
| 285 | this. Notice that $seq->id() maps to this function, mainly for | ||
| 286 | legacy/convience issues | ||
| 287 | Returns : A string | ||
| 288 | Args : None | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub display_id { | ||
| 293 | my ($obj,$value) = @_; | ||
| 294 | if( defined $value) { | ||
| 295 | $obj->{'display_id'} = $value; | ||
| 296 | } | ||
| 297 | return $obj->{'display_id'}; | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | =head2 header() | ||
| 302 | |||
| 303 | Title : header() | ||
| 304 | Usage : $header = $obj->header(); | ||
| 305 | Function: Get/set the header that the user wants printed for this | ||
| 306 | quality object. | ||
| 307 | Returns : A string | ||
| 308 | Args : None | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | sub header { | ||
| 313 | my ($obj,$value) = @_; | ||
| 314 | if( defined $value) { | ||
| 315 | $obj->{'header'} = $value; | ||
| 316 | } | ||
| 317 | return $obj->{'header'}; | ||
| 318 | |||
| 319 | } | ||
| 320 | |||
| 321 | =head2 accession_number() | ||
| 322 | |||
| 323 | Title : accession_number() | ||
| 324 | Usage : $unique_biological_key = $obj->accession_number(); | ||
| 325 | Function: Returns the unique biological id for a sequence, commonly | ||
| 326 | called the accession_number. For sequences from established | ||
| 327 | databases, the implementors should try to use the correct | ||
| 328 | accession number. Notice that primary_id() provides the unique id | ||
| 329 | for the implemetation, allowing multiple objects to have the same | ||
| 330 | accession number in a particular implementation. For sequences | ||
| 331 | with no accession number, this method should return "unknown". | ||
| 332 | Returns : A string | ||
| 333 | Args : None | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub accession_number { | ||
| 338 | my( $obj, $acc ) = @_; | ||
| 339 | |||
| 340 | if (defined $acc) { | ||
| 341 | $obj->{'accession_number'} = $acc; | ||
| 342 | } else { | ||
| 343 | $acc = $obj->{'accession_number'}; | ||
| 344 | $acc = 'unknown' unless defined $acc; | ||
| 345 | } | ||
| 346 | return $acc; | ||
| 347 | } | ||
| 348 | |||
| 349 | =head2 primary_id() | ||
| 350 | |||
| 351 | Title : primary_id() | ||
| 352 | Usage : $unique_implementation_key = $obj->primary_id(); | ||
| 353 | Function: Returns the unique id for this object in this implementation. | ||
| 354 | This allows implementations to manage their own object ids in a | ||
| 355 | way the implementaiton can control clients can expect one id to | ||
| 356 | map to one object. For sequences with no accession number, this | ||
| 357 | method should return a stringified memory location. | ||
| 358 | Returns : A string | ||
| 359 | Args : None | ||
| 360 | |||
| 361 | =cut | ||
| 362 | |||
| 363 | sub primary_id { | ||
| 364 | my ($obj,$value) = @_; | ||
| 365 | if( defined $value) { | ||
| 366 | $obj->{'primary_id'} = $value; | ||
| 367 | } | ||
| 368 | return $obj->{'primary_id'}; | ||
| 369 | |||
| 370 | } | ||
| 371 | |||
| 372 | =head2 desc() | ||
| 373 | |||
| 374 | Title : desc() | ||
| 375 | Usage : $qual->desc($newval); | ||
| 376 | $description = $qual->desc(); | ||
| 377 | Function: Get/set description text for a qual object | ||
| 378 | Example : | ||
| 379 | Returns : Value of desc | ||
| 380 | Args : newvalue (optional) | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | sub desc { | ||
| 385 | my ($obj,$value) = @_; | ||
| 386 | if( defined $value) { | ||
| 387 | $obj->{'desc'} = $value; | ||
| 388 | } | ||
| 389 | return $obj->{'desc'}; | ||
| 390 | |||
| 391 | } | ||
| 392 | |||
| 393 | =head2 id() | ||
| 394 | |||
| 395 | Title : id() | ||
| 396 | Usage : $id = $qual->id(); | ||
| 397 | Function: Return the ID of the quality. This should normally be (and | ||
| 398 | actually is in the implementation provided here) just a synonym | ||
| 399 | for display_id(). | ||
| 400 | Returns : A string. | ||
| 401 | Args : None. | ||
| 402 | |||
| 403 | =cut | ||
| 404 | |||
| 405 | sub id { | ||
| 406 | my ($self,$value) = @_; | ||
| 407 | if( defined $value ) { | ||
| 408 | return $self->display_id($value); | ||
| 409 | } | ||
| 410 | return $self->display_id(); | ||
| 411 | } | ||
| 412 | |||
| 413 | =head2 length() | ||
| 414 | |||
| 415 | Title : length() | ||
| 416 | Usage : $length = $qual->length(); | ||
| 417 | Function: Return the length of the array holding the quality values. | ||
| 418 | Under most circumstances, this should match the number of quality | ||
| 419 | values but no validation is done when the PrimaryQual object is | ||
| 420 | constructed and non-digits could be put into this array. Is this | ||
| 421 | a bug? Just enough rope... | ||
| 422 | Returns : A scalar (the number of elements in the quality array). | ||
| 423 | Args : None. | ||
| 424 | |||
| 425 | =cut | ||
| 426 | |||
| 427 | sub length { | ||
| 428 | my $self = shift; | ||
| 429 | if (ref($self->{qual}) ne "ARRAY") { | ||
| 430 | $self->warn("{qual} is not an array here. Why? It appears to be ".ref($self->{qual})."(".$self->{qual}."). Good thing this can _never_ happen."); | ||
| 431 | } | ||
| 432 | return scalar(@{$self->{qual}}); | ||
| 433 | } | ||
| 434 | |||
| 435 | =head2 qualat($position) | ||
| 436 | |||
| 437 | Title : qualat($position) | ||
| 438 | Usage : $quality = $obj->qualat(10); | ||
| 439 | Function: Return the quality value at the given location, where the | ||
| 440 | first value is 1 and the number is inclusive, ie 1-2 are the first | ||
| 441 | two bases of the sequence. Start cannot be larger than end but can | ||
| 442 | be equal. | ||
| 443 | Returns : A scalar. | ||
| 444 | Args : A position. | ||
| 445 | |||
| 446 | =cut | ||
| 447 | |||
| 448 | sub qualat { | ||
| 449 | my ($self,$val) = @_; | ||
| 450 | my @qualat = @{$self->subqual($val,$val)}; | ||
| 451 | if (scalar(@qualat) == 1) { | ||
| 452 | return $qualat[0]; | ||
| 453 | } | ||
| 454 | else { | ||
| 455 | $self->throw("AAAH! qualat provided more then one quality."); | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | =head2 to_string() | ||
| 460 | |||
| 461 | Title : to_string() | ||
| 462 | Usage : $quality = $obj->to_string(); | ||
| 463 | Function: Return a textual representation of what the object contains. | ||
| 464 | For this module, this function will return: | ||
| 465 | qual | ||
| 466 | display_id | ||
| 467 | accession_number | ||
| 468 | primary_id | ||
| 469 | desc | ||
| 470 | id | ||
| 471 | length | ||
| 472 | Returns : A scalar. | ||
| 473 | Args : None. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | sub to_string { | ||
| 478 | my ($self,$out,$result) = shift; | ||
| 479 | $out = "qual: ".join(',',@{$self->qual()}); | ||
| 480 | foreach (qw(display_id accession_number primary_id desc id length)) { | ||
| 481 | $result = $self->$_(); | ||
| 482 | if (!$result) { $result = "<unset>"; } | ||
| 483 | $out .= "$_: $result\n"; | ||
| 484 | } | ||
| 485 | return $out; | ||
| 486 | } | ||
| 487 | |||
| 488 | |||
| 489 | sub to_string_automatic { | ||
| 490 | my ($self,$sub_result,$out) = shift; | ||
| 491 | foreach (sort keys %$self) { | ||
| 492 | print("Working on $_\n"); | ||
| 493 | eval { $self->$_(); }; | ||
| 494 | if ($@) { $sub_result = ref($_); } | ||
| 495 | elsif (!($sub_result = $self->$_())) { | ||
| 496 | $sub_result = "<unset>"; | ||
| 497 | } | ||
| 498 | if (ref($sub_result) eq "ARRAY") { | ||
| 499 | print("This thing ($_) is an array!\n"); | ||
| 500 | $sub_result = join(',',@$sub_result); | ||
| 501 | } | ||
| 502 | $out .= "$_: ".$sub_result."\n"; | ||
| 503 | } | ||
| 504 | return $out; | ||
| 505 | } | ||
| 506 | |||
| 507 | 1; |