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;
}
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();
}
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);
}
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");
}
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;
}
Aggregations