-
Notifications
You must be signed in to change notification settings - Fork 1
/
patients.go
351 lines (306 loc) · 10.2 KB
/
patients.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
package main
import (
"database/sql"
"fmt"
"strconv"
"strings"
)
func sanitizeUmlaute(s string) string {
s = strings.ReplaceAll(s, "Ä", "Ae")
s = strings.ReplaceAll(s, "Ö", "Oe")
s = strings.ReplaceAll(s, "Ü", "Ue")
s = strings.ReplaceAll(s, "ä", "ae")
s = strings.ReplaceAll(s, "ö", "oe")
s = strings.ReplaceAll(s, "ü", "ue")
s = strings.ReplaceAll(s, "ß", "ss")
return s
}
type Patients struct {
db *sql.DB
}
func InitPatients(db *sql.DB) Patients {
return Patients{
db: db,
}
}
func (patients *Patients) FetchOcaPlusPatientIds() ([]string, error) {
query := `SELECT DISTINCT patienten_id FROM dk_molekulargenetik
JOIN prozedur ON (prozedur.id = dk_molekulargenetik.id)
JOIN patient ON (patient.id = prozedur.patient_id)
WHERE panel = 'OCAPlus' AND prozedur.geloescht = 0
ORDER BY patienten_id;`
var patientenIds []string
if rows, err := db.Query(query); err == nil {
var patientenId sql.NullString
for rows.Next() {
if err := rows.Scan(&patientenId); err == nil {
patientenIds = append(patientenIds, patientenId.String)
}
}
} else {
return nil, err
}
return patientenIds, nil
}
func (patients *Patients) FetchBy(patientIDs []string, tkType string, allTk bool) ([]PatientData, error) {
query := `SELECT DISTINCT
patient.patienten_id,
geschlecht,
DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),geburtsdatum)), '%Y')+0 AS geburtsdatum,
sterbedatum,
ki.karnofsky
FROM patient
-- karnofsky
LEFT OUTER JOIN (
SELECT patienten_id, karnofsky, MAX(p.beginndatum) FROM dk_ukw_tb_basisdaten dutb
JOIN dk_tumorkonferenz dt ON (dutb.id = dt.id AND dt.tk = ?)
JOIN prozedur p ON (p.id = dutb.id)
JOIN patient pat ON (pat.id = p.patient_id)
WHERE dutb.karnofsky IS NOT NULL AND p.geloescht = 0
GROUP BY patienten_id
ORDER BY patienten_id
) ki ON (ki.patienten_id = patient.patienten_id)
WHERE patient.patienten_id IN ('` + strings.Join(patientIDs, "','") + "') ORDER BY patient.patienten_id;"
var results = []PatientData{}
if rows, err := db.Query(query, tkType); err == nil {
var patientenId sql.NullString
var sex sql.NullString
var geburtsdatum sql.NullInt16
var sterbedatum sql.NullString
var karnofsky sql.NullString
for rows.Next() {
if err := rows.Scan(&patientenId, &sex, &geburtsdatum, &sterbedatum, &karnofsky); err == nil {
result := &PatientData{
ID: AnonymizedID(patientenId.String),
}
// GENDER + SEX
if sex, err := sex.Value(); err == nil && sex != nil {
if sex == "m" {
result.Sex = "Male"
} else if sex == "w" {
result.Sex = "Female"
}
if sex == "m" {
result.Gender = "Male"
} else if sex == "w" {
result.Gender = "Female"
}
// Others - Code?
}
// AGE
if geburtsdatum, err := geburtsdatum.Value(); err == nil && geburtsdatum != nil {
result.Age = fmt.Sprint(geburtsdatum)
}
// OS_STATUS
// OS_MONTHS applied using appendDiagnoseDaten()
if sterbedatum, err := sterbedatum.Value(); err == nil && sterbedatum != nil {
result.OsStatus = "DECEASED"
} else {
result.OsStatus = "LIVING"
}
if val, err := karnofsky.Value(); err == nil && val != nil {
result.MtbEcogStatus = karnofskyToEcog(fmt.Sprintf("%s", val))
} else {
result.MtbEcogStatus = "NA"
}
result = appendDiagnoseDaten(patientenId.String, result, allTk)
// DFS
result.DfsStatus = "NA"
result.DfsMonths = "NA"
results = append(results, *result)
}
}
return results, nil
}
return nil, fmt.Errorf("keine Daten zu Patienten gefunden")
}
// Ermittelt den ECOG anhand des Karnofsky-Grads
func karnofskyToEcog(karnofsky string) string {
// Plain percent number
karnofsky = strings.ReplaceAll(karnofsky, "%", "")
karnofsky = strings.TrimSpace(karnofsky)
if karnofskyGrade, err := strconv.Atoi(karnofsky); err == nil {
if karnofskyGrade >= 90 {
return "0"
} else if karnofskyGrade >= 70 {
return "1"
} else if karnofskyGrade >= 50 {
return "2"
} else if karnofskyGrade >= 30 {
return "3"
} else if karnofskyGrade > 0 {
return "4"
} else {
return "5"
}
}
return "NA"
}
// Ermittelt Diagnosedaten zu den Patientendaten und gibt diese zurück
func appendDiagnoseDaten(patientID string, data *PatientData, allTk bool) *PatientData {
query := `SELECT
icdo3histologie,
beginndatum,
icd10,
fernmetastasen,
pcve.shortdesc AS diagnose,
ROUND(DATEDIFF(IF(sterbedatum IS NULL, NOW(), sterbedatum),diagnosedatum) / 30) AS os_month,
YEAR(sub.first_mtb) AS first_mtb_year
FROM prozedur
JOIN dk_diagnose ON prozedur.id = dk_diagnose.id
JOIN property_catalogue_version_entry pcve ON pcve.code = icd10 AND pcve.property_version_id = icd10_propcat_version
JOIN patient p on p.id = prozedur.patient_id
JOIN erkrankung_prozedur ep ON ep.prozedur_id = prozedur.id
LEFT OUTER JOIN (
SELECT erkrankung_id, MIN(beginndatum) AS first_mtb FROM prozedur p
JOIN dk_tumorkonferenz dt ON (p.id = dt.id AND dt.tk = '27')
JOIN erkrankung_prozedur ep ON (ep.prozedur_id = p.id)
GROUP BY erkrankung_id
) sub ON (sub.erkrankung_id = ep.erkrankung_id)
WHERE prozedur.geloescht = 0 AND p.patienten_id = ? AND ep.erkrankung_id IN (
SELECT ep.erkrankung_id FROM dk_tumorkonferenz
JOIN prozedur pro on dk_tumorkonferenz.id = pro.id
JOIN patient pat on pro.patient_id = pat.id
JOIN erkrankung_prozedur ep ON ep.prozedur_id = pro.id
WHERE pat.patienten_id = ? AND (dk_tumorkonferenz.tk = '27' OR 1 = ?)
ORDER BY beginndatum DESC
)
ORDER BY beginndatum DESC;`
var icdo3histologie sql.NullString
var beginndatum sql.NullString
var icd10 sql.NullString
var fernmetastasen sql.NullString
var diagnose sql.NullString
var osMonth sql.NullInt16
var firstMtbYear sql.NullString
if row := db.QueryRow(query, patientID, patientID, allTk); row != nil {
if err := row.Scan(&icdo3histologie, &beginndatum, &icd10, &fernmetastasen, &diagnose, &osMonth, &firstMtbYear); err == nil {
// OS_MONTH
// Aktuell nur ganze Monate als Kommazahl (Anzahl Tage / 30) - ermittelt über SQL-Query
if osMonth, err := osMonth.Value(); err == nil && osMonth != nil {
data.OsMonths = fmt.Sprintf("%d.0", osMonth)
}
// ICD-O3-Morphologie Code
if icdo3histologie, err := icdo3histologie.Value(); err == nil && icdo3histologie != nil {
data.IcdO3MorphCode = fmt.Sprint(icdo3histologie)
}
// ICD10 Code
if icd10, err := icd10.Value(); err == nil && icd10 != nil {
data.Icd10Code = fmt.Sprint(icd10)
}
// DIAGNOSIS
if diagnose, err := diagnose.Value(); err == nil && diagnose != nil {
data.Diagnosis = sanitizeUmlaute(fmt.Sprint(diagnose))
}
// ONKOTREE_CODE
data.OncotreeCode = "NA"
// SPREAD_OF_DISEASE
if fernmetastasen, err := fernmetastasen.Value(); err == nil && fernmetastasen != nil {
if fernmetastasen == 1 {
data.SpreadOfDisease = "metastasiert"
}
}
// Erstes Jahr mit MTB
if firstMtbYear, err := firstMtbYear.Value(); err == nil && firstMtbYear != nil {
data.XFirstMtbYear = fmt.Sprint(firstMtbYear)
}
}
}
// Erforderlich: Beruecksichtigung von Krankheiten in "Anamnesebogen"?
queryErkrankungen := `SELECT DISTINCT pcve.shortdesc
FROM prozedur
JOIN dk_diagnose ON prozedur.id = dk_diagnose.id
JOIN property_catalogue_version_entry pcve ON pcve.code = icd10 AND pcve.property_version_id = icd10_propcat_version
JOIN erkrankung_prozedur ep ON prozedur.id = ep.prozedur_id
JOIN patient p on p.id = prozedur.patient_id
WHERE p.patienten_id = ?
ORDER BY beginndatum DESC`
if rows, err := db.Query(queryErkrankungen, patientID); err == nil {
var diags []string
var erkrankung sql.NullString
for rows.Next() {
if err := rows.Scan(&erkrankung); err == nil {
if erkrankung.String != data.Diagnosis {
diags = append(diags, erkrankung.String)
}
}
}
data.PastMalignantDisease = sanitizeUmlaute(strings.Join(diags, " + "))
}
return data
}
type PatientData struct {
ID string `csv:"PATIENT_ID"`
Gender string `csv:"GENDER"`
Sex string `csv:"SEX"`
Age string `csv:"AGE"`
IcdO3MorphCode string `csv:"ICD_O3_MORPH_CODE"`
Diagnosis string `csv:"DIAGNOSIS"`
OncotreeCode string `csv:"ONCOTREE_CODE"`
Icd10Code string `csv:"ICD_10_CODE"`
SpreadOfDisease string `csv:"SPREAD_OF_DISEASE"`
MtbEcogStatus string `csv:"MTB_ECOG_STATUS"`
PastMalignantDisease string `csv:"PAST_MALIGNANT_DISEASE"`
PretherapyProgress string `csv:"PREATHERAPY_PROGRESS"`
NumSystemicPretherapy string `csv:"NUM_SYSTEMIC_PRETHERAPY"`
PretherapyMedication string `csv:"PREATHERAPY_MEDICATION"`
PretherapyMedicationNcit string `csv:"PREATHERAPY_MEDICATION_NCIT"`
PretherapyBestResponse string `csv:"PREATHERAPY_BEST_RESPONSE"`
PretherapyPfs string `csv:"PREATHERAPY_PFS"`
OsStatus string `csv:"OS_STATUS"`
OsMonths string `csv:"OS_MONTHS"`
DfsStatus string `csv:"DFS_STATUS"`
DfsMonths string `csv:"DFS_MONTHS"`
XFirstMtbYear string `csv:"x_first_mtb_year"`
}
func PatientDataHeaders() []string {
return []string{"PATIENT_ID",
"GENDER",
"SEX",
"AGE",
"ICD_O3_MORPH_CODE",
"DIAGNOSIS",
"ONCOTREE_CODE",
"ICD_10_CODE",
"SPREAD_OF_DISEASE",
"MTB_ECOG_STATUS",
"PAST_MALIGNANT_DISEASE",
"PREATHERAPY_PROGRESS",
"NUM_SYSTEMIC_PRETHERAPY",
"PREATHERAPY_MEDICATION",
"PREATHERAPY_MEDICATION_NCIT",
"PREATHERAPY_BEST_RESPONSE",
"PREATHERAPY_PFS",
"OS_STATUS",
"OS_MONTHS",
"DFS_STATUS",
"DFS_MONTHS",
"x_first_mtb_year",
}
}
func (data *PatientData) AsStringArray() []string {
return []string{
data.ID,
data.Gender,
data.Sex,
data.Age,
data.IcdO3MorphCode,
data.Diagnosis,
data.OncotreeCode,
data.Icd10Code,
data.SpreadOfDisease,
data.MtbEcogStatus,
data.PastMalignantDisease,
data.PretherapyProgress,
data.NumSystemicPretherapy,
data.PretherapyMedication,
data.PretherapyMedicationNcit,
data.PretherapyBestResponse,
data.PretherapyPfs,
data.OsStatus,
data.OsMonths,
data.DfsStatus,
data.DfsMonths,
data.XFirstMtbYear,
}
}