Search in sources :

Example 1 with CodonFromNucleotideFrequencyModel

use of dr.evomodel.substmodel.CodonFromNucleotideFrequencyModel 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());
    if (dataType instanceof Codons && xo.getAttribute(CODON_FROM_NUCLEOTIDE, false)) {
        FrequencyModel nucleotideFrequencyModel = new FrequencyModel(Nucleotides.INSTANCE, freqsParam);
        Parameter codonFrequencies = new Parameter.Default(dataType.getStateCount(), 1.0 / dataType.getStateCount());
        codonFrequencies.setId(xo.getId());
        return new CodonFromNucleotideFrequencyModel((Codons) dataType, nucleotideFrequencyModel, codonFrequencies);
    } else {
        return new FrequencyModel(dataType, freqsParam);
    }
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) CodonFromNucleotideFrequencyModel(dr.evomodel.substmodel.CodonFromNucleotideFrequencyModel) PatternList(dr.evolution.alignment.PatternList) CodonFromNucleotideFrequencyModel(dr.evomodel.substmodel.CodonFromNucleotideFrequencyModel) HiddenDataType(dr.evolution.datatype.HiddenDataType) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) HiddenDataType(dr.evolution.datatype.HiddenDataType) Codons(dr.evolution.datatype.Codons) NumberFormat(java.text.NumberFormat)

Aggregations

PatternList (dr.evolution.alignment.PatternList)1 Codons (dr.evolution.datatype.Codons)1 DataType (dr.evolution.datatype.DataType)1 HiddenDataType (dr.evolution.datatype.HiddenDataType)1 CodonFromNucleotideFrequencyModel (dr.evomodel.substmodel.CodonFromNucleotideFrequencyModel)1 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)1 Parameter (dr.inference.model.Parameter)1 NumberFormat (java.text.NumberFormat)1