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