source: trunk/src/groovy/dbnp/query/AssaySearch.groovy @ 1526

Last change on this file since 1526 was 1526, checked in by robert@…, 9 years ago

Added assay search and improved query form

File size: 5.6 KB
Line 
1/**
2 * AssaySearch Domain Class
3 *
4 * This class provides querying capabilities for searching for assays
5 *
6 * @author  Robert Horlings (robert@isdat.nl)
7 * @since       20110118
8 * @package     dbnp.query
9 *
10 * Revision information:
11 * $Rev: 1524 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-02-15 15:05:23 +0100 (Tue, 15 Feb 2011) $
14 */
15package dbnp.query
16
17import groovy.lang.Closure;
18
19import java.util.Map;
20
21import dbnp.studycapturing.*
22import org.dbnp.gdt.*
23import org.apache.commons.logging.LogFactory;
24
25class AssaySearch extends Search {
26        private static final log = LogFactory.getLog(this);
27
28        public AssaySearch() {
29                super();
30
31                this.entity = "Assay";
32        }
33
34        /**
35         * Searches for assays based on the given criteria. All criteria have to be satisfied and
36         * criteria for the different entities are satisfied as follows:
37         *
38         *              Study.title = 'abc'             
39         *                              Only assays are returned from studies with title 'abc'
40         *             
41         *              Subject.species = 'human'
42         *                              Only assays are returned with samples from subjects with species = 'human' 
43         *
44         *              Sample.name = 'sample 1'
45         *                              Only assays are returned with samples with name = 'sample 1'
46         *
47         *              Event.startTime = '0s'
48         *                              Only assays are returned with samples from subjects that have had an event with start time = '0s' 
49         *
50         *              SamplingEvent.startTime = '0s'
51         *                              Only assays are returned with samples that have originated from a sampling event with start time = '0s' 
52         *
53         *              Assay.module = 'metagenomics'
54         *                              Only assays are returned with module = metagenomics 
55         *
56         * When searching for more than one criterion per entity, these are taken combined. Searching for
57         *
58         *              Subject.species = 'human'
59         *              Subject.name = 'Jan'
60         *
61         *  will result in all samples from a human subject named 'Jan'. Samples from a mouse subject
62         *  named 'Jan' or a human subject named 'Kees' won't satisfy the criteria.
63         *     
64         */
65        @Override
66        protected void executeAnd() {
67                def assays = Assay.list().findAll { it.parent?.canRead( this.user ) };
68
69                executeAnd( assays );
70        }
71
72        /**
73         * Searches for samples based on the given criteria. Only one of the criteria have to be satisfied and
74         * criteria for the different entities are satisfied as follows:
75         *
76         *              Study.title = 'abc'             
77         *                              Only assays are returned from studies with title 'abc'
78         *             
79         *              Subject.species = 'human'
80         *                              Only assays are returned with samples from subjects with species = 'human' 
81         *
82         *              Sample.name = 'sample 1'
83         *                              Only assays are returned with samples with name = 'sample 1'
84         *
85         *              Event.startTime = '0s'
86         *                              Only assays are returned with samples from subjects that have had an event with start time = '0s' 
87         *
88         *              SamplingEvent.startTime = '0s'
89         *                              Only assays are returned with samples that have originated from a sampling event with start time = '0s' 
90         *
91         *              Assay.module = 'metagenomics'
92         *                              Only assays are returned with module = metagenomics 
93         *
94         * When searching for more than one criterion per entity, these are taken separately. Searching for
95         *
96         *              Subject.species = 'human'
97         *              Subject.name = 'Jan'
98         *
99         *  will result in all samples from a human subject or a subject named 'Jan'. Samples from a mouse subject
100         *  named 'Jan' or a human subject named 'Kees' will also satisfy the criteria.
101         *
102         */
103        @Override
104        void executeOr() {
105                def allAssays = Assay.list().findAll { it.parent?.canRead( this.user ) }.toList();
106                executeOr( allAssays );
107        }
108
109        /**
110         * Returns a closure for the given entitytype that determines the value for a criterion
111         * on the given object. The closure receives two parameters: the object and a criterion.
112         *
113         * For example:
114         *              For a study search, the object given is a study. How to determine the value for that study of
115         *              the criterion field of type sample? This is done by returning the field values for all
116         *              samples in the study
117         *                      { study, criterion -> return study.samples?.collect { criterion.getFieldValue( it ); } }
118         * @return
119         */
120        protected Closure valueCallback( String entity ) {
121                switch( entity ) {
122                        case "Study":
123                                return { assay, criterion -> return criterion.getFieldValue( assay.parent ) }
124                        case "Subject":
125                                return { assay, criterion -> return assay.samples?.parentSubject?.collect { criterion.getFieldValue( it ) } }
126                        case "Sample":
127                                return { assay, criterion -> return assay.samples?.collect { criterion.getFieldValue( it ) } }
128                        case "Event":
129                                return { assay, criterion ->
130                                        def values = []
131                                        assay.samples?.each { sample ->
132                                                if( sample && sample.parentEventGroup && sample.parentEventGroup.events && sample.parentEventGroup.events.size() > 0 ) {
133                                                        values << sample.parentEventGroup.events.collect { criterion.getFieldValue( it ) };
134                                                }
135                                        }
136                                        return values;
137                                }
138                        case "SamplingEvent":
139                                return { assay, criterion -> return assay.samples?.parentEvent?.collect { criterion.getFieldValue( it ) } }
140                        case "Assay":
141                                return { assay, criterion -> return criterion.getFieldValue( assay ) }
142                        default:
143                                return super.valueCallback( entity );
144                }
145        }
146
147        /**
148         * Returns the saved field data that could be shown on screen. This means, the data
149         * is filtered to show only data of the query results. Also, the study title and assay
150         * name are filtered out, in order to be able to show all data on the screen without
151         * checking further
152         *
153         * @return      Map with the entity id as a key, and a field-value map as value
154         */
155        public Map getShowableResultFields() {
156                Map showableFields = super.getShowableResultFields()
157                showableFields.each { sampleElement ->
158                        sampleElement.value = sampleElement.value.findAll { fieldElement ->
159                                fieldElement.key != "Study title" && fieldElement.key != "Assay name"
160                        }
161                }
162        }
163}
Note: See TracBrowser for help on using the repository browser.