Skip to content

Commit

Permalink
add model analyser app -- introduce dependency on BEASTlabs
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouckaert committed Mar 28, 2016
1 parent 37f07e3 commit a9d94ff
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
4 changes: 3 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
<property name="libbModelTest" location="lib" />
<property name="release_dir" value="release" />
<property name="distbModelTest" location="${buildbModelTest}/dist" />
<property name="BEASTLabsclasspath" location="../BEASTLabs/build" />
<property name="beast2path" location="../beast2" />
<property name="libBeast2" location="${beast2path}/lib" />
<property name="srcBeast2" location="${beast2path}/src" />
<property name="beast2classpath" location="${beast2path}/build" />
<property name="Add_on_dir" value="${release_dir}/add-on" />
<property name="version" value="0.2.0" />
<property name="version" value="0.3.0" />

<import file="${beast2path}/build.xml" />

Expand All @@ -34,6 +35,7 @@
<fileset dir="${libBeast2}" includes="jam.jar"/>
<fileset dir="${libBeast2}" includes="colt.jar"/>
<pathelement path="${beast2classpath}"/>
<pathelement path="${BEASTLabsclasspath}"/>
</path>

<!-- start -->
Expand Down
139 changes: 139 additions & 0 deletions src/beast/app/tools/BModelAnalyser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package beast.app.tools;

import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import beast.app.beauti.BeautiConfig;
import beast.app.beauti.BeautiDoc;
import beast.app.draw.BEASTObjectDialog;
import beast.app.draw.BEASTObjectPanel;
import beast.app.util.Application;
import beast.app.util.ConsoleApp;
import beast.app.util.LogFile;
import beast.core.Description;
import beast.core.Input;
import beast.core.Input.Validate;
import beast.core.Runnable;
import beast.util.LogAnalyser;

@Description("Analyses bModelTest log and list useful stats such as 95% HPDs of model indicators")
public class BModelAnalyser extends Runnable {
public Input<LogFile> traceFileInput = new Input<>("file","trace log file containing output of a bModelTest analysis", Validate.REQUIRED);
public Input<String> prefixInput = new Input<>("prefix", "prefix of the entry in the log file containing the substitution model trace (default 'substmodel')" , "substmodel");
public Input<Integer> burninInput = new Input<>("burnin", "percentage of the log file to disregard as burn-in (default 10)" , 10);


@Override
public void initAndValidate() {
int burnin = burninInput.get();
if (burnin >= 100) {
throw new IllegalArgumentException("burnin is a percentage and should not be larger than 100");
}

}

@Override
public void run() throws Exception {
File file = traceFileInput.get();
String prefix = prefixInput.get();
int burnin = burninInput.get();
if (burnin < 0) {
burnin = 0;
}

LogAnalyser analyser = new LogAnalyser(file.getAbsolutePath(), burnin);
for (String label : analyser.getLabels()) {
if (label.startsWith(prefix)) {
Double [] trace = analyser.getTrace(label);
processTrace(label, trace);
}
}
}

private void processTrace(String label, Double[] trace) {
System.out.println(label);
int [] model = new int[trace.length];
for (int i = 0; i < trace.length; i++) {
model[i] = (int) (double) (trace[i] + 0.5);
}

Map<Integer, Integer> countMap = new HashMap<>();
for (int i : model) {
if (!countMap.containsKey(i)) {
countMap.put(i, 0);
}
countMap.put(i, countMap.get(i) + 1);
}

List<Integer> models = new ArrayList<>();
for (Integer i : countMap.keySet().toArray(new Integer[]{})) {
models.add(i);
}

Collections.sort(models, (i1,i2)-> {
return countMap.get(i1).compareTo(countMap.get(i2));
});

int treshold = 95 * trace.length / 100;
int sum = 0;
NumberFormat formatter = new DecimalFormat("##0.00");
for (int i = models.size() - 1; i > 0 && sum < treshold; i--) {
int current = models.get(i);
int contribution = countMap.get(current);
sum += contribution;
double con = 100*(contribution + 0.0)/trace.length;
if (con < 10) {
System.out.print(" ");
}
System.out.print(formatter.format(con) + "% " );
System.out.print(formatter.format(100*(sum + 0.0)/trace.length) + "% " );
System.out.println(current);
}
System.out.println();
}

static ConsoleApp app;
public static void main(String[] args) throws Exception {
BModelAnalyser analyser = new BModelAnalyser();
analyser.setID("Analyses bModelTest trace logs");
analyser.traceFileInput.setValue(new LogFile("someTrace.log"), analyser);

if (args.length == 0) {
// create BeautiDoc and beauti configuration
BeautiDoc doc = new BeautiDoc();
doc.beautiConfig = new BeautiConfig();
doc.beautiConfig.initAndValidate();

// create panel with entries for the application
BEASTObjectPanel panel = new BEASTObjectPanel(analyser, analyser.getClass(), doc);

// wrap panel in a dialog
BEASTObjectDialog dialog = new BEASTObjectDialog(panel, null);

// show the dialog
if (dialog.showDialog()) {
dialog.accept(analyser, doc);
// create a console to show standard error and standard output
app = new ConsoleApp("BModelAnalyser",
"BModelAnalyser: " + analyser.traceFileInput.get().getPath(),
null
);
analyser.initAndValidate();
analyser.run();
}
return;
}

Application main = new Application(analyser);
main.parseArgs(args, false);
analyser.initAndValidate();
analyser.run();
}

}
8 changes: 7 additions & 1 deletion version.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<addon name='bModelTest' version='0.2.0'>
<addon name='bModelTest' version='0.3.0'>
<depends on='beast2' atleast='2.4.0'/>
<depends on='BEASTLabs' atleast='1.5.0'/>

<addonapp description="BModelAnalyser"
class="beast.app.tools.BModelAnalyser"
args=""
/>
</addon>

0 comments on commit a9d94ff

Please sign in to comment.