@gmod/bam 7.1.4 → 7.1.6

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.
package/src/bamFile.ts CHANGED
@@ -11,14 +11,30 @@ import BAMFeature from './record.ts'
11
11
  import { parseHeaderText } from './sam.ts'
12
12
  import { gen2array, makeOpts } from './util.ts'
13
13
 
14
+ import type { Bytes } from './record.ts'
14
15
  import type { BamOpts, BaseOpts } from './util.ts'
15
16
  import type { GenericFilehandle } from 'generic-filehandle2'
16
17
 
18
+ export interface BamRecordLike {
19
+ ref_id: number
20
+ start: number
21
+ end: number
22
+ name: string
23
+ id: number
24
+ next_pos: number
25
+ next_refid: number
26
+ }
27
+
28
+ export type BamRecordClass<T extends BamRecordLike = BAMFeature> = new (args: {
29
+ bytes: Bytes
30
+ fileOffset: number
31
+ }) => T
32
+
17
33
  export const BAM_MAGIC = 21840194
18
34
 
19
35
  const blockLen = 1 << 16
20
36
 
21
- export default class BamFile {
37
+ export default class BamFile<T extends BamRecordLike = BAMFeature> {
22
38
  public renameRefSeq: (a: string) => string
23
39
  public bam: GenericFilehandle
24
40
  public header?: string
@@ -26,7 +42,7 @@ export default class BamFile {
26
42
  public indexToChr?: { refName: string; length: number }[]
27
43
  public index?: BAI | CSI
28
44
  public htsget = false
29
- public headerP?: ReturnType<BamFile['getHeaderPre']>
45
+ public headerP?: ReturnType<BamFile<T>['getHeaderPre']>
30
46
  public cache = new QuickLRU<
31
47
  string,
32
48
  { bytesRead: number; buffer: Uint8Array; nextIn: number }
@@ -38,9 +54,11 @@ export default class BamFile {
38
54
  // When a new chunk overlaps a cached chunk, we evict the cached one
39
55
  public chunkFeatureCache = new QuickLRU<
40
56
  string,
41
- { minBlock: number; maxBlock: number; features: BAMFeature[] }
57
+ { minBlock: number; maxBlock: number; features: T[] }
42
58
  >({ maxSize: 100 })
43
59
 
60
+ private RecordClass: BamRecordClass<T>
61
+
44
62
  constructor({
45
63
  bamFilehandle,
46
64
  bamPath,
@@ -53,6 +71,7 @@ export default class BamFile {
53
71
  csiUrl,
54
72
  htsget,
55
73
  renameRefSeqs = n => n,
74
+ recordClass,
56
75
  }: {
57
76
  bamFilehandle?: GenericFilehandle
58
77
  bamPath?: string
@@ -65,8 +84,10 @@ export default class BamFile {
65
84
  csiUrl?: string
66
85
  renameRefSeqs?: (a: string) => string
67
86
  htsget?: boolean
87
+ recordClass?: BamRecordClass<T>
68
88
  }) {
69
89
  this.renameRefSeq = renameRefSeqs
90
+ this.RecordClass = (recordClass ?? BAMFeature) as BamRecordClass<T>
70
91
 
71
92
  if (bamFilehandle) {
72
93
  this.bam = bamFilehandle
@@ -252,7 +273,7 @@ export default class BamFile {
252
273
  opts: BamOpts = {},
253
274
  ) {
254
275
  const { viewAsPairs } = opts
255
- const feats = [] as BAMFeature[][]
276
+ const feats = [] as T[][]
256
277
  let done = false
257
278
  // let cacheHits = 0
258
279
  // let cacheMisses = 0
@@ -262,7 +283,7 @@ export default class BamFile {
262
283
  const minBlock = chunk.minv.blockPosition
263
284
  const maxBlock = chunk.maxv.blockPosition
264
285
 
265
- let records: BAMFeature[]
286
+ let records: T[]
266
287
  const cached = this.chunkFeatureCache.get(cacheKey)
267
288
  if (cached) {
268
289
  records = cached.features
@@ -287,7 +308,7 @@ export default class BamFile {
287
308
  // cacheMisses++
288
309
  }
289
310
 
290
- const recs = [] as BAMFeature[]
311
+ const recs = [] as T[]
291
312
  for (const feature of records) {
292
313
  if (feature.ref_id === chrId) {
293
314
  if (feature.start >= max) {
@@ -318,7 +339,7 @@ export default class BamFile {
318
339
  }
319
340
  }
320
341
 
321
- async fetchPairs(chrId: number, feats: BAMFeature[][], opts: BamOpts) {
342
+ async fetchPairs(chrId: number, feats: T[][], opts: BamOpts) {
322
343
  const { pairAcrossChr, maxInsertSize = 200000 } = opts
323
344
  const unmatedPairs: Record<string, boolean> = {}
324
345
  const readIds: Record<string, number> = {}
@@ -376,7 +397,7 @@ export default class BamFile {
376
397
  chunk: c,
377
398
  opts,
378
399
  })
379
- const mateRecs = [] as BAMFeature[]
400
+ const mateRecs = [] as T[]
380
401
  for (const feature of await this.readBamFeatures(
381
402
  data,
382
403
  cpositions,
@@ -415,7 +436,7 @@ export default class BamFile {
415
436
  chunk: Chunk,
416
437
  ) {
417
438
  let blockStart = 0
418
- const sink = [] as BAMFeature[]
439
+ const sink = [] as T[]
419
440
  let pos = 0
420
441
 
421
442
  const dataView = new DataView(ba.buffer)
@@ -432,7 +453,7 @@ export default class BamFile {
432
453
  }
433
454
 
434
455
  if (blockEnd < ba.length) {
435
- const feature = new BAMFeature({
456
+ const feature = new this.RecordClass({
436
457
  bytes: {
437
458
  byteArray: ba,
438
459
  start: blockStart,
package/src/cigar.ts ADDED
@@ -0,0 +1,9 @@
1
+ export const CIGAR_MATCH = 0
2
+ export const CIGAR_INS = 1
3
+ export const CIGAR_DEL = 2
4
+ export const CIGAR_REF_SKIP = 3
5
+ export const CIGAR_SOFT_CLIP = 4
6
+ export const CIGAR_HARD_CLIP = 5
7
+ export const CIGAR_PAD = 6
8
+ export const CIGAR_EQUAL = 7
9
+ export const CIGAR_DIFF = 8
package/src/index.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  export { default as BAI } from './bai.ts'
2
- export { default as BamFile } from './bamFile.ts'
2
+ export {
3
+ type BamRecordClass,
4
+ type BamRecordLike,
5
+ default as BamFile,
6
+ } from './bamFile.ts'
3
7
  export { default as CSI } from './csi.ts'
4
- export { default as BamRecord } from './record.ts'
8
+ export { type Bytes, default as BamRecord } from './record.ts'
5
9
  export { default as HtsgetFile } from './htsget.ts'