-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExpLevelDensityPlot.java
197 lines (183 loc) · 7.84 KB
/
ExpLevelDensityPlot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tappas;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Task;
import javafx.scene.Node;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Hector del Risco - [email protected] & Pedro Salguero - [email protected]
*/
public class ExpLevelDensityPlot extends AppObject {
ProgressIndicator pi;
Pane paneImgChart;
ImageView imgChart;
boolean plotShown = false;
Path runScriptPath = null;
double ratio = 0.75;
DataApp.DataType dataType;
SubTabBase.SubTabInfo subTabInfo = null;
TaskHandler.ServiceExt service = null;
public ExpLevelDensityPlot(Project project) {
super(project, null);
}
public void showExpLevelsDensityPlot(DataApp.DataType type, SubTabBase.SubTabInfo subTabInfo, Node tabNode) {
this.dataType = type;
this.subTabInfo = subTabInfo;
paneImgChart = (Pane) tabNode.lookup("#paneImgChart");
imgChart = (ImageView) tabNode.lookup("#imgChart");
pi = (ProgressIndicator) tabNode.lookup("#piDensityPlot");
pi.layoutXProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart.widthProperty(), 2.0), 25.0));
pi.layoutYProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart.heightProperty(), 2.0), 25.0));
imgChart.fitHeightProperty().bind(paneImgChart.heightProperty());
imgChart.fitWidthProperty().bind(paneImgChart.widthProperty());
imgChart.setPreserveRatio(true);
app.ctls.setupImageExport(imgChart, "Transcripts Expression Levels Density Plot", "tapas_" + "TransExpLevelsDPlot.png", null);
String filepath = project.data.getMatrixMeanLogExpLevelsPlotFilepath(dataType.name());
if(!plotShown) {
if(Files.exists(Paths.get(filepath))) {
plotShown = true;
showDensityPlotImage(filepath);
}
else {
boolean runDLT = true;
if(!Files.exists(Paths.get(project.data.getMatrixMeanLogExpLevelsFilepath(dataType.name())))) {
if(!genExpLevelsPlotData(dataType))
runDLT = false;
}
// start task to create chart in R and return image
if(runDLT)
runDataLoadThread();
}
}
}
//
// Internal Functions
//
private void showDensityPlotImage(String filepath) {
Image img = new Image("file:" + filepath);
pi.setVisible(false);
imgChart.setImage(img);
imgChart.setVisible(true);
}
private boolean genExpLevelsPlotData(DataApp.DataType type) {
boolean result = false;
double[][] expLevels = project.data.getMeanExpressionLevels(type, project.data.getResultsTrans());
String filepath = project.data.getMatrixMeanLogExpLevelsFilepath(type.name());
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath, false), "utf-8"));
String header = "";
String[] names = project.data.getExpTypeGroupNames();
for(String cname : names)
header += (header.isEmpty()? "" : "\t") + cname;
writer.write(header + "\n");
int rows = expLevels[0].length;
for(int row = 0; row < rows; row++) {
String rowvals = "";
for(int cond = 0; cond < expLevels.length; cond++)
rowvals += (rowvals.isEmpty()? "" : "\t") + Math.log10(expLevels[cond][row]);
writer.write(rowvals + "\n");
}
result = true;
} catch(Exception e) {
logger.logError("Unable to write expression matrix mean log file: " + e.getMessage());
} finally {
try {
if(writer != null)
writer.close();
} catch (Exception e) {
System.out.println("Code exception closing Writer within an exception.");
}
}
// make sure to remove bad file if needed
if(!result)
Utils.removeFile(Paths.get(filepath));
return result;
}
//
// Data Load
//
private void runDataLoadThread() {
// get script path and run service/task
runScriptPath = app.data.getTmpScriptFileFromResource("densityPlot.R");
service = new DataLoadService();
service.start();
}
private class DataLoadService extends TaskHandler.ServiceExt {
@Override
protected void onRunning() {
pi.setProgress(-1);
pi.setVisible(true);
}
@Override
protected void onStopped() {
pi.setVisible(false);
pi.setProgress(0);
}
@Override
protected void onFailed() {
java.lang.Throwable e = getException();
if(e != null)
app.logWarning("ExpLevelDensityPlot failed - task aborted. Exception: " + e.getMessage());
else
app.logWarning("ExpLevelDensityPlot - task aborted.");
app.ctls.alertWarning("Experiment Level Density Plot", "ExpLevelDensityPlot failed - task aborted");
}
@Override
protected void onSucceeded() {
String filepath = project.data.getMatrixMeanLogExpLevelsPlotFilepath(dataType.name());
if(Files.exists(Paths.get(filepath))) {
if(!plotShown) {
plotShown = true;
showDensityPlotImage(filepath);
}
}
else
app.ctls.alertWarning("Transcripts Expression Levels Density Plot", "Unable to generate expression levels density plot.");
}
@Override
protected Task<Void> createTask() {
Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
String logfilepath = project.data.getSubTabLogFilepath(subTabInfo.subTabId);
subTabInfo.subTabBase.outputFirstLogLine("Expression Levels Density Plot thread running...", logfilepath);
// setup script arguments
List<String> lst = new ArrayList<>();
lst.add(app.data.getRscriptFilepath());
lst.add(runScriptPath.toString());
String type = app.data.getDataTypeSingular(dataType.name());
lst.add("-d" + type.substring(0,1).toUpperCase() + type.substring(1).toLowerCase());
lst.add("-i" + project.data.getMatrixMeanLogExpLevelsFilepath(dataType.name()));
lst.add("-o" + project.data.getMatrixMeanLogExpLevelsPlotFilepath(dataType.name()));
subTabInfo.subTabBase.appendLogLine("Starting R script...", logfilepath);
logger.logDebug("Running expression levels density plot script:\n " + lst);
subTabInfo.subTabBase.runScript(taskInfo, lst, "Expression Levels Density Plot", logfilepath);
// remove script file from temp folder
Utils.removeFile(runScriptPath);
runScriptPath = null;
return null;
}
};
taskInfo = new TaskHandler.TaskInfo("Expression Levels Density Plot", task);
return task;
}
}
}