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