Search in sources :

Example 1 with TraitSet

use of beast.evolution.tree.TraitSet 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 2 with TraitSet

use of beast.evolution.tree.TraitSet in project beast2 by CompEvol.

the class TraitSetTest method testDateForward.

@Test
public void testDateForward() {
    int Nleaves = 2;
    TraitSet timeTrait = new TraitSet();
    timeTrait.initByName("traitname", "date-forward", "taxa", taxonSet(Nleaves), "value", "t0=0, t1=10");
    // The trait actually represents the age of the taxa relative to
    // each other with arbitrary zero, so we test it like this.
    assertEquals(10.0, timeTrait.getValue("t0") - timeTrait.getValue("t1"), 1e-7);
}
Also used : TraitSet(beast.evolution.tree.TraitSet) Test(org.junit.Test)

Example 3 with TraitSet

use of beast.evolution.tree.TraitSet in project beast2 by CompEvol.

the class RandomTreeTest method testCoalescentTimes.

@Test
public void testCoalescentTimes() throws Exception {
    Randomizer.setSeed(53);
    int Nleaves = 10;
    int Niter = 5000;
    // (Serially sampled) coalescent time means and variances
    // estimated from 50000 trees simulated using MASTER
    double[] coalTimeMeansTruth = { 1.754662, 2.833337, 3.843532, 4.850805, 5.849542, 6.847016, 7.8482, 8.855137, 10.15442 };
    double[] coalTimeVarsTruth = { 0.2751625, 0.2727121, 0.2685172, 0.2705117, 0.2678611, 0.2671793, 0.2686952, 0.2828477, 1.076874 };
    // Assemble BEASTObjects needed by RandomTree
    StringBuilder traitSB = new StringBuilder();
    List<Sequence> seqList = new ArrayList<Sequence>();
    for (int i = 0; i < Nleaves; i++) {
        String taxonID = "t " + i;
        seqList.add(new Sequence(taxonID, "?"));
        if (i > 0)
            traitSB.append(",");
        traitSB.append(taxonID).append("=").append(i);
    }
    Alignment alignment = new Alignment(seqList, "nucleotide");
    TaxonSet taxonSet = new TaxonSet(alignment);
    TraitSet timeTrait = new TraitSet();
    timeTrait.initByName("traitname", "date-backward", "taxa", taxonSet, "value", traitSB.toString());
    ConstantPopulation popFunc = new ConstantPopulation();
    popFunc.initByName("popSize", new RealParameter("1.0"));
    // Create RandomTree and TreeInterval instances
    RandomTree tree = new RandomTree();
    TreeIntervals intervals = new TreeIntervals();
    // Estimate coalescence time moments
    double[] coalTimeMeans = new double[Nleaves - 1];
    double[] coalTimeVars = new double[Nleaves - 1];
    double[] coalTimes = new double[Nleaves - 1];
    for (int i = 0; i < Niter; i++) {
        tree.initByName("taxa", alignment, "populationModel", popFunc, "trait", timeTrait);
        intervals.initByName("tree", tree);
        intervals.getCoalescentTimes(coalTimes);
        for (int j = 0; j < Nleaves - 1; j++) {
            coalTimeMeans[j] += coalTimes[j];
            coalTimeVars[j] += coalTimes[j] * coalTimes[j];
        }
    }
    // Normalise means and variances
    for (int j = 0; j < Nleaves - 1; j++) {
        coalTimeMeans[j] /= Niter;
        coalTimeVars[j] /= Niter;
        coalTimeVars[j] -= coalTimeMeans[j] * coalTimeMeans[j];
    }
    // Test means and variances against independently estimated values
    for (int j = 0; j < Nleaves - 1; j++) {
        assert (relError(coalTimeMeans[j], coalTimeMeansTruth[j]) < 5e-3);
        assert (relError(coalTimeVars[j], coalTimeVarsTruth[j]) < 5e-2);
    }
}
Also used : ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) Sequence(beast.evolution.alignment.Sequence) TaxonSet(beast.evolution.alignment.TaxonSet) TreeIntervals(beast.evolution.tree.coalescent.TreeIntervals) Alignment(beast.evolution.alignment.Alignment) ConstantPopulation(beast.evolution.tree.coalescent.ConstantPopulation) RandomTree(beast.evolution.tree.RandomTree) TraitSet(beast.evolution.tree.TraitSet) Test(org.junit.Test)

Example 4 with TraitSet

use of beast.evolution.tree.TraitSet in project beast2 by CompEvol.

the class TipDatesInputEditor method init.

@Override
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
    m_bAddButtons = addButtons;
    this.itemNr = itemNr;
    if (itemNr >= 0) {
        tree = (Tree) ((List<?>) input.get()).get(itemNr);
    } else {
        tree = (Tree) input.get();
    }
    if (tree != null) {
        try {
            m_input = ((BEASTInterface) tree).getInput("trait");
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        m_beastObject = tree;
        traitSet = tree.getDateTrait();
        Box box = Box.createVerticalBox();
        JCheckBox useTipDates = new JCheckBox("Use tip dates", traitSet != null);
        useTipDates.addActionListener(e -> {
            JCheckBox checkBox = (JCheckBox) e.getSource();
            try {
                if (checkBox.isSelected()) {
                    if (traitSet == null) {
                        traitSet = new TraitSet();
                        traitSet.initByName("traitname", "date", "taxa", tree.getTaxonset(), "value", "");
                        traitSet.setID("dateTrait.t:" + BeautiDoc.parsePartition(tree.getID()));
                    }
                    tree.setDateTrait(traitSet);
                } else {
                    tree.setDateTrait(null);
                }
                refreshPanel();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        Box box2 = Box.createHorizontalBox();
        box2.add(useTipDates);
        box2.add(Box.createHorizontalGlue());
        box.add(box2);
        if (traitSet != null) {
            box.add(createButtonBox());
            box.add(createListBox());
        }
        add(box);
    }
}
Also used : TraitSet(beast.evolution.tree.TraitSet) List(java.util.List) DateTimeParseException(java.time.format.DateTimeParseException)

Example 5 with TraitSet

use of beast.evolution.tree.TraitSet in project MultiTypeTree by tgvaughan.

the class InitMigrationModelConnector method uniqueTraitsInData.

public static List<String> uniqueTraitsInData(StructuredCoalescentMultiTypeTree scTree) {
    SortedSet<String> uniqueTypes = new TreeSet<>();
    TraitSet typeTraitSet = scTree.typeTraitInput.get();
    for (String taxonName : typeTraitSet.taxaInput.get().getTaxaNames()) uniqueTypes.add(typeTraitSet.getStringValue(taxonName));
    return new ArrayList<>(uniqueTypes);
}
Also used : TraitSet(beast.evolution.tree.TraitSet)

Aggregations

TraitSet (beast.evolution.tree.TraitSet)11 Test (org.junit.Test)5 RealParameter (beast.core.parameter.RealParameter)3 Alignment (beast.evolution.alignment.Alignment)3 TaxonSet (beast.evolution.alignment.TaxonSet)3 RandomTree (beast.evolution.tree.RandomTree)3 Tree (beast.evolution.tree.Tree)3 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)3 Taxon (beast.evolution.alignment.Taxon)2 SimulatedAlignment (beast.app.seqgen.SimulatedAlignment)1 MCMC (beast.core.MCMC)1 CompoundDistribution (beast.core.util.CompoundDistribution)1 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)1 Sequence (beast.evolution.alignment.Sequence)1 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)1 TipDatesRandomWalker (beast.evolution.operators.TipDatesRandomWalker)1 SiteModel (beast.evolution.sitemodel.SiteModel)1 Frequencies (beast.evolution.substitutionmodel.Frequencies)1 HKY (beast.evolution.substitutionmodel.HKY)1 TreeIntervals (beast.evolution.tree.coalescent.TreeIntervals)1