| 1 | # $Id: SeqStats.pm,v 1.10.2.1 2001/03/03 08:28:58 heikki Exp $ | ||
| 2 | # | ||
| 3 | # BioPerl module for FAST::Bio::Tools::SeqStats | ||
| 4 | # | ||
| 5 | # Cared for by | ||
| 6 | # | ||
| 7 | # Copyright Peter Schattner | ||
| 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::Tools::SeqStats - Object holding statistics for one particular sequence | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | # build a primary nucleic acid or protein sequence object somehow | ||
| 20 | # then build a statistics object from the sequence object | ||
| 21 | |||
| 22 | $seqobj = FAST::Bio::PrimarySeq->new(-seq=>'ACTGTGGCGTCAACTG', | ||
| 23 | -moltype = 'dna', -id = 'test'); | ||
| 24 | $seq_stats = FAST::Bio::Tools::SeqStats->new(-seq=>$seqobj); | ||
| 25 | |||
| 26 | # obtain a hash of counts of each type of monomer | ||
| 27 | # (ie amino or nucleic acid) | ||
| 28 | |||
| 29 | $hash_ref = $seq_stats->count_monomers(); # eg for DNA sequence | ||
| 30 | foreach $base ( sort keys $$hash_ref) { | ||
| 31 | print "Number of bases of type ",$base "= ",%$hash_ref{$base}"\n"; | ||
| 32 | } | ||
| 33 | # or obtain the count directly without creating a new statistics object | ||
| 34 | $hash_ref = FAST::Bio::Tools::SeqStats->count_monomers($seqobj); | ||
| 35 | foreach $base ( sort keys $$hash_ref) { | ||
| 36 | print "Number of bases of type ",$base "= ",%$hash_ref{$base}"\n"; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | # obtain hash of counts of each type of codon in a nucleic acid sequence | ||
| 41 | $hash_ref = $seq_stats-> count_codons(); # for nucleic acid sequence | ||
| 42 | # or | ||
| 43 | $hash_ref = FAST::Bio::Tools::SeqStats->count_codons($seqobj); | ||
| 44 | |||
| 45 | |||
| 46 | # Obtain the molecular weight of a sequence. Since the sequence may contain | ||
| 47 | # ambiguous monomers, the molecular weight is returned as a (reference to) a | ||
| 48 | # two element array containing greatest lower bound (GLB) and least upper bound | ||
| 49 | # (LUB) of the molecular weight | ||
| 50 | |||
| 51 | $weight = $seq_stats->get_mol_wt(); | ||
| 52 | # or | ||
| 53 | $weight = FAST::Bio::Tools::SeqStats->get_mol_wt($seqobj); | ||
| 54 | print "Molecular weight of sequence ", $seqobj->id(), | ||
| 55 | " is greater than ", $$weight[0], " and less than " , | ||
| 56 | $$weight[1], "\n"; | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | FAST::Bio::Tools::SeqStats is a lightweight object for the calculation of | ||
| 61 | simple statistical and numerical properties of a sequence. By | ||
| 62 | "lightweight" I mean that only "primary" sequences are handled by the | ||
| 63 | object. The calling script needs to create the appropriate primary | ||
| 64 | sequence to be passed to SeqStats if statistics on a sequence feature | ||
| 65 | are required. Similarly if a codon count is desired for a | ||
| 66 | frame-shifted sequence and/or a negative strand sequence, the calling | ||
| 67 | script needs to create that sequence and pass it to the SeqStats | ||
| 68 | object. | ||
| 69 | |||
| 70 | SeqStats can be called in two distinct manners. If only a single | ||
| 71 | computation is required on a given sequence object, the method can be | ||
| 72 | called easily using the SeqStats object directly: | ||
| 73 | |||
| 74 | $weight = FAST::Bio::Tools::SeqStats->get_mol_wt($seqobj); | ||
| 75 | |||
| 76 | Alternately, if several computations will be required on a given | ||
| 77 | sequence object, an "instance" statistics object can be constructed | ||
| 78 | and used for the method calls: | ||
| 79 | |||
| 80 | $seq_stats = FAST::Bio::Tools::SeqStats->new($seqobj); | ||
| 81 | $monomers = $seq_stats->count_monomers(); | ||
| 82 | $codons = $seq_stats->count_codons(); | ||
| 83 | $weight = $seq_stats->get_mol_wt(); | ||
| 84 | |||
| 85 | As currently implemented the object can return the following values from a sequence: | ||
| 86 | * The molecular weight of the sequence: get_mol_wt() | ||
| 87 | * The number of each type of monomer present: count_monomers() | ||
| 88 | * The number of each codon present in a nucleic acid sequence: count_codons() | ||
| 89 | |||
| 90 | For dna (and rna) sequences, single-stranded weights are returned. The | ||
| 91 | molecular weights are calculated for neutral - ie not ionized - | ||
| 92 | nucleic acids. The returned weight is the sum of the | ||
| 93 | base-sugar-phosphate residues of the chain plus one weight of water to | ||
| 94 | to account for the additional OH on the phosphate of the 5' residue | ||
| 95 | and the additional H on the sugar ring of the 3' residue. Note that | ||
| 96 | this leads to a difference of 18 in calculated molecular weights | ||
| 97 | compared to some other available programs (eg Informax VectorNTI). | ||
| 98 | |||
| 99 | |||
| 100 | Note that since sequences may contain ambiguous monomers (eg "M" | ||
| 101 | meaning "A" or "C" in a nucleic acid sequence), the method get_mol_wt | ||
| 102 | returns a two-element array containing the greatest lower bound and | ||
| 103 | least upper bound of the molecule. (For a sequence with no ambiguous | ||
| 104 | monomers, the two elements of the returned array will be equal.) The | ||
| 105 | method count_codons() handles ambiguous bases by simply counting all | ||
| 106 | ambiguous codons together and issuing a warning to that effect. | ||
| 107 | |||
| 108 | |||
| 109 | =head1 DEVELOPERS NOTES | ||
| 110 | |||
| 111 | Ewan moved it from FAST::Bio::SeqStats to FAST::Bio::Tools::SeqStats | ||
| 112 | |||
| 113 | =head1 FEEDBACK | ||
| 114 | |||
| 115 | =head2 Mailing Lists | ||
| 116 | |||
| 117 | User feedback is an integral part of the evolution of this and other | ||
| 118 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 119 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 120 | |||
| 121 | bioperl-l@bioperl.org - General discussion | ||
| 122 | http://bio.perl.org/MailList.html - About the mailing lists | ||
| 123 | |||
| 124 | =head2 Reporting Bugs | ||
| 125 | |||
| 126 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 127 | the bugs and their resolution. | ||
| 128 | Bug reports can be submitted via email or the web: | ||
| 129 | |||
| 130 | bioperl-bugs@bio.perl.org | ||
| 131 | http://bio.perl.org/bioperl-bugs/ | ||
| 132 | |||
| 133 | =head1 AUTHOR - Peter Schattner | ||
| 134 | |||
| 135 | Email schattner@alum.mit.edu | ||
| 136 | |||
| 137 | =head1 APPENDIX | ||
| 138 | |||
| 139 | The rest of the documentation details each of the object methods. Internal methods are | ||
| 140 | usually preceded with a _ | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | |||
| 145 | package FAST::Bio::Tools::MySeqStats; | ||
| 146 | use strict; | ||
| 147 | use vars qw(@ISA %Alphabets %Alphabets_strict $amino_weights | ||
| 148 | $rna_weights $dna_weights %Weights ); | ||
| 149 | use FAST::Bio::Seq; | ||
| 150 | use FAST::Bio::Root::Root; | ||
| 151 | @ISA = qw(FAST::Bio::Root::Root); | ||
| 152 | |||
| 153 | BEGIN { | ||
| 154 | %Alphabets = ( | ||
| 155 | 'dna' => [ qw(A C G T R Y M K S W H B V D X N) ], | ||
| 156 | 'rna' => [ qw(A C G U R Y M K S W H B V D X N) ], | ||
| 157 | 'protein' => [ qw(A R N D C Q E G H I L K M F | ||
| 158 | P S T W X Y V B Z *) ], # sac: added B, Z | ||
| 159 | ); | ||
| 160 | |||
| 161 | # SAC: new strict alphabet: doesn't allow any ambiguity characters. | ||
| 162 | %Alphabets_strict = ( | ||
| 163 | 'dna' => [ qw( A C G T ) ], | ||
| 164 | 'rna' => [ qw( A C G U ) ], | ||
| 165 | 'protein' => [ qw(A R N D C Q E G H I L K M F | ||
| 166 | P S T W Y V) ], | ||
| 167 | ); | ||
| 168 | |||
| 169 | |||
| 170 | # IUPAC-IUB SYMBOLS FOR NUCLEOTIDE NOMENCLATURE: | ||
| 171 | # Cornish-Bowden (1985) Nucl. Acids Res. 13: 3021-3030. | ||
| 172 | |||
| 173 | # Amino Acid alphabet | ||
| 174 | |||
| 175 | # ------------------------------------------ | ||
| 176 | # Symbol Meaning | ||
| 177 | # ------------------------------------------ | ||
| 178 | |||
| 179 | my $amino_A_wt = 89.09; | ||
| 180 | my $amino_C_wt = 121.15; | ||
| 181 | my $amino_D_wt = 133.1; | ||
| 182 | my $amino_E_wt = 147.13; | ||
| 183 | my $amino_F_wt = 165.19; | ||
| 184 | my $amino_G_wt = 75.07; | ||
| 185 | my $amino_H_wt = 155.16; | ||
| 186 | my $amino_I_wt = 131.18; | ||
| 187 | my $amino_K_wt = 146.19; | ||
| 188 | my $amino_L_wt = 131.18; | ||
| 189 | my $amino_M_wt = 149.22; | ||
| 190 | my $amino_N_wt = 132.12; | ||
| 191 | my $amino_P_wt = 115.13; | ||
| 192 | my $amino_Q_wt = 146.15; | ||
| 193 | my $amino_R_wt = 174.21; | ||
| 194 | my $amino_S_wt = 105.09; | ||
| 195 | my $amino_T_wt = 119.12; | ||
| 196 | my $amino_V_wt = 117.15; | ||
| 197 | my $amino_W_wt = 204.22; | ||
| 198 | my $amino_Y_wt = 181.19; | ||
| 199 | |||
| 200 | $amino_weights = { | ||
| 201 | 'A' => [$amino_A_wt, $amino_A_wt], # Alanine | ||
| 202 | 'B' => [$amino_N_wt, $amino_D_wt], # Aspartic Acid, Asparagine | ||
| 203 | 'C' => [$amino_C_wt, $amino_C_wt], # Cystine | ||
| 204 | 'D' => [$amino_D_wt, $amino_D_wt], # Aspartic Acid | ||
| 205 | 'E' => [$amino_E_wt, $amino_E_wt], # Glutamic Acid | ||
| 206 | 'F' => [$amino_F_wt, $amino_F_wt], # Phenylalanine | ||
| 207 | 'G' => [$amino_G_wt, $amino_G_wt], # Glycine | ||
| 208 | 'H' => [$amino_H_wt, $amino_H_wt], # Histidine | ||
| 209 | 'I' => [$amino_I_wt, $amino_I_wt], # Isoleucine | ||
| 210 | 'K' => [$amino_K_wt, $amino_K_wt], # Lysine | ||
| 211 | 'L' => [$amino_L_wt, $amino_L_wt], # Leucine | ||
| 212 | 'M' => [$amino_M_wt, $amino_M_wt], # Methionine | ||
| 213 | 'N' => [$amino_N_wt, $amino_N_wt], # Asparagine | ||
| 214 | 'P' => [$amino_P_wt, $amino_P_wt], # Proline | ||
| 215 | 'Q' => [$amino_Q_wt, $amino_Q_wt], # Glutamine | ||
| 216 | 'R' => [$amino_R_wt, $amino_R_wt], # Arginine | ||
| 217 | 'S' => [$amino_S_wt, $amino_S_wt], # Serine | ||
| 218 | 'T' => [$amino_T_wt, $amino_T_wt], # Threonine | ||
| 219 | 'V' => [$amino_V_wt, $amino_V_wt], # Valine | ||
| 220 | 'W' => [$amino_W_wt, $amino_W_wt], # Tryptophan | ||
| 221 | 'X' => [$amino_G_wt, $amino_W_wt], # Unknown | ||
| 222 | 'Y' => [$amino_Y_wt, $amino_Y_wt], # Tyrosine | ||
| 223 | 'Z' => [$amino_Q_wt, $amino_E_wt], # Glutamic Acid, Glutamine | ||
| 224 | }; | ||
| 225 | |||
| 226 | # Extended Dna / Rna alphabet | ||
| 227 | use vars ( qw($C $O $N $H $P $water) ); | ||
| 228 | use vars ( qw($adenine $guanine $cytosine $thymine $uracil)); | ||
| 229 | use vars ( qw($ribose_phosphate $deoxyribose_phosphate $ppi)); | ||
| 230 | use vars ( qw($dna_A_wt $dna_C_wt $dna_G_wt $dna_T_wt | ||
| 231 | $rna_A_wt $rna_C_wt $rna_G_wt $rna_U_wt)); | ||
| 232 | use vars ( qw($dna_weights $rna_weights %Weights)); | ||
| 233 | |||
| 234 | $C = 12.01; | ||
| 235 | $O = 16.00; | ||
| 236 | $N = 14.01; | ||
| 237 | $H = 1.01; | ||
| 238 | $P = 30.97; | ||
| 239 | $water = 18.015; | ||
| 240 | |||
| 241 | $adenine = 5 * $C + 5 * $N + 5 * $H; | ||
| 242 | $guanine = 5 * $C + 5 * $N + 1 * $O + 5 * $H; | ||
| 243 | $cytosine = 4 * $C + 3 * $N + 1 * $O + 5 * $H; | ||
| 244 | $thymine = 5 * $C + 2 * $N + 2 * $O + 6 * $H; | ||
| 245 | $uracil = 4 * $C + 2 * $N + 2 * $O + 4 * $H; | ||
| 246 | |||
| 247 | $ribose_phosphate = 5 * $C + 7 * $O + 9 * $H + 1 * $P; #neutral (unionized) form | ||
| 248 | $deoxyribose_phosphate = 5 * $C + 6 * $O + 9 * $H + 1 * $P; | ||
| 249 | |||
| 250 | # the following are single strand molecular weights / base | ||
| 251 | $dna_A_wt = $adenine + $deoxyribose_phosphate - $water; | ||
| 252 | $dna_C_wt = $cytosine + $deoxyribose_phosphate - $water; | ||
| 253 | $dna_G_wt = $guanine + $deoxyribose_phosphate - $water; | ||
| 254 | $dna_T_wt = $thymine + $deoxyribose_phosphate - $water; | ||
| 255 | |||
| 256 | $rna_A_wt = $adenine + $ribose_phosphate - $water; | ||
| 257 | $rna_C_wt = $cytosine + $ribose_phosphate - $water; | ||
| 258 | $rna_G_wt = $guanine + $ribose_phosphate - $water; | ||
| 259 | $rna_U_wt = $uracil + $ribose_phosphate - $water; | ||
| 260 | |||
| 261 | $dna_weights = { | ||
| 262 | 'A' => [$dna_A_wt,$dna_A_wt], # Adenine | ||
| 263 | 'C' => [$dna_C_wt,$dna_C_wt], # Cytosine | ||
| 264 | 'G' => [$dna_G_wt,$dna_G_wt], # Guanine | ||
| 265 | 'T' => [$dna_T_wt,$dna_T_wt], # Thymine | ||
| 266 | 'M' => [$dna_C_wt,$dna_A_wt], # A or C | ||
| 267 | 'R' => [$dna_A_wt,$dna_G_wt], # A or G | ||
| 268 | 'W' => [$dna_T_wt,$dna_A_wt], # A or T | ||
| 269 | 'S' => [$dna_C_wt,$dna_G_wt], # C or G | ||
| 270 | 'Y' => [$dna_C_wt,$dna_T_wt], # C or T | ||
| 271 | 'K' => [$dna_T_wt,$dna_G_wt], # G or T | ||
| 272 | 'V' => [$dna_C_wt,$dna_G_wt], # A or C or G | ||
| 273 | 'H' => [$dna_C_wt,$dna_A_wt], # A or C or T | ||
| 274 | 'D' => [$dna_T_wt,$dna_G_wt], # A or G or T | ||
| 275 | 'B' => [$dna_C_wt,$dna_G_wt], # C or G or T | ||
| 276 | 'X' => [$dna_C_wt,$dna_G_wt], # G or A or T or C | ||
| 277 | 'N' => [$dna_C_wt,$dna_G_wt], # G or A or T or C | ||
| 278 | }; | ||
| 279 | |||
| 280 | $rna_weights = { | ||
| 281 | 'A' => [$rna_A_wt,$rna_A_wt], # Adenine | ||
| 282 | 'C' => [$rna_C_wt,$rna_C_wt], # Cytosine | ||
| 283 | 'G' => [$rna_G_wt,$rna_G_wt], # Guanine | ||
| 284 | 'U' => [$rna_U_wt,$rna_U_wt], # Uracil | ||
| 285 | 'M' => [$rna_C_wt,$rna_A_wt], # A or C | ||
| 286 | 'R' => [$rna_A_wt,$rna_G_wt], # A or G | ||
| 287 | 'W' => [$rna_U_wt,$rna_A_wt], # A or U | ||
| 288 | 'S' => [$rna_C_wt,$rna_G_wt], # C or G | ||
| 289 | 'Y' => [$rna_C_wt,$rna_U_wt], # C or U | ||
| 290 | 'K' => [$rna_U_wt,$rna_G_wt], # G or U | ||
| 291 | 'V' => [$rna_C_wt,$rna_G_wt], # A or C or G | ||
| 292 | 'H' => [$rna_C_wt,$rna_A_wt], # A or C or U | ||
| 293 | 'D' => [$rna_U_wt,$rna_G_wt], # A or G or U | ||
| 294 | 'B' => [$rna_C_wt,$rna_G_wt], # C or G or U | ||
| 295 | 'X' => [$rna_C_wt,$rna_G_wt], # G or A or U or C | ||
| 296 | 'N' => [$rna_C_wt,$rna_G_wt], # G or A or U or C | ||
| 297 | }; | ||
| 298 | |||
| 299 | %Weights = ( | ||
| 300 | 'dna' => $dna_weights, | ||
| 301 | 'rna' => $rna_weights, | ||
| 302 | 'protein' => $amino_weights, | ||
| 303 | ); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub new { | ||
| 307 | my($class,@args) = @_; | ||
| 308 | my $self = $class->SUPER::new(@args); | ||
| 309 | my ($seqobj) = $self->_rearrange([qw(SEQ)],@args); | ||
| 310 | unless ($seqobj->isa("FAST::Bio::PrimarySeqI")) { | ||
| 311 | $self->throw(" SeqStats works only on PrimarySeqI objects \n"); | ||
| 312 | } | ||
| 313 | if ( !defined $seqobj->alphabet || ! defined $Alphabets{$seqobj->alphabet}) { | ||
| 314 | $self->throw("Must have a valid alphabet defined for seq (". | ||
| 315 | join(",",keys %Alphabets)); | ||
| 316 | } | ||
| 317 | $self->{'_seqref'} = $seqobj; | ||
| 318 | # $self->{'_is_strict'} = _is_alphabet_strict($seqobj); # check the letters in the sequence | ||
| 319 | return $self; | ||
| 320 | } | ||
| 321 | |||
| 322 | =head2 count_monomers | ||
| 323 | |||
| 324 | Title : count_monomers | ||
| 325 | Usage : $rcount = $seq_stats->count_monomers(); | ||
| 326 | or $rcount = $seq_stats->FAST::Bio::Tools::SeqStats->($seqobj); | ||
| 327 | Function: Counts the number of each type of monomer (amino acid or | ||
| 328 | base) in the sequence. | ||
| 329 | Example : | ||
| 330 | Returns : Reference to a hash in which keys are letters of the | ||
| 331 | genetic alphabet used and values are number of occurrences | ||
| 332 | of the letter in the sequence. | ||
| 333 | Args : None or reference to sequence object | ||
| 334 | Throws : Throws an exception if type of sequence is unknown (ie amino | ||
| 335 | or nucleic)or if unknown letter in alphabet. Ambiguous | ||
| 336 | elements are allowed. | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub count_monomers{ | ||
| 341 | my $rcount; | ||
| 342 | my %count = (); | ||
| 343 | my $seqobj; | ||
| 344 | my $_is_strict; | ||
| 345 | my $element = ''; | ||
| 346 | my $_is_instance = 1 ; | ||
| 347 | my $self = shift @_; | ||
| 348 | my $object_argument = shift @_; | ||
| 349 | |||
| 350 | # First we need to determine if the present object is an instance | ||
| 351 | # object or if the sequence object has been passed as an argument | ||
| 352 | |||
| 353 | if (defined $object_argument) { | ||
| 354 | $_is_instance = 0; | ||
| 355 | } | ||
| 356 | |||
| 357 | # If we are using an instance object... | ||
| 358 | if ($_is_instance) { | ||
| 359 | if ($rcount = $self->{'_monomer_count'}) { | ||
| 360 | return $rcount; # return count if previously calculated | ||
| 361 | } | ||
| 362 | $_is_strict = $self->{'_is_strict'}; # retrieve "strictness" | ||
| 363 | $seqobj = $self->{'_seqref'}; | ||
| 364 | } else { | ||
| 365 | # otherwise... | ||
| 366 | $seqobj = $object_argument; | ||
| 367 | |||
| 368 | # Following two lines lead to error in "throw" routine | ||
| 369 | $seqobj->isa("FAST::Bio::PrimarySeqI") || | ||
| 370 | $self->throw(" SeqStats works only on PrimarySeqI objects \n"); | ||
| 371 | # is alphabet OK? Is it strict? | ||
| 372 | # $_is_strict = _is_alphabet_strict($seqobj); | ||
| 373 | } | ||
| 374 | |||
| 375 | # my $alphabet = $_is_strict ? $Alphabets_strict{$seqobj->alphabet} : | ||
| 376 | # $Alphabets{$seqobj->alphabet} ; # get array of allowed letters | ||
| 377 | |||
| 378 | # convert everything to upper case to be safe | ||
| 379 | my $seqstring = $seqobj->seq(); | ||
| 380 | |||
| 381 | # For each letter, count the number of times it appears in | ||
| 382 | # the sequence | ||
| 383 | LETTER: | ||
| 384 | foreach $element (split //, $seqstring) { | ||
| 385 | # skip terminator symbol which may confuse regex | ||
| 386 | # next LETTER if ($element eq '*'); | ||
| 387 | $count{$element}++; # = ( $seqstring =~ s/$element/$element/g); | ||
| 388 | } | ||
| 389 | |||
| 390 | $rcount = \%count; | ||
| 391 | |||
| 392 | if ($_is_instance) { | ||
| 393 | $self->{'_monomer_count'} = $rcount; # Save in case called again later | ||
| 394 | } | ||
| 395 | |||
| 396 | return $rcount; | ||
| 397 | } | ||
| 398 | |||
| 399 | =head2 get_mol_wt | ||
| 400 | |||
| 401 | Title : get_mol_wt | ||
| 402 | Usage : $wt = $seqobj->get_mol_wt() or | ||
| 403 | $wt = FAST::Bio::Tools::SeqStats ->get_mol_wt($seqobj); | ||
| 404 | Function: Calculate molecular weight of sequence | ||
| 405 | Example : | ||
| 406 | |||
| 407 | Returns : Reference to two element array containing lower and upper | ||
| 408 | bounds of molecule molecular weight. (For dna (and rna) | ||
| 409 | sequences, single-stranded weights are returned.) If | ||
| 410 | sequence contains no ambiguous elements, both entries in | ||
| 411 | array are equal to molecular weight of molecule. | ||
| 412 | Args : None or reference to sequence object | ||
| 413 | Throws : Exception if type of sequence is unknown (ie not amino or | ||
| 414 | nucleic) or if unknown letter in alphabet. Ambiguous | ||
| 415 | elements are allowed. | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | sub get_mol_wt { | ||
| 420 | |||
| 421 | my $seqobj; | ||
| 422 | my $_is_strict; | ||
| 423 | my $element = ''; | ||
| 424 | my $_is_instance = 1 ; | ||
| 425 | my $self = shift @_; | ||
| 426 | my $object_argument = shift @_; | ||
| 427 | my ($weight_array, $rcount); | ||
| 428 | |||
| 429 | if (defined $object_argument) { | ||
| 430 | $_is_instance = 0; | ||
| 431 | } | ||
| 432 | |||
| 433 | if ($_is_instance) { | ||
| 434 | if ($weight_array = $self->{'_mol_wt'}) { | ||
| 435 | # return mol. weight if previously calculated | ||
| 436 | return $weight_array; | ||
| 437 | } | ||
| 438 | $seqobj = $self->{'_seqref'}; | ||
| 439 | $rcount = $self->count_monomers(); | ||
| 440 | } else { | ||
| 441 | $seqobj = $object_argument; | ||
| 442 | $seqobj->isa("FAST::Bio::PrimarySeqI") || | ||
| 443 | die("Error: SeqStats works only on PrimarySeqI objects \n"); | ||
| 444 | $_is_strict = _is_alphabet_strict($seqobj); # is alphabet OK? | ||
| 445 | $rcount = $self->count_monomers($seqobj); | ||
| 446 | } | ||
| 447 | |||
| 448 | # We will also need to know what type of monomer we are dealing with | ||
| 449 | |||
| 450 | my $alphabet = $seqobj->alphabet(); | ||
| 451 | |||
| 452 | # In general,the molecular weight is bounded below by the sum of the | ||
| 453 | # weights of lower bounds of each alphabet symbol times the number of | ||
| 454 | # occurrences of the symbol in the sequence. A similar upper bound on | ||
| 455 | # the weight is also calculated. | ||
| 456 | |||
| 457 | # Note that for "strict" (ie unambiguous) sequences there is an | ||
| 458 | # inefficiency since the upper bound = the lower bound (and is | ||
| 459 | # calculated twice). However, this decrease in performance will be | ||
| 460 | # minor and leads to (IMO) significantly more readable code. | ||
| 461 | |||
| 462 | my $weight_lower_bound = 0; | ||
| 463 | my $weight_upper_bound = 0; | ||
| 464 | my $weight_table = $Weights{$alphabet}; | ||
| 465 | # my $water = 18.015; | ||
| 466 | |||
| 467 | # compute weight of all the residues | ||
| 468 | foreach $element (keys %$rcount) { | ||
| 469 | $weight_lower_bound += $$rcount{$element} * $$weight_table{$element}->[0]; | ||
| 470 | $weight_upper_bound += $$rcount{$element} * $$weight_table{$element}->[1]; | ||
| 471 | } | ||
| 472 | if ($alphabet =~ /protein/) { | ||
| 473 | # remove of H2O during peptide bond formation. | ||
| 474 | $weight_lower_bound -= $water * ($seqobj->length - 1); | ||
| 475 | $weight_upper_bound -= $water * ($seqobj->length - 1); | ||
| 476 | } else { | ||
| 477 | # Correction because phosphate of 5' residue has additional OH and | ||
| 478 | # sugar ring of 3' residue has additional H | ||
| 479 | $weight_lower_bound += $water; | ||
| 480 | $weight_upper_bound += $water; | ||
| 481 | } | ||
| 482 | |||
| 483 | $weight_lower_bound = sprintf("%.0f", $weight_lower_bound); | ||
| 484 | $weight_upper_bound = sprintf("%.0f", $weight_upper_bound); | ||
| 485 | |||
| 486 | $weight_array = [$weight_lower_bound, $weight_upper_bound]; | ||
| 487 | |||
| 488 | if ($_is_instance) { | ||
| 489 | $self->{'_mol_wt'} = $weight_array; # Save in case called again later | ||
| 490 | } | ||
| 491 | return $weight_array; | ||
| 492 | } | ||
| 493 | |||
| 494 | |||
| 495 | =head2 count_codons | ||
| 496 | |||
| 497 | Title : count_codons | ||
| 498 | Usage : $rcount = $seqstats->count_codons (); or | ||
| 499 | $rcount = FAST::Bio::Tools::SeqStats->count_codons($seqobj); | ||
| 500 | |||
| 501 | Function: Counts the number of each type of codons in a given frame | ||
| 502 | for a dna or rna sequence. | ||
| 503 | Example : | ||
| 504 | Returns : Reference to a hash in which keys are codons of the genetic | ||
| 505 | alphabet used and values are number of occurrences of the | ||
| 506 | codons in the sequence. All codons with "ambiguous" bases | ||
| 507 | are counted together. | ||
| 508 | Args : None or reference to sequence object | ||
| 509 | |||
| 510 | Throws : an exception if type of sequence is unknown or protein. | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | sub count_codons { | ||
| 515 | my $rcount = {}; | ||
| 516 | my $codon ; | ||
| 517 | my $seqobj; | ||
| 518 | my $_is_strict; | ||
| 519 | my $element = ''; | ||
| 520 | my $_is_instance = 1 ; | ||
| 521 | my $self = shift @_; | ||
| 522 | my $object_argument = shift @_; | ||
| 523 | |||
| 524 | if (defined $object_argument) { | ||
| 525 | $_is_instance = 0; | ||
| 526 | } | ||
| 527 | |||
| 528 | if ($_is_instance) { | ||
| 529 | if ($rcount = $self->{'_codon_count'}) { | ||
| 530 | return $rcount; # return count if previously calculated | ||
| 531 | } | ||
| 532 | $_is_strict = $self->{'_is_strict'}; # retrieve "strictness" | ||
| 533 | $seqobj = $self->{'_seqref'}; | ||
| 534 | } else { | ||
| 535 | $seqobj = $object_argument; | ||
| 536 | $seqobj->isa("FAST::Bio::PrimarySeqI") || | ||
| 537 | die(" Error: SeqStats works only on PrimarySeqI objects \n"); | ||
| 538 | $_is_strict = _is_alphabet_strict($seqobj); | ||
| 539 | } | ||
| 540 | |||
| 541 | # Codon counts only make sense for nucleic acid sequences | ||
| 542 | my $alphabet = $seqobj->alphabet(); | ||
| 543 | |||
| 544 | unless ($alphabet =~ /[dr]na/) { | ||
| 545 | $seqobj->throw(" Codon counts only meaningful for dna or rna, not for $alphabet sequences. \n"); | ||
| 546 | } | ||
| 547 | |||
| 548 | # If sequence contains ambiguous bases, warn that codons containing them will all be | ||
| 549 | # lumped together in the count. | ||
| 550 | |||
| 551 | if (!$_is_strict ) { | ||
| 552 | $seqobj->warn(" Sequence $seqobj contains ambiguous bases. \n All codons with ambiguous bases will be added together in count. \n"); | ||
| 553 | } | ||
| 554 | |||
| 555 | my $seq = $seqobj->seq(); | ||
| 556 | |||
| 557 | # Now step through the string by threes and count the codons | ||
| 558 | |||
| 559 | CODON: | ||
| 560 | while (length($seq) > 2) { | ||
| 561 | $codon = substr($seq,0,3); | ||
| 562 | $seq = substr($seq,3); | ||
| 563 | if ($codon =~ /[^ACTGU]/) { | ||
| 564 | $$rcount{'ambiguous'}++; #lump together ambiguous codons | ||
| 565 | next CODON; | ||
| 566 | } | ||
| 567 | if (!defined $$rcount{$codon}) { | ||
| 568 | $$rcount{$codon}= 1 ; | ||
| 569 | next CODON; | ||
| 570 | } | ||
| 571 | $$rcount{$codon}++; # default | ||
| 572 | } | ||
| 573 | |||
| 574 | |||
| 575 | if ($_is_instance) { | ||
| 576 | $self->{'_codon_count'} = $rcount; # Save in case called again later | ||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | |||
| 581 | |||
| 582 | return $rcount; | ||
| 583 | } | ||
| 584 | |||
| 585 | |||
| 586 | =head2 _is_alphabet_strict | ||
| 587 | |||
| 588 | Title : _is_alphabet_strict | ||
| 589 | Usage : | ||
| 590 | Function: internal function to determine whether there are | ||
| 591 | any ambiguous elements in the current sequence | ||
| 592 | Example : | ||
| 593 | Returns : 1 if strict alphabet is being used, | ||
| 594 | 0 if ambiguous elements are present | ||
| 595 | Args : | ||
| 596 | |||
| 597 | Throws : an exception if type of sequence is unknown (ie amino or | ||
| 598 | nucleic) or if unknown letter in alphabet. Ambiguous | ||
| 599 | monomers are allowed. | ||
| 600 | |||
| 601 | =cut | ||
| 602 | |||
| 603 | sub _is_alphabet_strict { | ||
| 604 | |||
| 605 | my ($seqobj) = @_; | ||
| 606 | my $alphabet = $seqobj->alphabet(); | ||
| 607 | # convert everything to upper case to be safe | ||
| 608 | my $seqstring = uc $seqobj->seq(); | ||
| 609 | |||
| 610 | # First we check if only the 'strict' letters are present in the | ||
| 611 | # sequence string If not, we check whether the remaining letters are | ||
| 612 | # ambiguous monomers or whether there are illegal letters in the | ||
| 613 | # string | ||
| 614 | |||
| 615 | # $alpha_array is a ref to an array of the 'strictly' allowed letters | ||
| 616 | my $alpha_array = $Alphabets_strict{$alphabet} ; | ||
| 617 | |||
| 618 | # $alphabet contains the allowed letters in string form | ||
| 619 | $alphabet = join ('', @$alpha_array) ; | ||
| 620 | |||
| 621 | unless ($seqstring =~ /[^$alphabet]/) { | ||
| 622 | return 1 ; | ||
| 623 | } | ||
| 624 | # Next try to match with the alphabet's ambiguous letters | ||
| 625 | |||
| 626 | $alpha_array = $Alphabets{$alphabet} ; | ||
| 627 | $alphabet = join ('', @$alpha_array) ; | ||
| 628 | |||
| 629 | unless ($seqstring =~ /[^$alphabet]/) { | ||
| 630 | return 0 ; | ||
| 631 | } | ||
| 632 | |||
| 633 | # If we got here there is an illegal letter in the sequence | ||
| 634 | |||
| 635 | $seqobj->throw(" Alphabet not OK for $seqobj \n"); | ||
| 636 | |||
| 637 | } | ||
| 638 | |||
| 639 | =head2 _print_data | ||
| 640 | |||
| 641 | Title : _print_data | ||
| 642 | Usage : $seqobj->_print_data() or FAST::Bio::Tools::SeqStats->_print_data(); | ||
| 643 | Function: Displays dna / rna parameters (used for debugging) | ||
| 644 | Returns : 1 | ||
| 645 | Args : None | ||
| 646 | |||
| 647 | Used for debugging. | ||
| 648 | |||
| 649 | =cut | ||
| 650 | |||
| 651 | sub _print_data { | ||
| 652 | |||
| 653 | print "\n adenine = : $adenine \n"; | ||
| 654 | print "\n guanine = : $guanine \n"; | ||
| 655 | print "\n cytosine = : $cytosine \n"; | ||
| 656 | print "\n thymine = : $thymine \n"; | ||
| 657 | print "\n uracil = : $uracil \n"; | ||
| 658 | |||
| 659 | print "\n dna_A_wt = : $dna_A_wt \n"; | ||
| 660 | print "\n dna_C_wt = : $dna_C_wt \n"; | ||
| 661 | print "\n dna_G_wt = : $dna_G_wt \n"; | ||
| 662 | print "\n dna_T_wt = : $dna_T_wt \n"; | ||
| 663 | |||
| 664 | print "\n rna_A_wt = : $rna_A_wt \n"; | ||
| 665 | print "\n rna_C_wt = : $rna_C_wt \n"; | ||
| 666 | print "\n rna_G_wt = : $rna_G_wt \n"; | ||
| 667 | print "\n rna_U_wt = : $rna_U_wt \n"; | ||
| 668 | |||
| 669 | return 1; | ||
| 670 | } |