|
| 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