use of dr.evolution.io.Importer in project beast-mcmc by beast-dev.
the class Coevolve method readNexusFile.
/**
* @param fileName
* @throws java.io.IOException
*/
private Alignment readNexusFile(String fileName) throws java.io.IOException {
Alignment alignment = 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) {
// ignore tree block
} else {
// Ignore the block..
}
} catch (EOFException ex) {
done = true;
}
}
} catch (Importer.ImportException ime) {
System.err.println("Error reading alignment: " + ime);
}
return alignment;
}
use of dr.evolution.io.Importer 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.Importer 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.Importer 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.Importer in project beast-mcmc by beast-dev.
the class TaxaOriginTrait method main.
/*
* Arguments:
* 0: Trait name
* 1: File name for list of taxaNames of interest
* 2: File name for trees (output from BranchJumpPlotter)
* 3: Output file name root
* */
public static void main(String[] args) {
try {
BufferedReader taxonReader = new BufferedReader(new FileReader(args[1]));
HashSet<String> tempTaxa = new HashSet<String>();
String line;
while ((line = taxonReader.readLine()) != null) {
tempTaxa.add(line);
}
String[] taxa = tempTaxa.toArray(new String[tempTaxa.size()]);
NexusImporter importer = new NexusImporter(new FileReader(args[2]));
importer.setSuppressWarnings(true);
ArrayList<FlexibleTree> tempTrees = new ArrayList<FlexibleTree>();
int count = 0;
while (importer.hasTree()) {
if (count % 100 == 0) {
System.out.println("Loaded " + count + " trees");
}
tempTrees.add((FlexibleTree) importer.importNextTree());
count++;
}
FlexibleTree[] trees = tempTrees.toArray(new FlexibleTree[tempTrees.size()]);
TaxaOriginTrait examiner = new TaxaOriginTrait(taxa, trees, args[0], args[3]);
examiner.tabulateOrigins();
} catch (IOException e) {
System.out.println("Failed to read files");
} catch (Importer.ImportException e) {
System.out.println("Failed to import trees");
}
}
Aggregations