Search in sources :

Example 1 with HiddenDataType

use of dr.evolution.datatype.HiddenDataType in project beast-mcmc by beast-dev.

the class MarkovModulatedHiddenClassRewardParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    MarkovModulatedSubstitutionModel substitutionModel = (MarkovModulatedSubstitutionModel) xo.getChild(MarkovModulatedSubstitutionModel.class);
    HiddenDataType hiddenDataType = (HiddenDataType) substitutionModel.getDataType();
    int classNumber = xo.getIntegerAttribute(CLASS_NUMBER);
    int hiddenClassCount = hiddenDataType.getHiddenClassCount();
    if (classNumber < 1 || classNumber > hiddenClassCount) {
        throw new XMLParseException("Invalid class number in " + xo.getId());
    }
    // Use zero-indexed number
    classNumber--;
    int stateCount = hiddenDataType.getStateCount() / hiddenClassCount;
    // Construct reward parameter
    Parameter parameter = new Parameter.Default(stateCount * hiddenClassCount, 0.0);
    for (int i = 0; i < stateCount; ++i) {
        parameter.setParameterValue(i + classNumber * stateCount, 1.0);
    }
    if (xo.hasAttribute(NAME)) {
        parameter.setId((String) xo.getAttribute(NAME));
    } else {
        parameter.setId(substitutionModel.getId() + "_" + Integer.toString(classNumber + 1));
    }
    return parameter;
}
Also used : MarkovModulatedSubstitutionModel(dr.evomodel.substmodel.MarkovModulatedSubstitutionModel) HiddenDataType(dr.evolution.datatype.HiddenDataType) Parameter(dr.inference.model.Parameter)

Example 2 with HiddenDataType

use of dr.evolution.datatype.HiddenDataType in project beast-mcmc by beast-dev.

the class MarkovModulatedSubstitutionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = DataTypeUtils.getDataType(xo);
    if (!(dataType instanceof HiddenDataType)) {
        throw new XMLParseException("Must construct " + MARKOV_MODULATED_MODEL + " with hidden data types");
    }
    Parameter switchingRates = (Parameter) xo.getElementFirstChild(SWITCHING_RATES);
    List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object cxo = xo.getChild(i);
        if (cxo instanceof SubstitutionModel) {
            substModels.add((SubstitutionModel) cxo);
        }
    }
    boolean geometricRates = xo.getAttribute(GEOMETRIC_RATES, false);
    Parameter rateScalar = xo.hasChildNamed(RATE_SCALAR) ? (Parameter) xo.getChild(RATE_SCALAR).getChild(Parameter.class) : null;
    SiteRateModel siteRateModel = (SiteRateModel) xo.getChild(SiteRateModel.class);
    if (siteRateModel != null) {
        if (siteRateModel.getCategoryCount() != substModels.size() && substModels.size() % siteRateModel.getCategoryCount() != 0) {
            throw new XMLParseException("Number of gamma categories must equal number of substitution models in " + xo.getId());
        }
    }
    MarkovModulatedSubstitutionModel mmsm = new MarkovModulatedSubstitutionModel(xo.getId(), substModels, switchingRates, dataType, null, rateScalar, geometricRates, siteRateModel);
    if (xo.getAttribute(RENORMALIZE, false)) {
        mmsm.setNormalization(true);
    }
    return mmsm;
}
Also used : MarkovModulatedSubstitutionModel(dr.evomodel.substmodel.MarkovModulatedSubstitutionModel) ArrayList(java.util.ArrayList) HiddenDataType(dr.evolution.datatype.HiddenDataType) DataType(dr.evolution.datatype.DataType) HiddenDataType(dr.evolution.datatype.HiddenDataType) Parameter(dr.inference.model.Parameter) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) MarkovModulatedSubstitutionModel(dr.evomodel.substmodel.MarkovModulatedSubstitutionModel) SiteRateModel(dr.evomodel.siteratemodel.SiteRateModel)

Example 3 with HiddenDataType

use of dr.evolution.datatype.HiddenDataType in project beast-mcmc by beast-dev.

the class FrequencyModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = DataTypeUtils.getDataType(xo);
    Parameter freqsParam = (Parameter) xo.getElementFirstChild(FREQUENCIES);
    double[] frequencies = null;
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object obj = xo.getChild(i);
        if (obj instanceof PatternList) {
            PatternList patternList = (PatternList) obj;
            if (xo.getAttribute(COMPRESS, false) && (patternList.getDataType() instanceof HiddenDataType)) {
                double[] hiddenFrequencies = patternList.getStateFrequencies();
                int hiddenCount = ((HiddenDataType) patternList.getDataType()).getHiddenClassCount();
                int baseStateCount = hiddenFrequencies.length / hiddenCount;
                frequencies = new double[baseStateCount];
                for (int j = 0; j < baseStateCount; ++j) {
                    for (int k = 0; k < hiddenCount; ++k) {
                        frequencies[j] += hiddenFrequencies[j + k * baseStateCount];
                    }
                }
            } else {
                // TODO
                if (xo.hasAttribute(COMPOSITION)) {
                    String type = xo.getStringAttribute(COMPOSITION);
                    if (type.equalsIgnoreCase(FREQ_3x4)) {
                        frequencies = getEmpirical3x4Freqs(patternList);
                    }
                } else {
                    frequencies = patternList.getStateFrequencies();
                }
            // END: composition check
            }
            break;
        }
    // END: patternList check
    }
    StringBuilder sb = new StringBuilder("\nCreating state frequencies model '" + freqsParam.getParameterName() + "': ");
    if (frequencies != null) {
        if (freqsParam.getDimension() != frequencies.length) {
            throw new XMLParseException("dimension of frequency parameter and number of sequence states don't match.");
        }
        for (int j = 0; j < frequencies.length; j++) {
            freqsParam.setParameterValue(j, frequencies[j]);
        }
        sb.append("Using empirical frequencies from data ");
    } else {
        sb.append("Initial frequencies ");
    }
    sb.append("= {");
    if (xo.getAttribute(NORMALIZE, false)) {
        double sum = 0;
        for (int j = 0; j < freqsParam.getDimension(); j++) sum += freqsParam.getParameterValue(j);
        for (int j = 0; j < freqsParam.getDimension(); j++) {
            if (sum != 0)
                freqsParam.setParameterValue(j, freqsParam.getParameterValue(j) / sum);
            else
                freqsParam.setParameterValue(j, 1.0 / freqsParam.getDimension());
        }
    }
    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(5);
    sb.append(format.format(freqsParam.getParameterValue(0)));
    for (int j = 1; j < freqsParam.getDimension(); j++) {
        sb.append(", ");
        sb.append(format.format(freqsParam.getParameterValue(j)));
    }
    sb.append("}");
    Logger.getLogger("dr.evomodel").info(sb.toString());
    return new FrequencyModel(dataType, freqsParam);
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) PatternList(dr.evolution.alignment.PatternList) HiddenDataType(dr.evolution.datatype.HiddenDataType) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) HiddenDataType(dr.evolution.datatype.HiddenDataType) NumberFormat(java.text.NumberFormat)

Aggregations

HiddenDataType (dr.evolution.datatype.HiddenDataType)3 Parameter (dr.inference.model.Parameter)3 DataType (dr.evolution.datatype.DataType)2 MarkovModulatedSubstitutionModel (dr.evomodel.substmodel.MarkovModulatedSubstitutionModel)2 PatternList (dr.evolution.alignment.PatternList)1 SiteRateModel (dr.evomodel.siteratemodel.SiteRateModel)1 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)1 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1