1 / 16

Strings and Patterns in Perl

Strings and Patterns in Perl. Ellen Walker Bioinformatics Hiram College. Finding a Fixed Pattern. my $string = “ATAAGCTTATCG”; my $pattern = “GCT”; print index($string,$pattern); print index (reverse($string), $pattern);. Finding multiple occurrences. my $start = 0;

Download Presentation

Strings and Patterns in Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and Patterns in Perl Ellen Walker Bioinformatics Hiram College

  2. Finding a Fixed Pattern • my $string = “ATAAGCTTATCG”; • my $pattern = “GCT”; • print index($string,$pattern); • print index (reverse($string), $pattern);

  3. Finding multiple occurrences • my $start = 0; • print index($string, $pattern, $start); • $start = index($string, $pattern, $start) + length($pattern); • print index($string, $pattern, $start); • $start = index($string, $pattern, $start) + length($pattern); When do you stop searching?

  4. Finding all (non-overlapping) occurrences my $start = 0; my $found; $found = index($string, $pattern, $start); while ($found > -1) { print “$pattern found at $found\n”; $start = $found + length($pattern); $found = index($string, $pattern, $start); }

  5. Pattern Matching Operators • Three types of operators (so far) • Translation: tr • Substitution: s and g • Matching: m • Used with =~ to modify a string • Example: • $complement =~ tr/ACGT/TGCA/

  6. Translation • The tr operator takes two sequences of characters of the same length • Every character in the first string is changed to the character at the same position in the second string • This is destructive; save the old string before you use it!

  7. Translation examples • my $string = “actgTGCA”; • my $capitalizedString = $string; • $capitalizedString =~ tr/actg/ACTG/; • my $lowerCaseString = $string; • $lowerCaseString =~tr/ACTG/actg/;

  8. Substitution • Replaces an entire pattern with another pattern • Patterns need not be the same length • s changes only the first occurrence • Add g to change all occurrences • Example: • $string =~ s/T/U/g

  9. Substitution Examples • My $aminoAcids = $dna; • $aminoAcids =~ s/AUG/Met/g; • $aminoAcids =~ s/GGU/Gly/g; • $aminoAcids =~ s/GGG/Gly/g; A sequence of these substitutions will not really work to translate RNA (why not?)

  10. Matching • Not destructive to the string • Tests if the string matched (can be used as a condition in an if statement. • Example: if ($string =~ m/T/) print “String is DNA, not RNA\n”;

  11. Non-Exact Patterns • Can be used with s or m • Include • wildcard characters, • multiple option matches • capturing

  12. Wildcard characters . Matches any character * Matches 0 or more characters equal to the preceding character + matches 1 or more… ^ before the beginning of the string $ matches after the end of the string

  13. Multiple option matches [actg] Matches one character in the set a, c, t, g [^A-Z] Matches one character that is not A-Z TAG|TGA|TAA Matches either TAG, TGA or TAA • Example:my $Rpattern = ‘A|G’;

  14. Capturing Patterns • Any pattern in parentheses is “captured” • The pattern can be recovered with \1, \2 etc. • Example: • s/(…)(…)/\2\1/ switches the first two codons in the string.

  15. Slides are not Complete! • Page 56-57 of the Perl book has an extensive list of regular expression examples.

  16. Examples • 6-mer palindrome (.)(.)(.)\3\2\1 • Pair of nucleotides repeated at least three times (.)(.).*\1\2.*\1\2 • Strings that end with GGA GGA$

More Related