1 | /** |
---|
2 | * SimpleQueryController Controler |
---|
3 | * |
---|
4 | * Description of my controller |
---|
5 | * |
---|
6 | * @author vincent@ludden.nl |
---|
7 | * @since 20100526 |
---|
8 | * @package dbnp.query |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 959 $ |
---|
12 | * $Author: j.a.m.wesbeek@umail.leidenuniv.nl $ |
---|
13 | * $Date: 2010-10-20 19:13:14 +0000 (wo, 20 okt 2010) $ |
---|
14 | */ |
---|
15 | package dbnp.query |
---|
16 | |
---|
17 | import dbnp.data.* |
---|
18 | import dbnp.studycapturing.Study |
---|
19 | import dbnp.studycapturing.Assay |
---|
20 | import org.compass.core.engine.SearchEngineQueryParseException |
---|
21 | import dbnp.rest.common.CommunicationManager |
---|
22 | |
---|
23 | class SimpleQueryController { |
---|
24 | /** |
---|
25 | * index closure |
---|
26 | */ |
---|
27 | def index = { |
---|
28 | redirect( action:'pages') |
---|
29 | } |
---|
30 | |
---|
31 | def searchableService |
---|
32 | |
---|
33 | def pagesFlow = { |
---|
34 | |
---|
35 | // Starting simpleQuery flow, initialize variables |
---|
36 | onStart { |
---|
37 | println "Starting webflow simpleQuery" |
---|
38 | flow.search_term = null |
---|
39 | flow.search_sa_compounds = [] |
---|
40 | flow.search_sa_operators = [] |
---|
41 | flow.search_sa_values = [] |
---|
42 | flow.page = 0 |
---|
43 | flow.pages = [ |
---|
44 | [title: 'Query'], |
---|
45 | [title: 'Results'] |
---|
46 | ] |
---|
47 | } |
---|
48 | |
---|
49 | // Render the query page and handle its actions |
---|
50 | query { |
---|
51 | render(view: "/simpleQuery/mainPage") |
---|
52 | |
---|
53 | onRender { |
---|
54 | println "Rendering mainPage" |
---|
55 | flow.operators = ['>', '=', '<'] |
---|
56 | |
---|
57 | if (!flow.search_sa_compounds) { |
---|
58 | flow.showFirstRowCompounds = true |
---|
59 | println "showRow true" |
---|
60 | } else { |
---|
61 | flow.showFirstRowCompounds = false |
---|
62 | println "showRow false" |
---|
63 | } |
---|
64 | |
---|
65 | flow.species = Term.findAll() |
---|
66 | flow.page = 1 |
---|
67 | } |
---|
68 | |
---|
69 | on("search") { |
---|
70 | println "Search!" |
---|
71 | if (!params.search_term.trim()) { |
---|
72 | return [:] |
---|
73 | } |
---|
74 | }.to "searching" |
---|
75 | |
---|
76 | on("refresh").to "query" |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | // Searching for results |
---|
81 | searching { |
---|
82 | action { |
---|
83 | println "Starting simpleQuery search..." |
---|
84 | def searchResult |
---|
85 | def searchGscfResult |
---|
86 | def searchSamResult = [] |
---|
87 | |
---|
88 | // TODO: walk parameters, remove empty entries |
---|
89 | |
---|
90 | // Map GSCF parameters |
---|
91 | flow.search_term = params.search_term // String |
---|
92 | |
---|
93 | // Map SAM parameters |
---|
94 | if (params.sa_compound instanceof String) { |
---|
95 | println "Compounds as String" |
---|
96 | //flow.search_sam = [:] |
---|
97 | flow.search_sa_compounds = [] |
---|
98 | flow.search_sa_operators = [] |
---|
99 | flow.search_sa_values = [] |
---|
100 | |
---|
101 | if (params.sa_compound) { |
---|
102 | flow.search_sa_compounds.add(params.sa_compound) |
---|
103 | flow.search_sa_operators.add(params.sa_operator) |
---|
104 | flow.search_sa_values.add(params.sa_value) |
---|
105 | } |
---|
106 | } else { |
---|
107 | println "Compounds as List" |
---|
108 | flow.search_sa_compounds = params.sa_compound as List |
---|
109 | flow.search_sa_operators = params.sa_operator as List |
---|
110 | flow.search_sa_values = params.sa_value as List |
---|
111 | } |
---|
112 | |
---|
113 | // Search the keyword with the Searchable plugin |
---|
114 | try { |
---|
115 | searchGscfResult = searchableService.search(flow.search_term) |
---|
116 | println "RESULT: " + searchGscfResult |
---|
117 | } catch (SearchEngineQueryParseException ex) { |
---|
118 | println ex |
---|
119 | return [parseException: true] |
---|
120 | } |
---|
121 | |
---|
122 | // Map non-study objects to Studies |
---|
123 | // ... todo when the plugin works and I can see the output |
---|
124 | |
---|
125 | // Search in the SAM module when a compound is entered |
---|
126 | // Todo: check whether the module is active and to be used |
---|
127 | // ... |
---|
128 | def listSamStudies = [] |
---|
129 | def listGscfStudies = [] |
---|
130 | def listStudies = [] |
---|
131 | |
---|
132 | if ((flow.search_sa_compounds) && (flow.search_sa_compounds.size() > 0)) { |
---|
133 | def resultSAM = [:] |
---|
134 | resultSAM = this.searchSAM(flow.search_sa_compounds, flow.search_sa_operators, flow.search_sa_values) |
---|
135 | println "Sam result: " + resultSAM |
---|
136 | listSamStudies = resultSAM.get('studies') |
---|
137 | } |
---|
138 | |
---|
139 | for (i in searchGscfResult.results) { |
---|
140 | //def x = i.id |
---|
141 | def objStudy = Study.get(i.id) |
---|
142 | println objStudy |
---|
143 | listGscfStudies.add(objStudy.id) |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | println "GSCF studies: " + listGscfStudies |
---|
148 | println "Sam studies " + listSamStudies |
---|
149 | |
---|
150 | // Merge the results of all searches |
---|
151 | if (listSamStudies.size() > 0) { |
---|
152 | listStudies = listGscfStudies.intersect(listSamStudies) |
---|
153 | println "Combined: " + listStudies |
---|
154 | } else { |
---|
155 | if (!flow.search_sa_compounds) { |
---|
156 | listStudies = listGscfStudies |
---|
157 | } else { |
---|
158 | listStudies = [] |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | def listObjStudies = [] |
---|
163 | for (i in listStudies) { |
---|
164 | def objStudy = Study.get(i) |
---|
165 | listObjStudies.add(objStudy) |
---|
166 | } |
---|
167 | |
---|
168 | // Save the results in the flow |
---|
169 | flow.listStudies = listObjStudies |
---|
170 | println flow.listStudies |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | on("error").to "query" |
---|
175 | on("success").to "results" |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | // Render result page including search options |
---|
180 | results { |
---|
181 | render(view: "/simpleQuery/mainPage") |
---|
182 | |
---|
183 | onRender { |
---|
184 | println "Rendering resultPage" |
---|
185 | flow.page = 2 |
---|
186 | |
---|
187 | flow.showFirstRowCompounds = false |
---|
188 | } |
---|
189 | |
---|
190 | on("reset") { |
---|
191 | flow.search_term = null |
---|
192 | flow.studies = null |
---|
193 | flow.search_sa_compounds = [] |
---|
194 | flow.search_sa_operators = [] |
---|
195 | flow.search_sa_values = [] |
---|
196 | flow.search_tt_genepaths = null |
---|
197 | flow.search_tt_regulations = null |
---|
198 | println "Resetting query flow" |
---|
199 | }.to "query" |
---|
200 | |
---|
201 | on("search").to "searching" |
---|
202 | on("refresh").to "results" |
---|
203 | } |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | static Map searchSAM (List compounds, List operators, List values) { |
---|
209 | if (compounds.size() == 1) { |
---|
210 | println "Single SAM call" |
---|
211 | def mapSamResult = [:] |
---|
212 | |
---|
213 | //def listAssays = [3, 1] |
---|
214 | //mapSamResult.put("assays", listAssays) |
---|
215 | //println "CommMngr result: " + mapSamResult |
---|
216 | |
---|
217 | CommunicationManager.addRestWrapper( 'http://localhost:8182/sam/rest', 'getQueryResult', ['query'] ) |
---|
218 | mapSamResult = CommunicationManager.getQueryResult( compounds.get(0) ) |
---|
219 | println "SAM REST query: " + compounds.get(0) |
---|
220 | println "SAM REST result: " + mapSamResult |
---|
221 | |
---|
222 | // mapSamResult = CommunicationManager.getQueryResult(compounds.get(0), operators.get(0), values.get(0)) |
---|
223 | |
---|
224 | //objAssay = objAssay.get(i) |
---|
225 | //println "Assay: " + objAssay |
---|
226 | |
---|
227 | /* |
---|
228 | for (i in mapSamResult.assays) { |
---|
229 | //def listStudies = Study.findAll("from Study as s where s.assays.id = " + i) |
---|
230 | def listStudies = Study.findAll("from Study as s where exists (from Assay as a where a.id = s.assays and a.id = ${i})") |
---|
231 | println "Studies found: " + listStudies |
---|
232 | } |
---|
233 | */ |
---|
234 | |
---|
235 | def listStudies = [] |
---|
236 | |
---|
237 | for (i in mapSamResult.assays) { |
---|
238 | def objAssay = Assay.get(i) |
---|
239 | listStudies.add(objAssay.parent.id) |
---|
240 | } |
---|
241 | |
---|
242 | mapSamResult.put("studies", listStudies) |
---|
243 | |
---|
244 | return mapSamResult |
---|
245 | |
---|
246 | } else { |
---|
247 | println "Multiple SAM calls" |
---|
248 | def tmpSamResult = [:] |
---|
249 | def mapSamResult = [assays:[]] |
---|
250 | def i = 0 |
---|
251 | |
---|
252 | compounds.each { compound -> |
---|
253 | println "SAM Search with " + compound |
---|
254 | CommunicationManager.addRestWrapper( 'http://localhost:8182/sam/rest', 'getQueryResult', ['query'] ) |
---|
255 | tmpSamResult = CommunicationManager.getQueryResult(compound) |
---|
256 | println "tmpsamres: " + tmpSamResult |
---|
257 | |
---|
258 | if (i == 0) { |
---|
259 | mapSamResult.assays = tmpSamResult.assays |
---|
260 | } else { |
---|
261 | if (mapSamResult.assays) { |
---|
262 | mapSamResult.assays = mapSamResult.assays.intersect(tmpSamResult.assays) |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | i++ |
---|
267 | } |
---|
268 | |
---|
269 | def listStudies = [] |
---|
270 | |
---|
271 | for (j in mapSamResult.assays) { |
---|
272 | def objAssay = Assay.get(j) |
---|
273 | listStudies.add(objAssay.parent.id) |
---|
274 | } |
---|
275 | |
---|
276 | mapSamResult.put("studies", listStudies) |
---|
277 | |
---|
278 | return mapSamResult |
---|
279 | } |
---|
280 | |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | static List merge (List list1, List list2) { |
---|
286 | |
---|
287 | def resultList = [] |
---|
288 | resultList = list1.intersect(list2) |
---|
289 | |
---|
290 | return resultList |
---|
291 | } |
---|
292 | |
---|
293 | } |
---|