Search in sources :

Example 6 with LogFileTraces

use of dr.inference.trace.LogFileTraces in project beast-mcmc by beast-dev.

the class MapperFrame method importLocationFiles.

void importLocationFiles(File[] files) {
    LogFileTraces[] traces = new LogFileTraces[files.length];
    for (int i = 0; i < files.length; i++) {
        traces[i] = new LogFileTraces(files[i].getName(), files[i]);
    }
    processTraces(traces);
}
Also used : LogFileTraces(dr.inference.trace.LogFileTraces)

Example 7 with LogFileTraces

use of dr.inference.trace.LogFileTraces in project beast-mcmc by beast-dev.

the class MapperFrame method processTraces.

protected void processTraces(final LogFileTraces[] tracesArray) {
    final JFrame frame = this;
    if (tracesArray.length == 1) {
        try {
            final LogFileTraces traces = tracesArray[0];
            final String fileName = traces.getName();
            final ProgressMonitorInputStream in = new ProgressMonitorInputStream(this, "Reading " + fileName, new FileInputStream(traces.getFile()));
            in.getProgressMonitor().setMillisToDecideToPopup(0);
            in.getProgressMonitor().setMillisToPopup(0);
            //                final Reader reader = new InputStreamReader(in);
            Thread readThread = new Thread() {

                public void run() {
                    try {
                        traces.loadTraces(in);
                        EventQueue.invokeLater(new Runnable() {

                            public void run() {
                            //                                            analyseTraceList(traces);
                            //                                            addTraceList(traces);
                            }
                        });
                    } catch (final TraceException te) {
                        EventQueue.invokeLater(new Runnable() {

                            public void run() {
                                JOptionPane.showMessageDialog(frame, "Problem with trace file: " + te.getMessage(), "Problem with tree file", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                    } catch (final InterruptedIOException iioex) {
                    // The cancel dialog button was pressed - do nothing
                    } catch (final IOException ioex) {
                        EventQueue.invokeLater(new Runnable() {

                            public void run() {
                                JOptionPane.showMessageDialog(frame, "File I/O Error: " + ioex.getMessage(), "File I/O Error", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                    //                    } catch (final Exception ex) {
                    //                        EventQueue.invokeLater (
                    //                                new Runnable () {
                    //                                    public void run () {
                    //                                        JOptionPane.showMessageDialog(frame, "Fatal exception: " + ex.getMessage(),
                    //                                                "Error reading file",
                    //                                                JOptionPane.ERROR_MESSAGE);
                    //                                    }
                    //                                });
                    }
                }
            };
            readThread.start();
        } catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(this, "Unable to open file: File not found", "Unable to open file", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ioex) {
            JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex, "File I/O Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, "Error reading file", JOptionPane.ERROR_MESSAGE);
        }
    } else {
        Thread readThread = new Thread() {

            public void run() {
                try {
                    for (final LogFileTraces traces : tracesArray) {
                        //                            final Reader reader = new FileReader(traces.getFile());
                        traces.loadTraces();
                        EventQueue.invokeLater(new Runnable() {

                            public void run() {
                            //                                            analyseTraceList(traces);
                            //                                            addTraceList(traces);
                            }
                        });
                    }
                } catch (final TraceException te) {
                    EventQueue.invokeLater(new Runnable() {

                        public void run() {
                            JOptionPane.showMessageDialog(frame, "Problem with trace file: " + te.getMessage(), "Problem with tree file", JOptionPane.ERROR_MESSAGE);
                        }
                    });
                } catch (final InterruptedIOException iioex) {
                // The cancel dialog button was pressed - do nothing
                } catch (final IOException ioex) {
                    EventQueue.invokeLater(new Runnable() {

                        public void run() {
                            JOptionPane.showMessageDialog(frame, "File I/O Error: " + ioex.getMessage(), "File I/O Error", JOptionPane.ERROR_MESSAGE);
                        }
                    });
                //                    } catch (final Exception ex) {
                //                        EventQueue.invokeLater (
                //                                new Runnable () {
                //                                    public void run () {
                //                                        JOptionPane.showMessageDialog(frame, "Fatal exception: " + ex.getMessage(),
                //                                                "Error reading file",
                //                                                JOptionPane.ERROR_MESSAGE);
                //                                    }
                //                                });
                }
            }
        };
        readThread.start();
    }
}
Also used : TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) TraceException(dr.inference.trace.TraceException)

Example 8 with LogFileTraces

use of dr.inference.trace.LogFileTraces in project beast-mcmc by beast-dev.

the class LocationsPanel method setBurnIn.

public void setBurnIn(int index, int burnIn) {
    LogFileTraces trace = traceLists.get(index);
    trace.setBurnIn(burnIn);
    analyseTraceList(trace);
    updateTraceTables();
}
Also used : LogFileTraces(dr.inference.trace.LogFileTraces)

Example 9 with LogFileTraces

use of dr.inference.trace.LogFileTraces in project beast-mcmc by beast-dev.

the class CoalGenApp method simulate.

public static void simulate(String inputFileName, String treesFileName, String outputFileName) throws IOException, TraceException, Importer.ImportException {
    File logFile = new File(inputFileName);
    System.out.println("Loading trace file: " + inputFileName);
    LogFileTraces traces = new LogFileTraces(inputFileName, logFile);
    traces.loadTraces();
    traces.setBurnIn(0);
    System.out.println(traces.getStateCount() + " states loaded");
    System.out.println();
    System.out.println("Opening trees file: " + treesFileName);
    //        BufferedReader reader = new BufferedReader(new FileReader(treesFileName));
    System.out.println("Simulating...");
    System.out.println("0              25             50             75            100");
    System.out.println("|--------------|--------------|--------------|--------------|");
    int stepSize = traces.getStateCount() / 60;
    if (stepSize < 1)
        stepSize = 1;
    PrintWriter writer = new PrintWriter(new FileWriter(outputFileName));
    FileReader fileReader = new FileReader(treesFileName);
    TreeImporter importer = new NexusImporter(fileReader);
    EmpiricalDemographicFunction demo = null;
    IntervalGenerator intervals = null;
    TreeSimulator sim = null;
    CoalGenData data = new CoalGenData();
    data.traces = traces;
    // const stepwise bsp
    data.setDemographicModel(7);
    data.setupSkyline();
    double[] popSizes = new double[data.popSizeCount];
    double[] groupSizes = new double[data.groupSizeCount];
    NewickExporter exporter = new NewickExporter(writer);
    int count = 0;
    try {
        while (importer.hasTree()) {
            RootedTree inTree = (RootedTree) importer.importNextTree();
            if (sim == null) {
                setSamplingTimes(inTree);
                sim = new TreeSimulator(inTree.getTaxa(), "date");
            }
            data.getNextSkyline(popSizes, groupSizes);
            double[] times = getTimes(inTree, groupSizes);
            demo = new EmpiricalDemographicFunction(popSizes, times, true);
            intervals = new CoalescentIntervalGenerator(demo);
            RootedTree outTree = sim.simulate(intervals);
            exporter.exportTree(outTree);
            writer.println();
            writer.flush();
            if (count > 0 && count % stepSize == 0) {
                System.out.print("*");
                System.out.flush();
            }
            count++;
        }
    } catch (ImportException e) {
        e.printStackTrace();
    }
    fileReader.close();
    writer.close();
}
Also used : TreeSimulator(jebl.evolution.treesimulation.TreeSimulator) NexusImporter(jebl.evolution.io.NexusImporter) NewickExporter(jebl.evolution.io.NewickExporter) ImportException(jebl.evolution.io.ImportException) CoalescentIntervalGenerator(jebl.evolution.treesimulation.CoalescentIntervalGenerator) RootedTree(jebl.evolution.trees.RootedTree) EmpiricalDemographicFunction(jebl.evolution.coalescent.EmpiricalDemographicFunction) LogFileTraces(dr.inference.trace.LogFileTraces) TreeImporter(jebl.evolution.io.TreeImporter) IntervalGenerator(jebl.evolution.treesimulation.IntervalGenerator) CoalescentIntervalGenerator(jebl.evolution.treesimulation.CoalescentIntervalGenerator)

Example 10 with LogFileTraces

use of dr.inference.trace.LogFileTraces in project beast-mcmc by beast-dev.

the class MarginalLikelihoodAnalysisParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String fileName = xo.getStringAttribute(FILE_NAME);
    try {
        File file = new File(fileName);
        String name = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, name);
        fileName = file.getAbsolutePath();
        XMLObject cxo = xo.getChild(COLUMN_NAME);
        String likelihoodName = cxo.getStringAttribute(Attribute.NAME);
        LogFileTraces traces = new LogFileTraces(fileName, file);
        traces.loadTraces();
        long maxState = traces.getMaxState();
        // leaving the burnin attribute off will result in 10% being used
        long burnin = xo.getAttribute(BURN_IN, maxState / 10);
        if (burnin < 0 || burnin >= maxState) {
            burnin = maxState / 10;
            System.out.println("WARNING: Burn-in larger than total number of states - using to 10%");
        }
        traces.setBurnIn(burnin);
        int traceIndex = -1;
        for (int i = 0; i < traces.getTraceCount(); i++) {
            String traceName = traces.getTraceName(i);
            if (traceName.equals(likelihoodName)) {
                traceIndex = i;
                break;
            }
        }
        if (traceIndex == -1) {
            throw new XMLParseException("Column '" + likelihoodName + "' can not be found for " + getParserName() + " element.");
        }
        // Set analysisType
        String analysisType = "smoothed";
        boolean harmonicOnly = false;
        if (xo.hasAttribute(ONLY_HARMONIC))
            harmonicOnly = xo.getBooleanAttribute(ONLY_HARMONIC);
        if (harmonicOnly)
            analysisType = "harmonic";
        int bootstrapLength = cxo.getAttribute(BOOTSTRAP_LENGTH, 1000);
        List<Double> sample = traces.getValues(traceIndex);
        MarginalLikelihoodAnalysis analysis = new MarginalLikelihoodAnalysis(sample, traces.getTraceName(traceIndex), (int) burnin, analysisType, bootstrapLength);
        System.out.println(analysis.toString());
        return analysis;
    } catch (FileNotFoundException fnfe) {
        throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element.");
    } catch (java.io.IOException ioe) {
        throw new XMLParseException(ioe.getMessage());
    } catch (TraceException e) {
        throw new XMLParseException(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) TraceException(dr.inference.trace.TraceException) LogFileTraces(dr.inference.trace.LogFileTraces) MarginalLikelihoodAnalysis(dr.inference.trace.MarginalLikelihoodAnalysis) File(java.io.File)

Aggregations

LogFileTraces (dr.inference.trace.LogFileTraces)11 TraceException (dr.inference.trace.TraceException)7 File (java.io.File)5 MarginalLikelihoodAnalysis (dr.inference.trace.MarginalLikelihoodAnalysis)4 FileNotFoundException (java.io.FileNotFoundException)4 Distribution (dr.math.distributions.Distribution)1 XMLObject (dr.xml.XMLObject)1 XMLParseException (dr.xml.XMLParseException)1 IOException (java.io.IOException)1 List (java.util.List)1 EmpiricalDemographicFunction (jebl.evolution.coalescent.EmpiricalDemographicFunction)1 ImportException (jebl.evolution.io.ImportException)1 NewickExporter (jebl.evolution.io.NewickExporter)1 NexusImporter (jebl.evolution.io.NexusImporter)1 TreeImporter (jebl.evolution.io.TreeImporter)1 RootedTree (jebl.evolution.trees.RootedTree)1 CoalescentIntervalGenerator (jebl.evolution.treesimulation.CoalescentIntervalGenerator)1 IntervalGenerator (jebl.evolution.treesimulation.IntervalGenerator)1 TreeSimulator (jebl.evolution.treesimulation.TreeSimulator)1