Skip to content

Commit

Permalink
make Newick parser work for MemoryFriendlyTreeSet. fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouckaert committed Jul 30, 2024
1 parent 4cabf06 commit d3d6a5c
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions src/beastfx/app/treeannotator/TreeAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.util.*;

import beastfx.app.tools.Application;
import beastfx.app.treeannotator.TreeAnnotator0.HeightsSummary;
import beastfx.app.treeannotator.TreeAnnotator0.Target;
import beastfx.app.treeannotator.services.NodeHeightSettingService;
import beastfx.app.treeannotator.services.TopologySettingService;
import beastfx.app.treeannotator.services.UserTargetTreeTopologyService;
Expand Down Expand Up @@ -212,16 +210,28 @@ public void reset() throws FileNotFoundException {
fin = new BufferedReader(new FileReader(new File(inputFileName)));
lineNr = 0;
try {
while (fin.ready()) {
final String str = nextLine();
if (str == null) {
return;
}
final String lower = str.toLowerCase();
if (lower.matches("^\\s*begin\\s+trees;\\s*$")) {
parseTreesBlock();
return;
}
if (isNexus) {
while (fin.ready()) {
final String str = nextLine();
if (str == null) {
return;
}
final String lower = str.toLowerCase();
if (lower.matches("^\\s*begin\\s+trees;\\s*$")) {
parseTreesBlock();
return;
}
}
} else {
while (fin.ready() && lineNr < burninCount) {
final String str = nextLine();
if (str == null) {
return;
}
if (str.trim().length() > 2 && !str.trim().startsWith("#")) {
lineNr++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -372,6 +382,10 @@ public Tree next() throws IOException {
String str = nextLine();
if (!isNexus) {
TreeParser treeParser;
if (taxa == null) {
collectTaxaNames(str);
}
current++;

if (origin != -1) {
treeParser = new TreeParser(taxa, str, origin, false);
Expand Down Expand Up @@ -410,6 +424,50 @@ public Tree next() throws IOException {
}
return null;
}

private void collectTaxaNames(String str) {
taxa = new ArrayList<>();
int i = 0;
while (i < str.length()) {
char c = str.charAt(i);
switch (c) {
case '(':
case ')':
case ',':
// ignore
i++;
break;
case '[':
// eat up meta data
while (i < str.length() && str.charAt(i) != ']') {
i++;
}
break;
case ':':
// eat up length
while (i < str.length() && !(str.charAt(i) == ')'|| str.charAt(i) == ',')) {
i++;
}
break;
default:
StringBuilder b = new StringBuilder();
boolean done = false;
while (i < str.length() && !done) {
c = str.charAt(i);
done = c == ')' || c == ':' || c == ',' || c == '(' || c == '[';
if (!done) {
if (c != '\'' && c != '"') {
b.append(c);
}
i++;
} else {
taxa.add(b.toString());
}
}

}
}
}
}
TreeSet treeSet;

Expand Down

0 comments on commit d3d6a5c

Please sign in to comment.