Search in sources :

Example 81 with Alignment

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

the class SequenceSimulator method main.

// printUsageAndExit
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {
        // parse arguments
        if (args.length < 2) {
            printUsageAndExit();
        }
        String fileName = args[0];
        int replications = Integer.parseInt(args[1]);
        PrintStream out = System.out;
        if (args.length == 3) {
            File file = new File(args[2]);
            out = new PrintStream(file);
        }
        // grab the file
        String xml = "";
        BufferedReader fin = new BufferedReader(new FileReader(fileName));
        while (fin.ready()) {
            xml += fin.readLine();
        }
        fin.close();
        // parse the xml
        XMLParser parser = new XMLParser();
        BEASTInterface beastObject = parser.parseFragment(xml, true);
        // find relevant objects from the model
        TreeLikelihood treeLikelihood = getTreeLikelihood(beastObject);
        if (treeLikelihood == null) {
            throw new IllegalArgumentException("No treelikelihood found in file. Giving up now.");
        }
        Alignment data = ((Input<Alignment>) treeLikelihood.getInput("data")).get();
        Tree tree = ((Input<Tree>) treeLikelihood.getInput("tree")).get();
        SiteModel pSiteModel = ((Input<SiteModel>) treeLikelihood.getInput("siteModel")).get();
        BranchRateModel pBranchRateModel = ((Input<BranchRateModel>) treeLikelihood.getInput("branchRateModel")).get();
        // feed to sequence simulator and generate leaves
        SequenceSimulator treeSimulator = new SequenceSimulator();
        treeSimulator.init(data, tree, pSiteModel, pBranchRateModel, replications);
        XMLProducer producer = new XMLProducer();
        Alignment alignment = treeSimulator.simulate();
        xml = producer.toRawXML(alignment);
        out.println("<beast version='2.0'>");
        out.println(xml);
        out.println("</beast>");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) XMLProducer(beast.util.XMLProducer) TreeLikelihood(beast.evolution.likelihood.TreeLikelihood) SiteModel(beast.evolution.sitemodel.SiteModel) XMLParserException(beast.util.XMLParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Alignment(beast.evolution.alignment.Alignment) Input(beast.core.Input) BranchRateModel(beast.evolution.branchratemodel.BranchRateModel) BufferedReader(java.io.BufferedReader) Tree(beast.evolution.tree.Tree) FileReader(java.io.FileReader) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 82 with Alignment

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

the class SequenceSimulator method run.

@Override
public void run() throws IllegalArgumentException, IllegalAccessException, IOException, XMLParserException {
    for (int i = 0; i < iterationsInput.get(); i++) {
        Alignment alignment = simulate();
        // Write output to stdout or file
        PrintStream pstream;
        if (m_outputFileName == null)
            pstream = System.out;
        else
            pstream = new PrintStream(m_outputFileName);
        pstream.println(new XMLProducer().toRawXML(alignment));
        for (MergeDataWith merge : mergeListInput.get()) {
            merge.process(alignment, i);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Alignment(beast.evolution.alignment.Alignment) XMLProducer(beast.util.XMLProducer)

Example 83 with Alignment

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

the class MergeDataWith method process.

// initAndValidate
void process(Alignment data, int iteration) throws IOException, XMLParserException, IllegalArgumentException, IllegalAccessException {
    // read template
    String templateXML = BeautiDoc.load(templateFile);
    templateXML = templateXML.replaceAll("\\$\\(n\\)", iteration + "");
    XMLParser parser = new XMLParser();
    BEASTInterface b = parser.parseBareFragment(templateXML, false);
    // repalce alignment
    Alignment a = getAlignment(b);
    List<Sequence> sequences = a.sequenceInput.get();
    sequences.clear();
    sequences.addAll(data.sequenceInput.get());
    // write file
    String outputFile = outputFileInput.get();
    outputFile = outputFile.replaceAll("\\$\\(n\\)", iteration + "");
    FileWriter outfile = new FileWriter(outputFile);
    Set<BEASTInterface> beastObjects = new HashSet<>();
    String xml = new XMLProducer().toXML(b, beastObjects);
    outfile.write(xml);
    outfile.close();
}
Also used : Alignment(beast.evolution.alignment.Alignment) XMLProducer(beast.util.XMLProducer) FileWriter(java.io.FileWriter) BEASTInterface(beast.core.BEASTInterface) Sequence(beast.evolution.alignment.Sequence) XMLParser(beast.util.XMLParser) HashSet(java.util.HashSet)

Example 84 with Alignment

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

the class Utils method main.

public static void main(String[] args) {
    try {
        Sequence a = new Sequence("A", "A");
        Sequence b = new Sequence("B", "A");
        Sequence c = new Sequence("C", "A");
        Sequence d = new Sequence("D", "A");
        Alignment data = new Alignment();
        data.initByName("sequence", a, "sequence", b, "sequence", c, "sequence", d, "dataType", "nucleotide");
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", "(((A:1,B:1):1,C:2):1,D:3)", "IsLabelledNewick", true);
        JukesCantor JC = new JukesCantor();
        JC.initAndValidate();
        SiteModel siteModel = new SiteModel();
        siteModel.initByName("mutationRate", "1.0", "gammaCategoryCount", 1, "substModel", JC);
        BeagleTreeLikelihood likelihood = new BeagleTreeLikelihood();
        likelihood.initByName("data", data, "tree", tree, "siteModel", siteModel);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Success");
    // if we got this far, exit with status 0
    System.exit(0);
}
Also used : BeagleTreeLikelihood(beast.evolution.likelihood.BeagleTreeLikelihood) Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) IOException(java.io.IOException)

Example 85 with Alignment

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

the class BeagleTreeLikelihood method setPartials.

/**
 * Sets the partials from a sequence in an alignment.
 *
 * @param beagle        beagle
 * @param nodeIndex     nodeIndex
 * @param taxon the taxon
 */
protected final void setPartials(Beagle beagle, int nodeIndex, int taxon) {
    Alignment data = dataInput.get();
    double[] partials = new double[patternCount * m_nStateCount * categoryCount];
    int v = 0;
    for (int i = 0; i < patternCount; i++) {
        double[] tipProbabilities = data.getTipLikelihoods(taxon, i);
        if (tipProbabilities != null) {
            for (int state = 0; state < m_nStateCount; state++) {
                partials[v++] = tipProbabilities[state];
            }
        } else {
            int stateCount = data.getPattern(taxon, i);
            boolean[] stateSet = data.getStateSet(stateCount);
            for (int state = 0; state < m_nStateCount; state++) {
                partials[v++] = (stateSet[state] ? 1.0 : 0.0);
            }
        }
    }
    // if there is more than one category then replicate the partials for each
    int n = patternCount * m_nStateCount;
    int k = n;
    for (int i = 1; i < categoryCount; i++) {
        System.arraycopy(partials, 0, partials, k, n);
        k += n;
    }
    beagle.setPartials(nodeIndex, partials);
}
Also used : Alignment(beast.evolution.alignment.Alignment)

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