Skip to content

Commit 64fde93

Browse files
committed
Refactored PropertyManager. Sync advanced with slider. Fixes #213.
1 parent b8c445b commit 64fde93

File tree

12 files changed

+134
-176
lines changed

12 files changed

+134
-176
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public void start(BundleContext bc) {
7171
if(headless) {
7272
// register the injector as an OSGi service so the integration tests can access it
7373
registerService(bc, injector, Injector.class, new Properties());
74-
}
75-
76-
if(!headless) {
74+
} else {
7775
// Don't load UI services if running headless
7876
// register actions
7977
registerAllServices(bc, injector.getInstance(OpenEnrichmentMapAction.class), new Properties());
@@ -85,7 +83,6 @@ public void start(BundleContext bc) {
8583
// chart factories
8684
final Properties chartProps = new Properties();
8785
chartProps.setProperty(CyCustomGraphics2Factory.GROUP, "Charts");
88-
8986
RadialHeatMapChartFactory radialHeatMapChartFactory = injector.getInstance(RadialHeatMapChartFactory.class);
9087
registerService(bc, radialHeatMapChartFactory, CyCustomGraphics2Factory.class, chartProps);
9188

Lines changed: 52 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.baderlab.csplugins.enrichmentmap;
22

3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Modifier;
5+
import java.util.ArrayList;
6+
import java.util.List;
37
import java.util.Properties;
48
import java.util.function.Function;
59

6-
import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters.SimilarityMetric;
710
import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapParams.Distance;
811
import org.cytoscape.property.CyProperty;
912

@@ -16,123 +19,70 @@
1619
@Singleton
1720
public class PropertyManager {
1821

19-
public static final String heatmap_autofocus_propname = "heatmapAutofocus";
20-
public static final String jaccardCutOff_propname = "default.jaccardCutoff";
21-
public static final String overlapCutOff_propname = "default.overlapCutoff";
22-
public static final String combinedCutoff_propname = "default.combinedCutoff";
23-
public static final String combinedConstant_propname = "default.combinedConstant";
24-
public static final String similarityMetric_propname = "default.similarityMetric";
25-
public static final String distanceMetric_propname = "default.distanceMetric";
26-
public static final String Pvalue_propname = "default.pvalue";
27-
public static final String Qvalue_propname = "default.qvalue";
28-
public static final String create_warn_show_propname = "create.warn.show";
29-
22+
public static class Property<T> {
23+
private final String key;
24+
public final T defaultValue;
25+
private final Function<String,T> converter;
26+
27+
private Property(String key, T defaultValue, Function<String,T> converter) {
28+
this.key = key;
29+
this.defaultValue = defaultValue;
30+
this.converter = converter;
31+
}
32+
}
3033

31-
private static final double jaccardCutOff_default = 0.25;
32-
private static final double overlapCutOff_default = 0.5;
33-
private static final double combinedCutoff_default = 0.375;
34-
private static final double combinedConstant_default = 0.5;
35-
private static final SimilarityMetric similarityMetric_default = SimilarityMetric.OVERLAP;
36-
private static final Distance distanceMetric_default = Distance.PEARSON;
37-
private static final double Pvalue_default = 1.0;
38-
private static final double Qvalue_default = 0.1;
39-
private static final boolean create_warn_show_default = true;
34+
public static final Property<Boolean> HEATMAP_AUTOFOCUS = new Property<>("heatmapAutofocus", false, Boolean::valueOf);
35+
public static final Property<Double> P_VALUE = new Property<>("default.pvalue", 1.0, Double::valueOf);
36+
public static final Property<Double> Q_VALUE = new Property<>("default.qvalue", 0.1, Double::valueOf);
37+
public static final Property<Boolean> CREATE_WARN = new Property<>("create.warn", true, Boolean::valueOf);
38+
public static final Property<Distance> DISTANCE_METRIC = new Property<>("default.distanceMetric", Distance.PEARSON, Distance::valueOf);
4039

41-
@Inject private CyProperty<Properties> cyProps;
40+
@Inject private CyProperty<Properties> cyProperties;
4241

4342
@AfterInjection
4443
private void initializeProperties() {
45-
Properties props = cyProps.getProperties();
46-
if(props.size() < 10) {
47-
props.setProperty(heatmap_autofocus_propname, String.valueOf(false));
48-
props.setProperty(jaccardCutOff_propname, String.valueOf(jaccardCutOff_default));
49-
props.setProperty(overlapCutOff_propname, String.valueOf(overlapCutOff_default));
50-
props.setProperty(combinedCutoff_propname, String.valueOf(combinedCutoff_default));
51-
props.setProperty(combinedConstant_propname, String.valueOf(combinedConstant_default));
52-
props.setProperty(similarityMetric_propname, String.valueOf(similarityMetric_default));
53-
props.setProperty(distanceMetric_propname, String.valueOf(distanceMetric_default));
54-
props.setProperty(Pvalue_propname, String.valueOf(Pvalue_default));
55-
props.setProperty(Qvalue_propname, String.valueOf(Qvalue_default));
56-
props.setProperty(create_warn_show_propname, String.valueOf(create_warn_show_default));
57-
// remember to increase the number in the if-statement above
58-
}
44+
getAllProperties().forEach(this::setDefault);
5945
}
6046

61-
public boolean getShowCreateWarnings() {
62-
return getValue(create_warn_show_propname, create_warn_show_default, Boolean::valueOf);
63-
}
6447

65-
public void setShowCreateWarnings(boolean show) {
66-
cyProps.getProperties().setProperty(create_warn_show_propname, String.valueOf(show));
48+
public <T> void setValue(Property<T> property, T value) {
49+
cyProperties.getProperties().setProperty(property.key, String.valueOf(value));
6750
}
6851

69-
public double getJaccardCutoff() {
70-
return getValue(jaccardCutOff_propname, jaccardCutOff_default, Double::valueOf);
71-
}
72-
73-
public double getOverlapCutoff() {
74-
return getValue(overlapCutOff_propname, overlapCutOff_default, Double::valueOf);
75-
}
76-
77-
public double getCombinedCutoff() {
78-
return getValue(combinedCutoff_propname, combinedCutoff_default, Double::valueOf);
79-
}
80-
81-
public double getCombinedConstant() {
82-
return getValue(combinedConstant_propname, combinedConstant_default, Double::valueOf);
83-
}
84-
85-
public SimilarityMetric getSimilarityMetric() {
86-
return getValue(similarityMetric_propname, similarityMetric_default, SimilarityMetric::valueOf);
87-
}
88-
89-
public Distance getDistanceMetric() {
90-
return getValue(distanceMetric_propname, distanceMetric_default, Distance::valueOf);
91-
}
92-
93-
public double getPvalue() {
94-
return getValue(Pvalue_propname, Pvalue_default, Double::valueOf);
95-
}
96-
97-
public double getQvalue() {
98-
return getValue(Qvalue_propname, Qvalue_default, Double::valueOf);
99-
}
100-
101-
public boolean isHeatmapAutofocus() {
102-
return getValue(heatmap_autofocus_propname, false, Boolean::valueOf);
103-
}
104-
105-
public void setHeatmapAutofocus(boolean autofocus) {
106-
cyProps.getProperties().setProperty(heatmap_autofocus_propname, String.valueOf(autofocus));
52+
public <T> void setDefault(Property<T> property) {
53+
setValue(property, property.defaultValue);
10754
}
10855

109-
public double getDefaultCutOff(SimilarityMetric metric) {
110-
switch(metric) {
111-
default:
112-
case COMBINED: return getCombinedCutoff();
113-
case JACCARD: return getJaccardCutoff();
114-
case OVERLAP: return getOverlapCutoff();
56+
public <T> T getValue(Property<T> property) {
57+
if(cyProperties == null) // happens in JUnits
58+
return property.defaultValue;
59+
Properties properties = cyProperties.getProperties();
60+
if(properties == null)
61+
return property.defaultValue;
62+
String string = properties.getProperty(property.key);
63+
if(string == null)
64+
return property.defaultValue;
65+
66+
try {
67+
return property.converter.apply(string);
68+
} catch(Exception e) {
69+
return property.defaultValue;
11570
}
11671
}
117-
11872

119-
private <V> V getValue(String name, V defaultVal, Function<String,V> converter) {
120-
if(cyProps == null) // happens in JUnits
121-
return defaultVal;
122-
Properties props = cyProps.getProperties();
123-
if(props == null)
124-
return defaultVal;
125-
String s = props.getProperty(name);
126-
if(name == null)
127-
return defaultVal;
128-
try {
129-
return converter.apply(s);
130-
} catch(Exception e) {
131-
return defaultVal;
73+
@SuppressWarnings("rawtypes")
74+
public static List<Property<?>> getAllProperties() {
75+
List<Property<?>> properties = new ArrayList<>();
76+
for(Field field : PropertyManager.class.getDeclaredFields()) {
77+
if(Modifier.isStatic(field.getModifiers()) && field.getType().equals(Property.class)) {
78+
try {
79+
properties.add((Property)field.get(null));
80+
} catch (IllegalArgumentException | IllegalAccessException e) {
81+
e.printStackTrace();
82+
}
83+
}
13284
}
85+
return properties;
13386
}
134-
135-
13687

137-
13888
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ else if(distinctEdges)
235235
String prefix = legacySupport.getNextAttributePrefix();
236236
EMCreationParameters creationParams =
237237
new EMCreationParameters(prefix, pvalue, qvalue, NESFilter.ALL, Optional.empty(),
238-
metric, similaritycutoff, propertyManager.getCombinedConstant(), edgeStrategy);
238+
metric, similaritycutoff, LegacySupport.combinedConstant_default, edgeStrategy);
239239

240240
//System.out.println(creationParams);
241241
//System.out.println(dataSets);

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ public class ResolverCommandTask extends AbstractTask {
5353
public Integer minExperiments = null;
5454

5555
@Tunable
56-
public Double similarityCutoff = 0.25;
56+
public Double similarityCutoff = LegacySupport.overlapCutOff_default;
5757

5858
@Tunable
5959
public ListSingleSelection<String> similarityMetric;
6060

6161
@Tunable
62-
public double combinedConstant = 0.5;
63-
62+
public double combinedConstant = LegacySupport.combinedConstant_default;
6463

6564

6665
private static final Logger logger = Logger.getLogger(CyUserLog.NAME);
@@ -72,21 +71,17 @@ public class ResolverCommandTask extends AbstractTask {
7271

7372
@Inject
7473
public ResolverCommandTask(PropertyManager propertyManager) {
75-
SimilarityMetric defaultMetric = propertyManager.getSimilarityMetric();
7674
similarityMetric = enumNames(SimilarityMetric.values());
77-
similarityMetric.setSelectedValue(defaultMetric.name());
75+
similarityMetric.setSelectedValue(LegacySupport.similarityMetric_default.name());
7876

7977
edgeStrategy = enumNames(EdgeStrategy.values());
8078
edgeStrategy.setSelectedValue(EdgeStrategy.AUTOMATIC.name());
8179

82-
similarityCutoff = propertyManager.getDefaultCutOff(defaultMetric);
83-
8480
nesFilter = enumNames(NESFilter.values());
8581
nesFilter.setSelectedValue(NESFilter.ALL.name());
8682
}
8783

8884

89-
9085
@Override
9186
public void run(TaskMonitor taskMonitor) throws Exception {
9287
logger.info("Running EnrichmentMap Data Set Resolver Task");

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ public EnrichmentMapParameters(StreamUtil streamUtil, CyApplicationManager appli
195195
this.method = EnrichmentMapParameters.method_GSEA;
196196

197197
//the set of default parameters we want to get
198-
this.defaultJaccardCutOff = propertyManager.getJaccardCutoff();
199-
this.defaultOverlapCutOff = propertyManager.getOverlapCutoff();
200-
this.defaultSimilarityMetric = similarityMetricToString(propertyManager.getSimilarityMetric());
198+
this.defaultJaccardCutOff = LegacySupport.jaccardCutOff_default;
199+
this.defaultOverlapCutOff = LegacySupport.overlapCutOff_default;
200+
this.defaultSimilarityMetric = similarityMetricToString(SimilarityMetric.OVERLAP);
201201
// this.defaultSortMethod = HeatMapParameters.Sort.CLUSTER.display;
202202
// this.defaultDistanceMetric = HeatMapParameters.DistanceMetric.PEARSON_CORRELATION.display;
203203

204-
this.pvalue = propertyManager.getPvalue();
205-
this.qvalue = propertyManager.getQvalue();
206-
this.combinedConstant = propertyManager.getCombinedConstant();
204+
this.pvalue = propertyManager.getValue(PropertyManager.P_VALUE);
205+
this.qvalue = propertyManager.getValue(PropertyManager.Q_VALUE);
206+
this.combinedConstant = LegacySupport.combinedConstant_default;
207207
this.disable_heatmap_autofocus = false;
208208

209209
//choose Jaccard or Overlap as default

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Set;
44

5+
import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters.SimilarityMetric;
56
import org.cytoscape.model.CyNetwork;
67
import org.cytoscape.model.CyNetworkTableManager;
78

@@ -18,6 +19,12 @@ public class LegacySupport {
1819
public static final String DATASET1 = "Dataset 1";
1920
public static final String DATASET2 = "Dataset 2";
2021

22+
public static final double jaccardCutOff_default = 0.25;
23+
public static final double overlapCutOff_default = 0.5;
24+
public static final double combinedCutoff_default = 0.375;
25+
public static final double combinedConstant_default = 0.5;
26+
public static final SimilarityMetric similarityMetric_default = SimilarityMetric.OVERLAP;
27+
2128

2229
@Inject private EnrichmentMapManager emManager;
2330
@Inject private CyNetworkTableManager networkTableManager;

0 commit comments

Comments
 (0)