@gmod/bam 1.1.17 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/CHANGELOG.md +65 -25
  2. package/README.md +108 -57
  3. package/dist/bai.d.ts +34 -15
  4. package/dist/bai.js +180 -273
  5. package/dist/bai.js.map +1 -1
  6. package/dist/bamFile.d.ts +33 -27
  7. package/dist/bamFile.js +353 -572
  8. package/dist/bamFile.js.map +1 -1
  9. package/dist/chunk.d.ts +4 -8
  10. package/dist/chunk.js +13 -21
  11. package/dist/chunk.js.map +1 -1
  12. package/dist/csi.d.ts +74 -10
  13. package/dist/csi.js +157 -256
  14. package/dist/csi.js.map +1 -1
  15. package/dist/errors.js +12 -57
  16. package/dist/errors.js.map +1 -1
  17. package/dist/htsget.d.ts +5 -8
  18. package/dist/htsget.js +120 -209
  19. package/dist/htsget.js.map +1 -1
  20. package/dist/index.d.ts +5 -6
  21. package/dist/index.js +11 -11
  22. package/dist/index.js.map +1 -1
  23. package/dist/indexFile.d.ts +0 -6
  24. package/dist/indexFile.js +3 -77
  25. package/dist/indexFile.js.map +1 -1
  26. package/dist/nullIndex.d.ts +7 -0
  27. package/dist/nullIndex.js +33 -0
  28. package/dist/nullIndex.js.map +1 -0
  29. package/dist/record.d.ts +2 -2
  30. package/dist/record.js +200 -193
  31. package/dist/record.js.map +1 -1
  32. package/dist/sam.js +12 -10
  33. package/dist/sam.js.map +1 -1
  34. package/dist/util.d.ts +13 -1
  35. package/dist/util.js +55 -58
  36. package/dist/util.js.map +1 -1
  37. package/dist/virtualOffset.js +13 -20
  38. package/dist/virtualOffset.js.map +1 -1
  39. package/esm/bai.d.ts +34 -15
  40. package/esm/bai.js +86 -91
  41. package/esm/bai.js.map +1 -1
  42. package/esm/bamFile.d.ts +33 -27
  43. package/esm/bamFile.js +124 -120
  44. package/esm/bamFile.js.map +1 -1
  45. package/esm/chunk.d.ts +4 -8
  46. package/esm/chunk.js +2 -8
  47. package/esm/chunk.js.map +1 -1
  48. package/esm/csi.d.ts +74 -10
  49. package/esm/csi.js +85 -93
  50. package/esm/csi.js.map +1 -1
  51. package/esm/htsget.d.ts +5 -8
  52. package/esm/htsget.js +68 -43
  53. package/esm/htsget.js.map +1 -1
  54. package/esm/index.d.ts +5 -6
  55. package/esm/index.js +5 -6
  56. package/esm/index.js.map +1 -1
  57. package/esm/indexFile.d.ts +0 -6
  58. package/esm/indexFile.js +0 -22
  59. package/esm/indexFile.js.map +1 -1
  60. package/esm/nullIndex.d.ts +7 -0
  61. package/esm/nullIndex.js +16 -0
  62. package/esm/nullIndex.js.map +1 -0
  63. package/esm/record.d.ts +2 -2
  64. package/esm/record.js +34 -24
  65. package/esm/record.js.map +1 -1
  66. package/esm/sam.js +9 -7
  67. package/esm/sam.js.map +1 -1
  68. package/esm/util.d.ts +13 -1
  69. package/esm/util.js +40 -14
  70. package/esm/util.js.map +1 -1
  71. package/package.json +19 -20
  72. package/src/bai.ts +99 -102
  73. package/src/bamFile.ts +174 -198
  74. package/src/chunk.ts +6 -20
  75. package/src/csi.ts +102 -111
  76. package/src/htsget.ts +81 -61
  77. package/src/index.ts +5 -7
  78. package/src/indexFile.ts +0 -27
  79. package/src/nullIndex.ts +18 -0
  80. package/src/record.ts +34 -24
  81. package/src/sam.ts +9 -7
  82. package/src/util.ts +54 -13
  83. package/src/declare.d.ts +0 -2
package/src/record.ts CHANGED
@@ -83,11 +83,11 @@ export default class BamRecord {
83
83
  }
84
84
  tags = tags.concat(this._tagList || [])
85
85
 
86
- Object.keys(this.data).forEach(k => {
86
+ for (const k of Object.keys(this.data)) {
87
87
  if (k[0] !== '_' && k !== 'next_seq_id') {
88
88
  tags.push(k)
89
89
  }
90
- })
90
+ }
91
91
 
92
92
  const seen: { [key: string]: boolean } = {}
93
93
  return tags.filter(t => {
@@ -107,7 +107,7 @@ export default class BamRecord {
107
107
  }
108
108
 
109
109
  parent() {
110
- return undefined
110
+ return
111
111
  }
112
112
 
113
113
  children() {
@@ -137,7 +137,7 @@ export default class BamRecord {
137
137
 
138
138
  qualRaw() {
139
139
  if (this.isSegmentUnmapped()) {
140
- return undefined
140
+ return
141
141
  }
142
142
 
143
143
  const { start, byteArray } = this.bytes
@@ -157,7 +157,7 @@ export default class BamRecord {
157
157
 
158
158
  multi_segment_next_segment_strand() {
159
159
  if (this.isMateUnmapped()) {
160
- return undefined
160
+ return
161
161
  }
162
162
  return this.isMateReverseComplemented() ? -1 : 1
163
163
  }
@@ -181,7 +181,7 @@ export default class BamRecord {
181
181
  // called, we already know that we have no such tag, because
182
182
  // it would already have been cached.
183
183
  if (this._allTagsParsed) {
184
- return undefined
184
+ return
185
185
  }
186
186
 
187
187
  const { byteArray, start } = this.bytes
@@ -204,40 +204,48 @@ export default class BamRecord {
204
204
 
205
205
  let value
206
206
  switch (type) {
207
- case 'A':
207
+ case 'A': {
208
208
  value = String.fromCharCode(byteArray[p])
209
209
  p += 1
210
210
  break
211
- case 'i':
211
+ }
212
+ case 'i': {
212
213
  value = byteArray.readInt32LE(p)
213
214
  p += 4
214
215
  break
215
- case 'I':
216
+ }
217
+ case 'I': {
216
218
  value = byteArray.readUInt32LE(p)
217
219
  p += 4
218
220
  break
219
- case 'c':
221
+ }
222
+ case 'c': {
220
223
  value = byteArray.readInt8(p)
221
224
  p += 1
222
225
  break
223
- case 'C':
226
+ }
227
+ case 'C': {
224
228
  value = byteArray.readUInt8(p)
225
229
  p += 1
226
230
  break
227
- case 's':
231
+ }
232
+ case 's': {
228
233
  value = byteArray.readInt16LE(p)
229
234
  p += 2
230
235
  break
231
- case 'S':
236
+ }
237
+ case 'S': {
232
238
  value = byteArray.readUInt16LE(p)
233
239
  p += 2
234
240
  break
235
- case 'f':
241
+ }
242
+ case 'f': {
236
243
  value = byteArray.readFloatLE(p)
237
244
  p += 4
238
245
  break
246
+ }
239
247
  case 'Z':
240
- case 'H':
248
+ case 'H': {
241
249
  value = ''
242
250
  while (p <= blockEnd) {
243
251
  const cc = byteArray[p++]
@@ -248,6 +256,7 @@ export default class BamRecord {
248
256
  }
249
257
  }
250
258
  break
259
+ }
251
260
  case 'B': {
252
261
  value = ''
253
262
  const cc = byteArray[p++]
@@ -339,10 +348,11 @@ export default class BamRecord {
339
348
  }
340
349
  break
341
350
  }
342
- default:
351
+ default: {
343
352
  console.warn(`Unknown BAM tag type '${type}', tags may be incomplete`)
344
353
  value = undefined
345
- p = blockEnd // stop parsing tags
354
+ p = blockEnd
355
+ } // stop parsing tags
346
356
  }
347
357
 
348
358
  this._tagOffset = p
@@ -355,7 +365,7 @@ export default class BamRecord {
355
365
  this.data[lcTag] = value
356
366
  }
357
367
  this._allTagsParsed = true
358
- return undefined
368
+ return
359
369
  }
360
370
 
361
371
  _parseAllTags() {
@@ -368,7 +378,7 @@ export default class BamRecord {
368
378
  cigar
369
379
  .match(/\d+\D/g)
370
380
  //@ts-ignore
371
- .map(op => [op.match(/\D/)[0].toUpperCase(), parseInt(op, 10)])
381
+ .map(op => [op.match(/\D/)[0].toUpperCase(), Number.parseInt(op, 10)])
372
382
  )
373
383
  }
374
384
 
@@ -436,7 +446,7 @@ export default class BamRecord {
436
446
 
437
447
  cigar() {
438
448
  if (this.isSegmentUnmapped()) {
439
- return undefined
449
+ return
440
450
  }
441
451
 
442
452
  const { byteArray, start } = this.bytes
@@ -568,7 +578,7 @@ export default class BamRecord {
568
578
  }
569
579
  return tmp.join('')
570
580
  }
571
- return null
581
+ return ''
572
582
  }
573
583
 
574
584
  _bin_mq_nl() {
@@ -597,13 +607,13 @@ export default class BamRecord {
597
607
 
598
608
  toJSON() {
599
609
  const data: { [key: string]: any } = {}
600
- Object.keys(this).forEach(k => {
610
+ for (const k of Object.keys(this)) {
601
611
  if (k.charAt(0) === '_' || k === 'bytes') {
602
- return
612
+ continue
603
613
  }
604
614
  //@ts-ignore
605
615
  data[k] = this[k]
606
- })
616
+ }
607
617
 
608
618
  return data
609
619
  }
package/src/sam.ts CHANGED
@@ -1,15 +1,17 @@
1
1
  export function parseHeaderText(text: string) {
2
2
  const lines = text.split(/\r?\n/)
3
3
  const data: { tag: string; data: { tag: string; value: string }[] }[] = []
4
- lines.forEach(line => {
4
+ for (const line of lines) {
5
5
  const [tag, ...fields] = line.split(/\t/)
6
- const parsedFields = fields.map(f => {
7
- const [fieldTag, value] = f.split(':', 2)
8
- return { tag: fieldTag, value }
9
- })
10
6
  if (tag) {
11
- data.push({ tag: tag.substr(1), data: parsedFields })
7
+ data.push({
8
+ tag: tag.slice(1),
9
+ data: fields.map(f => {
10
+ const [fieldTag, value] = f.split(':', 2)
11
+ return { tag: fieldTag, value }
12
+ }),
13
+ })
12
14
  }
13
- })
15
+ }
14
16
  return data
15
17
  }
package/src/util.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import Long from 'long'
1
2
  import Chunk from './chunk'
2
3
  import VirtualOffset from './virtualOffset'
3
4
 
@@ -33,13 +34,13 @@ export function checkAbortSignal(signal?: AbortSignal) {
33
34
 
34
35
  if (signal.aborted) {
35
36
  // console.log('bam aborted!')
36
- if (typeof DOMException !== 'undefined') {
37
- throw new DOMException('aborted', 'AbortError')
38
- } else {
37
+ if (typeof DOMException === 'undefined') {
39
38
  const e = new Error('aborted')
40
39
  //@ts-ignore
41
40
  e.code = 'ERR_ABORTED'
42
41
  throw e
42
+ } else {
43
+ throw new DOMException('aborted', 'AbortError')
43
44
  }
44
45
  }
45
46
  }
@@ -77,9 +78,9 @@ export function makeOpts(obj: AbortSignal | BaseOpts = {}): BaseOpts {
77
78
  return 'aborted' in obj ? ({ signal: obj } as BaseOpts) : (obj as BaseOpts)
78
79
  }
79
80
 
80
- export function optimizeChunks(chunks: Chunk[], lowest: VirtualOffset) {
81
+ export function optimizeChunks(chunks: Chunk[], lowest?: VirtualOffset) {
81
82
  const mergedChunks: Chunk[] = []
82
- let lastChunk: Chunk | null = null
83
+ let lastChunk: Chunk | undefined
83
84
 
84
85
  if (chunks.length === 0) {
85
86
  return chunks
@@ -87,16 +88,12 @@ export function optimizeChunks(chunks: Chunk[], lowest: VirtualOffset) {
87
88
 
88
89
  chunks.sort((c0, c1) => {
89
90
  const dif = c0.minv.blockPosition - c1.minv.blockPosition
90
- if (dif !== 0) {
91
- return dif
92
- } else {
93
- return c0.minv.dataPosition - c1.minv.dataPosition
94
- }
91
+ return dif === 0 ? c0.minv.dataPosition - c1.minv.dataPosition : dif
95
92
  })
96
93
 
97
- chunks.forEach(chunk => {
94
+ for (const chunk of chunks) {
98
95
  if (!lowest || chunk.maxv.compareTo(lowest) > 0) {
99
- if (lastChunk === null) {
96
+ if (lastChunk === undefined) {
100
97
  mergedChunks.push(chunk)
101
98
  lastChunk = chunk
102
99
  } else {
@@ -110,7 +107,51 @@ export function optimizeChunks(chunks: Chunk[], lowest: VirtualOffset) {
110
107
  }
111
108
  }
112
109
  }
113
- })
110
+ }
114
111
 
115
112
  return mergedChunks
116
113
  }
114
+
115
+ export function parsePseudoBin(bytes: Buffer, offset: number) {
116
+ const lineCount = longToNumber(
117
+ Long.fromBytesLE(
118
+ Array.prototype.slice.call(bytes, offset, offset + 8),
119
+ true,
120
+ ),
121
+ )
122
+ return { lineCount }
123
+ }
124
+
125
+ export function findFirstData(
126
+ firstDataLine: VirtualOffset | undefined,
127
+ virtualOffset: VirtualOffset,
128
+ ) {
129
+ return firstDataLine
130
+ ? firstDataLine.compareTo(virtualOffset) > 0
131
+ ? virtualOffset
132
+ : firstDataLine
133
+ : virtualOffset
134
+ }
135
+
136
+ export function parseNameBytes(
137
+ namesBytes: Buffer,
138
+ renameRefSeq: (arg: string) => string = s => s,
139
+ ) {
140
+ let currRefId = 0
141
+ let currNameStart = 0
142
+ const refIdToName = []
143
+ const refNameToId: { [key: string]: number } = {}
144
+ for (let i = 0; i < namesBytes.length; i += 1) {
145
+ if (!namesBytes[i]) {
146
+ if (currNameStart < i) {
147
+ let refName = namesBytes.toString('utf8', currNameStart, i)
148
+ refName = renameRefSeq(refName)
149
+ refIdToName[currRefId] = refName
150
+ refNameToId[refName] = currRefId
151
+ }
152
+ currNameStart = i + 1
153
+ currRefId += 1
154
+ }
155
+ }
156
+ return { refNameToId, refIdToName }
157
+ }
package/src/declare.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare module 'object.entries-ponyfill'
2
- declare module '@gmod/bgzf-filehandle'