use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class TreeSummary method summarizeTrees.
private Tree summarizeTrees(int burnin, Tree consensusTree, CladeSystem cladeSystem, String inputFileName) throws IOException {
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));
try {
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (counter >= burnin) {
cladeSystem.addSubTrees(tree);
}
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;
}
Tree bestTree = cladeSystem.getBestTree(consensusTree);
return bestTree;
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class DataModelImporter method importFromTreeFile.
public Map importFromTreeFile(String fileName, Map dataModel) throws IOException, Importer.ImportException {
Tree tree = null;
try {
Reader reader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
while (line != null && line.length() == 0) {
line = bufferedReader.readLine();
}
reader = new FileReader(fileName);
if ((line != null && line.toUpperCase().contains("#NEXUS"))) {
// is a NEXUS file
NexusImporter importer = new NexusImporter(reader);
tree = importer.importNextTree();
} else {
NewickImporter importer = new NewickImporter(reader);
tree = importer.importNextTree();
}
bufferedReader.close();
} catch (IOException e) {
throw new IOException(e.getMessage());
}
if (tree != null) {
dataModel.put("tree", TreeUtils.newick(tree));
}
return dataModel;
}
use of dr.evolution.io.NexusImporter in project beast-mcmc by beast-dev.
the class Coevolve method readTreeFile.
/**
* @param fileName
* @throws java.io.IOException
*/
private Tree readTreeFile(String fileName) throws java.io.IOException {
Alignment alignment = null;
Tree[] trees = null;
TaxonList taxonList = null;
try {
FileReader reader = new FileReader(fileName);
NexusImporter importer = new NexusImporter(reader);
boolean done = false;
while (!done) {
try {
NexusImporter.NexusBlock block = importer.findNextBlock();
if (block == NexusImporter.TAXA_BLOCK) {
if (taxonList != null) {
throw new NexusImporter.MissingBlockException("TAXA block already defined");
}
taxonList = importer.parseTaxaBlock();
} else if (block == NexusImporter.DATA_BLOCK) {
// A data block doesn't need a taxon block before it
// but if one exists then it will use it.
alignment = importer.parseDataBlock(taxonList);
if (taxonList == null) {
taxonList = alignment;
}
} else if (block == NexusImporter.TREES_BLOCK) {
trees = importer.parseTreesBlock(taxonList);
if (taxonList == null) {
taxonList = alignment;
}
} else {
// Ignore the block..
}
} catch (EOFException ex) {
done = true;
}
}
} catch (Importer.ImportException ime) {
System.err.println("Error reading alignment: " + ime);
}
return trees[0];
}
use of dr.evolution.io.NexusImporter 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.NexusImporter in project beast-mcmc by beast-dev.
the class TreeAnnotator method setTreeHeightsByCA.
boolean setTreeHeightsByCA(MutableTree targetTree, final String inputFileName, final int burnin) throws IOException, Importer.ImportException {
progressStream.println("Setting node heights...");
progressStream.println("0 25 50 75 100");
progressStream.println("|--------------|--------------|--------------|--------------|");
int reportStepSize = totalTrees / 60;
if (reportStepSize < 1)
reportStepSize = 1;
final FileReader fileReader = new FileReader(inputFileName);
final NexusImporter importer = new NexusImporter(fileReader, true);
// this call increments the clade counts and it shouldn't
// this is remedied with removeClades call after while loop below
CladeSystem cladeSystem = new CladeSystem(targetTree);
final int nClades = cladeSystem.getCladeMap().size();
// allocate posterior tree nodes order once
int[] postOrderList = new int[nClades];
BitSet[] ctarget = new BitSet[nClades];
BitSet[] ctree = new BitSet[nClades];
for (int k = 0; k < nClades; ++k) {
ctarget[k] = new BitSet();
ctree[k] = new BitSet();
}
cladeSystem.getTreeCladeCodes(targetTree, ctarget);
// temp collecting heights inside loop allocated once
double[] hs = new double[nClades];
// heights total sum from posterior trees
double[] ths = new double[nClades];
totalTreesUsed = 0;
int counter = 0;
while (importer.hasTree()) {
final Tree tree = importer.importNextTree();
if (counter >= burnin) {
TreeUtils.preOrderTraversalList(tree, postOrderList);
cladeSystem.getTreeCladeCodes(tree, ctree);
for (int k = 0; k < nClades; ++k) {
int j = postOrderList[k];
for (int i = 0; i < nClades; ++i) {
if (isSubSet(ctarget[i], ctree[j])) {
hs[i] = tree.getNodeHeight(tree.getNode(j));
}
}
}
for (int k = 0; k < nClades; ++k) {
ths[k] += hs[k];
}
totalTreesUsed += 1;
}
if (counter > 0 && counter % reportStepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
counter++;
}
cladeSystem.removeClades(targetTree, targetTree.getRoot(), true);
for (int k = 0; k < nClades; ++k) {
ths[k] /= totalTreesUsed;
final NodeRef node = targetTree.getNode(k);
targetTree.setNodeHeight(node, ths[k]);
}
fileReader.close();
progressStream.println();
progressStream.println();
return true;
}
Aggregations