10
$\begingroup$
"tgtaaaaacaagaaaaacatggaatgtataatactataaataaaaaaagtaggaagaagaaccaaaaca\
 aagggcgttttaaagagagacgaat"

based on the above string (a DNA sequence) I wish to extract sub-strings of length $>5$ characters that compose of consecutive "a"'s or "t"'s and not include "c" and "g".

For example, few of the substrings are "taaaaa", "tataat", "tataaat" $\ldots$ so on and so forth.

I tried using StringCases but I cannot identify a pattern for the StringCase function that I can employ to select only sub-strings that are combinations of "a"'s or "t"'s.

Moreover, I would be grateful if someone can direct me to a place where I can study more about how to use patterns for strings in Mathematica.

$\endgroup$

1 Answer 1

16
$\begingroup$

You can use Alternatives (|) and Repeated in StringCases.

dna = "tgtaaaaacaagaaaaacatggaatgtataatactataaataaaaaaagtaggaagaagaaccaaaacaaagggcgttttaaagagagacgaat";

StringCases[dna, Longest@Repeated["a" | "t", {6, Infinity}]]

(* {"taaaaa", "tataata", "tataaataaaaaaa", "ttttaaa"} *)

You can learn more from the String Patterns tutorial in the documentation.

Hope this helps.

PS: You may also want to take a look at the GenomeData function and other Life Sciences & Medicine functions in Mathematica.

$\endgroup$
2
  • $\begingroup$ Thank you so much !! works great. I was trying to create a pattern and that was what was preventing me from searching for Repeated ! Thanks again $\endgroup$
    – Ali Hashmi
    Commented Dec 25, 2015 at 5:55
  • 1
    $\begingroup$ The regex version: StringCases[dna, RegularExpression["[at]{6,}"]] $\endgroup$ Commented Mar 13, 2016 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.