|
| 1 | +package org.baderlab.csplugins.enrichmentmap.commands; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.function.Predicate; |
| 7 | + |
| 8 | +import org.baderlab.csplugins.enrichmentmap.commands.tunables.NetworkTunable; |
| 9 | +import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap; |
| 10 | +import org.baderlab.csplugins.enrichmentmap.style.ChartData; |
| 11 | +import org.baderlab.csplugins.enrichmentmap.style.ChartOptions; |
| 12 | +import org.baderlab.csplugins.enrichmentmap.style.ChartType; |
| 13 | +import org.baderlab.csplugins.enrichmentmap.style.ColorScheme; |
| 14 | +import org.baderlab.csplugins.enrichmentmap.view.control.ControlPanelMediator; |
| 15 | +import org.baderlab.csplugins.enrichmentmap.view.control.io.ViewParams; |
| 16 | +import org.cytoscape.view.model.CyNetworkView; |
| 17 | +import org.cytoscape.work.AbstractTask; |
| 18 | +import org.cytoscape.work.ContainsTunables; |
| 19 | +import org.cytoscape.work.TaskMonitor; |
| 20 | +import org.cytoscape.work.Tunable; |
| 21 | +import org.cytoscape.work.util.ListSingleSelection; |
| 22 | + |
| 23 | +import com.google.inject.Inject; |
| 24 | + |
| 25 | +public class ChartCommandTask extends AbstractTask { |
| 26 | + |
| 27 | + @Inject private ControlPanelMediator controlPanelMediator; |
| 28 | + |
| 29 | + |
| 30 | + @ContainsTunables @Inject |
| 31 | + public NetworkTunable networkTunable; |
| 32 | + |
| 33 | + @Tunable |
| 34 | + public ListSingleSelection<String> data; |
| 35 | + |
| 36 | + @Tunable |
| 37 | + public ListSingleSelection<String> type; |
| 38 | + |
| 39 | + @Tunable |
| 40 | + public ListSingleSelection<String> colors; |
| 41 | + |
| 42 | + @Tunable |
| 43 | + public boolean showLabels; |
| 44 | + |
| 45 | + |
| 46 | + public ChartCommandTask() { |
| 47 | + data = lssFromEnum(ChartData.values(), x -> true); |
| 48 | + type = lssFromEnum(ChartType.values(), ct -> ct != ChartType.DATASET_PIE); |
| 49 | + colors = lssFromEnum(ColorScheme.values(), x -> true); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void run(TaskMonitor taskMonitor) { |
| 54 | + // validate network |
| 55 | + CyNetworkView networkView = networkTunable.getNetworkView(); |
| 56 | + EnrichmentMap map = networkTunable.getEnrichmentMap(); |
| 57 | + if(networkView == null || map == null) |
| 58 | + throw new IllegalArgumentException("network is not an EnrichmentMap network"); |
| 59 | + |
| 60 | + Map<Long,ViewParams> viewParamsMap = controlPanelMediator.getAllViewParams(); |
| 61 | + ViewParams params = viewParamsMap.get(networkView.getSUID()); |
| 62 | + ChartOptions chartOptions = params.getChartOptions(); |
| 63 | + |
| 64 | + ChartData chartData = "null".equals(data.getSelectedValue()) ? chartOptions.getData() : ChartData.valueOf(data.getSelectedValue()); |
| 65 | + ChartType chartType = "null".equals(type.getSelectedValue()) ? chartOptions.getType() : ChartType.valueOf(type.getSelectedValue()); |
| 66 | + ColorScheme colorScheme = "null".equals(colors.getSelectedValue()) ? chartOptions.getColorScheme() : ColorScheme.valueOf(colors.getSelectedValue()); |
| 67 | + |
| 68 | + // validate |
| 69 | + if(chartData == ChartData.EXPRESSION_DATA && !networkTunable.isAssociatedEnrichmenMap()) |
| 70 | + throw new IllegalArgumentException("data=EXPRESSION_DATA can only be used with a GeneMANIA or STRING network created from an EnrichmentMap network"); |
| 71 | + if(chartData == ChartData.EXPRESSION_DATA && !(chartType == ChartType.RADIAL_HEAT_MAP || chartType == ChartType.HEAT_STRIPS)) |
| 72 | + throw new IllegalArgumentException("data=EXPRESSION_DATA can only be used with chartType=RADIAL_HEAT_MAP or chartType=HEAT_STRIPS"); |
| 73 | + if(chartData == ChartData.PHENOTYPES && !map.isTwoPhenotypeGeneric()) |
| 74 | + throw new IllegalArgumentException("data=PHENOTYPES can only be used with two-phenotype generic networks."); |
| 75 | + if(chartData == ChartData.FDR_VALUE && !map.getParams().isFDR()) |
| 76 | + throw new IllegalArgumentException("data=FDR_VALUE cannot be used on this network"); |
| 77 | + |
| 78 | + if(chartData == ChartData.DATA_SET) |
| 79 | + chartType = ChartType.DATASET_PIE; |
| 80 | + |
| 81 | + ChartOptions options = new ChartOptions(chartData, chartType, colorScheme, showLabels); |
| 82 | + params.setChartOptions(options); |
| 83 | + controlPanelMediator.reset(params); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private static ListSingleSelection<String> lssFromEnum(Enum<?>[] values, Predicate<Enum<?>> valueTester) { |
| 88 | + List<String> names = new ArrayList<>(values.length); |
| 89 | + names.add("null"); // important, need to know if the user did not set the value |
| 90 | + for(Enum<?> value : values) { |
| 91 | + if(valueTester.test(value)) { |
| 92 | + names.add(value.name()); |
| 93 | + } |
| 94 | + } |
| 95 | + return new ListSingleSelection<>(names); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | +} |
0 commit comments