Search in sources :

Example 1 with SubstitutionModel

use of beast.evolution.substitutionmodel.SubstitutionModel in project beast2 by CompEvol.

the class BeautiDoc method suggestedLinks.

/**
 * return all RealParameters that have
 * the same ID in another partition, or
 * the same partition with the same substitution model as output
 *
 * @param beastObject
 * @return
 */
public List<BEASTInterface> suggestedLinks(BEASTInterface beastObject) {
    String id = beastObject.getID();
    List<BEASTInterface> list = new ArrayList<>();
    String partitionID = null;
    if (id.indexOf('.') >= 0) {
        partitionID = id.substring(id.indexOf('.') + 1);
        id = id.substring(0, id.indexOf('.'));
    } else {
        return list;
    }
    for (BEASTInterface candidate : posteriorPredecessors) {
        String id2 = candidate.getID();
        if (id2.indexOf('.') >= 0) {
            String partitionID2 = id2.substring(id2.indexOf('.') + 1);
            id2 = id2.substring(0, id2.indexOf('.'));
            if (id2.equals(id)) {
                list.add(candidate);
            }
            if (beastObject instanceof Parameter<?> && partitionID2.equals(partitionID) && candidate.getClass().equals(beastObject.getClass())) {
                boolean dimensionMatches = true;
                if (((Parameter<?>) beastObject).getDimension() != ((Parameter<?>) candidate).getDimension()) {
                    dimensionMatches = false;
                }
                // ensure they share an output
                boolean foundCommonOutput = false;
                for (Object out1 : beastObject.getOutputs()) {
                    for (Object out2 : candidate.getOutputs()) {
                        if (out1 == out2 && out1 instanceof SubstitutionModel) {
                            foundCommonOutput = true;
                            break;
                        }
                    }
                }
                if (dimensionMatches && foundCommonOutput) {
                    list.add(candidate);
                }
            }
        }
    }
    list.remove(beastObject);
    return list;
}
Also used : ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel)

Example 2 with SubstitutionModel

use of beast.evolution.substitutionmodel.SubstitutionModel in project beast2 by CompEvol.

the class BeautiDoc method determineLinks.

// methods for dealing with linking
void determineLinks() {
    if (!allowLinking) {
        return;
    }
    linked.clear();
    for (BEASTInterface beastObject : posteriorPredecessors) {
        Map<String, Integer> outputIDs = new HashMap<>();
        for (Object output : beastObject.getOutputs()) {
            if (posteriorPredecessors.contains(output)) {
                String id = ((BEASTInterface) output).getID();
                if (id.indexOf('.') >= 0) {
                    id = id.substring(0, id.indexOf('.'));
                    if (outputIDs.containsKey(id)) {
                        outputIDs.put(id, outputIDs.get(id) + 1);
                    } else {
                        outputIDs.put(id, 1);
                    }
                }
            }
        }
        for (Object output : beastObject.getOutputs()) {
            if (posteriorPredecessors.contains(output)) {
                String id = ((BEASTInterface) output).getID();
                if (id.indexOf('.') >= 0) {
                    id = id.substring(0, id.indexOf('.'));
                    if (outputIDs.get(id) > 1) {
                        addLink(beastObject, (BEASTInterface) output);
                    }
                }
            }
        }
        // add parameters that have more than 1 outputs into susbtitution models
        if (beastObject instanceof Parameter<?>) {
            for (Object output : beastObject.getOutputs()) {
                if (posteriorPredecessors.contains(output)) {
                    if (output instanceof SubstitutionModel) {
                        int nrOfSubstModelsInOutput = 0;
                        try {
                            for (Input<?> input : ((BEASTInterface) output).listInputs()) {
                                if (input.get() != null && input.get().equals(beastObject)) {
                                    nrOfSubstModelsInOutput++;
                                }
                            }
                        } catch (Exception e) {
                        // ignore
                        }
                        if (nrOfSubstModelsInOutput > 1) {
                            addLink(beastObject, (BEASTInterface) output);
                        }
                    }
                }
            }
        }
    }
    hasLinkedAtLeastOnce = false;
    for (Input<?> input : linked) {
        if (input.getType().isAssignableFrom(RealParameter.class)) {
            hasLinkedAtLeastOnce = true;
            break;
        }
    }
}
Also used : HashMap(java.util.HashMap) RealParameter(beast.core.parameter.RealParameter) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with SubstitutionModel

use of beast.evolution.substitutionmodel.SubstitutionModel in project beast2 by CompEvol.

the class BeautiSubTemplate method createSubNet.

private BEASTInterface createSubNet(PartitionContext context, /*BeautiDoc doc,*/
HashMap<String, BEASTInterface> idMap, boolean init) {
    subNetDepth++;
    if (subNetDepth > 10) {
        // looks like we cannot find what we are looking for
        throw new IllegalArgumentException("Potential programmer error: It looks like there is a required input that was not specified in the tenmplate");
    }
    // wrap in a beast element with appropriate name spaces
    String _sXML = "<beast version='2.0' \n" + "namespace='beast.app.beauti:beast.core:beast.evolution.branchratemodel:beast.evolution.speciation:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood:beast.evolution:beast.math.distributions'>\n" + xml + "</beast>\n";
    // resolve alignment references
    _sXML = _sXML.replaceAll("idref=[\"']data['\"]", "idref='" + context.partition + "'");
    _sXML = _sXML.replaceAll("[\"']@data['\"]", "'@" + context.partition + "'");
    // ensure uniqueness of IDs
    // _sXML.replaceAll("\\$\\(n\\)", partition);
    _sXML = BeautiDoc.translatePartitionNames(_sXML, context);
    XMLParser parser = new XMLParser();
    parser.setRequiredInputProvider(doc, context);
    List<BEASTInterface> beastObjects = null;
    try {
        beastObjects = parser.parseTemplate(_sXML, idMap, true);
        for (BEASTInterface beastObject : beastObjects) {
            doc.addPlugin(beastObject);
            try {
                Log.warning.println("Adding " + beastObject.getClass().getName() + " " + beastObject);
            } catch (Exception e) {
                Log.err.println("Adding " + beastObject.getClass().getName());
            }
        }
        for (BeautiConnector connector : connectors) {
            if (init && connector.atInitialisationOnly()) {
                // ||
                doc.connect(connector, context);
            }
            // System.out.println(connector.sourceID + " == " + connector.targetID);
            if (connector.targetID != null && connector.targetID.equals("prior")) {
                Log.warning.println(">>> No description for connector " + connector.sourceID + " == " + connector.targetID);
            }
            if (connector.getTipText() != null) {
                String ID = BeautiDoc.translatePartitionNames(connector.sourceID, context);
                String tipText = BeautiDoc.translatePartitionNames(connector.getTipText(), context).trim().replaceAll("\\s+", " ");
                // System.out.println(ID + " -> " + tipText);
                doc.tipTextMap.put(ID, tipText);
            }
        }
        if (suppressedInputs.get() != null) {
            String[] inputs = suppressedInputs.get().split(",");
            for (String input : inputs) {
                input = input.trim();
                doc.beautiConfig.suppressBEASTObjects.add(input);
            }
        }
        if (inlineInput.get() != null) {
            String[] inputs = inlineInput.get().split(",");
            for (String input : inputs) {
                input = input.trim();
                doc.beautiConfig.inlineBEASTObject.add(input);
            }
        }
        if (collapsedInput.get() != null) {
            String[] inputs = collapsedInput.get().split(",");
            for (String input : inputs) {
                input = input.trim();
                doc.beautiConfig.collapsedBEASTObjects.add(input);
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (mainID.equals("[top]")) {
        subNetDepth--;
        return beastObjects.get(0);
    }
    String id = mainID;
    // id.replaceAll("\\$\\(n\\)", partition);
    id = BeautiDoc.translatePartitionNames(id, context);
    BEASTInterface beastObject = doc.pluginmap.get(id);
    if (this == doc.beautiConfig.partitionTemplate.get()) {
        // HACK: need to make sure the subst model is of the correct type
        BEASTInterface treeLikelihood = doc.pluginmap.get("treeLikelihood." + context.partition);
        if (treeLikelihood != null && ((GenericTreeLikelihood) treeLikelihood).siteModelInput.get() instanceof SiteModel.Base) {
            SiteModel.Base siteModel = (SiteModel.Base) ((GenericTreeLikelihood) treeLikelihood).siteModelInput.get();
            SubstitutionModel substModel = siteModel.substModelInput.get();
            try {
                if (!siteModel.canSetSubstModel(substModel)) {
                    setUpSubstModel(siteModel, context);
                }
            } catch (Exception e) {
                setUpSubstModel(siteModel, context);
            }
        }
        // HACK2: rename file name for trace log if it has the default value
        Logger logger = (Logger) doc.pluginmap.get("tracelog");
        if (logger != null) {
            String fileName = logger.fileNameInput.get();
            if (fileName.startsWith("beast.") && treeLikelihood != null) {
                Alignment data = ((GenericTreeLikelihood) treeLikelihood).dataInput.get();
                while (data instanceof FilteredAlignment) {
                    data = ((FilteredAlignment) data).alignmentInput.get();
                }
                fileName = data.getID() + fileName.substring(5);
                try {
                    logger.fileNameInput.setValue(fileName, logger);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    subNetDepth--;
    // System.err.println(new XMLProducer().toXML(beastObject));
    return beastObject;
}
Also used : GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) SiteModel(beast.evolution.sitemodel.SiteModel) Logger(beast.core.Logger) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) Base(beast.evolution.sitemodel.SiteModelInterface.Base) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser)

Example 4 with SubstitutionModel

use of beast.evolution.substitutionmodel.SubstitutionModel in project beast2 by CompEvol.

the class ThreadedTreeLikelihood method duplicate.

/**
 * create new instance of src object, connecting all inputs from src object
 * Note if input is a SubstModel, it is duplicated as well.
 * @param src object to be copied
 * @param i index used to extend ID with.
 * @return copy of src object
 */
private Object duplicate(BEASTInterface src, int i) {
    if (src == null) {
        return null;
    }
    BEASTInterface copy;
    try {
        copy = src.getClass().newInstance();
        copy.setID(src.getID() + "_" + i);
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
        throw new RuntimeException("Programmer error: every object in the model should have a default constructor that is publicly accessible: " + src.getClass().getName());
    }
    for (Input<?> input : src.listInputs()) {
        if (input.get() != null) {
            if (input.get() instanceof List) {
                // ((List)copy.getInput(input.getName())).clear();
                for (Object o : (List<?>) input.get()) {
                    if (o instanceof BEASTInterface) {
                        // make sure it is not already in the list
                        copy.setInputValue(input.getName(), o);
                    }
                }
            } else if (input.get() instanceof SubstitutionModel) {
                // duplicate subst models
                BEASTInterface substModel = (BEASTInterface) duplicate((BEASTInterface) input.get(), i);
                copy.setInputValue(input.getName(), substModel);
            } else {
                // it is some other value
                copy.setInputValue(input.getName(), input.get());
            }
        }
    }
    copy.initAndValidate();
    return copy;
}
Also used : BEASTInterface(beast.core.BEASTInterface) ArrayList(java.util.ArrayList) List(java.util.List) SubstitutionModel(beast.evolution.substitutionmodel.SubstitutionModel)

Aggregations

BEASTInterface (beast.core.BEASTInterface)4 SubstitutionModel (beast.evolution.substitutionmodel.SubstitutionModel)4 BEASTObject (beast.core.BEASTObject)2 Parameter (beast.core.parameter.Parameter)2 RealParameter (beast.core.parameter.RealParameter)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TransformerException (javax.xml.transform.TransformerException)2 SAXException (org.xml.sax.SAXException)2 Logger (beast.core.Logger)1 Alignment (beast.evolution.alignment.Alignment)1 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)1 GenericTreeLikelihood (beast.evolution.likelihood.GenericTreeLikelihood)1 SiteModel (beast.evolution.sitemodel.SiteModel)1 Base (beast.evolution.sitemodel.SiteModelInterface.Base)1 XMLParser (beast.util.XMLParser)1 XMLParserException (beast.util.XMLParserException)1 HashMap (java.util.HashMap)1 List (java.util.List)1