|
| 1 | +package org.baderlab.csplugins.enrichmentmap.commands; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.baderlab.csplugins.enrichmentmap.commands.tunables.DatasetColorTunable; |
| 8 | +import org.baderlab.csplugins.enrichmentmap.commands.tunables.NetworkTunable; |
| 9 | +import org.baderlab.csplugins.enrichmentmap.model.EMDataSet; |
| 10 | +import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap; |
| 11 | +import org.baderlab.csplugins.enrichmentmap.style.EMStyleBuilder.StyleUpdateScope; |
| 12 | +import org.baderlab.csplugins.enrichmentmap.style.EMStyleOptions; |
| 13 | +import org.baderlab.csplugins.enrichmentmap.view.control.ControlPanelMediator; |
| 14 | +import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapMediator; |
| 15 | +import org.cytoscape.view.model.CyNetworkView; |
| 16 | +import org.cytoscape.work.AbstractTask; |
| 17 | +import org.cytoscape.work.ContainsTunables; |
| 18 | +import org.cytoscape.work.TaskMonitor; |
| 19 | + |
| 20 | +import com.google.inject.Inject; |
| 21 | +import com.google.inject.Provider; |
| 22 | + |
| 23 | +public class DatasetColorCommandTask extends AbstractTask { |
| 24 | + |
| 25 | + @Inject private Provider<HeatMapMediator> heatMapMediatorProvider; |
| 26 | + @Inject private ControlPanelMediator controlPanelMediator; |
| 27 | + |
| 28 | + @ContainsTunables @Inject |
| 29 | + public NetworkTunable networkTunable; |
| 30 | + |
| 31 | + @ContainsTunables @Inject |
| 32 | + public DatasetColorTunable datasetColorTunable; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void run(TaskMonitor tm) throws Exception { |
| 36 | + tm.setTitle("Change Data Set Colors"); |
| 37 | + tm.setProgress(0.1); |
| 38 | + |
| 39 | + CyNetworkView netView = networkTunable.getNetworkView(); |
| 40 | + EnrichmentMap map = networkTunable.getEnrichmentMap(); |
| 41 | + if(netView == null || map == null) |
| 42 | + throw new IllegalArgumentException("network is not an EnrichmentMap network"); |
| 43 | + |
| 44 | + Map<String,Color> colors = datasetColorTunable.getColors(); |
| 45 | + if(colors.isEmpty()) |
| 46 | + throw new IllegalArgumentException("no colors specified"); |
| 47 | + |
| 48 | + List<EMDataSet> datasetList = map.getDataSetList(); |
| 49 | + |
| 50 | + for(var entry : colors.entrySet()) { |
| 51 | + String dsName = entry.getKey(); |
| 52 | + Color color = entry.getValue(); |
| 53 | + |
| 54 | + EMDataSet ds = map.getDataSet(dsName); |
| 55 | + if(ds != null) { |
| 56 | + ds.setColor(color); |
| 57 | + tm.setStatusMessage("Color changed for dataset: '" + dsName + "'"); |
| 58 | + } else { |
| 59 | + try { |
| 60 | + int index = Integer.parseInt(dsName); |
| 61 | + ds = datasetList.get(index); |
| 62 | + ds.setColor(color); |
| 63 | + tm.setStatusMessage("Color changed for dataset: '" + dsName + "'"); |
| 64 | + } catch(NumberFormatException | IndexOutOfBoundsException e) { |
| 65 | + tm.setStatusMessage("Dataset not found: '" + dsName + "'"); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // update panels |
| 71 | + controlPanelMediator.updateDataSetList(networkTunable.getNetworkView()); |
| 72 | + heatMapMediatorProvider.get().reset(); |
| 73 | + |
| 74 | + // update visual style |
| 75 | + EMStyleOptions styleOptions = controlPanelMediator.createStyleOptions(netView); |
| 76 | + controlPanelMediator.applyVisualStyle(styleOptions, StyleUpdateScope.ONLY_DATASETS); |
| 77 | + heatMapMediatorProvider.get().reset(); |
| 78 | + |
| 79 | + tm.setProgress(1.0); |
| 80 | + } |
| 81 | +} |
0 commit comments