Search in sources :

Example 6 with XMLProducer

use of beast.util.XMLProducer in project beast2 by CompEvol.

the class Logger method init.

/**
 * initialise log, open file (if necessary) and produce header of log
 */
public void init() throws IOException {
    final boolean needsHeader = openLogFile();
    if (needsHeader) {
        if (modelInput.get() != null) {
            // print model at top of log
            String xml = new XMLProducer().modelToXML(modelInput.get());
            xml = "#" + xml.replaceAll("\\n", "\n#");
            m_out.println("#\n#model:\n#");
            m_out.println(xml);
            m_out.println("#");
        }
        ByteArrayOutputStream baos = null;
        PrintStream tmp = null;
        if (m_out == System.out) {
            tmp = m_out;
            baos = new ByteArrayOutputStream();
            m_out = new PrintStream(baos);
        }
        final ByteArrayOutputStream rawbaos = new ByteArrayOutputStream();
        final PrintStream out = new PrintStream(rawbaos);
        if (mode == LOGMODE.compound) {
            out.print("Sample\t");
        }
        for (final Loggable m_logger : loggerList) {
            m_logger.init(out);
        }
        // Remove trailing tab from header
        String header = rawbaos.toString().trim();
        if (sanitiseHeadersInput.get()) {
            m_out.print(sanitiseHeader(header));
        } else {
            m_out.print(header);
        }
        if (baos != null) {
            assert tmp == System.out;
            m_out = tmp;
            try {
                String logContent = baos.toString("ASCII");
                logContent = prettifyLogLine(logContent);
                m_out.print(logContent);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        m_out.println();
    }
}
Also used : PrintStream(java.io.PrintStream) XMLProducer(beast.util.XMLProducer) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with XMLProducer

use of beast.util.XMLProducer 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 8 with XMLProducer

use of beast.util.XMLProducer 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 9 with XMLProducer

use of beast.util.XMLProducer 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 10 with XMLProducer

use of beast.util.XMLProducer in project beast2 by CompEvol.

the class SimulatedAlignment method initAndValidate.

@Override
public void initAndValidate() {
    m_tree = m_treeInput.get();
    m_siteModel = m_pSiteModelInput.get();
    m_branchRateModel = m_pBranchRateModelInput.get();
    m_sequenceLength = m_sequenceLengthInput.get();
    m_stateCount = m_data.get().getMaxStateCount();
    m_categoryCount = m_siteModel.getCategoryCount();
    m_probabilities = new double[m_categoryCount][m_stateCount * m_stateCount];
    m_outputFileName = m_outputFileNameInput.get();
    sequenceInput.get().clear();
    simulate();
    // Write simulated alignment to disk if requested:
    if (m_outputFileName != null) {
        PrintStream pstream;
        try {
            pstream = new PrintStream(m_outputFileName);
            pstream.println(new XMLProducer().toRawXML(this));
            pstream.close();
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }
    super.initAndValidate();
}
Also used : PrintStream(java.io.PrintStream) XMLProducer(beast.util.XMLProducer) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

XMLProducer (beast.util.XMLProducer)13 BEASTInterface (beast.core.BEASTInterface)9 File (java.io.File)6 XMLParser (beast.util.XMLParser)5 PrintStream (java.io.PrintStream)4 Alignment (beast.evolution.alignment.Alignment)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MCMC (beast.core.MCMC)2 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 FilenameFilter (java.io.FilenameFilter)2 HashSet (java.util.HashSet)2 Scanner (java.util.Scanner)2 Input (beast.core.Input)1 Sequence (beast.evolution.alignment.Sequence)1 Taxon (beast.evolution.alignment.Taxon)1 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)1 Nucleotide (beast.evolution.datatype.Nucleotide)1 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)1