Changeset 1436


Ignore:
Timestamp:
Jan 24, 2011, 3:48:46 PM (12 years ago)
Author:
robert@…
Message:

Improved querying, fixed some tests and added imports of the GDT plugin

Location:
trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/conf/BaseFilters.groovy

    r1430 r1436  
    4040
    4141                        afterView = {
    42                                 def actionDuration = request._timeAfterRequest - request._timeBeforeRequest
     42                                def actionDuration = request._timeAfterRequest ? request._timeAfterRequest - request._timeBeforeRequest : 0
    4343                                def viewDuration = System.currentTimeMillis() - request._timeAfterRequest
    4444                                log.info("Timer: ${controllerName}(${actionDuration}ms)::${actionName}(${viewDuration}ms)")
  • trunk/grails-app/controllers/dbnp/studycapturing/StudyController.groovy

    r1430 r1436  
    33import grails.converters.*
    44import grails.plugins.springsecurity.Secured
     5import nl.grails.plugins.gdt.TemplateFieldType
    56
    67/**
  • trunk/src/groovy/dbnp/query/Criterion.groovy

    r1430 r1436  
    9393                def classname = fieldValue.class.getName();
    9494                classname = classname[classname.lastIndexOf( '.' ) + 1..-1].toLowerCase();
    95 
     95               
     96                println "Match " + fieldValue + " of class " + classname + " with " + this
     97               
    9698                try {
    9799                        switch( classname ) {
     
    238240                                rt = new RelTime( value.toString() );
    239241                        }
    240 
     242                       
    241243                        return compareValues( fieldValue, this.operator, rt );
    242244                } catch( Exception e ) {
  • trunk/src/groovy/dbnp/query/SampleSearch.groovy

    r1432 r1436  
    7373                if( getEntityCriteria( 'Study' ).size() > 0 ) {
    7474                        def studies = Study.findAll();
     75                       
     76                        studies = filterOnStudyCriteria( studies );
     77                       
    7578                        if( studies.size() == 0 ) {
    7679                                results = [];
    7780                                return;
    7881                        }
    79                         studies = filterOnStudyCriteria( studies );
    8082                       
    8183                        def c = Sample.createCriteria()
     
    9294                samples = filterOnSamplingEventCriteria( samples );
    9395                samples = filterOnAssayCriteria( samples );
    94 
     96               
    9597                // Save matches
    9698                results = samples;
     
    142144         */
    143145        protected List filterOnEventCriteria( List samples ) {
     146                println "Event criteria: " + getEntityCriteria( 'Event' )
    144147                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 );
     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() );
    149152                });
    150153        }
     
    174177                        return [];
    175178
     179                if( getEntityCriteria( 'Assay' ).size() == 0 )
     180                        return samples
     181                       
    176182                // There is no sample.assays property, so we have to look for assays another way: just find
    177183                // all assays that match the criteria
  • trunk/test/integration/dbnp/query/SampleSearchTests.groovy

    r1430 r1436  
    55import dbnp.authentication.*;
    66import grails.test.*
     7import nl.grails.plugins.gdt.*
    78
    89/**
     
    2122 */
    2223class SampleSearchTests extends GroovyTestCase {
     24
    2325    protected void setUp() {
    2426        super.setUp()
    25 
    26                 def owner = SecUser.findByUsername( "user" );
    27                
    28                 // Look up the used ontologies which should be in the database by now
    29                 def speciesOntology                             = Ontology.getOrCreateOntologyByNcboId(1132)
    30 
    31                 // Look up the used templates which should also be in the database by now
    32                 def studyTemplate                               = Template.findByName("Academic study")
    33                 def mouseTemplate                               = Template.findByName("Mouse")
    34                 def dietTreatmentTemplate               = Template.findByName("Diet treatment")
    35                 def liverSamplingEventTemplate  = Template.findByName("Liver extraction")
    36                 def humanTissueSampleTemplate   = Template.findByName("Human tissue sample")
    37                 def ccAssayTemplate                             = Template.findByName("Clinical chemistry assay")
    38 
    39                 // Add terms manually, to avoid having to do many HTTP requests to the BioPortal website
    40                 def mouseTerm = Term.findByName( "Mus musculus" );
    41 
    42                 // Add example mouse study
    43                 def testStudy = new Study(
    44                         template        : studyTemplate,
    45                         title           : "TestStudy 1",
    46                         description     : "C57Bl/6 mice were fed a high fat (45 en%) or low fat (10 en%) diet after a four week run-in on low fat diet.",
    47                         code            : "TESTPPS3_leptin_module",
    48                         researchQuestion: "Leptin etc.",
    49                         ecCode          : "2007117.c",
    50                         startDate       : Date.parse('yyyy-MM-dd', '2008-01-02'),
    51                         owner           : owner
    52                 ).with { if (!validate()) { errors.each { println it} } else save()}
    53 
    54                 def evLF = new Event(
    55                         startTime       : 3600,
    56                         endTime         : 3600 + 7 * 24 * 3600,
    57                         template        : dietTreatmentTemplate
    58                 ).setFieldValue('Diet', 'low fat')
    59 
    60                 def evS = new SamplingEvent(
    61                         startTime       : 3600,
    62                         template        : liverSamplingEventTemplate,
    63                         sampleTemplate: humanTissueSampleTemplate).setFieldValue('Sample weight', 5F)
    64                 // Add events to study
    65                 testStudy.addToEvents(evLF).addToSamplingEvents(evS).with { if (!validate()) { errors.each { println it} } else save()}
    66 
    67                 // Extra check if the SamplingEvents are saved correctly
    68                 evS.with { if (!validate()) { errors.each { println it} } else save()}
    69 
    70                 def LFBV1 = new EventGroup(name: "10% fat + vehicle for 1 week").addToEvents(evLF).addToSamplingEvents(evS)
    71 
    72                 // Add subjects and samples and compose EventGroups
    73                 def x = 1
    74                 5.times {
    75                         def currentSubject = new Subject(
    76                                 name: "TestA" + x++,
    77                                 species: mouseTerm,
    78                                 template: mouseTemplate,
    79                         ).setFieldValue("Gender", "Male")
    80 
    81                         // We have to save the subject first, otherwise the parentEvent property of the sample cannot be set
    82                         // (this is possibly a Grails or Hibernate bug)
    83                         testStudy.addToSubjects(currentSubject)
    84                         currentSubject.with { if (!validate()) { errors.each { println it} } else save()}
    85 
    86                         // Add subject to appropriate EventGroup
    87                         LFBV1.addToSubjects(currentSubject).save()
    88 
    89                         // Create sample
    90                         def currentSample = new Sample(
    91                                 name: currentSubject.name + '_B',
    92                                 material: mouseTerm,
    93                                 template: humanTissueSampleTemplate,
    94                                 parentSubject: currentSubject,
    95                                 parentEvent: evS
    96                         );
    97                         testStudy.addToSamples(currentSample)
    98                         currentSample.with { if (!validate()) { errors.each { println it} } else save()}
    99                         currentSample.setFieldValue("Text on vial", "T" + (Math.random() * 100L))
    100                 }
    101 
    102                 // Add EventGroups to study
    103                 testStudy.addToEventGroups(LFBV1).with { if (!validate()) { errors.each { println it} } else save()}
    104 
    105                 // Add SAM assay reference
    106                 def clinicalModule = AssayModule.findByName( "SAM module for clinical data" );
    107 
    108                 def testAssayRef = new Assay(
    109                         name: 'Test Lipid profiling',
    110                         template: ccAssayTemplate,
    111                         module: clinicalModule,
    112                         externalAssayID: 'TestPPS3_SAM'
    113                 )
    114 
    115                 testStudy.samples*.each {
    116                         testAssayRef.addToSamples(it)
    117                 }
    118 
    119                 testStudy.addToAssays(testAssayRef);
    120                 testStudy.save()
    121 
    122                 assert Sample.list()*.name.contains( "TestA2_B" );
     27//
     28//              def owner = SecUser.findByUsername( "user" );
     29//             
     30//              // Look up the used ontologies which should be in the database by now
     31//              def speciesOntology                             = Ontology.getOrCreateOntologyByNcboId(1132)
     32//
     33//              // Look up the used templates which should also be in the database by now
     34//              def studyTemplate                               = Template.findByName("Academic study")
     35//              def mouseTemplate                               = Template.findByName("Mouse")
     36//              def dietTreatmentTemplate               = Template.findByName("Diet treatment")
     37//              def liverSamplingEventTemplate  = Template.findByName("Liver extraction")
     38//              def humanTissueSampleTemplate   = Template.findByName("Human tissue sample")
     39//              def ccAssayTemplate                             = Template.findByName("Clinical chemistry assay")
     40//
     41//              // Add terms manually, to avoid having to do many HTTP requests to the BioPortal website
     42//              def mouseTerm = Term.findByName( "Mus musculus" );
     43//
     44//              // Add example mouse study
     45//              def testStudy = new Study(
     46//                      template        : studyTemplate,
     47//                      title           : "TestStudy 1",
     48//                      description     : "C57Bl/6 mice were fed a high fat (45 en%) or low fat (10 en%) diet after a four week run-in on low fat diet.",
     49//                      code            : "TESTPPS3_leptin_module",
     50//                      researchQuestion: "Leptin etc.",
     51//                      ecCode          : "2007117.c",
     52//                      startDate       : Date.parse('yyyy-MM-dd', '2008-01-02'),
     53//                      owner           : owner
     54//              ).with { if (!validate()) { errors.each { println it} } else save()}
     55//
     56//             
     57//              def evLF = new Event(
     58//                      startTime       : 3600,
     59//                      endTime         : 3600 + 7 * 24 * 3600,
     60//                      template        : dietTreatmentTemplate
     61//              ).setFieldValue('Diet', 'low fat')
     62//
     63//              def evS = new SamplingEvent(
     64//                      startTime       : 3600,
     65//                      template        : liverSamplingEventTemplate,
     66//                      sampleTemplate: humanTissueSampleTemplate).setFieldValue('Sample weight', 5F)
     67//
     68//              // Add events to study
     69//              testStudy.addToEvents(evLF).addToSamplingEvents(evS).with { if (!validate()) { errors.each { println it} } else save()}
     70//
     71//              // Extra check if the SamplingEvents are saved correctly
     72//              evS.with { if (!validate()) { errors.each { println it} } else save()}
     73//              evLF.with { if (!validate()) { errors.each { println it} } else save()}
     74//
     75//             
     76//              def LFBV1 = new EventGroup(name: "10% fat + vehicle for 1 week").addToEvents(evLF).addToSamplingEvents(evS)
     77//
     78//              // Add subjects and samples and compose EventGroups
     79//              def x = 1
     80//              5.times {
     81//                      def currentSubject = new Subject(
     82//                              name: "TestA" + x++,
     83//                              species: mouseTerm,
     84//                              template: mouseTemplate,
     85//                      ).setFieldValue("Gender", "Male")
     86//
     87//                      // We have to save the subject first, otherwise the parentEvent property of the sample cannot be set
     88//                      // (this is possibly a Grails or Hibernate bug)
     89//                      testStudy.addToSubjects(currentSubject)
     90//                      currentSubject.with { if (!validate()) { errors.each { println it} } else save()}
     91//
     92//                      // Add subject to appropriate EventGroup
     93//                      LFBV1.addToSubjects(currentSubject).with { if (!validate()) { errors.each { println it} } else save()}
     94//
     95//                      // Create sample
     96//                      def currentSample = new Sample(
     97//                              name: currentSubject.name + '_B',
     98//                              material: mouseTerm,
     99//                              template: humanTissueSampleTemplate,
     100//                              parentSubject: currentSubject,
     101//                              parentEvent: evS
     102//                      );
     103//                      testStudy.addToSamples(currentSample)
     104//                      currentSample.setFieldValue("Text on vial", "T" + (Math.random() * 100L))
     105//                      currentSample.with { if (!validate()) { errors.each { println it} } else save()}
     106//              }
     107//
     108//              // Add EventGroups to study
     109//              testStudy.addToEventGroups(LFBV1)
     110//              LFBV1.with { if (!validate()) { errors.each { println it} } else save()}
     111//              testStudy.with { if (!validate()) { errors.each { println it} } else save(flush:true)}
     112//
     113//              testStudy.save(flush:true)
     114//
     115//              //assert Sample.list()*.name.contains( "TestA2_B" );
     116//             
     117//              // Make sure session is kept open
     118//              Session session = SessionFactoryUtils.getSession(sessionFactory, true)
     119//              session.flush();
     120//              session.clear();
     121
    123122    }
    124123
     
    131130                List criteria = [
    132131                        new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "B" ),
    133                         new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "TESTPPS3_leptin_module" ),
    134                         new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "PPS3_leptin_module" )
     132                        new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "PPS3_leptin_module" ),
     133                        new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "PPSH" )
    135134                ]
    136135               
     
    142141               
    143142                assert search.getResults()[0] instanceof Sample
    144                 assert search.getResults()*.name.contains( "TestA2_B" );
    145                 assert search.getResults()*.name.contains( "TestA4_B" );
     143                assert search.getResults()*.name.contains( "A2_B" );
     144                assert search.getResults()*.name.contains( "A4_B" );
    146145               
    147146                search.setCriteria( [ criteria[0], criteria[1] ] );
    148147                search.execute();
    149148                assert search.getResults().size() >= 1
    150                 assert search.getResults()*.name.contains( "TestA2_B" );
     149                assert search.getResults()*.name.contains( "A2_B" );
    151150               
    152151                search.setCriteria( [ criteria[0], criteria[2] ] );
     
    162161        void testExecuteDifferentCriteria() {
    163162                List criteria = [
    164                         new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TestStudy 1" ),
    165                         new Criterion( entity: "Subject", field: "name", operator: Operator.contains, value: "Test" ),
    166                         new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "Test" ),
    167                         new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "Test" ),
    168                         new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "3600" ),
    169                         new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: "3600" ),
     163                        new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "NuGO PPS3 mouse study" ),
     164                        new Criterion( entity: "Subject", field: "species", operator: Operator.equals, value: "Mus musculus" ),
     165                        new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "A" ),
     166                        new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "Lipid" ),
     167                        //new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "3600" ),
     168                        //new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: 3600 + 7 * 24 * 3600 ),
    170169                ]
    171170               
    172                 def search = new Search();
     171                def search = new SampleSearch();
    173172
    174173                // All criteria should result in 1 sample with code 'abc'
     
    177176                        search.setCriteria( [ it ] );
    178177                        search.execute();
    179                         assert search.getResults().size() >= 2
    180                         assert search.getResults()[0] instanceof Sample;
    181                         assert search.getResults()*.name.contains( "TestA2_B" );
     178                       
     179                        def results = search.getResults();
     180                        assert results;
     181                        assert results.size() >= 2
     182                        assert results[0] instanceof Sample;
     183                        assert results*.name.contains( "A2_B" );
    182184                }
    183185        }
  • trunk/test/integration/gscf/SubjectTests.groovy

    r1432 r1436  
    55import dbnp.data.*
    66import nl.grails.plugins.gdt.*
     7
    78/**
    89 * SubjectTests Test
Note: See TracChangeset for help on using the changeset viewer.