Skip to content

Commit 12fcdc2

Browse files
committed
Sync the heat map with the data set list on the control panel. Refs #313
1 parent 38d96a7 commit 12fcdc2

File tree

10 files changed

+158
-63
lines changed

10 files changed

+158
-63
lines changed

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/PropertyManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.lang.reflect.Field;
44
import java.lang.reflect.Modifier;
55
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.HashMap;
68
import java.util.List;
9+
import java.util.Map;
710
import java.util.Properties;
811
import java.util.function.Function;
912

@@ -20,6 +23,14 @@
2023
@Singleton
2124
public class PropertyManager {
2225

26+
@FunctionalInterface
27+
public interface PropertyListener<T> {
28+
void propertyChanged(Property<T> prop, T value);
29+
}
30+
31+
private final Map<Property<?>,List<PropertyListener<?>>> listeners = new HashMap<>();
32+
33+
2334
public static class Property<T> {
2435
private final String key;
2536
public final T def;
@@ -33,6 +44,7 @@ private Property(String key, T defaultValue, Function<String,T> converter) {
3344
}
3445

3546
public static final Property<Boolean> HEATMAP_AUTOFOCUS = new Property<>("heatmapAutofocus", false, Boolean::valueOf);
47+
public static final Property<Boolean> HEATMAP_DATASET_SYNC = new Property<>("heatmapDatasetSync", true, Boolean::valueOf);
3648
public static final Property<Double> P_VALUE = new Property<>("default.pvalue", 1.0, Double::valueOf);
3749
public static final Property<Double> Q_VALUE = new Property<>("default.qvalue", 0.1, Double::valueOf);
3850
public static final Property<Boolean> CREATE_WARN = new Property<>("create.warn", true, Boolean::valueOf);
@@ -48,9 +60,16 @@ private void initializeProperties() {
4860
.forEach(this::setDefault);
4961
}
5062

63+
public <T> void addListener(Property<T> property, PropertyListener<T> listener) {
64+
listeners.computeIfAbsent(property, k -> new ArrayList<>()).add(listener);
65+
}
5166

5267
public <T> void setValue(Property<T> property, T value) {
5368
cyProps.getProperties().setProperty(property.key, String.valueOf(value));
69+
70+
for(PropertyListener<?> listener : listeners.getOrDefault(property, Collections.emptyList())) {
71+
((PropertyListener<T>)listener).propertyChanged(property, value);
72+
}
5473
}
5574

5675
public <T> void setDefault(Property<T> property) {

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/commands/ExportPDFCommandTask.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
import java.io.File;
66
import java.util.ArrayList;
7+
import java.util.Collection;
78
import java.util.Comparator;
89
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
912

13+
import org.baderlab.csplugins.enrichmentmap.commands.tunables.DatasetListTunable;
1014
import org.baderlab.csplugins.enrichmentmap.commands.tunables.NetworkTunable;
1115
import org.baderlab.csplugins.enrichmentmap.model.Compress;
16+
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet;
1217
import org.baderlab.csplugins.enrichmentmap.model.EnrichmentMap;
1318
import org.baderlab.csplugins.enrichmentmap.model.Transform;
1419
import org.baderlab.csplugins.enrichmentmap.view.heatmap.ExportPDFTask;
@@ -37,6 +42,9 @@ public class ExportPDFCommandTask extends AbstractTask {
3742
@ContainsTunables @Inject
3843
public NetworkTunable networkTunable;
3944

45+
@ContainsTunables @Inject
46+
public DatasetListTunable datasetListTunable;
47+
4048
@Tunable(required=true, description = "Absolute path to the PDF file to be exported. Will be overwritten if it already exists.")
4149
public File file;
4250

@@ -73,9 +81,10 @@ public void run(TaskMonitor taskMonitor) {
7381
Transform transformValue = Transform.valueOf(transform.getSelectedValue());
7482
Compress compressValue = Compress.valueOf(compress.getSelectedValue());
7583

76-
List<String> genes = getGenes();
84+
Collection<EMDataSet> dataSets = datasetListTunable.getDataSets(map);
85+
List<String> genes = getGenes(dataSets);
7786

78-
HeatMapTableModel model = new HeatMapTableModel(network, map, null, genes, transformValue, compressValue);
87+
HeatMapTableModel model = new HeatMapTableModel(network, map, dataSets, null, genes, transformValue, compressValue);
7988
ExportPDFTask exportTask = new ExportPDFTask(file, model, RankingOption.none(), showValues);
8089

8190
TaskIterator moreTasks = new TaskIterator(exportTask);
@@ -87,7 +96,7 @@ public void run(TaskMonitor taskMonitor) {
8796
}
8897

8998

90-
private List<String> getGenes() {
99+
private List<String> getGenes(Collection<EMDataSet> dataSets) {
91100
CyNetwork network = networkTunable.getNetwork();
92101
EnrichmentMap map = networkTunable.getEnrichmentMap();
93102
String prefix = map.getParams().getAttributePrefix();
@@ -102,11 +111,13 @@ private List<String> getGenes() {
102111
edges = network.getEdgeList();
103112
}
104113

114+
Map<String,Set<Integer>> geneSetToGenes = map.unionGeneSetsOfInterest(dataSets);
115+
105116
List<String> genes;
106117
if("union".equals(operator.getSelectedValue())) {
107-
genes = new ArrayList<>(HeatMapMediator.unionGenesets(network, nodes, edges, prefix));
118+
genes = new ArrayList<>(HeatMapMediator.unionGenesets(geneSetToGenes, map, network, nodes, edges, prefix));
108119
} else {
109-
genes = new ArrayList<>(HeatMapMediator.intersectionGenesets(network, nodes, edges, prefix));
120+
genes = new ArrayList<>(HeatMapMediator.interGenesets(geneSetToGenes, map, network, nodes, edges, prefix));
110121
}
111122
genes.sort(Comparator.naturalOrder());
112123

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/model/EnrichmentMap.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Map;
1616
import java.util.Set;
1717
import java.util.function.Function;
18+
import java.util.function.Predicate;
1819
import java.util.stream.Collectors;
1920

2021
import org.baderlab.csplugins.enrichmentmap.model.EMDataSet.Method;
@@ -281,16 +282,25 @@ public Map<String, GeneSet> getEnrichmentGenesets() {
281282
return allGeneSets;
282283
}
283284

284-
// MKTODO write a JUnit
285285
public Map<String, Set<Integer>> unionAllGeneSetsOfInterest() {
286+
return unionGeneSetsOfInterest(x -> true);
287+
}
288+
289+
public Map<String, Set<Integer>> unionGeneSetsOfInterest(Collection<EMDataSet> dataSets) {
290+
return unionGeneSetsOfInterest(dataSets::contains);
291+
}
292+
293+
private Map<String, Set<Integer>> unionGeneSetsOfInterest(Predicate<EMDataSet> filter) {
286294
Map<String, Set<Integer>> allGeneSets = new HashMap<>();
287295

288296
for (EMDataSet ds : getDataSetList()) {
289-
Map<String, GeneSet> geneSets = ds.getGeneSetsOfInterest().getGeneSets();
290-
291-
geneSets.forEach((name, gs) -> {
292-
allGeneSets.computeIfAbsent(name, k -> new HashSet<>()).addAll(gs.getGenes());
293-
});
297+
if(filter.test(ds)) {
298+
Map<String, GeneSet> geneSets = ds.getGeneSetsOfInterest().getGeneSets();
299+
300+
geneSets.forEach((name, gs) -> {
301+
allGeneSets.computeIfAbsent(name, k -> new HashSet<>()).addAll(gs.getGenes());
302+
});
303+
}
294304
}
295305

296306
return allGeneSets;
@@ -333,6 +343,7 @@ public String findGeneSetDescription(String genesetName) {
333343

334344

335345
public Map<String, EMDataSet> getDataSets() {
346+
// this must return a new HashMap or client code might break
336347
return new HashMap<>(dataSets);
337348
}
338349

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/resolver/ResolverTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public void run(TaskMonitor tm) {
9292
@Override
9393
public <R> R getResults(Class<? extends R> type) {
9494
if(List.class.equals(type)) {
95-
return type.cast(results);
95+
return type.cast(getDataSetResults());
9696
}
9797
return null;
9898
}
9999

100100
public List<DataSetParameters> getDataSetResults() {
101-
return results;
101+
return new ArrayList<>(results);
102102
}
103103

104104
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.baderlab.csplugins.enrichmentmap.view.control.io.ViewParams;
7777
import org.baderlab.csplugins.enrichmentmap.view.control.io.ViewParams.CutoffParam;
7878
import org.baderlab.csplugins.enrichmentmap.view.creation.CreationDialogShowAction;
79+
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapMediator;
7980
import org.baderlab.csplugins.enrichmentmap.view.legend.CreationParametersPanel;
8081
import org.baderlab.csplugins.enrichmentmap.view.legend.LegendPanelMediator;
8182
import org.baderlab.csplugins.enrichmentmap.view.postanalysis.EdgeWidthDialog;
@@ -120,6 +121,7 @@ public class ControlPanelMediator implements SetCurrentNetworkViewListener, Netw
120121
@Inject private Provider<LegendPanelMediator> legendPanelMediatorProvider;
121122
@Inject private Provider<PADialogMediator> paDialogMediatorProvider;
122123
@Inject private Provider<EdgeWidthDialog> dialogProvider;
124+
@Inject private Provider<HeatMapMediator> heatMapMediatorProvider;
123125
@Inject private EnrichmentMapManager emManager;
124126
@Inject private CreationDialogShowAction masterMapDialogAction;
125127
@Inject private VisualMappingManager visualMappingManager;
@@ -456,10 +458,12 @@ private void addListeners(EMViewControlPanel viewPanel, EnrichmentMap map) {
456458
updateStyle = updateStyle || oldSize == 0 && newSize > 0;
457459
updateStyle = updateStyle || oldSize > 0 && newSize == 0;
458460

459-
if (updateStyle)
461+
if (updateStyle) {
460462
updateVisualStyle(map, viewPanel);
461-
else
463+
heatMapMediatorProvider.get().reset();
464+
} else {
462465
netView.updateView();
466+
}
463467
}
464468
});
465469

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/creation/MasterDetailDialogPage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.Comparator;
2324
import java.util.Iterator;
2425
import java.util.List;
2526
import java.util.Map;
@@ -396,6 +397,7 @@ public void taskFinished(ObservableTask task) {
396397
List<DataSetParameters> datasets = task.getResults(List.class);
397398
if(!datasets.isEmpty()) {
398399
foundDatasets = true;
400+
datasets.sort(Comparator.comparing(DataSetParameters::getName));
399401
datasets.forEach(MasterDetailDialogPage.this::addDataSetToList);
400402
dataSetList.setSelectedValue(datasets.get(0), true);
401403
}

EnrichmentMapPlugin/src/main/java/org/baderlab/csplugins/enrichmentmap/view/heatmap/HeatMapContentPanel.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,14 @@ public List<String> getInterGenes() {
349349

350350
void clear() {
351351
HeatMapParams params = new HeatMapParams.Builder().build();
352-
update(null, null, params, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), RankingOption.none());
352+
update(null, null, Collections.emptyList(), params, Collections.emptyList(),
353+
Collections.emptyList(), Collections.emptyList(), RankingOption.none());
353354
}
354355

355356
void update(
356357
CyNetwork network,
357358
EnrichmentMap map,
359+
Collection<EMDataSet> datasets,
358360
HeatMapParams params,
359361
List<RankingOption> moreRankOptions,
360362
Collection<String> union,
@@ -406,13 +408,20 @@ void update(
406408

407409
List<String> genesToUse = params.getOperator() == Operator.UNION ? unionGenes : interGenes;
408410
HeatMapTableModel tableModel = (HeatMapTableModel) getTable().getModel();
409-
tableModel.update(network, map, null, genesToUse, params.getTransform(), params.getCompress());
411+
tableModel.update(network, map, datasets, null, genesToUse, params.getTransform(), params.getCompress());
410412

411413
updateTableHeader(isShowValues());
412414
getTable().revalidate();
413415

414-
List<? extends SortKey> sortKeys = params.getSortKeys();
416+
updateSortKeys(params, rankingOption);
415417

418+
// Re-compute the ranking
419+
setSelectedRankingOption(rankingOption);
420+
}
421+
422+
423+
private void updateSortKeys(HeatMapParams params, RankingOption rankingOption) {
424+
List<? extends SortKey> sortKeys = params.getSortKeys();
416425
if (sortKeys == null)
417426
sortKeys = getTable().getRowSorter().getSortKeys();
418427
if (sortKeys.isEmpty())
@@ -421,9 +430,6 @@ void update(
421430
try {
422431
getTable().getRowSorter().setSortKeys(sortKeys);
423432
} catch(IllegalArgumentException e) {}
424-
425-
// Re-compute the ranking
426-
setSelectedRankingOption(rankingOption);
427433
}
428434

429435
protected OptionsPopup getOptionsPopup() {

0 commit comments

Comments
 (0)