Search in sources :

Example 16 with Input

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();
    }
}
Also used : Input(beast.core.Input) BEASTInterface(beast.core.BEASTInterface)

Example 17 with Input

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();
}
Also used : HashMap(java.util.HashMap) Input(beast.core.Input) BEASTInterface(beast.core.BEASTInterface) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Input

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();
    }
}
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 19 with Input

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);
}
Also used : Input(beast.core.Input) ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject) Test(org.junit.Test)

Example 20 with Input

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);
}
Also used : ArrayList(java.util.ArrayList) BEASTObject(beast.core.BEASTObject) Input(beast.core.Input) XMLParser(beast.util.XMLParser) Test(org.junit.Test)

Aggregations

Input (beast.core.Input)20 BEASTInterface (beast.core.BEASTInterface)12 ArrayList (java.util.ArrayList)11 List (java.util.List)7 BEASTObject (beast.core.BEASTObject)6 Test (org.junit.Test)4 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)3 SiteModel (beast.evolution.sitemodel.SiteModel)3 InputEditor (beast.app.draw.InputEditor)2 MCMC (beast.core.MCMC)2 GenericTreeLikelihood (beast.evolution.likelihood.GenericTreeLikelihood)2 SiteModelInterface (beast.evolution.sitemodel.SiteModelInterface)2 TreeInterface (beast.evolution.tree.TreeInterface)2 MRCAPrior (beast.math.distributions.MRCAPrior)2 XMLParser (beast.util.XMLParser)2 Dimension (java.awt.Dimension)2 FileNotFoundException (java.io.FileNotFoundException)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 JComboBox (javax.swing.JComboBox)2