2
$\begingroup$

I have many strings of the following kind:

string="(Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_\
acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_\
Achr4T07148_001:0.2741664889)_D_1_0_0_9260000000_:0.2005997307,(Oryza_\
sativa_Os10g21560_1:0.9733414222,Musa_acuminata_GSMUA_Achr2T06610_001:\
0.4881902598)0_1700000000:0.0623981619)_D_0_5__:0.13917144990000008);"

These are trees downloaded from the public database "TreeBase". I import them as strings and I want to get rid of what officially is the node names in the Newick format.

To be precise, I want to delete everything that stands between a ")" symbol and THE NEXT ":" symbol.

For my example, I want to get the following:

"(Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_\
acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_\
Achr4T07148_001:0.2741664889):0.2005997307,(Oryza_\
sativa_Os10g21560_1:0.9733414222,Musa_acuminata_GSMUA_Achr2T06610_001:\
0.4881902598):0.0623981619):0.13917144990000008);"

However, if I do

StringReplace[string, ")" ~~ __ ~~ ":" -> ")"]

what I get is this:

"(Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_\
acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_\
Achr4T07148_001:0.2741664889)0.13917144990000008);"

I.e., Mathematica replaces everything between the first ")" symbol and the LAST ":" symbol. But that's not what I want.

Can anyone help with this please? Thanks so much!

$\endgroup$
6
  • 2
    $\begingroup$ I am not very clear on what you are trying to do, but something tells me you should toss in a Shortest in your string pattern. Try StringReplace[string, Shortest[")"~~__~~":"] -> ")"]. I get the following output: (Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_\ acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_\ Achr4T07148_001:0.2741664889)0.2005997307,(Oryza_sativa_Os10g21560_1:\ 0.9733414222,Musa_acuminata_GSMUA_Achr2T06610_001:0.4881902598)0.\ 0623981619)0.13917144990000008); $\endgroup$ May 13 at 18:57
  • $\begingroup$ Crossposted here. $\endgroup$ May 13 at 19:26
  • $\begingroup$ Or StringReplace[string, ")" ~~ __?(StringFreeQ[#, ":"] &) ~~ ":" -> ")"] $\endgroup$
    – Bob Hanlon
    May 13 at 19:49
  • 2
    $\begingroup$ Is it possible to download the data in JSON format? $\endgroup$
    – Carl Woll
    May 13 at 21:56
  • 1
    $\begingroup$ The TreeBASE website notes that all data are available in NEXUS format and the WL documentation notes this as an available import format. Maybe it would be easier if you used the built-in import tools? $\endgroup$ May 14 at 10:04
2
$\begingroup$

This is a job for regular expressions:

string="(Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_
acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_
Achr4T07148_001:0.2741664889)D_1_0_0_9260000000:0.2005997307,(Oryza_
sativa_Os10g21560_1:0.9733414222,Musa_acuminata_GSMUA_Achr2T06610_001:
0.4881902598)0_1700000000:0.0623981619)D_0_5_:0.13917144990000008);"

string1="(Vitis_vinifera_VvNAC09_GSVIVT01009651001:1.174911034,((Musa_
acuminata_GSMUA_Achr7T26050_001:0.1846057474,Musa_acuminata_GSMUA_
Achr4T07148_001:0.2741664889)0.13917144990000008);"


regex = "\\).*?:";
StringReplace[s, RegularExpression[regex] -> "):"] == string1

(*True*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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