Search in sources :

Example 1 with XMLParseException

use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.

the class ConjugateRootTraitPrior method parseConjugateRootTraitPrior.

public static ConjugateRootTraitPrior parseConjugateRootTraitPrior(XMLObject xo, final int dim) throws XMLParseException {
    XMLObject cxo = xo.getChild(CONJUGATE_ROOT_PRIOR);
    Parameter meanParameter = (Parameter) cxo.getChild(MultivariateDistributionLikelihood.MVN_MEAN).getChild(Parameter.class);
    if (meanParameter.getDimension() != dim) {
        throw new XMLParseException("Root prior mean dimension (" + meanParameter.getDimension() + ") does not match trait diffusion dimension (" + dim + ")");
    }
    Parameter sampleSizeParameter = (Parameter) cxo.getChild(PRIOR_SAMPLE_SIZE).getChild(Parameter.class);
    return new ConjugateRootTraitPrior(meanParameter, sampleSizeParameter);
}
Also used : XMLObject(dr.xml.XMLObject) Parameter(dr.inference.model.Parameter) XMLParseException(dr.xml.XMLParseException)

Example 2 with XMLParseException

use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.

the class DataTypeUtils method getDataType.

public static DataType getDataType(XMLObject xo) throws XMLParseException {
    DataType dataType = null;
    if (xo.hasAttribute(DataType.DATA_TYPE)) {
        String dataTypeStr = xo.getStringAttribute(DataType.DATA_TYPE);
        if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
            dataTypeStr += "-" + xo.getStringAttribute(GeneticCode.GENETIC_CODE);
        }
        dataType = DataType.getRegisteredDataTypeByName(dataTypeStr);
    }
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof DataType) {
            if (dataType != null) {
                throw new XMLParseException("Multiple dataTypes defined for alignment element");
            }
            dataType = (DataType) child;
        }
    }
    return dataType;
}
Also used : DataType(dr.evolution.datatype.DataType) XMLObject(dr.xml.XMLObject) XMLParseException(dr.xml.XMLParseException)

Example 3 with XMLParseException

use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.

the class ParameterIntegerParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    int[] values = null;
    if (xo.hasAttribute(VALUE)) {
        if (values == null) {
            values = xo.getIntegerArrayAttribute(VALUE);
        } else {
            int[] v = xo.getIntegerArrayAttribute(VALUE);
            if (v.length == values.length) {
                System.arraycopy(v, 0, values, 0, v.length);
            } else if (v.length == 1) {
                for (int i = 0; i < values.length; i++) {
                    values[i] = v[0];
                }
            } else {
                throw new XMLParseException("value string must have 1 value or dimension values");
            }
        }
    } else {
        if (xo.hasAttribute(DIMENSION)) {
            values = new int[xo.getIntegerAttribute(DIMENSION)];
        } else {
            // parameter dimension will get set correctly by TreeModel presumably.
            // if (!xo.hasChildNamed(RANDOMIZE)) {
            // return new Parameter.Default(1);
            // }
            values = new int[1];
            values[0] = 0;
        }
    }
    // uppers = new double[values.length];
    // for(int i = 0; i < values.length; i++) {
    // uppers[i] = Double.POSITIVE_INFINITY;
    // }
    // 
    // lowers = new double[values.length];
    // for(int i = 0; i < values.length; i++) {
    // lowers[i] = Double.NEGATIVE_INFINITY;
    // }
    // if( xo.hasAttribute(UPPER) ) {
    // double[] v = xo.getDoubleArrayAttribute(UPPER);
    // if( v.length == uppers.length ) {
    // System.arraycopy(v, 0, uppers, 0, v.length);
    // } else if( v.length == 1 ) {
    // for(int i = 0; i < uppers.length; i++) {
    // uppers[i] = v[0];
    // }
    // } else {
    // throw new XMLParseException("uppers string must have 1 value or dimension values");
    // }
    // }
    // 
    // if( xo.hasAttribute(LOWER) ) {
    // double[] v = xo.getDoubleArrayAttribute(LOWER);
    // if( v.length == lowers.length ) {
    // System.arraycopy(v, 0, lowers, 0, v.length);
    // } else if( v.length == 1 ) {
    // for(int i = 0; i < lowers.length; i++) {
    // lowers[i] = v[0];
    // }
    // } else {
    // throw new XMLParseException("lowers string must have 1 value or dimension values");
    // }
    // }
    // assert uppers != null && lowers != null;
    // if( (uppers.length != values.length) ) {
    // throw new XMLParseException("value and upper limit strings have different dimension, in parameter");
    // }
    // 
    // if( (lowers.length != values.length) ) {
    // throw new XMLParseException("value and lower limit strings have different dimension, in parameter");
    // }
    // 
    // // check if uppers and lowers are consistent
    // for(int i = 0; i < values.length; i++) {
    // if( uppers[i] < lowers[i] ) {
    // throw new XMLParseException("upper is lower than lower, in parameter");
    // }
    // }
    // 
    // if (xo.hasChildNamed(RANDOMIZE)) {
    // 
    // Distribution distribution = (Distribution) xo.getChild(RANDOMIZE).getChild(Distribution.class);
    // for (int i = 0; i < values.length; i++) {
    // do {
    // // Not an efficient way to draw random variables, but this is currently the only general interface
    // values[i] = distribution.quantile(MathUtils.nextDouble());
    // } while (values[i] < lowers[i] || values[i] > uppers[i]);
    // }
    // 
    // } else {
    // 
    // // make values consistent with bounds
    // for(int i = 0; i < values.length; i++) {
    // if( uppers[i] < values[i] ) values[i] = uppers[i];
    // }
    // 
    // for(int i = 0; i < values.length; i++) {
    // if (lowers[i] > values[i]) values[i] = lowers[i];
    // }
    // }
    Variable<Integer> param = new Variable.I(values);
    param.addBounds(new Bounds.Staircase(param));
    return param;
}
Also used : XMLParseException(dr.xml.XMLParseException)

Example 4 with XMLParseException

use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.

the class PartitionParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    int from = 0;
    int to = -1;
    int every = xo.getAttribute(EVERY, 1);
    DataType dataType = null;
    if (xo.hasAttribute(FROM)) {
        from = xo.getIntegerAttribute(FROM) - 1;
        if (from < 0) {
            throw new XMLParseException("Illegal 'from' attribute in patterns element");
        }
    }
    if (xo.hasAttribute(TO)) {
        to = xo.getIntegerAttribute(TO) - 1;
        if (to < 0 || to < from) {
            throw new XMLParseException("Illegal 'to' attribute in patterns element");
        }
    }
    if (every <= 0) {
        throw new XMLParseException("Illegal 'every' attribute in patterns element");
    }
    if (xo.hasAttribute(DATA_TYPE)) {
        dataType = DataTypeUtils.getDataType(xo);
    }
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
    // FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
    FrequencyModel freqModel;
    List<FrequencyModel> freqModels = new ArrayList<FrequencyModel>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object cxo = xo.getChild(i);
        if (cxo instanceof FrequencyModel) {
            freqModels.add((FrequencyModel) cxo);
        }
    }
    if (freqModels.size() == 1) {
        freqModel = freqModels.get(0);
    } else {
        double[] freqParameter = new double[freqModels.size() * freqModels.get(0).getFrequencyCount()];
        int index = 0;
        for (int i = 0; i < freqModels.size(); i++) {
            for (int j = 0; j < freqModels.get(i).getFrequencyCount(); j++) {
                freqParameter[index] = (freqModels.get(i).getFrequency(j)) / freqModels.size();
                index++;
            }
        }
        freqModel = new FrequencyModel(dataType, freqParameter);
    }
    Sequence rootSequence = (Sequence) xo.getChild(Sequence.class);
    BranchRateModel rateModel = (BranchRateModel) xo.getChild(BranchRateModel.class);
    if (rateModel == null) {
        rateModel = new DefaultBranchRateModel();
    }
    BranchModel branchModel = (BranchModel) xo.getChild(BranchModel.class);
    if (branchModel == null) {
        SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
        branchModel = new HomogeneousBranchModel(substitutionModel);
    }
    Partition partition = new Partition(tree, branchModel, siteModel, rateModel, freqModel, from, to, every, dataType);
    if (rootSequence != null) {
        partition.setRootSequence(rootSequence);
    }
    return partition;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Partition(dr.app.beagle.tools.Partition) ArrayList(java.util.ArrayList) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) Sequence(dr.evolution.sequence.Sequence) BranchModel(dr.evomodel.branchmodel.BranchModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) TreeModel(dr.evomodel.tree.TreeModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) DataType(dr.evolution.datatype.DataType) XMLObject(dr.xml.XMLObject) XMLParseException(dr.xml.XMLParseException)

Example 5 with XMLParseException

use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.

the class RatioParameterParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Parameter parameter1 = (Parameter) xo.getElementFirstChild(PARAMETER1);
    Parameter parameter2 = (Parameter) xo.getElementFirstChild(PARAMETER2);
    if (parameter1.getDimension() != parameter2.getDimension()) {
        throw new XMLParseException("Parameters in ratio '" + xo.getId() + "' must have the same dimension");
    }
    return new RatioParameter(parameter1, parameter2);
}
Also used : Parameter(dr.inference.model.Parameter) XMLParseException(dr.xml.XMLParseException)

Aggregations

XMLParseException (dr.xml.XMLParseException)7 XMLObject (dr.xml.XMLObject)5 Parameter (dr.inference.model.Parameter)3 ArrayList (java.util.ArrayList)3 DataType (dr.evolution.datatype.DataType)2 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)2 Partition (dr.app.beagle.tools.Partition)1 Sequence (dr.evolution.sequence.Sequence)1 MutableTreeModel (dr.evolution.tree.MutableTreeModel)1 SimpleTree (dr.evolution.tree.SimpleTree)1 Tree (dr.evolution.tree.Tree)1 Taxa (dr.evolution.util.Taxa)1 TaxonList (dr.evolution.util.TaxonList)1 BranchModel (dr.evomodel.branchmodel.BranchModel)1 EpochBranchModel (dr.evomodel.branchmodel.EpochBranchModel)1 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)1 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)1 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)1 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)1 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)1