| 1 | # | ||
| 2 | # BioPerl module for IUPAC | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Aaron Mackey <amackey@virginia.edu> | ||
| 7 | # | ||
| 8 | # Copyright Aaron Mackey | ||
| 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::Tools::IUPAC - Generates unique Seq objects from an ambiguous Seq object | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use FAST::Bio::Seq; | ||
| 21 | use FAST::Bio::Tools::IUPAC; | ||
| 22 | |||
| 23 | my $ambiseq = FAST::Bio::Seq->new(-seq => 'ARTCGUTGR', -alphabet => 'dna'); | ||
| 24 | my $stream = FAST::Bio::Tools::IUPAC->new(-seq => $ambiseq); | ||
| 25 | |||
| 26 | while ($uniqueseq = $stream->next_seq()) { | ||
| 27 | # process the unique Seq object. | ||
| 28 | } | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | IUPAC is a tool that produces a stream of unique, "strict"-satisfying Seq | ||
| 33 | objects from an ambiquous Seq object (containing non-standard characters given | ||
| 34 | the meaning shown below) | ||
| 35 | |||
| 36 | Extended DNA / RNA alphabet : | ||
| 37 | (includes symbols for nucleotide ambiguity) | ||
| 38 | ------------------------------------------ | ||
| 39 | Symbol Meaning Nucleic Acid | ||
| 40 | ------------------------------------------ | ||
| 41 | A A Adenine | ||
| 42 | C C Cytosine | ||
| 43 | G G Guanine | ||
| 44 | T T Thymine | ||
| 45 | U U Uracil | ||
| 46 | M A or C | ||
| 47 | R A or G | ||
| 48 | W A or T | ||
| 49 | S C or G | ||
| 50 | Y C or T | ||
| 51 | K G or T | ||
| 52 | V A or C or G | ||
| 53 | H A or C or T | ||
| 54 | D A or G or T | ||
| 55 | B C or G or T | ||
| 56 | X G or A or T or C | ||
| 57 | N G or A or T or C | ||
| 58 | |||
| 59 | IUPAC-IUB SYMBOLS FOR NUCLEOTIDE NOMENCLATURE: | ||
| 60 | Cornish-Bowden (1985) Nucl. Acids Res. 13: 3021-3030. | ||
| 61 | |||
| 62 | ----------------------------------- | ||
| 63 | |||
| 64 | Amino Acid alphabet: | ||
| 65 | ------------------------------------------ | ||
| 66 | Symbol Meaning | ||
| 67 | ------------------------------------------ | ||
| 68 | A Alanine | ||
| 69 | B Aspartic Acid, Asparagine | ||
| 70 | C Cystine | ||
| 71 | D Aspartic Acid | ||
| 72 | E Glutamic Acid | ||
| 73 | F Phenylalanine | ||
| 74 | G Glycine | ||
| 75 | H Histidine | ||
| 76 | I Isoleucine | ||
| 77 | J Isoleucine/Leucine | ||
| 78 | K Lysine | ||
| 79 | L Leucine | ||
| 80 | M Methionine | ||
| 81 | N Asparagine | ||
| 82 | O Pyrrolysine | ||
| 83 | P Proline | ||
| 84 | Q Glutamine | ||
| 85 | R Arginine | ||
| 86 | S Serine | ||
| 87 | T Threonine | ||
| 88 | U Selenocysteine | ||
| 89 | V Valine | ||
| 90 | W Tryptophan | ||
| 91 | X Unknown | ||
| 92 | Y Tyrosine | ||
| 93 | Z Glutamic Acid, Glutamine | ||
| 94 | * Terminator | ||
| 95 | |||
| 96 | |||
| 97 | IUPAC-IUP AMINO ACID SYMBOLS: | ||
| 98 | Biochem J. 1984 Apr 15; 219(2): 345-373 | ||
| 99 | Eur J Biochem. 1993 Apr 1; 213(1): 2 | ||
| 100 | |||
| 101 | =head1 FEEDBACK | ||
| 102 | |||
| 103 | =head2 Mailing Lists | ||
| 104 | |||
| 105 | User feedback is an integral part of the evolution of this and other | ||
| 106 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 107 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 108 | |||
| 109 | bioperl-l@bioperl.org - General discussion | ||
| 110 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 111 | |||
| 112 | =head2 Support | ||
| 113 | |||
| 114 | Please direct usage questions or support issues to the mailing list: | ||
| 115 | |||
| 116 | I<bioperl-l@bioperl.org> | ||
| 117 | |||
| 118 | rather than to the module maintainer directly. Many experienced and | ||
| 119 | reponsive experts will be able look at the problem and quickly | ||
| 120 | address it. Please include a thorough description of the problem | ||
| 121 | with code and data examples if at all possible. | ||
| 122 | |||
| 123 | =head2 Reporting Bugs | ||
| 124 | |||
| 125 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 126 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 127 | web: | ||
| 128 | |||
| 129 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 130 | |||
| 131 | =head1 AUTHOR - Aaron Mackey | ||
| 132 | |||
| 133 | Email amackey-at-virginia.edu | ||
| 134 | |||
| 135 | =head1 APPENDIX | ||
| 136 | |||
| 137 | The rest of the documentation details each of the object | ||
| 138 | methods. Internal methods are usually preceded with a _ | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | |||
| 143 | # Let the code begin... | ||
| 144 | |||
| 145 | package FAST::Bio::Tools::IUPAC; | ||
| 146 | |||
| 147 | use strict; | ||
| 148 | use vars qw(%IUP %IUB %REV_IUB $AUTOLOAD); | ||
| 149 | |||
| 150 | BEGIN { | ||
| 151 | %IUB = ( A => [qw(A)], | ||
| 152 | C => [qw(C)], | ||
| 153 | G => [qw(G)], | ||
| 154 | T => [qw(T)], | ||
| 155 | U => [qw(U)], | ||
| 156 | M => [qw(A C)], | ||
| 157 | R => [qw(A G)], | ||
| 158 | W => [qw(A T)], | ||
| 159 | S => [qw(C G)], | ||
| 160 | Y => [qw(C T)], | ||
| 161 | K => [qw(G T)], | ||
| 162 | V => [qw(A C G)], | ||
| 163 | H => [qw(A C T)], | ||
| 164 | D => [qw(A G T)], | ||
| 165 | B => [qw(C G T)], | ||
| 166 | X => [qw(G A T C)], | ||
| 167 | N => [qw(G A T C)] | ||
| 168 | ); | ||
| 169 | %REV_IUB = (A => 'A', | ||
| 170 | T => 'T', | ||
| 171 | C => 'C', | ||
| 172 | G => 'G', | ||
| 173 | AC => 'M', | ||
| 174 | AG => 'R', | ||
| 175 | AT => 'W', | ||
| 176 | CG => 'S', | ||
| 177 | CT => 'Y', | ||
| 178 | 'GT' => 'K', | ||
| 179 | ACG => 'V', | ||
| 180 | ACT => 'H', | ||
| 181 | AGT => 'D', | ||
| 182 | CGT => 'B', | ||
| 183 | ACGT=> 'N', | ||
| 184 | N => 'N' | ||
| 185 | ); | ||
| 186 | |||
| 187 | |||
| 188 | %IUP = (A => [qw(A)], | ||
| 189 | B => [qw(D N)], | ||
| 190 | C => [qw(C)], | ||
| 191 | D => [qw(D)], | ||
| 192 | E => [qw(E)], | ||
| 193 | F => [qw(F)], | ||
| 194 | G => [qw(G)], | ||
| 195 | H => [qw(H)], | ||
| 196 | I => [qw(I)], | ||
| 197 | J => [qw(I L)], | ||
| 198 | K => [qw(K)], | ||
| 199 | L => [qw(L)], | ||
| 200 | M => [qw(M)], | ||
| 201 | N => [qw(N)], | ||
| 202 | O => [qw(O)], | ||
| 203 | P => [qw(P)], | ||
| 204 | Q => [qw(Q)], | ||
| 205 | R => [qw(R)], | ||
| 206 | S => [qw(S)], | ||
| 207 | T => [qw(T)], | ||
| 208 | U => [qw(U)], | ||
| 209 | V => [qw(V)], | ||
| 210 | W => [qw(W)], | ||
| 211 | X => [qw(X)], | ||
| 212 | Y => [qw(Y)], | ||
| 213 | Z => [qw(E Q)], | ||
| 214 | '*' => ['*'] | ||
| 215 | ); | ||
| 216 | |||
| 217 | } | ||
| 218 | use base qw(FAST::Bio::Root::Root); | ||
| 219 | |||
| 220 | =head2 new | ||
| 221 | |||
| 222 | Title : new | ||
| 223 | Usage : FAST::Bio::Tools::IUPAC->new( $seq) | ||
| 224 | Function: returns a new seq stream (akin to SeqIO) | ||
| 225 | Returns : a FAST::Bio::Tools::IUPAC stream object that will produce unique | ||
| 226 | Seq objects on demand. | ||
| 227 | Args : an ambiguously coded Seq.pm object that has a specified 'alphabet' | ||
| 228 | |||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | |||
| 233 | sub new { | ||
| 234 | my($class,@args) = @_; | ||
| 235 | my $self = $class->SUPER::new(@args); | ||
| 236 | |||
| 237 | my ($seq) = $self->_rearrange([qw(SEQ)],@args); | ||
| 238 | if((! defined($seq)) && @args && ref($args[0])) { | ||
| 239 | # parameter not passed as named parameter? | ||
| 240 | $seq = $args[0]; | ||
| 241 | } | ||
| 242 | $seq->isa('FAST::Bio::Seq') or | ||
| 243 | $self->throw("Must supply a Seq.pm object to IUPAC!"); | ||
| 244 | $self->{'_SeqObj'} = $seq; | ||
| 245 | if ($self->{'_SeqObj'}->alphabet() =~ m/^[dr]na$/i ) { | ||
| 246 | # nucleotide seq object | ||
| 247 | $self->{'_alpha'} = [ map { $IUB{uc($_)} } | ||
| 248 | split('', $self->{'_SeqObj'}->seq()) ]; | ||
| 249 | } elsif ($self->{'_SeqObj'}->alphabet() =~ m/^protein$/i ) { | ||
| 250 | # amino acid seq object | ||
| 251 | $self->{'_alpha'} = [ map { $IUP{uc($_)} } | ||
| 252 | split('', $self->{'_SeqObj'}->seq()) ]; | ||
| 253 | } else { # unknown type: we could make a guess, but let's not. | ||
| 254 | $self->throw("You must specify the 'type' of sequence provided to IUPAC"); | ||
| 255 | } | ||
| 256 | $self->{'_string'} = [(0) x length($self->{'_SeqObj'}->seq())]; | ||
| 257 | scalar @{$self->{'_string'}} or $self->throw("Sequence has zero-length!"); | ||
| 258 | $self->{'_string'}->[0] = -1; | ||
| 259 | return $self; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 next_seq | ||
| 263 | |||
| 264 | Title : next_seq | ||
| 265 | Usage : $iupac->next_seq() | ||
| 266 | Function: returns the next unique Seq object | ||
| 267 | Returns : a Seq.pm object | ||
| 268 | Args : none. | ||
| 269 | |||
| 270 | |||
| 271 | =cut | ||
| 272 | |||
| 273 | sub next_seq{ | ||
| 274 | my ($self) = @_; | ||
| 275 | |||
| 276 | for my $i ( 0 .. $#{$self->{'_string'}} ) { | ||
| 277 | next unless $self->{'_string'}->[$i] || @{$self->{'_alpha'}->[$i]} > 1; | ||
| 278 | if ( $self->{'_string'}->[$i] == $#{$self->{'_alpha'}->[$i]} ) { # rollover | ||
| 279 | if ( $i == $#{$self->{'_string'}} ) { # end of possibilities | ||
| 280 | return; | ||
| 281 | } else { | ||
| 282 | $self->{'_string'}->[$i] = 0; | ||
| 283 | next; | ||
| 284 | } | ||
| 285 | } else { | ||
| 286 | $self->{'_string'}->[$i]++; | ||
| 287 | my $j = -1; | ||
| 288 | $self->{'_SeqObj'}->seq(join('', map { $j++; $self->{'_alpha'}->[$j]->[$_]; } @{$self->{'_string'}})); | ||
| 289 | my $desc = $self->{'_SeqObj'}->desc(); | ||
| 290 | if ( !defined $desc ) { $desc = ""; } | ||
| 291 | |||
| 292 | $self->{'_num'}++; | ||
| 293 | 1 while $self->{'_num'} =~ s/(\d)(\d\d\d)(?!\d)/$1,$2/; | ||
| 294 | $desc =~ s/( \[FAST::Bio::Tools::IUPAC-generated\sunique sequence # [^\]]*\])|$/ \[FAST::Bio::Tools::IUPAC-generated unique sequence # $self->{'_num'}\]/; | ||
| 295 | $self->{'_SeqObj'}->desc($desc); | ||
| 296 | $self->{'_num'} =~ s/,//g; | ||
| 297 | return $self->{'_SeqObj'}; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | =head2 iupac_iup | ||
| 303 | |||
| 304 | Title : iupac_iup | ||
| 305 | Usage : my %aasymbols = $iupac->iupac_iup | ||
| 306 | Function: Returns a hash of PROTEIN symbols -> symbol components | ||
| 307 | Returns : Hash | ||
| 308 | Args : none | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | sub iupac_iup{ | ||
| 313 | return %IUP; | ||
| 314 | |||
| 315 | } | ||
| 316 | |||
| 317 | =head2 iupac_iub | ||
| 318 | |||
| 319 | Title : iupac_iub | ||
| 320 | Usage : my %dnasymbols = $iupac->iupac_iub | ||
| 321 | Function: Returns a hash of DNA symbols -> symbol components | ||
| 322 | Returns : Hash | ||
| 323 | Args : none | ||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | sub iupac_iub{ | ||
| 328 | return %IUB; | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 iupac_rev_iub | ||
| 332 | |||
| 333 | Title : iupac_rev_iub | ||
| 334 | Usage : my %dnasymbols = $iupac->iupac_rev_iub | ||
| 335 | Function: Returns a hash of nucleotide combinations -> IUPAC code | ||
| 336 | (a reverse of the iupac_iub hash). | ||
| 337 | Returns : Hash | ||
| 338 | Args : none | ||
| 339 | |||
| 340 | =cut | ||
| 341 | |||
| 342 | sub iupac_rev_iub{ | ||
| 343 | return %REV_IUB; | ||
| 344 | } | ||
| 345 | |||
| 346 | =head2 count | ||
| 347 | |||
| 348 | Title : count | ||
| 349 | Usage : my $total = $iupac->count(); | ||
| 350 | Function: Calculates the number of unique, unambiguous sequences that | ||
| 351 | this ambiguous sequence could generate | ||
| 352 | Return : int | ||
| 353 | Args : none | ||
| 354 | |||
| 355 | =cut | ||
| 356 | |||
| 357 | sub count { | ||
| 358 | my ($self) = @_; | ||
| 359 | |||
| 360 | my $count = 1; | ||
| 361 | $count *= scalar(@$_) for (@{$self->{'_alpha'}}); | ||
| 362 | return $count; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | sub AUTOLOAD { | ||
| 367 | |||
| 368 | my $self = shift @_; | ||
| 369 | my $method = $AUTOLOAD; | ||
| 370 | $method =~ s/.*:://; | ||
| 371 | return $self->{'_SeqObj'}->$method(@_) | ||
| 372 | unless $method eq 'DESTROY'; | ||
| 373 | } | ||
| 374 | |||
| 375 | 1; | ||
| 376 |