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);
}
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();
}
}
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();
}
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();
}
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());
}
}
Aggregations