forked from BioinfoUNIBA/REDItools2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslow.go
134 lines (105 loc) · 2.63 KB
/
slow.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
package main
import (
"os"
"strings"
"sync"
"github.com/biogo/hts/bam"
"github.com/golang-collections/collections/set"
)
func worker(wg *sync.WaitGroup, refs chan *Region, w chan string, omopolymericPositions, splicePositions, targetPositions map[string]*set.Set) {
defer wg.Done()
for {
ref, ok := <-refs
if !ok {
break
}
chrRef, err := fetchFasta(ref)
if err != nil {
sugar.Warnf("try to modify %s", ref.Chrom)
if strings.HasPrefix(ref.Chrom, "chr") {
chrRef, err = fetchFasta(&Region{Chrom: strings.ReplaceAll(ref.Chrom, "chr", "")})
} else {
chrRef, err = fetchFasta(&Region{Chrom: "chr" + ref.Chrom})
}
}
if err != nil {
sugar.Fatal(err)
}
chunks, err := fetchBam(ref)
if err != nil {
sugar.Fatal(err)
}
ifh, err := os.Open(conf.File)
//Panic if something went wrong:
if err != nil {
sugar.Fatal(err)
}
defer ifh.Close()
//Create a new BAM reader with maximum
//concurrency:
bamReader, err := bam.NewReader(ifh, 1)
if err != nil {
sugar.Fatal(err)
}
defer bamReader.Close()
iter, err := bam.NewIterator(bamReader, chunks)
if err != nil {
sugar.Fatal(err)
}
defer iter.Close()
lastEnd := 0
total := 0
edits := make(map[int]*EditsInfo)
for iter.Next() {
rec := iter.Record()
record := NewRecord(rec)
if err := iter.Error(); err != nil {
sugar.Fatal(err)
}
if lastEnd == 0 {
lastEnd = record.End
}
total++
if !filterReads(record) {
continue
}
if record.Start > lastEnd {
// if _, ok := edits[139102424]; ok {
// sugar.Infof("%d %s", lastEnd, record)
// }
getColumn(edits, []map[string]*set.Set{omopolymericPositions, splicePositions}, targetPositions, w)
edits = make(map[int]*EditsInfo)
}
edits = updateEdits(edits, record, chrRef, ref.Chrom)
// if record.End > 139102424 && record.Start < 139102424 {
// sugar.Info(record)
// }
if record.End > lastEnd {
lastEnd = record.End
}
}
getColumn(edits, []map[string]*set.Set{omopolymericPositions, splicePositions}, targetPositions, w)
sugar.Debugf("read %d reads from %s", total, ref)
}
w <- "done"
}
func slowMode(wg *sync.WaitGroup, w chan string,
omopolymericPositions, spicePositions, targetPositions map[string]*set.Set) {
wg.Add(1)
go writer(w, wg)
refs := make(chan *Region)
for i := 0; i < conf.Process; i++ {
go worker(wg, refs, w, omopolymericPositions, spicePositions, targetPositions)
wg.Add(1)
}
if region.Empty() {
if references, err := fetchBamRefs(); err == nil {
for _, r := range references {
refs <- &Region{Chrom: r}
}
}
} else {
refs <- region
}
close(refs)
}