Search in sources :

Example 1 with Taxon

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

the class BeautiDoc method getTaxon.

/**
 * create Taxon with given name, reuse if a taxon
 *  with this name already exists
 */
public Taxon getTaxon(String taxaName) {
    if (taxaset.keySet().contains(taxaName)) {
        return taxaset.get(taxaName);
    } else {
        try {
            Taxon taxon = new Taxon(taxaName);
            registerPlugin(taxon);
            return taxon;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : Taxon(beast.evolution.alignment.Taxon) 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 Taxon

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

the class NodeReheight method initAndValidate.

@Override
public void initAndValidate() {
    /**
     * maps gene taxa names to species number *
     */
    final Map<String, Integer> taxonMap = new HashMap<>();
    final List<Taxon> list = taxonSetInput.get().taxonsetInput.get();
    if (list.size() <= 1) {
        Log.err.println("NodeReheight operator requires at least 2 taxa while the taxon set (id=" + taxonSetInput.get().getID() + ") has only " + list.size() + " taxa. " + "If the XML file was set up in BEAUti, this probably means a taxon assignment needs to be set up in the taxonset panel.");
        // assume we are in BEAUti, back off for now
        return;
    }
    for (int i = 0; i < list.size(); i++) {
        final Taxon taxa = list.get(i);
        // cast should be ok if taxon-set is the set for the species tree
        final TaxonSet set = (TaxonSet) taxa;
        for (final Taxon taxon : set.taxonsetInput.get()) {
            taxonMap.put(taxon.getID(), i);
        }
    }
    /**
     * build the taxon map for each gene tree *
     */
    m_taxonMap = new ArrayList<>();
    for (final Tree tree : geneTreesInput.get()) {
        final Map<Integer, Integer> map = new HashMap<>();
        setupTaxaMap(tree.getRoot(), map, taxonMap);
        m_taxonMap.add(map);
    }
    nrOfGeneTrees = geneTreesInput.get().size();
    nrOfSpecies = treeInput.get().getLeafNodeCount();
}
Also used : HashMap(java.util.HashMap) Taxon(beast.evolution.alignment.Taxon) Tree(beast.evolution.tree.Tree) TaxonSet(beast.evolution.alignment.TaxonSet)

Example 3 with Taxon

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

the class MRCAPriorTest method testMRCATimePrior.

@Test
public void testMRCATimePrior() throws Exception {
    Alignment data = BEASTTestCase.getAlignment();
    TreeParser tree = new TreeParser();
    tree.initByName("taxa", data, "newick", "((human:0.024003,(chimp:0.010772,bonobo:0.010772):0.013231):0.012035," + "(gorilla:0.024003,(orangutan:0.010772,siamang:0.010772):0.013231):0.012035);", "IsLabelledNewick", true);
    Taxon human = new Taxon();
    human.setID("human");
    Taxon bonobo = new Taxon();
    bonobo.setID("bonobo");
    Taxon chimp = new Taxon();
    chimp.setID("chimp");
    Taxon gorilla = new Taxon();
    gorilla.setID("gorilla");
    Taxon orangutan = new Taxon();
    orangutan.setID("orangutan");
    Taxon siamang = new Taxon();
    siamang.setID("siamang");
    MRCAPrior prior = new MRCAPrior();
    TaxonSet set = new TaxonSet();
    set.initByName("taxon", human, "taxon", bonobo, "taxon", chimp);
    Exponential exp = new Exponential();
    /* get distribution for set (human, bonobo, chimp) */
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true, "distr", exp);
    double logP = prior.calculateLogP();
    assertEquals(-0.024003, logP, BEASTTestCase.PRECISION);
    /* get distribution for set (human, chimp), do not require the set to by monophyletic */
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", chimp);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", false);
    logP = prior.calculateLogP();
    assertEquals(-0.024003, logP, BEASTTestCase.PRECISION);
    /* get distribution for set (human, chimp), DO require the set to by monophyletic */
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(Double.NEGATIVE_INFINITY, logP, 0);
    /* get distribution for set (human, gorilla) = root, not monophyletic */
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", gorilla);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", false);
    logP = prior.calculateLogP();
    assertEquals(-0.024003 - 0.012035, logP, BEASTTestCase.PRECISION);
}
Also used : Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) Taxon(beast.evolution.alignment.Taxon) MRCAPrior(beast.math.distributions.MRCAPrior) Exponential(beast.math.distributions.Exponential) TaxonSet(beast.evolution.alignment.TaxonSet) Test(org.junit.Test)

Example 4 with Taxon

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

the class MRCAPriorTest method testSingleMonophyleticConstraint.

@Test
public void testSingleMonophyleticConstraint() throws Exception {
    Alignment data = BEASTTestCase.getAlignment();
    TreeParser tree = new TreeParser();
    tree.initByName("taxa", data, "newick", "((human:0.024003,(chimp:0.010772,bonobo:0.010772):0.013231):0.012035," + "(gorilla:0.024003,(orangutan:0.010772,siamang:0.010772):0.013231):0.012035);", "IsLabelledNewick", true);
    Taxon human = new Taxon();
    human.setID("human");
    Taxon bonobo = new Taxon();
    bonobo.setID("bonobo");
    Taxon chimp = new Taxon();
    chimp.setID("chimp");
    Taxon gorilla = new Taxon();
    gorilla.setID("gorilla");
    Taxon orangutan = new Taxon();
    orangutan.setID("orangutan");
    Taxon siamang = new Taxon();
    siamang.setID("siamang");
    MRCAPrior prior = new MRCAPrior();
    /* check (human, bonobo, chimp) is monophyletic **/
    TaxonSet set = new TaxonSet();
    set.initByName("taxon", human, "taxon", bonobo, "taxon", chimp);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    double logP = prior.calculateLogP();
    assertEquals(logP, 0, 0);
    /* check (gorilla, siamang) is NOT monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", gorilla, "taxon", siamang);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, Double.NEGATIVE_INFINITY, 0);
    /* check (gorilla, orangutan, siamang) is monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", gorilla, "taxon", orangutan, "taxon", siamang);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, 0, 0);
    /* check (human, gorilla) is NOT monophyletic **/
    set = new TaxonSet();
    set.initByName("taxon", human, "taxon", gorilla);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true);
    logP = prior.calculateLogP();
    assertEquals(logP, Double.NEGATIVE_INFINITY, 0);
    set.setID("test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    prior.init(ps);
    String log = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    assertEquals(log, "mrcatime(test)\t");
    baos = new ByteArrayOutputStream();
    ps = new PrintStream(baos);
    prior.initByName("tree", tree, "taxonset", set, "monophyletic", true, "useOriginate", true);
    prior.init(ps);
    log = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    assertEquals(log, "mrcatime(test.originate)\t");
}
Also used : PrintStream(java.io.PrintStream) Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) Taxon(beast.evolution.alignment.Taxon) MRCAPrior(beast.math.distributions.MRCAPrior) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TaxonSet(beast.evolution.alignment.TaxonSet) Test(org.junit.Test)

Example 5 with Taxon

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

the class TaxonSetInputEditor method guessTaxonSets.

/**
 * guesses taxon sets based on pattern in regExp based on the taxa in
 * m_rawData
 */
public int guessTaxonSets(String regexp, int minSize) {
    m_taxonset.clear();
    HashMap<String, TaxonSet> map = new HashMap<>();
    Pattern m_pattern = Pattern.compile(regexp);
    Set<Taxon> taxa = new HashSet<>();
    Set<String> taxonIDs = new HashSet<>();
    for (Alignment alignment : getDoc().alignments) {
        for (String id : alignment.getTaxaNames()) {
            if (!taxonIDs.contains(id)) {
                Taxon taxon = getDoc().getTaxon(id);
                taxa.add(taxon);
                taxonIDs.add(id);
            }
        }
        for (Sequence sequence : alignment.sequenceInput.get()) {
            String id = sequence.taxonInput.get();
            if (!taxonIDs.contains(id)) {
                Taxon taxon = getDoc().getTaxon(sequence.taxonInput.get());
                // ensure sequence and taxon do not get same ID
                if (sequence.getID().equals(sequence.taxonInput.get())) {
                    sequence.setID("_" + sequence.getID());
                }
                taxa.add(taxon);
                taxonIDs.add(id);
            }
        }
    }
    List<String> unknowns = new ArrayList<>();
    for (Taxon taxon : taxa) {
        if (!(taxon instanceof TaxonSet)) {
            Matcher matcher = m_pattern.matcher(taxon.getID());
            String match;
            if (matcher.find()) {
                match = matcher.group(1);
            } else {
                match = "UNKNOWN";
                unknowns.add(taxon.getID());
            }
            try {
                if (map.containsKey(match)) {
                    TaxonSet set = map.get(match);
                    set.taxonsetInput.setValue(taxon, set);
                } else {
                    TaxonSet set = newTaxonSet(match);
                    set.taxonsetInput.setValue(taxon, set);
                    map.put(match, set);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    if (unknowns.size() > 0) {
        showMisMatchMessage(unknowns);
    }
    // add taxon sets
    int ignored = 0;
    for (TaxonSet set : map.values()) {
        if (set.taxonsetInput.get().size() > minSize) {
            m_taxonset.add(set);
        } else {
            ignored += set.taxonsetInput.get().size();
        }
    }
    return ignored;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) Taxon(beast.evolution.alignment.Taxon) ArrayList(java.util.ArrayList) Sequence(beast.evolution.alignment.Sequence) TaxonSet(beast.evolution.alignment.TaxonSet) PatternSyntaxException(java.util.regex.PatternSyntaxException) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) HashSet(java.util.HashSet)

Aggregations

Taxon (beast.evolution.alignment.Taxon)26 TaxonSet (beast.evolution.alignment.TaxonSet)18 ArrayList (java.util.ArrayList)7 Alignment (beast.evolution.alignment.Alignment)6 Tree (beast.evolution.tree.Tree)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)3 MRCAPrior (beast.math.distributions.MRCAPrior)3 Box (javax.swing.Box)3 BEASTInterface (beast.core.BEASTInterface)2 RealParameter (beast.core.parameter.RealParameter)2 TreeParser (beast.util.TreeParser)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 PrintStream (java.io.PrintStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JButton (javax.swing.JButton)2