Changeset 7 for trunk/grails-app/services/nl
- Timestamp:
- Jan 26, 2011, 5:08:25 PM (12 years ago)
- Location:
- trunk/grails-app/services/nl/tno/metagenomics
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/nl/tno/metagenomics/files/ExcelService.groovy
r2 r7 1 1 package nl.tno.metagenomics.files 2 2 3 import java.text.DecimalFormat 4 import java.text.Format 5 import java.text.NumberFormat 3 6 import org.apache.poi.hssf.usermodel.* 4 7 import org.apache.poi.ss.usermodel.* … … 71 74 // Now loop through all rows, retrieving data from the excel file 72 75 def df = new DataFormatter() 76 DecimalFormat numberformat = new DecimalFormat( "0" ); 77 73 78 ArrayList data = [] 74 79 … … 78 83 79 84 for( def colNum = 0; colNum < excelRow.getLastCellNum(); colNum++ ) { 80 row << df.formatCellValue( excelRow.getCell( colNum ) ); 85 Cell c = excelRow.getCell( colNum ); 86 if( c ) { 87 if( c.getCellType() == Cell.CELL_TYPE_NUMERIC ) { 88 row << numberformat.format( c.getNumericCellValue() ); 89 } else { 90 row << df.formatCellValue( c ); 91 } 92 } else { 93 row << "" 94 } 95 81 96 } 82 97 -
trunk/grails-app/services/nl/tno/metagenomics/integration/SynchronizationService.groovy
r6 r7 96 96 public ArrayList<Study> synchronizeStudies() { 97 97 if( !performSynchronization() ) 98 return Study. list()98 return Study.findAllWhereTrashcan(false) 99 99 100 100 // When eager fetching is enabled, ask for all studies, otherwise only ask for studies marked dirty … … 106 106 log.trace "Eager synchronization"; 107 107 } else { 108 studies = Study.findAllWhere( [ isDirty: true] );108 studies = Study.findAllWhere( [trashcan: false, isDirty: true] ); 109 109 log.trace "Default synchronization: " + studies.size() 110 110 … … 184 184 // Synchronize authorization and study assays (since the study itself is already synchronized) 185 185 synchronizeAuthorization(studyFound); 186 synchronizeStudyAssays(studyFound); 186 if( studyFound.canRead( user ) ) 187 synchronizeStudyAssays(studyFound); 187 188 188 189 // Mark the study as clean … … 204 205 // should be deleted from this module as well. Looping backwards in order to avoid conflicts 205 206 // when removing elements from the list 207 208 println "Handle deleted studies: " + studies.size() + " -> " + newStudies.size(); 206 209 def numStudies = studies.size(); 207 210 for( int i = numStudies - 1; i >= 0; i-- ) { … … 300 303 study.isDirty = true; 301 304 synchronizeAuthorization( study ); 302 synchronizeStudyAssays( study ); 305 if( study.canRead( user ) ) 306 synchronizeStudyAssays( study ); 303 307 304 308 // Update properties and mark as clean 305 309 study.name = newStudy.title 306 310 study.isDirty = false; 307 study.save( )311 study.save(flush:true) 308 312 309 313 return study … … 466 470 467 471 // Copy properties from gscf object 468 a.canRead = gscfAuthorization.canRead 469 a.canWrite = gscfAuthorization.canWrite 470 a.isOwner = gscfAuthorization.isOwner 472 println "GSCF auth: " + gscfAuthorization 473 474 if( gscfAuthorization.canRead instanceof Boolean ) 475 a.canRead = gscfAuthorization.canRead.booleanValue() 476 477 if( gscfAuthorization.canWrite instanceof Boolean ) 478 a.canWrite = gscfAuthorization.canWrite.booleanValue() 479 480 if( gscfAuthorization.isOwner instanceof Boolean ) 481 a.isOwner = gscfAuthorization.isOwner.booleanValue() 482 483 println "Saved auth: " + a.canRead.toString() + " - " + a.canWrite.toString() + " - " + a.isOwner.toString() 471 484 472 485 a.save() … … 606 619 // already exist in the list of samples 607 620 newSamples.each { gscfSample -> 608 log.trace( "Processing GSCF sample " + gscfSample. name+ ": " + gscfSample )621 log.trace( "Processing GSCF sample " + gscfSample.sampleToken + ": " + gscfSample ) 609 622 if( gscfSample.name ) { 610 623 611 AssaySample assaySampleFound = assay.assaySamples.find { it.sample.sampleToken == gscfSample. name}624 AssaySample assaySampleFound = assay.assaySamples.find { it.sample.sampleToken == gscfSample.sampleToken } 612 625 Sample sampleFound 613 626 614 627 if(assaySampleFound) { 615 628 sampleFound = assaySampleFound.sample 616 log.trace( "AssaySample found with sample " + sampleFound.name )629 log.trace( "AssaySample found with sample name " + sampleFound.name ) 617 630 618 631 // Update the sample object if necessary … … 625 638 626 639 // Check if the sample already exists in the database. 627 sampleFound = Sample.findBySampleTokenAndStudy( gscfSample. nameas String, assay.study )640 sampleFound = Sample.findBySampleTokenAndStudy( gscfSample.sampleToken as String, assay.study ) 628 641 629 642 if( sampleFound ){ 630 log.trace( "Sample " + gscfSample. name+ " is found in database. Updating if necessary" )643 log.trace( "Sample " + gscfSample.sampleToken + " is found in database. Updating if necessary" ) 631 644 632 645 // Update the sample object if necessary 633 646 if( sampleFound.name != gscfSample.name ) { 634 sampleFound.name = gscfSample.name647 sampleFound.name = gscfSample.name 635 648 sampleFound.save(); 636 649 } 637 650 } else { 638 log.trace( "Sample " + gscfSample. name+ " not found in database. Creating a new object." )651 log.trace( "Sample " + gscfSample.sampleToken + " not found in database. Creating a new object." ) 639 652 640 653 // If it doesn't exist, create a new object 641 sampleFound = new Sample( sampleToken: gscfSample.name, name: gscfSample.name, study: assay.study ); 654 sampleFound = new Sample( sampleToken: gscfSample.sampleToken, name: gscfSample.name, study: assay.study ); 655 assay.study.addToSamples( sampleFound ); 642 656 sampleFound.save(); 643 657 } … … 649 663 assay.addToAssaySamples( assaySampleFound ); 650 664 sampleFound.addToAssaySamples( assaySampleFound ); 651 assaySampleFound.save(); 665 666 assaySampleFound.save() 652 667 } 653 668 } … … 670 685 def existingSample = assaySamples[i]; 671 686 672 AssaySample sampleFound = newSamples.find { it. name== existingSample.sample.sampleToken }687 AssaySample sampleFound = newSamples.find { it.sampleToken == existingSample.sample.sampleToken } 673 688 674 689 if( !sampleFound ) { … … 676 691 677 692 // The sample has been removed 678 trashService.moveToTrash( existingSample .sample);693 trashService.moveToTrash( existingSample ); 679 694 } 680 695 } … … 682 697 683 698 // Create a list of samples to return 684 return assay.assaySamples.toList() 685 699 if( assay.assaySamples ) 700 return assay.assaySamples.toList() 701 else 702 return [] 686 703 } 687 704 } -
trunk/grails-app/services/nl/tno/metagenomics/integration/TrashService.groovy
r4 r7 2 2 3 3 import nl.tno.metagenomics.* 4 5 4 6 5 class TrashService { … … 16 15 if( study.trashcan ) 17 16 return 18 17 19 18 saveDataInTrash( study ); 19 20 def l = [] 21 l += study.auth 22 23 l.each { auth -> 24 auth.user.removeFromAuth( auth ); 25 study.removeFromAuth( auth ); 26 } 27 20 28 study.delete(flush:true); 21 29 } 22 30 23 31 /** 24 32 * Moves the valuable data from an assay to trash and deletes the assay … … 27 35 def moveToTrash( Assay assay ) { 28 36 saveDataInTrash( assay ); 29 37 30 38 // Remove associations 39 def l = [] 40 if( assay.runs ) { 41 l += assay.runs 42 43 l.each { 44 if( it ) { 45 assay.removeFromRuns( it ); 46 it.removeFromAssays( assay ); 47 } 48 } 49 } 50 51 l = [] 52 l += assay.assaySamples 53 54 l.each { 55 it.sample.removeFromAssaySamples( it ); 56 assay.removeFromAssaySamples( it ); 57 } 58 59 def study = assay.study 60 if( study ) { 61 study.removeFromAssays( assay ); 62 } 63 64 /* 65 def assaySamples = assay.assaySamples.toList(); 66 assaySamples.each { 67 it.assay = null 68 69 assay.removeFromAssaySamples( it ); 70 it.sample.removeFromAssaySamples( it ); 71 it.sample = null; 72 it.delete( flush: true ); 73 } 74 31 75 assay.study.removeFromAssays( assay ); 32 assay.delete(flush:true); 33 } 34 76 assay.study.save(); 77 //assay.study = null 78 //assay.delete(flush:true); 79 */ 80 } 81 35 82 /** 36 83 * Moves the valuable data from a sample to trash and deletes the sample … … 39 86 def moveToTrash( Sample sample ) { 40 87 saveDataInTrash( sample ); 41 88 42 89 // Remove associations 90 def l = [] 91 l += sample.assaySamples 92 93 l.each { 94 it.assay.removeFromAssaySamples( it ); 95 sample.removeFromAssaySamples( it ); 96 } 97 98 def study = sample.study 43 99 sample.study.removeFromSamples( sample ); 44 sample.delete(flush:true); 45 } 46 100 study.save(); 101 } 102 103 /** 104 * Moves the valuable data from an assaySample to trash and deletes the assay 105 * @param Assay to move to trash 106 */ 107 def moveToTrash( AssaySample assaySample ) { 108 saveDataInTrash( assaySample ); 109 110 // Remove associations 111 if( assaySample.run ) { 112 assaySample.run.removeFromAssaySamples( assaySample ); 113 } 114 115 if( assaySample.assay ) { 116 assaySample.assay.removeFromAssaySamples( assaySample ); 117 } 118 119 if( assaySample.sample ) { 120 assaySample.sample.removeFromAssaySamples( assaySample ); 121 } 122 123 def l = [] 124 l += assaySample.sequenceData 125 126 l.each { 127 if( it ) { 128 assaySample.removeFromSequenceData( it ); 129 } 130 } 131 132 } 133 47 134 /** 48 135 * Saves data from the study in the trash can (if any data exists) … … 51 138 */ 52 139 def saveDataInTrash( Study study ) { 53 Study trashcan = Study.findByTrashcan(true);140 Study trashcan = this.giveTrashcan() 54 141 55 142 if( !trashcan ) { … … 71 158 */ 72 159 def saveDataInTrash( Assay assay ) { 73 Study trashcan = Study.findByTrashcan(true);160 Study trashcan = this.giveTrashcan() 74 161 75 162 if( !trashcan ) { … … 91 178 assaySamples.each { assaySample -> 92 179 Sample sample = assaySample.sample 93 180 94 181 // Create dummy sample 95 182 String newSampleToken = 'TrashSample ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); … … 118 205 */ 119 206 def saveDataInTrash( Sample sample ) { 120 Study trashcan = Study.findByTrashcan(true);207 Study trashcan = this.giveTrashcan() 121 208 122 209 if( !trashcan ) { … … 159 246 } 160 247 248 249 /** 250 * Saves data from the assay-sample in the trash can (if any data exists) 251 * @param study Sample to save data from 252 * @return 253 */ 254 def saveDataInTrash( AssaySample assaySample ) { 255 Study trashcan = this.giveTrashcan() 256 257 if( !trashcan ) { 258 log.warn "No trashcan (study with trashcan property set to true) found in the database when deleting sample " + sample.name + ". Possibly valuable data is deleted forever." 259 return; 260 } 261 262 // For every assay sample that contains data, save that data in the trashcan 263 if( assaySample.containsData() ) { 264 // Create dummy sample 265 String newSampleToken = 'TrashSample ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); 266 Sample dummySample = new Sample( sampleToken: newSampleToken, name: assaySample.sample.name, study: trashcan ); 267 trashcan.addToSamples( dummySample ); 268 dummySample.save() 269 270 Assay assay = assaySample.assay; 271 272 // Create a dummy assay copy of the existing assay 273 String newAssayToken = 'TrashAssay ' + new Date().format( 'yyyyMMddHHmmssSSS') + ( Math.random() * 10000 ); 274 275 Assay dummyAssay = new Assay( assayToken: newAssayToken, name: assay.name, study: trashcan ); 276 trashcan.addToAssays( dummyAssay ); 277 dummyAssay.save() 278 279 // Create dummy assay sample 280 AssaySample dummyAssaySample = new AssaySample( assay: dummyAssay, sample: dummySample ); 281 282 dummyAssay.addToAssaySamples( dummyAssaySample ); 283 dummySample.addToAssaySamples( dummyAssaySample ); 284 dummyAssaySample.save(); 285 286 // Move data from this assay sample to the trash version of it 287 assaySample.moveValuableDataTo( dummyAssaySample ); 288 dummyAssaySample.save(); 289 } 290 } 291 292 /** 293 * Retrieves the trashcan study from the database 294 */ 295 def giveTrashcan = { 296 def study = Study.findByTrashcan( true ); 297 298 if( !study ) 299 return null; 300 else 301 return study 302 } 303 304 /** 305 * Creates a new trashcan study. Should only be used by the bootstrap to create a trashcan 306 */ 307 def createTrashcan = { 308 def study = new Study( name: "Trashcan", studyToken: "trash", trashcan: true ) 309 study.save(); 310 } 311 161 312 /** 162 313 * Cleans up the trash by removing empty assays or samples. Empty means: … … 168 319 def cleanTrash = { 169 320 def studies = Study.findAllByTrashcan( true ); 170 321 171 322 studies.each { study -> 172 323 def numAssays = study.assays?.size() 173 324 def assayList = study.assays?.toList(); 174 325 175 326 def numSamples 176 327 def sampleList 177 328 178 329 // Loop backwards through the assays in order to facilitate removing assays 179 330 for( def i = numAssays -1; i >= 0; i-- ) { … … 182 333 numSamples = assayList[ i ].assaySamples.size() 183 334 sampleList = assayList[ i ].assaySamples.toList(); 184 335 185 336 for( def j = numSamples - 1; j >= 0; j-- ) { 186 337 def s = sampleList[ j ]; … … 197 348 study.removeFromAssays( assayList[ i ] ); 198 349 assayList[i].delete(); 199 } 200 } 201 350 } 351 } 352 202 353 // Loop through samples and delete the ones not referenced by an assaysample 203 354 /* 204 numSamples = study.samples?.size() 205 sampleList = study.samples?.toList(); 206 207 for( def j = numSamples - 1; j >= 0; j-- ) { 208 def s = sampleList[ j ]; 209 if( s.assaySamples == null || s.assaySamples.size() == 0 ) { 210 study.removeFromSamples(s); 211 s.delete(flush:true); 212 } 213 } 214 */ 215 } 216 } 217 355 numSamples = study.samples?.size() 356 sampleList = study.samples?.toList(); 357 for( def j = numSamples - 1; j >= 0; j-- ) { 358 def s = sampleList[ j ]; 359 if( s.assaySamples == null || s.assaySamples.size() == 0 ) { 360 study.removeFromSamples(s); 361 s.delete(flush:true); 362 } 363 } 364 */ 365 } 366 } 367 218 368 /** 219 369 * Restore an assay from trash and put the contents of the assay in another assay … … 227 377 // Find a sample with the same name 228 378 def restoreSample = restoreTo.assaySamples.find { it.sample?.name == assaySample.sample?.name } 229 379 230 380 if( restoreSample ) { 231 381 this.restoreSample( assaySample, restoreSample ); … … 234 384 } 235 385 } 236 386 237 387 /** 238 388 * Restore a sample from trash and put the contents of the sample into another sample
Note: See TracChangeset
for help on using the changeset viewer.