| 1 | # | ||
| 2 | # bioperl module for FAST::Bio::Tools::SeqPattern | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Steve Chervitz (sac-at-bioperl.org) | ||
| 7 | # | ||
| 8 | # Copyright Steve Chervitz | ||
| 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::SeqPattern - represent a sequence pattern or motif | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use FAST::Bio::Tools::SeqPattern; | ||
| 21 | |||
| 22 | my $pat1 = 'T[GA]AA...TAAT'; | ||
| 23 | my $pattern1 = FAST::Bio::Tools::SeqPattern->new(-SEQ =>$pat1, -TYPE =>'Dna'); | ||
| 24 | |||
| 25 | my $pat2 = '[VILM]R(GXX){3,2}...[^PG]'; | ||
| 26 | my $pattern2 = FAST::Bio::Tools::SeqPattern->new(-SEQ =>$pat2, -TYPE =>'Amino'); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | L<FAST::Bio::Tools::SeqPattern> module encapsulates generic data and | ||
| 31 | methods for manipulating regular expressions describing nucleic or | ||
| 32 | amino acid sequence patterns (a.k.a, "motifs"). | ||
| 33 | |||
| 34 | L<FAST::Bio::Tools::SeqPattern> is a concrete class that inherits from L<FAST::Bio::Seq>. | ||
| 35 | |||
| 36 | This class grew out of a need to have a standard module for doing routine | ||
| 37 | tasks with sequence patterns such as: | ||
| 38 | |||
| 39 | -- Forming a reverse-complement version of a nucleotide sequence pattern | ||
| 40 | -- Expanding patterns containing ambiguity codes | ||
| 41 | -- Checking for invalid regexp characters | ||
| 42 | -- Untainting yet preserving special characters in the pattern | ||
| 43 | |||
| 44 | Other features to look for in the future: | ||
| 45 | |||
| 46 | -- Full pattern syntax checking | ||
| 47 | -- Conversion between expanded and condensed forms of the pattern | ||
| 48 | |||
| 49 | =head1 MOTIVATIONS | ||
| 50 | |||
| 51 | A key motivation for L<FAST::Bio::Tools::SeqPattern> is to have a way to | ||
| 52 | generate a reverse complement of a nucleotide sequence pattern. | ||
| 53 | This makes possible simultaneous pattern matching on both sense and | ||
| 54 | anti-sense strands of a query sequence. | ||
| 55 | |||
| 56 | In principle, one could do such a search more inefficiently by testing | ||
| 57 | against both sense and anti-sense versions of a sequence. | ||
| 58 | It is entirely equivalent to test a regexp containing both sense and | ||
| 59 | anti-sense versions of the *pattern* against one copy of the sequence. | ||
| 60 | The latter approach is much more efficient since: | ||
| 61 | |||
| 62 | 1) You need only one copy of the sequence. | ||
| 63 | 2) Only one regexp is executed. | ||
| 64 | 3) Regexp patterns are typically much smaller than sequences. | ||
| 65 | |||
| 66 | Patterns can be quite complex and it is often difficult to | ||
| 67 | generate the reverse complement pattern. The Bioperl SeqPattern.pm | ||
| 68 | addresses this problem, providing a convenient set of tools | ||
| 69 | for working with biological sequence regular expressions. | ||
| 70 | |||
| 71 | Not all patterns have been tested. If you discover a pattern that | ||
| 72 | is not handled properly by FAST::Bio::Tools::SeqPattern.pm, please | ||
| 73 | send me some email (sac@bioperl.org). Thanks. | ||
| 74 | |||
| 75 | =head1 OTHER FEATURES | ||
| 76 | |||
| 77 | =head2 Extended Alphabet Support | ||
| 78 | |||
| 79 | This module supports the same set of ambiguity codes for nucleotide | ||
| 80 | sequences as supported by L<FAST::Bio::Seq>. These ambiguity codes | ||
| 81 | define the behavior or the L<expand> method. | ||
| 82 | |||
| 83 | ------------------------------------------ | ||
| 84 | Symbol Meaning Nucleic Acid | ||
| 85 | ------------------------------------------ | ||
| 86 | A A (A)denine | ||
| 87 | C C (C)ytosine | ||
| 88 | G G (G)uanine | ||
| 89 | T T (T)hymine | ||
| 90 | U U (U)racil | ||
| 91 | M A or C a(M)ino group | ||
| 92 | R A or G pu(R)ine | ||
| 93 | W A or T (W)eak bond | ||
| 94 | S C or G (S)trong bond | ||
| 95 | Y C or T p(Y)rimidine | ||
| 96 | K G or T (K)eto group | ||
| 97 | V A or C or G | ||
| 98 | H A or C or T | ||
| 99 | D A or G or T | ||
| 100 | B C or G or T | ||
| 101 | X G or A or T or C | ||
| 102 | N G or A or T or C | ||
| 103 | . G or A or T or C | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | ------------------------------------------ | ||
| 108 | Symbol Meaning | ||
| 109 | ------------------------------------------ | ||
| 110 | A Alanine | ||
| 111 | C Cysteine | ||
| 112 | D Aspartic Acid | ||
| 113 | E Glutamic Acid | ||
| 114 | F Phenylalanine | ||
| 115 | G Glycine | ||
| 116 | H Histidine | ||
| 117 | I Isoleucine | ||
| 118 | K Lysine | ||
| 119 | L Leucine | ||
| 120 | M Methionine | ||
| 121 | N Asparagine | ||
| 122 | P Proline | ||
| 123 | Q Glutamine | ||
| 124 | R Arginine | ||
| 125 | S Serine | ||
| 126 | T Threonine | ||
| 127 | V Valine | ||
| 128 | W Tryptophan | ||
| 129 | Y Tyrosine | ||
| 130 | |||
| 131 | B Aspartic Acid, Asparagine | ||
| 132 | Z Glutamic Acid, Glutamine | ||
| 133 | X Any amino acid | ||
| 134 | . Any amino acid | ||
| 135 | |||
| 136 | |||
| 137 | =head2 Multiple Format Support | ||
| 138 | |||
| 139 | Ultimately, this module should be able to build SeqPattern.pm objects | ||
| 140 | using a variety of pattern formats such as ProSite, Blocks, Prints, GCG, etc. | ||
| 141 | Currently, this module only supports patterns using a grep-like syntax. | ||
| 142 | |||
| 143 | =head1 USAGE | ||
| 144 | |||
| 145 | A simple demo script called seq_pattern.pl is included in the examples/ | ||
| 146 | directory of the central Bioperl distribution. | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<FAST::Bio::Seq> - Lightweight sequence object. | ||
| 151 | |||
| 152 | =head1 FEEDBACK | ||
| 153 | |||
| 154 | =head2 Mailing Lists | ||
| 155 | |||
| 156 | User feedback is an integral part of the evolution of this and other | ||
| 157 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 158 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 159 | |||
| 160 | bioperl-l@bioperl.org - General discussion | ||
| 161 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 162 | |||
| 163 | =head2 Support | ||
| 164 | |||
| 165 | Please direct usage questions or support issues to the mailing list: | ||
| 166 | |||
| 167 | I<bioperl-l@bioperl.org> | ||
| 168 | |||
| 169 | rather than to the module maintainer directly. Many experienced and | ||
| 170 | reponsive experts will be able look at the problem and quickly | ||
| 171 | address it. Please include a thorough description of the problem | ||
| 172 | with code and data examples if at all possible. | ||
| 173 | |||
| 174 | =head2 Reporting Bugs | ||
| 175 | |||
| 176 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 177 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 178 | web: | ||
| 179 | |||
| 180 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 181 | |||
| 182 | =head1 AUTHOR | ||
| 183 | |||
| 184 | Steve Chervitz, sac-at-bioperl.org | ||
| 185 | |||
| 186 | =head1 COPYRIGHT | ||
| 187 | |||
| 188 | Copyright (c) 1997-8 Steve Chervitz. All Rights Reserved. | ||
| 189 | This module is free software; you can redistribute it and/or | ||
| 190 | modify it under the same terms as Perl itself. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | # | ||
| 195 | ## | ||
| 196 | ### | ||
| 197 | #### END of main POD documentation. | ||
| 198 | ### | ||
| 199 | ## | ||
| 200 | #' | ||
| 201 | # CREATED : 28 Aug 1997 | ||
| 202 | |||
| 203 | |||
| 204 | package FAST::Bio::Tools::SeqPattern; | ||
| 205 | |||
| 206 | use base qw(FAST::Bio::Root::Root); | ||
| 207 | use strict; | ||
| 208 | use vars qw ($ID); | ||
| 209 | $ID = 'FAST::Bio::Tools::SeqPattern'; | ||
| 210 | |||
| 211 | ## These constants may be more appropriate in a FAST::Bio::Dictionary.pm | ||
| 212 | ## type of class. | ||
| 213 | my $PURINES = 'AG'; | ||
| 214 | my $PYRIMIDINES = 'CT'; | ||
| 215 | my $BEE = 'DN'; | ||
| 216 | my $ZED = 'EQ'; | ||
| 217 | my $Regexp_chars = '\w,.\*()\[\]<>\{\}^\$'; # quoted for use in regexps | ||
| 218 | |||
| 219 | ## Package variables used in reverse complementing. | ||
| 220 | my (%Processed_braces, %Processed_asterics); | ||
| 221 | |||
| 222 | ##################################################################################### | ||
| 223 | ## CONSTRUCTOR ## | ||
| 224 | ##################################################################################### | ||
| 225 | |||
| 226 | =head1 new | ||
| 227 | |||
| 228 | Title : new | ||
| 229 | Usage : my $seqpat = FAST::Bio::Tools::SeqPattern->new(); | ||
| 230 | Purpose : Verifies that the type is correct for superclass (FAST::Bio::Seq.pm) | ||
| 231 | : and calls superclass constructor last. | ||
| 232 | Returns : n/a | ||
| 233 | Argument : Parameters passed to new() | ||
| 234 | Throws : Exception if the pattern string (seq) is empty. | ||
| 235 | Comments : The process of creating a new SeqPattern.pm object | ||
| 236 | : ensures that the pattern string is untained. | ||
| 237 | |||
| 238 | See Also : L<FAST::Bio::Root::Root::new>, | ||
| 239 | L<FAST::Bio::Seq::_initialize> | ||
| 240 | |||
| 241 | =cut | ||
| 242 | |||
| 243 | #---------------- | ||
| 244 | sub new { | ||
| 245 | #---------------- | ||
| 246 | my($class, %param) = @_; | ||
| 247 | |||
| 248 | my $self = $class->SUPER::new(%param); | ||
| 249 | my ($seq,$type) = $self->_rearrange([qw(SEQ TYPE)], %param); | ||
| 250 | |||
| 251 | $seq || $self->throw("Empty pattern."); | ||
| 252 | my $t; | ||
| 253 | # Get the type ready for FAST::Bio::Seq.pm | ||
| 254 | if ($type =~ /nuc|[dr]na/i) { | ||
| 255 | $t = 'Dna'; | ||
| 256 | } elsif ($type =~ /amino|pep|prot/i) { | ||
| 257 | $t = 'Amino'; | ||
| 258 | } | ||
| 259 | $seq =~ tr/a-z/A-Z/; #ps 8/8/00 Canonicalize to upper case | ||
| 260 | $self->str($seq); | ||
| 261 | $self->type($t); | ||
| 262 | |||
| 263 | return $self; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | =head1 alphabet_ok | ||
| 268 | |||
| 269 | Title : alphabet_ok | ||
| 270 | Usage : $mypat->alphabet_ok; | ||
| 271 | Purpose : Checks for invalid regexp characters. | ||
| 272 | : Overrides FAST::Bio::Seq::alphabet_ok() to allow | ||
| 273 | : additional regexp characters ,.*()[]<>{}^$ | ||
| 274 | : in addition to the standard genetic alphabet. | ||
| 275 | : Also untaints the pattern and sets the sequence | ||
| 276 | : object's sequence to the untained string. | ||
| 277 | Returns : Boolean (1 | 0) | ||
| 278 | Argument : n/a | ||
| 279 | Throws : Exception if the pattern contains invalid characters. | ||
| 280 | Comments : Does not call the superclass method. | ||
| 281 | : Actually permits any alphanumeric, not just the | ||
| 282 | : standard genetic alphabet. | ||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | #----------------' | ||
| 287 | sub alphabet_ok { | ||
| 288 | #---------------- | ||
| 289 | my( $self) = @_; | ||
| 290 | |||
| 291 | return 1 if $self->{'_alphabet_checked'}; | ||
| 292 | |||
| 293 | $self->{'_alphabet_checked'} = 1; | ||
| 294 | |||
| 295 | my $pat = $self->seq(); | ||
| 296 | |||
| 297 | if($pat =~ /[^$Regexp_chars]/io) { | ||
| 298 | $self->throw("Pattern contains invalid characters: $pat", | ||
| 299 | 'Legal characters: a-z,A-Z,0-9,,.*()[]<>{}^$ '); | ||
| 300 | } | ||
| 301 | |||
| 302 | # Untaint pattern (makes code taint-safe). | ||
| 303 | $pat =~ /([$Regexp_chars]+)/io; | ||
| 304 | $self->setseq(uc($1)); | ||
| 305 | # print STDERR "\npattern ok: $pat\n"; | ||
| 306 | 1; | ||
| 307 | } | ||
| 308 | |||
| 309 | =head1 expand | ||
| 310 | |||
| 311 | Title : expand | ||
| 312 | Usage : $seqpat_object->expand(); | ||
| 313 | Purpose : Expands the sequence pattern using special ambiguity codes. | ||
| 314 | Example : $pat = $seq_pat->expand(); | ||
| 315 | Returns : String containing fully expanded sequence pattern | ||
| 316 | Argument : n/a | ||
| 317 | Throws : Exception if sequence type is not recognized | ||
| 318 | : (i.e., is not one of [DR]NA, Amino) | ||
| 319 | |||
| 320 | See Also : L<Extended Alphabet Support>, L<_expand_pep>(), L<_expand_nuc>() | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | #---------- | ||
| 325 | sub expand { | ||
| 326 | #---------- | ||
| 327 | my $self = shift; | ||
| 328 | |||
| 329 | if($self->type =~ /[DR]na/i) { $self->_expand_nuc(); } | ||
| 330 | elsif($self->type =~ /Amino/i) { $self->_expand_pep(); } | ||
| 331 | else{ | ||
| 332 | $self->throw("Don't know how to expand ${\$self->type} patterns.\n"); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | =head1 _expand_pep | ||
| 338 | |||
| 339 | Title : _expand_pep | ||
| 340 | Usage : n/a; automatically called by expand() | ||
| 341 | Purpose : Expands peptide patterns | ||
| 342 | Returns : String (the expanded pattern) | ||
| 343 | Argument : String (the unexpanded pattern) | ||
| 344 | Throws : n/a | ||
| 345 | |||
| 346 | See Also : L<expand>(), L<_expand_nuc>() | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | #---------------- | ||
| 351 | sub _expand_pep { | ||
| 352 | #---------------- | ||
| 353 | my ($self,$pat) = @_; | ||
| 354 | $pat ||= $self->str; | ||
| 355 | $pat =~ s/X/./g; | ||
| 356 | $pat =~ s/^</\^/; | ||
| 357 | $pat =~ s/>$/\$/; | ||
| 358 | |||
| 359 | ## Avoid nested situations: [bmnq] --/--> [[$ZED]mnq] | ||
| 360 | ## Yet correctly deal with: fze[bmnq] ---> f[$BEE]e[$ZEDmnq] | ||
| 361 | if($pat =~ /\[\w*[BZ]\w*\]/) { | ||
| 362 | $pat =~ s/\[(\w*)B(\w*)\]/\[$1$ZED$2\]/g; | ||
| 363 | $pat =~ s/\[(\w*)Z(\w*)\]/\[$1$BEE$2\]/g; | ||
| 364 | $pat =~ s/B/\[$ZED\]/g; | ||
| 365 | $pat =~ s/Z/\[$BEE\]/g; | ||
| 366 | } else { | ||
| 367 | $pat =~ s/B/\[$ZED\]/g; | ||
| 368 | $pat =~ s/Z/\[$BEE\]/g; | ||
| 369 | } | ||
| 370 | $pat =~ s/\((.)\)/$1/g; ## Doing these last since: | ||
| 371 | $pat =~ s/\[(.)\]/$1/g; ## Pattern could contain [B] (for example) | ||
| 372 | |||
| 373 | return $pat; | ||
| 374 | } | ||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 | =head1 _expand_nuc | ||
| 379 | |||
| 380 | Title : _expand_nuc | ||
| 381 | Purpose : Expands nucleotide patterns | ||
| 382 | Returns : String (the expanded pattern) | ||
| 383 | Argument : String (the unexpanded pattern) | ||
| 384 | Throws : n/a | ||
| 385 | |||
| 386 | See Also : L<expand>(), L<_expand_pep>() | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | #--------------- | ||
| 391 | sub _expand_nuc { | ||
| 392 | #--------------- | ||
| 393 | my ($self,$pat) = @_; | ||
| 394 | |||
| 395 | $pat ||= $self->str; | ||
| 396 | $pat =~ s/N|X/./g; | ||
| 397 | $pat =~ s/pu/R/ig; | ||
| 398 | $pat =~ s/py/Y/ig; | ||
| 399 | $pat =~ s/U/T/g; | ||
| 400 | $pat =~ s/^</\^/; | ||
| 401 | $pat =~ s/>$/\$/; | ||
| 402 | |||
| 403 | ## Avoid nested situations: [ya] --/--> [[ct]a] | ||
| 404 | ## Yet correctly deal with: sg[ya] ---> [gc]g[cta] | ||
| 405 | if($pat =~ /\[\w*[RYSWMK]\w*\]/) { | ||
| 406 | $pat =~ s/\[(\w*)R(\w*)\]/\[$1$PURINES$2\]/g; | ||
| 407 | $pat =~ s/\[(\w*)Y(\w*)\]/\[$1$PYRIMIDINES$2\]/g; | ||
| 408 | $pat =~ s/\[(\w*)S(\w*)\]/\[$1GC$2\]/g; | ||
| 409 | $pat =~ s/\[(\w*)W(\w*)\]/\[$1AT$2\]/g; | ||
| 410 | $pat =~ s/\[(\w*)M(\w*)\]/\[$1AC$2\]/g; | ||
| 411 | $pat =~ s/\[(\w*)K(\w*)\]/\[$1GT$2\]/g; | ||
| 412 | $pat =~ s/\[(\w*)V(\w*)\]/\[$1ACG$2\]/g; | ||
| 413 | $pat =~ s/\[(\w*)H(\w*)\]/\[$1ACT$2\]/g; | ||
| 414 | $pat =~ s/\[(\w*)D(\w*)\]/\[$1AGT$2\]/g; | ||
| 415 | $pat =~ s/\[(\w*)B(\w*)\]/\[$1CGT$2\]/g; | ||
| 416 | $pat =~ s/R/\[$PURINES\]/g; | ||
| 417 | $pat =~ s/Y/\[$PYRIMIDINES\]/g; | ||
| 418 | $pat =~ s/S/\[GC\]/g; | ||
| 419 | $pat =~ s/W/\[AT\]/g; | ||
| 420 | $pat =~ s/M/\[AC\]/g; | ||
| 421 | $pat =~ s/K/\[GT\]/g; | ||
| 422 | $pat =~ s/V/\[ACG\]/g; | ||
| 423 | $pat =~ s/H/\[ACT\]/g; | ||
| 424 | $pat =~ s/D/\[AGT\]/g; | ||
| 425 | $pat =~ s/B/\[CGT\]/g; | ||
| 426 | } else { | ||
| 427 | $pat =~ s/R/\[$PURINES\]/g; | ||
| 428 | $pat =~ s/Y/\[$PYRIMIDINES\]/g; | ||
| 429 | $pat =~ s/S/\[GC\]/g; | ||
| 430 | $pat =~ s/W/\[AT\]/g; | ||
| 431 | $pat =~ s/M/\[AC\]/g; | ||
| 432 | $pat =~ s/K/\[GT\]/g; | ||
| 433 | $pat =~ s/V/\[ACG\]/g; | ||
| 434 | $pat =~ s/H/\[ACT\]/g; | ||
| 435 | $pat =~ s/D/\[AGT\]/g; | ||
| 436 | $pat =~ s/B/\[CGT\]/g; | ||
| 437 | } | ||
| 438 | $pat =~ s/\((.)\)/$1/g; ## Doing thses last since: | ||
| 439 | $pat =~ s/\[(.)\]/$1/g; ## Pattern could contain [y] (for example) | ||
| 440 | |||
| 441 | return $pat; | ||
| 442 | } | ||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | =head1 revcom | ||
| 447 | |||
| 448 | Title : revcom | ||
| 449 | Usage : revcom([1]); | ||
| 450 | Purpose : Forms a pattern capable of recognizing the reverse complement | ||
| 451 | : version of a nucleotide sequence pattern. | ||
| 452 | Example : $pattern_object->revcom(); | ||
| 453 | : $pattern_object->revcom(1); ## returns expanded rev complement pattern. | ||
| 454 | Returns : Object reference for a new FAST::Bio::Tools::SeqPattern containing | ||
| 455 | : the revcom of the current pattern as its sequence. | ||
| 456 | Argument : (1) boolean (optional) (default= false) | ||
| 457 | : true : expand the pattern before rev-complementing. | ||
| 458 | : false: don't expand pattern before or after rev-complementing. | ||
| 459 | Throws : Exception if called for amino acid sequence pattern. | ||
| 460 | Comments : This method permits the simultaneous searching of both | ||
| 461 | : sense and anti-sense versions of a nucleotide pattern | ||
| 462 | : by means of a grep-type of functionality in which any | ||
| 463 | : number of patterns may be or-ed into the recognition | ||
| 464 | : pattern. | ||
| 465 | : Overrides FAST::Bio::Seq::revcom() and calls it first thing. | ||
| 466 | : The order of _fixpat() calls is critical. | ||
| 467 | |||
| 468 | See Also : L<FAST::Bio::Seq::revcom>, L</_fixpat_1>, L</_fixpat_2>, L</_fixpat_3>, L</_fixpat_4>, L</_fixpat_5> | ||
| 469 | |||
| 470 | =cut | ||
| 471 | |||
| 472 | #-----------' | ||
| 473 | sub revcom { | ||
| 474 | #----------- | ||
| 475 | my($self,$expand) = @_; | ||
| 476 | |||
| 477 | if ($self->type !~ /Dna|Rna/i) { | ||
| 478 | $self->throw("Can't get revcom for ${\$self->type} sequence types.\n"); | ||
| 479 | } | ||
| 480 | # return $self->{'_rev'} if defined $self->{'_rev'}; | ||
| 481 | |||
| 482 | $expand ||= 0; | ||
| 483 | my $str = $self->str; | ||
| 484 | $str =~ tr/acgtrymkswhbvdnxACGTRYMKSWHBVDNX/tgcayrkmswdvbhnxTGCAYRKMSWDVBHNX/; | ||
| 485 | my $rev = CORE::reverse $str; | ||
| 486 | $rev =~ tr/[](){}<>/][)(}{></; | ||
| 487 | |||
| 488 | if($expand) { | ||
| 489 | $rev = $self->_expand_nuc($rev); | ||
| 490 | # print "\nExpanded: $rev\n"; | ||
| 491 | } | ||
| 492 | |||
| 493 | %Processed_braces = (); | ||
| 494 | %Processed_asterics = (); | ||
| 495 | |||
| 496 | my $fixrev = _fixpat_1($rev); | ||
| 497 | # print "FIX 1: $fixrev";<STDIN>; | ||
| 498 | |||
| 499 | $fixrev = _fixpat_2($fixrev); | ||
| 500 | # print "FIX 2: $fixrev";<STDIN>; | ||
| 501 | |||
| 502 | $fixrev = _fixpat_3($fixrev); | ||
| 503 | # print "FIX 3: $fixrev";<STDIN>; | ||
| 504 | |||
| 505 | $fixrev = _fixpat_4($fixrev); | ||
| 506 | # print "FIX 4: $fixrev";<STDIN>; | ||
| 507 | |||
| 508 | $fixrev = _fixpat_5($fixrev); | ||
| 509 | # print "FIX 5: $fixrev";<STDIN>; | ||
| 510 | |||
| 511 | ##### Added by ps 8/7/00 to allow non-greedy matching | ||
| 512 | $fixrev = _fixpat_6($fixrev); | ||
| 513 | # print "FIX 6: $fixrev";<STDIN>; | ||
| 514 | |||
| 515 | # $self->{'_rev'} = $fixrev; | ||
| 516 | |||
| 517 | return new FAST::Bio::Tools::SeqPattern(-seq =>$fixrev, -type =>$self->type); | ||
| 518 | } | ||
| 519 | |||
| 520 | =head1 backtranslate | ||
| 521 | |||
| 522 | Title : backtranslate | ||
| 523 | Usage : backtranslate(); | ||
| 524 | Purpose : Produce a degenerate oligonucleotide whose translation would produce | ||
| 525 | : the original protein motif. | ||
| 526 | Example : $pattern_object->backtranslate(); | ||
| 527 | Returns : Object reference for a new FAST::Bio::Tools::SeqPattern containing | ||
| 528 | : the reverse translation of the current pattern as its sequence. | ||
| 529 | Throws : Exception if called for nucleotide sequence pattern. | ||
| 530 | |||
| 531 | =cut | ||
| 532 | |||
| 533 | sub backtranslate { | ||
| 534 | my $self = shift; | ||
| 535 | |||
| 536 | # _load_module loads dynamically, caches call if successful | ||
| 537 | $self->_load_module('FAST::Bio::Tools::SeqPattern::Backtranslate'); | ||
| 538 | FAST::Bio::Tools::SeqPattern::Backtranslate->import("_reverse_translate_motif"); | ||
| 539 | |||
| 540 | if ($self->type ne 'Amino') { | ||
| 541 | $self->throw( | ||
| 542 | "Can't get backtranslate for ${\$self->type} sequence types.\n" | ||
| 543 | ); | ||
| 544 | } | ||
| 545 | |||
| 546 | return __PACKAGE__->new( | ||
| 547 | -SEQ => _reverse_translate_motif($self->str), | ||
| 548 | -TYPE => 'Dna', | ||
| 549 | ); | ||
| 550 | } | ||
| 551 | |||
| 552 | =head1 _fixpat_1 | ||
| 553 | |||
| 554 | Title : _fixpat_1 | ||
| 555 | Usage : n/a; called automatically by revcom() | ||
| 556 | Purpose : Utility method for revcom() | ||
| 557 | : Converts all {7,5} --> {5,7} (Part I) | ||
| 558 | : and [T^] --> [^T] (Part II) | ||
| 559 | : and *N --> N* (Part III) | ||
| 560 | Returns : String (the new, partially reversed pattern) | ||
| 561 | Argument : String (the expanded pattern) | ||
| 562 | Throws : n/a | ||
| 563 | |||
| 564 | See Also : L<revcom>() | ||
| 565 | |||
| 566 | =cut | ||
| 567 | |||
| 568 | #-------------- | ||
| 569 | sub _fixpat_1 { | ||
| 570 | #-------------- | ||
| 571 | my $pat = shift; | ||
| 572 | |||
| 573 | ## Part I: | ||
| 574 | my (@done,@parts); | ||
| 575 | while(1) { | ||
| 576 | $pat =~ /(.*)\{(\S+?)\}(.*)/ or do{ push @done, $pat; last; }; | ||
| 577 | $pat = $1.'#{'.reverse($2).'}'.$3; | ||
| 578 | # print "1: $1\n2: $2\n3: $3\n"; | ||
| 579 | # print "modified pat: $pat";<STDIN>; | ||
| 580 | @parts = split '#', $pat; | ||
| 581 | push @done, $parts[1]; | ||
| 582 | $pat = $parts[0]; | ||
| 583 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 584 | last if not $pat; | ||
| 585 | } | ||
| 586 | $pat = join('', reverse @done); | ||
| 587 | |||
| 588 | ## Part II: | ||
| 589 | @done = (); | ||
| 590 | while(1) { | ||
| 591 | $pat =~ /(.*)\[(\S+?)\](.*)/ or do{ push @done, $pat; last; }; | ||
| 592 | $pat = $1.'#['.reverse($2).']'.$3; | ||
| 593 | # print "1: $1\n2: $2\n3: $3\n"; | ||
| 594 | # print "modified pat: $pat";<STDIN>; | ||
| 595 | @parts = split '#', $pat; | ||
| 596 | push @done, $parts[1]; | ||
| 597 | $pat = $parts[0]; | ||
| 598 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 599 | last if not $pat; | ||
| 600 | } | ||
| 601 | $pat = join('', reverse @done); | ||
| 602 | |||
| 603 | ## Part III: | ||
| 604 | @done = (); | ||
| 605 | while(1) { | ||
| 606 | $pat =~ /(.*)\*([\w.])(.*)/ or do{ push @done, $pat; last; }; | ||
| 607 | $pat = $1.'#'.$2.'*'.$3; | ||
| 608 | $Processed_asterics{$2}++; | ||
| 609 | # print "1: $1\n2: $2\n3: $3\n"; | ||
| 610 | # print "modified pat: $pat";<STDIN>; | ||
| 611 | @parts = split '#', $pat; | ||
| 612 | push @done, $parts[1]; | ||
| 613 | $pat = $parts[0]; | ||
| 614 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 615 | last if not $pat; | ||
| 616 | } | ||
| 617 | return join('', reverse @done); | ||
| 618 | |||
| 619 | } | ||
| 620 | |||
| 621 | |||
| 622 | =head1 _fixpat_2 | ||
| 623 | |||
| 624 | Title : _fixpat_2 | ||
| 625 | Usage : n/a; called automatically by revcom() | ||
| 626 | Purpose : Utility method for revcom() | ||
| 627 | : Converts all {5,7}Y ---> Y{5,7} | ||
| 628 | : and {10,}. ---> .{10,} | ||
| 629 | Returns : String (the new, partially reversed pattern) | ||
| 630 | Argument : String (the expanded, partially reversed pattern) | ||
| 631 | Throws : n/a | ||
| 632 | |||
| 633 | See Also : L<revcom>() | ||
| 634 | |||
| 635 | =cut | ||
| 636 | |||
| 637 | #-------------- | ||
| 638 | sub _fixpat_2 { | ||
| 639 | #-------------- | ||
| 640 | my $pat = shift; | ||
| 641 | |||
| 642 | local($^W) = 0; | ||
| 643 | my (@done,@parts,$braces); | ||
| 644 | while(1) { | ||
| 645 | # $pat =~ s/(.*)([^])])(\{\S+?\})([\w.])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; }; | ||
| 646 | $pat =~ s/(.*)(\{\S+?\})([\w.])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; }; | ||
| 647 | $braces = $2; | ||
| 648 | $braces =~ s/[{}]//g; | ||
| 649 | $Processed_braces{"$3$braces"}++; | ||
| 650 | # print "modified pat: $pat";<STDIN>; | ||
| 651 | @parts = split '#', $pat; | ||
| 652 | push @done, $parts[1]; | ||
| 653 | $pat = $parts[0]; | ||
| 654 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 655 | last if not $pat; | ||
| 656 | } | ||
| 657 | return join('', reverse @done); | ||
| 658 | } | ||
| 659 | |||
| 660 | |||
| 661 | =head1 _fixpat_3 | ||
| 662 | |||
| 663 | Title : _fixpat_3 | ||
| 664 | Usage : n/a; called automatically by revcom() | ||
| 665 | Purpose : Utility method for revcom() | ||
| 666 | : Converts all {5,7}(XXX) ---> (XXX){5,7} | ||
| 667 | Returns : String (the new, partially reversed pattern) | ||
| 668 | Argument : String (the expanded, partially reversed pattern) | ||
| 669 | Throws : n/a | ||
| 670 | |||
| 671 | See Also : L<revcom>() | ||
| 672 | |||
| 673 | =cut | ||
| 674 | |||
| 675 | #------------- | ||
| 676 | sub _fixpat_3 { | ||
| 677 | #------------- | ||
| 678 | my $pat = shift; | ||
| 679 | |||
| 680 | my (@done,@parts,$braces,$newpat,$oldpat); | ||
| 681 | while(1) { | ||
| 682 | # $pat =~ s/(.+)(\{\S+\})(\(\w+\))(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; }; | ||
| 683 | if( $pat =~ /(.*)(.)(\{\S+\})(\(\w+\))(.*)/) { | ||
| 684 | $newpat = "$1#$2$4$3$5"; | ||
| 685 | ##ps $oldpat = "$1#$2$3$4$5"; | ||
| 686 | # print "1: $1\n2: $2\n3: $3\n4: $4\n5: $5\n"; | ||
| 687 | ##ps $braces = $3; | ||
| 688 | ##ps $braces =~ s/[{}]//g; | ||
| 689 | ##ps if( exists $Processed_braces{"$2$braces"} || exists $Processed_asterics{$2}) { | ||
| 690 | ##ps $pat = $oldpat; # Don't change it. Already processed. | ||
| 691 | # print "saved pat: $pat";<STDIN>; | ||
| 692 | ##ps } else { | ||
| 693 | # print "new pat: $newpat";<STDIN>; | ||
| 694 | $pat = $newpat; # Change it. | ||
| 695 | ##ps } | ||
| 696 | } elsif( $pat =~ /^(\{\S+\})(\(\w+\))(.*)/) { | ||
| 697 | $pat = "#$2$1$3"; | ||
| 698 | } else { | ||
| 699 | push @done, $pat; last; | ||
| 700 | } | ||
| 701 | @parts = split '#', $pat; | ||
| 702 | push @done, $parts[1]; | ||
| 703 | $pat = $parts[0]; | ||
| 704 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 705 | last if not $pat; | ||
| 706 | } | ||
| 707 | return join('', reverse @done); | ||
| 708 | } | ||
| 709 | |||
| 710 | |||
| 711 | =head1 _fixpat_4 | ||
| 712 | |||
| 713 | Title : _fixpat_4 | ||
| 714 | Usage : n/a; called automatically by revcom() | ||
| 715 | Purpose : Utility method for revcom() | ||
| 716 | : Converts all {5,7}[XXX] ---> [XXX]{5,7} | ||
| 717 | Returns : String (the new, partially reversed pattern) | ||
| 718 | Argument : String (the expanded, partially reversed pattern) | ||
| 719 | Throws : n/a | ||
| 720 | |||
| 721 | See Also : L<revcom>() | ||
| 722 | |||
| 723 | =cut | ||
| 724 | |||
| 725 | #--------------- | ||
| 726 | sub _fixpat_4 { | ||
| 727 | #--------------- | ||
| 728 | my $pat = shift; | ||
| 729 | |||
| 730 | my (@done,@parts,$braces,$newpat,$oldpat); | ||
| 731 | while(1) { | ||
| 732 | # $pat =~ s/(.*)(\{\S+\})(\[\w+\])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; }; | ||
| 733 | # $pat =~ s/(.*)([^\w.])(\{\S+\})(\[\w+\])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; }; | ||
| 734 | if( $pat =~ /(.*)(.)(\{\S+\})(\[\w+\])(.*)/) { | ||
| 735 | $newpat = "$1#$2$4$3$5"; | ||
| 736 | $oldpat = "$1#$2$3$4$5"; | ||
| 737 | # print "1: $1\n2: $2\n3: $3\n4: $4\n5: $5\n"; | ||
| 738 | $braces = $3; | ||
| 739 | $braces =~ s/[{}]//g; | ||
| 740 | if( (defined $braces and defined $2) and | ||
| 741 | exists $Processed_braces{"$2$braces"} || exists $Processed_asterics{$2}) { | ||
| 742 | $pat = $oldpat; # Don't change it. Already processed. | ||
| 743 | # print "saved pat: $pat";<STDIN>; | ||
| 744 | } else { | ||
| 745 | $pat = $newpat; # Change it. | ||
| 746 | # print "new pat: $pat";<STDIN>; | ||
| 747 | } | ||
| 748 | } elsif( $pat =~ /^(\{\S+\})(\[\w+\])(.*)/) { | ||
| 749 | $pat = "#$2$1$3"; | ||
| 750 | } else { | ||
| 751 | push @done, $pat; last; | ||
| 752 | } | ||
| 753 | |||
| 754 | @parts = split '#', $pat; | ||
| 755 | push @done, $parts[1]; | ||
| 756 | $pat = $parts[0]; | ||
| 757 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 758 | last if not $pat; | ||
| 759 | } | ||
| 760 | return join('', reverse @done); | ||
| 761 | } | ||
| 762 | |||
| 763 | |||
| 764 | =head1 _fixpat_5 | ||
| 765 | |||
| 766 | Title : _fixpat_5 | ||
| 767 | Usage : n/a; called automatically by revcom() | ||
| 768 | Purpose : Utility method for revcom() | ||
| 769 | : Converts all *[XXX] ---> [XXX]* | ||
| 770 | : and *(XXX) ---> (XXX)* | ||
| 771 | Returns : String (the new, partially reversed pattern) | ||
| 772 | Argument : String (the expanded, partially reversed pattern) | ||
| 773 | Throws : n/a | ||
| 774 | |||
| 775 | See Also : L<revcom>() | ||
| 776 | |||
| 777 | =cut | ||
| 778 | |||
| 779 | #-------------- | ||
| 780 | sub _fixpat_5 { | ||
| 781 | #-------------- | ||
| 782 | my $pat = shift; | ||
| 783 | |||
| 784 | my (@done,@parts,$newpat,$oldpat); | ||
| 785 | while(1) { | ||
| 786 | # $pat =~ s/(.*)(\{\S+\})(\[\w+\])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; }; | ||
| 787 | # $pat =~ s/(.*)([^\w.])(\{\S+\})(\[\w+\])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; }; | ||
| 788 | if( $pat =~ /(.*)(.)\*(\[\w+\]|\(\w+\))(.*)/) { | ||
| 789 | $newpat = "$1#$2$3*$4"; | ||
| 790 | $oldpat = "$1#$2*$3$4"; | ||
| 791 | # print "1: $1\n2: $2\n3: $3\n4: $4\n"; | ||
| 792 | if( exists $Processed_asterics{$2}) { | ||
| 793 | $pat = $oldpat; # Don't change it. Already processed. | ||
| 794 | # print "saved pat: $pat";<STDIN>; | ||
| 795 | } else { | ||
| 796 | $pat = $newpat; # Change it. | ||
| 797 | # print "new pat: $pat";<STDIN>; | ||
| 798 | } | ||
| 799 | } elsif( $pat =~ /^\*(\[\w+\]|\(\w+\))(.*)/) { | ||
| 800 | $pat = "#$1*$3"; | ||
| 801 | } else { | ||
| 802 | push @done, $pat; last; | ||
| 803 | } | ||
| 804 | |||
| 805 | @parts = split '#', $pat; | ||
| 806 | push @done, $parts[1]; | ||
| 807 | $pat = $parts[0]; | ||
| 808 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 809 | last if not $pat; | ||
| 810 | } | ||
| 811 | return join('', reverse @done); | ||
| 812 | } | ||
| 813 | |||
| 814 | |||
| 815 | |||
| 816 | |||
| 817 | |||
| 818 | ############################ | ||
| 819 | # | ||
| 820 | # PS: Added 8/7/00 to allow non-greedy matching patterns | ||
| 821 | # | ||
| 822 | ###################################### | ||
| 823 | |||
| 824 | =head1 _fixpat_6 | ||
| 825 | |||
| 826 | Title : _fixpat_6 | ||
| 827 | Usage : n/a; called automatically by revcom() | ||
| 828 | Purpose : Utility method for revcom() | ||
| 829 | : Converts all ?Y{5,7} ---> Y{5,7}? | ||
| 830 | : and ?(XXX){5,7} ---> (XXX){5,7}? | ||
| 831 | : and ?[XYZ]{5,7} ---> [XYZ]{5,7}? | ||
| 832 | Returns : String (the new, partially reversed pattern) | ||
| 833 | Argument : String (the expanded, partially reversed pattern) | ||
| 834 | Throws : n/a | ||
| 835 | |||
| 836 | See Also : L<revcom>() | ||
| 837 | |||
| 838 | =cut | ||
| 839 | |||
| 840 | #-------------- | ||
| 841 | sub _fixpat_6 { | ||
| 842 | #-------------- | ||
| 843 | my $pat = shift; | ||
| 844 | my (@done,@parts); | ||
| 845 | |||
| 846 | @done = (); | ||
| 847 | while(1) { | ||
| 848 | $pat =~ /(.*)\?(\[\w+\]|\(\w+\)|\w)(\{\S+?\})?(.*)/ or do{ push @done, $pat; last; }; | ||
| 849 | my $quantifier = $3 ? $3 : ""; # Shut up warning if no explicit quantifier | ||
| 850 | $pat = $1.'#'.$2.$quantifier.'?'.$4; | ||
| 851 | # $pat = $1.'#'.$2.$3.'?'.$4; | ||
| 852 | |||
| 853 | # print "1: $1\n2: $2\n3: $3\n"; | ||
| 854 | # print "modified pat: $pat";<STDIN>; | ||
| 855 | @parts = split '#', $pat; | ||
| 856 | push @done, $parts[1]; | ||
| 857 | $pat = $parts[0]; | ||
| 858 | # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>; | ||
| 859 | last if not $pat; | ||
| 860 | } | ||
| 861 | return join('', reverse @done); | ||
| 862 | |||
| 863 | } | ||
| 864 | |||
| 865 | =head2 str | ||
| 866 | |||
| 867 | Title : str | ||
| 868 | Usage : $obj->str($newval) | ||
| 869 | Function: | ||
| 870 | Returns : value of str | ||
| 871 | Args : newvalue (optional) | ||
| 872 | |||
| 873 | |||
| 874 | =cut | ||
| 875 | |||
| 876 | sub str{ | ||
| 877 | my $obj = shift; | ||
| 878 | if( @_ ) { | ||
| 879 | my $value = shift; | ||
| 880 | $obj->{'str'} = $value; | ||
| 881 | } | ||
| 882 | return $obj->{'str'}; | ||
| 883 | |||
| 884 | } | ||
| 885 | |||
| 886 | =head2 type | ||
| 887 | |||
| 888 | Title : type | ||
| 889 | Usage : $obj->type($newval) | ||
| 890 | Function: | ||
| 891 | Returns : value of type | ||
| 892 | Args : newvalue (optional) | ||
| 893 | |||
| 894 | |||
| 895 | =cut | ||
| 896 | |||
| 897 | sub type{ | ||
| 898 | my $obj = shift; | ||
| 899 | if( @_ ) { | ||
| 900 | my $value = shift; | ||
| 901 | $obj->{'type'} = $value; | ||
| 902 | } | ||
| 903 | return $obj->{'type'}; | ||
| 904 | |||
| 905 | } | ||
| 906 | |||
| 907 | 1; | ||
| 908 | |||
| 909 | __END__ |