Search in sources :

Example 6 with GenericTreeLikelihood

use of beast.evolution.likelihood.GenericTreeLikelihood in project beast2 by CompEvol.

the class AlignmentListInputEditor method delItem.

void delItem() {
    int[] selected = getTableRowSelection();
    if (selected.length == 0) {
        JOptionPane.showMessageDialog(this, "Select partitions to delete, before hitting the delete button");
    }
    // do the actual deleting
    for (int i = selected.length - 1; i >= 0; i--) {
        int rowNr = selected[i];
        // before deleting, unlink site model, clock model and tree
        // check whether any of the models are linked
        BranchRateModel.Base clockModel = likelihoods[rowNr].branchRateModelInput.get();
        SiteModelInterface siteModel = likelihoods[rowNr].siteModelInput.get();
        TreeInterface tree = likelihoods[rowNr].treeInput.get();
        List<GenericTreeLikelihood> cModels = new ArrayList<>();
        List<GenericTreeLikelihood> models = new ArrayList<>();
        List<GenericTreeLikelihood> tModels = new ArrayList<>();
        for (GenericTreeLikelihood likelihood : likelihoods) {
            if (likelihood != likelihoods[rowNr]) {
                if (likelihood.branchRateModelInput.get() == clockModel) {
                    cModels.add(likelihood);
                }
                if (likelihood.siteModelInput.get() == siteModel) {
                    models.add(likelihood);
                }
                if (likelihood.treeInput.get() == tree) {
                    tModels.add(likelihood);
                }
            }
        }
        try {
            if (cModels.size() > 0) {
                // clock model is linked, so we need to unlink
                if (doc.getPartitionNr(clockModel) != rowNr) {
                    tableData[rowNr][CLOCKMODEL_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
                } else {
                    int freePartition = doc.getPartitionNr(cModels.get(0));
                    tableData[rowNr][CLOCKMODEL_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
                }
                updateModel(CLOCKMODEL_COLUMN, rowNr);
            }
            if (models.size() > 0) {
                // site model is linked, so we need to unlink
                if (doc.getPartitionNr((BEASTInterface) siteModel) != rowNr) {
                    tableData[rowNr][SITEMODEL_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
                } else {
                    int freePartition = doc.getPartitionNr(models.get(0));
                    tableData[rowNr][SITEMODEL_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
                }
                updateModel(SITEMODEL_COLUMN, rowNr);
            }
            if (tModels.size() > 0) {
                // tree is linked, so we need to unlink
                if (doc.getPartitionNr((BEASTInterface) tree) != rowNr) {
                    tableData[rowNr][TREE_COLUMN] = getDoc().partitionNames.get(rowNr).partition;
                } else {
                    int freePartition = doc.getPartitionNr(tModels.get(0));
                    tableData[rowNr][TREE_COLUMN] = getDoc().partitionNames.get(freePartition).partition;
                }
                updateModel(TREE_COLUMN, rowNr);
            }
            getDoc().delAlignmentWithSubnet(alignments.get(rowNr));
            alignments.remove(rowNr);
            // remove deleted likelihood from likelihoods array
            GenericTreeLikelihood[] tmp = new GenericTreeLikelihood[likelihoods.length - 1];
            int k = 0;
            for (int j = 0; j < likelihoods.length; j++) {
                if (j != rowNr) {
                    tmp[k] = likelihoods[j];
                    k++;
                }
            }
            likelihoods = tmp;
            partitionCount--;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Deletion failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
    MRCAPriorInputEditor.customConnector(doc);
    refreshPanel();
}
Also used : BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) SiteModelInterface(beast.evolution.sitemodel.SiteModelInterface) TreeInterface(beast.evolution.tree.TreeInterface)

Example 7 with GenericTreeLikelihood

use of beast.evolution.likelihood.GenericTreeLikelihood in project beast2 by CompEvol.

the class AlignmentListInputEditor method initTableData.

void initTableData() {
    this.likelihoods = new GenericTreeLikelihood[partitionCount];
    if (tableData == null) {
        tableData = new Object[partitionCount][NR_OF_COLUMNS];
    }
    CompoundDistribution likelihoods = (CompoundDistribution) doc.pluginmap.get("likelihood");
    for (int i = 0; i < partitionCount; i++) {
        Alignment data = alignments.get(i);
        // partition name
        tableData[i][NAME_COLUMN] = data;
        // alignment name
        if (data instanceof FilteredAlignment) {
            tableData[i][FILE_COLUMN] = ((FilteredAlignment) data).alignmentInput.get();
        } else {
            tableData[i][FILE_COLUMN] = data;
        }
        // # taxa
        tableData[i][TAXA_COLUMN] = data.getTaxonCount();
        // # sites
        tableData[i][SITES_COLUMN] = data.getSiteCount();
        // Data type
        tableData[i][TYPE_COLUMN] = data.getDataType();
        // site model
        GenericTreeLikelihood likelihood = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(i);
        assert (likelihood != null);
        this.likelihoods[i] = likelihood;
        tableData[i][SITEMODEL_COLUMN] = getPartition(likelihood.siteModelInput);
        // clock model
        tableData[i][CLOCKMODEL_COLUMN] = getPartition(likelihood.branchRateModelInput);
        // tree
        tableData[i][TREE_COLUMN] = getPartition(likelihood.treeInput);
        // useAmbiguities
        tableData[i][USE_AMBIGUITIES_COLUMN] = null;
        try {
            if (hasUseAmbiguitiesInput(i)) {
                tableData[i][USE_AMBIGUITIES_COLUMN] = likelihood.getInputValue("useAmbiguities");
            }
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : CompoundDistribution(beast.core.util.CompoundDistribution) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) FilteredAlignment(beast.evolution.alignment.FilteredAlignment)

Example 8 with GenericTreeLikelihood

use of beast.evolution.likelihood.GenericTreeLikelihood in project beast2 by CompEvol.

the class SiteModelInputEditor method customConnector.

public static boolean customConnector(BeautiDoc doc) {
    try {
        DeltaExchangeOperator operator = (DeltaExchangeOperator) doc.pluginmap.get("FixMeanMutationRatesOperator");
        if (operator == null) {
            return false;
        }
        List<RealParameter> parameters = operator.parameterInput.get();
        parameters.clear();
        // String weights = "";
        CompoundDistribution likelihood = (CompoundDistribution) doc.pluginmap.get("likelihood");
        boolean hasOneEstimatedRate = false;
        List<String> rateIDs = new ArrayList<>();
        List<Integer> weights = new ArrayList<>();
        for (Distribution d : likelihood.pDistributions.get()) {
            GenericTreeLikelihood treelikelihood = (GenericTreeLikelihood) d;
            Alignment data = treelikelihood.dataInput.get();
            int weight = data.getSiteCount();
            if (data.isAscertained) {
                weight -= data.getExcludedPatternCount();
            }
            if (treelikelihood.siteModelInput.get() instanceof SiteModel) {
                SiteModel siteModel = (SiteModel) treelikelihood.siteModelInput.get();
                RealParameter mutationRate = siteModel.muParameterInput.get();
                // clockRate.m_bIsEstimated.setValue(true, clockRate);
                if (mutationRate.isEstimatedInput.get()) {
                    hasOneEstimatedRate = true;
                    if (rateIDs.indexOf(mutationRate.getID()) == -1) {
                        parameters.add(mutationRate);
                        weights.add(weight);
                        rateIDs.add(mutationRate.getID());
                    } else {
                        int k = rateIDs.indexOf(mutationRate.getID());
                        weights.set(k, weights.get(k) + weight);
                    }
                }
            }
        }
        IntegerParameter weightParameter;
        if (weights.size() == 0) {
            weightParameter = new IntegerParameter();
        } else {
            String weightString = "";
            for (int k : weights) {
                weightString += k + " ";
            }
            weightParameter = new IntegerParameter(weightString);
            weightParameter.setID("weightparameter");
        }
        weightParameter.isEstimatedInput.setValue(false, weightParameter);
        operator.parameterWeightsInput.setValue(weightParameter, operator);
        return hasOneEstimatedRate;
    } catch (Exception e) {
    }
    return false;
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) SiteModel(beast.evolution.sitemodel.SiteModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompoundDistribution(beast.core.util.CompoundDistribution) Alignment(beast.evolution.alignment.Alignment) CompoundDistribution(beast.core.util.CompoundDistribution) DeltaExchangeOperator(beast.evolution.operators.DeltaExchangeOperator)

Example 9 with GenericTreeLikelihood

use of beast.evolution.likelihood.GenericTreeLikelihood in project beast2 by CompEvol.

the class BeautiPanel method cloneFrom.

/**
 * Clones partition identified by sourceID to targetID and type (Site/Clock/Tree model)
 * as stored in config.
 * @param sourceID
 * @param targetID
 */
public void cloneFrom(String sourceID, String targetID) {
    if (sourceID.equals(targetID)) {
        return;
    }
    String type = config.hasPartitionsInput.get().toString();
    java.util.List<BEASTInterface> list = doc.getPartitions(type);
    int source = -1, target = -1;
    for (int i = 0; i < list.size(); i++) {
        BEASTInterface partition = list.get(i);
        if (type.equals("SiteModel")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).siteModelInput.get();
        } else if (type.equals("ClockModel")) {
            partition = ((GenericTreeLikelihood) partition).branchRateModelInput.get();
        } else if (type.equals("Tree")) {
            partition = (BEASTInterface) ((GenericTreeLikelihood) partition).treeInput.get();
        }
        String partitionID = partition.getID();
        partitionID = partitionID.substring(partitionID.lastIndexOf('.') + 1);
        if (partitionID.length() > 1 && partitionID.charAt(1) == ':') {
            partitionID = partitionID.substring(2);
        }
        if (partitionID.equals(sourceID)) {
            source = i;
        }
        if (partitionID.equals(targetID)) {
            target = i;
        }
    }
    if (target == -1) {
        throw new RuntimeException("Programmer error: sourceID and targetID should be in list");
    }
    CompoundDistribution likelihoods = (CompoundDistribution) doc.pluginmap.get("likelihood");
    GenericTreeLikelihood likelihoodSource = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(source);
    GenericTreeLikelihood likelihood = (GenericTreeLikelihood) likelihoods.pDistributions.get().get(target);
    PartitionContext oldContext = doc.getContextFor(likelihoodSource);
    PartitionContext newContext = doc.getContextFor(likelihood);
    // this ensures the config.sync does not set any input value
    config._input.setValue(null, config);
    if (type.equals("SiteModel")) {
        SiteModelInterface siteModelSource = likelihoodSource.siteModelInput.get();
        SiteModelInterface siteModel = null;
        try {
            siteModel = (SiteModel.Base) BeautiDoc.deepCopyPlugin((BEASTInterface) siteModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (RuntimeException e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        likelihood.siteModelInput.setValue(siteModel, likelihood);
        return;
    } else if (type.equals("ClockModel")) {
        BranchRateModel clockModelSource = likelihoodSource.branchRateModelInput.get();
        BranchRateModel clockModel = null;
        try {
            clockModel = (BranchRateModel) BeautiDoc.deepCopyPlugin((BEASTInterface) clockModelSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        // make sure that *if* the clock model has a tree as input, it is
        // the same as for the likelihood
        TreeInterface tree = null;
        try {
            for (Input<?> input : ((BEASTInterface) clockModel).listInputs()) {
                if (input.getName().equals("tree")) {
                    tree = (TreeInterface) input.get();
                }
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (tree != null && tree != likelihood.treeInput.get()) {
            // likelihood.treeInput.setValue(tree, likelihood);
            JOptionPane.showMessageDialog(null, "Cannot clone clock model with different trees");
            return;
        }
        likelihood.branchRateModelInput.setValue(clockModel, likelihood);
        return;
    } else if (type.equals("Tree")) {
        TreeInterface tree = null;
        TreeInterface treeSource = likelihoodSource.treeInput.get();
        try {
            tree = (TreeInterface) BeautiDoc.deepCopyPlugin((BEASTInterface) treeSource, likelihood, (MCMC) doc.mcmc.get(), oldContext, newContext, doc, null);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Could not clone " + sourceID + " to " + targetID + " " + e.getMessage());
            return;
        }
        // sanity check: make sure taxon sets are compatible
        Taxon.assertSameTaxa(tree.getID(), tree.getTaxonset().getTaxaNames(), likelihood.dataInput.get().getID(), likelihood.dataInput.get().getTaxaNames());
        likelihood.treeInput.setValue(tree, likelihood);
        return;
    } else {
        throw new RuntimeException("Programmer error calling cloneFrom: Should only clone Site/Clock/Tree model");
    }
}
Also used : GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) MCMC(beast.core.MCMC) SiteModel(beast.evolution.sitemodel.SiteModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) TreeInterface(beast.evolution.tree.TreeInterface) CompoundDistribution(beast.core.util.CompoundDistribution) Input(beast.core.Input) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) BEASTInterface(beast.core.BEASTInterface) SiteModelInterface(beast.evolution.sitemodel.SiteModelInterface)

Example 10 with GenericTreeLikelihood

use of beast.evolution.likelihood.GenericTreeLikelihood in project beast2 by CompEvol.

the class BeautiDoc method determinePartitions.

public void determinePartitions() {
    CompoundDistribution likelihood = (CompoundDistribution) pluginmap.get("likelihood");
    if (likelihood == null) {
        return;
    }
    partitionNames.clear();
    possibleContexts.clear();
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            alignments.add(treeLikelihood.dataInput.get());
            PartitionContext context = new PartitionContext(treeLikelihood);
            partitionNames.add(context);
            boolean found = false;
            for (PartitionContext context2 : possibleContexts) {
                if (context.equals(context2)) {
                    found = true;
                }
            }
            if (!found) {
                possibleContexts.add(context);
            }
        }
    }
    alignments.clear();
    for (int i = 0; i < 3; i++) {
        pPartitionByAlignments[i].clear();
        pPartition[i].clear();
        currentPartitions[i].clear();
    }
    List<GenericTreeLikelihood> treeLikelihoods = new ArrayList<>();
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            alignments.add(treeLikelihood.dataInput.get());
            treeLikelihoods.add(treeLikelihood);
        }
    }
    for (Distribution distr : likelihood.pDistributions.get()) {
        if (distr instanceof GenericTreeLikelihood) {
            GenericTreeLikelihood treeLikelihood = (GenericTreeLikelihood) distr;
            try {
                // sync SiteModel, ClockModel and Tree to any changes that
                // may have occurred
                // this should only affect the clock model in practice
                int partition = getPartitionNr((BEASTInterface) treeLikelihood.siteModelInput.get());
                GenericTreeLikelihood treeLikelihood2 = treeLikelihoods.get(partition);
                treeLikelihood.siteModelInput.setValue(treeLikelihood2.siteModelInput.get(), treeLikelihood);
                currentPartitions[0].add(partition);
                BranchRateModel rateModel = treeLikelihood.branchRateModelInput.get();
                if (rateModel != null) {
                    partition = getPartitionNr((BEASTInterface) rateModel);
                    treeLikelihood2 = treeLikelihoods.get(partition);
                    treeLikelihood.branchRateModelInput.setValue(treeLikelihood2.branchRateModelInput.get(), treeLikelihood);
                    currentPartitions[1].add(partition);
                } else {
                    currentPartitions[1].add(0);
                }
                partition = getPartitionNr((BEASTInterface) treeLikelihood.treeInput.get());
                treeLikelihood2 = treeLikelihoods.get(partition);
                treeLikelihood.treeInput.setValue(treeLikelihood2.treeInput.get(), treeLikelihood);
                currentPartitions[2].add(partition);
            } catch (Exception e) {
                e.printStackTrace();
            }
            pPartitionByAlignments[0].add(treeLikelihood);
            pPartitionByAlignments[1].add(treeLikelihood);
            pPartitionByAlignments[2].add(treeLikelihood);
        }
    }
    int partitionCount = partitionNames.size();
    for (int i = 0; i < 3; i++) {
        boolean[] usedPartition = new boolean[partitionCount];
        for (int j = 0; j < partitionCount; j++) {
            // getPartitionNr(m_pPartitionByAlignments[i].get(j));
            int partitionIndex = currentPartitions[i].get(j);
            usedPartition[partitionIndex] = true;
        }
        for (int j = 0; j < partitionCount; j++) {
            if (usedPartition[j]) {
                pPartition[i].add(pPartitionByAlignments[i].get(j));
            }
        }
    }
    Log.warning.println("PARTITIONS0:\n");
    Log.warning.println(Arrays.toString(currentPartitions));
}
Also used : CompoundDistribution(beast.core.util.CompoundDistribution) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) CompoundDistribution(beast.core.util.CompoundDistribution) ParametricDistribution(beast.math.distributions.ParametricDistribution) Distribution(beast.core.Distribution) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

GenericTreeLikelihood (beast.evolution.likelihood.GenericTreeLikelihood)12 CompoundDistribution (beast.core.util.CompoundDistribution)9 BEASTInterface (beast.core.BEASTInterface)8 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)6 Distribution (beast.core.Distribution)5 SiteModel (beast.evolution.sitemodel.SiteModel)5 ParametricDistribution (beast.math.distributions.ParametricDistribution)5 IOException (java.io.IOException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 TransformerException (javax.xml.transform.TransformerException)5 SAXException (org.xml.sax.SAXException)5 XMLParserException (beast.util.XMLParserException)4 ArrayList (java.util.ArrayList)4 MCMC (beast.core.MCMC)3 RealParameter (beast.core.parameter.RealParameter)3 Alignment (beast.evolution.alignment.Alignment)3 SiteModelInterface (beast.evolution.sitemodel.SiteModelInterface)3 Tree (beast.evolution.tree.Tree)3 TreeInterface (beast.evolution.tree.TreeInterface)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3