|
1 | 1 | package org.baderlab.csplugins.enrichmentmap;
|
2 | 2 |
|
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.Modifier; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
3 | 7 | import java.util.Properties;
|
4 | 8 | import java.util.function.Function;
|
5 | 9 |
|
6 |
| -import org.baderlab.csplugins.enrichmentmap.model.EMCreationParameters.SimilarityMetric; |
7 | 10 | import org.baderlab.csplugins.enrichmentmap.view.heatmap.HeatMapParams.Distance;
|
8 | 11 | import org.cytoscape.property.CyProperty;
|
9 | 12 |
|
|
16 | 19 | @Singleton
|
17 | 20 | public class PropertyManager {
|
18 | 21 |
|
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 | + } |
30 | 33 |
|
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); |
40 | 39 |
|
41 |
| - @Inject private CyProperty<Properties> cyProps; |
| 40 | + @Inject private CyProperty<Properties> cyProperties; |
42 | 41 |
|
43 | 42 | @AfterInjection
|
44 | 43 | 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); |
59 | 45 | }
|
60 | 46 |
|
61 |
| - public boolean getShowCreateWarnings() { |
62 |
| - return getValue(create_warn_show_propname, create_warn_show_default, Boolean::valueOf); |
63 |
| - } |
64 | 47 |
|
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)); |
67 | 50 | }
|
68 | 51 |
|
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); |
107 | 54 | }
|
108 | 55 |
|
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; |
115 | 70 | }
|
116 | 71 | }
|
117 |
| - |
118 | 72 |
|
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 | + } |
132 | 84 | }
|
| 85 | + return properties; |
133 | 86 | }
|
134 |
| - |
135 |
| - |
136 | 87 |
|
137 |
| - |
138 | 88 | }
|
0 commit comments