| 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 sam |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "encoding/binary" |
| 10 | "encoding/hex" |
| 11 | "fmt" |
| 12 | "math" |
| 13 | "reflect" |
| 14 | "strconv" |
| 15 | "unsafe" |
| 16 | ) |
| 17 | |
| 18 | // ASCII is a printable ASCII character included in an Aux tag. |
| 19 | type ASCII byte |
| 20 | |
| 21 | // Hex is a byte slice represented as a hex string in an Aux tag. |
| 22 | type Hex []byte |
| 23 | |
| 24 | // Text is a byte slice represented as a string in an Aux tag. |
| 25 | type Text []byte |
| 26 | |
| 27 | // An Aux represents an auxiliary data field from a SAM alignment record. |
| 28 | type Aux []byte |
| 29 | |
| 30 | // NewAux returns a new Aux with the given tag, type and value. Acceptable value |
| 31 | // types and their corresponding SAM type are: |
| 32 | // |
| 33 | // A - ASCII |
| 34 | // c - int8 |
| 35 | // C - uint8 |
| 36 | // s - int16 |
| 37 | // S - uint16 |
| 38 | // i - int, uint or int32 |
| 39 | // I - int, uint or uint32 |
| 40 | // f - float32 |
| 41 | // Z - Text or string |
| 42 | // H - Hex |
| 43 | // B - []int8, []int16, []int32, []uint8, []uint16, []uint32 or []float32 |
| 44 | // |
| 45 | // The handling of int and uint types is provided as a convenience - values must |
| 46 | // fit within either int32 or uint32 and are converted to the smallest possible |
| 47 | // representation. |
| 48 | func NewAux(t Tag, value interface{}) (Aux, error) { |
| 49 | var a Aux |
| 50 | switch v := value.(type) { |
| 51 | case ASCII: |
| 52 | a = Aux{t[0], t[1], 'A', byte(v)} |
| 53 | case int: |
| 54 | switch { |
| 55 | case math.MinInt8 <= v && v <= math.MaxInt8: |
| 56 | a = Aux{t[0], t[1], 'c', byte(v)} |
| 57 | case math.MinInt16 <= v && v <= math.MaxInt16: |
| 58 | a = Aux{t[0], t[1], 's', 0, 0} |
| 59 | binary.LittleEndian.PutUint16(a[3:5], uint16(v)) |
| 60 | case math.MinInt32 <= v && v <= math.MaxInt32: |
| 61 | a = Aux{t[0], t[1], 'i', 0, 0, 0, 0} |
| 62 | binary.LittleEndian.PutUint32(a[3:7], uint32(v)) |
| 63 | default: |
| 64 | return nil, fmt.Errorf("sam: integer value out of range %d > %d", v, math.MaxInt32) |
| 65 | } |
| 66 | case uint: |
| 67 | switch { |
| 68 | case v <= math.MaxUint8: |
| 69 | a = Aux{t[0], t[1], 'C', byte(v)} |
| 70 | case v <= math.MaxUint16: |
| 71 | a = Aux{t[0], t[1], 'S', 0, 0} |
| 72 | binary.LittleEndian.PutUint16(a[3:5], uint16(v)) |
| 73 | case v <= math.MaxUint32: |
| 74 | a = Aux{t[0], t[1], 'I', 0, 0, 0, 0} |
| 75 | binary.LittleEndian.PutUint32(a[3:7], uint32(v)) |
| 76 | default: |
| 77 | return nil, fmt.Errorf("sam: unsigned integer value out of range %d > %d", v, uint(math.MaxUint32)) |
| 78 | } |
| 79 | case int8: |
| 80 | a = Aux{t[0], t[1], 'c', byte(v)} |
| 81 | case uint8: |
| 82 | a = Aux{t[0], t[1], 'C', v} |
| 83 | case int16: |
| 84 | a = Aux{t[0], t[1], 's', 0, 0} |
| 85 | binary.LittleEndian.PutUint16(a[3:5], uint16(v)) |
| 86 | case uint16: |
| 87 | a = Aux{t[0], t[1], 'S', 0, 0} |
| 88 | binary.LittleEndian.PutUint16(a[3:5], v) |
| 89 | case int32: |
| 90 | a = Aux{t[0], t[1], 'i', 0, 0, 0, 0} |
| 91 | binary.LittleEndian.PutUint32(a[3:7], uint32(v)) |
| 92 | case uint32: |
| 93 | a = Aux{t[0], t[1], 'I', 0, 0, 0, 0} |
| 94 | binary.LittleEndian.PutUint32(a[3:7], v) |
| 95 | case float32: |
| 96 | a = Aux{t[0], t[1], 'f', 0, 0, 0, 0} |
| 97 | binary.LittleEndian.PutUint32(a[3:7], math.Float32bits(v)) |
| 98 | case Text: |
| 99 | a = make(Aux, len(v)+3) |
| 100 | a[0], a[1], a[2] = t[0], t[1], 'Z' |
| 101 | copy(a[3:], v) |
| 102 | case string: |
| 103 | a = make(Aux, len(v)+3) |
| 104 | a[0], a[1], a[2] = t[0], t[1], 'Z' |
| 105 | copy(a[3:], v) |
| 106 | case Hex: |
| 107 | a = make(Aux, 3, len(v)+3) |
| 108 | copy(a, Aux{t[0], t[1], 'H'}) |
| 109 | a = append(a, v...) |
| 110 | default: |
| 111 | rv := reflect.ValueOf(value) |
| 112 | rt := rv.Type() |
| 113 | if k := rt.Kind(); k != reflect.Array && k != reflect.Slice { |
| 114 | return nil, fmt.Errorf("sam: unknown type %T", value) |
| 115 | } |
| 116 | l := rv.Len() |
| 117 | if uint(l) > math.MaxUint32 { |
| 118 | return nil, fmt.Errorf("sam: array too long") |
| 119 | } |
| 120 | a = Aux{t[0], t[1], 'B', 0xff, 0, 0, 0, 0} |
| 121 | binary.LittleEndian.PutUint32([]byte(a[4:8]), uint32(l)) |
| 122 | |
| 123 | switch rt.Elem().Kind() { |
| 124 | case reflect.Int8: |
| 125 | a[3] = 'c' |
| 126 | value := value.([]int8) |
| 127 | b := *(*[]byte)(unsafe.Pointer(&value)) |
| 128 | return append(a, b...), nil |
| 129 | case reflect.Uint8: |
| 130 | a[3] = 'C' |
| 131 | return append(a, value.([]uint8)...), nil |
| 132 | case reflect.Int16: |
| 133 | a[3] = 's' |
| 134 | case reflect.Uint16: |
| 135 | a[3] = 'S' |
| 136 | case reflect.Int32: |
| 137 | a[3] = 'i' |
| 138 | case reflect.Uint32: |
| 139 | a[3] = 'I' |
| 140 | case reflect.Float32: |
| 141 | a[3] = 'f' |
| 142 | default: |
| 143 | return nil, fmt.Errorf("sam: unsupported array type: %T", value) |
| 144 | } |
| 145 | buf := bytes.NewBuffer(a) |
| 146 | err := binary.Write(buf, binary.LittleEndian, value) |
| 147 | a = buf.Bytes() |
| 148 | if err != nil { |
| 149 | return nil, fmt.Errorf("sam: failed to encode array: %w", err) |
| 150 | } |
| 151 | } |
| 152 | return a, nil |
| 153 | } |
| 154 | |
| 155 | // ParseAux returns an AUX parsed from the given text. |
| 156 | func ParseAux(text []byte) (Aux, error) { |
| 157 | // TG:T:v... |
| 158 | // 012345... |
| 159 | if len(text) < 6 || text[2] != ':' || text[4] != ':' { |
| 160 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 161 | } |
| 162 | txt := text[5:] |
| 163 | var value interface{} |
| 164 | switch typ := text[3]; typ { |
| 165 | case 'A': |
| 166 | if len(txt) != 1 { |
| 167 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 168 | } |
| 169 | value = ASCII(txt[0]) |
| 170 | case 'i': |
| 171 | i, err := strconv.Atoi(string(txt)) |
| 172 | if err != nil { |
| 173 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 174 | } |
| 175 | if i < 0 { |
| 176 | value = i |
| 177 | } else { |
| 178 | value = uint(i) |
| 179 | } |
| 180 | case 'f': |
| 181 | f, err := strconv.ParseFloat(string(txt), 32) |
| 182 | if err != nil { |
| 183 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 184 | } |
| 185 | value = float32(f) |
| 186 | case 'Z': |
| 187 | value = Text(txt) |
| 188 | case 'H': |
| 189 | b := make([]byte, hex.DecodedLen(len(txt))) |
| 190 | _, err := hex.Decode(b, txt) |
| 191 | if err != nil { |
| 192 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 193 | } |
| 194 | value = Hex(b) |
| 195 | case 'B': |
| 196 | if txt[1] != ',' { |
| 197 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 198 | } |
| 199 | nf := bytes.Split(txt[2:], []byte{','}) |
| 200 | if len(nf) == 0 { |
| 201 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 202 | } |
| 203 | switch txt[0] { |
| 204 | case 'c': |
| 205 | a := make([]int8, len(nf)) |
| 206 | for i, n := range nf { |
| 207 | v, err := strconv.ParseInt(string(n), 0, 8) |
| 208 | if err != nil { |
| 209 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 210 | } |
| 211 | a[i] = int8(v) |
| 212 | } |
| 213 | value = a |
| 214 | case 'C': |
| 215 | a := make([]uint8, len(nf)) |
| 216 | for i, n := range nf { |
| 217 | v, err := strconv.ParseUint(string(n), 0, 8) |
| 218 | if err != nil { |
| 219 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 220 | } |
| 221 | a[i] = uint8(v) |
| 222 | } |
| 223 | value = a |
| 224 | case 's': |
| 225 | a := make([]int16, len(nf)) |
| 226 | for i, n := range nf { |
| 227 | v, err := strconv.ParseInt(string(n), 0, 16) |
| 228 | if err != nil { |
| 229 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 230 | } |
| 231 | a[i] = int16(v) |
| 232 | } |
| 233 | value = a |
| 234 | case 'S': |
| 235 | a := make([]uint16, len(nf)) |
| 236 | for i, n := range nf { |
| 237 | v, err := strconv.ParseUint(string(n), 0, 16) |
| 238 | if err != nil { |
| 239 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 240 | } |
| 241 | a[i] = uint16(v) |
| 242 | } |
| 243 | value = a |
| 244 | case 'i': |
| 245 | a := make([]int32, len(nf)) |
| 246 | for i, n := range nf { |
| 247 | v, err := strconv.ParseInt(string(n), 0, 32) |
| 248 | if err != nil { |
| 249 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 250 | } |
| 251 | a[i] = int32(v) |
| 252 | } |
| 253 | value = a |
| 254 | case 'I': |
| 255 | a := make([]uint32, len(nf)) |
| 256 | for i, n := range nf { |
| 257 | v, err := strconv.ParseUint(string(n), 0, 32) |
| 258 | if err != nil { |
| 259 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 260 | } |
| 261 | a[i] = uint32(v) |
| 262 | } |
| 263 | value = a |
| 264 | case 'f': |
| 265 | a := make([]float32, len(nf)) |
| 266 | for i, n := range nf { |
| 267 | f, err := strconv.ParseFloat(string(n), 32) |
| 268 | if err != nil { |
| 269 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 270 | } |
| 271 | a[i] = float32(f) |
| 272 | } |
| 273 | value = a |
| 274 | default: |
| 275 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 276 | } |
| 277 | default: |
| 278 | return nil, fmt.Errorf("sam: invalid aux tag field: %q", text) |
| 279 | } |
| 280 | aux, err := NewAux(Tag{text[0], text[1]}, value) |
| 281 | if err != nil { |
| 282 | return nil, fmt.Errorf("sam: invalid aux tag field: %w", err) |
| 283 | } |
| 284 | return aux, nil |
| 285 | } |
| 286 | |
| 287 | var auxKind = [256]byte{ |
| 288 | 'A': 'A', |
| 289 | 'c': 'i', 'C': 'i', |
| 290 | 's': 'i', 'S': 'i', |
| 291 | 'i': 'i', 'I': 'i', |
| 292 | 'f': 'f', |
| 293 | 'Z': 'Z', |
| 294 | 'H': 'H', |
| 295 | 'B': 'B', |
| 296 | } |
| 297 | |
| 298 | // String returns the string representation of an Aux type. |
| 299 | func (a Aux) String() string { |
| 300 | switch a.Type() { |
| 301 | case 'A': |
| 302 | return fmt.Sprintf("%s:%c:%c", []byte(a[:2]), a.Kind(), a.Value()) |
| 303 | case 'H': |
| 304 | return fmt.Sprintf("%s:%c:%02x", []byte(a[:2]), a.Kind(), a.Value()) |
| 305 | case 'B': |
| 306 | return fmt.Sprintf("%s:%c:%c:%v", []byte(a[:2]), a.Kind(), a[3], a.Value()) |
| 307 | } |
| 308 | return fmt.Sprintf("%s:%c:%v", []byte(a[:2]), a.Kind(), a.Value()) |
| 309 | } |
| 310 | |
| 311 | // samAux implements SAM aux field formatting. |
| 312 | type samAux Aux |
| 313 | |
| 314 | // String returns the string representation of an Aux type. |
| 315 | func (sa samAux) String() string { |
| 316 | a := Aux(sa) |
| 317 | switch a.Type() { |
| 318 | case 'A': |
| 319 | return fmt.Sprintf("%s:%c:%c", []byte(a[:2]), a.Kind(), a.Value()) |
| 320 | case 'H': |
| 321 | return fmt.Sprintf("%s:%c:%02x", []byte(a[:2]), a.Kind(), a.Value()) |
| 322 | case 'B': |
| 323 | var buf bytes.Buffer |
| 324 | fmt.Fprintf(&buf, "%s:%c:%c", []byte(a[:2]), a.Kind(), a[3]) |
| 325 | rv := reflect.ValueOf(a.Value()) |
| 326 | for i := 0; i < rv.Len(); i++ { |
| 327 | fmt.Fprintf(&buf, ",%v", rv.Index(i).Interface()) |
| 328 | } |
| 329 | return buf.String() |
| 330 | } |
| 331 | return fmt.Sprintf("%s:%c:%v", []byte(a[:2]), a.Kind(), a.Value()) |
| 332 | } |
| 333 | |
| 334 | // A Tag represents an auxiliary or header tag label. |
| 335 | type Tag [2]byte |
| 336 | |
| 337 | var ( |
| 338 | headerTag = Tag{'H', 'D'} |
| 339 | versionTag = Tag{'V', 'N'} |
| 340 | sortOrderTag = Tag{'S', 'O'} |
| 341 | groupOrderTag = Tag{'G', 'O'} |
| 342 | |
| 343 | refDictTag = Tag{'S', 'Q'} |
| 344 | refNameTag = Tag{'S', 'N'} |
| 345 | refLengthTag = Tag{'L', 'N'} |
| 346 | alternativeLocus = Tag{'A', 'H'} |
| 347 | assemblyIDTag = Tag{'A', 'S'} |
| 348 | md5Tag = Tag{'M', '5'} |
| 349 | speciesTag = Tag{'S', 'P'} |
| 350 | uriTag = Tag{'U', 'R'} |
| 351 | |
| 352 | readGroupTag = Tag{'R', 'G'} |
| 353 | centerTag = Tag{'C', 'N'} |
| 354 | descriptionTag = Tag{'D', 'S'} |
| 355 | dateTag = Tag{'D', 'T'} |
| 356 | flowOrderTag = Tag{'F', 'O'} |
| 357 | keySequenceTag = Tag{'K', 'S'} |
| 358 | libraryTag = Tag{'L', 'B'} |
| 359 | insertSizeTag = Tag{'P', 'I'} |
| 360 | platformTag = Tag{'P', 'L'} |
| 361 | platformUnitTag = Tag{'P', 'U'} |
| 362 | sampleTag = Tag{'S', 'M'} |
| 363 | |
| 364 | programTag = Tag{'P', 'G'} |
| 365 | idTag = Tag{'I', 'D'} |
| 366 | programNameTag = Tag{'P', 'N'} |
| 367 | commandLineTag = Tag{'C', 'L'} |
| 368 | previousProgTag = Tag{'P', 'P'} |
| 369 | progDesc = Tag{'D', 'S'} |
| 370 | |
| 371 | commentTag = Tag{'C', 'O'} |
| 372 | ) |
| 373 | |
| 374 | // NewTag returns a Tag from the tag string. It panics is len(tag) != 2. |
| 375 | func NewTag(tag string) Tag { |
| 376 | var t Tag |
| 377 | if copy(t[:], tag) != 2 { |
| 378 | panic("sam: illegal tag length") |
| 379 | } |
| 380 | return t |
| 381 | } |
| 382 | |
| 383 | // String returns a string representation of a Tag. |
| 384 | func (t Tag) String() string { return string(t[:]) } |
| 385 | |
| 386 | // Tag returns the Tag representation of the Aux tag ID. |
| 387 | func (a Aux) Tag() Tag { var t Tag; copy(t[:], a[:2]); return t } |
| 388 | |
| 389 | // Type returns a byte corresponding to the type of the auxiliary tag. |
| 390 | // Returned values are in {'A', 'c', 'C', 's', 'S', 'i', 'I', 'f', 'Z', 'H', 'B'}. |
| 391 | func (a Aux) Type() byte { return a[2] } |
| 392 | |
| 393 | // Kind returns a byte corresponding to the kind of the auxiliary tag. |
| 394 | // Returned values are in {'A', 'i', 'f', 'Z', 'H', 'B'}. |
| 395 | func (a Aux) Kind() byte { return auxKind[a[2]] } |
| 396 | |
| 397 | // Value returns v containing the value of the auxiliary tag. |
| 398 | func (a Aux) Value() interface{} { |
| 399 | switch t := a.Type(); t { |
| 400 | case 'A': |
| 401 | return a[3] |
| 402 | case 'c': |
| 403 | return int8(a[3]) |
| 404 | case 'C': |
| 405 | return uint8(a[3]) |
| 406 | case 's': |
| 407 | return int16(binary.LittleEndian.Uint16(a[3:5])) |
| 408 | case 'S': |
| 409 | return binary.LittleEndian.Uint16(a[3:5]) |
| 410 | case 'i': |
| 411 | return int32(binary.LittleEndian.Uint32(a[3:7])) |
| 412 | case 'I': |
| 413 | return binary.LittleEndian.Uint32(a[3:7]) |
| 414 | case 'f': |
| 415 | return math.Float32frombits(binary.LittleEndian.Uint32(a[3:7])) |
| 416 | case 'Z': // Z and H Require that parsing stops before the terminating zero. |
| 417 | return string(a[3:]) |
| 418 | case 'H': |
| 419 | return []byte(a[3:]) |
| 420 | case 'B': |
| 421 | length := int32(binary.LittleEndian.Uint32(a[4:8])) |
| 422 | switch t := a[3]; t { |
| 423 | case 'c': |
| 424 | c := a[8:] |
| 425 | return *(*[]int8)(unsafe.Pointer(&c)) |
| 426 | case 'C': |
| 427 | return []uint8(a[8:]) |
| 428 | case 's': |
| 429 | Bs := make([]int16, length) |
| 430 | err := binary.Read(bytes.NewBuffer(a[8:]), binary.LittleEndian, &Bs) |
| 431 | if err != nil { |
| 432 | panic(fmt.Sprintf("sam: binary.Read of s field failed: %v", err)) |
| 433 | } |
| 434 | return Bs |
| 435 | case 'S': |
| 436 | BS := make([]uint16, length) |
| 437 | err := binary.Read(bytes.NewBuffer(a[8:]), binary.LittleEndian, &BS) |
| 438 | if err != nil { |
| 439 | panic(fmt.Sprintf("sam: binary.Read of S field failed: %v", err)) |
| 440 | } |
| 441 | return BS |
| 442 | case 'i': |
| 443 | Bi := make([]int32, length) |
| 444 | err := binary.Read(bytes.NewBuffer(a[8:]), binary.LittleEndian, &Bi) |
| 445 | if err != nil { |
| 446 | panic(fmt.Sprintf("sam: binary.Read of i field failed: %v", err)) |
| 447 | } |
| 448 | return Bi |
| 449 | case 'I': |
| 450 | BI := make([]uint32, length) |
| 451 | err := binary.Read(bytes.NewBuffer(a[8:]), binary.LittleEndian, &BI) |
| 452 | if err != nil { |
| 453 | panic(fmt.Sprintf("sam: binary.Read of I field failed: %v", err)) |
| 454 | } |
| 455 | return BI |
| 456 | case 'f': |
| 457 | Bf := make([]float32, length) |
| 458 | err := binary.Read(bytes.NewBuffer(a[8:]), binary.LittleEndian, &Bf) |
| 459 | if err != nil { |
| 460 | panic(fmt.Sprintf("sam: binary.Read of f field failed: %v", err)) |
| 461 | } |
| 462 | return Bf |
| 463 | default: |
| 464 | return fmt.Errorf("%%B!(UNKNOWN ARRAY type=%c)", t) |
| 465 | } |
| 466 | default: |
| 467 | return fmt.Errorf("%%?!(UNKNOWN type=%c)", t) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func (a Aux) matches(tag []byte) bool { |
| 472 | return a[1] == tag[1] && a[0] == tag[0] |
| 473 | } |
| 474 | |
| 475 | // AuxFields is a set of auxiliary fields. |
| 476 | type AuxFields []Aux |
| 477 | |
| 478 | // Get returns the auxiliary field identified by the given tag, or nil |
| 479 | // if no field matches. |
| 480 | func (a AuxFields) Get(tag Tag) Aux { |
| 481 | for _, f := range a { |
| 482 | if f.Tag() == tag { |
| 483 | return f |
| 484 | } |
| 485 | } |
| 486 | return nil |
| 487 | } |