Search in sources :

Example 46 with Alignment

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

the class ClusterTreeTest method testUPGMA.

public void testUPGMA() throws Exception {
    Alignment alignment = BEASTTestCase.getAlignment();
    JukesCantor JC = new JukesCantor();
    JC.initAndValidate();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("substModel", JC);
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    String expectedNewick = "((((human:0.01903085702575253,(chimp:0.008560512208575313,bonobo:0.008560512208575313):0.010470344817177218):0.007962255880644985,gorilla:0.026993112906397516):0.019197419394211015,orangutan:0.04619053230060853):0.007214240662738673,siamang:0.053404772963347204):0.0";
    String actualNewick = tree.getRoot().toNewick();
    assertEquals(expectedNewick, actualNewick);
    // select 3 sequences
    List<Sequence> seqs = new ArrayList<Sequence>();
    seqs.addAll(alignment.sequenceInput.get());
    List<Sequence> newseqs = alignment.sequenceInput.get();
    newseqs.clear();
    newseqs.add(Sequence.getSequenceByTaxon("bonobo", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("chimp", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("human", seqs));
    alignment.initAndValidate();
    tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    expectedNewick = "((bonobo:0.008560512208575313,chimp:0.008560512208575313):0.010470344817177218,human:0.01903085702575253):0.0";
    System.err.println("Seqs:");
    for (Sequence s : seqs) {
        System.err.println(s.taxonInput.get());
    }
    System.err.println("Newseqs:");
    for (Sequence s : newseqs) {
        System.err.println(s.taxonInput.get());
    }
    actualNewick = tree.getRoot().toNewick();
    assertEquals(expectedNewick, actualNewick);
    // same sequences in different order
    newseqs.clear();
    newseqs.add(Sequence.getSequenceByTaxon("human", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("chimp", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("bonobo", seqs));
    alignment.initAndValidate();
    tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    actualNewick = tree.getRoot().toNewick();
    expectedNewick = "(human:0.01903085702575253,(chimp:0.008560512208575313,bonobo:0.008560512208575313):0.010470344817177218):0.0";
    assertEquals(expectedNewick, actualNewick);
}
Also used : Alignment(beast.evolution.alignment.Alignment) ClusterTree(beast.util.ClusterTree) ArrayList(java.util.ArrayList) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) JukesCantor(beast.evolution.substitutionmodel.JukesCantor)

Example 47 with Alignment

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

Example 48 with Alignment

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

the class TaxonSetInputEditor method parseTrait.

void parseTrait(String trait) {
    Map<String, String> traitmap = new HashMap<>();
    for (String line : trait.split(",")) {
        String[] strs = line.split("=");
        if (strs.length == 2) {
            traitmap.put(strs[0].trim(), strs[1].trim());
        }
    }
    m_taxonset.clear();
    Set<Taxon> taxa = new HashSet<>();
    Set<String> taxonIDs = new HashSet<>();
    for (Alignment alignment : getDoc().alignments) {
        if (alignment instanceof FilteredAlignment) {
            alignment = ((FilteredAlignment) alignment).alignmentInput.get();
        }
        for (String id : alignment.getTaxaNames()) {
            if (!taxonIDs.contains(id)) {
                Taxon taxon = getDoc().getTaxon(id);
                taxa.add(taxon);
                taxonIDs.add(id);
            }
        }
    }
    HashMap<String, TaxonSet> map = new HashMap<>();
    List<String> unknowns = new ArrayList<>();
    for (Taxon taxon : taxa) {
        if (!(taxon instanceof TaxonSet)) {
            String match = traitmap.get(taxon.getID());
            if (match == null) {
                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();
            }
        }
    }
    // add taxon sets
    for (TaxonSet set : map.values()) {
        m_taxonset.add(set);
    }
    if (unknowns.size() > 0) {
        showMisMatchMessage(unknowns);
    }
}
Also used : HashMap(java.util.HashMap) Taxon(beast.evolution.alignment.Taxon) ArrayList(java.util.ArrayList) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) TaxonSet(beast.evolution.alignment.TaxonSet) PatternSyntaxException(java.util.regex.PatternSyntaxException) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) HashSet(java.util.HashSet)

Example 49 with Alignment

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

the class SiteModelInputEditor method customConnector.

public static boolean customConnector(BeautiDoc doc) {
    try {
        DeltaExchangeOperator operator = (DeltaExchangeOperator) doc.pluginmap.get("FixMeanMutationRatesOperator");
        if (operator == null) {
            return false;
        }
        List<RealParameter> parameters = operator.parameterInput.get();
        parameters.clear();
        // String weights = "";
        CompoundDistribution likelihood = (CompoundDistribution) doc.pluginmap.get("likelihood");
        boolean hasOneEstimatedRate = false;
        List<String> rateIDs = new ArrayList<>();
        List<Integer> weights = new ArrayList<>();
        for (Distribution d : likelihood.pDistributions.get()) {
            GenericTreeLikelihood treelikelihood = (GenericTreeLikelihood) d;
            Alignment data = treelikelihood.dataInput.get();
            int weight = data.getSiteCount();
            if (data.isAscertained) {
                weight -= data.getExcludedPatternCount();
            }
            if (treelikelihood.siteModelInput.get() instanceof SiteModel) {
                SiteModel siteModel = (SiteModel) treelikelihood.siteModelInput.get();
                RealParameter mutationRate = siteModel.muParameterInput.get();
                // clockRate.m_bIsEstimated.setValue(true, clockRate);
                if (mutationRate.isEstimatedInput.get()) {
                    hasOneEstimatedRate = true;
                    if (rateIDs.indexOf(mutationRate.getID()) == -1) {
                        parameters.add(mutationRate);
                        weights.add(weight);
                        rateIDs.add(mutationRate.getID());
                    } else {
                        int k = rateIDs.indexOf(mutationRate.getID());
                        weights.set(k, weights.get(k) + weight);
                    }
                }
            }
        }
        IntegerParameter weightParameter;
        if (weights.size() == 0) {
            weightParameter = new IntegerParameter();
        } else {
            String weightString = "";
            for (int k : weights) {
                weightString += k + " ";
            }
            weightParameter = new IntegerParameter(weightString);
            weightParameter.setID("weightparameter");
        }
        weightParameter.isEstimatedInput.setValue(false, weightParameter);
        operator.parameterWeightsInput.setValue(weightParameter, operator);
        return hasOneEstimatedRate;
    } catch (Exception e) {
    }
    return false;
}
Also used : IntegerParameter(beast.core.parameter.IntegerParameter) GenericTreeLikelihood(beast.evolution.likelihood.GenericTreeLikelihood) ArrayList(java.util.ArrayList) RealParameter(beast.core.parameter.RealParameter) SiteModel(beast.evolution.sitemodel.SiteModel) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompoundDistribution(beast.core.util.CompoundDistribution) Alignment(beast.evolution.alignment.Alignment) CompoundDistribution(beast.core.util.CompoundDistribution) DeltaExchangeOperator(beast.evolution.operators.DeltaExchangeOperator)

Example 50 with Alignment

use of beast.evolution.alignment.Alignment in project bacter by tgvaughan.

the class SerializationDeserializationTest method testXML.

@Test
public void testXML() throws Exception {
    Alignment alignment = getAlignment();
    alignment.setID("alignment");
    Locus locus = new Locus("locus", alignment.getSiteCount());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Add recombination event 1
    Node node1 = acg.getExternalNodes().get(0);
    Node node2 = node1.getParent();
    double height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    double height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    int startLocus = 100;
    int endLocus = 200;
    Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb1);
    // Add recombination event 2
    node1 = acg.getExternalNodes().get(0);
    node2 = acg.getRoot();
    height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    height2 = node2.getHeight() + 1.0;
    startLocus = 300;
    endLocus = 400;
    Conversion recomb2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb2);
    // Write ARG out to XML string
    String xmlStr = acg.toXML();
    // Build DOM from XML string
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
    doc.normalize();
    NodeList nodes = doc.getElementsByTagName("*");
    org.w3c.dom.Node docNode = nodes.item(0);
    // Read ARG back in from DOM
    ConversionGraph argNew = new ConversionGraph();
    argNew.assignFrom(tree);
    argNew.initByName("locus", locus);
    argNew.fromXML(docNode);
    // Check that new ARG matches old
    Conversion newRecomb1 = argNew.getConversions(locus).get(0);
    assertEquals(newRecomb1.getNode1().getNr(), recomb1.getNode1().getNr());
    assertEquals(newRecomb1.getNode2().getNr(), recomb1.getNode2().getNr());
    assertEquals(newRecomb1.getHeight1(), recomb1.getHeight1(), 1e-15);
    assertEquals(newRecomb1.getHeight2(), recomb1.getHeight2(), 1e-15);
    assertEquals(newRecomb1.getStartSite(), recomb1.getStartSite());
    assertEquals(newRecomb1.getEndSite(), recomb1.getEndSite());
    Conversion newRecomb2 = argNew.getConversions(locus).get(1);
    assertEquals(newRecomb2.getNode1().getNr(), recomb2.getNode1().getNr());
    assertEquals(newRecomb2.getNode2().getNr(), recomb2.getNode2().getNr());
    assertEquals(newRecomb2.getHeight1(), recomb2.getHeight1(), 1e-15);
    assertEquals(newRecomb2.getHeight2(), recomb2.getHeight2(), 1e-15);
    assertEquals(newRecomb2.getStartSite(), recomb2.getStartSite());
    assertEquals(newRecomb2.getEndSite(), recomb2.getEndSite());
// Note that there are minor differences in the tree due to
// rounding errors.  Is this normal!?
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Alignment(beast.evolution.alignment.Alignment) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

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