Search in sources :

Example 1 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BeautiDoc method deepCopyPlugin.

/**
 * Create a deep copy of a beastObject, but in a different partition context
 * First, find all beastObjects that are predecessors of the beastObject to be copied
 * that are ancestors of StateNodes
 *
 * @param beastObject
 * @param parent
 * @return
 */
public static BEASTInterface deepCopyPlugin(BEASTInterface beastObject, BEASTInterface parent, MCMC mcmc, PartitionContext oldContext, PartitionContext newContext, BeautiDoc doc, List<BEASTInterface> tabooList) {
    /**
     * taboo = list of beastObjects that should not be copied *
     */
    Set<BEASTInterface> taboo = new HashSet<>();
    taboo.add(parent);
    // add state
    taboo.add(mcmc.startStateInput.get());
    // add likelihood and prior
    if (mcmc.posteriorInput.get() instanceof CompoundDistribution) {
        for (Distribution distr : ((CompoundDistribution) mcmc.posteriorInput.get()).pDistributions.get()) {
            if (distr instanceof CompoundDistribution) {
                taboo.add(distr);
            }
        }
    }
    // add posterior
    taboo.add(mcmc.posteriorInput.get());
    // parent of operators
    taboo.add(mcmc);
    // add loggers
    taboo.addAll(mcmc.loggersInput.get());
    // add exception for *BEAST logger (perhaps need to be generalised?)
    if (doc.pluginmap.containsKey("SpeciesTreeLoggerX")) {
        taboo.add(doc.pluginmap.get("SpeciesTreeLoggerX"));
    }
    // add trees
    for (StateNode node : mcmc.startStateInput.get().stateNodeInput.get()) {
        if (node instanceof Tree) {
            taboo.add(node);
        }
    }
    // add MRCAPriors
    for (String id : doc.pluginmap.keySet()) {
        BEASTInterface o = doc.pluginmap.get(id);
        if (o instanceof MRCAPrior) {
            taboo.add(o);
        }
    }
    if (tabooList != null) {
        taboo.addAll(tabooList);
    }
    // find predecessors of beastObject to be copied
    List<BEASTInterface> predecessors = new ArrayList<>();
    collectPredecessors(beastObject, predecessors);
    // find ancestors of StateNodes that are predecessors + the beastObject
    // itself
    Set<BEASTInterface> ancestors = new HashSet<>();
    collectAncestors(beastObject, ancestors, taboo);
    Log.info.print(Arrays.toString(ancestors.toArray()));
    for (BEASTInterface beastObject2 : predecessors) {
        if (beastObject2 instanceof StateNode) {
            Set<BEASTInterface> ancestors2 = new HashSet<>();
            collectAncestors(beastObject2, ancestors2, taboo);
            ancestors.addAll(ancestors2);
        } else if (beastObject2 instanceof Alignment || beastObject2 instanceof FilteredAlignment) {
            for (Object output : beastObject2.getOutputs()) {
                if (!taboo.contains(output)) {
                    Set<BEASTInterface> ancestors2 = new HashSet<>();
                    collectAncestors((BEASTInterface) output, ancestors2, taboo);
                    ancestors.addAll(ancestors2);
                }
            }
        }
    }
    // collect priors
    predecessors.addAll(ancestors);
    for (BEASTInterface o : predecessors) {
        if (o instanceof Prior) {
            List<BEASTInterface> priorPredecessors = new ArrayList<>();
            collectPredecessors(o, priorPredecessors);
            ancestors.addAll(priorPredecessors);
        }
    }
    Log.info.print(Arrays.toString(predecessors.toArray()));
    for (BEASTInterface p : ancestors) {
        Log.info.print("(");
        try {
            for (BEASTInterface p2 : p.listActiveBEASTObjects()) {
                if (ancestors.contains(p2)) {
                    Log.info.print(p2.getID() + " ");
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        Log.info.print(") ");
        Log.info.println(p.getID());
    }
    // now the ancestors contain all beastObjects to be copied
    // make a copy of all individual BEASTObjects, before connecting them up
    Map<String, BEASTInterface> copySet = new HashMap<>();
    for (BEASTInterface beastObject2 : ancestors) {
        String id = beastObject2.getID();
        if (id == null) {
            id = beastObject.getClass().getName().replaceAll(".*\\.", "");
            int i = 0;
            while (doc.pluginmap.containsKey(id + "." + i)) {
                i++;
            }
            id = id + "." + i;
            beastObject2.setID(id);
        }
        String copyID = renameId(id, oldContext, newContext);
        if (!id.equals(copyID)) {
            if (doc.pluginmap.containsKey(copyID)) {
                BEASTInterface org = doc.pluginmap.get(copyID);
                copySet.put(id, org);
            } else {
                BEASTInterface copy;
                try {
                    copy = beastObject2.getClass().newInstance();
                    copy.setID(copyID);
                    copySet.put(id, copy);
                } 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");
                }
            }
        }
        Log.warning.println("Copy: " + id + " -> " + copyID);
    }
    // set all inputs of copied beastObjects + outputs to taboo
    for (BEASTInterface beastObject2 : ancestors) {
        String id = beastObject2.getID();
        BEASTInterface copy = copySet.get(id);
        if (copy != null) {
            Log.warning.println("Processing: " + id + " -> " + copy.getID());
            // set inputs
            for (Input<?> input : beastObject2.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) {
                                BEASTInterface value = getCopyValue((BEASTInterface) o, copySet, oldContext, newContext, doc);
                                // make sure it is not already in the list
                                Object o2 = copy.getInput(input.getName()).get();
                                boolean alreadyInList = false;
                                if (o2 instanceof List) {
                                    List<?> currentList = (List<?>) o2;
                                    for (Object v : currentList) {
                                        if (v == value) {
                                            alreadyInList = true;
                                            break;
                                        }
                                    }
                                }
                                if (!alreadyInList) {
                                    // add to the list
                                    copy.setInputValue(input.getName(), value);
                                }
                            } else {
                                // it is a primitive value
                                if (copy instanceof Parameter.Base && input.getName().equals("value")) {
                                    // // prevent appending to parameter values
                                    Parameter.Base<?> p = ((Parameter.Base<?>) copy);
                                    ((List<?>) p.valuesInput.get()).clear();
                                }
                                copy.setInputValue(input.getName(), input.get());
                            }
                        }
                    } else if (input.get() instanceof BEASTInterface) {
                        // handle BEASTObject
                        BEASTInterface value = getCopyValue((BEASTInterface) input.get(), copySet, oldContext, newContext, doc);
                        copy.setInputValue(input.getName(), value);
                    } else if (input.get() instanceof String) {
                        // may need to replace partition info
                        String s = (String) input.get();
                        s = s.replaceAll("\\.c:[a-zA-Z0-9_]*", ".c:" + newContext.clockModel);
                        s = s.replaceAll("\\.s:[a-zA-Z0-9_]*", ".s:" + newContext.siteModel);
                        s = s.replaceAll("\\.t:[a-zA-Z0-9_]*", ".t:" + newContext.tree);
                        copy.setInputValue(input.getName(), s);
                    } else {
                        // it is a primitive value
                        copy.setInputValue(input.getName(), input.get());
                    }
                }
            }
            // set outputs
            for (Object output : beastObject2.getOutputs()) {
                if (taboo.contains(output) && output != parent) {
                    BEASTInterface output2 = getCopyValue((BEASTInterface) output, copySet, oldContext, newContext, doc);
                    for (Input<?> input : ((BEASTInterface) output).listInputs()) {
                        // do not add state node initialisers automatically
                        if (input.get() instanceof List && // do not update state node initialisers
                        !(taboo.contains(output2) && input.getName().equals("init"))) {
                            List<?> list = (List<?>) input.get();
                            if (list.contains(beastObject2)) {
                                List<?> list2 = (List<?>) output2.getInput(input.getName()).get();
                                if (!list2.contains(copy)) {
                                    output2.setInputValue(input.getName(), copy);
                                }
                            }
                        }
                    }
                }
            }
            copySet.put(id, copy);
        // Log.warning.println(base.operatorsAsString());
        }
    }
    // deep copy must be obtained from copyset, before sorting
    // since the sorting changes (deletes items) from the copySet map
    BEASTInterface deepCopy = copySet.get(beastObject.getID());
    // first need to sort copySet by topology, before we can initAndValidate
    // them
    List<BEASTInterface> sorted = new ArrayList<>();
    Collection<BEASTInterface> values = copySet.values();
    while (values.size() > 0) {
        for (BEASTInterface copy : values) {
            boolean found = false;
            for (BEASTInterface beastObject2 : copy.listActiveBEASTObjects()) {
                if (values.contains(beastObject2)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                sorted.add(copy);
            }
        }
        values.remove(sorted.get(sorted.size() - 1));
    }
    // initialise copied beastObjects
    Set<BEASTInterface> done = new HashSet<>();
    for (BEASTInterface copy : sorted) {
        try {
            if (!done.contains(copy)) {
                copy.initAndValidate();
                done.add(copy);
            }
        } catch (Exception e) {
            // ignore
            Log.warning.print(e.getMessage());
        }
        if (doc != null) {
            doc.addPlugin(copy);
        }
    }
    doc.scrubAll(true, false);
    return deepCopy;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) TraitSet(beast.evolution.tree.TraitSet) TaxonSet(beast.evolution.alignment.TaxonSet) HashMap(java.util.HashMap) StateNode(beast.core.StateNode) ArrayList(java.util.ArrayList) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) CompoundDistribution(beast.core.util.CompoundDistribution) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) Tree(beast.evolution.tree.Tree) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Prior(beast.math.distributions.Prior) MRCAPrior(beast.math.distributions.MRCAPrior) CompoundDistribution(beast.core.util.CompoundDistribution) ParametricDistribution(beast.math.distributions.ParametricDistribution) Distribution(beast.core.Distribution) MRCAPrior(beast.math.distributions.MRCAPrior) RealParameter(beast.core.parameter.RealParameter) Parameter(beast.core.parameter.Parameter) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject)

Example 2 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BeautiDoc method mergeSequences.

/**
 * Merge sequence data with xml specification.
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws XMLParserException
 */
void mergeSequences(String xml) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    if (xml == null) {
        xml = processTemplate(STANDARD_TEMPLATE);
    }
    loadTemplate(xml);
    // create XML for alignments
    if (beautiConfig != null) {
        for (Alignment alignment : alignments) {
            beautiConfig.partitionTemplate.get().createSubNet(alignment, this, true);
        }
    } else {
        // replace alignment
        for (BEASTInterface beastObject : pluginmap.values()) {
            if (beastObject instanceof Alignment) {
                // use toArray to prevent ConcurrentModificationException
                for (Object output : beastObject.getOutputs().toArray()) {
                    replaceInputs((BEASTInterface) output, beastObject, alignments.get(0));
                }
            }
        }
        return;
    }
    determinePartitions();
}
Also used : FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) BEASTInterface(beast.core.BEASTInterface) BEASTObject(beast.core.BEASTObject)

Example 3 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BeautiDoc method importXMLAlignment.

public void importXMLAlignment(File file) {
    Alignment data = (Alignment) BeautiAlignmentProvider.getXMLData(file);
    data.initAndValidate();
    addAlignmentWithSubnet(data, beautiConfig.partitionTemplate.get());
// connectModel();
// fireDocHasChanged();
}
Also used : FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment)

Example 4 with Alignment

use of beast.evolution.alignment.Alignment in project beast2 by CompEvol.

the class BeautiDoc method parseArgs.

public ActionOnExit parseArgs(String[] args) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    ActionOnExit endState = ActionOnExit.UNKNOWN;
    String outputFileName = "beast.xml";
    String xml = null;
    String templateXML = null;
    TraitSet traitset = null;
    int i = 0;
    try {
        while (i < args.length) {
            int old = i;
            if (args[i].equals("")) {
                i += 1;
            } else if (args[i].equals("-capture")) {
                // capture stderr and stdout
                // already done in beast.app.beauti.Beauti
                i += 1;
            } else if (args[i].equals("-xml")) {
                String fileName = args[i + 1];
                xml = load(fileName);
                // XMLParser parser = new XMLParser();
                // m_doc.m_mcmc.setValue(parser.parseFile(fileName),
                // m_doc);
                this.fileName = nameFromFile(fileName);
                i += 2;
            } else if (args[i].equals("-template")) {
                String fileName = args[i + 1];
                templateXML = processTemplate(fileName);
                templateFileName = fileName;
                templateName = nameFromFile(fileName);
                i += 2;
            } else if (args[i].equals("-nex")) {
                // NB: multiple -nex/-xmldata commands can be processed!
                String fileName = args[i + 1];
                NexusParser parser = new NexusParser();
                parser.parseFile(new File(fileName));
                if (parser.filteredAlignments.size() > 0) {
                    for (Alignment data : parser.filteredAlignments) {
                        alignments.add(data);
                    }
                } else {
                    alignments.add(parser.m_alignment);
                }
                i += 2;
                traitset = parser.traitSet;
            } else if (args[i].equals("-xmldata")) {
                // NB: multiple -xmldata/-nex commands can be processed!
                String fileName = args[i + 1];
                Alignment alignment = (Alignment) BeautiAlignmentProvider.getXMLData(new File(fileName));
                alignments.add(alignment);
                i += 2;
            } else if (args[i].equals("-exitaction")) {
                if (args[i + 1].equals("writexml")) {
                    endState = ActionOnExit.WRITE_XML;
                } else if (args[i + 1].equals("usetemplate")) {
                    endState = ActionOnExit.SHOW_DETAILS_USE_TEMPLATE;
                } else if (args[i + 1].equals("usexml")) {
                    endState = ActionOnExit.SHOW_DETAILS_USE_XML_SPEC;
                } else if (args[i + 1].equals("merge")) {
                    endState = ActionOnExit.MERGE_AND_WRITE_XML;
                } else {
                    throw new IllegalArgumentException("Expected one of 'writexml','usetemplate' or 'usexml', not " + args[i + 1]);
                }
                i += 2;
            } else if (args[i].equals("-out")) {
                outputFileName = args[i + 1];
                i += 2;
            } else if (args[i].equals("-noerr")) {
                System.setErr(new PrintStream(new OutputStream() {

                    @Override
                    public void write(int b) {
                    }
                }));
                i += 1;
            }
            if (i == old) {
                throw new IllegalArgumentException("Wrong argument: " + args[i]);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
    initialize(endState, xml, templateXML, outputFileName);
    addTraitSet(traitset);
    return endState;
}
Also used : PrintStream(java.io.PrintStream) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TraitSet(beast.evolution.tree.TraitSet) NexusParser(beast.util.NexusParser) File(java.io.File) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with Alignment

use of beast.evolution.alignment.Alignment 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)

Aggregations

Alignment (beast.evolution.alignment.Alignment)102 Test (org.junit.Test)43 Sequence (beast.evolution.alignment.Sequence)31 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)30 Tree (beast.evolution.tree.Tree)29 SiteModel (beast.evolution.sitemodel.SiteModel)27 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)22 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)20 ArrayList (java.util.ArrayList)17 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)17 Frequencies (beast.evolution.substitutionmodel.Frequencies)13 RealParameter (beast.core.parameter.RealParameter)12 TaxonSet (beast.evolution.alignment.TaxonSet)11 BEASTInterface (beast.core.BEASTInterface)10 ClusterTree (beast.util.ClusterTree)9 Taxon (beast.evolution.alignment.Taxon)6 HKY (beast.evolution.substitutionmodel.HKY)6 Node (beast.evolution.tree.Node)6 TreeParser (beast.util.TreeParser)6 IOException (java.io.IOException)6