Search in sources :

Example 21 with Date

use of dr.evolution.util.Date in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModelParser method parseAncestor.

private static AncestralTaxonInTree parseAncestor(MutableTreeModel tree, XMLObject xo, final int index) throws XMLParseException {
    Taxon ancestor = (Taxon) xo.getChild(Taxon.class);
    Parameter pseudoBranchLength = (Parameter) xo.getChild(Parameter.class);
    AncestralTaxonInTree ancestorInTree;
    NodeRef node = new NodeRef() {

        @Override
        public int getNumber() {
            return index;
        }

        @Override
        public void setNumber(int n) {
            throw new RuntimeException("Do not set");
        }
    };
    if (xo.hasChildNamed(MonophylyStatisticParser.MRCA)) {
        TaxonList descendants = MonophylyStatisticParser.parseTaxonListOrTaxa(xo.getChild(MonophylyStatisticParser.MRCA));
        try {
            ancestorInTree = new AncestralTaxonInTree(ancestor, tree, descendants, pseudoBranchLength, null, node, index, 0.0);
        } catch (TreeUtils.MissingTaxonException e) {
            throw new XMLParseException("Unable to find taxa for " + ancestor.getId());
        }
    } else {
        XMLObject cxo = xo.getChild(ANCESTRAL_PATH);
        Taxon taxon = (Taxon) cxo.getChild(Taxon.class);
        Parameter time = (Parameter) cxo.getChild(Parameter.class);
        boolean relativeHeight = cxo.getAttribute(RELATIVE_HEIGHT, false);
        double offset = 0;
        if (relativeHeight) {
            Object date = taxon.getAttribute("date");
            if (date != null && date instanceof Date) {
                offset = taxon.getHeight();
            } else {
                throw new XMLParseException("Taxon '" + taxon.getId() + "' has no specified date");
            }
        } else {
            if (time.getParameterValue(0) <= taxon.getHeight()) {
                throw new XMLParseException("Ancestral path time must be > sampling time for taxon '" + taxon.getId() + "'");
            }
        }
        Taxa descendent = new Taxa();
        descendent.addTaxon(taxon);
        try {
            ancestorInTree = new AncestralTaxonInTree(ancestor, tree, descendent, pseudoBranchLength, time, node, index, // TODO Refactor into separate class from MRCA version
            offset);
        } catch (TreeUtils.MissingTaxonException e) {
            throw new XMLParseException("Unable to find taxa for " + ancestor.getId());
        }
    }
    return ancestorInTree;
}
Also used : TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date) NodeRef(dr.evolution.tree.NodeRef) Taxa(dr.evolution.util.Taxa) AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree) FastMatrixParameter(dr.inference.model.FastMatrixParameter) Parameter(dr.inference.model.Parameter) TreeUtils(dr.evolution.tree.TreeUtils)

Example 22 with Date

use of dr.evolution.util.Date in project beast-mcmc by beast-dev.

the class SimpleNodeParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    SimpleNode node = new SimpleNode();
    Taxon taxon = null;
    if (xo.hasAttribute(HEIGHT)) {
        node.setHeight(xo.getDoubleAttribute(HEIGHT));
    }
    if (xo.hasAttribute(RATE)) {
        node.setRate(xo.getDoubleAttribute(RATE));
    }
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof dr.evolution.tree.SimpleNode) {
            node.addChild((dr.evolution.tree.SimpleNode) child);
        } else if (child instanceof Taxon) {
            taxon = (Taxon) child;
        } else if (child instanceof Date) {
            node.setAttribute("date", child);
        } else if (child instanceof Attribute) {
            Attribute attr = (Attribute) child;
            String name = attr.getAttributeName();
            Object value = attr.getAttributeValue();
            node.setAttribute(name, value);
        } else if (child instanceof Attribute[]) {
            Attribute[] attrs = (Attribute[]) child;
            for (int j = 0; j < attrs.length; j++) {
                String name = attrs[j].getAttributeName();
                Object value = attrs[j].getAttributeValue();
                node.setAttribute(name, value);
            }
        } else if (child instanceof XMLObject) {
            XMLObject xoc = (XMLObject) child;
            if (xoc.getName().equals(Attributable.ATTRIBUTE)) {
                node.setAttribute(xoc.getStringAttribute(Attributable.NAME), xoc.getAttribute(Attributable.VALUE));
            } else {
                throw new XMLParseException("Unrecognized element" + xoc.getName() + " found in node element!");
            }
        } else {
            throw new XMLParseException("Unrecognized element found in node element!");
        }
    }
    if (taxon != null) {
        node.setTaxon(taxon);
    }
    return node;
}
Also used : Attribute(dr.util.Attribute) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date) SimpleNode(dr.evolution.tree.SimpleNode)

Example 23 with Date

use of dr.evolution.util.Date in project beast-mcmc by beast-dev.

the class TaxonParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    if (dr.xml.XMLParser.ID.contains("\'") && dr.xml.XMLParser.ID.contains("\"")) {
        // as it won't be possible to wrap it in either.
        throw new XMLParseException("Illegal taxon name, " + dr.xml.XMLParser.ID + ", - contains both single and double quotes");
    }
    Taxon taxon = new Taxon(xo.getStringAttribute(dr.xml.XMLParser.ID));
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof Date) {
            taxon.setDate((Date) child);
        } else if (child instanceof Location) {
            taxon.setLocation((Location) child);
        } else if (child instanceof Attribute) {
            final Attribute attr = (Attribute) child;
            taxon.setAttribute(attr.getAttributeName(), attr.getAttributeValue());
        } else if (child instanceof Attribute[]) {
            Attribute[] attrs = (Attribute[]) child;
            for (Attribute attr : attrs) {
                taxon.setAttribute(attr.getAttributeName(), attr.getAttributeValue());
            }
        } else {
            throw new XMLParseException("Unrecognized element found in taxon element");
        }
    }
    return taxon;
}
Also used : Attribute(dr.util.Attribute) Taxon(dr.evolution.util.Taxon) Date(dr.evolution.util.Date) Location(dr.evolution.util.Location)

Example 24 with Date

use of dr.evolution.util.Date in project beast-mcmc by beast-dev.

the class DateParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, java.util.Locale.UK);
    dateFormat.setLenient(true);
    if (xo.getChildCount() > 0) {
        throw new XMLParseException("No child elements allowed in date element.");
    }
    double value = 0.0;
    java.util.Date dateValue = null;
    if (xo.hasAttribute(VALUE)) {
        try {
            value = xo.getDoubleAttribute(VALUE);
        } catch (XMLParseException e) {
            String dateString = xo.getStringAttribute(VALUE);
            try {
                dateValue = dateFormat.parse(dateString);
            } catch (Exception ex) {
                throw new XMLParseException("value=" + dateString + " not recognised as a date, use DD/MM/YYYY");
            }
        }
    } else {
        throw new XMLParseException("Value attribute missing from date element.");
    }
    boolean backwards = false;
    if (xo.hasAttribute(DIRECTION)) {
        String direction = (String) xo.getAttribute(DIRECTION);
        if (direction.equals(BACKWARDS)) {
            backwards = true;
        }
    }
    Units.Type units = XMLUnits.Utils.getUnitsAttr(xo);
    Date date;
    if (xo.hasAttribute(ORIGIN)) {
        String originString = (String) xo.getAttribute(ORIGIN);
        java.util.Date origin;
        try {
            origin = dateFormat.parse(originString);
        } catch (Exception e) {
            throw new XMLParseException("origin=" + originString + " not recognised as a date, use DD/MM/YYYY");
        }
        if (dateValue != null) {
            date = new Date(dateValue, units, origin);
        } else {
            date = new Date(value, units, backwards, origin);
        }
    } else {
        // No origin specified so use default (1st Jan 1970)
        if (dateValue != null) {
            date = new Date(dateValue, units);
        } else {
            date = new Date(value, units, backwards);
        }
    }
    if (xo.hasAttribute(UNCERTAINTY)) {
        double uncertainty = xo.getDoubleAttribute(UNCERTAINTY);
        date.setUncertainty(uncertainty);
    } else if (xo.hasAttribute(PRECISION)) {
        // Precision was the old term for uncertainty which was confusing
        // because it was the reciprocal. Parse this term synonymously for
        // backwards compatibility.
        double uncertainty = xo.getDoubleAttribute(PRECISION);
        date.setUncertainty(uncertainty);
    }
    return date;
}
Also used : Units(dr.evolution.util.Units) XMLUnits(dr.evoxml.util.XMLUnits) Date(dr.evolution.util.Date)

Example 25 with Date

use of dr.evolution.util.Date in project beast-mcmc by beast-dev.

the class SequenceLikelihoodTest method createAlignmentWithAllUniquePatterns.

protected void createAlignmentWithAllUniquePatterns(Object[][] taxa_sequence, DataType dataType) {
    alignment = new SimpleAlignment();
    alignment.setDataType(dataType);
    int nTaxa = taxa_sequence[0].length;
    String[] allUniquePatterns = createAllUniquePatterns(nTaxa, dataType);
    taxa_sequence[1] = allUniquePatterns;
    // 6, 17
    taxa = new Taxon[nTaxa];
    System.out.println("Taxon len = " + taxa_sequence[0].length);
    System.out.println("Alignment len = " + taxa_sequence[1].length);
    if (taxa_sequence.length > 2)
        System.out.println("Date len = " + taxa_sequence[2].length);
    for (int i = 0; i < taxa_sequence[0].length; i++) {
        taxa[i] = new Taxon(taxa_sequence[0][i].toString());
        if (taxa_sequence.length > 2) {
            Date date = new Date((Double) taxa_sequence[2][i], Units.Type.YEARS, (Boolean) taxa_sequence[3][0]);
            taxa[i].setDate(date);
        }
        // taxonList.addTaxon(taxon);
        Sequence sequence = new Sequence(taxa_sequence[1][i].toString());
        sequence.setTaxon(taxa[i]);
        sequence.setDataType(dataType);
        alignment.addSequence(sequence);
    }
    System.out.println("Sequence pattern count = " + alignment.getPatternCount());
}
Also used : SimpleAlignment(dr.evolution.alignment.SimpleAlignment) Taxon(dr.evolution.util.Taxon) Sequence(dr.evolution.sequence.Sequence) Date(dr.evolution.util.Date)

Aggregations

Date (dr.evolution.util.Date)39 Taxon (dr.evolution.util.Taxon)19 Taxa (dr.evolution.util.Taxa)5 TimeScale (dr.evolution.util.TimeScale)5 Parameter (dr.inference.model.Parameter)5 java.util (java.util)5 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)4 Sequence (dr.evolution.sequence.Sequence)4 NodeRef (dr.evolution.tree.NodeRef)4 dr.evolution.util (dr.evolution.util)4 Units (dr.evolution.util.Units)3 CompoundParameter (dr.inference.model.CompoundParameter)3 ArrayList (java.util.ArrayList)3 Tree (dr.evolution.tree.Tree)2 TaxonList (dr.evolution.util.TaxonList)2 Attribute (dr.util.Attribute)2 NexusExporter (dr.app.tools.NexusExporter)1 CoalescentSimulator (dr.evolution.coalescent.CoalescentSimulator)1 PiecewiseLinearPopulation (dr.evolution.coalescent.PiecewiseLinearPopulation)1 SimpleNode (dr.evolution.tree.SimpleNode)1