5626 |
24 Sep 19 |
nicklas |
1 |
package net.sf.basedb.reggie.ssp; |
5626 |
24 Sep 19 |
nicklas |
2 |
|
5626 |
24 Sep 19 |
nicklas |
3 |
import java.util.ArrayList; |
5962 |
03 Jun 20 |
nicklas |
4 |
import java.util.HashMap; |
5626 |
24 Sep 19 |
nicklas |
5 |
import java.util.List; |
5962 |
03 Jun 20 |
nicklas |
6 |
import java.util.Map; |
5626 |
24 Sep 19 |
nicklas |
7 |
|
5626 |
24 Sep 19 |
nicklas |
8 |
import org.jdom2.Element; |
5626 |
24 Sep 19 |
nicklas |
9 |
|
5626 |
24 Sep 19 |
nicklas |
10 |
import net.sf.basedb.core.AnnotationType; |
5626 |
24 Sep 19 |
nicklas |
11 |
import net.sf.basedb.core.DbControl; |
5626 |
24 Sep 19 |
nicklas |
12 |
import net.sf.basedb.core.Item; |
5626 |
24 Sep 19 |
nicklas |
13 |
import net.sf.basedb.core.ItemQuery; |
5626 |
24 Sep 19 |
nicklas |
14 |
import net.sf.basedb.core.Type; |
5626 |
24 Sep 19 |
nicklas |
15 |
import net.sf.basedb.core.query.Expressions; |
5626 |
24 Sep 19 |
nicklas |
16 |
import net.sf.basedb.core.query.Hql; |
5626 |
24 Sep 19 |
nicklas |
17 |
import net.sf.basedb.core.query.Restrictions; |
5626 |
24 Sep 19 |
nicklas |
18 |
import net.sf.basedb.reggie.Reggie; |
5626 |
24 Sep 19 |
nicklas |
19 |
import net.sf.basedb.reggie.XmlConfig; |
5626 |
24 Sep 19 |
nicklas |
20 |
|
5626 |
24 Sep 19 |
nicklas |
21 |
/** |
5626 |
24 Sep 19 |
nicklas |
Instances of this class represents a single SSP model and holds |
5626 |
24 Sep 19 |
nicklas |
a mapping to an annotation type for importing the results. |
5626 |
24 Sep 19 |
nicklas |
24 |
|
5626 |
24 Sep 19 |
nicklas |
Models are typically configured in reggie-config.xml inside |
5626 |
24 Sep 19 |
nicklas |
the /reggie/rscript/ssp/models tag. Each <model> entry |
5626 |
24 Sep 19 |
nicklas |
has a value that points to the filename of the model data and |
5626 |
24 Sep 19 |
nicklas |
it should also have a "name", and "annotation-type" attribute. |
5626 |
24 Sep 19 |
nicklas |
For example: |
5626 |
24 Sep 19 |
nicklas |
30 |
|
5626 |
24 Sep 19 |
nicklas |
<model name="Subtype" annotation-type="SSP_Subtype"> |
5626 |
24 Sep 19 |
nicklas |
Training_Run19081Genes_noNorm_SSP.subtypeMost.Fcc15_5x5foldCV.num.rules.50_24.selRules.AIMS.GS.RData |
5626 |
24 Sep 19 |
nicklas |
</model> |
5626 |
24 Sep 19 |
nicklas |
34 |
|
5626 |
24 Sep 19 |
nicklas |
@since 4.24 |
5626 |
24 Sep 19 |
nicklas |
36 |
*/ |
5626 |
24 Sep 19 |
nicklas |
37 |
public class SspModel |
5926 |
29 Apr 20 |
nicklas |
38 |
implements Comparable<SspModel> |
5626 |
24 Sep 19 |
nicklas |
39 |
{ |
5626 |
24 Sep 19 |
nicklas |
40 |
|
5626 |
24 Sep 19 |
nicklas |
41 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get a list with all SSP models that has been configured in reggie-config.xml |
5626 |
24 Sep 19 |
nicklas |
43 |
*/ |
5626 |
24 Sep 19 |
nicklas |
44 |
public static List<SspModel> getConfiguredModels() |
5626 |
24 Sep 19 |
nicklas |
45 |
{ |
5626 |
24 Sep 19 |
nicklas |
46 |
XmlConfig cfg = Reggie.getConfig(); |
5626 |
24 Sep 19 |
nicklas |
47 |
List<Element> elements = cfg.getElements("rscript/ssp/models/model"); |
5626 |
24 Sep 19 |
nicklas |
48 |
|
5626 |
24 Sep 19 |
nicklas |
49 |
List<SspModel> models = new ArrayList<>(); |
5626 |
24 Sep 19 |
nicklas |
50 |
for (Element e : elements) |
5626 |
24 Sep 19 |
nicklas |
51 |
{ |
5626 |
24 Sep 19 |
nicklas |
52 |
models.add(new SspModel(e)); |
5626 |
24 Sep 19 |
nicklas |
53 |
} |
5626 |
24 Sep 19 |
nicklas |
54 |
|
5626 |
24 Sep 19 |
nicklas |
55 |
return models; |
5626 |
24 Sep 19 |
nicklas |
56 |
} |
5626 |
24 Sep 19 |
nicklas |
57 |
|
5626 |
24 Sep 19 |
nicklas |
58 |
/** |
5942 |
18 May 20 |
nicklas |
Get a list with all SSP models that has a valid annotation type and |
5942 |
18 May 20 |
nicklas |
.Rdata file. Eg. this method returns models where {@link #getAnnotationType(DbControl)} |
5942 |
18 May 20 |
nicklas |
returns an existing annotation type and {@link #modelDataExists()} returns true. |
5942 |
18 May 20 |
nicklas |
62 |
*/ |
5942 |
18 May 20 |
nicklas |
63 |
public static List<SspModel> getValidModels(DbControl dc) |
5942 |
18 May 20 |
nicklas |
64 |
{ |
5942 |
18 May 20 |
nicklas |
65 |
List<SspModel> models = new ArrayList<SspModel>(); |
5942 |
18 May 20 |
nicklas |
66 |
for (SspModel m : getConfiguredModels()) |
5942 |
18 May 20 |
nicklas |
67 |
{ |
5942 |
18 May 20 |
nicklas |
68 |
if (m.modelDataExists() && m.getAnnotationType(dc) != null) |
5942 |
18 May 20 |
nicklas |
69 |
{ |
5942 |
18 May 20 |
nicklas |
70 |
models.add(m); |
5942 |
18 May 20 |
nicklas |
71 |
} |
5942 |
18 May 20 |
nicklas |
72 |
} |
5942 |
18 May 20 |
nicklas |
73 |
return models; |
5942 |
18 May 20 |
nicklas |
74 |
} |
5942 |
18 May 20 |
nicklas |
75 |
|
5942 |
18 May 20 |
nicklas |
76 |
/** |
5922 |
27 Apr 20 |
nicklas |
Get a list with all SSP model names. |
5922 |
27 Apr 20 |
nicklas |
@since 4.27 |
5922 |
27 Apr 20 |
nicklas |
79 |
*/ |
5922 |
27 Apr 20 |
nicklas |
80 |
public static List<String> getModelNames() |
5922 |
27 Apr 20 |
nicklas |
81 |
{ |
5922 |
27 Apr 20 |
nicklas |
82 |
XmlConfig cfg = Reggie.getConfig(); |
5922 |
27 Apr 20 |
nicklas |
83 |
List<Element> elements = cfg.getElements("rscript/ssp/models/model"); |
5922 |
27 Apr 20 |
nicklas |
84 |
|
5922 |
27 Apr 20 |
nicklas |
85 |
List<String> models = new ArrayList<>(); |
5922 |
27 Apr 20 |
nicklas |
86 |
for (Element e : elements) |
5922 |
27 Apr 20 |
nicklas |
87 |
{ |
5922 |
27 Apr 20 |
nicklas |
88 |
models.add(e.getAttributeValue("name")); |
5922 |
27 Apr 20 |
nicklas |
89 |
} |
5922 |
27 Apr 20 |
nicklas |
90 |
return models; |
5922 |
27 Apr 20 |
nicklas |
91 |
} |
5922 |
27 Apr 20 |
nicklas |
92 |
|
5922 |
27 Apr 20 |
nicklas |
93 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get the SSP model with the given name. Return null |
5626 |
24 Sep 19 |
nicklas |
if no model with the given name can be found. |
5626 |
24 Sep 19 |
nicklas |
96 |
*/ |
5626 |
24 Sep 19 |
nicklas |
97 |
public static SspModel getModelByName(String name) |
5626 |
24 Sep 19 |
nicklas |
98 |
{ |
5626 |
24 Sep 19 |
nicklas |
99 |
XmlConfig cfg = Reggie.getConfig(); |
5626 |
24 Sep 19 |
nicklas |
100 |
Element e = cfg.getElement("rscript/ssp/models/model[@name='"+name+"']"); |
5626 |
24 Sep 19 |
nicklas |
101 |
return e == null ? null : new SspModel(e); |
5626 |
24 Sep 19 |
nicklas |
102 |
} |
5626 |
24 Sep 19 |
nicklas |
103 |
|
5962 |
03 Jun 20 |
nicklas |
104 |
/** |
5962 |
03 Jun 20 |
nicklas |
Get translations that should should be applied to results from SSP |
5962 |
03 Jun 20 |
nicklas |
models before they are stored as annotations in BASE. |
5962 |
03 Jun 20 |
nicklas |
@since 4.27.1 |
5962 |
03 Jun 20 |
nicklas |
108 |
*/ |
5962 |
03 Jun 20 |
nicklas |
109 |
public static Map<String, String> getTranslations() |
5962 |
03 Jun 20 |
nicklas |
110 |
{ |
5962 |
03 Jun 20 |
nicklas |
111 |
XmlConfig cfg = Reggie.getConfig(); |
5962 |
03 Jun 20 |
nicklas |
112 |
List<Element> elements = cfg.getElements("rscript/ssp/translations/text"); |
5962 |
03 Jun 20 |
nicklas |
113 |
Map<String, String> translations = new HashMap<>(); |
5962 |
03 Jun 20 |
nicklas |
114 |
for (Element e : elements) |
5962 |
03 Jun 20 |
nicklas |
115 |
{ |
5962 |
03 Jun 20 |
nicklas |
116 |
translations.put(e.getAttributeValue("from").toLowerCase(), e.getAttributeValue("to")); |
5962 |
03 Jun 20 |
nicklas |
117 |
} |
5962 |
03 Jun 20 |
nicklas |
118 |
return translations; |
5962 |
03 Jun 20 |
nicklas |
119 |
} |
5962 |
03 Jun 20 |
nicklas |
120 |
|
5626 |
24 Sep 19 |
nicklas |
121 |
private final String name; |
5626 |
24 Sep 19 |
nicklas |
122 |
private final String modelData; |
5626 |
24 Sep 19 |
nicklas |
123 |
private final String annotationTypeName; |
5937 |
15 May 20 |
nicklas |
124 |
private final String annotationTypeNameScores; |
5962 |
03 Jun 20 |
nicklas |
125 |
private final String description; |
5626 |
24 Sep 19 |
nicklas |
126 |
private AnnotationType annotationType; |
5937 |
15 May 20 |
nicklas |
127 |
private AnnotationType annotationTypeScores; |
5937 |
15 May 20 |
nicklas |
128 |
private boolean hasSearchedForAnnotationTypes; |
5626 |
24 Sep 19 |
nicklas |
129 |
|
5626 |
24 Sep 19 |
nicklas |
130 |
SspModel(Element e) |
5626 |
24 Sep 19 |
nicklas |
131 |
{ |
5626 |
24 Sep 19 |
nicklas |
132 |
this.name = e.getAttributeValue("name"); |
5626 |
24 Sep 19 |
nicklas |
133 |
this.modelData = e.getTextTrim(); |
5626 |
24 Sep 19 |
nicklas |
134 |
this.annotationTypeName = e.getAttributeValue("annotation-type"); |
5937 |
15 May 20 |
nicklas |
135 |
this.annotationTypeNameScores = e.getAttributeValue("annotation-type-scores"); |
5962 |
03 Jun 20 |
nicklas |
136 |
this.description = e.getAttributeValue("description"); |
5626 |
24 Sep 19 |
nicklas |
137 |
} |
5626 |
24 Sep 19 |
nicklas |
138 |
|
5626 |
24 Sep 19 |
nicklas |
139 |
|
5626 |
24 Sep 19 |
nicklas |
140 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get the name of the model. Configured in reggie-config.xml, "name" attribute of the "model" tag. |
5626 |
24 Sep 19 |
nicklas |
142 |
*/ |
5626 |
24 Sep 19 |
nicklas |
143 |
public String getName() |
5626 |
24 Sep 19 |
nicklas |
144 |
{ |
5626 |
24 Sep 19 |
nicklas |
145 |
return name; |
5626 |
24 Sep 19 |
nicklas |
146 |
} |
5626 |
24 Sep 19 |
nicklas |
147 |
|
5626 |
24 Sep 19 |
nicklas |
148 |
/** |
5962 |
03 Jun 20 |
nicklas |
Get a description of the SSP model. |
5962 |
03 Jun 20 |
nicklas |
150 |
*/ |
5962 |
03 Jun 20 |
nicklas |
151 |
public String getDescription() |
5962 |
03 Jun 20 |
nicklas |
152 |
{ |
5962 |
03 Jun 20 |
nicklas |
153 |
return description; |
5962 |
03 Jun 20 |
nicklas |
154 |
} |
5962 |
03 Jun 20 |
nicklas |
155 |
|
5962 |
03 Jun 20 |
nicklas |
156 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get the name of the file with model data. Configured in reggie-config.xml, value of the "model" tag. |
5626 |
24 Sep 19 |
nicklas |
158 |
*/ |
5626 |
24 Sep 19 |
nicklas |
159 |
public String getModelData() |
5626 |
24 Sep 19 |
nicklas |
160 |
{ |
5626 |
24 Sep 19 |
nicklas |
161 |
return modelData; |
5626 |
24 Sep 19 |
nicklas |
162 |
} |
5941 |
18 May 20 |
nicklas |
163 |
|
5927 |
29 Apr 20 |
nicklas |
164 |
/** |
5941 |
18 May 20 |
nicklas |
Check if the .Rdata file actually exists at the expected location. |
5927 |
29 Apr 20 |
nicklas |
166 |
*/ |
5941 |
18 May 20 |
nicklas |
167 |
public boolean modelDataExists() |
5927 |
29 Apr 20 |
nicklas |
168 |
{ |
5941 |
18 May 20 |
nicklas |
169 |
XmlConfig config = Reggie.getConfig(); |
5941 |
18 May 20 |
nicklas |
// Check that files and directories exists |
5941 |
18 May 20 |
nicklas |
171 |
String sourceDir = config.getConfig("rscript/ssp/path", null, null); |
5941 |
18 May 20 |
nicklas |
172 |
String modelsDir = config.getConfig("rscript/ssp/models-dir", null, sourceDir + "/models"); |
5941 |
18 May 20 |
nicklas |
173 |
java.io.File rdataFile = new java.io.File(modelsDir, modelData); |
5941 |
18 May 20 |
nicklas |
174 |
return rdataFile.isFile(); |
5927 |
29 Apr 20 |
nicklas |
175 |
} |
5626 |
24 Sep 19 |
nicklas |
176 |
|
5626 |
24 Sep 19 |
nicklas |
177 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get the name of the mapped annotation type. |
5626 |
24 Sep 19 |
nicklas |
Configured in reggie-config.xml, "annotation-type" attribute of the "model" tag. |
5626 |
24 Sep 19 |
nicklas |
180 |
*/ |
5626 |
24 Sep 19 |
nicklas |
181 |
public String getAnnotationTypeName() |
5626 |
24 Sep 19 |
nicklas |
182 |
{ |
5626 |
24 Sep 19 |
nicklas |
183 |
return annotationTypeName; |
5626 |
24 Sep 19 |
nicklas |
184 |
} |
5626 |
24 Sep 19 |
nicklas |
185 |
|
5626 |
24 Sep 19 |
nicklas |
186 |
/** |
5937 |
15 May 20 |
nicklas |
Get the name of the mapped annotation type for scores. |
5937 |
15 May 20 |
nicklas |
Configured in reggie-config.xml, "annotation-type-scores" attribute of the "model" tag. |
5937 |
15 May 20 |
nicklas |
189 |
*/ |
5937 |
15 May 20 |
nicklas |
190 |
public String getAnnotationTypeNameScores() |
5937 |
15 May 20 |
nicklas |
191 |
{ |
5937 |
15 May 20 |
nicklas |
192 |
return annotationTypeNameScores; |
5937 |
15 May 20 |
nicklas |
193 |
} |
5937 |
15 May 20 |
nicklas |
194 |
|
5937 |
15 May 20 |
nicklas |
195 |
/** |
5626 |
24 Sep 19 |
nicklas |
Get the mapped annotation type. Returns null if an |
5626 |
24 Sep 19 |
nicklas |
annotation type with expected name can't be found. |
5626 |
24 Sep 19 |
nicklas |
198 |
*/ |
5626 |
24 Sep 19 |
nicklas |
199 |
public AnnotationType getAnnotationType(DbControl dc) |
5626 |
24 Sep 19 |
nicklas |
200 |
{ |
5937 |
15 May 20 |
nicklas |
201 |
if (!hasSearchedForAnnotationTypes) searchForAnnotationTypes(dc); |
5937 |
15 May 20 |
nicklas |
202 |
return annotationType; |
5937 |
15 May 20 |
nicklas |
203 |
} |
5626 |
24 Sep 19 |
nicklas |
204 |
|
5937 |
15 May 20 |
nicklas |
205 |
public AnnotationType getAnnotationTypeScores(DbControl dc) |
5937 |
15 May 20 |
nicklas |
206 |
{ |
5937 |
15 May 20 |
nicklas |
207 |
if (!hasSearchedForAnnotationTypes) searchForAnnotationTypes(dc); |
5937 |
15 May 20 |
nicklas |
208 |
return annotationTypeScores; |
5937 |
15 May 20 |
nicklas |
209 |
} |
5937 |
15 May 20 |
nicklas |
210 |
|
5937 |
15 May 20 |
nicklas |
211 |
private void searchForAnnotationTypes(DbControl dc) |
5937 |
15 May 20 |
nicklas |
212 |
{ |
5937 |
15 May 20 |
nicklas |
213 |
hasSearchedForAnnotationTypes = true; |
5626 |
24 Sep 19 |
nicklas |
214 |
if (annotationTypeName != null) |
5626 |
24 Sep 19 |
nicklas |
215 |
{ |
5937 |
15 May 20 |
nicklas |
216 |
ItemQuery<AnnotationType> query = getQuery(); |
5937 |
15 May 20 |
nicklas |
217 |
query.setParameter("name", annotationTypeName, Type.STRING); |
5626 |
24 Sep 19 |
nicklas |
218 |
List<AnnotationType> list = query.list(dc); |
5937 |
15 May 20 |
nicklas |
219 |
if (list.size() > 0) annotationType = list.get(0); |
5937 |
15 May 20 |
nicklas |
220 |
|
5937 |
15 May 20 |
nicklas |
221 |
if (annotationTypeNameScores != null) |
5626 |
24 Sep 19 |
nicklas |
222 |
{ |
5937 |
15 May 20 |
nicklas |
223 |
query.setParameter("name", annotationTypeNameScores, Type.STRING); |
5937 |
15 May 20 |
nicklas |
224 |
list = query.list(dc); |
5937 |
15 May 20 |
nicklas |
225 |
if (list.size() > 0) annotationTypeScores = list.get(0); |
5626 |
24 Sep 19 |
nicklas |
226 |
} |
5626 |
24 Sep 19 |
nicklas |
227 |
} |
5626 |
24 Sep 19 |
nicklas |
228 |
} |
5937 |
15 May 20 |
nicklas |
229 |
|
5937 |
15 May 20 |
nicklas |
230 |
private ItemQuery<AnnotationType> getQuery() |
5937 |
15 May 20 |
nicklas |
231 |
{ |
5937 |
15 May 20 |
nicklas |
232 |
ItemQuery<AnnotationType> query = AnnotationType.getQuery(Item.RAWBIOASSAY); |
5937 |
15 May 20 |
nicklas |
233 |
query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT); |
5937 |
15 May 20 |
nicklas |
234 |
query.restrict( |
5937 |
15 May 20 |
nicklas |
235 |
Restrictions.eq( |
5937 |
15 May 20 |
nicklas |
236 |
Hql.property("name"), |
5937 |
15 May 20 |
nicklas |
237 |
Expressions.parameter("name", Type.STRING) |
5937 |
15 May 20 |
nicklas |
238 |
)); |
5937 |
15 May 20 |
nicklas |
239 |
query.setMaxResults(1); |
5937 |
15 May 20 |
nicklas |
240 |
return query; |
5937 |
15 May 20 |
nicklas |
241 |
} |
5937 |
15 May 20 |
nicklas |
242 |
|
5626 |
24 Sep 19 |
nicklas |
243 |
@Override |
5626 |
24 Sep 19 |
nicklas |
244 |
public int hashCode() |
5626 |
24 Sep 19 |
nicklas |
245 |
{ |
5626 |
24 Sep 19 |
nicklas |
246 |
return name.hashCode(); |
5626 |
24 Sep 19 |
nicklas |
247 |
} |
5626 |
24 Sep 19 |
nicklas |
248 |
|
5626 |
24 Sep 19 |
nicklas |
249 |
@Override |
5626 |
24 Sep 19 |
nicklas |
250 |
public boolean equals(Object obj) |
5626 |
24 Sep 19 |
nicklas |
251 |
{ |
5626 |
24 Sep 19 |
nicklas |
252 |
if (!(obj instanceof SspModel)) return false; |
5626 |
24 Sep 19 |
nicklas |
253 |
SspModel other = (SspModel)obj; |
5626 |
24 Sep 19 |
nicklas |
254 |
return name.equals(other.name); |
5626 |
24 Sep 19 |
nicklas |
255 |
} |
5626 |
24 Sep 19 |
nicklas |
256 |
|
5626 |
24 Sep 19 |
nicklas |
257 |
@Override |
5626 |
24 Sep 19 |
nicklas |
258 |
public String toString() |
5626 |
24 Sep 19 |
nicklas |
259 |
{ |
5626 |
24 Sep 19 |
nicklas |
260 |
return "SspModel["+name+"]"; |
5626 |
24 Sep 19 |
nicklas |
261 |
} |
5626 |
24 Sep 19 |
nicklas |
262 |
|
5926 |
29 Apr 20 |
nicklas |
263 |
@Override |
5926 |
29 Apr 20 |
nicklas |
264 |
public int compareTo(SspModel other) |
5926 |
29 Apr 20 |
nicklas |
265 |
{ |
5926 |
29 Apr 20 |
nicklas |
266 |
return name.compareTo(other.name); |
5926 |
29 Apr 20 |
nicklas |
267 |
} |
5926 |
29 Apr 20 |
nicklas |
268 |
|
5626 |
24 Sep 19 |
nicklas |
269 |
} |