Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Add serializer for DataType retrieval #36

Open
wants to merge 5 commits into
base: gb-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package org.transmartproject.rest
import grails.validation.Validateable
import groovy.json.JsonException
import groovy.json.JsonSlurper
import org.apache.commons.lang.NullArgumentException
import org.springframework.beans.factory.annotation.Autowired
import org.transmartproject.core.exceptions.InvalidArgumentsException
import org.transmartproject.core.exceptions.NoSuchResourceException
import org.transmartproject.db.RestExportService

class ExportController {
Expand All @@ -29,31 +29,8 @@ class ExportController {
* Returns datatypes and patient number of given concepts.
*
*/
def datatypes(){
def jsonSlurper = new JsonSlurper()
if (!(params.containsKey('concepts'))){
throw new NoSuchElementException(
"No parameter named concepts."
)
}
def test = params.get('concepts').decodeURL()
try {
def concept_arguments = jsonSlurper.parseText(test)
if (concept_arguments==null){
throw new NullArgumentException(
"Parameter concepts has no value."
)
}
List datatypes = []
concept_arguments.each { it ->
List conceptKeysList = it.conceptKeys
datatypes += restExportService.getDataTypes(conceptKeysList)
}
respond(restExportService.formatDataTypes(datatypes))
} catch(JsonException e){
"Given value was non valid JSON."
}

def datatypes() throws NoSuchResourceException {
respond restExportService.retrieveDataTypes(params)
}

private void throwIfInvalid(command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class PatientSetController {
* GET /patient_sets
*/
def index() {
respond queriesResource.getQueryResultsSummaryByUsername(currentUser.getUsername() )
String noPatients = "There are no saved patient sets by $currentUser.username"
def patientSets = queriesResource.getQueryResultsSummaryByUsername(currentUser.getUsername() )
patientSets = patientSets == null ? noPatients : patientSets
respond patientSets

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it wrap the noPatients string in a JSON object here?
Why not return an empty Json object instead?

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.transmartproject.rest.marshallers

import org.transmartproject.export.DataTypeRetrieved

class DataTypeSerializationHelper extends AbstractHalOrJsonSerializationHelper<DataTypeRetrieved>{

final Class targetType = DataTypeRetrieved
final String collectionName = 'dataTypeRetrieved'

@Override
Map<String, Object> convertToMap(DataTypeRetrieved dataTypeRetrieved) {
def cohortInfoList = []
def cohortsMap = [:]
dataTypeRetrieved.OntologyTermsMap.each { ID, terms ->
terms.collect { term ->
if (ID in cohortsMap.keySet()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you describe the desired control flow here? I am not sure if I get it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The frontend wants to know which concepts are in which cohort. Now a cohort can only contain one study, so the most logical thing to do would be to differentiate between studies. However in the near future cross studies will be added, which would result in this serialiser not functioning properly. So there needed to be a way to easily differentiate between the cohorts. This way it also supports multiple cohorts, which is also planned for the future. So based on (cohort)ID I create the structure for cohorts.

cohortsMap[ID].add([subjects: term.patients.collect({ it.id }), conceptPath: term.fullName])
} else {
cohortsMap[ID] = [[subjects: term.patients.collect({ it.id }), conceptPath: term.fullName]]
}
}
}
cohortsMap.each{ key, value ->
cohortInfoList.add([concepts:value])
}
def datatypeMap = [dataType:dataTypeRetrieved.dataType,
dataTypeCode: dataTypeRetrieved.dataTypeCode,
cohorts:cohortInfoList]
}

}
14 changes: 6 additions & 8 deletions test/functional/org/transmartproject/rest/ExportTests.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.transmartproject.rest

import org.codehaus.groovy.grails.web.mime.MimeType

import org.codehaus.groovy.grails.web.json.JSONArray
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.*

Expand All @@ -22,7 +21,7 @@ class ExportTests extends ResourceTestCase{
hasEntry('dataTypeCode', 'mrna'),
hasEntry(is('cohorts'), contains(allOf(
hasEntry(is('concepts'), contains(allOf(
hasEntry('numOfPatients', 0),
hasEntry(is('subjects'), contains(-101)),
hasEntry('conceptPath', "\\foo\\study1\\bar\\")
)))
))),
Expand All @@ -31,17 +30,16 @@ class ExportTests extends ResourceTestCase{
hasEntry('dataType', 'Clinical data'),
hasEntry('dataTypeCode', 'clinical'),
hasEntry(is('cohorts'), contains(allOf(
hasEntry(is('concepts'), allOf(
contains(
hasEntry(is('concepts'), contains(
allOf(
hasEntry('numOfPatients', 0),
hasEntry(is('subjects'), is(JSONArray.class)),
hasEntry('conceptPath', "\\foo\\study2\\long path\\")
),
allOf(
hasEntry('numOfPatients', 0),
hasEntry(is('subjects'), containsInAnyOrder(-201, -202)),
hasEntry('conceptPath', "\\foo\\study2\\sex\\")
))
))
)
))),
)
)))
Expand Down