use of uk.ac.babraham.SeqMonk.Displays.PCAPlot.PCAScatterPlotDialog in project SeqMonk by s-andrews.
the class TsneDataStoreResult method runTsne.
private void runTsne() {
File tempDir;
try {
pd.progressUpdated("Creating temp directory", 0, 1);
tempDir = TempDirectory.createTempDirectory();
// System.err.println("Temp dir is "+tempDir.getAbsolutePath());
pd.progressUpdated("Writing R script", 0, 1);
// Get the template script
Template template = new Template(ClassLoader.getSystemResource("uk/ac/babraham/SeqMonk/Displays/TsneDataStorePlot/tsne_template.r"));
// Substitute in the variables we need to change
template.setValue("WORKING", tempDir.getAbsolutePath().replace("\\", "/"));
template.setValue("ITERATIONS", "" + iterations);
template.setValue("PERPLEXITY", "" + perplexity);
// Write the script file
File scriptFile = new File(tempDir.getAbsoluteFile() + "/script.r");
PrintWriter pr = new PrintWriter(scriptFile);
pr.print(template.toString());
pr.close();
// Write the count data
File countFile = new File(tempDir.getAbsoluteFile() + "/data.txt");
pr = new PrintWriter(countFile);
StringBuffer sb;
pd.progressUpdated("Writing count data", 0, 1);
for (int p = 0; p < usedProbes.length; p++) {
sb = new StringBuffer();
for (int d = 0; d < stores.length; d++) {
if (d > 0)
sb.append("\t");
sb.append(stores[d].getValueForProbe(usedProbes[p]));
}
pr.println(sb.toString());
}
pr.close();
pd.progressUpdated("Running R Script", 0, 1);
RScriptRunner runner = new RScriptRunner(tempDir);
RProgressListener listener = new RProgressListener(runner);
runner.addProgressListener(new ProgressRecordDialog("R Session", runner));
runner.runScript();
while (true) {
if (listener.cancelled()) {
pd.progressCancelled();
return;
}
if (listener.exceptionReceived()) {
pd.progressExceptionReceived(listener.exception());
return;
}
if (listener.complete())
break;
Thread.sleep(500);
}
// We can now parse and store the results
// Tsne only ever returns 2 dimensions so we'll fake up a variances
// result which says that we only have 2 components with 50% each
variances = new float[] { 50, 50 };
File tsneFile = new File(tempDir.getAbsolutePath() + "/tsne_data.txt");
BufferedReader br = new BufferedReader(new FileReader(tsneFile));
String line = br.readLine();
pcaResults = new float[stores.length][variances.length];
int storeIndex = 0;
while ((line = br.readLine()) != null) {
String[] sections = line.split("\t");
for (int i = 0; i < variances.length; i++) {
pcaResults[storeIndex][i] = Float.parseFloat(sections[i + 1]);
}
++storeIndex;
}
br.close();
// We don't get weights from Tsne
runner.cleanUp();
} catch (Exception e) {
pd.progressExceptionReceived(e);
return;
}
pd.progressComplete("tsne_analysis", this);
new PCAScatterPlotDialog(this);
}
Aggregations