-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
369 lines (322 loc) · 10.8 KB
/
main.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
package main
import (
"crypto/sha256"
"database/sql"
"encoding/csv"
"encoding/hex"
"fmt"
"github.com/alecthomas/kong"
"github.com/go-sql-driver/mysql"
"github.com/gocarina/gocsv"
"golang.org/x/term"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"io"
"log"
"slices"
"strings"
"syscall"
_ "github.com/go-sql-driver/mysql"
_ "syscall"
)
var (
cli *CLI
context *kong.Context
db *sql.DB
)
type Globals struct {
User string `short:"U" help:"Database username" required:"NA"`
Password string `short:"P" help:"Database password"`
Host string `short:"H" help:"Database host" default:"localhost"`
Port int `help:"Database port" default:"3306"`
Ssl string `help:"SSL-Verbindung ('true', 'false', 'skip-verify', 'preferred')" default:"false"`
Database string `short:"D" help:"Database name" default:"onkostar"`
IDPrefix string `help:"Zu verwendender Prefix für anonymisierte IDs. 'WUE', wenn nicht anders angegeben." default:"WUE"`
AllTk bool `help:"Diagnosen: Erlaube Diagnosen mit allen Tumorkonferenzen, nicht nur Diagnosen mit MTBs"`
MtbType string `help:"MTB-Typ der Tumorkonferenz in Onkostar. Wenn nicht angegeben, Wert: '27'" default:"27"`
NoAnon bool `help:"Keine ID-Anonymisierung anwenden. Hierbei wird auch das ID-Prefix ignoriert."`
}
type PatientSelection struct {
PatientID []string `help:"PatientenIDs der zu exportierenden Patienten. Kommagetrennt bei mehreren IDs" group:"Patienten" xor:"PatientID,OcaPlus" required:"true"`
OcaPlus bool `help:"Alle Patienten mit OCAPlus-Panel" group:"Patienten" xor:"PatientID,OcaPlus" required:"true"`
PersStamm int `help:"ID des Personenstamms" group:"Patienten" default:"4"`
}
type CLI struct {
Globals
PatientSelection
ExportPatients struct {
Filename string `help:"Exportiere in diese Datei" required:"NA"`
Append bool `help:"An bestehende Datei anhängen" default:"false"`
Csv bool `help:"Verwende CSV-Format anstelle TSV-Format. Trennung mit ';' für MS Excel" default:"false"`
} `cmd:"NA" help:"Export patient data"`
ExportSamples struct {
Filename string `help:"Exportiere in diese Datei" required:"NA"`
Append bool `help:"An bestehende Datei anhängen" default:"false"`
Csv bool `help:"Verwende CSV-Format anstelle TSV-Format. Trennung mit ';' für MS Excel" default:"false"`
} `cmd:"NA" help:"Export sample data"`
ExportXlsx struct {
Filename string `help:"Exportiere in diese Datei" required:"NA"`
} `aliases:"export-xls" cmd:"NA" help:"Export all into Excel-File"`
Preview struct {
} `cmd:"NA" help:"Show patient data. Exit Preview-Mode with <CTRL>+'C'"`
FakePatients struct {
Input string `help:"Lese Einsendenummern aus dieser (MAF-)Datei" required:"NA"`
PatientFile string `help:"Exportiere Fake-Patienten in diese Datei" required:"NA"`
SamplesFile string `help:"Exportiere angepasste Samples mit Fake-PatientID in diese Datei" required:"NA"`
} `cmd:"NA" help:"Create fake patients based on samples"`
}
func initCLI() {
cli = &CLI{
Globals: Globals{},
}
context = kong.Parse(cli,
kong.Name("os2cb"),
kong.Description("A simple tool to export data from Onkostar into TSV file format for cBioportal"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
)
}
func main() {
initCLI()
gocsv.SetCSVWriter(getCsvWriter(cli.ExportPatients.Csv || cli.ExportSamples.Csv))
gocsv.SetCSVReader(getCsvReader(cli.ExportPatients.Csv || cli.ExportSamples.Csv))
if context.Command() == "fake-patients" {
fakePatients(cli)
return
}
if (context.Command() == "export-xls" || context.Command() == "export-xlsx") && !strings.HasSuffix(cli.ExportXlsx.Filename, ".xlsx") {
log.Fatalf("Cannot use filename: '%s'. Required filename suffix is '.xlsx'", cli.ExportXlsx.Filename)
return
}
if len(cli.Password) == 0 {
fmt.Print("Passwort: ")
if bytePw, err := term.ReadPassword(int(syscall.Stdin)); err == nil {
cli.Password = string(bytePw)
}
println()
}
dbCfg := mysql.Config{
User: cli.User,
Passwd: cli.Password,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", cli.Host, cli.Port),
DBName: cli.Database,
AllowNativePasswords: true,
TLSConfig: cli.Ssl,
}
if dbx, dbErr := initDb(dbCfg); dbErr == nil {
db = dbx
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println("Cannot close database connection")
}
}(db)
} else {
log.Fatalf("Cannot connect to Database: %s\n", dbErr.Error())
}
if cli.OcaPlus {
patients := InitPatients(db)
cli.PatientID, _ = patients.FetchOcaPlusPatientIds()
}
switch context.Command() {
case "export-patients":
handleCommand(cli, db, FetchAllPatientData)
case "export-samples":
handleCommand(cli, db, FetchAllSampleData)
case "export-xlsx":
exportXlsx(cli, cli.PatientID, db)
case "export-xls":
exportXlsx(cli, cli.PatientID, db)
case "preview":
preview(db)
default:
}
}
func initDb(dbCfg mysql.Config) (*sql.DB, error) {
if dbx, err := sql.Open("mysql", dbCfg.FormatDSN()); err == nil {
if err := dbx.Ping(); err == nil {
return dbx, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
func AnonymizedID(id string) string {
if cli.NoAnon {
return id
}
sha := sha256.New()
sha.Write([]byte(id))
hash := hex.EncodeToString(sha.Sum(nil))
return cli.IDPrefix + "_" + hash[0:10]
}
// Übergibt Methode zum Erstellen des passenden CsvWriters für TSV (cBioportal) oder CSV (Excel mit UTF16BE)
func getCsvWriter(isCsv bool) func(out io.Writer) *gocsv.SafeCSVWriter {
return func(out io.Writer) *gocsv.SafeCSVWriter {
win16be := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
utf16bom := unicode.BOMOverride(win16be.NewEncoder())
var writer *csv.Writer
if isCsv {
transformWriter := transform.NewWriter(out, utf16bom)
writer = csv.NewWriter(transformWriter)
writer.Comma = ';'
} else {
writer = csv.NewWriter(out)
writer.Comma = '\t'
}
return gocsv.NewSafeCSVWriter(writer)
}
}
// Übergibt Methode zum Erstellen des passenden CsvReaders für TSV (cBioportal) oder CSV (Excel mit UTF16BE)
func getCsvReader(isCsv bool) func(in io.Reader) gocsv.CSVReader {
return func(in io.Reader) gocsv.CSVReader {
win16be := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
utf16bom := unicode.BOMOverride(win16be.NewDecoder())
var reader *csv.Reader
if isCsv {
transformReader := transform.NewReader(in, utf16bom)
reader = csv.NewReader(transformReader)
reader.Comma = ';'
} else {
reader = csv.NewReader(in)
reader.Comma = '\t'
}
reader.Comment = '#'
return reader
}
}
// Bearbeitet die Ausführung und ermittelt Daten abhängig von übergebener Funktion
func handleCommand[D PatientData | SampleData](cli *CLI, db *sql.DB, fetchFunc func(patientIds []string, db *sql.DB) ([]D, error)) {
var result []D
var filename string
if len(cli.ExportPatients.Filename) > 0 {
filename = cli.ExportPatients.Filename
} else if len(cli.ExportSamples.Filename) > 0 {
filename = cli.ExportSamples.Filename
}
if cli.ExportPatients.Append || cli.ExportSamples.Append {
if r, err := ReadFile(filename, result); err == nil {
result = r
} else {
log.Fatalln(err.Error())
}
}
if r, err := fetchFunc(cli.PatientID, db); err == nil {
result = append(result, r...)
} else {
log.Fatalln(err.Error())
}
if err := WriteFile(filename, result); err != nil {
log.Fatalln(err.Error())
}
}
func exportXlsx(cli *CLI, patientIds []string, db *sql.DB) {
patientsData := make([]PatientData, 0)
samplesData := make([]SampleData, 0)
if data, err := FetchAllPatientData(patientIds, db); err == nil {
patientsData = append(patientsData, data...)
} else {
log.Printf(err.Error())
}
if data, err := FetchAllSampleData(patientIds, db); err == nil {
samplesData = append(samplesData, data...)
} else {
log.Printf(err.Error())
}
if err := WriteXlsxFile(cli.ExportXlsx.Filename, patientsData, samplesData); err != nil {
log.Fatalln(err.Error())
}
}
func preview(db *sql.DB) {
NewBrowser(cli.PatientID, cli.NoAnon, db).Show()
}
func fakePatients(cli *CLI) {
var sampleData []SampleData
if r, err := ReadFile(cli.FakePatients.Input, sampleData); err == nil {
sampleData = r
} else {
log.Fatalln(err.Error())
}
var uniqueSampleIds = make([]string, 0)
var fakePatients = make([]PatientData, 0)
var fixedSamples = make([]SampleData, 0)
// Ersetze PatientID
for _, sample := range sampleData {
if !slices.Contains(uniqueSampleIds, sample.SampleID) {
uniqueSampleIds = append(uniqueSampleIds, sample.SampleID)
}
// Neue Fake-PatientID
fakePatientId := fmt.Sprintf("2000%d", slices.IndexFunc(uniqueSampleIds, func(s string) bool {
return sample.SampleID == s
}))
sample.PatientID = fakePatientId
fixedSamples = append(fixedSamples, sample)
}
// Für jede unique Sample-ID
for idx := range uniqueSampleIds {
fakePatients = append(fakePatients, PatientData{
ID: fmt.Sprintf("2000%d", idx),
Gender: "NA",
Sex: "NA",
Age: "NA",
IcdO3MorphCode: "NA",
Diagnosis: "NA",
OncotreeCode: "NA",
Icd10Code: "NA",
SpreadOfDisease: "NA",
MtbEcogStatus: "NA",
PastMalignantDisease: "NA",
PretherapyProgress: "NA",
NumSystemicPretherapy: "NA",
PretherapyMedication: "NA",
PretherapyMedicationNcit: "NA",
PretherapyBestResponse: "NA",
PretherapyPfs: "NA",
OsStatus: "NA",
OsMonths: "NA",
DfsStatus: "NA",
DfsMonths: "NA",
XFirstMtbYear: "NA",
})
}
if err := WriteFile(cli.FakePatients.PatientFile, fakePatients); err != nil {
log.Fatalln(err.Error())
}
if err := WriteFile(cli.FakePatients.SamplesFile, fixedSamples); err != nil {
log.Fatalln(err.Error())
}
}
// Ermittelt alle Patientendaten von allen angegebenen Patienten
func FetchAllPatientData(patientIds []string, db *sql.DB) ([]PatientData, error) {
patients := InitPatients(db)
if data, err := patients.FetchBy(patientIds, cli.MtbType, cli.AllTk); err == nil {
return data, nil
} else {
if !strings.HasPrefix(context.Command(), "preview") {
log.Println(err.Error())
}
}
return []PatientData{}, nil
}
// Ermittelt alle Probendaten von allen angegebenen Patienten
func FetchAllSampleData(patientIds []string, db *sql.DB) ([]SampleData, error) {
samples := InitSamples(db, cli.OcaPlus)
var result []SampleData
for _, patientID := range patientIds {
if data, err := samples.Fetch(patientID); err == nil {
for _, d := range data {
result = append(result, d)
}
} else {
if !strings.HasPrefix(context.Command(), "preview") {
log.Println(err.Error())
}
}
}
return result, nil
}