Skip to content

Commit 3cfbe23

Browse files
committed
Filter "Color by Data Set" chart based on selected data sets. Fixes #405
1 parent 342111e commit 3cfbe23

File tree

5 files changed

+42
-41
lines changed

5 files changed

+42
-41
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/EnrichmentMap.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static com.google.common.base.Strings.isNullOrEmpty;
44

5-
import java.awt.Color;
65
import java.util.ArrayList;
76
import java.util.Collection;
87
import java.util.Collections;
@@ -382,10 +381,6 @@ public List<String> getDataSetNames() {
382381
return getDataSetList().stream().map(EMDataSet::getName).collect(Collectors.toList());
383382
}
384383

385-
public List<Color> getDataSetColors() {
386-
return getDataSetList().stream().map(EMDataSet::getColor).collect(Collectors.toList());
387-
}
388-
389384
public EMCreationParameters getParams() {
390385
return params;
391386
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/style/EMStyleOptions.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.util.Collection;
44
import java.util.Objects;
5-
import java.util.function.Predicate;
6-
import java.util.stream.Collectors;
75

86
import org.baderlab.csplugins.enrichmentmap.model.AbstractDataSet;
97
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
@@ -13,34 +11,34 @@ public class EMStyleOptions {
1311

1412
private final CyNetworkView networkView;
1513
private final EnrichmentMap map;
16-
private final Predicate<AbstractDataSet> filter;
1714
private final ChartOptions chartOptions;
1815
private boolean postAnalysis;
1916
private final boolean publicationReady;
17+
private final Collection<? extends AbstractDataSet> dataSets;
2018

2119
/**
2220
* It is assumed that all the given DataSets come from the same EnrichmentMap.
2321
*/
24-
public EMStyleOptions(CyNetworkView networkView, EnrichmentMap map, Predicate<AbstractDataSet> filter,
22+
public EMStyleOptions(CyNetworkView networkView, EnrichmentMap map, Collection<? extends AbstractDataSet> dataSets,
2523
ChartOptions chartOptions, boolean postAnalysis, boolean publicationReady) {
2624
this.networkView = Objects.requireNonNull(networkView);
2725
this.map = Objects.requireNonNull(map);
28-
this.filter = Objects.requireNonNull(filter);
26+
this.dataSets = Objects.requireNonNull(dataSets);
2927
this.chartOptions = chartOptions;
3028
this.postAnalysis = postAnalysis;
3129
this.publicationReady = publicationReady;
3230
}
3331

3432
public EMStyleOptions(CyNetworkView networkView, EnrichmentMap map) {
35-
this(networkView, map, x -> true, null, false, false);
33+
this(networkView, map, map.getDataSetList(), null, false, false);
3634
}
3735

3836
public CyNetworkView getNetworkView() {
3937
return networkView;
4038
}
4139

42-
public Collection<AbstractDataSet> getDataSets() {
43-
return map.getDataSetList().stream().filter(filter).collect(Collectors.toList());
40+
public Collection<? extends AbstractDataSet> getDataSets() {
41+
return dataSets;
4442
}
4543

4644
public EnrichmentMap getEnrichmentMap() {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/task/ApplyEMStyleTask.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import java.util.Arrays;
55
import java.util.Collection;
66
import java.util.HashMap;
7+
import java.util.Iterator;
78
import java.util.List;
89
import java.util.Map;
9-
import java.util.Set;
1010
import java.util.stream.Collectors;
1111

1212
import org.baderlab.csplugins.enrichmentmap.model.AbstractDataSet;
@@ -74,7 +74,7 @@ public void run(TaskMonitor tm) throws Exception {
7474

7575
if (chartOptions != null && chartOptions.getData() == ChartData.DATA_SET) {
7676
tm.setStatusMessage("Updating Data Columns...");
77-
createDataSetColumn();
77+
createOrUpdateDataSetColumn();
7878
}
7979

8080
tm.setStatusMessage("Updating Style...");
@@ -83,34 +83,38 @@ public void run(TaskMonitor tm) throws Exception {
8383
applyVisualStyle();
8484
}
8585

86-
private void createDataSetColumn() {
86+
private void createOrUpdateDataSetColumn() {
8787
EnrichmentMap map = options.getEnrichmentMap();
8888
CyNetwork network = networkManager.getNetwork(map.getNetworkID());
8989
CyTable nodeTable = network.getDefaultNodeTable();
90+
Collection<? extends AbstractDataSet> dataSets = options.getDataSets();
9091

9192
String prefix = map.getParams().getAttributePrefix();
9293

93-
if (!Columns.DATASET_CHART.hasColumn(nodeTable, prefix)) {
94+
if(!Columns.DATASET_CHART.hasColumn(nodeTable, prefix)) {
9495
Columns.DATASET_CHART.createColumn(nodeTable, prefix);
96+
}
9597

96-
Map<Long, int[]> columnData = new HashMap<>();
97-
List<EMDataSet> dataSets = map.getDataSetList();
98-
int n = dataSets.size();
98+
Map<Long, int[]> columnData = new HashMap<>();
99+
int n = dataSets.size();
99100

100-
for (CyNode node : network.getNodeList())
101-
columnData.put(node.getSUID(), new int[n]);
101+
for(CyNode node : network.getNodeList()) {
102+
columnData.put(node.getSUID(), new int[n]);
103+
}
102104

103-
for (int i = 0; i < n; i++) {
104-
EMDataSet dataSet = dataSets.get(i);
105-
106-
for (Long suid : dataSet.getNodeSuids())
107-
columnData.get(suid)[i] = 1;
105+
Iterator<? extends AbstractDataSet> iter = dataSets.iterator();
106+
int i = 0;
107+
while(iter.hasNext()) {
108+
AbstractDataSet ds = iter.next();
109+
for(Long suid : ds.getNodeSuids()) {
110+
columnData.get(suid)[i] = 1;
108111
}
109-
110-
columnData.forEach((suid, data) ->
111-
Columns.DATASET_CHART.set(nodeTable.getRow(suid), prefix, Ints.asList(data))
112-
);
112+
i++;
113113
}
114+
115+
columnData.forEach((suid, data) ->
116+
Columns.DATASET_CHART.set(nodeTable.getRow(suid), prefix, Ints.asList(data))
117+
);
114118
}
115119

116120
private void applyVisualStyle() {
@@ -161,8 +165,7 @@ public CyCustomGraphics2<?> createChart() {
161165
ChartData data = chartOptions != null ? chartOptions.getData() : null;
162166

163167
if (data != null && data != ChartData.NONE) {
164-
// Ignore Signature Data Sets in charts
165-
Set<EMDataSet> dataSets = filterEMDataSets(options.getDataSets());
168+
List<EMDataSet> dataSets = filterEMDataSets(options.getDataSets()); // Ignore Signature Data Sets in charts
166169

167170
if (!dataSets.isEmpty()) {
168171
ChartType type = chartOptions.getType();
@@ -173,7 +176,7 @@ public CyCustomGraphics2<?> createChart() {
173176

174177
if (data == ChartData.DATA_SET) {
175178
List<CyColumnIdentifier> columns = Arrays.asList(columnIdFactory.createColumnIdentifier(columnDescriptor.with(prefix)));
176-
List<Color> colors = options.getEnrichmentMap().getDataSetColors();
179+
List<Color> colors = getColors(dataSets);
177180
props.put("cy_dataColumns", columns);
178181
props.put("cy_colors", colors);
179182
props.put("cy_showItemLabels", chartOptions.isShowLabels());
@@ -216,12 +219,17 @@ public CyCustomGraphics2<?> createChart() {
216219
return chart;
217220
}
218221

222+
223+
public static List<Color> getColors(Collection<EMDataSet> dataSets) {
224+
return dataSets.stream().map(EMDataSet::getColor).collect(Collectors.toList());
225+
}
226+
219227
@SuppressWarnings("unchecked")
220-
private static Set<EMDataSet> filterEMDataSets(Collection<AbstractDataSet> abstractDataSets) {
221-
Set<?> set = abstractDataSets.stream()
228+
public static List<EMDataSet> filterEMDataSets(Collection<? extends AbstractDataSet> abstractDataSets) {
229+
Collection<?> set = abstractDataSets.stream()
222230
.filter(ds -> ds instanceof EMDataSet) // Ignore Signature Data Sets
223-
.collect(Collectors.toSet());
231+
.collect(Collectors.toList());
224232

225-
return (Set<EMDataSet>) set;
233+
return (List<EMDataSet>) set;
226234
}
227235
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/task/UpdateAssociatedStyleTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private CyCustomGraphics2<?> createChart() {
207207

208208
if (data != null && data != ChartData.NONE) {
209209
ChartType type = chartOptions.getType();
210-
List<AbstractDataSet> dataSets = options.getDataSets(); // Ignore Signature Data Sets in charts
210+
List<EMDataSet> dataSets = ApplyEMStyleTask.filterEMDataSets(options.getDataSets());
211211

212212
if (type != null && !dataSets.isEmpty()) {
213213
Map<String,Object> props = new HashMap<>(type.getProperties());
@@ -217,7 +217,7 @@ private CyCustomGraphics2<?> createChart() {
217217

218218
if (data == ChartData.DATA_SET) {
219219
List<CyColumnIdentifier> columns = Arrays.asList(columnIdFactory.createColumnIdentifier(columnDescriptor.with(prefix)));
220-
List<Color> colors = options.getEnrichmentMap().getDataSetColors();
220+
List<Color> colors = ApplyEMStyleTask.getColors(dataSets);
221221

222222
props.put("cy_dataColumns", columns);
223223
props.put("cy_colors", colors);

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/control/ControlPanelMediator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ private EMStyleOptions createStyleOptions(EnrichmentMap map, EMViewControlPanel
789789
boolean showLabels = viewPanel.getShowChartLabelsCheck().isSelected();
790790
ChartOptions chartOptions = new ChartOptions(data, type, colorScheme, showLabels);
791791

792-
return new EMStyleOptions(viewPanel.getNetworkView(), map, dataSets::contains, chartOptions, postAnalysis, publicationReady);
792+
return new EMStyleOptions(viewPanel.getNetworkView(), map, dataSets, chartOptions, postAnalysis, publicationReady);
793793
}
794794

795795
private AssociatedStyleOptions createAssociatedStyleOptions(EnrichmentMap map, AssociatedViewControlPanel viewPanel) {

0 commit comments

Comments
 (0)