use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.
the class VariableCoalescentSimulator method readSampleFile.
private static Taxa readSampleFile(String fileName, double generationTime) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
Taxa taxa = new Taxa();
int id = 0;
while (line != null) {
if (!line.startsWith("#")) {
String[] tokens = line.split("[\t ]+");
// sample times are in the same units as simulation
double sampleTime = Double.parseDouble(tokens[0]) / generationTime;
int count = Integer.parseInt(tokens[1]);
for (int i = 0; i < count; i++) {
Taxon taxon = new Taxon(id + "");
taxon.setAttribute(dr.evolution.util.Date.DATE, new Date(sampleTime, Units.Type.GENERATIONS, true));
taxa.addTaxon(taxon);
id += 1;
}
}
line = reader.readLine();
}
return taxa;
}
use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.
the class GibbsIndependentCoalescentOperator method doOperation.
/**
* change the parameter and return the hastings ratio.
*/
public double doOperation() {
CoalescentSimulator simulator = new CoalescentSimulator();
List<TaxonList> taxonLists = new ArrayList<TaxonList>();
double rootHeight = -1.0;
double oldLikelihood = 0.0;
double newLikelihood = 0.0;
// should have one child that is node
for (int i = 0; i < xo.getChildCount(); i++) {
final Object child = xo.getChild(i);
//careful: Trees are TaxonLists ... (AER); see OldCoalescentSimulatorParser
if (child instanceof Tree) {
//do nothing
} else if (child instanceof TaxonList) {
//taxonLists.add((TaxonList) child);
taxonLists.add((Taxa) child);
//taxa added
break;
}
}
try {
Tree[] trees = new Tree[taxonLists.size()];
// simulate each taxonList separately
for (int i = 0; i < taxonLists.size(); i++) {
trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
}
oldLikelihood = coalescent.getLogLikelihood();
SimpleTree simTree = simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
//this would be the normal way to do it
treeModel.beginTreeEdit();
//now it's allowed to adjust the tree structure
treeModel.adoptTreeStructure(simTree);
//endTreeEdit() would then fire the events
treeModel.endTreeEdit();
newLikelihood = coalescent.getLogLikelihood();
} catch (IllegalArgumentException iae) {
try {
throw new XMLParseException(iae.getMessage());
} catch (XMLParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return oldLikelihood - newLikelihood;
return 0;
}
use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.
the class ExternalLengthStatisticParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String name = xo.getAttribute(Statistic.NAME, xo.getId());
Tree tree = (Tree) xo.getChild(Tree.class);
TaxonList taxa = (TaxonList) xo.getChild(Taxa.class);
try {
return new ExternalLengthStatistic(name, tree, taxa);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
}
}
use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.
the class RandomSubsetTaxaParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Taxa originalTaxa = new Taxa();
for (int i = 0; i < xo.getChildCount(); i++) {
Object child = xo.getChild(i);
if (child instanceof Taxon) {
Taxon taxon = (Taxon) child;
originalTaxa.addTaxon(taxon);
} else if (child instanceof TaxonList) {
TaxonList taxonList1 = (TaxonList) child;
for (int j = 0; j < taxonList1.getTaxonCount(); j++) {
originalTaxa.addTaxon(taxonList1.getTaxon(j));
}
} else {
throwUnrecognizedElement(xo);
}
}
List<Taxon> originalTaxonList = originalTaxa.asList();
int sampleTotal = xo.getAttribute(COUNT, originalTaxonList.size());
if (sampleTotal < 2) {
throw new XMLParseException("Must sample atleast two taxa");
}
boolean withReplacement = xo.getAttribute(WITH_REPLACEMENT, false);
Taxa sampledTaxa = new Taxa();
for (int i = 0; i < sampleTotal; i++) {
int choice = MathUtils.nextInt(originalTaxonList.size());
Taxon taxonToAdd = originalTaxonList.get(choice);
sampledTaxa.addTaxon(taxonToAdd);
if (!withReplacement) {
originalTaxonList.remove(choice);
}
}
return sampledTaxa;
}
use of dr.evolution.util.Taxa in project beast-mcmc by beast-dev.
the class RandomTaxaSampleParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Taxa population = (Taxa) xo.getChild(Taxa.class);
int n = xo.getIntegerAttribute(SAMPLE);
int N = population.getTaxonCount();
if (n <= 0 || n > N) {
throw new XMLParseException("sample must be greater than 0 and less than or equal to the population size");
}
Taxa sample = new Taxa();
ArrayList<Integer> indexes = new ArrayList<Integer>(N);
for (int i = 0; i < N; i++) indexes.add(i);
Logger.getLogger("dr.evolution").info("Generating a random taxa sample of size: " + n);
for (int i = 0; i < n; i++) {
int randomIndex = MathUtils.nextInt(indexes.size());
Taxon selectedTaxon = population.getTaxon(indexes.get(randomIndex));
sample.addTaxon(selectedTaxon);
indexes.remove(randomIndex);
}
if (xo.hasAttribute(PRINT_TAXA) && xo.getBooleanAttribute(PRINT_TAXA)) {
String fileName = null;
if (xo.hasAttribute(FILE_NAME)) {
fileName = xo.getStringAttribute(FILE_NAME);
}
if (fileName != null) {
try {
Writer write;
File file = new File(fileName);
String name = file.getName();
String parent = file.getParent();
if (!file.isAbsolute()) {
parent = System.getProperty("user.dir");
}
write = new FileWriter(new File(parent, name));
write.write("<taxa id=\"randomTaxaSample\">\n");
for (int i = 0; i < n; i++) {
write.write("\t<taxon idref=\"" + sample.getTaxonId(i) + "\"/>\n");
}
write.write("</taxa id=\"randomTaxaSample\">\n");
write.flush();
} catch (IOException fnfe) {
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
}
} else {
Logger.getLogger("dr.evomodel").info("<taxa id=\"randomTaxaSample\">");
for (int i = 0; i < n; i++) {
Logger.getLogger("dr.evomodel").info("\t<taxon idref=\" " + sample.getTaxonId(i) + " \"> ");
}
Logger.getLogger("dr.evomodel").info("</taxa id=\"randomTaxaSample\">");
}
}
return sample;
}
Aggregations