Search in sources :

Example 41 with DataType

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

the class RandomBranchModel method setup.

// END: Constructor
private void setup() {
    DataType dataType = baseSubstitutionModel.getDataType();
    FrequencyModel freqModel = baseSubstitutionModel.getFrequencyModel();
    Parameter kappaParameter = new Parameter.Default("kappa", 1, baseSubstitutionModel.getKappa());
    substitutionModels = new LinkedList<SubstitutionModel>();
    branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
    int branchClass = 0;
    for (NodeRef node : treeModel.getNodes()) {
        if (!treeModel.isRoot(node)) {
            double nodeHeight = treeModel.getNodeHeight(node);
            double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node));
            double time = 0.5 * (parentHeight + nodeHeight);
            double baseOmega = baseSubstitutionModel.getOmega();
            double fixed = baseOmega * time;
            // Math.exp((random.nextGaussian() * stdev + mean));
            double epsilon = (Math.log(1 - random.nextDouble()) / (-rate));
            double value = fixed + epsilon;
            Parameter omegaParameter = new Parameter.Default("omega", 1, value);
            GY94CodonModel gy94 = new GY94CodonModel((Codons) dataType, omegaParameter, kappaParameter, freqModel);
            substitutionModels.add(gy94);
            branchAssignmentMap.put(node, branchClass);
            branchClass++;
        }
    // END: root check
    }
// END: nodes loop
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) NodeRef(dr.evolution.tree.NodeRef) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) GY94CodonModel(dr.evomodel.substmodel.codon.GY94CodonModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 42 with DataType

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

the class BeagleBranchLikelihood method loadBeagleInstance.

// END: finalizeBeagle
private void loadBeagleInstance() {
    this.substitutionModelDelegate = new SubstitutionModelDelegate(treeModel, branchModel);
    DataType dataType = freqModel.getDataType();
    int partitionSiteCount = patternList.getPatternCount();
    nodeCount = treeModel.getNodeCount();
    this.matrixBufferHelper = new BufferIndexHelper(nodeCount, 0);
    int tipCount = treeModel.getExternalNodeCount();
    int internalNodeCount = treeModel.getInternalNodeCount();
    partialBufferHelper = new BufferIndexHelper(nodeCount, tipCount);
    BufferIndexHelper scaleBufferHelper = new BufferIndexHelper(internalNodeCount + 1, 0);
    int compactPartialsCount = tipCount;
    int stateCount = dataType.getStateCount();
    int patternCount = partitionSiteCount;
    int siteRateCategoryCount = siteRateModel.getCategoryCount();
    int[] resourceList = new int[] { 0 };
    long preferenceFlags = 0;
    long requirementFlags = 0;
    beagle = // 
    BeagleFactory.loadBeagleInstance(// 
    tipCount, // 
    partialBufferHelper.getBufferCount(), // 
    compactPartialsCount, // 
    stateCount, // 
    patternCount, // 
    substitutionModelDelegate.getEigenBufferCount(), // 
    substitutionModelDelegate.getMatrixBufferCount(), // 
    siteRateCategoryCount, // 
    scaleBufferHelper.getBufferCount(), // 
    resourceList, // 
    preferenceFlags, // 
    requirementFlags);
}
Also used : DataType(dr.evolution.datatype.DataType) SubstitutionModelDelegate(dr.evomodel.treelikelihood.SubstitutionModelDelegate) BufferIndexHelper(dr.evomodel.treedatalikelihood.BufferIndexHelper)

Example 43 with DataType

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

the class OrderedLatentLiabilityTransformParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    OrderedLatentLiabilityLikelihood likelihood = (OrderedLatentLiabilityLikelihood) xo.getChild(OrderedLatentLiabilityLikelihood.class);
    CompoundParameter parameter = likelihood.getTipTraitParameter();
    DataType dataType = likelihood.getPatternList().getDataType();
    if (!(dataType instanceof TwoStates)) {
        throw new XMLParseException("Liability transformation is currently only implemented for binary traits");
    }
    Parameter mask = null;
    if (xo.hasChildNamed(MaskedParameterParser.MASKING)) {
        mask = (Parameter) xo.getElementFirstChild(MaskedParameterParser.MASKING);
    }
    List<Transform> transforms = new ArrayList<Transform>();
    int index = 0;
    for (int tip = 0; tip < parameter.getParameterCount(); ++tip) {
        final int[] tipData = likelihood.getData(tip);
        for (int trait = 0; trait < tipData.length; ++trait) {
            int discreteState = tipData[trait];
            boolean valid = true;
            Transform transform;
            if (discreteState == 0) {
                transform = Transform.LOG_NEGATE;
                if (parameter.getParameterValue(index) >= 0.0) {
                    valid = false;
                }
            } else if (discreteState == 1) {
                transform = Transform.LOG;
                if (parameter.getParameterValue(index) <= 0.0) {
                    valid = false;
                }
            } else {
                transform = Transform.NONE;
            // transforms.add(Transform.NONE);
            }
            if (!valid) {
                throw new XMLParseException("Incompatible binary trait and latent value in tip '" + parameter.getParameter(tip).getId() + "'");
            }
            if (mask == null || mask.getParameterValue(index) == 1.0) {
                transforms.add(transform);
            }
            ++index;
        }
    }
    return new Transform.Array(transforms, parameter);
}
Also used : OrderedLatentLiabilityLikelihood(dr.evomodel.continuous.OrderedLatentLiabilityLikelihood) TwoStates(dr.evolution.datatype.TwoStates) ArrayList(java.util.ArrayList) DataType(dr.evolution.datatype.DataType) Transform(dr.util.Transform)

Example 44 with DataType

use of dr.evolution.datatype.DataType 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)

Example 45 with DataType

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

the class BinarySubstitutionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Parameter ratesParameter;
    XMLObject cxo = xo.getChild(dr.oldevomodelxml.substmodel.GeneralSubstitutionModelParser.FREQUENCIES);
    FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
    DataType dataType = freqModel.getDataType();
    if (dataType != TwoStates.INSTANCE)
        throw new XMLParseException("Frequency model must have binary (two state) data type.");
    int relativeTo = 0;
    ratesParameter = new Parameter.Default(0);
    return new GeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, relativeTo);
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType) GeneralSubstitutionModel(dr.evomodel.substmodel.GeneralSubstitutionModel)

Aggregations

DataType (dr.evolution.datatype.DataType)66 Parameter (dr.inference.model.Parameter)26 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)11 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)11 ArrayList (java.util.ArrayList)8 Sequence (dr.evolution.sequence.Sequence)7 PartitionSubstitutionModel (dr.app.beauti.options.PartitionSubstitutionModel)4 PatternList (dr.evolution.alignment.PatternList)4 GeneralDataType (dr.evolution.datatype.GeneralDataType)4 Taxon (dr.evolution.util.Taxon)4 TaxonList (dr.evolution.util.TaxonList)4 GeneralSubstitutionModel (dr.evomodel.substmodel.GeneralSubstitutionModel)4 SVSComplexSubstitutionModel (dr.oldevomodel.substmodel.SVSComplexSubstitutionModel)4 Attribute (dr.util.Attribute)4 Codons (dr.evolution.datatype.Codons)3 HiddenDataType (dr.evolution.datatype.HiddenDataType)3 NodeRef (dr.evolution.tree.NodeRef)3 Tree (dr.evolution.tree.Tree)3 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)3 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)3