-
Notifications
You must be signed in to change notification settings - Fork 2
/
DlgInputData.java
1408 lines (1357 loc) · 72.8 KB
/
DlgInputData.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.stage.FileChooser;
import javafx.stage.Window;
import tappas.AnnotationFiles.AnnotationFileInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.UnaryOperator;
/* TODO
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at tappas.DlgInputData.lambda$showAndWait$9(DlgInputData.java:178)
I think it happens when converting from old format to new format w/ demo
*/
/**
*
* @author Hector del Risco - [email protected] & Pedro Salguero - [email protected]
*/
public class DlgInputData extends DlgBase {
final String msgDemo = "<Demo includes its own expression matrix file>";
Label lblFilterValues, lblFilterCOV;
Pane panelExpMatrix;
CheckBox chkFilter, chkNorm;
TextField txtMatrix, txtDesign, txtList, txtAnnot, txtProject, txtFilterValues, txtFilterCOV;
Button btnMatrix, btnDesign, btnList, btnAnnot;
RadioButton rbAppFile, rbUserFile;
ComboBox cboSpecies, cboFiles, cboExperiment;
ChoiceBox cboFilters;
ArrayList<AnnotationFileInfo> lstAnnotationFiles = new ArrayList<>();
ArrayList<String> lstSpecies = new ArrayList<>();
//HashMap<String, String> vals = new HashMap<>();
Params params;
boolean initializing = true;
boolean newProject = false;
String projectId = "";
public DlgInputData(Project project, Window window) {
super(project, window);
}
public Params showAndWait(boolean newProject, Params dfltParams) throws IllegalArgumentException {
this.params = dfltParams == null? new Params(null) : dfltParams;
this.newProject = newProject;
if(createDialog("InputData.fxml", newProject? "New Project" : "Load Input Data for Current Project", true, "Help_Dlg_NewProject.html")) {
/// get control objects
txtProject = (TextField) scene.lookup("#txtProject");
panelExpMatrix = (Pane) scene.lookup("#panelExpMatrix");
txtDesign = (TextField) scene.lookup("#txtDesign");
txtMatrix = (TextField) scene.lookup("#txtMatrix");
txtAnnot = (TextField) scene.lookup("#txtAnnot");
rbUserFile = (RadioButton) scene.lookup("#rbUserFile");
rbAppFile = (RadioButton) scene.lookup("#rbAppFile");
btnDesign = (Button) scene.lookup("#btnDesign");
btnMatrix = (Button) scene.lookup("#btnMatrix");
btnAnnot = (Button) scene.lookup("#btnAnnot");
chkFilter = (CheckBox) scene.lookup("#chkFilter");
chkNorm = (CheckBox) scene.lookup("#chkNorm");
txtFilterValues = (TextField) scene.lookup("#txtFilterValues");
txtFilterCOV = (TextField) scene.lookup("#txtFilterCOV");
lblFilterValues = (Label) scene.lookup("#lblFilterValues");
lblFilterCOV = (Label) scene.lookup("#lblFilterCOV");
cboExperiment = (ComboBox) scene.lookup("#cboExperiment");
cboSpecies = (ComboBox) scene.lookup("#cboSpecies");
cboFiles = (ComboBox) scene.lookup("#cboFiles");
cboFilters = (ChoiceBox) scene.lookup("#cboFilters");
txtList = (TextField) scene.lookup("#txtList");
btnList = (Button) scene.lookup("#btnList");
// setup dialog
txtProject.setDisable(!newProject);
UnaryOperator<TextFormatter.Change> filter = (TextFormatter.Change change) -> {
if (change.getControlNewText().length() > Params.MAX_PROJECTNAME_LENGTH) {
showDlgMsg("Project name may not exceed " + Params.MAX_PROJECTNAME_LENGTH + " characters.");
return null;
} else {
showDlgMsg("");
return change;
}
};
UnaryOperator<TextFormatter.Change> filterValue = (TextFormatter.Change change) -> {
if (change.getControlNewText().length() > Params.MAX_VALDIGS)
return null;
else
return change;
};
UnaryOperator<TextFormatter.Change> filterCOV = (TextFormatter.Change change) -> {
if (change.getControlNewText().length() > Params.MAX_COVDIGS)
return null;
else
return change;
};
// get annotation files list also used for species
lstAnnotationFiles = app.getAnnotationFilesList();
lstSpecies = new ArrayList<>();
for(AnnotationFileInfo afi : lstAnnotationFiles) {
String name = afi.genus + " " + afi.species;
if(!lstSpecies.contains(name))
lstSpecies.add(name);
}
// add one for other - make sure to keep 2 word format
lstSpecies.add("Other species");
// setup controls
txtProject.setTextFormatter(new TextFormatter(filter));
btnDesign.setOnAction((event) -> { getDesignFile(); });
btnMatrix.setOnAction((event) -> { getMatrixFile(); });
btnAnnot.setOnAction((event) -> { getAnnotFile(); });
rbAppFile.setOnAction((event) -> { onAnnotSelection(event); });
rbUserFile.setOnAction((event) -> { onAnnotSelection(event); });
txtAnnot.disableProperty().bind(rbAppFile.selectedProperty());
btnAnnot.disableProperty().bind(rbAppFile.selectedProperty());
txtFilterValues.disableProperty().bind(chkFilter.selectedProperty().not());
txtFilterValues.setTextFormatter(new TextFormatter(filterValue));
txtFilterCOV.disableProperty().bind(chkFilter.selectedProperty().not());
txtFilterCOV.setTextFormatter(new TextFormatter(filterCOV));
lblFilterValues.disableProperty().bind(chkFilter.selectedProperty().not());
lblFilterCOV.disableProperty().bind(chkFilter.selectedProperty().not());
for(DataApp.EnumData rep : DataApp.lstExperiments)
cboExperiment.getItems().add(rep.name);
cboExperiment.getSelectionModel().clearSelection();
for(String name : lstSpecies)
cboSpecies.getItems().add(name);
cboSpecies.getSelectionModel().clearSelection();
cboFiles.disableProperty().bind(Bindings.or(rbAppFile.selectedProperty().not(), rbAppFile.disabledProperty()));
btnList.setOnAction((event) -> { getListFile(); });
cboSpecies.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
if(newValue != null && (int) newValue != -1) {
// only enable, panes are disabled in initial form - no way to go back to disabled
panelExpMatrix.setDisable(false);
rbUserFile.setDisable(false);
if(cboFiles.getItems() != null)
cboFiles.getItems().clear();
rbAppFile.setDisable(false);
rbAppFile.setSelected(true);
String species = lstSpecies.get((int) newValue);
for(AnnotationFileInfo afi : lstAnnotationFiles) {
String name = afi.genus + " " + afi.species;
if(name.equals(species))
cboFiles.getItems().add(afi.genus + " " + afi.species + " " + afi.reference + " " + afi.release);
}
if(cboFiles.getItems().isEmpty()) {
rbUserFile.setSelected(true);
rbAppFile.setDisable(true);
}
else {
if(initializing) {
// check if this already has data from previous processing
if(params.refType != null && !params.refRelease.isEmpty()) {
String selection = params.genus + " " + params.species + " " + params.refType.name() + " " + params.refRelease;
for(String sel : (ObservableList<String>) cboFiles.getItems()) {
if(selection.equals(sel)) {
cboFiles.getSelectionModel().select(sel);
break;
}
}
}
}
}
}
});
cboFiles.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
if(newValue != null) {
boolean demo = false;
if((int) newValue != -1) {
int idx = getRefFileIndexFromId((String) cboFiles.getItems().get((int) newValue), lstAnnotationFiles);
AnnotationFileInfo afi = lstAnnotationFiles.get(idx);
if(afi != null && afi.reference.equals(DataApp.RefType.Demo.name()))
demo = true;
}
if(demo) {
txtDesign.setText(msgDemo);
txtMatrix.setText(msgDemo);
// currently only one demo is provided - change if needed
cboExperiment.getSelectionModel().select(getExpTypeIndexFromId(DataApp.ExperimentType.Two_Group_Comparison.name(), DataApp.lstExperiments));
}
else {
if(txtDesign.getText().equals(msgDemo))
txtDesign.setText("");
if(txtMatrix.getText().equals(msgDemo))
txtMatrix.setText("");
}
txtDesign.setDisable(demo);
btnDesign.setDisable(demo);
txtMatrix.setDisable(demo);
btnMatrix.setDisable(demo);
cboExperiment.setDisable(demo);
}
});
// initial change of species will set the reference file type and overwrite expMatrix file name if demo
if(!params.genus.isEmpty() && !params.species.isEmpty())
cboSpecies.getSelectionModel().select(getSpeciesIndex(params.genus, params.species));
if(params.experimentType != null)
cboExperiment.getSelectionModel().select(getExpTypeIndexFromId(params.experimentType.name(), DataApp.lstExperiments));
txtProject.setText(params.name);
txtDesign.setText(params.edFilepath);
txtMatrix.setText(params.emFilepath);
txtAnnot.setText(params.afFilepath);
if(params.useAppRef)
rbAppFile.setSelected(true);
else
rbUserFile.setSelected(true);
ToggleGroup tg = rbAppFile.getToggleGroup();
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) -> { onAnnotSelection(null); });
onAnnotSelection(null);
if(params.filter) {
chkFilter.setSelected(true);
txtFilterValues.setText("" + params.filterValue);
txtFilterCOV.setText("" + params.filterCOV);
}
else
chkFilter.setSelected(false);
if(params.norm)
chkNorm.setSelected(true);
else
chkNorm.setSelected(false);
// set filter list
for(DataApp.EnumData ed : Params.lstFilterLists)
cboFilters.getItems().add(ed.name);
cboFilters.getSelectionModel().select(Params.getFilterListIndexById(params.filterList.name()));
txtList.setText(params.lstFilepath);
cboFilters.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
onFiltersChanged();
});
onFiltersChanged();
dialog.setOnCloseRequest((DialogEvent event) -> {
if(dialog.getResult() != null && dialog.getResult().containsKey("ERRMSG")) {
showDlgMsg((String)dialog.getResult().get("ERRMSG"));
dialog.setResult(null);
event.consume();
}
});
dialog.setResultConverter((ButtonType b) -> {
HashMap<String, String> prm = null;
System.out.println(b.getButtonData().toString());
if (b.getButtonData() == ButtonBar.ButtonData.OK_DONE)
prm = validate(dialog);
return prm;
});
initializing = false;
Optional<HashMap> result = dialog.showAndWait();
if(result.isPresent())
return (new Params(result.get()));
}
return null;
}
private void onAnnotSelection(ActionEvent event) {
boolean flg = !rbUserFile.isSelected();
boolean demo = false;
if(flg) {
if(cboFiles.getSelectionModel().getSelectedIndex() != -1) {
int idx = getRefFileIndexFromId((String) cboFiles.getSelectionModel().getSelectedItem(), lstAnnotationFiles);
AnnotationFileInfo afi = lstAnnotationFiles.get(idx);
if(afi != null && afi.reference.equals(DataApp.RefType.Demo.name()))
demo = true;
}
}
if(demo) {
txtDesign.setText(msgDemo);
txtMatrix.setText(msgDemo);
}
else {
if(txtDesign.getText().equals(msgDemo))
txtDesign.setText("");
if(txtMatrix.getText().equals(msgDemo))
txtMatrix.setText("");
}
txtDesign.setDisable(demo);
btnDesign.setDisable(demo);
txtMatrix.setDisable(demo);
btnMatrix.setDisable(demo);
cboExperiment.setDisable(demo);
}
private void onFiltersChanged() {
// must use getSelectedIndex - called from changed listener
ArrayList<String> lst = new ArrayList<>();
if(cboFilters.getSelectionModel().getSelectedIndex() != -1) {
int idx = Params.getFilterListIndexByName((String)cboFilters.getItems().get(cboFilters.getSelectionModel().getSelectedIndex()));
Params.FilterList fl = Params.FilterList.valueOf(Params.lstFilterLists.get(idx).id);
txtList.setDisable(fl.equals(Params.FilterList.NONE));
btnList.setDisable(fl.equals(Params.FilterList.NONE));
}
}
private HashMap<String, String> validate(Dialog dialog) {
HashMap<String, String> results = new HashMap<>();
// pass project id back if given
if(!newProject && !projectId.isEmpty())
results.put(DlgOpenProject.Params.ID_PARAM, projectId);
// check project selection
String errmsg = "";
int sidx = cboSpecies.getSelectionModel().getSelectedIndex();
if(sidx != -1) {
results.put(Params.VER_PARAM, "" + Params.PARAM_VER);
// save species name
String species = lstSpecies.get(sidx).trim();
String[] fields = species.split(" ");
results.put(Params.GENUS_PARAM, fields[0]);
results.put(Params.SPECIES_PARAM, fields[1]);
String txt = txtProject.getText().trim();
if(!txt.isEmpty()) {
if(newProject) {
String name = txt.toLowerCase();
ArrayList<Project.ProjectDef> lstProjects = app.getProjectsList();
for(Project.ProjectDef def : lstProjects) {
if(name.equals(def.name.toLowerCase())) {
errmsg = "Specified project's name is already in use.";
break;
}
}
}
results.put(DlgOpenProject.Params.NAME_PARAM, txt);
}
else {
if(newProject) {
errmsg = "You must enter the name for the project.";
txtProject.requestFocus();
}
}
if(errmsg.isEmpty()) {
int didx = cboExperiment.getSelectionModel().getSelectedIndex();
if(didx != -1) {
// lstExperiments MUST be in same order as display
DataApp.EnumData ed = DataApp.lstExperiments.get(didx);
results.put(Params.EXPTYPE_PARAM, ed.id);
DataApp.ExperimentType et = DataApp.ExperimentType.valueOf(ed.id);
// check file paths
String inpAnnot;
boolean chkMatrix = true;
String expDesign = txtDesign.getText().trim();
String inpMatrix = txtMatrix.getText().trim();
if(rbUserFile.isSelected()) {
results.put(Params.USEAPPREF_PARAM, "false");
inpAnnot = txtAnnot.getText().trim();
errmsg = checkAnnotFile(inpAnnot);
if(!errmsg.isEmpty())
txtAnnot.requestFocus();
}
else {
// need to get cboFiles setting and process accordingly - may require a download
int cboidx = cboFiles.getSelectionModel().getSelectedIndex();
int idx = getRefFileIndexFromId((String) cboFiles.getItems().get(cboidx), lstAnnotationFiles);
AnnotationFileInfo afi = lstAnnotationFiles.get(idx);
results.put(Params.USEAPPREF_PARAM, "true");
results.put(Params.REFTYPE_PARAM, afi.reference);
results.put(Params.REFFILE_PARAM, afi.toString());
results.put(Params.REFREL_PARAM, afi.release);
inpAnnot = "";
if(afi.reference.equals(DataApp.RefType.Demo.name())) {
expDesign = "";
inpMatrix = "";
chkMatrix = false;
}
}
if(errmsg.isEmpty() && chkMatrix) {
errmsg = checkExperimentData(et, expDesign, inpMatrix, results, logger);
if(!errmsg.isEmpty())
txtMatrix.requestFocus();
}
if(errmsg.isEmpty()) {
results.put(Params.EDFILE_PARAM, expDesign);
results.put(Params.EMFILE_PARAM, inpMatrix);
results.put(Params.AFILE_PARAM, inpAnnot);
if(errmsg.isEmpty()) {
if(chkFilter.isSelected()) {
results.put(Params.FILTER_PARAM, Boolean.TRUE.toString());
txt = txtFilterValues.getText().trim();
if(txt.length() > 0) {
try {
Double val = Double.parseDouble(txt);
if(val >= Params.MIN_EMFVAL && val <= Params.MAX_EMFVAL) {
results.put(Params.EMFVAL_PARAM, txt);
}
else {
errmsg = "Invalid level cutoff value entered (" + Params.MIN_EMFVAL + " to " + Params.MAX_EMFVAL + " allowed).";
txtFilterValues.requestFocus();
}
} catch(Exception e) {
errmsg = "Invalid level cutoff value number entered.";
txtFilterValues.requestFocus();
}
}
else {
errmsg = "You must enter a level cutoff value.";
txtFilterValues.requestFocus();
}
}
else
results.put(Params.FILTER_PARAM, Boolean.FALSE.toString());
if(chkNorm.isSelected())
results.put(Params.NORM_PARAM, Boolean.TRUE.toString());
else
results.put(Params.NORM_PARAM, Boolean.FALSE.toString());
}
if(errmsg.isEmpty()) {
if(chkFilter.isSelected()) {
txt = txtFilterCOV.getText().trim();
if(txt.length() > 0) {
try {
Double val = Double.parseDouble(txt);
if(val >= Params.MIN_EMFCOV && val <= Params.MAX_EMFCOV) {
results.put(Params.EMFCOV_PARAM, txt);
}
else {
errmsg = "Invalid COV cutoff value entered (" + Params.MIN_EMFCOV + " to " + Params.MAX_EMFCOV + " allowed).";
txtFilterCOV.requestFocus();
}
} catch(Exception e) {
errmsg = "Invalid COV cutoff value number entered.";
txtFilterCOV.requestFocus();
}
}
else {
errmsg = "You must enter a COV cutoff value.";
txtFilterCOV.requestFocus();
}
}
}
if(!errmsg.isEmpty())
results.put("ERRMSG", errmsg);
}
else
results.put("ERRMSG", errmsg);
}
else {
errmsg = "You must select an experiment type.";
cboExperiment.requestFocus();
results.put("ERRMSG", errmsg);
}
}
else
results.put("ERRMSG", errmsg);
if(errmsg.isEmpty()) {
// get filter type
if(cboFilters.getSelectionModel().getSelectedIndex() != -1) {
int idx = Params.getFilterListIndexByName((String)cboFilters.getSelectionModel().getSelectedItem());
Params.FilterList fl = Params.FilterList.valueOf(Params.lstFilterLists.get(idx).id);
results.put(Params.FILTERLIST_PARAM, fl.name());
if(!fl.equals(Params.FilterList.NONE)) {
String filepath = txtList.getText().trim();
errmsg = checkListFile(filepath, results);
if(errmsg.isEmpty())
results.put(Params.LISTFILE_PARAM, filepath);
else
txtList.requestFocus();
}
}
else {
errmsg = "You must select a transcript filtering option.";
cboFilters.requestFocus();
}
if(!errmsg.isEmpty())
results.put("ERRMSG", errmsg);
}
}
else {
cboSpecies.requestFocus();
errmsg = "You must select a species.";
results.put("ERRMSG", errmsg);
}
return results;
}
// We want to provide meaningful messages to the user - so there is a lot of checking taking place
public String checkExperimentData(DataApp.ExperimentType experiment, String designPath, String matrixPath, HashMap<String, String> hmResults, Logger logger) {
DesignResults dr = checkDesignFile(experiment, designPath, hmResults, logger);
String errmsg = dr.errmsg;
if(errmsg.isEmpty())
errmsg = checkExpMatrixFile(experiment, dr.lstCols, matrixPath, hmResults, logger);
if(!errmsg.isEmpty())
app.logInfo("Experiment design/matrix file check failed: " + errmsg);
return errmsg;
}
// NOTE: No comment lines allowed in design files
public DesignResults checkDesignFile(DataApp.ExperimentType experimentType, String filepath, HashMap<String, String> hmResults, Logger logger) {
DesignResults results = new DesignResults();
String errmsg = "";
HashMap<String, Object> hmColNames = new HashMap<>();
ArrayList<String> lstCols = new ArrayList<>();
ArrayList<Params.ExpMatrixGroup> lstGroups = new ArrayList<>();
HashMap<String, HashMap<String, HashMap<String, Object>>> hmGroups = new HashMap<>();
logger.logInfo("Checking design file: " + filepath);
DlgInputData.Params.ExperimentLimits lims = new DlgInputData.Params.ExperimentLimits(experimentType);
try {
if(!filepath.isEmpty()) {
File f = new File(filepath);
long size = f.length();
double curTime = Double.MIN_VALUE;
String curGroup = "";
if(size >= Params.MIN_DESIGN_FILE_SIZE && size <= Params.MAX_DESIGN_FILE_SIZE) {
if(f.canRead()) {
List<String> lines = Files.readAllLines(Paths.get(filepath), StandardCharsets.UTF_8);
int lnum = 1;
int colcnt = experimentType.equals(DataApp.ExperimentType.Two_Group_Comparison)? 2 : 3;
for(String line : lines) {
// process if past header line
if(lnum++ > 1) {
if(!line.trim().isEmpty()) {
// break column name into sample name and experimental group - will return null if not a valid column name
Params.ColumnInfo ci = Params.ColumnInfo.fromName(line, experimentType);
if(ci != null) {
// dashes are not allowed in column names, R will convert to periods
if(!ci.sample.contains("-")) {
// all columns must have unique names to avoid issues
if(!hmColNames.containsKey(ci.sample)) {
// check if this is a new group
if(!ci.group.equals(curGroup)) {
// check that we have not processed it before
// all entries for a group must be continous
if(hmGroups.containsKey(ci.group)) {
errmsg = "Samples in group, '" + ci.group + "', not continous, see Help.";
break;
}
curGroup = ci.group;
curTime = Integer.valueOf(ci.time);
}
else {
double time = Double.parseDouble(ci.time);
if(time < curTime) {
errmsg = "Sample times in group, '" + ci.group + "', not in chronological order, see Help.";
break;
}
curTime = time;
}
lstCols.add(ci.sample);
hmColNames.put(ci.sample, null);
if(hmGroups.containsKey(ci.group)) {
HashMap<String, HashMap<String, Object>> hmTimes = hmGroups.get(ci.group);
if(hmTimes.containsKey(ci.time)) {
HashMap<String, Object> hm = hmTimes.get(ci.time);
if(!hm.containsKey(ci.sample)) {
hm.put(ci.sample, null);
for(Params.ExpMatrixGroup ct : lstGroups) {
if(ct.name.equals(ci.group)) {
for(Params.ExpMatrixTime ts : ct.lstTimes) {
if(ts.name.equals(ci.time)) {
ts.addSample(ci.sample);
break;
}
}
break;
}
}
}
else {
errmsg = "Duplicate sample name, '" + ci.sample + "', within the same group (" + ci.group + ").";
break;
}
}
else {
// add new time with sample
HashMap<String, Object> hmSamples = new HashMap<>();
hmSamples.put(ci.sample, null);
hmTimes.put(ci.time, hmSamples);
Params.ExpMatrixTime ts = new Params.ExpMatrixTime(ci.time);
ts.addSample(ci.sample);
for(Params.ExpMatrixGroup ct : lstGroups) {
if(ct.name.equals(ci.group)) {
ct.addTime(ts);
break;
}
}
}
}
else {
// add new condition with time and sample
HashMap<String, Object> hmSamples = new HashMap<>();
hmSamples.put(ci.sample, null);
HashMap<String, HashMap<String, Object>> hmTimes = new HashMap<>();
hmTimes.put(ci.time, hmSamples);
hmGroups.put(ci.group, hmTimes);
Params.ExpMatrixTime ts = new Params.ExpMatrixTime(ci.time);
ts.addSample(ci.sample);
Params.ExpMatrixGroup ct = new Params.ExpMatrixGroup(ci.group);
ct.addTime(ts);
lstGroups.add(ct);
}
}
else {
errmsg = "Duplicate column names, " + ci.sample + ", are not allowed.";
break;
}
}
else {
errmsg = "Sample column names, " + ci.sample + ", can not contain dashes, see Help.";
break;
}
}
else {
errmsg = "Invalid line contents: '" + line + "'";
break;
}
}
}
else {
// just check for the right number of columns
String[] fields = line.split("\t");
if(fields.length != colcnt) {
errmsg = "Design file header must contain " + colcnt + " columns.";
break;
}
}
}
// check contents if no errors so far
if(errmsg.isEmpty()) {
// check for allowed number of conditions
int cnt = hmGroups.size();
if(cnt < lims.minGroups || cnt > lims.maxGroups) {
if(lims.minGroups == lims.maxGroups)
errmsg = "Invalid number of experimental groups, " + cnt + ". Must contain " + lims.minGroups + " experimental groups.";
else
errmsg = "Invalid number of experimental groups, " + cnt + ". Valid range is from " + lims.minGroups + " to " + lims.maxGroups + " experimental groups.";
}
else {
// check for min samples per time slot, total time slots, and get total samples count
int totalSamples = 0;
HashMap<String, Integer> hmTimeCounts = new HashMap<>();
for(String group : hmGroups.keySet()) {
HashMap<String, HashMap<String, Object>> hmTimes = hmGroups.get(group);
for(String time : hmTimes.keySet()) {
if(hmTimeCounts.containsKey(time))
hmTimeCounts.put(time, hmTimeCounts.get(time) + hmTimes.size());
else
hmTimeCounts.put(time, hmTimes.size());
int tcnt = hmTimes.get(time).size();
if(tcnt < lims.minTimeSamples) {
if(experimentType.equals(DataApp.ExperimentType.Two_Group_Comparison))
errmsg = "Invalid number of samples, " + tcnt + ", per group. Must have at least " + lims.minTimeSamples + " per group.";
else
errmsg = "Invalid number of samples, " + tcnt + ", per time slot. Must have at least " + lims.minTimeSamples + " per time slot.";
break;
}
totalSamples += tcnt;
}
if(!errmsg.isEmpty())
break;
}
if(errmsg.isEmpty()) {
cnt = hmTimeCounts.size();
if(cnt < lims.minTimes || cnt > lims.maxTimes) {
if(lims.minTimes == lims.maxTimes)
errmsg = "Invalid number of time slots, " + cnt + ". Must contain " + lims.minTimes + " time slot(s).";
else
errmsg = "Invalid number of time slots, " + cnt + ". Valid range is from " + lims.minTimes + " to " + lims.maxTimes + " time slots.";
}
else {
// ony check the max total limit exceeded - min is checked per time slot
cnt = totalSamples;
if(cnt > lims.maxTotalSamples)
errmsg = "Invalid number of total samples, " + cnt + ". Up to " + lims.maxTotalSamples + " total samples allowed.";
else {
// check that we have at least some shared counts
boolean shared = false;
for(int tc : hmTimeCounts.values()) {
if(tc > 1) {
shared = true;
break;
}
}
if(!shared)
errmsg = "There are no shared time slots across samples.";
}
}
}
if(errmsg.isEmpty()) {
// all checking is done, make sure we have at least one shared time slot
for(String cond : hmGroups.keySet()) {
HashMap<String, HashMap<String, Object>> hmTimes = hmGroups.get(cond);
int times = hmTimes.size();
if(times < lims.minTimes || times > lims.maxTimes) {
errmsg = "Experimental group '" + cond + "' contains invalid number of time slots. Valid range is from " + lims.minTimes + " to " + lims.maxTimes + " time slots.";
break;
}
}
}
// save results if no errors
if(errmsg.isEmpty()) {
int c = 1;
for(Params.ExpMatrixGroup ct : lstGroups) {
String cond = ct.name;
hmResults.put(DlgInputData.Params.GROUP_PARAM + c, cond);
int t = 1;
for(Params.ExpMatrixTime ts : ct.lstTimes) {
String time = ts.name;
hmResults.put(DlgInputData.Params.CTIMES_PREFIX + c + DlgInputData.Params.CTIME_PARAM + t, time);
String samples = "";
for(String sample : ts.lstSampleNames)
samples += (samples.isEmpty()? "" : "\t") + sample;
hmResults.put(DlgInputData.Params.CTSAMPLES_PREFIX1 + c + DlgInputData.Params.CTSAMPLES_PREFIX2 + t + DlgInputData.Params.CTSAMPLES_PARAM, samples);
t++;
}
c++;
}
}
}
}
}
else
errmsg = "Matrix file does not have read access. Check file permissions.";
}
else {
if(size > Params.MAX_DESIGN_FILE_SIZE)
errmsg = "Design file exceeds maximum size allowed of " + (Params.MAX_DESIGN_FILE_SIZE/1000) + "KB";
else
errmsg = "Design file does not have sufficient data (" + size + "bytes).";
}
}
else
errmsg = "You must specify the design file's location.";
} catch(Exception e) {
errmsg = "Unable to process specified design file: " + e.getMessage();
}
results.errmsg = errmsg;
if(errmsg.isEmpty()) {
results.lstCols = lstCols;
results.lstGroups = lstGroups;
results.hmGroups = hmGroups;
app.logInfo("Design file passed initial check.");
}
return results;
}
// this is checking the expression matrix provided by the user
// it is possible that we are only using a subset of the groups and/or samples
// so we just need to make sure that all the specified samples are present
public String checkExpMatrixFile(DataApp.ExperimentType experiment, ArrayList<String> lstCols,
String path, HashMap<String, String> hmResults, Logger logger) {
String errmsg = "";
logger.logInfo("Checking specified matrix file (" + experiment.name() + "): " + path);
DlgInputData.Params.ExperimentLimits lims = new DlgInputData.Params.ExperimentLimits(experiment);
if(!path.isEmpty()) {
try {
File f = new File(path);
long size = f.length();
if(size >= Params.MIN_MATRIX_FILE_SIZE && size <= Params.MAX_MATRIX_FILE_SIZE) {
if(f.canRead()) {
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line = br.readLine();
if(line != null) {
// allow having a # at start of line
if(line.startsWith("#"))
line = line.substring(1);
line = line.trim();
// parse into column fields
String[] fields = line.split("\t");
int fldcnt = fields.length;
if(fldcnt > 0) {
// check that rows match header - may not have the row id column header
int cnt1 = br.readLine().split("\t").length;
int cnt2 = br.readLine().split("\t").length;
if(cnt1 == cnt2 && (cnt1 == fldcnt || cnt1 == (fldcnt + 1))) {
// create a list of columns in expression matrix
int stridx = (cnt1 == fldcnt)? 1 : 0;
ArrayList<String> lstMatrix = new ArrayList<>();
for(int i = stridx; i < fldcnt; i++)
lstMatrix.add(fields[i]);
// make sure all the columns specified in design file are present
if(lstMatrix.size() >= lstCols.size()) {
for(int i = 0; i < lstCols.size(); i++) {
if(lstMatrix.indexOf(lstCols.get(i)) == -1) {
errmsg = "Column name specified in design file, " + lstCols.get(i) + ", not found in the matrix file.";
break;
}
}
}
else
errmsg = "The number of columns specified in the design file, " + lstCols.size() + ", is greater than the number of columns in the matrix file, " + lstMatrix.size() + ".";
}
else
errmsg = "Number of columns in matrix file header and data rows do not match.";
}
else
errmsg = "Expression matrix must contain some data columns.";
}
else
errmsg = "Unable to read expression matrix file data.";
}
}
else
errmsg = "Matrix file does not have read access. Check file permissions.";
}
else {
if(size > Params.MAX_MATRIX_FILE_SIZE)
errmsg = "Matrix file exceeds maximum size allowed of " + (Params.MAX_MATRIX_FILE_SIZE/1000000) + "MB";
else
errmsg = "Matrix file does not have sufficient data (" + size + "bytes).";
}
} catch(Exception e) {
errmsg = "Unable to open specified expression matrix file.";
}
}
else
errmsg = "You must specify the expression matrix file's location.";
if(!errmsg.isEmpty())
app.logInfo("Matrix file check failed: " + errmsg);
else
app.logInfo("Matrix file passed initial check.");
return errmsg;
}
private String checkAnnotFile(String path) {
String errmsg = "";
app.logInfo("Checking specified annotation file: " + path);
if(path.length() > 0) {
try {
int x = 2;
} catch(Exception e) {
errmsg = "Unable to open specified annotation file.";
}
}
else
errmsg = "You must specify the annotation file's location or, if provided, use one of the application files.";
return errmsg;
}
// We want to provide meaningful messages to the user - so there is a lot of checking taking place
private String checkListFile(String path, HashMap<String, String> results) {
String errmsg = "";
if(path.length() > 0) {
try {
File f = new File(path);
if(f.exists()) {
long size = f.length();
if(size >= Params.MIN_LIST_FILE_SIZE && size <= Params.MAX_LIST_FILE_SIZE) {
if(f.canRead()) {
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line = br.readLine();
if(line == null)
errmsg = "Unable to read list file data.";
}
}
else
errmsg = "List file does not have read access. Check file permissions.";
}
else {
if(size > Params.MAX_LIST_FILE_SIZE)
errmsg = "List file exceeds maximum size allowed of " + (Params.MAX_LIST_FILE_SIZE/1000000) + "MB";
else
errmsg = "List file does not have sufficient data (" + size + "bytes).";
}
}
else
errmsg = "Unable to find specified list file.";
} catch(Exception e) {
errmsg = "Unable to open specified list file.";
}
}
else
errmsg = "You must specify the list file's location.";
return errmsg;
}
private void getListFile() {
File f = getListName("Select Transcripts Filter List File", new FileChooser.ExtensionFilter("List files", "*.txt", "*.tsv"));
if(f != null){
app.userPrefs.setImportListFolder(f.getParent());
txtList.setText(f.getPath());
}
}
private void getDesignFile() {
File f = getDataName("Select Experiment Design File", new FileChooser.ExtensionFilter("Experiment design files", "*.txt", "*.tsv"));
if(f != null){
app.userPrefs.setImportDataFolder(f.getParent());
txtDesign.setText(f.getPath());
}
}
private void getMatrixFile() {
File f = getDataName("Select Expression Matrix File", new FileChooser.ExtensionFilter("Expression Matrix files", "*.txt", "*.tsv"));
if(f != null){
app.userPrefs.setImportDataFolder(f.getParent());
txtMatrix.setText(f.getPath());
}
}
private void getAnnotFile() {
File f = getAnnotName("Select Annotation Data File", new FileChooser.ExtensionFilter("Annotation files", "*.txt", "*.tsv", "*.gff", "*.gff3"));
if(f != null){
app.userPrefs.setImportAnnotFolder(f.getParent());
txtAnnot.setText(f.getPath());
}
}
private File getListName(String title, FileChooser.ExtensionFilter ef) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(title);
File f = new File(app.userPrefs.getImportListFolder());
if(f.exists()) {
fileChooser.setInitialDirectory(f);
}
fileChooser.getExtensionFilters().addAll(ef, new FileChooser.ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(dialog.getOwner());
return selectedFile;
}
private File getDataName(String title, FileChooser.ExtensionFilter ef) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(title);
File f = new File(app.userPrefs.getImportDataFolder());
if(f.exists()) {
fileChooser.setInitialDirectory(f);
}
fileChooser.getExtensionFilters().addAll(ef, new FileChooser.ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(dialog.getOwner());
return selectedFile;
}
private File getAnnotName(String title, FileChooser.ExtensionFilter ef) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(title);
File f = new File(app.userPrefs.getImportAnnotFolder());
if(f.exists()) {
fileChooser.setInitialDirectory(f);
}
fileChooser.getExtensionFilters().addAll(ef, new FileChooser.ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(dialog.getOwner());
return selectedFile;
}
private int getSpeciesIndex(String genus, String species) {
int idx = 0;
String fullname = genus + " " + species;
for(String name : lstSpecies) {
if(name.equals(fullname))
break;
idx++;
}
if(idx >= lstSpecies.size())
idx = 0;
return idx;
}
private static int getRefFileIndexFromId(String id, ArrayList<AnnotationFileInfo> files) {
int idx = 0;
if(id != null && !id.isEmpty()) {
String[] fields = id.split(" ");
if(fields.length == 4) {
AnnotationFileInfo afid = new AnnotationFileInfo(fields[0], fields[1], fields[2], fields[3], "", false);
for(AnnotationFileInfo afi : files) {
if(afi.compare(afid))
break;
idx++;
}
if(idx >= files.size())
idx = 0;
}
}
return idx;
}
private static int getExpTypeIndexFromId(String id, List<DataApp.EnumData> lstExperiments) {
int idx = 0;
for(DataApp.EnumData ed : lstExperiments) {
if(ed.id.equals(id))
break;
idx++;
}
if(idx >= lstExperiments.size())
idx = 0;
return idx;
}
//
// Data Classes
//
public static class DesignResults {
String errmsg;
ArrayList<String> lstCols;
ArrayList<Params.ExpMatrixGroup> lstGroups;
HashMap<String, HashMap<String, HashMap<String, Object>>> hmGroups;