use of beast.util.XMLParser in project beast2 by CompEvol.
the class Document method reinit.
void reinit() {
String xml = toXML();
m_objects.clear();
try {
XMLParser parser = new XMLParser();
BEASTInterface plugin0 = parser.parseBareFragment(xml, false);
init(plugin0);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
use of beast.util.XMLParser in project beast2 by CompEvol.
the class Document method loadFile.
public void loadFile(String fileName) {
m_objects.clear();
XMLParser parser = new XMLParser();
try {
// fileName;
StringBuilder xml = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new File(fileName));
try {
while (scanner.hasNextLine()) {
xml.append(scanner.nextLine() + NL);
}
} finally {
scanner.close();
}
BEASTInterface plugin0 = parser.parseBareFragment(xml.toString(), false);
init(plugin0);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
use of beast.util.XMLParser 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.XMLParser 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.XMLParser in project beast2 by CompEvol.
the class XMLElementNameTest method test_ReservedElementNames.
/**
* test that Inputs that use reserved names have the correct type *
*/
@Test
public void test_ReservedElementNames() {
// retrieve list of reserved names and their classes
XMLParser parser = new XMLParser();
HashMap<String, String> element2ClassMap = parser.getElement2ClassMap();
// allow 'parameter' for any of the various parameter derivatives, not just RealParameter
element2ClassMap.put("parameter", "beast.core.parameter.Parameter");
// check each beastObject
List<String> pluginNames = PackageManager.find(beast.core.BEASTObject.class, PackageManager.IMPLEMENTATION_DIR);
List<String> improperInputs = new ArrayList<String>();
for (String beastObjectName : pluginNames) {
try {
BEASTObject beastObject = (BEASTObject) Class.forName(beastObjectName).newInstance();
// check each input
List<Input<?>> inputs = beastObject.listInputs();
for (Input<?> input : inputs) {
if (element2ClassMap.containsKey(input.getName())) {
if (beastObject.getClass() == null) {
input.determineClass(beastObject);
}
Class<?> type = input.getType();
String baseType = element2ClassMap.get(input.getName());
if (!isDerivedType(type, baseType)) {
improperInputs.add(beastObjectName + "." + input.getName());
}
}
}
} catch (InstantiationException e) {
// ignore
} catch (Exception e) {
// ignore
}
}
if (improperInputs.size() > 0) {
String str = improperInputs.toString();
str = str.replaceAll(",", "\n");
System.err.println("Reserved element names used for wrong types in:\n" + str);
}
// not activated till problem with naming is solved
assertTrue("Reserved element names used for wrong types in: " + improperInputs.toString(), improperInputs.size() == 0);
}
Aggregations