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: 1432 $ |
---|
12 | * $Author: robert@isdat.nl $ |
---|
13 | * $Date: 2011-01-22 15:07:36 +0000 (za, 22 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 | if( studies.size() == 0 ) { |
---|
76 | results = []; |
---|
77 | return; |
---|
78 | } |
---|
79 | studies = filterOnStudyCriteria( studies ); |
---|
80 | |
---|
81 | def c = Sample.createCriteria() |
---|
82 | samples = c.list { |
---|
83 | 'in'( 'parent', studies ) |
---|
84 | } |
---|
85 | } else { |
---|
86 | samples = Sample.findAll() |
---|
87 | } |
---|
88 | |
---|
89 | samples = filterOnSubjectCriteria( samples ); |
---|
90 | samples = filterOnSampleCriteria( samples ); |
---|
91 | samples = filterOnEventCriteria( samples ); |
---|
92 | samples = filterOnSamplingEventCriteria( samples ); |
---|
93 | samples = filterOnAssayCriteria( samples ); |
---|
94 | |
---|
95 | // Save matches |
---|
96 | results = samples; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Filters the given list of samples on the sample criteria |
---|
101 | * @param samples Original list of samples |
---|
102 | * @return List with all samples that match the Sample-criteria |
---|
103 | */ |
---|
104 | protected List filterOnStudyCriteria( List studies ) { |
---|
105 | return filterEntityList( studies, getEntityCriteria( 'Study' ), { study, criterion -> |
---|
106 | return criterion.matchOne( study ); |
---|
107 | }); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Filters the given list of samples on the subject criteria |
---|
112 | * @param samples Original list of samples |
---|
113 | * @return List with all samples that match the Subject-criteria |
---|
114 | */ |
---|
115 | protected List filterOnSubjectCriteria( List samples ) { |
---|
116 | return filterEntityList( samples, getEntityCriteria( 'Subject' ), { sample, criterion -> |
---|
117 | if( !sample.parentSubject ) |
---|
118 | return false |
---|
119 | |
---|
120 | return criterion.matchOne( sample.parentSubject ); |
---|
121 | }); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Filters the given list of samples on the sample criteria |
---|
126 | * @param samples Original list of samples |
---|
127 | * @return List with all samples that match the sample-criteria |
---|
128 | */ |
---|
129 | protected List filterOnSampleCriteria( List samples ) { |
---|
130 | return filterEntityList( samples, getEntityCriteria( 'Sample' ), { sample, criterion -> |
---|
131 | if( !sample ) |
---|
132 | return false |
---|
133 | |
---|
134 | return criterion.matchOne( sample ); |
---|
135 | }); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Filters the given list of samples on the event criteria |
---|
140 | * @param samples Original list of samples |
---|
141 | * @return List with all samples that match the event-criteria |
---|
142 | */ |
---|
143 | protected List filterOnEventCriteria( List samples ) { |
---|
144 | return filterEntityList( samples, getEntityCriteria( 'Event' ), { sample, criterion -> |
---|
145 | if( !sample || !sample.parentEventGroup || !sample.parentEventGroup.events?.size() ) |
---|
146 | return false |
---|
147 | |
---|
148 | return criterion.matchAny( sample.parentEventGroup.events ); |
---|
149 | }); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Filters the given list of samples on the sampling event criteria |
---|
154 | * @param samples Original list of samples |
---|
155 | * @return List with all samples that match the event-criteria |
---|
156 | */ |
---|
157 | protected List filterOnSamplingEventCriteria( List samples ) { |
---|
158 | return filterEntityList( samples, getEntityCriteria( 'SamplingEvent' ), { sample, criterion -> |
---|
159 | if( !sample.parentEvent ) |
---|
160 | return false |
---|
161 | |
---|
162 | return criterion.matchOne( sample.parentEvent ); |
---|
163 | }); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | * Filters the given list of samples on the assay criteria |
---|
169 | * @param samples Original list of samples |
---|
170 | * @return List with all samples that match the assay-criteria |
---|
171 | */ |
---|
172 | protected List filterOnAssayCriteria( List samples ) { |
---|
173 | if( !samples?.size() ) |
---|
174 | return []; |
---|
175 | |
---|
176 | // There is no sample.assays property, so we have to look for assays another way: just find |
---|
177 | // all assays that match the criteria |
---|
178 | def assays = filterEntityList( Assay.list(), getEntityCriteria( 'Assay' ), { assay, criterion -> |
---|
179 | if( !assay ) |
---|
180 | return false |
---|
181 | |
---|
182 | return criterion.matchOne( assay ); |
---|
183 | }); |
---|
184 | |
---|
185 | // If no assays match these criteria, then no samples will match either |
---|
186 | if( assays.size() == 0 ) |
---|
187 | return []; |
---|
188 | |
---|
189 | // Now filter the samples on whether they are attached to the filtered assays |
---|
190 | return samples.findAll { sample -> |
---|
191 | if( !sample.parent ) |
---|
192 | return false; |
---|
193 | |
---|
194 | def studyAssays = assays.findAll { it.parent.equals( sample.parent ); } |
---|
195 | |
---|
196 | // See if this sample is present in any of the matching assays. If so, |
---|
197 | // this sample matches the criteria |
---|
198 | for( def assay in studyAssays ) { |
---|
199 | if( assay.samples?.contains( sample ) ) |
---|
200 | return true; |
---|
201 | } |
---|
202 | |
---|
203 | return false; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | } |
---|