PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Bio/Tools/SeqPattern.pm

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