-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcohortdata.go
412 lines (350 loc) · 15.3 KB
/
cohortdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package controllers
import (
"bytes"
"encoding/csv"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/uc-cdis/cohort-middleware/middlewares"
"github.com/uc-cdis/cohort-middleware/models"
"github.com/uc-cdis/cohort-middleware/utils"
)
type CohortDataController struct {
cohortDataModel models.CohortDataI
dataDictionaryModel models.DataDictionaryI
teamProjectAuthz middlewares.TeamProjectAuthzI
}
func NewCohortDataController(cohortDataModel models.CohortDataI, dataDictionaryModel models.DataDictionaryI, teamProjectAuthz middlewares.TeamProjectAuthzI) CohortDataController {
return CohortDataController{
cohortDataModel: cohortDataModel,
dataDictionaryModel: dataDictionaryModel,
teamProjectAuthz: teamProjectAuthz,
}
}
func (u CohortDataController) RetrieveHistogramForCohortIdAndConceptId(c *gin.Context) {
sourceId, cohortId, conceptDefsAndCohortPairs, err := utils.ParseSourceIdAndCohortIdAndVariablesAsSingleList(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request", "error": err.Error()})
c.Abort()
return
}
// parse cohortPairs separately as well, so we can validate permissions
_, cohortPairs := utils.GetConceptDefsAndValuesAndCohortPairsAsSeparateLists(conceptDefsAndCohortPairs)
validAccessRequest := u.teamProjectAuthz.TeamProjectValidation(c, []int{cohortId}, cohortPairs)
if !validAccessRequest {
log.Printf("Error: invalid request")
c.JSON(http.StatusForbidden, gin.H{"message": "access denied"})
c.Abort()
return
}
cohortData, err := u.cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId, cohortId, conceptDefsAndCohortPairs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving histogram data", "error": err.Error()})
c.Abort()
return
}
conceptValues := []float64{}
for _, personData := range cohortData {
conceptValues = append(conceptValues, float64(*personData.ConceptValueAsNumber))
}
histogramData := utils.GenerateHistogramData(conceptValues)
c.JSON(http.StatusOK, gin.H{"bins": histogramData})
}
func (u CohortDataController) RetrieveStatsForCohortIdAndConceptId(c *gin.Context) {
sourceId, cohortId, conceptDefsAndCohortPairs, err := utils.ParseSourceIdAndCohortIdAndVariablesAsSingleList(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request", "error": err.Error()})
c.Abort()
return
}
// parse cohortPairs separately as well, so we can validate permissions
_, cohortPairs := utils.GetConceptDefsAndValuesAndCohortPairsAsSeparateLists(conceptDefsAndCohortPairs)
validAccessRequest := u.teamProjectAuthz.TeamProjectValidation(c, []int{cohortId}, cohortPairs)
if !validAccessRequest {
log.Printf("Error: invalid request")
c.JSON(http.StatusForbidden, gin.H{"message": "access denied"})
c.Abort()
return
}
cohortData, err := u.cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId, cohortId, conceptDefsAndCohortPairs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving concept details", "error": err.Error()})
c.Abort()
return
}
conceptValues := []float64{}
for _, personData := range cohortData {
conceptValues = append(conceptValues, float64(*personData.ConceptValueAsNumber))
}
conceptToStat, errGetLast := utils.CheckAndGetLastCustomConceptVariableDef(conceptDefsAndCohortPairs)
if errGetLast != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error: last variable should be of numeric type", "error": errGetLast.Error()})
c.Abort()
return
}
statsData := utils.GenerateStatsData(cohortId, conceptToStat.ConceptId, conceptValues)
c.JSON(http.StatusOK, gin.H{"statsData": statsData})
}
func (u CohortDataController) RetrieveDataBySourceIdAndCohortIdAndVariables(c *gin.Context) {
// TODO - add some validation to ensure that only calls from Argo are allowed through since it outputs FULL data?
// -> this concern is considered to be addressed by https://github.com/uc-cdis/cloud-automation/pull/1884
sourceId, cohortId, conceptDefsAndCohortPairs, err := utils.ParseSourceIdAndCohortIdAndVariablesAsSingleList(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request", "error": err.Error()})
c.Abort()
return
}
// parse cohortPairs separately as well, so we can validate permissions
conceptDefs, cohortPairs := utils.GetConceptDefsAndValuesAndCohortPairsAsSeparateLists(conceptDefsAndCohortPairs)
validAccessRequest := u.teamProjectAuthz.TeamProjectValidation(c, []int{cohortId}, cohortPairs)
if !validAccessRequest {
log.Printf("Error: invalid request")
c.JSON(http.StatusForbidden, gin.H{"message": "access denied"})
c.Abort()
return
}
// Iterate over conceptDefsAndCohortPairs and collect the concept values for each person:
// {PersonId:1, ConceptId:1, ConceptValue: "A value with, comma!"},
// {PersonId:1, ConceptId:2, ConceptValue: B},
// {PersonId:2, ConceptId:1, ConceptValue: C},
var variablesToQuery []interface{}
var finalConceptDataset []*models.PersonConceptAndValue
for _, item := range conceptDefsAndCohortPairs {
variablesToQuery = append(variablesToQuery, item)
// if item is of type CustomConceptVariableDef, get the data:
if _, ok := item.(utils.CustomConceptVariableDef); ok {
// use variablesToQuery to query an increasingly tight set (simulating the attrition table that generated this query)
cohortData, err := u.cohortDataModel.RetrieveHistogramDataBySourceIdAndCohortIdAndConceptDefsPlusCohortPairs(sourceId, cohortId, variablesToQuery)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving concept details", "error": err.Error()})
c.Abort()
return
}
// add to final concept data set:
finalConceptDataset = append(finalConceptDataset, cohortData...)
}
}
conceptIds := utils.ExtractConceptIdsFromCustomConceptVariablesDef(conceptDefs)
partialCSV := GeneratePartialCSV(sourceId, finalConceptDataset, conceptIds) // use conceptdefs to improve column description? nah...no person is reading this table....just needs to be unique
personIdToCSVValues, err := u.RetrievePeopleIdAndCohort(sourceId, cohortId, cohortPairs, finalConceptDataset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving people ID to csv value map", "error": err.Error()})
c.Abort()
return
}
b := GenerateCompleteCSV(partialCSV, personIdToCSVValues, cohortPairs)
c.String(http.StatusOK, b.String())
}
func generateCohortPairsHeaders(cohortPairs []utils.CustomDichotomousVariableDef) []string {
cohortPairsHeaders := []string{}
for _, cohortPair := range cohortPairs {
cohortPairsHeaders = append(cohortPairsHeaders, utils.GetCohortPairKey(cohortPair.CohortDefinitionId1, cohortPair.CohortDefinitionId2))
}
return cohortPairsHeaders
}
func GenerateCompleteCSV(partialCSV [][]string, personIdToCSVValues map[int64]map[string]string, cohortPairs []utils.CustomDichotomousVariableDef) *bytes.Buffer {
b := new(bytes.Buffer)
w := csv.NewWriter(b)
w.Comma = ',' // CSV
cohortPairHeaders := generateCohortPairsHeaders(cohortPairs)
partialCSV[0] = append(partialCSV[0], cohortPairHeaders...)
for i := 1; i < len(partialCSV); i++ {
personId, _ := strconv.ParseInt(partialCSV[i][0], 10, 64)
for _, cohortPair := range cohortPairHeaders {
partialCSV[i] = append(partialCSV[i], personIdToCSVValues[personId][cohortPair])
}
}
// TODO - is there a way to write as the rows are produced? Building up all rows in memory
// could cause issues if the cohort vs concepts matrix gets very large...or will the number of concepts
// queried at the same time never be very large? Should we restrict the number of concepts to
// a max here in this method?
err := w.WriteAll(partialCSV)
if err != nil {
log.Fatal(err)
}
return b
}
// This function will take the given cohort data and transform it into a matrix
// that contains the person id as the first column and the concept values found
// for this person in the subsequent columns. The transformation is necessary
// since the cohortData list contains one row per person-concept combination.
// E.g. the following (simplified version of the) data:
//
// {PersonId:1, ConceptId:1, ConceptValue: "A value with, comma!"},
// {PersonId:1, ConceptId:2, ConceptValue: B},
// {PersonId:2, ConceptId:1, ConceptValue: C},
//
// will be transformed to a CSV table like:
//
// sample.id,ID_concept_id1,ID_concept_id2
// 1,"A value with, comma!",B
// 2,Simple value,NA
//
// where "NA" means that the person did not have a data element for that concept
// or that the data element had a NULL/empty value.
func GeneratePartialCSV(sourceId int, cohortData []*models.PersonConceptAndValue, conceptIds []int64) [][]string {
var rows [][]string
var header []string
header = append(header, "sample.id")
header = addConceptsToHeader(sourceId, header, conceptIds)
rows = append(rows, header)
var currentPersonId int64 = -1
var row []string
for _, cohortDatum := range cohortData {
// if new person, start new row:
if cohortDatum.PersonId != currentPersonId {
if currentPersonId != -1 {
rows = append(rows, row)
}
row = []string{}
row = append(row, strconv.FormatInt(cohortDatum.PersonId, 10))
row = appendInitEmptyConceptValues(row, len(conceptIds))
currentPersonId = cohortDatum.PersonId
}
row = populateConceptValue(row, *cohortDatum, conceptIds)
}
// append last person row:
rows = append(rows, row)
return rows
}
func addConceptsToHeader(sourceId int, header []string, conceptIds []int64) []string {
for i := 0; i < len(conceptIds); i++ {
//var conceptName = getConceptName(sourceId, conceptIds[i]) // instead of name, we now prefer ID_concept_id...below:
var conceptPrefixedId = models.GetPrefixedConceptId(conceptIds[i])
header = append(header, conceptPrefixedId)
}
return header
}
func appendInitEmptyConceptValues(row []string, nrConceptIds int) []string {
for i := 0; i < nrConceptIds; i++ {
row = append(row, "NA")
}
return row
}
func populateConceptValue(row []string, cohortItem models.PersonConceptAndValue, conceptIds []int64) []string {
var conceptIdIdx int = utils.Pos(cohortItem.ConceptId, conceptIds)
if conceptIdIdx != -1 {
// conceptIdIdx+1 because first column is sample.id:
conceptIdxInRow := conceptIdIdx + 1
if cohortItem.ConceptClassId == "MVP Continuous" {
if cohortItem.ConceptValueAsNumber != nil {
row[conceptIdxInRow] = strconv.FormatFloat(float64(*cohortItem.ConceptValueAsNumber), 'f', 2, 64)
}
} else {
// default to the ObservationValueAsConceptName for now:
if cohortItem.ObservationValueAsConceptName != "" {
row[conceptIdxInRow] = cohortItem.ObservationValueAsConceptName
}
}
}
return row
}
func (u CohortDataController) RetrieveCohortOverlapStats(c *gin.Context) {
errors := make([]error, 4)
var sourceId, caseCohortId, controlCohortId int
var conceptDefsAndCohortPairs []interface{}
sourceId, errors[0] = utils.ParseNumericArg(c, "sourceid")
caseCohortId, errors[1] = utils.ParseNumericArg(c, "casecohortid")
controlCohortId, errors[2] = utils.ParseNumericArg(c, "controlcohortid")
conceptDefsAndCohortPairs, errors[3] = utils.ParseConceptDefsAndDichotomousDefsAsSingleList(c)
_, cohortPairs := utils.GetConceptDefsAndValuesAndCohortPairsAsSeparateLists(conceptDefsAndCohortPairs)
validAccessRequest := u.teamProjectAuthz.TeamProjectValidation(c, []int{caseCohortId, controlCohortId}, cohortPairs)
if !validAccessRequest {
log.Printf("Error: invalid request")
c.JSON(http.StatusForbidden, gin.H{"message": "access denied"})
c.Abort()
return
}
if utils.ContainsNonNil(errors) {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request"})
c.Abort()
return
}
overlapStats, err := u.cohortDataModel.RetrieveCohortOverlapStats(sourceId, caseCohortId,
controlCohortId, conceptDefsAndCohortPairs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "Error retrieving stats", "error": err.Error()})
c.Abort()
return
}
c.JSON(http.StatusOK, gin.H{"cohort_overlap": overlapStats})
}
func convertCohortPeopleDataToMap(cohortPeopleData []*models.PersonIdAndCohort) map[int64]int64 {
personIdToCohortDefinitionId := make(map[int64]int64)
for _, cohortPersonData := range cohortPeopleData {
personIdToCohortDefinitionId[cohortPersonData.PersonId] = cohortPersonData.CohortId
}
return personIdToCohortDefinitionId
}
func generateCohortPairCSVValue(personId int64, firstCohortValue int64, secondCohortValue int64) string {
if firstCohortValue == 0 && secondCohortValue == 0 {
return "NA" // the person is not in either cohort
}
if firstCohortValue > 0 && secondCohortValue > 0 {
log.Printf("person with id %v has an overlap and is in cohort %v and cohort %v", personId, firstCohortValue, secondCohortValue)
return "NA" // the person is overlapped
}
if firstCohortValue > 0 {
return "0" // the person belongs to the first cohort
}
if secondCohortValue > 0 {
return "1" // the person belongs to the second cohort
}
log.Printf("error with personId %v with first cohort value %v and second cohort value %v", personId, firstCohortValue, secondCohortValue)
return "NA"
}
func getAllPeopleIdInCohortData(cohortData []*models.PersonConceptAndValue) []int64 {
var personIds []int64
for _, data := range cohortData {
personIds = append(personIds, data.PersonId)
}
return personIds
}
func (u CohortDataController) RetrievePeopleIdAndCohort(sourceId int, cohortId int, cohortPairs []utils.CustomDichotomousVariableDef, cohortData []*models.PersonConceptAndValue) (map[int64]map[string]string, error) {
peopleIds := getAllPeopleIdInCohortData(cohortData)
/**
makes a map of {
"{person_id}" : {
"{first_cohort}_{second_cohort}": "{csv_value}"
}
}
*/
personIdToCSVValues := make(map[int64]map[string]string)
for _, cohortPair := range cohortPairs {
firstCohortDefinitionId := cohortPair.CohortDefinitionId1
secondCohortDefinitionId := cohortPair.CohortDefinitionId2
cohortPairKey := utils.GetCohortPairKey(firstCohortDefinitionId, secondCohortDefinitionId)
firstCohortPeopleData, err1 := u.cohortDataModel.RetrieveDataByOriginalCohortAndNewCohort(sourceId, cohortId, firstCohortDefinitionId)
secondCohortPeopleData, err2 := u.cohortDataModel.RetrieveDataByOriginalCohortAndNewCohort(sourceId, cohortId, secondCohortDefinitionId)
if err1 != nil || err2 != nil {
return nil, fmt.Errorf("getting cohort people data failed")
}
firstCohortPeopleMap := convertCohortPeopleDataToMap(firstCohortPeopleData)
secondCohortPeopleMap := convertCohortPeopleDataToMap(secondCohortPeopleData)
for _, personId := range peopleIds {
CSVValue := generateCohortPairCSVValue(personId, firstCohortPeopleMap[personId], secondCohortPeopleMap[personId])
_, exists := personIdToCSVValues[personId]
if exists {
personIdToCSVValues[personId][cohortPairKey] = CSVValue
} else {
personIdToCSVValues[personId] = map[string]string{cohortPairKey: CSVValue}
}
}
}
return personIdToCSVValues, nil
}
func (u CohortDataController) RetrieveDataDictionary(c *gin.Context) {
var dataDictionary, error = u.dataDictionaryModel.GetDataDictionary()
if dataDictionary == nil {
c.JSON(http.StatusServiceUnavailable, error)
} else {
c.JSON(http.StatusOK, dataDictionary)
}
}
func (u CohortDataController) GenerateDataDictionary(c *gin.Context) {
log.Printf("Generating Data Dictionary...")
go u.dataDictionaryModel.GenerateDataDictionary()
c.JSON(http.StatusOK, "Data Dictionary Kicked Off")
}