Java tutorial
/* * 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 coolmapplugin.actions; import com.google.common.collect.Range; import coolmap.application.CoolMapMaster; import coolmap.application.widget.impl.console.CMConsole; import coolmap.canvas.CoolMapView; import coolmap.data.CoolMapObject; import coolmapplugin.util.AggregationUtil; import coolmapplugin.util.CMCyCommunicationUtil; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import javax.swing.AbstractAction; /** * * @author Keqiang Li */ public class MapSelectedToCytoscapeWithAggregatedValue extends AbstractAction { private AggregationType passedInAggregationType = AggregationType.MEAN; public static enum AggregationType { MEAN, SUM, MAX, MIN, MEDIAN } public MapSelectedToCytoscapeWithAggregatedValue(AggregationType type) { this.passedInAggregationType = type; } @Override public void actionPerformed(ActionEvent e) { HashMap<String, Double> selectedElements = getSelectedColumnAndRowNamesAndAggregatedValues( passedInAggregationType); if (selectedElements == null || selectedElements.isEmpty()) { return; } Long curNetwork = CMCyCommunicationUtil.getSelectedNetwork(); List<Object> nameList = new ArrayList(selectedElements.keySet()); boolean result = CMCyCommunicationUtil.setNodesSelectedByNodeNames(curNetwork.toString(), nameList, true); if (!result) { CMConsole.logError("Failed to Map. Couldn't communicate with Cytoscape"); return; } String columnName = "mean"; switch (passedInAggregationType) { case MEDIAN: columnName = "median"; break; case MAX: columnName = "max"; break; case MIN: columnName = "min"; break; case SUM: columnName = "sum"; break; case MEAN: columnName = "mean"; break; } result = CMCyCommunicationUtil.addColumnToNodeTable(curNetwork.toString(), selectedElements, columnName); if (!result) { CMConsole.logError( "Failed to add aggregated value in the node table. Couldn't communicate with Cytoscape"); } } private HashMap<String, Double> getSelectedColumnAndRowNamesAndAggregatedValues( AggregationType aggregationType) { try { CoolMapObject obj = CoolMapMaster.getActiveCoolMapObject(); CoolMapView coolMapView = obj.getCoolMapView(); List<Range<Integer>> columnList = coolMapView.getSelectedColumns(); List<Range<Integer>> rowList = coolMapView.getSelectedRows(); HashMap<String, Double> nameValueHashMap = new HashMap<>(); for (Range<Integer> rangeColumn : columnList) { for (int i = rangeColumn.lowerEndpoint(); i < rangeColumn.upperEndpoint(); ++i) { String columnName = obj.getViewNodeColumn(i).getName(); LinkedList<Double> values = new LinkedList<>(); for (Range<Integer> rangeRow : rowList) { for (int j = rangeRow.lowerEndpoint(); j < rangeRow.upperEndpoint(); ++j) { Double value = (Double) obj.getViewValue(j, i); if (value == null) { continue; } values.add(value); } } Double aggregatedValue = getAggregatedValue(values, aggregationType); nameValueHashMap.put(columnName, aggregatedValue); } } for (Range<Integer> rangeRow : rowList) { for (int i = rangeRow.lowerEndpoint(); i < rangeRow.upperEndpoint(); ++i) { String rowName = obj.getViewNodeRow(i).getName(); LinkedList<Double> values = new LinkedList<>(); for (Range<Integer> rangeColumn : columnList) { for (int j = rangeColumn.lowerEndpoint(); j < rangeColumn.upperEndpoint(); ++j) { Double value = (Double) obj.getViewValue(i, j); if (value == null) { continue; } values.add(value); } } Double aggregatedValue = getAggregatedValue(values, aggregationType); nameValueHashMap.put(rowName, aggregatedValue); } } return nameValueHashMap; } catch (Exception e) { CMConsole.logError("Failed to get selected columns and rows. Internal Error : " + e.getMessage()); } return null; } private Double getAggregatedValue(List<Double> values, AggregationType aggregationType) { switch (aggregationType) { case MEAN: return AggregationUtil.getMean(values); case SUM: return AggregationUtil.getSum(values); case MIN: return AggregationUtil.getMin(values); case MAX: return AggregationUtil.getMax(values); case MEDIAN: return AggregationUtil.getMedian(values); } return null; } }