Search in sources :

Example 1 with NexusParser

use of beast.util.NexusParser in project beast2 by CompEvol.

the class BeautiDoc method parseArgs.

public ActionOnExit parseArgs(String[] args) throws XMLParserException, SAXException, IOException, ParserConfigurationException {
    ActionOnExit endState = ActionOnExit.UNKNOWN;
    String outputFileName = "beast.xml";
    String xml = null;
    String templateXML = null;
    TraitSet traitset = null;
    int i = 0;
    try {
        while (i < args.length) {
            int old = i;
            if (args[i].equals("")) {
                i += 1;
            } else if (args[i].equals("-capture")) {
                // capture stderr and stdout
                // already done in beast.app.beauti.Beauti
                i += 1;
            } else if (args[i].equals("-xml")) {
                String fileName = args[i + 1];
                xml = load(fileName);
                // XMLParser parser = new XMLParser();
                // m_doc.m_mcmc.setValue(parser.parseFile(fileName),
                // m_doc);
                this.fileName = nameFromFile(fileName);
                i += 2;
            } else if (args[i].equals("-template")) {
                String fileName = args[i + 1];
                templateXML = processTemplate(fileName);
                templateFileName = fileName;
                templateName = nameFromFile(fileName);
                i += 2;
            } else if (args[i].equals("-nex")) {
                // NB: multiple -nex/-xmldata commands can be processed!
                String fileName = args[i + 1];
                NexusParser parser = new NexusParser();
                parser.parseFile(new File(fileName));
                if (parser.filteredAlignments.size() > 0) {
                    for (Alignment data : parser.filteredAlignments) {
                        alignments.add(data);
                    }
                } else {
                    alignments.add(parser.m_alignment);
                }
                i += 2;
                traitset = parser.traitSet;
            } else if (args[i].equals("-xmldata")) {
                // NB: multiple -xmldata/-nex commands can be processed!
                String fileName = args[i + 1];
                Alignment alignment = (Alignment) BeautiAlignmentProvider.getXMLData(new File(fileName));
                alignments.add(alignment);
                i += 2;
            } else if (args[i].equals("-exitaction")) {
                if (args[i + 1].equals("writexml")) {
                    endState = ActionOnExit.WRITE_XML;
                } else if (args[i + 1].equals("usetemplate")) {
                    endState = ActionOnExit.SHOW_DETAILS_USE_TEMPLATE;
                } else if (args[i + 1].equals("usexml")) {
                    endState = ActionOnExit.SHOW_DETAILS_USE_XML_SPEC;
                } else if (args[i + 1].equals("merge")) {
                    endState = ActionOnExit.MERGE_AND_WRITE_XML;
                } else {
                    throw new IllegalArgumentException("Expected one of 'writexml','usetemplate' or 'usexml', not " + args[i + 1]);
                }
                i += 2;
            } else if (args[i].equals("-out")) {
                outputFileName = args[i + 1];
                i += 2;
            } else if (args[i].equals("-noerr")) {
                System.setErr(new PrintStream(new OutputStream() {

                    @Override
                    public void write(int b) {
                    }
                }));
                i += 1;
            }
            if (i == old) {
                throw new IllegalArgumentException("Wrong argument: " + args[i]);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
        e.printStackTrace();
        System.exit(1);
    }
    initialize(endState, xml, templateXML, outputFileName);
    addTraitSet(traitset);
    return endState;
}
Also used : PrintStream(java.io.PrintStream) FilteredAlignment(beast.evolution.alignment.FilteredAlignment) Alignment(beast.evolution.alignment.Alignment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TraitSet(beast.evolution.tree.TraitSet) NexusParser(beast.util.NexusParser) File(java.io.File) XMLParserException(beast.util.XMLParserException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with NexusParser

use of beast.util.NexusParser in project beast2 by CompEvol.

the class LogCombinerTest method treeCount.

private int treeCount(String file) throws IOException {
    NexusParser analyser = new NexusParser();
    analyser.parseFile(new File(file));
    return analyser.trees.size();
}
Also used : NexusParser(beast.util.NexusParser) File(java.io.File)

Example 3 with NexusParser

use of beast.util.NexusParser in project beast2 by CompEvol.

the class AlignmentViewer method main.

public static void main(String[] args) {
    try {
        NexusParser parser = new NexusParser();
        parser.parseFile(new File(args[0]));
        Alignment data = parser.m_alignment;
        AlignmentViewer panel = new AlignmentViewer(data);
        panel.showInDialog();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Alignment(beast.evolution.alignment.Alignment) NexusParser(beast.util.NexusParser) File(java.io.File)

Example 4 with NexusParser

use of beast.util.NexusParser in project beast2 by CompEvol.

the class XMLProducerTest method test_ThatNexusExamplesProduces.

// @Test
// public void test_ThatXmlExamplesProduces() {
// System.setProperty("java.only", "true");
// String dir = System.getProperty("user.dir") + "/examples";
// //String dir = "/tmp";
// List<String> exceptions = new ArrayList<String>();
// exceptions.add("testExponentialGrowth.xml");
// test_ThatXmlExamplesProduces(dir, exceptions);
// }
@Test
public void test_ThatNexusExamplesProduces() {
    try {
        String dirName = System.getProperty("user.dir") + "/examples/nexus";
        System.out.println("Test Nexus Examples in " + dirName);
        File exampleDir = new File(dirName);
        String[] exampleFiles = exampleDir.list(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".nex") || name.endsWith(".nxs");
            }
        });
        List<String> failedFiles = new ArrayList<>();
        for (String fileName : exampleFiles) {
            System.out.println("Processing " + fileName);
            NexusParser parser = new NexusParser();
            try {
                parser.parseFile(new File(dirName + "/" + fileName));
            } catch (Exception e) {
                System.out.println("ExampleNexusParsing::Failed for " + fileName + ": " + e.getMessage());
                failedFiles.add(fileName);
            }
            XMLProducer producer = new XMLProducer();
            BEASTInterface o = parser.m_alignment;
            if (o != null) {
                String xml = producer.toXML(o);
                // FileWriter outfile = new FileWriter(new File("/tmp/XMLProducerTest.xml"));
                // outfile.write(xml);
                // outfile.close();
                XMLParser parser2 = new XMLParser();
                try {
                    BEASTInterface o2 = parser2.parseFragment(xml, false);
                    System.out.println(o2);
                } catch (Exception e) {
                    System.out.println("test_ThatNexusExamplesProduces::Failed for " + fileName + ": " + e.getMessage());
                    failedFiles.add(fileName);
                }
            }
            System.out.println("Done " + fileName);
        }
        if (failedFiles.size() > 0) {
            System.out.println("\test_ThatNexusExamplesProduces::Failed for : " + failedFiles.toString());
        } else {
            System.out.println("\test_ThatNexusExamplesProduces::Success");
        }
        assertTrue(failedFiles.toString(), failedFiles.size() == 0);
    } catch (Exception e) {
        System.out.println("exception thrown ");
        System.out.println(e.getMessage());
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) XMLProducer(beast.util.XMLProducer) ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) NexusParser(beast.util.NexusParser) XMLParser(beast.util.XMLParser) File(java.io.File) Test(org.junit.Test)

Example 5 with NexusParser

use of beast.util.NexusParser in project beast2 by CompEvol.

the class NexusParserTest method testTranslateBlock.

@Test
public void testTranslateBlock() {
    String nexusTreeWithTranslateBlock = "#NEXUS\n" + "\n" + "Begin trees;\n" + "\tTranslate\n" + "\t\t1 ID1,\n" + "\t\t2 ID0,\n" + "\t\t3 ID4,\n" + "\t\t4 ID2,\n" + "\t\t5 ID3,\n" + "\t\t6 ID5,\n" + "\t\t7 ID6\n" + "\t\t;\n" + "tree TREE1  = [&R] (((1[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=3.885354014841496E-17,height_range={0.0,8.881784197001252E-16}]:0.3500782084890231,2[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=3.885336674542856E-17,height_range={0.0,8.881784197001252E-16}]:0.3500782084890231)[&height_95%_HPD={0.07176417767165222,0.32897302210795143},height_median=0.1721780016900536,height=0.18167779468916231,posterior=0.9992003198720512,height_range={0.019500497296338903,0.472878180264112}]:0.9248218821607495,(3[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=3.9830145767837334E-17,height_range={0.0,4.440892098500626E-16}]:0.9953937953591572,(4[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=4.332577657073782E-17,height_range={0.0,8.881784197001252E-16}]:0.2885313407858645,5[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=4.332594997372422E-17,height_range={0.0,8.881784197001252E-16}]:0.2885313407858645)[&height_95%_HPD={0.03428997737185302,0.29384644242980307},height_median=0.13917660355071027,height=0.15063132242919447,posterior=0.9992003198720512,height_range={0.012135234424637736,0.43470259067266337}]:0.7068624545732927)[&height_95%_HPD={0.4703860181701143,1.2401041356897795},height_median=0.8841915264598103,height=0.8799641568544527,posterior=0.9668132746901239,height_range={0.2376833426394811,1.5008735971013971}]:0.2795062952906153)[&height_95%_HPD={0.871170115451148,1.736574411464855},height_median=1.3388341576137588,height=1.3419155689487607,posterior=0.8912435025989605,height_range={0.609339147487161,2.0082114277758554}]:0.1115505866359714,(6[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=3.805449918706939E-17,height_range={0.0,4.440892098500626E-16}]:0.1143648180955704,7[&height_95%_HPD={0.0,2.220446049250313E-16},height_median=0.0,height=3.805449918706939E-17,height_range={0.0,4.440892098500626E-16}]:0.1143648180955704)[&height_95%_HPD={0.07189918035503462,0.3145415291508802},height_median=0.1807116511737813,height=0.18641149700061227,posterior=0.9996001599360256,height_range={0.0042543872372436675,0.4254547656747345}]:1.2720858591901736)[&height_95%_HPD={1.2498874674331548,2.2880779508436415},height_median=1.7581890486365315,height=1.7391129102773757,posterior=1.0,height_range={0.003684245207410762,2.42575160443681}];\n" + "End;\n";
    NexusParser parser = new NexusParser();
    try {
        Set<String> taxa = new TreeSet<>();
        taxa.add("ID0");
        taxa.add("ID1");
        taxa.add("ID2");
        taxa.add("ID3");
        taxa.add("ID4");
        taxa.add("ID5");
        taxa.add("ID6");
        parser.parseFile("testTranslateBlock", new StringReader(nexusTreeWithTranslateBlock));
        assertEquals(1, parser.trees.size());
        assertNotNull(parser.trees.get(0));
        assertEquals(7, parser.trees.get(0).getTaxaNames().length);
        for (String taxaName : parser.trees.get(0).getTaxaNames()) {
            assertNotNull(taxaName);
        }
        assertTrue(taxa.containsAll(Arrays.asList(parser.trees.get(0).getTaxaNames())));
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : TreeSet(java.util.TreeSet) StringReader(java.io.StringReader) NexusParser(beast.util.NexusParser) Test(org.junit.Test)

Aggregations

NexusParser (beast.util.NexusParser)9 File (java.io.File)6 Test (org.junit.Test)5 Alignment (beast.evolution.alignment.Alignment)3 ArrayList (java.util.ArrayList)3 FilteredAlignment (beast.evolution.alignment.FilteredAlignment)2 FilenameFilter (java.io.FilenameFilter)2 StringReader (java.io.StringReader)2 BEASTInterface (beast.core.BEASTInterface)1 TraitSet (beast.evolution.tree.TraitSet)1 XMLParser (beast.util.XMLParser)1 XMLParserException (beast.util.XMLParserException)1 XMLProducer (beast.util.XMLProducer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 TreeSet (java.util.TreeSet)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1