-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheet.go
345 lines (314 loc) · 8.43 KB
/
sheet.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
package main
import (
"log"
"os"
"strconv"
"strings"
"github.com/liserjrqlxue/goUtil/fmtUtil"
"github.com/liserjrqlxue/goUtil/osUtil"
"github.com/liserjrqlxue/goUtil/simpleUtil"
"github.com/liserjrqlxue/goUtil/textUtil"
"github.com/liserjrqlxue/goUtil/xlsxUtil"
"github.com/liserjrqlxue/simple-util"
"github.com/tealeg/xlsx/v3"
"github.com/xuri/excelize/v2"
"github.com/liserjrqlxue/anno2xlsx/v2/anno"
)
func addFile2Row(file string, row *xlsx.Row) (rows []string) {
rows = textUtil.File2Array(file)
xlsxUtil.AddArray2Row(rows, row)
return
}
func addFamInfoSheet(excel *xlsx.File, sheetName string, sampleList []string) {
var sheet = xlsxUtil.AddSheet(excel, sheetName)
var row = sheet.AddRow()
if row.GetHeight() == 0 {
row.SetHeight(14)
}
row.AddCell().SetString("SampleID")
for _, sampleID := range sampleList {
row = sheet.AddRow()
if row.GetHeight() == 0 {
row.SetHeight(14)
}
row.AddCell().SetString(sampleID)
}
}
func addQCSheet(excel *xlsx.File, sheetName string, qualityColumn []string, qualitys []map[string]string) {
sheet, err := excel.AddSheet(sheetName)
simple_util.CheckErr(err)
for _, key := range qualityColumn {
var row = sheet.AddRow()
if row.GetHeight() == 0 {
row.SetHeight(14)
}
row.AddCell().SetString(key)
for _, item := range qualitys {
row.AddCell().SetString(item[key])
}
}
}
func addCnv2Sheet(
sheet *xlsx.Sheet, title, paths []string, sampleMap map[string]bool, filterSize, filterGene bool, stats map[string]int,
key, gender string, cnvFile *os.File) {
cnvDb, _ := simple_util.LongFiles2MapArray(paths, "\t", nil)
for _, item := range cnvDb {
// 跳过其他样品
if !sampleMap[item["Sample"]] {
continue
}
if *wesim {
if item["chromosome"] == "" {
item["chromosome"] = strings.TrimLeft(item["Chr"], "chr")
}
if item["start"] == "" {
item["start"] = item["Start"]
}
if item["end"] == "" {
item["end"] = item["End"]
}
if item["cn"] == "" {
if item["Copy_Num"] != "" {
item["cn"] = item["Copy_Num"]
} else if item["Copy_Number"] != "" {
item["cn"] = item["Copy_Number"]
}
}
if gender != "" {
item["gender"] = strings.Split(gender, ",")[0]
}
}
item["Primer"] = anno.CnvPrimer(item, sheet.Name)
var gene = item["OMIM_Gene"]
var geneIDs []string
for _, g := range strings.Split(gene, ";") {
var id, ok = gene2id[g]
if !ok {
if g != "-" && g != "." {
if *warn {
log.Printf("can not find gene id of [%s]:[%s]\n", g, gene)
} else {
log.Fatalf("can not find gene id of [%s]:[%s]\n", g, gene)
}
}
}
geneIDs = append(geneIDs, id)
}
chpo.Annos(item, "\n", geneIDs)
// 基因-疾病
diseaseDb.Annos(item, "\n", geneIDs)
// 突变频谱
spectrumDb.Annos(item, "\n", geneIDs)
if *cnvAnnot {
anno.UpdateCnvAnnot(gene, sheet.Name, item, gene2id, diseaseDb.Db)
}
item["OMIM"] = item["OMIM_Phenotype_ID"]
stats[key]++
if item["OMIM"] != "" {
stats["Tier1"+key]++
}
if filterGene && item["OMIM"] == "" {
continue
}
if filterSize {
length, err := strconv.ParseFloat(item["Len(Kb)"], 16)
if err != nil {
log.Printf(
"can not ParseFloat of Len(Kb)[%s] for Summary[%s]\n",
item["Len(Kb)"], item["Summary"],
)
} else if length < 1000 {
continue
}
}
xlsxUtil.AddMap2Row(item, title, sheet.AddRow())
if *wesim {
var cnvArray []string
for _, key := range cnvColumn {
cnvArray = append(cnvArray, item[key])
}
fmtUtil.FprintStringArray(cnvFile, cnvArray, "\t")
}
}
if *wesim {
simpleUtil.CheckErr(cnvFile.Close())
}
}
func addTier2Row(tier2 *xlsxTemplate, item map[string]string) {
tier2Row := tier2.sheet.AddRow()
for _, str := range tier2.title {
switch str {
case "HGMDorClinvar":
if isEnProduct[*productID] {
tier2Row.AddCell().SetString(transEN[item[str]])
} else {
tier2Row.AddCell().SetString(item[str])
}
case "DiseaseName/ModeInheritance":
inheritance := strings.Split(item["ModeInheritance"], "\n")
var disease []string
if isEnProduct[*productID] {
disease = strings.Split(item["DiseaseNameEN"], "\n")
} else {
disease = strings.Split(item["DiseaseNameCH"], "\n")
}
if len(disease) == len(inheritance) {
for i, text := range disease {
inheritance[i] = text + "/" + inheritance[i]
}
} else {
log.Fatalf("Disease error:%s\t%v vs %v\n", item["Gene Symbol"], disease, inheritance)
}
tier2Row.AddCell().SetString(strings.Join(inheritance, "\n"))
item["DiseaseName/ModeInheritance"] = strings.Join(inheritance, "<br>")
default:
tier2Row.AddCell().SetString(item[str])
}
}
}
func appendLOHs(excel *xlsxUtil.File, lohs, lohSheetName string, sampleList []string) {
for i, path := range strings.Split(lohs, ",") {
var sampleID = strconv.Itoa(i)
if i < len(sampleList) {
sampleID = sampleList[i]
}
excel.AppendSheet(*xlsxUtil.OpenFile(path).File.Sheet[lohSheetName], sampleID+"-loh")
}
}
func addExon() {
if *exon != "" {
anno.LoadGeneTrans(anno.GuessPath(TomlTree.Get("annotation.Gene.transcript").(string), dbPath))
var paths []string
for _, path := range strings.Split(*exon, ",") {
if osUtil.FileExists(path) {
paths = append(paths, path)
} else {
log.Printf("ERROR:not exists or not a file:%v \n", path)
}
}
addCnv2Sheet(
tier1Xlsx.Sheet["exon_cnv"], exonCnvTitle, paths, sampleMap,
false, *cnvFilter, stats, "exonCNV", *gender, exonFile,
)
logTime("add exon cnv")
}
}
func addLarge() {
if *large != "" {
var paths []string
var pathMap = make(map[string]bool)
for _, path := range strings.Split(*large, ",") {
if osUtil.FileExists(path) {
pathMap[path] = true
} else {
log.Printf("ERROR:not exists or not a file:%v \n", path)
}
}
for path := range pathMap {
paths = append(paths, path)
}
addCnv2Sheet(
tier1Xlsx.Sheet["large_cnv"], largeCnvTitle, paths, sampleMap,
*cnvFilter, false, stats, "largeCNV", *gender, largeFile,
)
logTime("add large cnv")
}
}
func addExtra() {
// extra sheet
if *extra != "" {
extraArray := strings.Split(*extra, ",")
extraSheetArray := strings.Split(*extraSheetName, ",")
if len(extraArray) != len(extraSheetArray) {
log.Printf(
"extra files not equal length to sheetnames:%+vvs.%+v",
extraArray,
extraSheetArray,
)
} else {
for i := range extraArray {
if strings.HasSuffix(extraArray[i], "xlsx") {
simpleUtil.HandleError(
tier1Xlsx.AppendSheet(
*xlsxUtil.OpenFile(extraArray[i]).File.Sheet[extraSheetArray[i]],
extraSheetArray[i],
),
)
} else {
xlsxUtil.AddSlice2Sheet(
textUtil.File2Slice(extraArray[i], "\t"),
xlsxUtil.AddSheet(tier1Xlsx, extraSheetArray[i]),
)
}
}
}
}
}
func addQC() {
parseQC()
// QC Sheet
updateQC(stats, qualitys[0])
addQCSheet(tier1Xlsx, "quality", qualityColumn, qualitys)
logTime("add qc")
}
func addLOH() {
// append loh sheet
if *loh != "" {
appendLOHs(&xlsxUtil.File{File: tier1Xlsx}, *loh, *lohSheet, sampleList)
}
}
func fillSheet() {
parseList()
addExon()
addLarge()
addExtra()
addFamInfoSheet(tier1Xlsx, "fam_info", sampleList)
addFV()
addLOH()
// need stats
addQC()
}
func saveExcel() {
if *save {
if *wgs && *snv != "" {
simpleUtil.CheckErr(wgsXlsx.Save(*prefix + ".WGS.xlsx"))
logTime("save WGS")
}
// Tier1 excel
tagStr := ""
if *tag != "" {
tagStr = textUtil.File2Array(*tag)[0]
}
var tier1Output = *prefix + ".Tier1" + tagStr + ".xlsx"
simpleUtil.CheckErr(tier1Xlsx.Save(tier1Output))
logTime("save Tier1")
if *snv != "" {
// Tier2 excel
simpleUtil.CheckErr(tier2.Save(), "Tier2 save fail")
logTime("save Tier2")
// Tier3 excel
if outputTier3 {
simpleUtil.CheckErr(tier3SW.Flush())
simpleUtil.CheckErr(tier3Xlsx.SaveAs(*prefix + ".Tier3.xlsx"))
logTime("save Tier3")
}
}
}
logTime0("total work")
}
func SteamWriterSetString2Row(sw *excelize.StreamWriter, col, row int, rows []string) {
var values = make([]interface{}, len(rows))
for i, s := range rows {
values[i] = s
}
var axis = simpleUtil.HandleError(excelize.CoordinatesToCellName(col, row)).(string)
simpleUtil.CheckErr(sw.SetRow(axis, values))
}
func SteamWriterSetStringMap2Row(sw *excelize.StreamWriter, col, row int, item map[string]string, keys []string) {
var values = make([]interface{}, len(keys))
for i, s := range keys {
values[i] = item[s]
}
var axis = simpleUtil.HandleError(excelize.CoordinatesToCellName(col, row)).(string)
simpleUtil.CheckErr(sw.SetRow(axis, values))
}