Debian Codegraph @

sam/cigar.go

raw symbols SCIP
1 // Copyright ©2012 The bíogo Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package samscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/
6
7 import (
8 "bytesscip-go gomod github.com/golang/go/src go1.19 bytes/"
9 "fmtscip-go gomod github.com/golang/go/src go1.19 fmt/"
10 )
11
12 // Cigar is a set of CIGAR operations.
13 type Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar# []CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#
14
15 // IsValid returns whether the CIGAR string is valid for a record of the given
16 // sequence length. Validity is defined by the sum of query consuming operations
17 // matching the given length, clipping operations only being present at the ends
18 // of alignments, and that CigarBack operations only result in query-consuming
19 // positions at or right of the start of the alignment.
20 func (c Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#) IsValidscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#IsValid().(length int) bool {
21 var pos int
22 for i, co := range c {
23 ct := co.Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().()
24 if ct == CigarHardClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarHardClipped. && i != 0 && i != len(c)-1 {
25 return false
26 }
27 if ct == CigarSoftClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarSoftClipped. && i != 0 && i != len(c)-1 {
28 if c[i-1].Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().() != CigarHardClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarHardClipped. && c[i+1].Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().() != CigarHardClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarHardClipped. {
29 return false
30 }
31 }
32 con := ct.Consumesscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#Consumes().()
33 if pos < 0 && con.Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query. != 0 {
34 return false
35 }
36 length -= co.Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().() * con.Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.
37 pos += co.Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().() * con.Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.
38 }
39 return length == 0
40 }
41
42 // String returns the CIGAR string for c.
43 func (c Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#) Stringscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#String().() string {
44 if len(c) == 0 {
45 return "*"
46 }
47 var b bytesscip-go gomod github.com/golang/go/src go1.19 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.19 bytes/Buffer#
48 for _, co := range c {
49 fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.19 fmt/Fprint().(&b, co)
50 }
51 return b.Stringscip-go gomod github.com/golang/go/src go1.19 bytes/Buffer#String().()
52 }
53
54 // Lengths returns the number of reference and read bases described by the Cigar.
55 func (c Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#) Lengthsscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#Lengths().() (ref, read int) {
56 var con Consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#
57 for _, co := range c {
58 con = co.Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().().Consumesscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#Consumes().()
59 if co.Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().() != CigarBackscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarBack. {
60 ref += co.Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().() * con.Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.
61 }
62 read += co.Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().() * con.Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.
63 }
64 return ref, read
65 }
66
67 // CigarOp is a single CIGAR operation including the operation type and the
68 // length of the operation.
69 type CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp# uint32
70
71 // NewCigarOp returns a CIGAR operation of the specified type with length n.
72 // Due to a limitation of the BAM format, CIGAR operation lengths are limited
73 // to 2^28-1, and NewCigarOp will panic if n is above this or negative.
74 func NewCigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/NewCigarOp().(t CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#, n int) CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp# {
75 if uint64(n) > 1<<28-1 {
76 panic("sam: illegal CIGAR op length")
77 }
78 return CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#(t) | (CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#(n) << 4)
79 }
80
81 // Type returns the type of the CIGAR operation for the CigarOp.
82 func (co CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#) Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().() CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType# { return CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#(co & 0xf) }
83
84 // Len returns the number of positions affected by the CigarOp CIGAR operation.
85 func (co CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#) Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().() int { return int(co >> 4) }
86
87 // String returns the string representation of the CigarOp.
88 func (co CigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#) Stringscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#String().() string { return fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.19 fmt/Sprintf().("%d%s", co.Lenscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Len().(), co.Typescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOp#Type().().Stringscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#String().()) }
89
90 // A CigarOpType represents the type of operation described by a CigarOp.
91 type CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType# byte
92
93 const (
94 CigarMatchscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarMatch. CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType# = iota // Alignment match (can be a sequence match or mismatch).
95 CigarInsertionscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarInsertion. // Insertion to the reference.
96 CigarDeletionscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarDeletion. // Deletion from the reference.
97 CigarSkippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarSkipped. // Skipped region from the reference.
98 CigarSoftClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarSoftClipped. // Soft clipping (clipped sequences present in SEQ).
99 CigarHardClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarHardClipped. // Hard clipping (clipped sequences NOT present in SEQ).
100 CigarPaddedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarPadded. // Padding (silent deletion from padded reference).
101 CigarEqualscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarEqual. // Sequence match.
102 CigarMismatchscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarMismatch. // Sequence mismatch.
103 CigarBackscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarBack. // Skip backwards.
104 lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar.
105 )
106
107 var cigarOpsscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOps. = []string{"M", "I", "D", "N", "S", "H", "P", "=", "X", "B", "?"}
108
109 // Consumes returns the CIGAR operation alignment consumption characteristics for the CigarOpType.
110 //
111 // The Consume values for each of the CigarOpTypes is as follows:
112 //
113 // Query Reference
114 // CigarMatch 1 1
115 // CigarInsertion 1 0
116 // CigarDeletion 0 1
117 // CigarSkipped 0 1
118 // CigarSoftClipped 1 0
119 // CigarHardClipped 0 0
120 // CigarPadded 0 0
121 // CigarEqual 1 1
122 // CigarMismatch 1 1
123 // CigarBack 0 -1
124 func (ct CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#) Consumesscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#Consumes().() Consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume# { return consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/consume.[ct] }
125
126 // String returns the string representation of a CigarOpType.
127 func (ct CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#) Stringscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#String().() string {
128 if ct < 0 || ct > lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar. {
129 ct = lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar.
130 }
131 return cigarOpsscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOps.[ct]
132 }
133
134 // Consume describes how CIGAR operations consume alignment bases.
135 type Consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume# struct {
136 Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query., Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference. int
137 }
138
139 // A few years ago, Complete Genomics (CG) proposed to add a new CIGAR
140 // operator 'B' for an operation of moving backward along the reference
141 // genome. It is the opposite of the 'N', the reference skip. In a later
142 // discussion on a separate issue, Fred expressed his preference to a
143 // negative reference skip which is equivalent to a positive 'B' operation.
144 // Now the SRA group from NCBI intends to archive the CG alignment in
145 // the SAM format and raises this request again. I think it may be the
146 // time to add this backward operation.
147 //
148 // The backward operation is designed to describe such an alignment:
149 //
150 // REF:: GCATACGATCGACTAGTCACGT
151 // READ: --ATACGATCGA----------
152 // READ: ---------CGACTAGTCAC--
153 //
154 // i.e. there is an overlap between two segments of a read, which is quite
155 // frequent in CG data. We are unable to fully describe such an alignment
156 // with the original CIGAR. In the current spec, we suggest using a CIGAR
157 // 18M and storing the overlap information in optional tags. This is a
158 // little clumsy and is not compressed well for the purpose of archiving.
159 // With 'B', the new CIGAR is "10M3B11M" with no other optional tags.
160 //
161 // Using "B" in this case is cleaner, but the major concern is that it breaks
162 // the compatibility and is also likely to complicate SNP calling and many
163 // other applications. As I think now, the solution is to implement a
164 // "remove_B()" routine in samtools. This routine collapses overlapping
165 // sequences, recalculates base quality in the overlap and gives a CIGAR
166 // without 'B'. For the example above, remove_B() gives CIGAR 18M. For SNP
167 // calling, we may call remove_B() immediately after the alignment loaded
168 // into memory. The downstream pileup engine does not need any changes. Other
169 // SNP callers can do the same. A new option will be added to "samtools view"
170 // as a way to remove 'B' operations on the command-line.
171 //
172 // The implementation of remove_B() may be quite complicated in the generic
173 // case - we may be dealing with a multiple-sequence alignment, but it should
174 // be straightforward in the simple cases such as the example above. Users may
175 // not need to care too much about how remove_B() is implemented.
176 //
177 // http://sourceforge.net/p/samtools/mailman/message/28463294/
178 var consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/consume. = []Consumescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#{
179 CigarMatchscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarMatch.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 1, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 1},
180 CigarInsertionscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarInsertion.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 1, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 0},
181 CigarDeletionscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarDeletion.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 0, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 1},
182 CigarSkippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarSkipped.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 0, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 1},
183 CigarSoftClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarSoftClipped.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 1, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 0},
184 CigarHardClippedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarHardClipped.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 0, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 0},
185 CigarPaddedscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarPadded.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 0, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 0},
186 CigarEqualscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarEqual.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 1, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 1},
187 CigarMismatchscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarMismatch.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 1, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: 1},
188 CigarBackscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarBack.: {Queryscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Query.: 0, Referencescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Consume#Reference.: -1}, // See notes above.
189 lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar.: {},
190 }
191
192 var cigarOpTypeLookupscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOpTypeLookup. [256]CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#
193
194 func initscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/init().() {
195 for i := range cigarOpTypeLookupscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOpTypeLookup. {
196 cigarOpTypeLookupscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOpTypeLookup.[i] = lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar.
197 }
198 for op, c := range []byte{'M', 'I', 'D', 'N', 'S', 'H', 'P', '=', 'X', 'B'} {
199 cigarOpTypeLookupscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOpTypeLookup.[c] = CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#(op)
200 }
201 }
202
203 var powersscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/powers. = []int64{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12}
204
205 // atoi returns the integer interpretation of b which must be an ASCII decimal number representation.
206 func atoiscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/atoi().(b []byte) (int, error) {
207 if len(b) > len(powersscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/powers.) {
208 return 0, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("sam: integer overflow: %q", b)
209 }
210 var n int64
211 k := len(b) - 1
212 for i, v := range b {
213 n += int64(v-'0') * powersscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/powers.[k-i]
214 if int64(int(n)) != n {
215 return 0, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("sam: integer overflow: %q at %d", b, i)
216 }
217 }
218 return int(n), nil
219 }
220
221 // ParseCigar returns a Cigar parsed from the provided byte slice.
222 // ParseCigar will break CIGAR operations longer than 2^28-1 into
223 // multiple operations summing to the same length.
224 func ParseCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/ParseCigar().(b []byte) (Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#, error) {
225 if len(b) == 1 && b[0] == '*' {
226 return nil, nil
227 }
228 var (
229 c Cigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/Cigar#
230 op CigarOpTypescip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/CigarOpType#
231 n int
232 err error
233 )
234 for i := 0; i < len(b); i++ {
235 for j := i; j < len(b); j++ {
236 if b[j] < '0' || '9' < b[j] {
237 n, err = atoiscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/atoi().(b[i:j])
238 if err != nil {
239 return nil, err
240 }
241 op = cigarOpTypeLookupscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/cigarOpTypeLookup.[b[j]]
242 i = j
243 break
244 }
245 }
246 if op == lastCigarscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/lastCigar. {
247 return nil, fmtscip-go gomod github.com/golang/go/src go1.19 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.19 fmt/Errorf().("sam: failed to parse cigar string %q: unknown operation %q", b, op)
248 }
249
250 for {
251 c = append(c, NewCigarOpscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/NewCigarOp().(op, minIntscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/minInt().(n, 1<<28-1)))
252 n -= 1<<28 - 1
253 if n <= 0 {
254 break
255 }
256 }
257 }
258 return c, nil
259 }
260
261 func minIntscip-go gomod github.com/biogo/hts . `github.com/biogo/hts/sam`/minInt().(a, b int) int {
262 if a < b {
263 return a
264 }
265 return b
266 }