Skip to content

Commit 7b0feba

Browse files
committed
Fixes global chart range and Heat Strips colors.
1 parent d061aff commit 7b0feba

File tree

2 files changed

+108
-7
lines changed

2 files changed

+108
-7
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.baderlab.csplugins.enrichmentmap.view.legend.LegendPanelMediator;
5858
import org.baderlab.csplugins.enrichmentmap.view.postanalysis.EdgeWidthDialog;
5959
import org.baderlab.csplugins.enrichmentmap.view.postanalysis.PostAnalysisPanelMediator;
60+
import org.baderlab.csplugins.enrichmentmap.view.util.ChartUtil;
6061
import org.baderlab.csplugins.enrichmentmap.view.util.SliderBarPanel;
6162
import org.cytoscape.application.CyApplicationManager;
6263
import org.cytoscape.application.events.SetCurrentNetworkViewEvent;
@@ -532,13 +533,25 @@ private CyCustomGraphics2<?> createChart(ChartData data, ChartType type, ColorSc
532533
Map<String, Object> props = new HashMap<>(type.getProperties());
533534
props.put("cy_dataColumns", columns);
534535

535-
if (type == ChartType.LINE) {
536-
props.put("cy_colors", colorScheme.getColors(1));
537-
} else if (colorScheme != null) {
538-
if (colorScheme == ColorScheme.RANDOM)
539-
props.put("cy_colors", colorScheme.getColors(columns.size()));
540-
else
541-
props.put("cy_colorScheme", colorScheme.getKey());
536+
if (type != null && type != ChartType.PIE) {
537+
List<Double> range = ChartUtil.calculateGlobalRange(options.getNetworkView().getModel(), columns);
538+
539+
props.put("cy_range", range);
540+
props.put("cy_globalRange", true);
541+
}
542+
543+
if (colorScheme == ColorScheme.CONTRASTING || colorScheme == ColorScheme.MODULATED
544+
|| colorScheme == ColorScheme.RAINBOW) {
545+
props.put("cy_colorScheme", colorScheme.getKey());
546+
} else {
547+
int nColors = columns.size();
548+
549+
if (type == ChartType.LINE)
550+
nColors = 1;
551+
else if (type == ChartType.HEAT_STRIPS)
552+
nColors = 3;
553+
554+
props.put("cy_colors", colorScheme.getColors(nColors));
542555
}
543556

544557
try {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.baderlab.csplugins.enrichmentmap.view.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.stream.Collectors;
9+
10+
import org.cytoscape.model.CyColumn;
11+
import org.cytoscape.model.CyNetwork;
12+
import org.cytoscape.model.CyNode;
13+
import org.cytoscape.model.CyRow;
14+
import org.cytoscape.view.presentation.property.values.CyColumnIdentifier;
15+
16+
public final class ChartUtil {
17+
18+
private ChartUtil() {
19+
}
20+
21+
/**
22+
* @return List whose first item is the minimum value of the range, and whose second item is the maximum value.
23+
*/
24+
@SuppressWarnings("unchecked")
25+
public static List<Double> calculateGlobalRange(CyNetwork network, List<CyColumnIdentifier> dataColumns) {
26+
List<Double> range = new ArrayList<>(2);
27+
List<CyNode> nodes = network.getNodeList();
28+
29+
if (!nodes.isEmpty()) {
30+
double min = Double.POSITIVE_INFINITY;
31+
double max = Double.NEGATIVE_INFINITY;
32+
33+
Collection<CyColumn> columns = network.getDefaultNodeTable().getColumns();
34+
Map<String, CyColumn> columnMap = columns.stream().collect(Collectors.toMap(CyColumn::getName, c -> c));
35+
36+
for (final CyColumnIdentifier colId : dataColumns) {
37+
final CyColumn column = columnMap.get(colId);
38+
39+
if (column == null)
40+
continue;
41+
42+
final Class<?> colType = column.getType();
43+
final Class<?> colListType = column.getListElementType();
44+
45+
if (Number.class.isAssignableFrom(colType) ||
46+
(List.class.isAssignableFrom(colType) && Number.class.isAssignableFrom(colListType))) {
47+
for (final CyNode n : nodes) {
48+
List<? extends Number> values = null;
49+
final CyRow row = network.getRow(n);
50+
51+
if (List.class.isAssignableFrom(colType))
52+
values = (List<? extends Number>) row.getList(column.getName(), colListType);
53+
else if (row.isSet(column.getName()))
54+
values = Collections.singletonList((Number)row.get(column.getName(), colType));
55+
56+
double[] mm = minMax(min, max, values);
57+
min = mm[0];
58+
max = mm[1];
59+
}
60+
}
61+
}
62+
63+
if (min != Double.POSITIVE_INFINITY && max != Double.NEGATIVE_INFINITY) {
64+
range.add(min);
65+
range.add(max);
66+
}
67+
} else {
68+
range.add(0d);
69+
range.add(0d);
70+
}
71+
72+
return range;
73+
}
74+
75+
private static double[] minMax(double min, double max, final List<? extends Number> values) {
76+
if (values != null) {
77+
for (final Number v : values) {
78+
if (v != null) {
79+
final double dv = v.doubleValue();
80+
min = Math.min(min, dv);
81+
max = Math.max(max, dv);
82+
}
83+
}
84+
}
85+
86+
return new double[]{ min, max };
87+
}
88+
}

0 commit comments

Comments
 (0)