# How to take all sub-strings in a string that are combinations of specified characters only

"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.

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.

• 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 Dec 25 '15 at 5:55
• The regex version: StringCases[dna, RegularExpression["[at]{6,}"]] Mar 13 '16 at 8:08