-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosdepth.go
114 lines (94 loc) · 2.29 KB
/
mosdepth.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
package main
import (
"compress/gzip"
_ "embed"
"encoding/csv"
"io"
"log"
"os"
"strconv"
)
type TargetCoverage struct {
chr string
start int
end int
x5 int
x10 int
x20 int
x30 int
}
func parseMosdepthBed(coverageBed string) (map[string][]TargetCoverage, int, int, int, int, int) {
coverageFile, err := os.Open(coverageBed)
if err != nil {
log.Fatal("unable to open coverage file")
}
defer coverageFile.Close()
coverageRdr, err := gzip.NewReader(coverageFile)
if err != nil {
log.Fatal(err)
}
// read gencode file as TSV
tsvReader := csv.NewReader(coverageRdr)
tsvReader.Comma = '\t'
tsvReader.Comment = '#'
targets := make(map[string][]TargetCoverage)
var globalTotalBases, globalCoveredBases5x, globalCoveredBases10x, globalCoveredBases20x, globalCoveredBases30x int
record, err := tsvReader.Read()
for err == nil {
if len(record) == 0 {
break
}
start, err := strconv.Atoi(record[1])
if err != nil {
log.Fatalf("error parsing start, %v", err)
}
end, err := strconv.Atoi(record[2])
if err != nil {
log.Fatalf("error parsing end, %v", err)
}
x5, err := strconv.Atoi(record[4])
if err != nil {
log.Fatalf("error parsing x5, %v", err)
}
x10, err := strconv.Atoi(record[5])
if err != nil {
log.Fatalf("error parsing x10, %v", err)
}
x20, err := strconv.Atoi(record[6])
if err != nil {
log.Fatalf("error parsing x20, %v", err)
}
x30, err := strconv.Atoi(record[7])
if err != nil {
log.Fatalf("error parsing x30, %v", err)
}
if err != nil {
log.Fatal("unable to parse bed file", err, record)
}
globalTotalBases += end - start
globalCoveredBases5x += x5
globalCoveredBases10x += x10
globalCoveredBases20x += x20
globalCoveredBases30x += x30
bed := TargetCoverage{
chr: record[0],
start: start,
end: end,
x5: x5,
x10: x10,
x20: x20,
x30: x30,
}
if beds, exists := targets[bed.chr]; exists {
beds = append(beds, bed)
targets[bed.chr] = beds
} else {
targets[bed.chr] = []TargetCoverage{bed}
}
record, err = tsvReader.Read()
}
if err != nil && err != io.EOF {
log.Fatal("error parsing coverage bed file", err, record)
}
return targets, globalTotalBases, globalCoveredBases5x, globalCoveredBases10x, globalCoveredBases20x, globalCoveredBases30x
}