use of beast.core.Input in project beast2 by CompEvol.
the class Document method checkForOtherPluginShapes.
// addNewShape
void checkForOtherPluginShapes(List<Integer> objects, BEASTObjectShape shape) {
// check whether we need to create any input beastObjects
try {
List<Input<?>> inputs = shape.m_beastObject.listInputs();
for (Input<?> input : inputs) {
if (input.get() instanceof BEASTInterface) {
BEASTInterface beastObject = (BEASTInterface) input.get();
BEASTObjectShape beastObjectShape = new BEASTObjectShape(beastObject, this);
beastObjectShape.m_x = Math.max(shape.m_x - DX, 0);
beastObjectShape.m_y = shape.m_y;
beastObjectShape.m_w = 100;
beastObjectShape.m_h = 80;
setPluginID(beastObjectShape);
m_objects.add(beastObjectShape);
objects.add(m_objects.size() - 1);
Arrow arrow = new Arrow(beastObjectShape, shape, input.getName());
m_objects.add(arrow);
objects.add(m_objects.size() - 1);
// recurse
checkForOtherPluginShapes(objects, beastObjectShape);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of beast.core.Input in project beast2 by CompEvol.
the class Document method recalcArrows.
// isValidModel
/**
* remove all arrows, then add based on the beastObject inputs *
*/
void recalcArrows() {
// remove all arrows
for (int i = m_objects.size() - 1; i >= 0; i--) {
Shape shape = m_objects.get(i);
if (shape instanceof Arrow) {
m_objects.remove(i);
}
}
// build map for quick resolution of PluginShapes
HashMap<BEASTInterface, BEASTObjectShape> map = new HashMap<>();
for (Shape shape : m_objects) {
if (shape instanceof BEASTObjectShape) {
map.put(((BEASTObjectShape) shape).m_beastObject, (BEASTObjectShape) shape);
}
}
// re-insert arrows, if any
for (int i = m_objects.size() - 1; i >= 0; i--) {
Shape shape = m_objects.get(i);
if (shape instanceof BEASTObjectShape) {
BEASTObjectShape headShape = ((BEASTObjectShape) shape);
BEASTInterface beastObject = headShape.m_beastObject;
try {
List<Input<?>> inputs = beastObject.listInputs();
for (Input<?> input : inputs) {
if (input.get() != null) {
if (input.get() instanceof BEASTInterface) {
BEASTObjectShape tailShape = map.get(input.get());
try {
Arrow arrow = new Arrow(tailShape, headShape, input.getName());
arrow.setID(getNewID(null));
m_objects.add(arrow);
} catch (Exception e) {
// ignore, can happen when not all inputs are to be shown
}
}
if (input.get() instanceof List<?>) {
for (Object o : (List<?>) input.get()) {
if (o != null && o instanceof BEASTInterface) {
BEASTObjectShape tailShape = map.get(o);
try {
Arrow arrow = new Arrow(tailShape, headShape, input.getName());
arrow.setID(getNewID(null));
m_objects.add(arrow);
} catch (Exception e) {
// ignore, can happen when not all inputs are to be shown
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// moveArrowsToBack();
}
use of beast.core.Input 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.core.Input in project beast2 by CompEvol.
the class InputTypeTest method testInputTypeCanBeSet.
/*
* Test that the type of an input can be determined, If not, the programmer
* has to manually initialise the type of the input (most easily done
* through a constructor of Input)
*/
@Test
public void testInputTypeCanBeSet() throws Exception {
List<String> beastObjectNames = PackageManager.find(beast.core.BEASTObject.class, PackageManager.IMPLEMENTATION_DIR);
List<String> failingInputs = new ArrayList<String>();
for (String beastObjectName : beastObjectNames) {
try {
BEASTObject beastObject = (BEASTObject) Class.forName(beastObjectName).newInstance();
List<Input<?>> inputs = beastObject.listInputs();
for (Input<?> input : inputs) {
if (input.getType() == null) {
try {
input.determineClass(beastObject);
if (input.getType() == null) {
failingInputs.add(beastObject + ":" + input.getName());
}
} catch (Exception e2) {
failingInputs.add(beastObject + ":" + input.getName());
}
}
}
} catch (Exception e) {
// ignore
}
}
assertTrue("Type of input could not be set for these inputs (probably requires to be set by using the appropriate constructure of Input): " + failingInputs.toString(), failingInputs.size() == 0);
}
use of beast.core.Input 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