-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlotsDFI.java
394 lines (358 loc) · 17.7 KB
/
PlotsDFI.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* 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.control.ProgressIndicator;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author Pedro Salguero - [email protected]
*/
public class PlotsDFI extends AppObject {
ProgressIndicator pi1, pi2, pi3, pi4;
Pane paneImgChart1, paneImgChart2, paneImgChart3, paneImgChart4;
ImageView imgChart1, imgChart2, imgChart3, imgChart4;
String analysisId = "";
boolean plotShown = false;
Path runScriptPath = null;
double ratio = 0.75;
DataApp.DataType dataType;
SubTabBase.SubTabInfo subTabInfo = null;
TaskHandler.ServiceExt service = null;
String export_name1 = "DFIBarPlot_Features";
String export_name2 = "DFIBarPlot_Genes";
String export_name3 = "DFIBarPlot_Bar";
String export_name4 = "DFIBarPlot_Box";
String png_extension = ".png";
public PlotsDFI(Project project, String analysisId) {
super(project, null, analysisId);
}
public void showDFIBarPlots(SubTabBase.SubTabInfo subTabInfo, ScrollPane spAll, String analysisId, boolean update){
this.subTabInfo = subTabInfo;
this.analysisId = analysisId;
paneImgChart1 = (Pane) spAll.getContent().lookup("#paneImgChart1");
imgChart1 = (ImageView) spAll.getContent().lookup("#imgChart1");
pi1 = (ProgressIndicator) spAll.getContent().lookup("#piDFIBarPlot1");
paneImgChart2 = (Pane) spAll.getContent().lookup("#paneImgChart2");
imgChart2 = (ImageView) spAll.getContent().lookup("#imgChart2");
pi2 = (ProgressIndicator) spAll.getContent().lookup("#piDFIBarPlot2");
paneImgChart3 = (Pane) spAll.getContent().lookup("#paneImgChart3");
imgChart3 = (ImageView) spAll.getContent().lookup("#imgChart3");
pi3 = (ProgressIndicator) spAll.getContent().lookup("#piDFIBarPlot3");
paneImgChart4 = (Pane) spAll.getContent().lookup("#paneImgChart4");
imgChart4 = (ImageView) spAll.getContent().lookup("#imgChart4");
pi4 = (ProgressIndicator) spAll.getContent().lookup("#piDFIBarPlot4");
pi1.layoutXProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart1.widthProperty(), 2.0), 25.0));
pi1.layoutYProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart1.heightProperty(), 2.0), 25.0));
imgChart1.fitHeightProperty().bind(paneImgChart1.heightProperty());
imgChart1.fitWidthProperty().bind(paneImgChart1.widthProperty());
imgChart1.setPreserveRatio(true);
//imgChart1.translateXProperty().bind(paneImgChart1.widthProperty().subtract(imgChart1.getLayoutX()).divide(2));
app.ctls.setupImageExport(imgChart1, "DFI Bar Plot Features", "tappAS_" + export_name1 + analysisId + png_extension, null);
pi2.layoutXProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart2.widthProperty(), 2.0), 25.0));
pi2.layoutYProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart2.heightProperty(), 2.0), 25.0));
imgChart2.fitHeightProperty().bind(paneImgChart2.heightProperty());
imgChart2.fitWidthProperty().bind(paneImgChart2.widthProperty());
imgChart2.setPreserveRatio(true);
app.ctls.setupImageExport(imgChart2, "DFI Bar Plot Genes", "tappAS_" + export_name2 + analysisId + png_extension, null);
pi3.layoutXProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart3.widthProperty(), 2.0), 25.0));
pi3.layoutYProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart3.heightProperty(), 2.0), 25.0));
imgChart3.fitHeightProperty().bind(paneImgChart3.heightProperty());
imgChart3.fitWidthProperty().bind(paneImgChart3.widthProperty());
imgChart3.setPreserveRatio(true);
app.ctls.setupImageExport(imgChart3, "DFI Bar Plot Genes", "tappAS_" + export_name3 + analysisId + png_extension, null);
pi4.layoutXProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart4.widthProperty(), 2.0), 25.0));
pi4.layoutYProperty().bind(Bindings.subtract(Bindings.divide(paneImgChart4.heightProperty(), 2.0), 25.0));
imgChart4.fitHeightProperty().bind(paneImgChart4.heightProperty());
imgChart4.fitWidthProperty().bind(paneImgChart4.widthProperty());
imgChart4.setPreserveRatio(true);
app.ctls.setupImageExport(imgChart4, "DFI Bar Plot Genes", "tappAS_" + export_name4 + analysisId + png_extension, null);
//Change loading and load DFI plots results
String filepath1 = project.data.getDFIPlot1Filepath(analysisId);
String filepath2 = project.data.getDFIPlot2Filepath(analysisId);
String filepath3 = project.data.getDFIPlot3Filepath(analysisId);
String filepath4 = project.data.getDFIPlot4Filepath(analysisId);
if(!plotShown) {
if(Files.exists(Paths.get(filepath1)) && Files.exists(Paths.get(filepath2)) &&
Files.exists(Paths.get(filepath3)) && Files.exists(Paths.get(filepath4)) && !update) {
plotShown = true;
showDFIPlotsImage(filepath1, filepath2, filepath3, filepath4);
}
else {
boolean runDLT = true;
if(!Files.exists(Paths.get(project.data.getMatrixDFIFilepath(analysisId)))) {
runDLT = false;
}
// start task to create chart in R and return image
if(runDLT)
runDataLoadThread();
}
}
}
/*public void runDFISummaryAnalysis(String analysisId){
boolean runDLT = true;
if(!Files.exists(Paths.get(project.data.getMatrixDFIFilepath(analysisId)))) {
runDLT = false;
}
// start task to create chart in R and return image
if(runDLT)
runDataLoadFirstTimeThread();
}*/
//
// Internal Functions
//
private void showDFIPlotsImage(String filepath1, String filepath2, String filepath3, String filepath4) {
Image img = new Image("file:" + filepath1);
pi1.setVisible(false);
imgChart1.setImage(img);
imgChart1.setVisible(true);
img = new Image("file:" + filepath2);
pi2.setVisible(false);
imgChart2.setImage(img);
imgChart2.setVisible(true);
img = new Image("file:" + filepath3);
pi3.setVisible(false);
imgChart3.setImage(img);
imgChart3.setVisible(true);
if(project.data.isTimeCourseExpType()) {
img = new Image("file:" + filepath4);
pi4.setVisible(false);
imgChart4.setImage(img);
imgChart4.setVisible(false);
}else{
img = new Image("file:" + filepath4);
pi4.setVisible(false);
imgChart4.setImage(img);
imgChart4.setVisible(true);
}
}
//
// Data Load
//
private void runDataLoadThread() {
// get script path and run service/task
//CountDownLatch latch = new CountDownLatch(1);
runScriptPath = app.data.getTmpScriptFileFromResource("DFI_BarPlot.R");
service = new DataLoadService();
service.start();
}
private class DataLoadService extends TaskHandler.ServiceExt {
@Override
protected void onRunning() {
pi1.setProgress(-1);
pi2.setProgress(-1);
pi3.setProgress(-1);
pi4.setProgress(-1);
pi1.setVisible(true);
pi2.setVisible(true);
pi3.setVisible(true);
pi4.setVisible(true);
}
@Override
protected void onStopped() {
pi1.setVisible(false);
pi2.setVisible(false);
pi3.setVisible(false);
pi4.setVisible(false);
pi1.setProgress(0);
pi2.setProgress(0);
pi3.setProgress(0);
pi4.setProgress(0);
}
@Override
protected void onFailed() {
java.lang.Throwable e = getException();
if(e != null)
app.logWarning("DFI Bar Plot failed - task aborted. Exception: " + e.getMessage());
else
app.logWarning("DFI Bar Plot - task aborted.");
app.ctls.alertWarning("DFI Bar Plot", "ScatterPlot failed - task aborted");
}
@Override
protected void onSucceeded() {
String filepath1 = project.data.getDFIPlot1Filepath(analysisId);
String filepath2 = project.data.getDFIPlot2Filepath(analysisId);
String filepath3 = project.data.getDFIPlot3Filepath(analysisId);
String filepath4 = project.data.getDFIPlot4Filepath(analysisId);
if(Files.exists(Paths.get(filepath1)) && Files.exists(Paths.get(filepath2)) &&
Files.exists(Paths.get(filepath3)) && Files.exists(Paths.get(filepath4))) {
if(!plotShown) {
plotShown = true;
showDFIPlotsImage(filepath1, filepath2, filepath3, filepath4);
}
}
else
app.ctls.alertWarning("DFI Bar Plot", "Unable to generate DFI plots.");
}
@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("DFI Bar Plot Running...", logfilepath);
//Creating lists
String trans = "";
String proteins = "";
HashMap<String, HashMap<String, String>> hmType = project.data.getAFStatsType();
for(String db : hmType.keySet()){
HashMap<String, String> hmFeat = hmType.get(db);
for(String feat : hmFeat.keySet()){
if(hmFeat.get(feat).equals("T")){
if(trans.equals(""))
trans = feat;
else
trans = trans + "," + feat;
}else if(hmFeat.get(feat).equals("P")){
if(proteins.equals(""))
proteins = feat;
else
proteins = proteins + "," + feat;
}
}
}
// setup script arguments
List<String> lst = new ArrayList<>();
lst.add(app.data.getRscriptFilepath());
lst.add(runScriptPath.toString());
lst.add("-i" + project.data.getMatrixDFIFilepath(analysisId));
lst.add("-a" + analysisId);
lst.add("-c" + project.data.getDFITotalFeaturesFilepath(analysisId));
lst.add("-lt" + trans);
lst.add("-lp" + proteins);
lst.add("-o1" + project.data.getDFIPlot1Filepath(analysisId));
lst.add("-o2" + project.data.getDFIPlot2Filepath(analysisId));
lst.add("-o3" + project.data.getDFIPlot3Filepath(analysisId));
lst.add("-o4" + project.data.getDFIPlot4Filepath(analysisId));
lst.add("-t1" + project.data.getDFITestFeaturesFilepath(analysisId));
lst.add("-t2" + project.data.getDFITestGenesFilepath(analysisId));
/*//DONT WORK IN WINDOWS
List<String> newlst = new ArrayList<>();
if(Utils.isWindowsOS()) {
for (String i : lst) {
if (i.startsWith("-"))
newlst.add(i.replace("\\", "/"));
else
newlst.add(i);
}
lst = newlst;
}
System.out.println(lst);*/
subTabInfo.subTabBase.appendLogLine("Starting R script...", logfilepath);
logger.logDebug("Running bar plot script:\n " + lst);
subTabInfo.subTabBase.runScript(taskInfo, lst, "DFI Bar Plot for " + analysisId, logfilepath);
// remove script file from temp folder
Utils.removeFile(runScriptPath);
runScriptPath = null;
return null;
}
};
taskInfo = new TaskHandler.TaskInfo("DFI Bar Plot for " + analysisId, task);
return task;
}
}
/*private void runDataLoadFirstTimeThread() {
// get script path and run service/task
//CountDownLatch latch = new CountDownLatch(1);
runScriptPath = app.data.getTmpScriptFileFromResource("DFI_BarPlot.R");
service = new DataLoadFirstTime();
service.start();
}
private class DataLoadFirstTime extends TaskHandler.ServiceExt {
@Override
protected void onRunning() {
logger.logDebug("Running...");
}
@Override
protected void onStopped() {
logger.logDebug("Stopped...");
}
@Override
protected void onFailed() {
java.lang.Throwable e = getException();
if(e != null)
app.logWarning("DFI Bar Plot " + analysisId + " failed - task aborted. Exception: " + e.getMessage());
else
app.logWarning("DFI Bar Plot " + analysisId + " - task aborted.");
app.ctls.alertWarning("DFI Bar Plot " + analysisId, "ScatterPlot failed - task aborted");
}
@Override
protected void onSucceeded() {
String filepath1 = project.data.getDFIPlot1Filepath(analysisId);
String filepath2 = project.data.getDFIPlot2Filepath(analysisId);
String filepath3 = project.data.getDFIPlot3Filepath(analysisId);
String filepath4 = project.data.getDFIPlot4Filepath(analysisId);
if(Files.exists(Paths.get(filepath1)) && Files.exists(Paths.get(filepath2)) &&
Files.exists(Paths.get(filepath3)) && Files.exists(Paths.get(filepath4))) {
logger.logDebug("Complete...");
}
else
app.ctls.alertWarning("DFI Bar Plot " + analysisId, "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.getDFILogFilepath(analysisId);
//Creating lists
String trans = "";
String proteins = "";
HashMap<String, HashMap<String, String>> hmType = project.data.getAFStatsType();
for(String db : hmType.keySet()){
HashMap<String, String> hmFeat = hmType.get(db);
for(String feat : hmFeat.keySet()){
if(hmFeat.get(feat).equals("T")){
if(trans.equals(""))
trans = feat;
else
trans = trans + "\t" + feat;
}else if(hmFeat.get(feat).equals("P")){
if(proteins.equals(""))
proteins = feat;
else
proteins = proteins + "\t" + feat;
}
}
}
// setup script arguments
List<String> lst = new ArrayList<>();
lst.add(app.data.getRscriptFilepath());
lst.add(runScriptPath.toString());
lst.add("-i" + project.data.getMatrixDFIFilepath(analysisId));
lst.add("-a" + analysisId);
lst.add("-c" + project.data.getDFITotalFeaturesFilepath(analysisId));
lst.add("-lt" + trans);
lst.add("-lp" + proteins);
lst.add("-o1" + project.data.getDFIPlot1Filepath(analysisId));
lst.add("-o2" + project.data.getDFIPlot2Filepath(analysisId));
lst.add("-o3" + project.data.getDFIPlot3Filepath(analysisId));
lst.add("-o4" + project.data.getDFIPlot4Filepath(analysisId));
lst.add("-t1" + project.data.getDFITestFeaturesFilepath(analysisId));
lst.add("-t2" + project.data.getDFITestGenesFilepath(analysisId));
logger.logDebug("Running bar plot script:\n " + lst);
System.out.println(lst);
// remove script file from temp folder
Utils.removeFile(runScriptPath);
runScriptPath = null;
return null;
}
};
taskInfo = new TaskHandler.TaskInfo("DFI Bar Plot " + analysisId, task);
return task;
}
}*/
}