Search in sources :

Example 16 with Sequence

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

the class ACGLikelihoodApproxTest method testPairwiseDistances.

@Test
public void testPairwiseDistances() throws Exception {
    List<Sequence> sequences = new ArrayList<>();
    // 01234567890123456789
    sequences.add(new Sequence("t1", "AAAAAAAAAAAAAAAAAAAA"));
    sequences.add(new Sequence("t2", "AAAACAAAAAAGAAAAAAAA"));
    sequences.add(new Sequence("t3", "CCCCCCCCCCCCCCCCCCCC"));
    Alignment alignment = new Alignment(sequences, "nucleotide");
    Locus locus = new Locus("locus", alignment);
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", locus.getAlignment());
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    ACGLikelihoodApprox likelihoodApprox = new ACGLikelihoodApprox();
    likelihoodApprox.initByName("acg", acg, "substitutionRate", "1.0", "alignment", alignment, "locus", locus);
    Assert.assertEquals(0, likelihoodApprox.getPairwiseDistance(0, 1, 0, 4));
    Assert.assertEquals(1, likelihoodApprox.getPairwiseDistance(0, 1, 0, 5));
    Assert.assertEquals(1, likelihoodApprox.getPairwiseDistance(0, 1, 0, 11));
    Assert.assertEquals(2, likelihoodApprox.getPairwiseDistance(0, 1, 0, 12));
    Assert.assertEquals(2, likelihoodApprox.getPairwiseDistance(0, 1, 0, 20));
    Assert.assertEquals(5, likelihoodApprox.getPairwiseDistance(1, 2, 2, 8));
    Assert.assertEquals(19, likelihoodApprox.getPairwiseDistance(1, 2, 0, 20));
    Assert.assertEquals(6, likelihoodApprox.getPairwiseDistance(0, 2, 2, 8));
    Assert.assertEquals(20, likelihoodApprox.getPairwiseDistance(0, 2, 0, 20));
}
Also used : Alignment(beast.evolution.alignment.Alignment) ClusterTree(beast.util.ClusterTree) ArrayList(java.util.ArrayList) Sequence(beast.evolution.alignment.Sequence) Test(org.junit.Test)

Example 17 with Sequence

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

the class SimulatedAlignment method simulate.

/**
 * Perform actual sequence simulation.
 */
private void simulate(Locus locus) {
    Node cfRoot = acg.getRoot();
    int nTaxa = acg.getLeafNodeCount();
    double[] categoryProbs = siteModel.getCategoryProportions(cfRoot);
    int nCategories = siteModel.getCategoryCount();
    int nStates = dataType.getStateCount();
    double[][] transitionProbs = new double[nCategories][nStates * nStates];
    int[][] alignment = new int[nTaxa][locus.getSiteCount()];
    for (Region region : acg.getRegions(locus)) {
        int thisLength = region.getRegionLength();
        MarginalTree marginalTree = new MarginalTree(acg, region);
        int[] categories = new int[thisLength];
        for (int i = 0; i < thisLength; i++) categories[i] = Randomizer.randomChoicePDF(categoryProbs);
        int[][] regionAlignment = new int[nTaxa][thisLength];
        Node thisRoot = marginalTree.getRoot();
        int[] parentSequence = new int[region.getRegionLength()];
        double[] frequencies = siteModel.getSubstitutionModel().getFrequencies();
        for (int i = 0; i < parentSequence.length; i++) parentSequence[i] = Randomizer.randomChoicePDF(frequencies);
        traverse(thisRoot, parentSequence, categories, transitionProbs, regionAlignment);
        // DEBUG: Count differences
        int segsites = 0;
        for (int s = 0; s < regionAlignment[0].length; s++) {
            int state = regionAlignment[0][s];
            for (int l = 1; l < nTaxa; l++) {
                if (state != regionAlignment[l][s]) {
                    segsites += 1;
                    break;
                }
            }
        }
        System.out.println(segsites + " segregating sites in region " + region);
        copyToAlignment(alignment, regionAlignment, region);
    }
    for (int leafIdx = 0; leafIdx < nTaxa; leafIdx++) {
        String sSeq = dataType.encodingToString(alignment[leafIdx]);
        String sTaxon = acg.getNode(leafIdx).getID();
        sequenceInput.setValue(new Sequence(sTaxon, sSeq), this);
    }
}
Also used : Node(beast.evolution.tree.Node) Region(bacter.Region) MarginalTree(bacter.MarginalTree) Sequence(beast.evolution.alignment.Sequence)

Example 18 with Sequence

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

the class BeautiAlignmentProvider method parseBeast1XML.

private static BEASTInterface parseBeast1XML(String ID, String xml) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
    doc.normalize();
    NodeList alignments = doc.getElementsByTagName("alignment");
    Alignment alignment = new Alignment();
    alignment.dataTypeInput.setValue("nucleotide", alignment);
    // parse first alignment
    org.w3c.dom.Node node = alignments.item(0);
    String dataTypeName = node.getAttributes().getNamedItem("dataType").getNodeValue();
    int totalCount = 4;
    if (dataTypeName == null) {
        alignment.dataTypeInput.setValue("integer", alignment);
    } else if (dataTypeName.toLowerCase().equals("dna") || dataTypeName.toLowerCase().equals("nucleotide")) {
        alignment.dataTypeInput.setValue("nucleotide", alignment);
        totalCount = 4;
    } else if (dataTypeName.toLowerCase().equals("aminoacid") || dataTypeName.toLowerCase().equals("protein")) {
        alignment.dataTypeInput.setValue("aminoacid", alignment);
        totalCount = 20;
    } else {
        alignment.dataTypeInput.setValue("integer", alignment);
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        org.w3c.dom.Node child = children.item(i);
        if (child.getNodeName().equals("sequence")) {
            Sequence sequence = new Sequence();
            // find the taxon
            String taxon = "";
            NodeList sequenceChildren = child.getChildNodes();
            for (int j = 0; j < sequenceChildren.getLength(); j++) {
                org.w3c.dom.Node child2 = sequenceChildren.item(j);
                if (child2.getNodeName().equals("taxon")) {
                    taxon = child2.getAttributes().getNamedItem("idref").getNodeValue();
                }
            }
            String data = child.getTextContent();
            sequence.initByName("totalcount", totalCount, "taxon", taxon, "value", data);
            sequence.setID("seq_" + taxon);
            alignment.sequenceInput.setValue(sequence, alignment);
        }
    }
    alignment.setID(ID);
    alignment.initAndValidate();
    return alignment;
}
Also used : InputSource(org.xml.sax.InputSource) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) Sequence(beast.evolution.alignment.Sequence) Document(org.w3c.dom.Document)

Example 19 with Sequence

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

the class FilteredAlignmentTest method getAlignmentNoTInHuman.

public static Alignment getAlignmentNoTInHuman() throws Exception {
    Sequence human = new Sequence("0human", "AAAACCCCGGGGAAAA");
    Sequence chimp = new Sequence("1chimp", "ACGTACGTACGTACGT");
    Alignment data = new Alignment();
    data.initByName("sequence", human, "sequence", chimp, "dataType", "nucleotide");
    return data;
}
Also used : Alignment(beast.evolution.alignment.Alignment) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Sequence(beast.evolution.alignment.Sequence)

Example 20 with Sequence

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

the class FilteredAlignmentTest method getAlignment3.

public static Alignment getAlignment3() throws Exception {
    // reordered from getAlignment() & with duplicates
    Sequence human = new Sequence("0human", "GGGAAA");
    Sequence chimp = new Sequence("1chimp", "AGGACA");
    Alignment data = new Alignment();
    data.initByName("sequence", human, "sequence", chimp, "dataType", "nucleotide");
    return data;
}
Also used : Alignment(beast.evolution.alignment.Alignment) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Sequence(beast.evolution.alignment.Sequence)

Aggregations

Sequence (beast.evolution.alignment.Sequence)34 Alignment (beast.evolution.alignment.Alignment)31 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)6 SiteModel (beast.evolution.sitemodel.SiteModel)6 RealParameter (beast.core.parameter.RealParameter)5 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)5 Tree (beast.evolution.tree.Tree)5 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)4 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)4 TaxonSet (beast.evolution.alignment.TaxonSet)3 DataType (beast.evolution.datatype.DataType)3 Frequencies (beast.evolution.substitutionmodel.Frequencies)3 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)3 TreeParser (beast.util.TreeParser)3 GeneralSubstitutionModel (beast.evolution.substitutionmodel.GeneralSubstitutionModel)2 RandomTree (beast.evolution.tree.RandomTree)2 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)2 TreeIntervals (beast.evolution.tree.coalescent.TreeIntervals)2