Search in sources :

Example 36 with XMLParser

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

Example 37 with XMLParser

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
    }
}
Also used : Scanner(java.util.Scanner) BEASTInterface(beast.core.BEASTInterface) XMLParser(beast.util.XMLParser) File(java.io.File)

Example 38 with XMLParser

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();
    }
}
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 39 with XMLParser

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();
}
Also used : Alignment(beast.evolution.alignment.Alignment) XMLProducer(beast.util.XMLProducer) FileWriter(java.io.FileWriter) BEASTInterface(beast.core.BEASTInterface) Sequence(beast.evolution.alignment.Sequence) XMLParser(beast.util.XMLParser) HashSet(java.util.HashSet)

Example 40 with XMLParser

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

Aggregations

XMLParser (beast.util.XMLParser)46 File (java.io.File)36 ArrayList (java.util.ArrayList)29 Test (org.junit.Test)27 Expectation (test.beast.beast2vs1.trace.Expectation)23 LogAnalyser (test.beast.beast2vs1.trace.LogAnalyser)23 BEASTInterface (beast.core.BEASTInterface)11 XMLProducer (beast.util.XMLProducer)5 IOException (java.io.IOException)5 FilenameFilter (java.io.FilenameFilter)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 SAXException (org.xml.sax.SAXException)4 MCMC (beast.core.MCMC)3 Alignment (beast.evolution.alignment.Alignment)3 XMLParserException (beast.util.XMLParserException)3 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 Input (beast.core.Input)2 StateNode (beast.core.StateNode)2 CompoundDistribution (beast.core.util.CompoundDistribution)2