use of dr.evolution.tree.Tree 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.tree.Tree in project beast-mcmc by beast-dev.
the class TreeSpaceFrame method processTrees1.
private int processTrees1(InputFile inputFile) throws IOException {
PrintStream progressStream = System.out;
int totalTrees = 10000;
int totalTreesUsed = 0;
progressStream.println("Reading trees (bar assumes 10,000 trees)...");
progressStream.println("0 25 50 75 100");
progressStream.println("|--------------|--------------|--------------|--------------|");
int stepSize = totalTrees / 60;
if (stepSize < 1)
stepSize = 1;
CladeSystem cladeSystem = document.getCladeSystem();
FileReader fileReader = new FileReader(inputFile.getFile());
TreeImporter importer = new dr.evolution.io.NexusImporter(fileReader);
try {
totalTrees = 0;
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (totalTrees >= inputFile.getBurnin()) {
cladeSystem.add(tree, true);
totalTreesUsed += 1;
}
if (totalTrees > 0 && totalTrees % stepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
totalTrees++;
}
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
return 0;
}
fileReader.close();
progressStream.println();
progressStream.println();
if (totalTrees < 1) {
System.err.println("No trees");
return 0;
}
if (totalTreesUsed <= 1) {
if (inputFile.getBurnin() > 0) {
System.err.println("No trees to use: burnin too high");
return 0;
}
}
cladeSystem.normalizeClades(totalTreesUsed);
progressStream.println("Total trees read: " + totalTrees);
if (inputFile.getBurnin() > 0) {
progressStream.println("Ignoring first " + inputFile.getBurnin() + " trees.");
}
int cladeCount = cladeSystem.getCladeMap().keySet().size();
progressStream.println("Total unique clades: " + cladeCount);
progressStream.println();
progressStream.println("Processing trees for correlated clades:");
fileReader = new FileReader(inputFile.getFile());
importer = new dr.evolution.io.NexusImporter(fileReader);
try {
totalTrees = 0;
while (importer.hasTree()) {
Tree tree = importer.importNextTree();
if (totalTrees >= inputFile.getBurnin()) {
cladeSystem.addCooccurances(tree);
}
if (totalTrees > 0 && totalTrees % stepSize == 0) {
progressStream.print("*");
progressStream.flush();
}
totalTrees++;
}
} catch (Importer.ImportException e) {
System.err.println("Error Parsing Input Tree: " + e.getMessage());
return 0;
}
fileReader.close();
progressStream.println();
progressStream.println();
double THRESHOLD = 0.05;
PrintWriter writer = new PrintWriter("clade_co-occurance.txt");
writer.println("source\tsize\ttarget\tco-occurence");
java.util.List<CladeSystem.Clade> allClades = cladeSystem.getClades();
for (CladeSystem.Clade clade1 : allClades) {
String name1;
int card1 = clade1.bits.cardinality();
if (card1 == 1) {
name1 = clade1.label;
} else {
name1 = "clade" + (clade1.index + 1);
}
if (clade1.parents != null) {
for (CladeSystem.Clade clade2 : clade1.parents.keySet()) {
String name2;
int card2 = clade2.bits.cardinality();
name2 = "clade" + (clade2.index + 1);
double value = clade1.parents.get(clade2);
value /= totalTreesUsed;
if (value > THRESHOLD) {
if (card1 > card2) {
writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + name2 + "_" + card2 + "\t" + value);
} else {
writer.println(name2 + "_" + card2 + "\t" + card2 + "\t" + name1 + "_" + card1 + "\t" + value);
}
}
}
}
}
writer.close();
writer = new PrintWriter("clade_frequency.txt");
writer.println("source\tsize\tfrequency");
for (CladeSystem.Clade clade1 : allClades) {
String name1;
int card1 = clade1.bits.cardinality();
if (card1 == 1) {
name1 = clade1.label;
} else {
name1 = "clade" + (clade1.index + 1);
}
double value = clade1.count;
value /= totalTreesUsed;
if (value > THRESHOLD) {
writer.println(name1 + "_" + card1 + "\t" + card1 + "\t" + value);
}
}
writer.close();
progressStream.println();
cladePlotter.setCladeSystem(cladeSystem);
return totalTreesUsed;
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class OrderNexusTranslationTable method main.
public static void main(String[] args) throws Importer.ImportException, IOException {
NexusImporter importer = new NexusImporter(new FileReader(args[0]));
Tree[] trees = importer.importTrees(null);
System.out.println("Read " + trees.length + " trees from " + args[0]);
String newFileName = args[0] + ".new";
PrintStream ps = new PrintStream(new FileOutputStream(newFileName));
NexusExporter exporter = new NexusExporter(ps);
exporter.setTreePrefix("STATE_");
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(7);
exporter.setNumberFormat(format);
exporter.setSortedTranslationTable(true);
exporter.exportTrees(trees, false);
ps.flush();
ps.close();
System.out.println("Wrote " + trees.length + " trees to " + newFileName);
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class BeagleSeqSimTest method simulateCodon.
// END: simulateAminoAcid
static void simulateCodon() {
try {
boolean calculateLikelihood = true;
System.out.println("Test case 6: simulate codons");
MathUtils.setSeed(666);
int sequenceLength = 10;
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
// create tree
NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
Tree tree = importer.importTree(null);
TreeModel treeModel = new TreeModel(tree);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
// create branch rate model
BranchRateModel branchRateModel = new DefaultBranchRateModel();
// create Frequency Model
Parameter freqs = new Parameter.Default(Utils.UNIFORM_CODON_FREQUENCIES);
FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL, freqs);
// create substitution model
Parameter alpha = new Parameter.Default(1, 10);
Parameter beta = new Parameter.Default(1, 5);
// Parameter kappa = new Parameter.Default(1, 1);
MG94CodonModel mg94 = new MG94CodonModel(Codons.UNIVERSAL, alpha, beta, freqModel);
HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(mg94);
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
sequenceLength - 1, // every
1);
partitionsList.add(partition1);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
Alignment alignment = simulator.simulate(simulateInPar, false);
System.out.println(alignment.toString());
if (calculateLikelihood) {
// NewBeagleSequenceLikelihood nbtl = new
// NewBeagleSequenceLikelihood(alignment, treeModel,
// substitutionModel, (SiteModel) siteRateModel,
// branchRateModel, null, false,
// PartialsRescalingScheme.DEFAULT);
ConvertAlignment convert = new ConvertAlignment(Nucleotides.INSTANCE, GeneticCode.UNIVERSAL, alignment);
BeagleTreeLikelihood nbtl = new //
BeagleTreeLikelihood(//
convert, //
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
null, //
false, PartialsRescalingScheme.DEFAULT, true);
System.out.println("likelihood = " + nbtl.getLogLikelihood());
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// END: try-catch
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class BeagleSeqSimTest method simulateThreePartitions.
// END: simulateTwoPartitions
static void simulateThreePartitions(int i, int N) {
try {
MathUtils.setSeed(666);
System.out.println("Test case 3: simulateThreePartitions");
int sequenceLength = 100000;
ArrayList<Partition> partitionsList = new ArrayList<Partition>();
// create tree
NewickImporter importer = new NewickImporter("(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
Tree tree = importer.importTree(null);
TreeModel treeModel = new TreeModel(tree);
// create Frequency Model
Parameter freqs = new Parameter.Default(new double[] { 0.25, 0.25, 0.25, 0.25 });
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE, freqs);
// create substitution model
Parameter kappa = new Parameter.Default(1, 10);
HKY hky = new HKY(kappa, freqModel);
HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(hky);
// create site model
GammaSiteRateModel siteRateModel = new GammaSiteRateModel("siteModel");
// create branch rate model
BranchRateModel branchRateModel = new DefaultBranchRateModel();
// create partition
Partition partition1 = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
0, // to
sequenceLength - 1, // every
3);
// create partition
Partition Partition = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
1, // to
sequenceLength - 1, // every
3);
// create partition
Partition partition3 = new //
Partition(//
treeModel, //
substitutionModel, //
siteRateModel, //
branchRateModel, //
freqModel, // from
2, // to
sequenceLength - 1, // every
3);
partitionsList.add(partition1);
partitionsList.add(Partition);
partitionsList.add(partition3);
// feed to sequence simulator and generate data
BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(partitionsList);
if (i == (N - 1)) {
System.out.println(simulator.simulate(simulateInPar, false).toString());
} else {
simulator.simulate(simulateInPar, false);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// END: try-catch block
}
Aggregations