-
Notifications
You must be signed in to change notification settings - Fork 2
/
GOGraph.java
252 lines (241 loc) · 9.91 KB
/
GOGraph.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
/*
* 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.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author Hector del Risco - [email protected] & Pedro Salguero - [email protected]
*/
public class GOGraph extends AppObject {
AnchorPane paneGO;
Pane paneApply, paneControls;
ToggleButton tbBP, tbMF, tbCC;
Slider sliderLevels;
WebView webGraph;
WebEngine engine;
ChoiceBox cbTerms, cbLevels;
SubTabBase subTabBase;
String parentID;
ArrayList<String> lstDataSelections = new ArrayList<>();
ArrayList<String> lstGOTerms = new ArrayList<>();
GO.GOCat catSel = GO.GOCat.BP;
GO.Show show = GO.Show.Name;
boolean bpData = false;
boolean mfData = false;
boolean ccData = false;
boolean updatingFlag = true;
String selectedData = "";
double tpWidth = 0.0;
double tpHeight = 0.0;
GO go = new GO();
public GOGraph() {
super(null, null);
}
public void initialize(SubTabBase subTabBase, String parentID) {
this.subTabBase = subTabBase;
this.parentID = parentID;
}
public void setupControls(AnchorPane paneGO, double tpWidth, double tpHeight, ArrayList<String> lstDataSelections) {
this.paneGO = paneGO;
this.tpWidth = tpWidth;
this.tpHeight = tpHeight;
this.lstDataSelections = lstDataSelections;
try {
Node tabNode = (Node) FXMLLoader.load(app.ctlr.fxdc.getClass().getResource("/tappas/tabs/GOGraph.fxml"));
paneGO.getChildren().clear();
paneGO.getChildren().add(tabNode);
AnchorPane.setTopAnchor(tabNode, 0.0);
AnchorPane.setBottomAnchor(tabNode, 0.0);
AnchorPane.setLeftAnchor(tabNode, 0.0);
AnchorPane.setRightAnchor(tabNode, 0.0);
// get control objects
paneControls = (Pane) tabNode.lookup("#paneGO_Controls");
cbTerms = (ChoiceBox) tabNode.lookup("#cbGO_Terms");
tbBP = (ToggleButton) tabNode.lookup("#tbGO_BP");
tbMF = (ToggleButton) tabNode.lookup("#tbGO_MF");
tbCC = (ToggleButton) tabNode.lookup("#tbGO_CC");
cbLevels = (ChoiceBox) tabNode.lookup("#cbGO_Levels");
webGraph = (WebView) tabNode.lookup("#webGO_DAG");
// setup listeners and event handlers
cbTerms.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
selectedData = (String) cbTerms.getItems().get((int)newValue);
updateData();
setGODataFlags();
if(!updatingFlag)
updatePage();
});
tbBP.setOnAction((itemEvent) -> { if(tbBP.isSelected()) catSel = GO.GOCat.BP; if(!updatingFlag) updatePage();});// disableBtnApply(getCategory() == null);});
tbMF.setOnAction((itemEvent) -> { if(tbMF.isSelected()) catSel = GO.GOCat.MF; if(!updatingFlag) updatePage();});// disableBtnApply(getCategory() == null);});
tbCC.setOnAction((itemEvent) -> { if(tbCC.isSelected()) catSel = GO.GOCat.CC; if(!updatingFlag) updatePage();});// disableBtnApply(getCategory() == null);});
cbLevels.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
if(!updatingFlag)
updatePage();
});
// setup web objects
webGraph.setContextMenuEnabled(false);
engine = webGraph.getEngine();
} catch(Exception e) {
app.logError("Unable to create GO graph display: " + e.getMessage());
}
}
public void updateDataSelections(ArrayList<String> lstDataSelections) {
this.lstDataSelections = lstDataSelections;
}
public void updateShowGraph(GO.Show show) {
this.show = show;
showGraph(false);
}
public void showGraph(boolean populated) {
if(!populated) {
// set first time only
updatingFlag = true;
if(cbTerms.getItems().size() == 0) {
for(String sel : lstDataSelections)
cbTerms.getItems().add(sel);
cbTerms.getSelectionModel().select(0);
updateData();
setGODataFlags();
if(getCategory() == null) {
app.ctls.alertInformation("No GO Terms", "There are no GO Terms for specified data");
paneControls.setDisable(true);
engine.loadContent("<htm><body></body></html>");
return;
}
}
else
updateData();
updateGraph(true);
updatingFlag = false;
}
}
public void updateGraph(boolean alert) {
// build graph html file
String filepath = Paths.get(app.data.getAppDataFolder(), "GODAG_" + parentID + ".html").toString();
int selLevel = 0;
if(cbLevels.getItems().size() > 0)
selLevel = cbLevels.getSelectionModel().getSelectedIndex();
int maxLevel = (selLevel == 0)? 2 : ((selLevel == 1)? 999 : selLevel - 1);
double width = paneGO.getWidth();
double height = paneGO.getHeight();
width = (width == 0.0)? tpWidth : width;
height = (height == 0.0)? tpHeight : height;
GO.GraphResult result = go.buildGraph(getCategory(), maxLevel, lstGOTerms, width, height, show);
if(result.maxLevel > 0) {
cbLevels.getItems().clear();
cbLevels.getItems().add("Low level graph");
cbLevels.getItems().add("Full graph");
for(int i = 1; i <= result.maxLevel; i++)
cbLevels.getItems().add("Up to level " + i);
if((selLevel - 1) > result.maxLevel)
cbLevels.getSelectionModel().select(1);
else
cbLevels.getSelectionModel().select(selLevel);
//System.out.println("Max DAG level: " + result.maxLevel);
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath, false), "utf-8"));
writer.write(result.html);
} catch(Exception e) {
app.logError("Unable to write GO DAG html file: " + e.getMessage());
} finally {
try { if(writer != null) writer.close();} catch (Exception e) { System.out.println(e.getClass().getName() + " Close writer code exception: " + e.getMessage()); }
}
String url;
if(Utils.isWindowsOS()) {
// need an additional slash when using drive letters
url = "/" + filepath.replaceAll("\\\\", "/");
logger.logInfo("fp: " + url);
}
else
url = filepath;
engine.load("file://" + url);
}
else {
app.ctls.alertInformation("No GO Terms", "There are no GO Terms for specified data");
engine.loadContent("<htm><body></body></html>");
}
}
//
// Internal Functions
//
private void setGODataFlags() {
double width = paneGO.getWidth();
double height = paneGO.getHeight();
width = (width == 0.0)? tpWidth : width;
height = (height == 0.0)? tpHeight : height;
// determine what GO categories are available (BP, MF, CC) and setup UI accordingly
GO.GraphResult result = go.buildGraph(GO.GOCat.BP, 1, lstGOTerms, width, height, show);
bpData = result.bp;
result = go.buildGraph(GO.GOCat.MF, 1, lstGOTerms, width, height, show);
mfData = result.mf;
result = go.buildGraph(GO.GOCat.CC, 1, lstGOTerms, width, height, show);
ccData = result.cc;
tbBP.setDisable(!bpData);
tbMF.setDisable(!mfData);
tbCC.setDisable(!ccData);
if(!tbBP.isSelected() && !tbMF.isSelected() && !tbCC.isSelected())
tbBP.setSelected(true);
if(tbBP.isSelected() && !bpData) {
tbBP.setSelected(false);
if(mfData)
tbMF.setSelected(true);
else if(ccData)
tbCC.setSelected(true);
}
if(tbMF.isSelected() && !mfData) {
tbMF.setSelected(false);
if(bpData)
tbBP.setSelected(true);
else if(ccData)
tbCC.setSelected(true);
}
if(tbCC.isSelected() && !ccData) {
tbCC.setSelected(false);
if(bpData)
tbBP.setSelected(true);
else if(mfData)
tbMF.setSelected(true);
}
}
private void updatePage() {
updateData();
showGraph(false);
}
private GO.GOCat getCategory() {
GO.GOCat cat = null;
if(tbBP.isSelected())
cat = GO.GOCat.BP;
else if(tbMF.isSelected())
cat = GO.GOCat.MF;
else if(tbCC.isSelected())
cat = GO.GOCat.CC;
return cat;
}
private void updateData() {
lstGOTerms.clear();
HashMap<String, Object> hm = new HashMap<>();
if(cbTerms.getSelectionModel().getSelectedItem() != null) {
hm.put("getData", selectedData);
lstGOTerms = (ArrayList<String>) subTabBase.processRequest(hm);
}
}
}