/
EnrichmentTableModel.java
361 lines (329 loc) · 14.6 KB
/
EnrichmentTableModel.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
package org.nrnb.gsoc.enrichment.ui;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.nrnb.gsoc.enrichment.model.EnrichmentTerm;
import org.nrnb.gsoc.enrichment.model.EnrichmentTerm.TermSource;
import javax.swing.table.AbstractTableModel;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author ighosh98
* @description This class defines how the data fetched from the API request will be stored and displayed to the customer
*/
public class EnrichmentTableModel extends AbstractTableModel {
private String[] columnNames;
private CyTable cyTable;
private Long[] rowNames;
public EnrichmentTableModel(CyTable cyTable, String[] columnNames) {
this.columnNames = columnNames;
this.cyTable = cyTable;
initData();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rowNames.length;
}
public int getAllRowCount() {
return cyTable.getRowCount();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Long[] getRowNames() {
return rowNames;
}
/**
* @description Fetch the value with a particular column name as an enum for a specific row
* @param row
* @param col
* @return return value at the particular row and column of the enrichment table
*/
public Object getValueAt(int row, int col) {
if (getRowCount() == 0) return null;
final String colName = columnNames[col];
final Long rowName = rowNames[row];
if (colName.equals(EnrichmentTerm.colEffectiveDomainSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colIntersectionSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colQuerySize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colRecall)) {
return cyTable.getRow(rowName).get(colName, Double.class);
} else if (colName.equals(EnrichmentTerm.colGoshv)) {
return cyTable.getRow(rowName).get(colName, Double.class);
} else if (colName.equals(EnrichmentTerm.colTermID)) {
return cyTable.getRow(rowName).get(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colGroupID)) {
return cyTable.getRow(rowName).get(colName, Long.class);
} else if (colName.equals(EnrichmentTerm.colTermSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colGenes)) {
return cyTable.getRow(rowName).getList(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colSource)) {
return cyTable.getRow(rowName).get(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colID)) {
return cyTable.getRow(rowName).get(colName, Long.class);
} else if (colName.equals(EnrichmentTerm.colPvalue)) {
return cyTable.getRow(rowName).get(colName, String.class);
}
else if(colName.equals(EnrichmentTerm.colPrecision)){
return cyTable.getRow(rowName).get(colName, Double.class);
}
else if (colName.equals(EnrichmentTerm.colIsSignificant)) {
return cyTable.getRow(rowName).getList(colName, Boolean.class);
}
else if (colName.equals(EnrichmentTerm.colGenesSUID)) {
return cyTable.getRow(rowName).getList(colName, Long.class);
}
else if (colName.equals(EnrichmentTerm.colGenesEvidenceCode)) {
return cyTable.getRow(rowName).getList(colName, String.class);
} else {
return cyTable.getRow(rowName).get(colName, String.class);
}
}
/**
* @description Fetch the value with a particular column name as a string for a specific row
* @param row
* @param colName
* @return
*/
public Object getValueAt(int row, String colName) {
final Long rowName = rowNames[row];
if (colName.equals(EnrichmentTerm.colEffectiveDomainSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colIntersectionSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colQuerySize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colSource)) {
return cyTable.getRow(rowName).get(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colRecall)) {
return cyTable.getRow(rowName).get(colName, Double.class);
} else if (colName.equals(EnrichmentTerm.colGoshv)) {
return cyTable.getRow(rowName).get(colName, Double.class);
} else if(colName.equals(EnrichmentTerm.colID)) {
return cyTable.getRow(rowName).get(colName, Long.class);
} else if (colName.equals(EnrichmentTerm.colTermID)) {
return cyTable.getRow(rowName).get(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colGroupID)) {
return cyTable.getRow(rowName).get(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colTermSize)) {
return cyTable.getRow(rowName).get(colName, Integer.class);
} else if (colName.equals(EnrichmentTerm.colGenes)) {
return cyTable.getRow(rowName).getList(colName, String.class);
} else if (colName.equals(EnrichmentTerm.colIsSignificant)) {
return cyTable.getRow(rowName).getList(colName, Boolean.class);
} else if(colName.equals(EnrichmentTerm.colIntersectionSize)){
return cyTable.getRow(rowName).get(colName, Integer.class);
}
else if(colName.equals(EnrichmentTerm.colTermSize)){
return cyTable.getRow(rowName).get(colName, Integer.class);
}
else if(colName.equals(EnrichmentTerm.colPrecision)){
return cyTable.getRow(rowName).get(colName, Double.class);
}
else if (colName.equals(EnrichmentTerm.colGenesSUID)) {
return cyTable.getRow(rowName).getList(colName, Long.class);
} else {
return cyTable.getRow(rowName).get(colName, String.class);
}
}
public Class<?> getColumnClass(int c) {
final String colName = columnNames[c];
if (colName.equals(EnrichmentTerm.colEffectiveDomainSize)) {
return Integer.class;
} else if (colName.equals(EnrichmentTerm.colIntersectionSize)) {
return Integer.class;
} else if (colName.equals(EnrichmentTerm.colQuerySize)) {
return Integer.class;
} else if (colName.equals(EnrichmentTerm.colSource)) {
return String.class;
} else if (colName.equals(EnrichmentTerm.colRecall)) {
return Double.class;
} else if (colName.equals(EnrichmentTerm.colGoshv)) {
return Double.class;
} else if (colName.equals(EnrichmentTerm.colID)) {
return Long.class;
} else if (colName.equals(EnrichmentTerm.colTermID)) {
return String.class;
} else if (colName.equals(EnrichmentTerm.colGroupID)) {
return String.class;
} else if (colName.equals(EnrichmentTerm.colTermSize)) {
return Integer.class;
} else if (colName.equals(EnrichmentTerm.colGenes)) {
return String.class;
} else if (colName.equals(EnrichmentTerm.colIsSignificant)) {
return Boolean.class;
} else if(colName.equals(EnrichmentTerm.colIntersectionSize)){
return (Integer.class);
}
else if(colName.equals(EnrichmentTerm.colTermSize)){
return Integer.class;
}
else if(colName.equals(EnrichmentTerm.colPrecision)){
return Double.class;
}
else if (colName.equals(EnrichmentTerm.colGenesSUID)) {
return List.class;
}
else {
return String.class;
}
}
public boolean isCellEditable(int row, int col) {
return false;
}
// filter by source and nodeSUID
public void filterByNodeSUID(List<Long> nodesToFilterSUID, boolean annotateAllNodes,
List<TermSource> sources) {
// filter(sources);
filterByNodeSUID(nodesToFilterSUID, annotateAllNodes);
}
// Filter the table by node SUID
public void filterByNodeSUID(List<Long> nodesToFilterSUID, boolean annotateAllNodes) {
List<CyRow> rows = cyTable.getAllRows();
List<Long> shownRows = Arrays.asList(rowNames);
Long[] rowArray = new Long[rows.size()];
int i = 0;
for (CyRow row : rows) {
Long rowID = row.get(EnrichmentTerm.colID, Long.class);
if (!shownRows.contains(rowID)) {
continue;
}
List<Long> genesSUID = new ArrayList<Long>(row.getList(EnrichmentTerm.colGenesSUID, Long.class));
genesSUID.retainAll(nodesToFilterSUID);
if ((genesSUID.size() > 0 && !annotateAllNodes) ||
(annotateAllNodes && genesSUID.size() == nodesToFilterSUID.size())) {
rowArray[i] = rowID;
i++;
}
}
rowNames = Arrays.copyOf(rowArray, i);
fireTableDataChanged();
}
// Filter the table by souce
public void filter(List<TermSource> sources, List<String> evidenceList, boolean removeOverlapping,
double cutoff) {
filterBySource(sources);
int length = filterByEvidenceCode(evidenceList);
if (removeOverlapping && length > 0) {
rowNames = removeRedundancy(length, cutoff);
}
fireTableDataChanged();
}
private void filterBySource(List<TermSource> sources) {
List<CyRow> rows = cyTable.getAllRows();
Long[] rowArray = new Long[rows.size()];
int rowCount = 0;
for (CyRow row : rows) {
// implement this again
String termSource = row.get(EnrichmentTerm.colSource, String.class);
if (sources.isEmpty() || inSource(sources, termSource)) {
rowArray[rowCount] = row.get(EnrichmentTerm.colID, Long.class);
rowCount++;
}
}
rowNames = Arrays.copyOf(rowArray, rowCount);
}
public int filterByEvidenceCode(List<String> evidenceCodes) {
List<CyRow> rows = cyTable.getAllRows();
HashSet<Long> shownRows = new HashSet<>(Arrays.asList(rowNames));
Long[] rowArray = new Long[rows.size()];
int rowCount = 0;
for (CyRow row : rows) {
Long rowID = row.get(EnrichmentTerm.colID, Long.class);
if (!shownRows.contains(rowID)) {
continue;
}
List<String> termSource = row.get(EnrichmentTerm.colGenesEvidenceCode, List.class);
if (evidenceCodes.isEmpty() ||
evidenceCodes.stream().allMatch(e -> termSource.contains("\"" + e + "\""))) {
rowArray[rowCount++] = row.get(EnrichmentTerm.colID, Long.class);
}
}
rowNames = Arrays.copyOf(rowArray, rowCount);
fireTableDataChanged();
return rowCount;
}
private boolean inSource(List<TermSource> sources, String termName) {
for (TermSource ts: sources) {
if (ts.getName().equals(termName))
return true;
}
return false;
}
private Long[] removeRedundancy(int length, double cutoff) {
// Sort by pValue
Long[] sortedArray = pValueSort(rowNames, length);
// Initialize with the most significant term
List<Long> currentTerms = new ArrayList<Long>();
currentTerms.add(sortedArray[0]);
for (int i = 1; i < length; i++) {
if (jaccard(currentTerms, sortedArray[i]) < cutoff)
currentTerms.add(sortedArray[i]);
}
return(currentTerms.toArray(new Long[1]));
}
private Long[] pValueSort(Long[] rowArray, int length) {
// @TODO: Sort by pValue if not (ideally should be already sorted in table)
return Arrays.copyOf(rowArray, length);
}
// Two versions of jaccard similarity calculation. This one
// looks at the maximum jaccard between the currently selected
// terms and the new term.
private double jaccard(List<Long> currentTerms, Long term) {
double maxJaccard = 0;
for (Long currentTerm: currentTerms)
maxJaccard = Math.max(maxJaccard, jaccard(currentTerm, term));
return maxJaccard;
}
// This version of the jaccard calculation returns the jaccard between
// all currently selected nodes and the nodes of the new term.
private double jaccard2(List<Long> currentTerms, Long term) {
Set<Long> currentNodes = new HashSet<Long>();
for (Long currentTerm: currentTerms) {
List<Long> nodes = cyTable.getRow(currentTerm).getList(EnrichmentTerm.colGenesSUID, Long.class);
currentNodes.addAll(nodes);
}
List<Long> newNodes = cyTable.getRow(term).getList(EnrichmentTerm.colGenesSUID, Long.class);
return jaccard2(currentNodes, newNodes);
}
private double jaccard2(Set<Long> currentNodes, List<Long> newNodes) {
int intersection = 0;
for (Long cn: newNodes) {
if (currentNodes.contains(cn))
intersection++;
}
double j = ((double)intersection) / (double)(currentNodes.size()+newNodes.size()-intersection);
return j;
}
private double jaccard(Long currentTerm, Long term) {
List<Long> currentNodes = cyTable.getRow(currentTerm).getList(EnrichmentTerm.colGenesSUID, Long.class);
List<Long> newNodes = cyTable.getRow(term).getList(EnrichmentTerm.colGenesSUID, Long.class);
if (currentNodes == null || newNodes == null)
return 0;
int intersection = 0;
for (Long cn: currentNodes) {
if (newNodes.contains(cn))
intersection++;
}
double j = ((double)intersection) / (double)(currentNodes.size()+newNodes.size()-intersection);
return j;
}
/**
* @description Initialize the data model
*/
private void initData() {
List<CyRow> rows = cyTable.getAllRows();
rowNames = new Long[rows.size()];
int i = 0;
for (CyRow row : rows) {
rowNames[i] = row.get(EnrichmentTerm.colID, Long.class);
i++;
}
}
}