use of dr.evolution.io.NexusImporter 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());
}
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class Utils method importTreeFromFile.
// END: importTaxaFromFile
public static Tree importTreeFromFile(File file) throws IOException, ImportException {
Tree tree = null;
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
if (line.toUpperCase().startsWith("#NEXUS")) {
NexusImporter importer = new NexusImporter(reader);
tree = importer.importTree(null);
} else {
NewickImporter importer = new NewickImporter(reader);
tree = importer.importTree(null);
}
reader.close();
return tree;
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class TreeStatFrame method processTreeFile.
protected void processTreeFile(File inFile, File outFile) throws IOException, Importer.ImportException {
processTreeFileAction.setEnabled(false);
BufferedReader r = new BufferedReader(new FileReader(inFile));
String line = r.readLine();
r.close();
final ProgressMonitorInputStream in = new ProgressMonitorInputStream(this, "Reading " + inFile.getName(), new FileInputStream(inFile));
in.getProgressMonitor().setMillisToDecideToPopup(0);
in.getProgressMonitor().setMillisToPopup(0);
final Reader reader = new InputStreamReader(new BufferedInputStream(in));
// final Reader reader = new FileReader(inFile);
final TreeImporter importer;
if (line.toUpperCase().startsWith("#NEXUS")) {
importer = new NexusImporter(reader);
} else {
reader.close();
importer = new NewickImporter(reader);
}
final Tree firstTree = importer.importNextTree();
boolean isUltrametric = TreeUtils.isUltrametric(firstTree);
boolean isBinary = TreeUtils.isBinary(firstTree);
boolean stop = false;
// check that the trees conform with the requirements of the selected statistics
for (int i = 0; i < treeStatData.statistics.size(); i++) {
TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
String label = tss.getSummaryStatisticName();
if (!isUltrametric && !tss.allowsNonultrametricTrees()) {
if (JOptionPane.showConfirmDialog(this, "Warning: These trees may not be ultrametric and this is\na requirement of the " + label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
stop = true;
break;
}
// don't ask the question again...
isUltrametric = true;
}
if (!isBinary && !tss.allowsPolytomies()) {
if (JOptionPane.showConfirmDialog(this, "Warning: These trees may not be strictly bifurcating and this is\na requirement of the " + label + " statistic. Do you wish to continue?", "Warning", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
stop = true;
break;
}
// don't ask the question again...
isBinary = true;
}
}
if (stop) {
processTreeFileAction.setEnabled(true);
return;
}
final PrintWriter writer = new PrintWriter(new FileWriter(outFile));
// Thread readThread = new Thread() {
// public void run() {
Tree tree = firstTree;
writer.print("state");
for (int i = 0; i < treeStatData.statistics.size(); i++) {
TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
int dim = tss.getStatisticDimensions(tree);
for (int j = 0; j < dim; j++) {
writer.print("\t" + tss.getStatisticLabel(tree, j));
}
}
writer.println();
state = 0;
do {
writer.print(state);
for (int i = 0; i < treeStatData.statistics.size(); i++) {
TreeSummaryStatistic tss = (TreeSummaryStatistic) treeStatData.statistics.get(i);
double[] stats = tss.getSummaryStatistic(tree);
for (int j = 0; j < stats.length; j++) {
writer.print("\t" + stats[j]);
}
}
writer.println();
state += 1;
final int currentState = state;
in.getProgressMonitor().setNote("Processing Tree " + currentState + "...");
// EventQueue.invokeLater(
// new Runnable() {
// public void run() {
// progressLabel.setText("Processing Tree " + currentState + "...");
// }
// });
// try {
tree = importer.importNextTree();
// } catch (final IOException e) {
// EventQueue.invokeLater(
// new Runnable() {
// public void run() {
// JOptionPane.showMessageDialog(TreeStatFrame.this, "File I/O Error: " + e.getMessage(),
// "File I/O Error",
// JOptionPane.ERROR_MESSAGE);
// }
// });
// } catch (final Importer.ImportException e) {
// EventQueue.invokeLater(
// new Runnable() {
// public void run() {
// JOptionPane.showMessageDialog(TreeStatFrame.this, "Error importing tree: " + e.getMessage(),
// "Tree Import Error",
// JOptionPane.ERROR_MESSAGE);
// }
// });
// }
} while (tree != null);
// }
// };
//
// readThread.start();
// while (readThread.isAlive()) {
// Thread.yield();
// }
reader.close();
writer.close();
progressLabel.setText("" + state + " trees processed.");
processTreeFileAction.setEnabled(true);
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class TreeStatFrame method importFromFile.
protected void importFromFile(File file) throws IOException, Importer.ImportException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
Tree tree = null;
if (line.toUpperCase().startsWith("#NEXUS")) {
NexusImporter importer = new NexusImporter(reader);
tree = importer.importTree(null);
} else {
reader.close();
reader = new BufferedReader(new FileReader(file));
NewickImporter importer = new NewickImporter(reader);
tree = importer.importTree(null);
}
treeStatData.allTaxa = TreeUtils.getLeafSet(tree);
statusLabel.setText(Integer.toString(treeStatData.allTaxa.size()) + " taxa loaded.");
reader.close();
fireDataChanged();
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class TreeShape method main.
public static void main(String[] args) throws Exception {
NexusImporter importer = new NexusImporter(new FileReader(args[0]));
Tree[] trees = importer.importTrees(null);
System.out.println("File = " + args[0]);
double[] treeness = new double[trees.length];
for (int i = 0; i < treeness.length; i++) {
treeness[i] = getTreeness(trees[i]);
}
System.out.println("Mean treeness = " + dr.stats.DiscreteStatistics.mean(treeness));
System.out.println("Lower (95%) treeness = " + dr.stats.DiscreteStatistics.quantile(0.025, treeness));
System.out.println("Upper (95%) treeness = " + dr.stats.DiscreteStatistics.quantile(0.975, treeness));
}
Aggregations