0

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

HyperGroups
  • 8,619
  • 1
  • 26
  • 63
  • In general, you'd be better off using Row to join styled strings, and this is answered in http://mathematica.stackexchange.com/q/10990/5. Also see WReach's answer on converting to StandardForm and then using StringJoin – rm -rf May 05 '13 at 16:45
  • 1
    Try replacing your Style[#, Red]& with ToString[Style[#, Red], StandardForm]& in the rules. – Silvia May 05 '13 at 18:14
  • 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. – m_goldberg May 05 '13 at 23:12
  • @rm -rf thanks for tips, I see some examples in Help Pages use Row To remove StringExpression~~ ,so I tried that. , – HyperGroups May 06 '13 at 02:21
  • @Silvia it works, thanks – HyperGroups May 06 '13 at 02:23
  • @m_goldberg some problem show up when I tried shorten the example in method 1 – HyperGroups May 06 '13 at 02:24
  • Ok, since Silvia's comment solved your problem and that is exactly one of the solutions in the linked post (which is very similar to this one), I'm marking it a duplicate. Please let me know if this is not the case (and why) – rm -rf May 06 '13 at 02:31
  • @rm-rf how do you think about appendix I's problem, I think to acieve method 1's effect is the main problem of this post. – HyperGroups May 06 '13 at 02:36

0 Answers0