use of dr.evolution.io.TreeImporter in project beast-mcmc by beast-dev.
the class TreeAnnotator method summarizeTrees.
private Tree summarizeTrees(int burnin, CladeSystem cladeSystem, String inputFileName) throws /*, boolean useSumCladeCredibility */
IOException {
Tree bestTree = null;
double bestScore = Double.NEGATIVE_INFINITY;
progressStream.println("Analyzing " + totalTreesUsed + " trees...");
progressStream.println("0 25 50 75 100");
progressStream.println("|--------------|--------------|--------------|--------------|");
int stepSize = totalTrees / 60;
if (stepSize < 1)
stepSize = 1;
int counter = 0;
int bestTreeNumber = 0;
TreeImporter importer = new NexusImporter(new FileReader(inputFileName), true);
try {
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (counter >= burnin) {
double score = scoreTree(tree, cladeSystem);
// progressStream.println(score);
if (score > bestScore) {
bestTree = tree;
bestScore = score;
bestTreeNumber = counter + 1;
}
}
if (counter > 0 && counter % stepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
counter++;
}
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
return null;
}
progressStream.println();
progressStream.println();
progressStream.println("Best tree: " + bestTree.getId() + " (tree number " + bestTreeNumber + ")");
// if (useSumCladeCredibility) {
// progressStream.println("Highest Sum Clade Credibility: " + bestScore);
// } else {
progressStream.println("Highest Log Clade Credibility: " + bestScore);
return bestTree;
}
use of dr.evolution.io.TreeImporter in project beast-mcmc by beast-dev.
the class TreeLengthFinder method report.
/**
* Recursively analyzes trees files.
*
* @param name the file to analyze (if this is a directory then the files within it are analyzed)
* @param burnin the burnin to use
*/
private void report(String name, int burnin) {
double treeLength = 0.0;
int count = 0;
try {
FileReader fileReader = new FileReader(new File(name));
TreeImporter importer = new NexusImporter(fileReader);
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (count >= burnin) {
treeLength += TreeLength.FACTORY.createStatistic().getSummaryStatistic(tree)[0];
}
count++;
}
treeLength /= (count - burnin);
System.out.println(name + "\t" + burnin + "\t" + treeLength);
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
} catch (IOException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
}
}
use of dr.evolution.io.TreeImporter in project beast-mcmc by beast-dev.
the class TreeSpaceFrame method processTrees1.
private int processTrees1(InputFile inputFile) throws IOException {
PrintStream progressStream = System.out;
int totalTrees = 10000;
int totalTreesUsed = 0;
progressStream.println("Reading trees (bar assumes 10,000 trees)...");
progressStream.println("0 25 50 75 100");
progressStream.println("|--------------|--------------|--------------|--------------|");
int stepSize = totalTrees / 60;
if (stepSize < 1)
stepSize = 1;
CladeSystem cladeSystem = document.getCladeSystem();
FileReader fileReader = new FileReader(inputFile.getFile());
TreeImporter importer = new dr.evolution.io.NexusImporter(fileReader);
try {
totalTrees = 0;
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (totalTrees >= inputFile.getBurnin()) {
cladeSystem.add(tree, true);
totalTreesUsed += 1;
}
if (totalTrees > 0 && totalTrees % stepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
totalTrees++;
}
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
return 0;
}
fileReader.close();
progressStream.println();
progressStream.println();
if (totalTrees < 1) {
System.err.println("No trees");
return 0;
}
if (totalTreesUsed <= 1) {
if (inputFile.getBurnin() > 0) {
System.err.println("No trees to use: burnin too high");
return 0;
}
}
cladeSystem.normalizeClades(totalTreesUsed);
progressStream.println("Total trees read: " + totalTrees);
if (inputFile.getBurnin() > 0) {
progressStream.println("Ignoring first " + inputFile.getBurnin() + " trees.");
}
int cladeCount = cladeSystem.getCladeMap().keySet().size();
progressStream.println("Total unique clades: " + cladeCount);
progressStream.println();
progressStream.println("Processing trees for correlated clades:");
fileReader = new FileReader(inputFile.getFile());
importer = new dr.evolution.io.NexusImporter(fileReader);
try {
totalTrees = 0;
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (totalTrees >= inputFile.getBurnin()) {
cladeSystem.addCooccurances(tree);
}
if (totalTrees > 0 && totalTrees % stepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
totalTrees++;
}
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
return 0;
}
fileReader.close();
progressStream.println();
progressStream.println();
double THRESHOLD = 0.05;
PrintWriter writer = new PrintWriter("clade_co-occurance.txt");
writer.println("source\tsize\ttarget\tco-occurence");
java.util.List<CladeSystem.Clade> allClades = cladeSystem.getClades();
for (CladeSystem.Clade clade1 : allClades) {
String name1;
int card1 = clade1.bits.cardinality();
if (card1 == 1) {
name1 = clade1.label;
} else {
name1 = "clade" + (clade1.index + 1);
}
if (clade1.parents != null) {
for (CladeSystem.Clade clade2 : clade1.parents.keySet()) {
String name2;
int card2 = clade2.bits.cardinality();
name2 = "clade" + (clade2.index + 1);
double value = clade1.parents.get(clade2);
value /= totalTreesUsed;
if (value > THRESHOLD) {
if (card1 > card2) {
writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + name2 + "_" + card2 + "\t" + value);
} else {
writer.println(name2 + "_" + card2 + "\t" + card2 + "\t" + name1 + "_" + card1 + "\t" + value);
}
}
}
}
}
writer.close();
writer = new PrintWriter("clade_frequency.txt");
writer.println("source\tsize\tfrequency");
for (CladeSystem.Clade clade1 : allClades) {
String name1;
int card1 = clade1.bits.cardinality();
if (card1 == 1) {
name1 = clade1.label;
} else {
name1 = "clade" + (clade1.index + 1);
}
double value = clade1.count;
value /= totalTreesUsed;
if (value > THRESHOLD) {
writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + value);
}
}
writer.close();
progressStream.println();
cladePlotter.setCladeSystem(cladeSystem);
return totalTreesUsed;
}
use of dr.evolution.io.TreeImporter in project beast-mcmc by beast-dev.
the class GetNSCountsFromTrees method analyze.
private void analyze(File inputFile, int burnin, BranchSet branchSet, List<Set> inclusionSets, List<Set> exclusionSets, double[] siteList) {
if (summary) {
resultsStream.print("tree" + SEP + "cN" + SEP + "uN" + SEP + "cS" + SEP + "uS" + SEP + "cNrate" + SEP + "cSrate" + SEP + "dN/dS" + "\n");
} else {
resultsStream.print("tree" + SEP + "branch" + SEP + "N/S" + SEP + "site" + SEP + "height/date" + SEP + "fromState" + SEP + "toState");
if (branchInfo) {
resultsStream.print(SEP + "branchLength" + SEP + "branchCN/S" + SEP + "branchUN/S" + "\n");
} else {
resultsStream.print("\n");
}
}
int totalTrees = 10000;
int totalStars = 0;
System.out.println("Reading and analyzing trees (bar assumes 10,000 trees)...");
System.out.println("0 25 50 75 100");
System.out.println("|--------------|--------------|--------------|--------------|");
int stepSize = totalTrees / 60;
if (stepSize < 1)
stepSize = 1;
int count = 0;
int treeUsed = 1;
try {
FileReader fileReader = new FileReader(inputFile);
TreeImporter importer = new NexusImporter(fileReader);
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (count >= burnin) {
getNSCounts(tree, treeUsed, branchSet, inclusionSets, exclusionSets, siteList);
treeUsed++;
}
count++;
if (totalTrees > 0 && totalTrees % stepSize == 0) {
System.out.print("*");
totalStars++;
if (totalStars % 61 == 0)
System.out.print("\n");
System.out.flush();
}
totalTrees++;
}
System.out.print("\n");
} catch (Importer.ImportException e) {
progressStream.println("Error Parsing Input Tree: " + e.getMessage());
} catch (IOException e) {
progressStream.println("Error Parsing Input Tree: " + e.getMessage());
}
}
use of dr.evolution.io.TreeImporter in project beast-mcmc by beast-dev.
the class GetAncestralSequenceFromSplitTrait method analyze.
/**
* Recursively analyzes log files.
*
* @param treeAnnotatorInputFile the file to analyze (if this is a directory then the files within it are analyzed)
* @throws dr.inference.trace.TraceException
* if the trace file is in the wrong format or corrupted
*/
private void analyze(File treeAnnotatorInputFile) throws TraceException {
try {
FileReader fileReader = new FileReader(treeAnnotatorInputFile);
TreeImporter importer = new NexusImporter(fileReader);
FlexibleTree tree = (FlexibleTree) importer.importNextTree();
for (int i = 0; i < tree.getNodeCount(); i++) {
Hashtable<Integer, State> states = new Hashtable<Integer, State>();
for (Iterator<String> j = tree.getNodeAttributeNames(tree.getNode(i)); j.hasNext(); ) {
String name = j.next();
if (name.indexOf("states_") >= 0) {
Integer d = Integer.parseInt(name.replaceFirst("states_", "").replaceFirst("\\..+", ""));
//= new State(name.);
State s;
if (states.containsKey(d)) {
s = states.get(d);
} else {
s = new State(d);
}
if (name.matches("states_" + d + ".prob")) {
Object o = tree.getNodeAttribute(tree.getNode(i), name);
double probability = (Double) o;
s.setProbability(probability);
} else if (name.matches("states_" + d)) {
Object o = tree.getNodeAttribute(tree.getNode(i), name);
String value = (String) o;
s.setState(value.replaceAll("\"", ""));
} else if (name.matches("states_" + d + ".set.prob")) {
/* Not necessary but lets parse it anyways */
Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
double[] probabilities = new double[o.length];
for (int k = 0; k < o.length; k++) {
probabilities[k] = (Double) o[k];
}
s.setProbabilities(probabilities);
} else if (name.matches("states_" + d + ".set")) {
/* Not necessary but lets parse it anyways */
Object[] o = (Object[]) tree.getNodeAttribute(tree.getNode(i), name);
String[] set = new String[o.length];
for (int k = 0; k < o.length; k++) {
set[k] = ((String) o[k]).replaceAll("\"", "");
}
s.setSet(set);
}
// }
states.put(d, s);
}
}
State[] statesArray = states.values().toArray(new State[states.size()]);
Arrays.sort(statesArray);
/* Set the default length to the number of characters that it would need */
StringBuffer sb = new StringBuffer(statesArray.length * statesArray[0].getState().length());
for (State s : statesArray) {
sb.append(s.getState());
}
tree.setNodeAttribute(tree.getNode(i), "seq", sb.toString());
}
/* Export the new tree with the new sequences */
TreeExporter exporter = new NexusExporter(System.out);
exporter.exportTree(tree);
System.out.println("Begin trees;");
System.out.println("\ttree max_tree = " + tree.toString());
System.out.println("End;");
} catch (IOException e) {
System.err.println("Error Parsing Input log: " + e.getMessage());
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
}
}
Aggregations