0
$\begingroup$

This is an example in the help pages (Working with patterns: Highlight Patterns):

    SeedRandom[12345];
    dna=StringJoin[Table[{"a","c","t"}[[RandomInteger[{1,3}]]],{50}]]

    StringReplace[dna,x:("a"~~_~~"a"):>"\!\(\*StyleBox[\""<>x<>"\",
FontColor->RGBColor[1,0,0],FontSize->18,FontWeight->\"Bold\"]\)"]
"\!\(\*StyleBox[\"aca\",\n    FontColor->RGBColor[1,FontWeight->\"Bold\"]\)atccttttattactcccacccatcccatc\!\(\*StyleBox[\"ata\",\n    FontColor->RGBColor[1,0,0],FontSize->18,FontWeight->\"Bold\"]\)ctatcttctct\!\(\*StyleBox[\"ata\",\n    FontColor->RGBColor[1,0,0],FontSize->18,FontWeight->\"Bold\"]\)t"   

screen grab

How to highlight the black part of the DNA with a pattern# (any professional name)?

Method 1: not perfect, some problem see appendix I

rules=MapThread[RuleDelayed,{StringSplit[dna,x:("ag"~~_~~_~~"t"~~_~~"ca")],Style[#,Red]&/@StringSplit[dna,x:("ag"~~_~~_~~"t"~~_~~"ca")]}];
StringReplace[dna,rules]
acttaagctttcgggtgagtgtactgcccactcctagctttcctttagttcttagacgagctggcgcgtagatggctacgtgacttgaatgcattagtgtctagatgcattatggaaccattccaatcgtctcggagggctagtgctgccatcttggttatgtggtatcgtgatttgaatcagtccagcgctataaagtcttcgctgtt~~aggctgca~~ggagccgagggcatcggacgtacaaaatgcgggggtagactgacgaccgcagaggccacatacgacaggccgccggcgtgcgactagatagccgcgagtggggttggctaagcacatcga~~agtgtaca~~gaaaacccctaccccatgagtcgtgcagacgcctccagaaaccagg~~agtttaca~~gcaggacctgagaactaatgttaatgctccatcacctctgcatgcctatccattaagcaaggcacactcagcccctaaaagcgagcggctccccaggagaa

enter image description here

[update1] sub question 1: How to remove the StringExpression (~~) to improve it? thanks for rm and Silva's tips, [this is solved].

**[unsolved-main problem]**sub Question 2: How to achieve the effect in a better way without appendix I's problem? my first thought is to use Except,but failed

Following are the CellPrint effects:

 CellPrint@TextCell[Row[List@@StringReplace[dna,rules],""],"Text"]
"acttaagctttcgggtgagtgtactgcccactcctagctttcctttagttcttagacgagctggcgcgtagatggctacgtgacttgaatgcattagtgtctagatgcattatggaaccattccaatcgtctcggagggctagtgctgccatcttggttatgtggtatcgtgatttgaatcagtccagcgctataaagtcttcgctgtt""aggctgca""ggagccgagggcatcggacgtacaaaatgcgggggtagactgacgaccgcagaggccacatacgacaggccgccggcgtgcgactagatagccgcgagtggggttggctaagcacatcga""agtgtaca""gaaaacccctaccccatgagtcgtgcagacgcctccagaaaccagg""agtttaca""gcaggacctgagaactaatgttaatgctccatcacctctgcatgcctatccattaagcaaggcacactcagcccctaaaagcgagcggctccccaggagaa"

Appendix I

When I'm trying golden's suggest to shorten my example, @m_goldberg

   SeedRandom[12345];
    dna = StringJoin[Table[{"a", "c", "t"}[[RandomInteger[{1, 3}]]], {50}]]
    rules=MapThread[RuleDelayed,{StringSplit[dna,x:("a"~~_~~"a")],Style[#,Red]    &/@StringSplit[dna,x:("a"~~_~~"a")]}];
    StringReplace[dna,rules]

enter image description here

$\endgroup$
8
  • $\begingroup$ In general, you'd be better off using Row to join styled strings, and this is answered in mathematica.stackexchange.com/q/10990/5. Also see WReach's answer on converting to StandardForm and then using StringJoin $\endgroup$
    – rm -rf
    Commented May 5, 2013 at 16:45
  • 1
    $\begingroup$ Try replacing your Style[#, Red]& with ToString[Style[#, Red], StandardForm]& in the rules. $\endgroup$
    – Silvia
    Commented May 5, 2013 at 18:14
  • $\begingroup$ Please use shorter strings in your examples. There is no need to use a string of 500 characters. You could surely produce an example with 100 characters or less. $\endgroup$
    – m_goldberg
    Commented May 5, 2013 at 23:12
  • $\begingroup$ @rm -rf thanks for tips, I see some examples in Help Pages use Row To remove StringExpression~~ ,so I tried that. , $\endgroup$ Commented May 6, 2013 at 2:21
  • $\begingroup$ @Silvia it works, thanks $\endgroup$ Commented May 6, 2013 at 2:23

0

Browse other questions tagged or ask your own question.