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 #66

Open
wants to merge 5 commits into
base: gb-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
119 changes: 60 additions & 59 deletions grails-app/services/org/transmartproject/db/RestExportService.groovy
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.transmartproject.db

import grails.transaction.Transactional
import groovy.json.JsonException
import groovy.json.JsonSlurper
import org.springframework.beans.factory.annotation.Autowired
import org.transmartproject.core.dataquery.highdim.HighDimensionResource
import org.transmartproject.core.dataquery.highdim.assayconstraints.AssayConstraint
import org.transmartproject.core.exceptions.InvalidArgumentsException
import org.transmartproject.core.exceptions.NoSuchResourceException
import org.transmartproject.core.ontology.ConceptsResource
import org.transmartproject.core.ontology.OntologyTerm
import org.transmartproject.export.Datatypes
import org.transmartproject.export.Tasks.DataExportFetchTask
import org.transmartproject.export.Tasks.DataExportFetchTaskFactory
import org.transmartproject.core.dataquery.highdim.HighDimensionResource
import org.transmartproject.core.ontology.ConceptsResource

import static org.transmartproject.core.ontology.OntologyTerm.VisualAttributes.HIGH_DIMENSIONAL

Expand All @@ -27,47 +32,39 @@ class RestExportService {
task.getTsv()
}

def formatDataTypes(List datatypes){
//Maybe make this a marshaller.
def returnDataTypeList = []
def dataTypesList = []
datatypes.each { dataTypeMap ->
dataTypeMap.each {key, value->
if (key in dataTypesList){
returnDataTypeList.each { map ->
if (key in map.values()){
value.each{it.remove('datatypeCode')}
map.get('cohorts').add([concepts:value])
}
}
} else{
dataTypesList.add(key)
def datatypeCode = value.collect({it ->
it.get('datatypeCode')})
value.each{it.remove('datatypeCode')}
def datatypeMap = [dataType:key,
dataTypeCode: datatypeCode[0],
cohorts:[[concepts:value]]]
returnDataTypeList.add(datatypeMap)
}
public List<Datatypes> retrieveDataTypes(params) {
if (!(params.containsKey('concepts'))) {
throw new NoSuchResourceException("No parameter named concepts was given.")
}

if (params.get('concepts') == "") {

Choose a reason for hiding this comment

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

can be replaced with

if (params.get('concepts')) 

throw new InvalidArgumentsException("Parameter concepts has no value.")
}

def jsonSlurper = new JsonSlurper()
def conceptParameters = params.get('concepts').decodeURL()
List<Datatypes> dataTypes = []
try {
def conceptArguments = jsonSlurper.parseText(conceptParameters)
int cohortNumber = 0
conceptArguments.cohorts.collect { it ->
cohortNumber += 1
getDataTypes(it, dataTypes, cohortNumber)
}
dataTypes
} catch (JsonException e) {
throw new InvalidArgumentsException("Given parameter was non valid JSON.")
}
returnDataTypeList
}


def getDataTypes(List conceptKeysList){
List cohortDataTypes = []
Map datatypesMap = [:]
conceptKeysList.each { conceptKey ->
OntologyTerm concept = conceptsResourceService.getByKey(conceptKey)
datatypesMap = getHighDimDataType(concept, datatypesMap)
private List<Datatypes> getDataTypes(Map conceptKeysList, List dataTypes, int cohortNumber) {
conceptKeysList.conceptKeys.collect { conceptKey ->
getDataType(conceptKey, dataTypes, cohortNumber)
}
cohortDataTypes += datatypesMap
cohortDataTypes
}

def getHighDimDataType(OntologyTerm term, Map datatypesMap) {
private void getDataType(String conceptKey, List dataTypes, int cohortNumber) {
OntologyTerm term = conceptsResourceService.getByKey(conceptKey)
// Retrieve all descendant terms that have the HIGH_DIMENSIONAL attribute

Choose a reason for hiding this comment

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

this comment is intended as a description of this method right?
Then you should put it on top of the method declaration like this:

/**
 * description of the functionality
 */
 method() {}  

def terms = term.getAllDescendants() + term
def highDimTerms = terms.findAll { it.visualAttributes.contains(HIGH_DIMENSIONAL) }
Expand All @@ -83,34 +80,38 @@ class RestExportService {
})
]
)
//datatypes contains the number of patients for each datatype.
def datatypes = highDimensionResourceService.getSubResourcesAssayMultiMap([constraint])
datatypes.collect({ key, value ->
String datatype = key.dataTypeDescription
String datatypeCode = key.dataTypeName
if (datatype in datatypesMap.keySet()){
// term.getPatientCount() can also be value.size(). They give different numbers but I'm not sure
// what's the difference
datatypesMap[datatype].add([numOfPatients: term.getPatientCount(), conceptPath: term.fullName , datatypeCode: datatypeCode])
} else{
datatypesMap[datatype] = [[numOfPatients: term.getPatientCount(), conceptPath: term.fullName, datatypeCode: datatypeCode]]
}
addDataType(term, dataTypes, cohortNumber, key)
})
datatypesMap
}
else {
} else {
// No high dimensional data found for this term, this means it is clinical data
String datatype = "Clinical data"
String datatypeCode = "clinical"
if (datatype in datatypesMap.keySet()){
// term.getPatientCount() can also be value.size(). They give different numbers but I'm not sure
// what's the difference
datatypesMap[datatype].add([numOfPatients: term.patientCount, conceptPath: term.fullName , datatypeCode: datatypeCode])
} else{
datatypesMap[datatype] = [[numOfPatients: term.patientCount, conceptPath: term.fullName, datatypeCode: datatypeCode]]
}
datatypesMap
addDataType(term, dataTypes, cohortNumber)
}
}

private void addDataType(OntologyTerm term, List dataTypes, int cohortNumber, datatype = null) {
String dataTypeString = datatype ? datatype.dataTypeDescription : "Clinical data"
String dataTypeCode = datatype ? datatype.dataTypeName : "clinical"
List tempDataTypes = dataTypes.collect { it.dataType }
if (dataTypeString in tempDataTypes) {
int index = tempDataTypes.indexOf(dataTypeString)
Datatypes dataType = dataTypes[index]
addOntologyTerm(term, dataType, cohortNumber)
} else {
Datatypes dataType = new Datatypes(dataType: dataTypeString, dataTypeCode: dataTypeCode)
addOntologyTerm(term, dataType, cohortNumber)
dataTypes.add(dataType)
}
}

private void addOntologyTerm(OntologyTerm term, Datatypes dataType, int cohortNumberID) {
if (cohortNumberID in dataType.OntologyTermsMap.keySet()) {
dataType.OntologyTermsMap[cohortNumberID].add(term)
} else {
dataType.OntologyTermsMap[cohortNumberID] = [term]
}

}

}
13 changes: 13 additions & 0 deletions src/groovy/org/transmartproject/export/Datatypes.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.transmartproject.export

import org.transmartproject.core.ontology.OntologyTerm

class Datatypes {

Map<Integer, List<OntologyTerm>> OntologyTermsMap = [:]

String dataType

String dataTypeCode

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class AbstractI2b2MetadataTests {
<CreationDateTime>04/15/2007 01:23:14</CreationDateTime>
<TestID>UA-BLD</TestID>
<TestName>Occult Blood</TestName>
<DataType>Enum</DataType>
<CodeType>GRP</CodeType>
<Loinc>5794-3</Loinc>
<Flagstouse>HL</Flagstouse>
Expand Down