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.


1 Answer 1


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.

  • $\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
    Dec 25, 2015 at 5:55
  • 1
    $\begingroup$ The regex version: StringCases[dna, RegularExpression["[at]{6,}"]] $\endgroup$ 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.