1 | /** |
---|
2 | * SampleSearch Domain Class |
---|
3 | * |
---|
4 | * This class provides querying capabilities for searching for samples |
---|
5 | * |
---|
6 | * @author Robert Horlings (robert@isdat.nl) |
---|
7 | * @since 20110118 |
---|
8 | * @package dbnp.query |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1436 $ |
---|
12 | * $Author: robert@isdat.nl $ |
---|
13 | * $Date: 2011-01-24 14:48:46 +0000 (ma, 24 jan 2011) $ |
---|
14 | */ |
---|
15 | package dbnp.query |
---|
16 | |
---|
17 | import java.util.List |
---|
18 | import dbnp.studycapturing.* |
---|
19 | import nl.grails.plugins.gdt.* |
---|
20 | |
---|
21 | class SampleSearch extends Search { |
---|
22 | public SampleSearch() { |
---|
23 | this.entity = "Sample"; |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Searches for samples based on the given criteria. All criteria have to be satisfied and |
---|
28 | * criteria for the different entities are satisfied as follows: |
---|
29 | * |
---|
30 | * Sample.title = 'abc' |
---|
31 | * Only samples are returned from studies with title 'abc' |
---|
32 | * |
---|
33 | * Subject.species = 'human' |
---|
34 | * Only samples are returned from subjects with species = 'human' |
---|
35 | * |
---|
36 | * Sample.name = 'sample 1' |
---|
37 | * Only samples are returned with name = 'sample 1' |
---|
38 | * |
---|
39 | * Event.startTime = '0s' |
---|
40 | * Only samples are returned from subjects that have had an event with start time = '0s' |
---|
41 | * |
---|
42 | * SamplingEvent.startTime = '0s' |
---|
43 | * Only samples are returned that have originated from a sampling event with start time = '0s' |
---|
44 | * |
---|
45 | * Assay.module = 'metagenomics' |
---|
46 | * Only samples are returned that have been processed in an assay with module = metagenomics |
---|
47 | * |
---|
48 | * When searching for more than one criterion per entity, these are taken combined. Searching for |
---|
49 | * |
---|
50 | * Subject.species = 'human' |
---|
51 | * Subject.name = 'Jan' |
---|
52 | * |
---|
53 | * will result in all samples from a human subject named 'Jan'. Samples from a mouse subject |
---|
54 | * named 'Jan' or a human subject named 'Kees' won't satisfy the criteria. |
---|
55 | * |
---|
56 | */ |
---|
57 | @Override |
---|
58 | void execute() { |
---|
59 | // TODO: check for authorization for these studies? |
---|
60 | |
---|
61 | // If no criteria are found, return all samples |
---|
62 | if( !criteria || criteria.size() == 0 ) { |
---|
63 | results = Sample.list(); |
---|
64 | return; |
---|
65 | } |
---|
66 | |
---|
67 | // We expect the sample criteria to be the most discriminative, and discard |
---|
68 | // the most samples. (e.g. by searching on sample title of sample type). For |
---|
69 | // that reason we first look through the list of studies. However, when the |
---|
70 | // user didn't enter any sample criteria, this will be an extra step, but doesn't |
---|
71 | // cost much time to process. |
---|
72 | def samples = [] |
---|
73 | if( getEntityCriteria( 'Study' ).size() > 0 ) { |
---|
74 | def studies = Study.findAll(); |
---|
75 | |
---|
76 | studies = filterOnStudyCriteria( studies ); |
---|
77 | |
---|
78 | if( studies.size() == 0 ) { |
---|
79 | results = []; |
---|
80 | return; |
---|
81 | } |
---|
82 | |
---|
83 | def c = Sample.createCriteria() |
---|
84 | samples = c.list { |
---|
85 | 'in'( 'parent', studies ) |
---|
86 | } |
---|
87 | } else { |
---|
88 | samples = Sample.findAll() |
---|
89 | } |
---|
90 | |
---|
91 | samples = filterOnSubjectCriteria( samples ); |
---|
92 | samples = filterOnSampleCriteria( samples ); |
---|
93 | samples = filterOnEventCriteria( samples ); |
---|
94 | samples = filterOnSamplingEventCriteria( samples ); |
---|
95 | samples = filterOnAssayCriteria( samples ); |
---|
96 | |
---|
97 | // Save matches |
---|
98 | results = samples; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Filters the given list of samples on the sample criteria |
---|
103 | * @param samples Original list of samples |
---|
104 | * @return List with all samples that match the Sample-criteria |
---|
105 | */ |
---|
106 | protected List filterOnStudyCriteria( List studies ) { |
---|
107 | return filterEntityList( studies, getEntityCriteria( 'Study' ), { study, criterion -> |
---|
108 | return criterion.matchOne( study ); |
---|
109 | }); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Filters the given list of samples on the subject criteria |
---|
114 | * @param samples Original list of samples |
---|
115 | * @return List with all samples that match the Subject-criteria |
---|
116 | */ |
---|
117 | protected List filterOnSubjectCriteria( List samples ) { |
---|
118 | return filterEntityList( samples, getEntityCriteria( 'Subject' ), { sample, criterion -> |
---|
119 | if( !sample.parentSubject ) |
---|
120 | return false |
---|
121 | |
---|
122 | return criterion.matchOne( sample.parentSubject ); |
---|
123 | }); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Filters the given list of samples on the sample criteria |
---|
128 | * @param samples Original list of samples |
---|
129 | * @return List with all samples that match the sample-criteria |
---|
130 | */ |
---|
131 | protected List filterOnSampleCriteria( List samples ) { |
---|
132 | return filterEntityList( samples, getEntityCriteria( 'Sample' ), { sample, criterion -> |
---|
133 | if( !sample ) |
---|
134 | return false |
---|
135 | |
---|
136 | return criterion.matchOne( sample ); |
---|
137 | }); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Filters the given list of samples on the event criteria |
---|
142 | * @param samples Original list of samples |
---|
143 | * @return List with all samples that match the event-criteria |
---|
144 | */ |
---|
145 | protected List filterOnEventCriteria( List samples ) { |
---|
146 | println "Event criteria: " + getEntityCriteria( 'Event' ) |
---|
147 | return filterEntityList( samples, getEntityCriteria( 'Event' ), { sample, criterion -> |
---|
148 | if( !sample || !sample.parentEventGroup || !sample.parentEventGroup.events || sample.parentEventGroup.events.size() == 0 ) |
---|
149 | return false |
---|
150 | |
---|
151 | return criterion.matchAny( sample.parentEventGroup.events.toList() ); |
---|
152 | }); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Filters the given list of samples on the sampling event criteria |
---|
157 | * @param samples Original list of samples |
---|
158 | * @return List with all samples that match the event-criteria |
---|
159 | */ |
---|
160 | protected List filterOnSamplingEventCriteria( List samples ) { |
---|
161 | return filterEntityList( samples, getEntityCriteria( 'SamplingEvent' ), { sample, criterion -> |
---|
162 | if( !sample.parentEvent ) |
---|
163 | return false |
---|
164 | |
---|
165 | return criterion.matchOne( sample.parentEvent ); |
---|
166 | }); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | /** |
---|
171 | * Filters the given list of samples on the assay criteria |
---|
172 | * @param samples Original list of samples |
---|
173 | * @return List with all samples that match the assay-criteria |
---|
174 | */ |
---|
175 | protected List filterOnAssayCriteria( List samples ) { |
---|
176 | if( !samples?.size() ) |
---|
177 | return []; |
---|
178 | |
---|
179 | if( getEntityCriteria( 'Assay' ).size() == 0 ) |
---|
180 | return samples |
---|
181 | |
---|
182 | // There is no sample.assays property, so we have to look for assays another way: just find |
---|
183 | // all assays that match the criteria |
---|
184 | def assays = filterEntityList( Assay.list(), getEntityCriteria( 'Assay' ), { assay, criterion -> |
---|
185 | if( !assay ) |
---|
186 | return false |
---|
187 | |
---|
188 | return criterion.matchOne( assay ); |
---|
189 | }); |
---|
190 | |
---|
191 | // If no assays match these criteria, then no samples will match either |
---|
192 | if( assays.size() == 0 ) |
---|
193 | return []; |
---|
194 | |
---|
195 | // Now filter the samples on whether they are attached to the filtered assays |
---|
196 | return samples.findAll { sample -> |
---|
197 | if( !sample.parent ) |
---|
198 | return false; |
---|
199 | |
---|
200 | def studyAssays = assays.findAll { it.parent.equals( sample.parent ); } |
---|
201 | |
---|
202 | // See if this sample is present in any of the matching assays. If so, |
---|
203 | // this sample matches the criteria |
---|
204 | for( def assay in studyAssays ) { |
---|
205 | if( assay.samples?.contains( sample ) ) |
---|
206 | return true; |
---|
207 | } |
---|
208 | |
---|
209 | return false; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|