forked from BioinfoUNIBA/REDItools2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
191 lines (153 loc) · 3.52 KB
/
utils.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
package main
import (
"sort"
"strings"
"github.com/biogo/hts/sam"
"github.com/golang-collections/collections/set"
)
func maxInt(i, j int) int {
if i > j {
return i
}
return j
}
func withinInterval(i int) bool {
if region.Empty() {
return true
}
return region.Start <= i && i <= region.End
}
func prop(tot, va int) float64 {
if tot == 0 {
return 0.0
}
return float64(va) / float64(tot)
}
func vStrand(strands string) string {
data := map[rune]int{'+': 0, '-': 0, '*': 0}
for _, j := range strands {
data[j]++
}
if data['+'] == data['-'] {
return "*"
}
if conf.StrandConfidence != 0 {
if prop(data['+']+data['-'], data['+']) >= conf.StrandConfidenceValue {
return "+"
} else if prop(data['+']+data['-'], data['-']) >= conf.StrandConfidenceValue {
return "-"
} else {
return "*"
}
} else {
if data['+'] == data['-'] && data['*'] == 0 {
return "+"
}
}
res, num := "", 0
for i, j := range data {
if j > num {
num = j
res = string(i)
}
}
return res
}
func complementAll(sequence []byte) []byte {
data := map[byte]byte{'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
res := make([]byte, 0, 0)
for _, i := range sequence {
res = append(res, data[i])
}
return res
}
func normByStrand(seqParam []byte, strandParam string, sequalParam []byte, inferStrand string) ([]byte, string, []byte) {
strands := []byte(strandParam)
seq, strand, qual := make([]byte, 0, 0), make([]string, 0, 0), make([]byte, 0, 0)
if len(strands) != len(seqParam) {
sugar.Fatalf("strandParam: %v; seqParam: %v", strands, seqParam)
}
for i, j := range seqParam {
if string(strands[i]) == inferStrand {
seq = append(seq, j)
strand = append(strand, string(strands[i]))
qual = append(qual, sequalParam[i])
}
}
return seq, vStrand(strings.Join(strand, "")), qual
}
func frequency(data []string) map[string]int {
res := make(map[string]int)
for _, j := range data {
if temp, ok := res[j]; ok {
temp++
res[j] = temp
} else {
res[j] = 1
}
}
return res
}
func getColumn(edits map[int]*EditsInfo, positions []map[string]*set.Set, targetPositions map[string]*set.Set, w chan string) {
timer := InitTimer()
timer.Tic()
keys := make([]int, 0, 0)
for key := range edits {
keys = append(keys, key)
}
sort.Ints(keys)
for _, key := range keys {
edit, ok := edits[key]
if !ok {
continue
}
for _, pos := range positions {
if temp, ok := pos[edit.LastChr]; ok {
if temp.Has(edit.Pos) {
continue
}
}
}
if temp, ok := targetPositions[edit.LastChr]; ok {
if !temp.Has(edit.Pos) {
continue
}
}
//w <- edit.String()
if edit.Valid() {
w <- edit.String()
}
}
}
func updateEdits(edits map[int]*EditsInfo, record *Record, chrRef []byte, ref string) map[int]*EditsInfo {
// INDEX keep the position of record base position
// START keep the genomic position
start, index := record.Start, 0
for _, i := range record.Cigar {
if conf.MaxBasePosition > 0 && record.QueryLength-index < conf.MaxBasePosition {
break
}
if i.Type() != sam.CigarDeletion &&
i.Type() != sam.CigarHardClipped &&
i.Type() != sam.CigarInsertion {
if i.Type() == sam.CigarMatch {
for j := 0; j < i.Len(); j++ {
if _, ok := edits[start]; !ok {
edits[start] = NewEditsInfo(ref, chrRef[start], start+1)
}
edits[start].AddReads(record, index)
index++
start++
}
} else {
if i.Type() != sam.CigarSoftClipped {
start += i.Len()
}
if i.Type() != sam.CigarSkipped {
index += i.Len()
}
}
}
}
return edits
}