use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class AncestralTraitParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String traitName = xo.getAttribute(TRAIT_NAME, STATES);
String name = xo.getAttribute(NAME, traitName);
Tree tree = (Tree) xo.getChild(Tree.class);
TreeTraitProvider treeTraitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
TaxonList taxa = null;
if (xo.hasChildNamed(MRCA)) {
taxa = (TaxonList) xo.getElementFirstChild(MRCA);
}
TreeTrait trait = treeTraitProvider.getTreeTrait(traitName);
if (trait == null) {
throw new XMLParseException("A trait called, " + traitName + ", was not available from the TreeTraitProvider supplied to " + getParserName() + (xo.hasId() ? ", with ID " + xo.getId() : ""));
}
try {
return new AncestralTrait(name, trait, tree, taxa);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
}
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class AttributePatternsParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String attributeName = xo.getStringAttribute(ATTRIBUTE);
String secondaryAttributeName = xo.getAttribute(SECONDARY_ATTRIBUTE, (String) null);
TaxonList taxa = (TaxonList) xo.getChild(TaxonList.class);
DataType dataType = DataTypeUtils.getDataType(xo);
if (dataType == null) {
throw new XMLParseException("dataType expected for attributePatterns element");
}
// using a SimpleSiteList rather than Patterns to allow ancestral reconstruction
SimpleSiteList patterns = new SimpleSiteList(dataType, taxa);
int[] pattern = new int[taxa.getTaxonCount()];
boolean attributeFound = true;
for (int i = 0; i < taxa.getTaxonCount(); i++) {
Taxon taxon = taxa.getTaxon(i);
if (secondaryAttributeName == null || secondaryAttributeName.isEmpty()) {
Object value = taxon.getAttribute(attributeName);
if (value != null) {
int state = dataType.getState(value.toString());
if (state < 0) {
throw new XMLParseException("State for attribute, " + attributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + value.toString());
}
pattern[i] = state;
} else {
attributeFound = false;
}
} else {
Object value1 = taxon.getAttribute(attributeName);
Object value2 = taxon.getAttribute(secondaryAttributeName);
if (value1 != null && value2 != null) {
String code = value1.toString() + CompositeDataTypeParser.COMPOSITE_STATE_SEPARATOR + value2.toString();
int state = dataType.getState(code);
if (state < 0) {
throw new XMLParseException("State for attributes, " + attributeName + " & " + secondaryAttributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + code);
}
pattern[i] = state;
} else {
attributeFound = false;
}
}
}
if (!attributeFound) {
throw new XMLParseException("The attribute, " + attributeName + " was not found in all taxa. Check the name of the attribute.");
}
patterns.addPattern(pattern);
if (xo.hasAttribute(XMLParser.ID)) {
Logger.getLogger("dr.evoxml").info("Read attribute patterns, '" + xo.getId() + "' for attribute, " + attributeName);
} else {
Logger.getLogger("dr.evoxml").info("Read attribute patterns for attribute, " + attributeName);
}
return patterns;
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class TreeTraitParserUtilities method jitter.
public void jitter(XMLObject xo, int length, List<Integer> missingIndices) throws XMLParseException {
XMLObject cxo = xo.getChild(TreeTraitParserUtilities.JITTER);
Parameter traits = (Parameter) cxo.getChild(Parameter.class);
// Must be included, no default value
double[] window = cxo.getDoubleArrayAttribute(TreeTraitParserUtilities.WINDOW);
// default = true
boolean duplicates = cxo.getAttribute(TreeTraitParserUtilities.DUPLICATES, true);
TaxonList taxonList = (TaxonList) cxo.getChild(TaxonList.class);
jitter(traits, length, missingIndices, window, taxonList, duplicates, true);
}
use of dr.evolution.util.TaxonList 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);
}
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class TMRCAStatisticParser 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 = null;
if (xo.hasChildNamed(MRCA)) {
taxa = (TaxonList) xo.getElementFirstChild(MRCA);
}
boolean isAbsolute = xo.getAttribute(ABSOLUTE, false);
boolean includeStem = false;
if (xo.hasAttribute(PARENT) && xo.hasAttribute(STEM)) {
throw new XMLParseException("Please use either " + PARENT + " or " + STEM + "!");
} else if (xo.hasAttribute(PARENT)) {
includeStem = xo.getBooleanAttribute(PARENT);
} else if (xo.hasAttribute(STEM)) {
includeStem = xo.getBooleanAttribute(STEM);
}
try {
return new TMRCAStatistic(name, tree, taxa, isAbsolute, includeStem);
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
}
}
Aggregations