Changeset 1834


Ignore:
Timestamp:
May 11, 2011, 12:56:03 PM (12 years ago)
Author:
s.h.sikkema@…
Message:

Study inferring ability (in progress)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/studycapturing/SimpleWizardController.groovy

    r1817 r1834  
    2222import dbnp.importer.ImportRecord
    2323import dbnp.importer.MappingColumn
    24 import org.hibernate.SessionFactory;
     24import org.hibernate.SessionFactory
     25import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass;
    2526
    2627@Secured(['IS_AUTHENTICATED_REMEMBERED'])
     
    12341235    def testMethod = {
    12351236
     1237        ////////////////////////////////////////////////////////////////////////
     1238        // Functionality mimics intermediate results from simple wizard       //
     1239        ////////////////////////////////////////////////////////////////////////
     1240
    12361241        def data = [//['sample name',         'subject',         'timepoint'],
    12371242                    [ '97___N_151_HAKA_1',    'N_151_HAKA',    '0w'],
     
    12671272        def subjectTemplate = Template.findByName ('Human')
    12681273        def samplingEventTemplate = Template.findByName ('Blood extraction')
    1269         def eventTemplate = Template.findByName ('Diet treatment')
     1274//        def eventTemplate = Template.findByName ('Diet treatment')
    12701275
    12711276
     
    12831288        }
    12841289
    1285         // collect unique subjects and sampling events from table
    1286         def uniqueSubjects = table.collect{it[1]}.unique()
    1287         def uniqueSamplingEvents = table.collect{it[2]}.unique()
    1288 
    1289         // create an event group for each unique sampling event (not much of a group, is it ...)
    1290         def eventGroups = uniqueSamplingEvents.collect{
    1291             def eventGroup = new EventGroup(name: "Sampling_${it.name}_${it.startTime}").addToSamplingEvents(it)
    1292             //study.addToEventGroups eventGroup
    1293             eventGroup
     1290        ////////////////////////////////////////////////////////////////////////
     1291        // Functionality below should be inserted into simple wizard
     1292        // We'll assume entities with pre-existing preferred identifiers have
     1293        // already been loaded from the db so that updating will work.
     1294        ////////////////////////////////////////////////////////////////////////
     1295
     1296        def inferStudyDesign = { study ->
     1297
     1298            // Check for duplicate samples
     1299            def samples = table.collect{it[0]}
     1300
     1301            def uniques     = [] as Set
     1302            def duplicates  = [] as Set
     1303
     1304            // this approach separates the unique from the duplicate entries
     1305            samples*.name.each {
     1306                uniques.add(it) || duplicates.add(it)
     1307            }
     1308
     1309            duplicates.each{ duplicateName ->
     1310                samples.findAllByName(duplicateName).each{ sample ->
     1311                    numInvalidEntities++
     1312                    failedcells = addNonValidatingCells(failedcells, sample, flow)
     1313                    errors += "(Sample) duplicate name: $duplicateName"
     1314                }
     1315            }
     1316
     1317            // A closure that returns a sub list of entities from a list that have
     1318            // unique values of a property indicated by propertyName
     1319            def uniqueEntitiesByProperty = { entities, propertyName ->
     1320
     1321               entities*."$propertyName".unique().collect { uniquePropertyValue ->
     1322
     1323                    entities.find{ it."$propertyName" == uniquePropertyValue }
     1324
     1325                }
     1326            }
     1327
     1328            // collect unique subjects and sampling events from table
     1329            def uniqueSubjects =
     1330                uniqueEntitiesByProperty(table.collect{it[1]}, 'name')
     1331            def uniqueSamplingEvents =
     1332                uniqueEntitiesByProperty(table.collect{it[2]}, 'startTime')
     1333
     1334            // create an event group for each unique sampling event (not much of a group, is it ...)
     1335            uniqueSamplingEvents.each{
     1336                new EventGroup(name: "Sampling_${it.name}_${it.startTime}").addToSamplingEvents(it)
     1337                study.addToEventGroups eventGroup
     1338            }
     1339
     1340            def addToCollectionIfNecessary = { parent, collectionName, entity, propertyName ->
     1341
     1342                if (!parent.'collectionName'.find{it.'propertyName' == entity.'propertyName'})
     1343                    parent."addTo${collectionName.toUpperCase()}" entity
     1344
     1345            }
     1346
     1347            table.each{ record ->
     1348
     1349                Sample sample = record[0]
     1350
     1351                // gather all sample related entities
     1352                def correspondingSamplingEvent  = uniqueSamplingEvents.findByStartTime(record[2].startTime)
     1353                def correspondingSubject        = uniqueSubjects.findByName(record[1].name)
     1354                def correspondingEventGroup     = correspondingSamplingEvent.eventGroup
     1355
     1356                correspondingSamplingEvent.addToSamples sample
     1357                correspondingSubject.addToSamples       sample
     1358                correspondingEventGroup.addToSamples    sample
     1359
     1360                if (!correspondingEventGroup.subjects.find{it.name == correspondingSubject.name})
     1361                    correspondingEventGroup.addToSubjects correspondingSubject
     1362
     1363                study.addToSamples sample
     1364            }
    12941365        }
    1295         //TODO: add event groups to study
    1296 
    1297 //        uniqueSubjects.each {
    1298 //            eventGroup.addToSubjects it
    1299 //            // study.addToSubject it
    1300 //        }
    1301 
    1302         table.each{ record ->
    1303 
    1304             Sample sample = record[0]
    1305 
    1306             // gather all sample related entities
    1307             def correspondingSamplingEvent  = uniqueSamplingEvents.findByStartTime(record[2].startTime)
    1308             def correspondingSubject        = uniqueSubjects.findByName(record[1].name)
    1309             def correspondingEventGroup     = correspondingSamplingEvent.eventGroup
    1310 
    1311             correspondingSamplingEvent.addToSamples sample
    1312             correspondingEventGroup.addToSamples    sample
    1313             correspondingEventGroup.addToSubjects   correspondingSubject
    1314             sample.parentSubject =                  correspondingSubject
    1315 
    1316 
    1317 
    1318 
    1319             // study.addToSamples sample
    1320             uniqueSamplingEvents.findByStartTime(record[2].startTime).addToSamples(sample)
    1321             sample.addToSubjects(uniqueSubjects.findByName(record[1].name))
    1322 
    1323         }
    1324 
     1366
     1367        // inferStudyDesign(study)
    13251368
    13261369        println 'hoi'
Note: See TracChangeset for help on using the changeset viewer.