Ignore:
Timestamp:
Apr 19, 2011, 3:27:02 PM (8 years ago)
Author:
robert@…
Message:
  • Updated a bug on production with org.apache.tools.zip
  • Added tooltips to all icons
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/nl/tno/massSequencing/AssaySample.groovy

    r49 r52  
    88 */
    99class AssaySample {
    10         // To be computed in run time
     10        // Grails datasource is used for executing custom SQL statement
     11        def dataSource
     12       
     13        // To be computed at run time
    1114        private long _numSequences = -1;
    1215        private float _averageQuality = -1.0;
    1316        private long _numQualScores = -1;
    14 
     17        private long _numSequenceFiles = -1;
     18        private long _numQualityFiles = -1;
     19       
    1520        Integer numUniqueSequences      // Number of unique sequences / OTUs. Is only available after preprocessing
    1621
     
    5055                }
    5156                sequenceData cascade: "all-delete-orphan"
     57                sample fetch: 'join'
     58                assay fetch: 'join'
     59                run fetch: 'join'
    5260        }
    5361
     
    5967         */
    6068        public int numFiles() {
    61                 if( !sequenceData )
    62                         return 0
    63 
    64                 int numFiles = 0;
    65                 sequenceData.each { numFiles += it.numFiles() }
    66 
    67                 return numFiles;
     69                return numSequenceFiles() + numQualityFiles();
    6870        }
    6971
     
    7577         */
    7678        public int numSequenceFiles() {
     79                if( _numSequenceFiles > -1 )
     80                        return _numSequenceFiles;
     81
    7782                if( !sequenceData )
    7883                        return 0
     
    9499         */
    95100        public int numQualityFiles() {
     101                if( _numQualityFiles > -1 )
     102                        return _numQualityFiles;
     103               
    96104                if( !sequenceData )
    97105                        return 0
     
    183191                _numQualScores = -1;
    184192                _averageQuality = -1;
     193               
     194                _numSequenceFiles = -1;
     195                _numQualityFiles = -1;
     196        }
     197       
     198        /**
     199         * Fill statistics for multiple assaySamples at once.
     200         *
     201         * This method is used to improve performance. If multiple assaysamples are shown on the screen
     202         * and all have to collect the statistics, it is done with n queries (where n is the number of assaysamples)
     203         * This method reduces this number to 1
     204         *
     205         * @param assaySamples  List of assaySamples to collect the statistics for
     206         */
     207        public static void initStats( ArrayList<AssaySample> assaySamples ) {
     208                if( !assaySamples )
     209                        return;
     210               
     211                // Order assaysamples by id
     212                assaySamples.sort { it.id }
     213               
     214                // Retrieve the datasource for these assaysamples
     215                groovy.sql.Sql sql = new groovy.sql.Sql(assaySamples[0].dataSource)
     216
     217                // Execute a custom query
     218                String ids = assaySamples.id*.toString().join( ', ' );
     219                String sqlStatement = """
     220                        SELECT
     221                                a.id,
     222                                count( s.sequence_file ) AS numSequenceFiles,
     223                                count( s.quality_file ) AS numQualityFiles,
     224                                sum( s.num_sequences ) AS numSequences,
     225                                sum( CASE WHEN s.quality_file IS NOT NULL THEN s.num_sequences ELSE 0 END ) AS numQualScores
     226                        FROM assay_sample a
     227                        LEFT JOIN sequence_data s ON a.id = s.sample_id
     228                        WHERE a.id IN (${ids})
     229                        GROUP BY a.id
     230                        ORDER BY a.id
     231                """
     232               
     233                // For each assaysample, one row is returned. In order to prevent lookups in
     234                // the asasySamples list, both lists are sorted by id. For that reason, we can just
     235                // walk through both lists simultaneously
     236                def listIndex = 0;
     237                sql.eachRow( sqlStatement ) {
     238                        // Still, we perform a check to see whether the ids match. If they don't
     239                        // something went wrong in the database or in grails. We note an error, and
     240                        // skip the assignment. The variables will be filled later on anyhow, it
     241                        // will only be a little bit slower
     242                        if( it.id != assaySamples[ listIndex ]?.id ) {
     243                                log.error "ID of the database row and the domain object don't match. DB: " + it.id + ", Domain object: " + assaySamples[ listIndex ]?.id
     244                        } else {
     245                                assaySamples[ listIndex ]._numSequences = it.numSequences ?: 0;
     246                                assaySamples[ listIndex ]._numQualScores = it.numQualScores ?: 0;
     247                                assaySamples[ listIndex ]._numSequenceFiles = it.numSequenceFiles ?: 0;
     248                                assaySamples[ listIndex ]._numQualityFiles = it.numQualityFiles ?: 0;
     249                        }
     250                       
     251                        listIndex++;
     252                }
    185253        }
    186254       
Note: See TracChangeset for help on using the changeset viewer.