Search in sources :

Example 71 with TreeModel

use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.

the class RandomLocalClockModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    Parameter rateIndicatorParameter = (Parameter) xo.getElementFirstChild(RATE_INDICATORS);
    Parameter ratesParameter = (Parameter) xo.getElementFirstChild(RATES);
    Parameter meanRateParameter = null;
    if (xo.hasChildNamed(CLOCK_RATE)) {
        meanRateParameter = (Parameter) xo.getElementFirstChild(CLOCK_RATE);
    }
    boolean ratesAreMultipliers = xo.getAttribute(RATES_ARE_MULTIPLIERS, false);
    Logger.getLogger("dr.evomodel").info("\nUsing random local clock (RLC) model.");
    Logger.getLogger("dr.evomodel").info("  rates at change points are parameterized to be " + (ratesAreMultipliers ? " relative to parent rates." : "independent of parent rates."));
    // NaN == binary values
    double threshold = xo.getAttribute(THRESHOLD, Double.NaN);
    return new RandomLocalClockModel(tree, meanRateParameter, rateIndicatorParameter, ratesParameter, ratesAreMultipliers, threshold);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) RandomLocalClockModel(dr.evomodel.branchratemodel.RandomLocalClockModel) Parameter(dr.inference.model.Parameter)

Example 72 with TreeModel

use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.

the class DiscreteTraitBranchRateModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    PatternList patternList = (PatternList) xo.getChild(PatternList.class);
    TreeTraitProvider traitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
    DataType dataType = DataTypeUtils.getDataType(xo);
    Parameter rateParameter = null;
    Parameter relativeRatesParameter = null;
    Parameter indicatorsParameter = null;
    if (xo.getChild(RATE) != null) {
        rateParameter = (Parameter) xo.getElementFirstChild(RATE);
    }
    if (xo.getChild(RATES) != null) {
        rateParameter = (Parameter) xo.getElementFirstChild(RATES);
    }
    if (xo.getChild(RELATIVE_RATES) != null) {
        relativeRatesParameter = (Parameter) xo.getElementFirstChild(RELATIVE_RATES);
    }
    if (xo.getChild(INDICATORS) != null) {
        indicatorsParameter = (Parameter) xo.getElementFirstChild(INDICATORS);
    }
    int traitIndex = xo.getAttribute(TRAIT_INDEX, 1) - 1;
    String traitName = "states";
    Logger.getLogger("dr.evomodel").info("Using discrete trait branch rate model.\n" + "\tIf you use this model, please cite:\n" + "\t\tDrummond and Suchard (in preparation)");
    if (traitProvider == null) {
        // Use the version that reconstructs the trait using parsimony:
        return new DiscreteTraitBranchRateModel(treeModel, patternList, traitIndex, rateParameter);
    } else {
        if (traitName != null) {
            TreeTrait trait = traitProvider.getTreeTrait(traitName);
            if (trait == null) {
                throw new XMLParseException("A trait called, " + traitName + ", was not available from the TreeTraitProvider supplied to " + getParserName() + ", with ID " + xo.getId());
            }
            if (relativeRatesParameter != null) {
                return new DiscreteTraitBranchRateModel(traitProvider, dataType, treeModel, trait, traitIndex, rateParameter, relativeRatesParameter, indicatorsParameter);
            } else {
                return new DiscreteTraitBranchRateModel(traitProvider, dataType, treeModel, trait, traitIndex, rateParameter);
            }
        } else {
            TreeTrait[] traits = new TreeTrait[dataType.getStateCount()];
            for (int i = 0; i < dataType.getStateCount(); i++) {
                traits[i] = traitProvider.getTreeTrait(dataType.getCode(i));
                if (traits[i] == null) {
                    throw new XMLParseException("A trait called, " + dataType.getCode(i) + ", was not available from the TreeTraitProvider supplied to " + getParserName() + ", with ID " + xo.getId());
                }
            }
            return new DiscreteTraitBranchRateModel(traitProvider, traits, treeModel, rateParameter);
        }
    }
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) TreeTraitProvider(dr.evolution.tree.TreeTraitProvider) PatternList(dr.evolution.alignment.PatternList) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) DiscreteTraitBranchRateModel(dr.evomodel.branchratemodel.DiscreteTraitBranchRateModel) TreeTrait(dr.evolution.tree.TreeTrait)

Example 73 with TreeModel

use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.

the class HiddenLinkageLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    HiddenLinkageModel hlm = (HiddenLinkageModel) xo.getChild(HiddenLinkageModel.class);
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    return new HiddenLinkageLikelihood(hlm, tree);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) HiddenLinkageLikelihood(dr.oldevomodel.treelikelihood.HiddenLinkageLikelihood) HiddenLinkageModel(dr.evomodel.tree.HiddenLinkageModel)

Example 74 with TreeModel

use of dr.evomodel.tree.TreeModel 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);
    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");
    }
    // END: every check
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    GammaSiteRateModel siteModel = (GammaSiteRateModel) xo.getChild(GammaSiteRateModel.class);
    FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
    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);
    if (rootSequence != null) {
        partition.setRootSequence(rootSequence);
    }
    return partition;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) TreeModel(dr.evomodel.tree.TreeModel) Partition(dr.app.beagle.tools.Partition) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) Sequence(dr.evolution.sequence.Sequence) XMLParseException(dr.xml.XMLParseException) BranchModel(dr.evomodel.branchmodel.BranchModel) HomogeneousBranchModel(dr.evomodel.branchmodel.HomogeneousBranchModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel)

Example 75 with TreeModel

use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.

the class XMLGenerator method generateXML.

// END: Constructor
public void generateXML(File file) throws IOException {
    XMLWriter writer = new XMLWriter(new BufferedWriter(new FileWriter(file)));
    // //////////////
    // ---header---//
    // //////////////
    writer.writeText("<?xml version=\"1.0\" standalone=\"yes\"?>");
    writer.writeComment("Generated by " + BeagleSequenceSimulatorApp.LONG_NAME + " " + BeagleSequenceSimulatorApp.VERSION);
    writer.writeOpenTag("beast");
    writer.writeBlankLine();
    try {
        int suffix = 1;
        ArrayList<Taxa> taxaList = new ArrayList<Taxa>();
        for (PartitionData data : dataList) {
            if (data.record == null) {
                throw new RuntimeException("Set Tree Model in Partitions tab for " + suffix + " partition.");
            } else {
                Taxa taxa = null;
                if (data.demographicModelIndex == 0 && data.record.isTreeSet()) {
                    taxa = new Taxa(data.record.getTree().asList());
                } else if (data.demographicModelIndex == 0 && data.record.isTaxaSet()) {
                    throw new RuntimeException("Data and demographic model incompatible for partition " + suffix);
                } else if ((data.demographicModelIndex > 0 && data.demographicModelIndex <= PartitionData.lastImplementedIndex) && data.record.isTreeSet()) {
                    taxa = new Taxa(data.record.getTree().asList());
                } else if ((data.demographicModelIndex > 0 && data.demographicModelIndex <= PartitionData.lastImplementedIndex) && data.record.isTaxaSet()) {
                    taxa = data.record.getTaxa();
                } else {
                //
                }
                if (taxaList.size() == 0 | !Utils.isTaxaInList(taxa, taxaList)) {
                    data.taxaIdref += suffix;
                    writeTaxa(taxa, writer, String.valueOf(suffix));
                    writer.writeBlankLine();
                    taxaList.add(taxa);
                //							 System.out.println("NOT IN LIST");
                } else {
                    int index = Utils.taxaIsIdenticalWith(taxa, taxaList) + 1;
                    data.taxaIdref += index;
                //							 System.out.println("IDENTICAL WITH " + index);
                }
            }
            // END: treeModel set check
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Taxa generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<TreeModel> treeModelList = new ArrayList<TreeModel>();
        for (PartitionData data : dataList) {
            if (data.demographicModelIndex == 0) {
                TreeModel treeModel = data.createTreeModel();
                if (treeModelList.size() == 0 | !Utils.isTreeModelInList(treeModel, treeModelList)) {
                    data.treeModelIdref += suffix;
                    writeNewick(treeModel, writer, String.valueOf(suffix));
                    writer.writeBlankLine();
                    treeModelList.add(treeModel);
                } else {
                    int index = Utils.treeModelIsIdenticalWith(treeModel, treeModelList) + 1;
                    data.treeModelIdref += index;
                }
            // END: list check
            } else if (data.demographicModelIndex > 0 && data.demographicModelIndex <= PartitionData.lastImplementedIndex) {
                data.demographicModelIdref += suffix;
                data.treeModelIdref += suffix;
                writeDemographicModel(data, writer, String.valueOf(suffix));
                writer.writeBlankLine();
            } else {
            // throw exception
            }
            // END: demo model check
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Topology generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<TreesTableRecord> recordsList = new ArrayList<TreesTableRecord>();
        for (PartitionData data : dataList) {
            TreesTableRecord record = data.record;
            if (recordsList.size() == 0 | !Utils.isRecordInList(record, recordsList)) {
                writeTreeModel(writer, String.valueOf(suffix));
                writer.writeBlankLine();
                recordsList.add(record);
            }
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Tree model generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<PartitionData> partitionList = new ArrayList<PartitionData>();
        for (PartitionData data : dataList) {
            if (partitionList.size() == 0 | !Utils.isElementInList(data, partitionList, Utils.BRANCH_RATE_MODEL_ELEMENT)) {
                data.clockModelIdref += suffix;
                writeBranchRatesModel(data, writer, String.valueOf(suffix));
                writer.writeBlankLine();
                partitionList.add(data);
            // System.out.println("NOT IN LIST");
            } else {
                int index = Utils.isIdenticalWith(data, partitionList, Utils.BRANCH_RATE_MODEL_ELEMENT) + 1;
                data.clockModelIdref += index;
            // System.out.println("IDENTICAL WITH " + index);
            }
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Clock model generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<PartitionData> partitionList = new ArrayList<PartitionData>();
        for (PartitionData data : dataList) {
            if (partitionList.size() == 0 | !Utils.isElementInList(data, partitionList, Utils.FREQUENCY_MODEL_ELEMENT)) {
                data.frequencyModelIdref += suffix;
                writeFrequencyModel(data, writer);
                writer.writeBlankLine();
                partitionList.add(data);
            //					System.out.println("NOT IN LIST");
            } else {
                int index = Utils.isIdenticalWith(data, partitionList, Utils.FREQUENCY_MODEL_ELEMENT) + 1;
                data.frequencyModelIdref += index;
            //					System.out.println("IDENTICAL WITH " + index);
            }
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Frequency model generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<PartitionData> partitionList = new ArrayList<PartitionData>();
        for (PartitionData data : dataList) {
            if (partitionList.size() == 0 | !Utils.isElementInList(data, partitionList, Utils.BRANCH_MODEL_ELEMENT)) {
                data.substitutionModelIdref += suffix;
                writeBranchModel(data, writer, String.valueOf(suffix));
                writer.writeBlankLine();
                partitionList.add(data);
            } else {
                int index = Utils.isIdenticalWith(data, partitionList, Utils.BRANCH_MODEL_ELEMENT) + 1;
                data.substitutionModelIdref += index;
            }
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        throw new RuntimeException("Branch model generation has failed:\n" + e.getMessage());
    }
    try {
        int suffix = 1;
        ArrayList<PartitionData> partitionList = new ArrayList<PartitionData>();
        for (PartitionData data : dataList) {
            if (partitionList.size() == 0 | !Utils.isElementInList(data, partitionList, Utils.SITE_RATE_MODEL_ELEMENT)) {
                data.siteRateModelIdref += suffix;
                writeSiteRateModel(data, writer, suffix);
                writer.writeBlankLine();
                partitionList.add(data);
            } else {
                int index = Utils.isIdenticalWith(data, partitionList, Utils.SITE_RATE_MODEL_ELEMENT) + 1;
                data.siteRateModelIdref += index;
            }
            suffix++;
        }
    // END: partition loop
    } catch (Exception e) {
        System.err.println(e);
        throw new RuntimeException("Site rate model generation has failed:\n" + e.getMessage());
    }
    try {
        writeBeagleSequenceSimulator(writer);
        writer.writeBlankLine();
    } catch (Exception e) {
        throw new RuntimeException("Beagle Sequence Simulator element generation has failed:\n" + e.getMessage());
    }
    try {
        writeReport(writer);
        writer.writeBlankLine();
    } catch (Exception e) {
        System.err.println(e);
        throw new RuntimeException("Report element generation has failed:\n" + e.getMessage());
    }
    // END: try-catch block
    writer.writeCloseTag("beast");
    writer.flush();
    writer.close();
    // reset all idrefs
    for (PartitionData data : dataList) {
        data.resetIdrefs();
    }
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) XMLWriter(dr.app.beauti.util.XMLWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Taxa(dr.evolution.util.Taxa) TreeModel(dr.evomodel.tree.TreeModel)

Aggregations

TreeModel (dr.evomodel.tree.TreeModel)142 Parameter (dr.inference.model.Parameter)62 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)28 ArrayList (java.util.ArrayList)28 Tree (dr.evolution.tree.Tree)26 NewickImporter (dr.evolution.io.NewickImporter)21 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)20 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)19 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)18 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)15 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)15 PatternList (dr.evolution.alignment.PatternList)14 NodeRef (dr.evolution.tree.NodeRef)14 Partition (dr.app.beagle.tools.Partition)12 BranchModel (dr.evomodel.branchmodel.BranchModel)12 IOException (java.io.IOException)12 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)11 Taxon (dr.evolution.util.Taxon)11 TaxonList (dr.evolution.util.TaxonList)11 Patterns (dr.evolution.alignment.Patterns)9