package nl.tno.metagenomics.integration import grails.converters.* import nl.tno.metagenomics.Study import nl.tno.metagenomics.Sample import nl.tno.metagenomics.Assay import org.apache.catalina.connector.Response; import org.codehaus.groovy.grails.commons.ConfigurationHolder /** Expose the list of features within a certain assay * * @author Robert Horlings (robert@isdat.nl) * @since 20101229 * @see SAM.RestController * * $Rev$ * * This class provides a REST-full service for getting and setting the data * in the Metagenomics Module. The service consists of several * resources listed below. So far, all resources are GET resoruces, i.e. we * do not use PUT, POST or DELETE. Because we are using Grails' web libaries, * each resource corresponds to exactly one action of this controller. * * * The REST resources implemented in this controller are: * * metagenomics-host:port/metagenomics/rest/getMeasurements(assayToken) * metagenomics-host:port/metagenomics/rest/getMeasurementMetadata(assayToken, measurementTokens) * metagenomics-host:port/metagenomics/rest/getMeasurementData(assayToken, measurementTokens, sampleTokens) * metagenomics-host:port/metagenomics/rest/getAssayURL(assayToken) * * Where 'metagenomics-host' is the url of the metagenomics server's host with port number 'port'. * */ class RestController { def synchronizationService /****************************************************************/ /* REST resource for handling study change in GSCF */ /****************************************************************/ /** * Is called by GSCF when a study is added, changed or deleted. * Sets the 'dirty' flag of a study to true, so that it will be updated * next time the study is asked for. * * @param studyToken */ def notifyStudyChange = { def studyToken = params.studyToken if( !studyToken ) { response.sendError(400, "No studyToken given" ) return } // Search for the changed study def study = Study.findByStudyToken( studyToken ); // If the study is not found, it is added in GSCF. Add a dummy (dirty) study, in order to // update it immediately when asked for if( !study ) { log.info( "METAGENOMICS: GSCF notification for new study " + studyToken ); study = new Study( name: "", studyToken: studyToken, isDirty: true ) } else { log.info( "METAGENOMICS: GSCF notification for existing study " + studyToken ); study.isDirty = true; } study.save(flush:true); def jsonData = [ 'studyToken': studyToken, message: "Notify succesful" ]; render jsonData as JSON } /** * Return URL to view an assay. * * @param assayToken * @return URL to view an assay as single hash entry with key 'url'. * */ def getAssayURL = { def assayToken = params.assayToken if( !assayToken ) { render [] as JSON return } def assay = Assay.findByAssayToken( assayToken ) // If the assay is not found, try synchronizing synchronizationService.sessionToken = session.sessionToken if( !assay ) { synchronizationService.synchronizeStudies() assay = Assay.findByAssayToken( assayToken ); if( !assay ) { response.sendError(404, "Not Found" ) return; } } else { try { synchronizationService.synchronizeAssay(assay); } catch( Exception e ) { response.sendError( 500, e.getMessage()) return } def url = [ 'url' : ConfigurationHolder.config.grails.serverURL + '/assay/show/' + assay.id.toString() ] render url as JSON } } /***************************************************/ /* REST resources related to the querying in GSCF */ /***************************************************/ /** * Retrieves a list of fields that could be queried when searching for a specific entity. * * The module is allowed to return different fields when the user searches for different entities * * Example call: [moduleurl]/rest/getQueryableFields?entity=Study&entity=Sample * Example response: { "Study": [ "# sequences" ], "Sample": [ "# sequences", "# bacteria" ] } * * @param params.entity Entity that is searched for. Might be more than one. If no entity is given, * a list of searchable fields for all entities is given * @return JSON List with the names of the fields */ def getQueryableFields = { // We don't really care about the entity. The only thing is that this module // is only aware of studies, assays and samples, but doesn't know anything about // subjects or events. If the user searches for those entities (maybe in the future) // this module doesn't have anything to search for. def entities = params.entity ?: [] if( entities instanceof String ) entities = [entities] else entities = entities.toList() if( !entities ) entities = [ "Study", "Assay", "Sample" ] def fields = [:]; entities.unique().each { entity -> switch( entity ) { case "Study": case "Assay": case "Sample": fields[ entity ] = [ "# sequences", "Tag name", "Run name" ] break; default: // Do nothing break; } } render fields as JSON } /** * Returns data for the given field and entities. * * Example call: [moduleurl]/rest/getQueryableFieldData?entity=Study&tokens=abc1&tokens=abc2&fields=# sequences&fields=# bacteria * Example response: { "abc1": { "# sequences": 141, "# bacteria": 0 }, "abc2": { "#sequences": 412 } } * * @param params.entity Entity that is searched for * @param params.tokens One or more tokens of the entities that the data should be returned for * @param params.fields One or more field names of the data to be returned. * @return JSON Map with keys being the entity tokens and the values being maps with entries [field] = [value]. Not all * fields and tokens that are asked for have to be returned by the module (e.g. when a specific entity can * not be found, or a value is not present for an entity) */ def getQueryableFieldData = { def entity = params.entity; def tokens = params.tokens ?: [] def fields = params.fields ?: [] if( tokens instanceof String ) tokens = [tokens] else tokens = tokens.toList(); if( fields instanceof String ) fields = [fields] else fields = fields.toList(); // Only search for unique tokens and fields tokens = tokens.unique() fields = fields.unique() // Without tokens or fields we can only return an empty list def map = [:] if( tokens.size() == 0 || fields.size() == 0 ) { log.trace "Return empty string for getQueryableFieldData: #tokens: " + tokens.size() + " #fields: " + fields.size() render map as JSON return; } for( token in tokens ) { def object = getQueryableObject( entity, token ); if( object ) { // Check whether the user has sufficient privileges: def study; switch( entity ) { case "Study": study = object; break; case "Assay": case "Sample": study = object.study break; default: log.error "Incorrect entity used: " + entity; continue; } if( !study.canRead( session.user ) ) { log.error "Data was requested for " + entity.toLowerCase() + " " + object.name + " but the user " + session.user.username + " doesn't have the right privileges." continue; } map[ token ] = [:] fields.each { field -> def v = getQueryableFieldValue( entity, object, field ); if( v != null ) map[ token ][ field ] = v } } else { log.trace "No " + entity + " with token " + token + " found." } } render map as JSON } /** * Searches for a specific entity * * @param entity Entity to search in * @param token Token of the entity to search in * @return */ protected def getQueryableObject( def entity, def token ) { switch( entity ) { case "Study": return Study.findByStudyToken( token ); case "Assay": return Assay.findByAssayToken( token ); case "Sample": return Sample.findBySampleToken( token ); default: // Other entities can't be handled return null; } } /** * Searches for the value of a specific field in a specific entity * * @param entity Entity of the given object * @param object Object to search in * @param field Field value to retrieve * @return */ protected def getQueryableFieldValue( def entity, def object, def field ) { if( !entity || !object || !field ) return null; // First determine all assaysamples involved, in order to return data later. // All data that has to be returned is found in assaysamples def assaySamples switch( entity ) { case "Study": assaySamples = object.assays*.assaySamples; if( assaySamples ) { assaySamples = assaySamples.flatten() } break; case "Assay": case "Sample": assaySamples = object.assaySamples; break; default: // Other entities can't be handled return null; } // If no assaySamples are involved, return null as empty value if( !assaySamples ) { return null; } // Now determine the exact field to return switch( field ) { // Returns the total number of sequences in this sample case "# sequences": return assaySamples.collect { it.numSequences() }.sum(); // Returns the unique tag names case "Tag name": return assaySamples.collect { it.tagName }.findAll { it != null }.unique(); case "Run name": return assaySamples.collect { it.run?.name }.findAll { it != null }.unique(); // Other fields are not handled default: return null; } } private def checkAssayToken( def assayToken ) { if( !assayToken || assayToken == null ) { return false } def list = [] def assay = Assay.findByAssayToken( assayToken ) if( !assay || assay == null ) { return false; } return assay; } /****************************************************************/ /* REST resources for exporting data from GSCF */ /****************************************************************/ /** * Retrieves a list of actions that can be performed on data with a specific entity. * * The module is allowed to return different fields when the user searches for different entities * * Example call: [moduleurl]/rest/getPossibleActions?entity=Assay&entity=Sample * Example response: { "Assay": [ { name: "excel", description: "Export as excel" } ], * "Sample": [ { name: "excel", description: "Export as excel" }, { name: "fasta", description: : "Export as fasta" } ] } * * @param params.entity Entity that is searched for. Might be more than one. If no entity is given, * a list of searchable fields for all entities is given * @return JSON Hashmap with keys being the entities and the values are lists with the action this module can * perform on this entity. The actions as hashmaps themselves, with keys 'name' and 'description' */ def getPossibleActions = { def entities = params.entity ?: [] if( entities instanceof String ) entities = [entities] else entities = entities.toList() if( !entities ) entities = [ "Study", "Assay", "Sample" ] def actions = [:]; entities.unique().each { entity -> switch( entity ) { case "Study": actions[ entity ] = [ [ name: "excel", description: "Export metadata", url: createLink( controller: "study", action: "exportMetaData", absolute: true ) ], [ name: "fasta", description: "Export as fasta", url: createLink( controller: "study", action: "exportAsFasta", absolute: true ) ] ] break; case "Assay": actions[ entity ] = [ [ name: "fasta", description: "Export as fasta", url: createLink( controller: "assay", action: "exportAsFasta", absolute: true ) ], [ name: "excel", description: "Export metadata", url: createLink( controller: "assay", action: "exportMetaData", absolute: true ) ] ] break; case "Sample": actions[ entity ] = [ [ name: "fasta", description: "Export as fasta", url: createLink( controller: "sample", action: "exportAsFasta", absolute: true ) ], [ name: "excel", description: "Export metadata", url: createLink( controller: "sample", action: "exportMetaData", absolute: true ) ] ] break; default: // Do nothing break; } } render actions as JSON } /****************************************************************/ /* REST resources for providing basic data to the GSCF */ /****************************************************************/ private getMeasurementTypes() { return [ "# sequences" ] } /** * Return a list of simple assay measurements matching the querying text. * * @param assayToken * @return list of measurements for token. Each member of the list is a hash. * the hash contains the three keys values pairs: value, sampleToken, and * measurementMetadata. * * Example REST call: * http://localhost:8184/metagenomics/rest/getMeasurements/query?assayToken=16S-5162 * * Resulting JSON object: * * [ "# sequences", "average quality" ] * */ def getMeasurements = { def assayToken = params.assayToken; if( !checkAssayToken( assayToken ) ) { response.sendError(404) return false } render getMeasurementTypes() as JSON } /** * Return measurement metadata for measurement * * @param assayToken * @param measurementTokens. List of measurements for which the metadata is returned. * If this is not given, then return metadata for all * measurements belonging to the specified assay. * @return list of measurements * * Example REST call: * http://localhost:8184/metagenomics/rest/getMeasurementMetadata/query?assayToken=16S-5162 * &measurementToken=# sequences * &measurementToken=average quality * * Example resulting JSON object: * * [ {"name":"# sequences","type":"raw"}, * {"name":"average quality", "unit":"Phred"} ] */ def getMeasurementMetadata = { def assayToken = params.assayToken def measurementTokens = params.measurementToken if( !checkAssayToken( assayToken ) ) { response.sendError(404) return false } // For now, we don't have any metadata about the measurements def measurements = getMeasurementTypes(); def measurementMetadata = [] measurementTokens.each { token -> if( measurements.contains( token ) ) measurementMetadata << [ "name" : token ]; } render measurementMetadata as JSON } /** * Return list of measurement data. * * @param assayTokes * @param measurementToken. Restrict the returned data to the measurementTokens specified here. * If this argument is not given, all samples for the measurementTokens are returned. * Multiple occurences of this argument are possible. * @param sampleToken. Restrict the returned data to the samples specified here. * If this argument is not given, all samples for the measurementTokens are returned. * Multiple occurences of this argument are possible. * @param boolean verbose. If this argument is not present or it's value is true, then return * the date in a redundant format that is easier to process. * By default, return a more compact JSON object as follows. * * The list contains three elements: * * (1) a list of sampleTokens, * (2) a list of measurementTokens, * (3) a list of values. * * The list of values is a matrix represented as a list. Each row of the matrix * contains the values of a measurementToken (in the order given in the measurement * token list, (2)). Each column of the matrix contains the values for the sampleTokens * (in the order given in the list of sampleTokens, (1)). * (cf. example below.) * * * @return table (as hash) with values for given samples and measurements * * * List of examples. * * * Example REST call: * http://localhost:8184/metagenomics/rest/getMeasurementData/doit?assayToken=PPSH-Glu-A * &measurementToken=total carbon dioxide (tCO) * &sampleToken=5_A * &sampleToken=1_A * &verbose=true * * Resulting JSON object: * [ {"sampleToken":"1_A","measurementToken":"total carbon dioxide (tCO)","value":28}, * {"sampleToken":"5_A","measurementToken":"total carbon dioxide (tCO)","value":29} ] * * * * Example REST call without sampleToken, without measurementToken, * and with verbose representation: * http://localhost:8184/metagenomics/rest/getMeasurementData/dossit?assayToken=PPSH-Glu-A * &verbose=true * * Resulting JSON object: * [ {"sampleToken":"1_A","measurementToken":"sodium (Na+)","value":139}, * {"sampleToken":"1_A","measurementToken":"potassium (K+)","value":4.5}, * {"sampleToken":"1_A","measurementToken":"total carbon dioxide (tCO)","value":26}, * {"sampleToken":"2_A","measurementToken":"sodium (Na+)","value":136}, * {"sampleToken":"2_A","measurementToken":"potassium (K+)","value":4.3}, * {"sampleToken":"2_A","measurementToken":"total carbon dioxide (tCO)","value":28}, * {"sampleToken":"3_A","measurementToken":"sodium (Na+)","value":139}, * {"sampleToken":"3_A","measurementToken":"potassium (K+)","value":4.6}, * {"sampleToken":"3_A","measurementToken":"total carbon dioxide (tCO)","value":27}, * {"sampleToken":"4_A","measurementToken":"sodium (Na+)","value":137}, * {"sampleToken":"4_A","measurementToken":"potassium (K+)","value":4.6}, * {"sampleToken":"4_A","measurementToken":"total carbon dioxide (tCO)","value":26}, * {"sampleToken":"5_A","measurementToken":"sodium (Na+)","value":133}, * {"sampleToken":"5_A","measurementToken":"potassium (K+)","value":4.5}, * {"sampleToken":"5_A","measurementToken":"total carbon dioxide (tCO)","value":29} ] * * * * Example REST call with default (non-verbose) view and without sampleToken: * * Resulting JSON object: * http://localhost:8184/metagenomics/rest/getMeasurementData/query? * assayToken=PPSH-Glu-A& * measurementToken=total carbon dioxide (tCO) * * Resulting JSON object: * [ ["1_A","2_A","3_A","4_A","5_A"], * ["sodium (Na+)","potassium (K+)","total carbon dioxide (tCO)"], * [139,136,139,137,133,4.5,4.3,4.6,4.6,4.5,26,28,27,26,29] ] * * Explanation: * The JSON object returned by default (i.e., unless verbose is set) is an array of three arrays. * The first nested array gives the sampleTokens for which data was retrieved. * The second nested array gives the measurementToken for which data was retrieved. * The thrid nested array gives the data for sampleTokens and measurementTokens. * * * In the example, the matrix represents the values of the above Example and * looks like this: * * 1_A 2_A 3_A 4_A 5_A * * Na+ 139 136 139 137 133 * * K+ 4.5 4.3 4.6 4.6 4.5 * * tCO 26 28 27 26 29 * */ def getMeasurementData = { def assayToken = params.assayToken def measurementTokens = params.measurementToken def sampleTokens = params.sampleToken def verbose = false if(params.verbose && (params.verbose=='true'||params.verbose==true) ) { verbose=true } def assay = checkAssayToken( assayToken ); if( !assay ) { response.sendError(404) return false } if( !measurementTokens ) { measurementTokens = [] } else if( measurementTokens.class == java.lang.String ) { measurementTokens = [ measurementTokens ] } if( !sampleTokens ) { sampleTokens = [] } else if( sampleTokens.class == java.lang.String ) { sampleTokens = [ sampleTokens ] } def data = SampleAssay.findAllByAssay( assay ); def measurements = getMeasurementTypes() def results = [] data.each { sampleAssay -> measurements.each { type -> def sample = sampleAssay.sample.sampleToken def isMatch = false // Check if this measurement should be returned if( ( measurementTokens.isEmpty() || measurementTokens.contains( type ) ) && ( sampleTokens.isEmpty() || sampleTokens.contains( sample ) ) ) { results.push( [ 'sampleToken': sample, 'measurementToken': type, 'value': sampleAssay[ type ] ] ) } } } if(!verbose) { results = compactTable( results ) } render results as JSON } /* helper function for getSamples * * Return compact JSON object for data. The format of the returned array is as follows. * * The list contains three elements: * * (1) a list of sampleTokens, * (2) a list of measurementTokens, * (3) a list of values. * * The list of values is a matrix represented as a list. Each row of the matrix * contains the values of a measurementToken (in the order given in the measurement * token list, (2)). Each column of the matrix contains the values for the sampleTokens * (in the order given in the list of sampleTokens, (1)). */ def compactTable( results ) { def i = 0 def sampleTokenIndex = [:] def sampleTokens = results.collect( { it['sampleToken'] } ).unique() sampleTokens.each{ sampleTokenIndex[it] = i++ } i = 0 def measurementTokenIndex= [:] def measurementTokens = results.collect( { it['measurementToken'] } ).unique() measurementTokens.each{ measurementTokenIndex[it] = i++ } def data = [] measurementTokens.each{ m -> sampleTokens.each{ s -> def item = results.find{ it['sampleToken']==s && it['measurementToken']==m } data.push item ? item['value'] : null } } return [ sampleTokens, measurementTokens, data ] } }