3 # bioperl module for Bio::Tools::SeqPattern
5 # Cared for by Steve Chervitz (sac-at-bioperl.org)
7 # Copyright Steve Chervitz
9 # You may distribute this module under the same terms as perl itself
11 # POD documentation - main docs before the code
15 Bio::Tools::SeqPattern - represent a sequence pattern or motif
19 use Bio::Tools::SeqPattern;
21 my $pat1 = 'T[GA]AA...TAAT';
22 my $pattern1 = Bio::Tools::SeqPattern->new(-SEQ =>$pat1, -TYPE =>'Dna');
24 my $pat2 = '[VILM]R(GXX){3,2}...[^PG]';
25 my $pattern2 = Bio::Tools::SeqPattern->new(-SEQ =>$pat2, -TYPE =>'Amino');
29 L<Bio::Tools::SeqPattern> module encapsulates generic data and
30 methods for manipulating regular expressions describing nucleic or
31 amino acid sequence patterns (a.k.a, "motifs").
33 L<Bio::Tools::SeqPattern> is a concrete class that inherits from L<Bio::Seq>.
35 This class grew out of a need to have a standard module for doing routine
36 tasks with sequence patterns such as:
38 -- Forming a reverse-complement version of a nucleotide sequence pattern
39 -- Expanding patterns containing ambiguity codes
40 -- Checking for invalid regexp characters
41 -- Untainting yet preserving special characters in the pattern
43 Other features to look for in the future:
45 -- Full pattern syntax checking
46 -- Conversion between expanded and condensed forms of the pattern
50 A key motivation for L<Bio::Tools::SeqPattern> is to have a way to
51 generate a reverse complement of a nucleotide sequence pattern.
52 This makes possible simultaneous pattern matching on both sense and
53 anti-sense strands of a query sequence.
55 In principle, one could do such a search more inefficiently by testing
56 against both sense and anti-sense versions of a sequence.
57 It is entirely equivalent to test a regexp containing both sense and
58 anti-sense versions of the *pattern* against one copy of the sequence.
59 The latter approach is much more efficient since:
61 1) You need only one copy of the sequence.
62 2) Only one regexp is executed.
63 3) Regexp patterns are typically much smaller than sequences.
65 Patterns can be quite complex and it is often difficult to
66 generate the reverse complement pattern. The Bioperl SeqPattern.pm
67 addresses this problem, providing a convenient set of tools
68 for working with biological sequence regular expressions.
70 Not all patterns have been tested. If you discover a pattern that
71 is not handled properly by Bio::Tools::SeqPattern.pm, please
72 send me some email (sac@bioperl.org). Thanks.
76 =head2 Extended Alphabet Support
78 This module supports the same set of ambiguity codes for nucleotide
79 sequences as supported by L<Bio::Seq>. These ambiguity codes
80 define the behavior or the L<expand> method.
82 ------------------------------------------
83 Symbol Meaning Nucleic Acid
84 ------------------------------------------
90 M A or C a(M)ino group
93 S C or G (S)trong bond
106 ------------------------------------------
108 ------------------------------------------
130 B Aspartic Acid, Asparagine
131 Z Glutamic Acid, Glutamine
136 =head2 Multiple Format Support
138 Ultimately, this module should be able to build SeqPattern.pm objects
139 using a variety of pattern formats such as ProSite, Blocks, Prints, GCG, etc.
140 Currently, this module only supports patterns using a grep-like syntax.
144 A simple demo script called seq_pattern.pl is included in the examples/
145 directory of the central Bioperl distribution.
149 L<Bio::Seq> - Lightweight sequence object.
155 User feedback is an integral part of the evolution of this and other
156 Bioperl modules. Send your comments and suggestions preferably to one
157 of the Bioperl mailing lists. Your participation is much appreciated.
159 bioperl-l@bioperl.org - General discussion
160 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
162 =head2 Reporting Bugs
164 Report bugs to the Bioperl bug tracking system to help us keep track
165 the bugs and their resolution. Bug reports can be submitted via the
168 http://bugzilla.open-bio.org/
172 Steve Chervitz, sac-at-bioperl.org
176 Copyright (c) 1997-8 Steve Chervitz. All Rights Reserved.
177 This module is free software; you can redistribute it and/or
178 modify it under the same terms as Perl itself.
185 #### END of main POD documentation.
189 # CREATED : 28 Aug 1997
192 package Bio
::Tools
::SeqPattern
;
194 use base
qw(Bio::Root::Root);
197 $ID = 'Bio::Tools::SeqPattern';
199 ## These constants may be more appropriate in a Bio::Dictionary.pm
202 my $PYRIMIDINES = 'CT';
205 my $Regexp_chars = '\w,.\*()\[\]<>\{\}^\$'; # quoted for use in regexps
207 ## Package variables used in reverse complementing.
208 my (%Processed_braces, %Processed_asterics);
210 #####################################################################################
212 #####################################################################################
218 Usage : my $seqpat = Bio::Tools::SeqPattern->new();
219 Purpose : Verifies that the type is correct for superclass (Bio::Seq.pm)
220 : and calls superclass constructor last.
222 Argument : Parameters passed to new()
223 Throws : Exception if the pattern string (seq) is empty.
224 Comments : The process of creating a new SeqPattern.pm object
225 : ensures that the pattern string is untained.
227 See Also : L<Bio::Root::Root::new>,
228 L<Bio::Seq::_initialize>
235 my($class, %param) = @_;
237 my $self = $class->SUPER::new
(%param);
238 my ($seq,$type) = $self->_rearrange([qw(SEQ TYPE)], %param);
240 $seq || $self->throw("Empty pattern.");
242 # Get the type ready for Bio::Seq.pm
243 if ($type =~ /nuc|[dr]na/i) {
245 } elsif ($type =~ /amino|pep|prot/i) {
248 $seq =~ tr/a-z/A-Z/; #ps 8/8/00 Canonicalize to upper case
259 Usage : $mypat->alphabet_ok;
260 Purpose : Checks for invalid regexp characters.
261 : Overrides Bio::Seq::alphabet_ok() to allow
262 : additional regexp characters ,.*()[]<>{}^$
263 : in addition to the standard genetic alphabet.
264 : Also untaints the pattern and sets the sequence
265 : object's sequence to the untained string.
266 Returns : Boolean (1 | 0)
268 Throws : Exception if the pattern contains invalid characters.
269 Comments : Does not call the superclass method.
270 : Actually permits any alphanumeric, not just the
271 : standard genetic alphabet.
280 return 1 if $self->{'_alphabet_checked'};
282 $self->{'_alphabet_checked'} = 1;
284 my $pat = $self->seq();
286 if($pat =~ /[^$Regexp_chars]/io) {
287 $self->throw("Pattern contains invalid characters: $pat",
288 'Legal characters: a-z,A-Z,0-9,,.*()[]<>{}^$ ');
291 # Untaint pattern (makes code taint-safe).
292 $pat =~ /([$Regexp_chars]+)/io;
293 $self->setseq(uc($1));
294 # print STDERR "\npattern ok: $pat\n";
301 Usage : $seqpat_object->expand();
302 Purpose : Expands the sequence pattern using special ambiguity codes.
303 Example : $pat = $seq_pat->expand();
304 Returns : String containing fully expanded sequence pattern
306 Throws : Exception if sequence type is not recognized
307 : (i.e., is not one of [DR]NA, Amino)
309 See Also : L<Extended Alphabet Support>, L<_expand_pep>(), L<_expand_nuc>()
318 if($self->type =~ /[DR]na/i) { $self->_expand_nuc(); }
319 elsif($self->type =~ /Amino/i) { $self->_expand_pep(); }
321 $self->throw("Don't know how to expand ${\$self->type} patterns.\n");
329 Usage : n/a; automatically called by expand()
330 Purpose : Expands peptide patterns
331 Returns : String (the expanded pattern)
332 Argument : String (the unexpanded pattern)
335 See Also : L<expand>(), L<_expand_nuc>()
342 my ($self,$pat) = @_;
348 ## Avoid nested situations: [bmnq] --/--> [[$ZED]mnq]
349 ## Yet correctly deal with: fze[bmnq] ---> f[$BEE]e[$ZEDmnq]
350 if($pat =~ /\[\w*[BZ]\w*\]/) {
351 $pat =~ s/\[(\w*)B(\w*)\]/\[$1$ZED$2\]/g;
352 $pat =~ s/\[(\w*)Z(\w*)\]/\[$1$BEE$2\]/g;
353 $pat =~ s/B/\[$ZED\]/g;
354 $pat =~ s/Z/\[$BEE\]/g;
356 $pat =~ s/B/\[$ZED\]/g;
357 $pat =~ s/Z/\[$BEE\]/g;
359 $pat =~ s/\((.)\)/$1/g; ## Doing these last since:
360 $pat =~ s/\[(.)\]/$1/g; ## Pattern could contain [B] (for example)
370 Purpose : Expands nucleotide patterns
371 Returns : String (the expanded pattern)
372 Argument : String (the unexpanded pattern)
375 See Also : L<expand>(), L<_expand_pep>()
382 my ($self,$pat) = @_;
392 ## Avoid nested situations: [ya] --/--> [[ct]a]
393 ## Yet correctly deal with: sg[ya] ---> [gc]g[cta]
394 if($pat =~ /\[\w*[RYSWMK]\w*\]/) {
395 $pat =~ s/\[(\w*)R(\w*)\]/\[$1$PURINES$2\]/g;
396 $pat =~ s/\[(\w*)Y(\w*)\]/\[$1$PYRIMIDINES$2\]/g;
397 $pat =~ s/\[(\w*)S(\w*)\]/\[$1GC$2\]/g;
398 $pat =~ s/\[(\w*)W(\w*)\]/\[$1AT$2\]/g;
399 $pat =~ s/\[(\w*)M(\w*)\]/\[$1AC$2\]/g;
400 $pat =~ s/\[(\w*)K(\w*)\]/\[$1GT$2\]/g;
401 $pat =~ s/\[(\w*)V(\w*)\]/\[$1ACG$2\]/g;
402 $pat =~ s/\[(\w*)H(\w*)\]/\[$1ACT$2\]/g;
403 $pat =~ s/\[(\w*)D(\w*)\]/\[$1AGT$2\]/g;
404 $pat =~ s/\[(\w*)B(\w*)\]/\[$1CGT$2\]/g;
405 $pat =~ s/R/\[$PURINES\]/g;
406 $pat =~ s/Y/\[$PYRIMIDINES\]/g;
407 $pat =~ s/S/\[GC\]/g;
408 $pat =~ s/W/\[AT\]/g;
409 $pat =~ s/M/\[AC\]/g;
410 $pat =~ s/K/\[GT\]/g;
411 $pat =~ s/V/\[ACG\]/g;
412 $pat =~ s/H/\[ACT\]/g;
413 $pat =~ s/D/\[AGT\]/g;
414 $pat =~ s/B/\[CGT\]/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;
427 $pat =~ s/\((.)\)/$1/g; ## Doing thses last since:
428 $pat =~ s/\[(.)\]/$1/g; ## Pattern could contain [y] (for example)
439 Purpose : Forms a pattern capable of recognizing the reverse complement
440 : version of a nucleotide sequence pattern.
441 Example : $pattern_object->revcom();
442 : $pattern_object->revcom(1); ## returns expanded rev complement pattern.
443 Returns : Object reference for a new Bio::Tools::SeqPattern containing
444 : the revcom of the current pattern as its sequence.
445 Argument : (1) boolean (optional) (default= false)
446 : true : expand the pattern before rev-complementing.
447 : false: don't expand pattern before or after rev-complementing.
448 Throws : Exception if called for amino acid sequence pattern.
449 Comments : This method permits the simultaneous searching of both
450 : sense and anti-sense versions of a nucleotide pattern
451 : by means of a grep-type of functionality in which any
452 : number of patterns may be or-ed into the recognition
454 : Overrides Bio::Seq::revcom() and calls it first thing.
455 : The order of _fixpat() calls is critical.
457 See Also : L<Bio::Seq::revcom()>, L<_fixpat_1>(), L<_fixpat_2>(), L<_fixpat_3>(), L<_fixpat_4>(), L<_fixpat_5>()
464 my($self,$expand) = @_;
466 if ($self->type !~ /Dna|Rna/i) {
467 $self->throw("Can't get revcom for ${\$self->type} sequence types.\n");
469 # return $self->{'_rev'} if defined $self->{'_rev'};
472 my $str = $self->str;
473 $str =~ tr/acgtrymkswhbvdnxACGTRYMKSWHBVDNX/tgcayrkmswdvbhnxTGCAYRKMSWDVBHNX/;
474 my $rev = CORE
::reverse $str;
475 $rev =~ tr/[](){}<>/][)(}{></;
478 $rev = $self->_expand_nuc($rev);
479 # print "\nExpanded: $rev\n";
482 %Processed_braces = ();
483 %Processed_asterics = ();
485 my $fixrev = _fixpat_1
($rev);
486 # print "FIX 1: $fixrev";<STDIN>;
488 $fixrev = _fixpat_2
($fixrev);
489 # print "FIX 2: $fixrev";<STDIN>;
491 $fixrev = _fixpat_3
($fixrev);
492 # print "FIX 3: $fixrev";<STDIN>;
494 $fixrev = _fixpat_4
($fixrev);
495 # print "FIX 4: $fixrev";<STDIN>;
497 $fixrev = _fixpat_5
($fixrev);
498 # print "FIX 5: $fixrev";<STDIN>;
500 ##### Added by ps 8/7/00 to allow non-greedy matching
501 $fixrev = _fixpat_6
($fixrev);
502 # print "FIX 6: $fixrev";<STDIN>;
504 # $self->{'_rev'} = $fixrev;
506 return new Bio
::Tools
::SeqPattern
(-seq
=>$fixrev, -type
=>$self->type);
514 Usage : n/a; called automatically by revcom()
515 Purpose : Utility method for revcom()
516 : Converts all {7,5} --> {5,7} (Part I)
517 : and [T^] --> [^T] (Part II)
518 : and *N --> N* (Part III)
519 Returns : String (the new, partially reversed pattern)
520 Argument : String (the expanded pattern)
523 See Also : L<revcom>()
535 $pat =~ /(.*)\{(\S+?)\}(.*)/ or do{ push @done, $pat; last; };
536 $pat = $1.'#{'.reverse($2).'}'.$3;
537 # print "1: $1\n2: $2\n3: $3\n";
538 # print "modified pat: $pat";<STDIN>;
539 @parts = split '#', $pat;
540 push @done, $parts[1];
542 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
545 $pat = join('', reverse @done);
550 $pat =~ /(.*)\[(\S+?)\](.*)/ or do{ push @done, $pat; last; };
551 $pat = $1.'#['.reverse($2).']'.$3;
552 # print "1: $1\n2: $2\n3: $3\n";
553 # print "modified pat: $pat";<STDIN>;
554 @parts = split '#', $pat;
555 push @done, $parts[1];
557 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
560 $pat = join('', reverse @done);
565 $pat =~ /(.*)\*([\w.])(.*)/ or do{ push @done, $pat; last; };
566 $pat = $1.'#'.$2.'*'.$3;
567 $Processed_asterics{$2}++;
568 # print "1: $1\n2: $2\n3: $3\n";
569 # print "modified pat: $pat";<STDIN>;
570 @parts = split '#', $pat;
571 push @done, $parts[1];
573 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
576 return join('', reverse @done);
584 Usage : n/a; called automatically by revcom()
585 Purpose : Utility method for revcom()
586 : Converts all {5,7}Y ---> Y{5,7}
587 : and {10,}. ---> .{10,}
588 Returns : String (the new, partially reversed pattern)
589 Argument : String (the expanded, partially reversed pattern)
592 See Also : L<revcom>()
602 my (@done,@parts,$braces);
604 # $pat =~ s/(.*)([^])])(\{\S+?\})([\w.])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; };
605 $pat =~ s/(.*)(\{\S+?\})([\w.])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; };
607 $braces =~ s/[{}]//g;
608 $Processed_braces{"$3$braces"}++;
609 # print "modified pat: $pat";<STDIN>;
610 @parts = split '#', $pat;
611 push @done, $parts[1];
613 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
616 return join('', reverse @done);
623 Usage : n/a; called automatically by revcom()
624 Purpose : Utility method for revcom()
625 : Converts all {5,7}(XXX) ---> (XXX){5,7}
626 Returns : String (the new, partially reversed pattern)
627 Argument : String (the expanded, partially reversed pattern)
630 See Also : L<revcom>()
639 my (@done,@parts,$braces,$newpat,$oldpat);
641 # $pat =~ s/(.+)(\{\S+\})(\(\w+\))(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; };
642 if( $pat =~ /(.*)(.)(\{\S+\})(\(\w+\))(.*)/) {
643 $newpat = "$1#$2$4$3$5";
644 ##ps $oldpat = "$1#$2$3$4$5";
645 # print "1: $1\n2: $2\n3: $3\n4: $4\n5: $5\n";
647 ##ps $braces =~ s/[{}]//g;
648 ##ps if( exists $Processed_braces{"$2$braces"} || exists $Processed_asterics{$2}) {
649 ##ps $pat = $oldpat; # Don't change it. Already processed.
650 # print "saved pat: $pat";<STDIN>;
652 # print "new pat: $newpat";<STDIN>;
653 $pat = $newpat; # Change it.
655 } elsif( $pat =~ /^(\{\S+\})(\(\w+\))(.*)/) {
658 push @done, $pat; last;
660 @parts = split '#', $pat;
661 push @done, $parts[1];
663 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
666 return join('', reverse @done);
673 Usage : n/a; called automatically by revcom()
674 Purpose : Utility method for revcom()
675 : Converts all {5,7}[XXX] ---> [XXX]{5,7}
676 Returns : String (the new, partially reversed pattern)
677 Argument : String (the expanded, partially reversed pattern)
680 See Also : L<revcom>()
689 my (@done,@parts,$braces,$newpat,$oldpat);
691 # $pat =~ s/(.*)(\{\S+\})(\[\w+\])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; };
692 # $pat =~ s/(.*)([^\w.])(\{\S+\})(\[\w+\])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; };
693 if( $pat =~ /(.*)(.)(\{\S+\})(\[\w+\])(.*)/) {
694 $newpat = "$1#$2$4$3$5";
695 $oldpat = "$1#$2$3$4$5";
696 # print "1: $1\n2: $2\n3: $3\n4: $4\n5: $5\n";
698 $braces =~ s/[{}]//g;
699 if( (defined $braces and defined $2) and
700 exists $Processed_braces{"$2$braces"} || exists $Processed_asterics{$2}) {
701 $pat = $oldpat; # Don't change it. Already processed.
702 # print "saved pat: $pat";<STDIN>;
704 $pat = $newpat; # Change it.
705 # print "new pat: $pat";<STDIN>;
707 } elsif( $pat =~ /^(\{\S+\})(\[\w+\])(.*)/) {
710 push @done, $pat; last;
713 @parts = split '#', $pat;
714 push @done, $parts[1];
716 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
719 return join('', reverse @done);
726 Usage : n/a; called automatically by revcom()
727 Purpose : Utility method for revcom()
728 : Converts all *[XXX] ---> [XXX]*
729 : and *(XXX) ---> (XXX)*
730 Returns : String (the new, partially reversed pattern)
731 Argument : String (the expanded, partially reversed pattern)
734 See Also : L<revcom>()
743 my (@done,@parts,$newpat,$oldpat);
745 # $pat =~ s/(.*)(\{\S+\})(\[\w+\])(.*)/$1#$3$2$4/ or do{ push @done, $pat; last; };
746 # $pat =~ s/(.*)([^\w.])(\{\S+\})(\[\w+\])(.*)/$1$2#$4$3$5/ or do{ push @done, $pat; last; };
747 if( $pat =~ /(.*)(.)\*(\[\w+\]|\(\w+\))(.*)/) {
748 $newpat = "$1#$2$3*$4";
749 $oldpat = "$1#$2*$3$4";
750 # print "1: $1\n2: $2\n3: $3\n4: $4\n";
751 if( exists $Processed_asterics{$2}) {
752 $pat = $oldpat; # Don't change it. Already processed.
753 # print "saved pat: $pat";<STDIN>;
755 $pat = $newpat; # Change it.
756 # print "new pat: $pat";<STDIN>;
758 } elsif( $pat =~ /^\*(\[\w+\]|\(\w+\))(.*)/) {
761 push @done, $pat; last;
764 @parts = split '#', $pat;
765 push @done, $parts[1];
767 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
770 return join('', reverse @done);
777 ############################
779 # PS: Added 8/7/00 to allow non-greedy matching patterns
781 ######################################
786 Usage : n/a; called automatically by revcom()
787 Purpose : Utility method for revcom()
788 : Converts all ?Y{5,7} ---> Y{5,7}?
789 : and ?(XXX){5,7} ---> (XXX){5,7}?
790 : and ?[XYZ]{5,7} ---> [XYZ]{5,7}?
791 Returns : String (the new, partially reversed pattern)
792 Argument : String (the expanded, partially reversed pattern)
795 See Also : L<revcom>()
807 $pat =~ /(.*)\?(\[\w+\]|\(\w+\)|\w)(\{\S+?\})?(.*)/ or do{ push @done, $pat; last; };
808 my $quantifier = $3 ?
$3 : ""; # Shut up warning if no explicit quantifier
809 $pat = $1.'#'.$2.$quantifier.'?'.$4;
810 # $pat = $1.'#'.$2.$3.'?'.$4;
812 # print "1: $1\n2: $2\n3: $3\n";
813 # print "modified pat: $pat";<STDIN>;
814 @parts = split '#', $pat;
815 push @done, $parts[1];
817 # print "done: $parts[1]<---\nnew pat: $pat<---";<STDIN>;
820 return join('', reverse @done);
827 Usage : $obj->str($newval)
829 Returns : value of str
830 Args : newvalue (optional)
839 $obj->{'str'} = $value;
841 return $obj->{'str'};
848 Usage : $obj->type($newval)
850 Returns : value of type
851 Args : newvalue (optional)
860 $obj->{'type'} = $value;
862 return $obj->{'type'};
870 #########################################################################
872 #########################################################################
874 =head1 FOR DEVELOPERS ONLY
878 Information about the various data members of this module is provided
879 for those wishing to modify or understand the code. Two things to bear
884 =item 1 Do NOT rely on these in any code outside of this module.
886 All data members are prefixed with an underscore to signify that they
887 are private. Always use accessor methods. If the accessor doesn't
888 exist or is inadequate, create or modify an accessor (and let me know,
891 =item 2 This documentation may be incomplete and out of date.
893 It is easy for this documentation to become obsolete as this module is
894 still evolving. Always double check this info and search for members
899 An instance of Bio::Tools::RestrictionEnzyme.pm is a blessed reference
900 to a hash containing all or some of the following fields:
903 ------------------------------------------------------------------------
904 _rev : The corrected reverse complement of the fully expanded pattern.
906 INHERITED DATA MEMBERS:
908 _seq : (From Bio::Seq.pm) The original, unexpanded input sequence after untainting.
909 _type : (From Bio::Seq.pm) 'Dna' or 'Amino'