-
Notifications
You must be signed in to change notification settings - Fork 0
/
wfa_cigar.go
360 lines (316 loc) · 7.9 KB
/
wfa_cigar.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright © 2024 Wei Shen <shenwei356@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package wfa
import (
"bytes"
"strconv"
"sync"
)
// AlignmentResult represent a AlignmentResult structure.
type AlignmentResult struct {
// Ops []*CIGARRecord
Ops []uint64 // 24-bit null + 8 bit Op + 32-bit N
Score uint32 // Alignment score
TBegin, TEnd int // 1-based location of the alignment in target seq, no including flanking clipping/insertion sequences
QBegin, QEnd int // 1-based location of the alignment in query seq, no including flanking clipping/insertion sequences
// Stats of the aligned region, no including flanking clipping/insertion sequences
AlignLen uint32
Matches uint32
Gaps uint32
GapRegions uint32
proccessed bool
globalAlignment bool
}
// // CIGARRecord records the operation and the number.
// type CIGARRecord struct {
// N uint32
// Op byte
// }
// Op extracts operation type and count.
func Op(op uint64) (byte, uint32) {
return byte(op >> 32), uint32(op & MaskLower32)
}
const OpM = uint64('M')
const OpD = uint64('D')
const OpI = uint64('I')
const OpX = uint64('X')
const OpH = uint64('H')
const MaskLower32 = 4294967295
// NewAlignmentResult returns a new CIGAR from the object pool.
func NewAlignmentResult(globalAlignment bool) *AlignmentResult {
cigar := poolCIGAR.Get().(*AlignmentResult)
cigar.reset()
cigar.globalAlignment = globalAlignment
return cigar
}
// reset resets a CIGAR.
func (cigar *AlignmentResult) reset() {
// for _, r := range cigar.Ops {
// poolCIGARRecord.Put(r)
// }
cigar.Ops = cigar.Ops[:0]
cigar.Score = 0
cigar.proccessed = false
cigar.AlignLen = 0
cigar.Matches = 0
cigar.Gaps = 0
cigar.GapRegions = 0
}
// RecycleAlignmentResult recycles a CIGAR object.
func RecycleAlignmentResult(cigar *AlignmentResult) {
if cigar != nil {
poolCIGAR.Put(cigar)
}
}
// object pool of a CIGAR.
var poolCIGAR = &sync.Pool{New: func() interface{} {
cigar := AlignmentResult{
// Ops: make([]*CIGARRecord, 0, 128),
Ops: make([]uint64, 0, 1024),
}
return &cigar
}}
// // object pool of CIGARRecord.
// var poolCIGARRecord = &sync.Pool{New: func() interface{} {
// return &CIGARRecord{}
// }}
// Add adds a new record in backtrace.
func (cigar *AlignmentResult) Add(op byte) {
cigar.AddN(op, 1)
}
// Add adds a new record in backtrace and set its number as n.
func (cigar *AlignmentResult) AddN(op byte, n uint32) {
// r := poolCIGARRecord.Get().(*CIGARRecord)
// r.Op = op
// r.N = n
// cigar.Ops = append(cigar.Ops, r)
cigar.Ops = append(cigar.Ops, uint64(op)<<32|uint64(n))
}
// Update updates the last record.
func (cigar *AlignmentResult) Update(n uint32) {
l := len(cigar.Ops)
if l > 0 {
// cigar.Ops[l-1].N += n
cigar.Ops[l-1] += uint64(n)
}
}
// process processes the data.
func (cigar *AlignmentResult) process() {
if cigar.proccessed {
return
}
s := &cigar.Ops
// reverse the order of all operations.
var i, j int
for i, j = 0, len(*s)-1; i < j; i, j = i+1, j-1 {
(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
}
// merge operations of the same type.
// var opPre, op *CIGARRecord
var opPre, op uint64
i, j = 0, 0
opPre = (*s)[0]
for i = 1; i < len(*s); i++ {
op = (*s)[i]
if op>>32 == opPre>>32 {
opPre += op & MaskLower32 // update count
(*s)[j] = opPre
continue
}
j++
if i != j {
(*s)[j] = (*s)[i]
}
opPre = op
}
*s = (*s)[:j+1]
// count matches, gaps
var begin, end int
for i, op = range *s {
// if op.Op == 'M' {
if op>>32 == OpM {
begin = i
break
}
}
for i = len(*s) - 1; i >= 0; i-- {
op = (*s)[i]
// if op.Op == 'M' {
if op>>32 == OpM {
end = i
break
}
}
var alen uint32
var matches uint32
var gaps uint32
var gapRegions uint32
for i = begin; i <= end; i++ {
op = (*s)[i]
// alen += op.N
alen += uint32(op & MaskLower32)
// switch op.Op {
switch op >> 32 {
// case 'M':
case OpM:
matches += uint32(op & MaskLower32)
// case 'I', 'D':
case OpI, OpD:
gaps += uint32(op & MaskLower32)
gapRegions++
}
}
cigar.AlignLen = alen
cigar.Matches = matches
cigar.Gaps = gaps
cigar.GapRegions = gapRegions
cigar.proccessed = true
}
// trimOps trim ops to keep only aligned region
func trimOps(ops []uint64) []uint64 {
var start, end int
start, end = -1, -1
for i, op := range ops {
if op>>32 == OpM {
start = i
break
}
}
for i := len(ops) - 1; i >= 0; i-- {
if ops[i]>>32 == OpM {
end = i
break
}
}
return ops[start : end+1]
}
// CIGAR returns the CIGAR string.
func (cigar *AlignmentResult) CIGAR(onlyAignedRegion bool) string {
cigar.process()
buf := poolBytesBuffer.Get().(*bytes.Buffer)
buf.Reset()
ops := cigar.Ops
if onlyAignedRegion {
ops = trimOps(cigar.Ops)
}
for _, op := range ops {
// buf.WriteString(strconv.Itoa(int(op.N)))
buf.WriteString(strconv.Itoa(int(op & MaskLower32)))
// buf.WriteByte(op.Op)
buf.WriteByte(byte(op >> 32))
}
text := buf.String()
poolBytesBuffer.Put(buf)
return text
}
// AlignmentText returns the formated alignment text for Query, Alignment, and Target.
// Do not forget to recycle them with RecycleAlignmentText().
func (cigar *AlignmentResult) AlignmentText(q0, t0 *[]byte, onlyAignedRegion bool) (*[]byte, *[]byte, *[]byte) {
cigar.process()
var q, t []byte
ops := cigar.Ops
if !onlyAignedRegion {
q = *q0
t = *t0
} else {
q = (*q0)[cigar.QBegin-1 : cigar.QEnd]
t = (*t0)[cigar.TBegin-1 : cigar.TEnd]
ops = trimOps(cigar.Ops)
}
Q := poolBytes.Get().(*[]byte)
A := poolBytes.Get().(*[]byte)
T := poolBytes.Get().(*[]byte)
// var n int
var h, v int
// lenQ := len(*q)
// lenT := len(*t)
v, h = 0, 0
// var i uint32
var i, n uint64
for _, op := range ops {
n = op & MaskLower32
// switch op.Op {
switch op >> 32 {
// case 'M':
case OpM:
// for i = 0; i < op.N; i++ {
for i = 0; i < n; i++ {
*Q = append(*Q, q[v])
*A = append(*A, '|')
*T = append(*T, t[h])
v++
h++
}
// case 'X':
case OpX:
// for i = 0; i < op.N; i++ {
for i = 0; i < n; i++ {
*Q = append(*Q, q[v])
*A = append(*A, ' ')
*T = append(*T, t[h])
v++
h++
}
// case 'I':
case OpI:
// for i = 0; i < op.N; i++ {
for i = 0; i < n; i++ {
*Q = append(*Q, '-')
*A = append(*A, ' ')
*T = append(*T, t[h])
h++
}
// case 'D', 'H':
case OpD, OpH:
// for i = 0; i < op.N; i++ {
for i = 0; i < n; i++ {
*Q = append(*Q, q[v])
*A = append(*A, ' ')
*T = append(*T, '-')
v++
}
}
}
return Q, A, T
}
// object pool of aligners.
var poolBytesBuffer = &sync.Pool{New: func() interface{} {
buf := make([]byte, 1024)
return bytes.NewBuffer(buf)
}}
var poolBytes = &sync.Pool{New: func() interface{} {
buf := make([]byte, 0, 1024)
return &buf
}}
// RecycleAlignmentText recycle alignment text.
func RecycleAlignmentText(Q, A, T *[]byte) {
if Q != nil {
*Q = (*Q)[:0]
poolBytes.Put(Q)
}
if A != nil {
*A = (*A)[:0]
poolBytes.Put(A)
}
if T != nil {
*T = (*T)[:0]
poolBytes.Put(T)
}
}