use of dr.evolution.io.Importer 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.Importer 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.Importer in project beast-mcmc by beast-dev.
the class SeqGen method main.
public static void main(String[] argv) {
String treeFileName = argv[0];
String outputFileStem = argv[1];
int length = 500;
double[] frequencies = new double[] { 0.25, 0.25, 0.25, 0.25 };
double kappa = 10.0;
double alpha = 0.5;
double substitutionRate = argv.length < 3 ? 1.0E-3 : Double.parseDouble(argv[2]);
int categoryCount = argv.length < 4 ? 8 : Integer.parseInt(argv[3]);
//1.56E-6;
double damageRate = argv.length < 5 ? 0 : Double.parseDouble(argv[4]);
System.out.println("substitutionRate = " + substitutionRate + "; categoryCount = " + categoryCount + "; damageRate = " + damageRate);
FrequencyModel freqModel = new FrequencyModel(dr.evolution.datatype.Nucleotides.INSTANCE, frequencies);
HKY hkyModel = new HKY(kappa, freqModel);
SiteModel siteModel = null;
if (categoryCount > 1) {
siteModel = new GammaSiteModel(hkyModel, alpha, categoryCount);
} else {
// no rate heterogeneity
siteModel = new GammaSiteModel(hkyModel);
}
List<Tree> trees = new ArrayList<Tree>();
FileReader reader = null;
try {
reader = new FileReader(treeFileName);
// TreeImporter importer = new NexusImporter(reader);
TreeImporter importer = new NewickImporter(reader);
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
trees.add(tree);
System.out.println("tree height = " + tree.getNodeHeight(tree.getRoot()) + "; leave nodes = " + tree.getExternalNodeCount());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (Importer.ImportException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
SeqGen seqGen = new SeqGen(length, substitutionRate, freqModel, hkyModel, siteModel, damageRate);
int i = 1;
for (Tree tree : trees) {
Alignment alignment = seqGen.simulate(tree);
FileWriter writer = null;
try {
// writer = new FileWriter(outputFileStem + (i < 10 ? "00" : (i < 100 ? "0" : "")) + i + ".nex");
// NexusExporter exporter = new NexusExporter(writer);
//
// exporter.exportAlignment(alignment);
//
// writer.close();
String outputFileName = outputFileStem + "-" + substitutionRate + ".fasta";
writer = new FileWriter(outputFileName);
BufferedWriter bf = new BufferedWriter(writer);
FastaExporter exporter = new FastaExporter(bf);
exporter.exportSequences(alignment.getSequenceList());
bf.close();
System.out.println("Write " + i + "th sequence file : " + outputFileName);
i++;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
use of dr.evolution.io.Importer 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.Importer in project beast-mcmc by beast-dev.
the class EmpiricalTreeDistributionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
final String fileName = xo.getStringAttribute(FILE_NAME);
// default is random tree
int startingTree = xo.getAttribute(STARTING_TREE, -1);
// default is random draw
boolean iterate = xo.getAttribute(ITERATE, false);
if (iterate && startingTree < 0) {
startingTree = 0;
}
Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");
TaxonList taxa = (TaxonList) xo.getChild(TaxonList.class);
final File file = FileHelpers.getFile(fileName);
Tree[] trees = null;
NexusImporter importer = null;
try {
FileReader reader = new FileReader(file);
importer = new NexusImporter(reader);
if (!iterate) {
// Re-order taxon numbers to original TaxonList order
trees = importer.importTrees(taxa, true);
reader.close();
}
} catch (FileNotFoundException e) {
throw new XMLParseException(e.getMessage());
} catch (IOException e) {
throw new XMLParseException(e.getMessage());
} catch (Importer.ImportException e) {
throw new XMLParseException(e.getMessage());
}
if (iterate) {
Logger.getLogger("dr.evomodel").info(" Iterate over each tree from file, " + fileName);
return new EmpiricalTreeDistributionModel(importer, startingTree);
} else {
Logger.getLogger("dr.evomodel").info(" Randomly jump between " + trees.length + " trees from file, " + fileName);
return new EmpiricalTreeDistributionModel(trees, startingTree);
}
}
Aggregations