use of dr.evolution.io.NewickImporter 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.NewickImporter 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.NewickImporter in project beast-mcmc by beast-dev.
the class KCPathDifferenceMetric method main.
public static void main(String[] args) {
try {
//4-taxa example
NewickImporter importer = new NewickImporter("(('A':1.2,'B':0.8):0.5,('C':0.8,'D':1.0):1.1)");
Tree treeOne = importer.importNextTree();
System.out.println("4-taxa tree 1: " + treeOne);
importer = new NewickImporter("((('A':0.8,'B':1.4):0.3,'C':0.7):0.9,'D':1.0)");
Tree treeTwo = importer.importNextTree();
System.out.println("4-taxa tree 2: " + treeTwo + "\n");
ArrayList<Double> lambdaValues = new ArrayList<Double>();
lambdaValues.add(0.0);
lambdaValues.add(0.5);
lambdaValues.add(1.0);
List<Double> metric = (new KCPathDifferenceMetric().getMetric(treeOne, treeTwo, lambdaValues));
List<Double> metric_old = (new KCPathDifferenceMetric().getMetric_old(treeOne, treeTwo, lambdaValues));
System.out.println("\nPaired trees:");
System.out.println("lambda (0.0) = " + metric.get(0) + " old = " + metric_old.get(0));
System.out.println("lambda (0.5) = " + metric.get(1) + " old = " + metric_old.get(1));
System.out.println("lambda (1.0) = " + metric.get(2) + " old = " + metric_old.get(2));
//Additional test for comparing a collection of trees against a (fixed) focal tree
metric = new KCPathDifferenceMetric(treeOne).getMetric(treeTwo, lambdaValues);
metric_old = new KCPathDifferenceMetric(treeOne).getMetric_old(treeTwo, lambdaValues);
System.out.println("\nFocal trees:");
System.out.println("lambda (0.0) = " + metric.get(0) + " old = " + metric_old.get(0));
System.out.println("lambda (0.5) = " + metric.get(1) + " old = " + metric_old.get(1));
System.out.println("lambda (1.0) = " + metric.get(2) + " old = " + metric_old.get(2));
//5-taxa example
importer = new NewickImporter("(((('A':0.6,'B':0.6):0.1,'C':0.5):0.4,'D':0.7):0.1,'E':1.3)");
treeOne = importer.importNextTree();
System.out.println("5-taxa tree 1: " + treeOne);
importer = new NewickImporter("((('A':0.8,'B':1.4):0.1,'C':0.7):0.2,('D':1.0,'E':0.9):1.3)");
treeTwo = importer.importNextTree();
System.out.println("5-taxa tree 2: " + treeTwo + "\n");
//lambda = 0.0 should yield: sqrt(7) = 2.6457513110645907162
//lambda = 1.0 should yield: sqrt(2.96) = 1.7204650534085252911
lambdaValues = new ArrayList<Double>();
lambdaValues.add(0.0);
lambdaValues.add(0.5);
lambdaValues.add(1.0);
metric = (new KCPathDifferenceMetric().getMetric(treeOne, treeTwo, lambdaValues));
System.out.println("\nPaired trees:");
System.out.println("lambda (0.0) = " + metric.get(0) + " old = " + metric_old.get(0));
System.out.println("lambda (0.5) = " + metric.get(1) + " old = " + metric_old.get(1));
System.out.println("lambda (1.0) = " + metric.get(2) + " old = " + metric_old.get(2));
//Additional test for comparing a collection of trees against a (fixed) focal tree
metric = new KCPathDifferenceMetric(treeOne).getMetric(treeTwo, lambdaValues);
System.out.println("\nFocal trees:");
System.out.println("lambda (0.0) = " + metric.get(0) + " old = " + metric_old.get(0));
System.out.println("lambda (0.5) = " + metric.get(1) + " old = " + metric_old.get(1));
System.out.println("lambda (1.0) = " + metric.get(2) + " old = " + metric_old.get(2));
//timings
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
new KCPathDifferenceMetric().getMetric_old(treeOne, treeTwo, lambdaValues);
}
System.out.println("Old algorithm: " + (System.currentTimeMillis() - startTime) + " ms");
startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
new KCPathDifferenceMetric().getMetric(treeOne, treeTwo, lambdaValues);
}
System.out.println("New algorithm: " + (System.currentTimeMillis() - startTime) + " ms");
} catch (Importer.ImportException ie) {
System.err.println(ie);
} catch (IOException ioe) {
System.err.println(ioe);
}
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class TreeMetrics method main.
public static final void main(String[] args) throws Exception {
FileReader reader = new FileReader(args[0]);
NewickImporter importer = new NewickImporter(reader);
Tree[] trees = importer.importTrees(null);
System.out.println("Imported " + trees.length + " trees...");
analyze(trees, 1000);
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class GMRFFixedGridLikelihood method run.
public static void run() throws Exception {
NewickImporter importer = new NewickImporter("((((5:0.5,1:0.2):0.5,0:1):0.2,2:0.8):0.2,3:1.4)");
Tree tree = importer.importNextTree();
double[] data = new double[15];
double[] times = new double[15];
data[0] = 1.0;
times[0] = 0.05;
for (int i = 1; i < data.length; i++) {
data[i] = data[i - 1] + 0.5;
times[i] = times[i - 1] + 0.1;
}
GMRFFixedGridLikelihood like = new GMRFFixedGridLikelihood(tree, new Parameter.Default(data), new Parameter.Default(times), 4);
System.out.println(like.getLogLikelihood());
}
Aggregations