-
Notifications
You must be signed in to change notification settings - Fork 2
/
DlgFDAnalysis.java
616 lines (558 loc) · 24.3 KB
/
DlgFDAnalysis.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* 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.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import tappas.DataApp.EnumData;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.UnaryOperator;
import static tappas.DataAnnotation.*;
/**
*
* @author Hector del Risco - hdelrisco@ufl.edu & Pedro Salguero - psalguero@cipf.es
*/
public class DlgFDAnalysis extends DlgBase {
Hyperlink lnkClearAll, lnkCheckAll;
TreeView tvFeaturesPresence, tvFeaturesPos;
TreeView tv;
ArrayList<String> lstFeatures;
ChoiceBox cbTestList, cbUsing;
TextField txtTestFile, txtName;
Button btnTestFile;
Label lblTestSelection;
RadioButton rbCat, rbId;
String paramId = "";
public DlgFDAnalysis(Project project, Window window) {
super(project, window);
}
public Params showAndWait(Params dfltParams) {
if(createDialog("FDAParams.fxml", "Functional Diversity Analysis Parameters", true, "Help_Dlg_FDA.html")) {
if(dfltParams == null)
dfltParams = new Params();
// get control objects
txtName = (TextField) scene.lookup("#txtName");
tvFeaturesPos = (TreeView) scene.lookup("#tvFeaturesPos");
tvFeaturesPresence = (TreeView) scene.lookup("#tvFeaturesPresence");
lnkClearAll = (Hyperlink) scene.lookup("#lnkClearAll");
lnkCheckAll = (Hyperlink) scene.lookup("#lnkCheckAll");
cbUsing = (ChoiceBox) scene.lookup("#cbUsing");
rbId = (RadioButton) scene.lookup("#rbId");
rbCat = (RadioButton) scene.lookup("#rbCat");
// select file with header, cause tappAS export tables with #header
txtTestFile = (TextField) scene.lookup("#txtTestFile");
btnTestFile = (Button) scene.lookup("#btnTestFile");
cbTestList = (ChoiceBox) scene.lookup("#cbTestLists");
lblTestSelection = (Label) scene.lookup("#lblTestSelection");
// setup listeners and bindings
lnkClearAll.setOnAction((event) -> {
if(tvFeaturesPos.isDisable())
clearAllFeatures(tvFeaturesPresence);
else
clearAllFeatures(tvFeaturesPos);
});
lnkCheckAll.setOnAction((event) -> {
if(tvFeaturesPos.isDisable())
checkAllFeatures(tvFeaturesPresence);
else
checkAllFeatures(tvFeaturesPos);
});
//Set analysis name
UnaryOperator<TextFormatter.Change> filter = (TextFormatter.Change change) -> {
if (change.getControlNewText().length() > Params.MAX_NAME_LENGTH) {
showDlgMsg("Name may not exceed " + Params.MAX_NAME_LENGTH + " characters");
return null;
} else {
showDlgMsg("");
return change;
}
};
txtName.setTextFormatter(new TextFormatter(filter));
txtName.setText(dfltParams.name);
paramId = dfltParams.paramId;
// setup dialog
btnTestFile.setOnAction((event) -> { getTestFile();});
rbId.setOnAction((event) -> {setIdView(); clearAllFeatures(tvFeaturesPos); clearAllFeatures(tvFeaturesPresence);});
rbCat.setOnAction((event) -> {setCatView();});
onDataTypeChange();
// set test list
cbTestList.getSelectionModel().select(Params.getIdxByName(dfltParams.testList.name(), Params.lstTestList));
int idx = 0;
for(int i = 0; i < cbTestList.getItems().size(); i++) {
if(((String)cbTestList.getItems().get(i)).startsWith(getCompareNameStart(Params.lstTestList.get(idx).name))){
cbTestList.getSelectionModel().select(i);
break;
}
}
onTestListChanged();
cbTestList.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
onTestListChanged();
});
// set using list
for(EnumData use : Params.lstUsing) {
cbUsing.getItems().add(use.name);
}
cbUsing.getSelectionModel().select(Params.getIdxById(dfltParams.using.name(), Params.lstUsing));
// populate annotation features
populateAnnotationFeaturesPresence(tvFeaturesPresence, dfltParams.hmFeatures);
populateAnnotationFeaturesPositional(tvFeaturesPos, dfltParams.hmFeatures);
//By deafult Genomic Position
tvFeaturesPresence.disableProperty();
tvFeaturesPresence.setVisible(false);
if(dfltParams.hmFeatures.isEmpty()){
checkAllFeatures(tvFeaturesPos);
checkAllFeatures(tvFeaturesPresence);
}
//set radio button
if(dfltParams.method.equals(Params.Method.CATEGORY)){
rbCat.setSelected(true);
rbId.setSelected(false);
}else{
rbId.setSelected(true);
rbCat.setSelected(false);
}
// setup dialog event handlers
dialog.setOnCloseRequest((DialogEvent event) -> {
if(dialog.getResult() != null && dialog.getResult().containsKey("ERRMSG")) {
showDlgMsg((String)dialog.getResult().get("ERRMSG"));
dialog.setResult(null);
event.consume();
}
});
int midx1 = cbUsing.getSelectionModel().getSelectedIndex();
String selected1 = Params.lstUsing.get(midx1).id;
if(selected1.equals(Params.lstUsing.get(0).id)){
loadPositionalFeatures();
}else{
loadPresenceFeatures();
}
cbUsing.setOnAction((event) -> {
int midx = cbUsing.getSelectionModel().getSelectedIndex();
String selected = Params.lstUsing.get(midx).id;
if(selected.equals(Params.lstUsing.get(0).id)){
loadPositionalFeatures();
}else{
loadPresenceFeatures();
}
});
dialog.setResultConverter((ButtonType b) -> {
HashMap<String, String> params = null;
System.out.println(b.getButtonData().toString());
if (b.getButtonData() == ButtonBar.ButtonData.OK_DONE)
params = validate(dialog);
return params;
});
// process dialog
Optional<HashMap> result = dialog.showAndWait();
if(result.isPresent())
return(new Params(result.get()));
}
return null;
}
//
// Internal Functions
//
private void loadPositionalFeatures(){
tvFeaturesPos.setDisable(false);
tvFeaturesPresence.setVisible(false);
tvFeaturesPresence.setDisable(true);
tvFeaturesPos.setVisible(true);
}
private void loadPresenceFeatures(){
tvFeaturesPresence.setDisable(false);
tvFeaturesPos.setVisible(false);
tvFeaturesPos.setDisable(true);
tvFeaturesPresence.setVisible(true);
}
private void onDataTypeChange() {
ArrayList<String> lstTest = new ArrayList<>();
cbTestList.getSelectionModel().clearSelection();
cbTestList.getItems().clear();
lstTest.add(Params.lstTestList.get(Params.TestList.ALL.ordinal()).name);
lstTest.add(Params.lstTestList.get(Params.TestList.FROMFILE.ordinal()).name);
for(String name : lstTest)
cbTestList.getItems().add(name);
if(cbTestList.getItems().size() > 0)
cbTestList.getSelectionModel().select(0);
}
private void setIdView(){
//Edit tree to acept just one source
rbCat.setSelected(false);
}
private void setCatView(){
rbId.setSelected(false);
}
private void onTestListChanged() {
// must use getSelectedIndex - called from changed listener
ArrayList<String> lst = new ArrayList<>();
if(cbTestList.getSelectionModel().getSelectedIndex() != -1) {
int idx = cbTestList.getSelectionModel().getSelectedIndex();
String rl = Params.TestList.valueOf(Params.lstTestList.get(idx).id).name();
switch(rl) {
case "FROMFILE":
lblTestSelection.setDisable(false);
txtTestFile.setDisable(false);
btnTestFile.setDisable(false);
break;
default:
lblTestSelection.setDisable(true);
txtTestFile.setDisable(true);
btnTestFile.setDisable(true);
break;
}
}
}
private void getTestFile() {
File f = getFileName();
if(f != null)
txtTestFile.setText(f.getPath());
}
private File getFileName() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(("Select List File"));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("List files", "*.txt", "*.tsv"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
String expFolder = app.userPrefs.getImportRankedListFEAFolder();
if(!expFolder.isEmpty()) {
File f = new File(expFolder);
if(f.exists() && f.isDirectory())
fileChooser.setInitialDirectory(f);
else
app.userPrefs.setImportRankedListFEAFolder("");
}
File selectedFile = fileChooser.showOpenDialog(dialog.getOwner());
if(selectedFile!=null)
app.userPrefs.setImportRankedListFEAFolder(selectedFile.getParent());
return selectedFile;
}
private HashMap<String, String> validate(Dialog dialog) {
HashMap<String, String> results = new HashMap<>();
String errmsg = "";
System.out.println("Validate dialog");
// get analysis name
String txt = txtName.getText().trim();
if(!txt.isEmpty()) {
// check if not an existing parameter id
if(paramId.isEmpty()) {
// check that name is not already used
ArrayList<DataApp.EnumData> lstFDAParams = project.data.analysis.getFDAParamsList();
for(DataApp.EnumData data : lstFDAParams) {
if(txt.toLowerCase().equals(data.name.toLowerCase())) {
errmsg = "The name specified is already in use.";
break;
}
}
}
if(errmsg.isEmpty())
results.put(Params.NAME_PARAM, txt);
}
else
errmsg = "You must provide a name for the analysis.";
if(!errmsg.isEmpty()) {
txtName.requestFocus();
results.put("ERRMSG", errmsg);
return results;
}
// get the test list
int idx = cbTestList.getSelectionModel().getSelectedIndex();
String lsitId = Params.lstTestList.get(idx).id;
results.put(Params.TESTLIST_PARAM, lsitId);
if(lsitId.equals(Params.TestList.FROMFILE.name())) {
String filepath = txtTestFile.getText().trim();
errmsg = checkTestListFile(filepath);
if(errmsg.isEmpty())
results.put(Params.TESTFROMFILE_PARAM, filepath);
else
txtTestFile.requestFocus();
}
if(errmsg.isEmpty()){
int midx = cbUsing.getSelectionModel().getSelectedIndex();
results.put(Params.USING_PARAM, Params.lstUsing.get(midx).id);
}
boolean cat = rbCat.isSelected();
if(errmsg.isEmpty()){
if(cat)
results.put(Params.METHOD_PARAM, Params.Method.CATEGORY.name());
else
results.put(Params.METHOD_PARAM, Params.Method.ID.name());
}
if(cat){
if(errmsg.isEmpty()) {
if(tvFeaturesPos.isDisable()){
tv = tvFeaturesPresence;
}else{
tv = tvFeaturesPos;
}
String term;
ArrayList<String> dbcats = new ArrayList<>();
TreeItem<String> rootItem = tv.getRoot();
ObservableList<TreeItem<String>> lst = rootItem.getChildren();
for(TreeItem ti : lst) {
term = "";
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
if(item.isSelected() && !item.isIndeterminate()) {
term = item.getValue();
dbcats.add(term);
System.out.println("Source: " + item.getValue());
}
else {
ObservableList<TreeItem<String>> sublst = item.getChildren();
for(TreeItem subti : sublst) {
CheckBoxTreeItem<String> subitem = (CheckBoxTreeItem<String>) subti;
if(subitem.isSelected()) {
if(term.isEmpty())
term = item.getValue();
term += "\t" + subitem.getValue();
}
}
if(!term.isEmpty())
dbcats.add(term);
}
}
if(dbcats.isEmpty())
errmsg = "You must select at least one annotation feature.";
if(errmsg.isEmpty()) {
int termnum = 1;
for(String ieterm : dbcats)
results.put(Params.FEATURE_PARAM + termnum++, ieterm);
}
else {
tv.requestFocus();
results.put("ERRMSG", errmsg);
}
}
}else{
//just save one
if(errmsg.isEmpty()) {
if(tvFeaturesPos.isDisable()){
tv = tvFeaturesPresence;
}else{
tv = tvFeaturesPos;
}
String term;
ArrayList<String> dbcats = new ArrayList<>();
TreeItem<String> rootItem = tv.getRoot();
ObservableList<TreeItem<String>> lst = rootItem.getChildren();
for(TreeItem ti : lst) {
term = "";
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
if(item.isSelected() || item.isIndeterminate()){
ObservableList<TreeItem<String>> sublst = item.getChildren();
for(TreeItem subti : sublst) {
CheckBoxTreeItem<String> subitem = (CheckBoxTreeItem<String>) subti;
if(subitem.isSelected()) {
if(term.isEmpty())
term = item.getValue();
term += "\t" + subitem.getValue();
}
}
if(!term.isEmpty())
dbcats.add(term);
}
}
if(dbcats.isEmpty())
errmsg = "You must select one annotation source.";
if(dbcats.size()>1)
errmsg = "You must select just one annotation source for ID analysis.";
if(errmsg.isEmpty()) {
int termnum = 1;
for(String ieterm : dbcats)
results.put(Params.FEATURE_PARAM + termnum++, ieterm);
}
else {
tv.requestFocus();
results.put("ERRMSG", errmsg);
}
}
}
return results;
}
private String checkTestListFile(String filepath) {
String errmsg = "";
if(!Files.exists(Paths.get(filepath)))
errmsg = "Specified test list file not found.";
return errmsg;
}
private static String getCompareNameStart(String name) {
int idx = name.indexOf("@");
if(idx != -1)
return name.substring(0, idx);
return name;
}
private void clearAllFeatures(TreeView aux) {
TreeItem<String> rootItem = aux.getRoot();
ObservableList<TreeItem<String>> lst = rootItem.getChildren();
for(TreeItem ti : lst) {
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
ObservableList<TreeItem<String>> sublst = item.getChildren();
for(TreeItem subti : sublst) {
CheckBoxTreeItem<String> subitem = (CheckBoxTreeItem<String>) subti;
subitem.setSelected(false);
}
item.setSelected(false);
}
}
private void checkAllFeatures(TreeView aux) {
TreeItem<String> rootItem = aux.getRoot();
ObservableList<TreeItem<String>> lst = rootItem.getChildren();
for(TreeItem ti : lst) {
CheckBoxTreeItem<String> item = (CheckBoxTreeItem<String>) ti;
ObservableList<TreeItem<String>> sublst = item.getChildren();
for(TreeItem subti : sublst) {
CheckBoxTreeItem<String> subitem = (CheckBoxTreeItem<String>) subti;
subitem.setSelected(true);
}
item.setSelected(true);
}
}
//
// Data Classes
//
public static class Params extends DlgParams {
public static enum Using {PRESENCE, GENPOS}
public static enum Method {CATEGORY, ID}
public static enum TestList {ALL, FROMFILE}
private static final List<EnumData> lstTestList = Arrays.asList(
new EnumData(TestList.ALL.name(), "All genes"),
new EnumData(TestList.FROMFILE.name(), "Use list of genes from file...")
);
private static final List<EnumData> lstMethod = Arrays.asList(
new EnumData(Method.CATEGORY.name(), "Category"),
new EnumData(Method.ID.name(), "Id")
);
private static final List<EnumData> lstUsing = Arrays.asList(
new EnumData(Using.GENPOS.name(), "Feature genomic position"),
new EnumData(Using.PRESENCE.name(), "Feature presence")
);
// special features - no spaces allowed, used for table column ID
public static final String FEATURE_CDS_LENGTH = CDS_FEATURE;
public static final String FEATURE_3UTR_LENGTH = UTRLENGTH3_FEATURE;
public static final String FEATURE_5UTR_LENGTH = UTRLENGTH5_FEATURE;
public static final String FEATURE_PAS_POSITION = POLYA_FEATURE;
public static final String NAME_PARAM = "name";
public static final String METHOD_PARAM = "method";
public static final String USING_PARAM = "using";
public static final String TESTLIST_PARAM = "testList";
public static final String TESTFROMFILE_PARAM = "testFromFile";
public static final String FEATURE_PARAM = "feature";
public static final int MAX_FEATURES = 99;
public static int MAX_NAME_LENGTH = 60;
private final Method dfltMethod = Method.CATEGORY;
private final Using dfltUsing = Using.GENPOS;
private final TestList dfltTestList = TestList.ALL;
public String name = "";
public String paramId = "";
TestList testList;
Integer totalFeatures;
public String testListFilepath = "";
Method method;
Using using;
HashMap<String, HashMap<String, Object>> hmFeatures = new HashMap<>();
public Params() {
name = "";
this.testList = dfltTestList;
this.testListFilepath = "";
this.method = dfltMethod;
this.using = dfltUsing;
this.totalFeatures = 0;
}
public Params(HashMap<String, String> hmParams){
name = hmParams.containsKey(NAME_PARAM)? hmParams.get(NAME_PARAM) : "";
this.testList = hmParams.containsKey(TESTLIST_PARAM)? TestList.valueOf(hmParams.get(TESTLIST_PARAM)) : dfltTestList;
this.testListFilepath = hmParams.containsKey(TESTFROMFILE_PARAM)? hmParams.get(TESTFROMFILE_PARAM) : "";
this.method = hmParams.containsKey(METHOD_PARAM)? Method.valueOf(hmParams.get(METHOD_PARAM)) : dfltMethod;
this.using = hmParams.containsKey(USING_PARAM)? Using.valueOf(hmParams.get(USING_PARAM)) : dfltUsing;
this.hmFeatures = new HashMap<>();
this.totalFeatures = 0;
for(int t = 1; t < MAX_FEATURES; t++) {
if(hmParams.containsKey(FEATURE_PARAM + t)) {
String[] fields = hmParams.get(FEATURE_PARAM + t).trim().split("\t");
if(fields.length > 0) {
this.totalFeatures = t;
HashMap<String, Object> hm = new HashMap<>();
hmFeatures.put(fields[0].trim(), hm);
for(int i = 1; i < fields.length; i++)
hm.put(fields[i].trim(), null);
}
}
}
}
@Override
public HashMap<String, String> getParams() {
HashMap<String, String> hm = new HashMap<>();
hm.put(NAME_PARAM, name);
hm.put(TESTLIST_PARAM, testList.name());
hm.put(TESTFROMFILE_PARAM, testListFilepath.toString());
hm.put(METHOD_PARAM, method.name());
hm.put(USING_PARAM, using.name());
int t = 1;
for(String db : hmFeatures.keySet()) {
String features = db;
HashMap<String, Object> hmCats = hmFeatures.get(db);
for(String cat : hmCats.keySet())
features += "\t" + cat;
hm.put(FEATURE_PARAM + t, features);
if(++t > MAX_FEATURES)
break;
}
return hm;
}
public String getFeaturesName() {
String source = "";
String features = "";
// NOTE: only single source allowed
for(String src : hmFeatures.keySet()) {
source = src;
HashMap<String, Object> hmSF = hmFeatures.get(src);
for(String feature : hmSF.keySet())
features += (features.isEmpty()? " [" : ", ") + feature;
features += features.isEmpty()? "" : "]";
break;
}
return source + features;
}
//
// Static functions
//
public static Params load(String filepath) {
HashMap<String, String> params = new HashMap<>();
Utils.loadParams(params, filepath);
return (new Params(params));
}
private static int getIdxById(String id, List<EnumData> lst) {
int idx = 0;
for(EnumData ed : lst) {
if(id.equals(ed.id))
break;
idx++;
}
if(idx >= lst.size())
idx = 0;
return idx;
}
private static int getIdxByName(String name, List<EnumData> lst) {
int idx = 0;
for(EnumData entry : lst) {
String cmpName = getCompareNameStart(entry.name);
if(name.startsWith(cmpName)) {
// "All @" matches "All multiple..." so handle it at end
if(idx != 0)
break;
}
idx++;
}
if(idx >= lst.size())
idx = 0;
return idx;
}
}
}