-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess.go
153 lines (132 loc) · 3.34 KB
/
process.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
// Package bamstats provides functions for computing several mapping statistics on a BAM file.
package bamstats
import (
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/biogo/hts/bam"
"github.com/guigolab/bamstats/annotation"
"github.com/guigolab/bamstats/config"
"github.com/guigolab/bamstats/sam"
"github.com/guigolab/bamstats/stats"
)
func init() {
log.SetLevel(log.WarnLevel)
}
// var wg sync.WaitGroup
func worker(id int, in interface{}, out chan stats.Map, index *annotation.RtreeMap, cfg *config.Config, wg *sync.WaitGroup) {
defer wg.Done()
logger := log.WithFields(log.Fields{
"worker": id,
})
logger.Debug("Starting")
sm := makeStatsMap(index, cfg)
collectStats(in, sm)
logger.Debug("Done")
out <- sm
}
func collectStats(in interface{}, sm stats.Map) {
switch in.(type) {
case chan *sam.Record:
c := in.(chan *sam.Record)
for record := range c {
for _, s := range sm {
s.Collect(record)
}
}
case chan *sam.Iterator:
iterators := in.(chan *sam.Iterator)
for it := range iterators {
for it.Next() {
for _, s := range sm {
s.Collect(it.Record())
}
}
}
}
}
func process(bamFile string, index *annotation.RtreeMap, cpu int, maxBuf int, reads int, uniq bool) (stats.Map, error) {
var wg sync.WaitGroup
conf := config.NewConfig(cpu, maxBuf, reads, uniq)
br, err := sam.NewReader(bamFile, conf)
defer br.Close()
if err != nil {
return nil, err
}
statChan := make(chan stats.Map, cpu)
for i := 0; i < br.Workers; i++ {
id := i + 1
wg.Add(1)
go worker(id, br.Channels[i], statChan, index, conf, &wg)
}
go br.Read()
go waitProcess(statChan, &wg)
stat := <-statChan
stat.Merge(statChan)
for k, v := range stat {
switch k {
case "general":
s := v.(*stats.GeneralStats)
s.Reads.Total += br.Unmapped()
if s.Pairs.Total > 0 {
s.Pairs.Total += br.Unmapped() / 2
}
case "rnaseq":
s := v.(*stats.RNAseqStats)
s.UpdateTotal(br.Unmapped())
}
v.Finalize()
}
return stat, nil
}
func waitProcess(st chan stats.Map, wg *sync.WaitGroup) {
wg.Wait()
close(st)
}
func getChrLens(bamFile string, cpu int) (chrs map[string]int) {
bf, err := os.Open(bamFile)
if err != nil {
return nil
}
br, err := bam.NewReader(bf, cpu)
if err != nil {
return nil
}
refs := br.Header().Refs()
chrs = make(map[string]int, len(refs))
for _, r := range refs {
chrs[r.Name()] = r.Len()
}
return
}
// Process process the input BAM file and collect different mapping stats.
func Process(bamFile string, anno string, cpu int, maxBuf int, reads int, uniq bool) (stats.Map, error) {
var index *annotation.RtreeMap
if anno != "" {
log.Infof("Creating index for %s", anno)
start := time.Now()
chrLens := getChrLens(bamFile, cpu)
index = annotation.CreateIndex(anno, chrLens)
log.Infof("Index done in %v", time.Since(start))
}
start := time.Now()
log.Infof("Collecting stats for %s", bamFile)
allStats, err := process(bamFile, index, cpu, maxBuf, reads, uniq)
if err != nil {
return nil, err
}
log.Infof("Stats done in %v", time.Since(start))
return allStats, nil
}
func makeStatsMap(index *annotation.RtreeMap, cfg *config.Config) stats.Map {
m := stats.NewMap(stats.NewGeneralStats())
if index != nil {
m.Add(stats.NewCoverageStats(index, false))
if cfg.Uniq {
m.Add(stats.NewCoverageStats(index, true))
}
m.Add(stats.NewIHECstats(index))
}
return m
}