@gmod/cram 8.0.4 → 8.0.5

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/README.md CHANGED
@@ -27,10 +27,10 @@ $ yarn add @gmod/cram
27
27
  ## Usage
28
28
 
29
29
  ```js
30
- const { IndexedCramFile, CramFile, CraiIndex } = require('@gmod/cram')
30
+ import { IndexedCramFile, CramFile, CraiIndex } from '@gmod/cram'
31
31
 
32
32
  // Use indexedfasta library for seqFetch, if using local file (see below)
33
- const { IndexedFasta, BgzipIndexedFasta } = require('@gmod/indexedfasta')
33
+ import { IndexedFasta, BgzipIndexedFasta } from '@gmod/indexedfasta'
34
34
 
35
35
  // this uses local file paths for node.js for IndexedFasta, for usages using
36
36
  // remote URLs see indexedfasta docs for filehandles and
@@ -45,79 +45,74 @@ const t = new IndexedFasta({
45
45
  // For indexedfasta the numeric ID is the order in which the sequence names
46
46
  // appear in the header
47
47
 
48
- // Wrap in an async and then run
49
- run = async () => {
50
- const idToName = []
51
- const nameToId = {}
52
-
53
- // example opening local files on node.js
54
- // can also pass `cramUrl` (for the IndexedCramFile class), and `url` (for
55
- // the CraiIndex) params to open remote URLs
56
- //
57
- // alternatively `cramFilehandle` (for the IndexedCramFile class) and
58
- // `filehandle` (for the CraiIndex) can be used, see for examples
59
- // https://github.com/gmod/generic-filehandle2
60
-
61
- const indexedFile = new IndexedCramFile({
62
- cramPath: '/filesystem/yourfile.cram',
63
- //or
64
- //cramUrl: 'url/to/file.cram'
65
- //cramFilehandle: a generic-filehandle2 or similar filehandle
66
- index: new CraiIndex({
67
- path: '/filesystem/yourfile.cram.crai',
68
- // or
69
- // url: 'url/to/file.cram.crai'
70
- // filehandle: a generic-filehandle2 or similar filehandle
71
- }),
72
- seqFetch: async (seqId, start, end) => {
73
- // note:
74
- // * seqFetch should return a promise for a string, in this instance retrieved from IndexedFasta
75
- // * we use start-1 because cram-js uses 1-based but IndexedFasta uses 0-based coordinates
76
- // * the seqId is a numeric identifier, so we convert it back to a name with idToName
77
- // * you can return an empty string from this function for testing if you want, but you may not get proper interpretation of record.readFeatures
78
- return t.getSequence(idToName[seqId], start - 1, end)
79
- },
80
- checkSequenceMD5: false,
81
- })
82
- const samHeader = await indexedFile.cram.getSamHeader()
83
-
84
- // use the @SQ lines in the header to figure out the
85
- // mapping between ref ID numbers and names
86
-
87
- const sqLines = samHeader.filter(l => l.tag === 'SQ')
88
- sqLines.forEach((sqLine, refId) => {
89
- sqLine.data.forEach(item => {
90
- if (item.tag === 'SN') {
91
- // this is the ref name
92
- const refName = item.value
93
- nameToId[refName] = refId
94
- idToName[refId] = refName
95
- }
96
- })
97
- })
48
+ const idToName = []
49
+ const nameToId = {}
50
+
51
+ // example opening local files on node.js
52
+ // can also pass `cramUrl` (for the IndexedCramFile class), and `url` (for
53
+ // the CraiIndex) params to open remote URLs
54
+ //
55
+ // alternatively `cramFilehandle` (for the IndexedCramFile class) and
56
+ // `filehandle` (for the CraiIndex) can be used, see for examples
57
+ // https://github.com/gmod/generic-filehandle2
98
58
 
99
- const records = await indexedFile.getRecordsForRange(
100
- nameToId['chr1'],
101
- 10000,
102
- 20000,
103
- )
104
- records.forEach(record => {
105
- console.log(`got a record named ${record.readName}`)
106
- if (record.readFeatures != undefined) {
107
- record.readFeatures.forEach(({ code, pos, refPos, ref, sub }) => {
108
- // process the read features. this can be used similar to
109
- // CIGAR/MD strings in SAM. see CRAM specs for more details.
110
- if (code === 'X') {
111
- console.log(
112
- `${record.readName} shows a base substitution of ${ref}->${sub} at ${refPos}`,
113
- )
114
- }
115
- })
59
+ const indexedFile = new IndexedCramFile({
60
+ cramPath: '/filesystem/yourfile.cram',
61
+ //or
62
+ //cramUrl: 'url/to/file.cram'
63
+ //cramFilehandle: a generic-filehandle2 or similar filehandle
64
+ index: new CraiIndex({
65
+ path: '/filesystem/yourfile.cram.crai',
66
+ // or
67
+ // url: 'url/to/file.cram.crai'
68
+ // filehandle: a generic-filehandle2 or similar filehandle
69
+ }),
70
+ seqFetch: async (seqId, start, end) => {
71
+ // note:
72
+ // * seqFetch should return a promise for a string, in this instance retrieved from IndexedFasta
73
+ // * we use start-1 because cram-js uses 1-based but IndexedFasta uses 0-based coordinates
74
+ // * the seqId is a numeric identifier, so we convert it back to a name with idToName
75
+ // * you can return an empty string from this function for testing if you want, but you may not get proper interpretation of record.readFeatures
76
+ return t.getSequence(idToName[seqId], start - 1, end)
77
+ },
78
+ checkSequenceMD5: false,
79
+ })
80
+ const samHeader = await indexedFile.cram.getSamHeader()
81
+
82
+ // use the @SQ lines in the header to figure out the
83
+ // mapping between ref ID numbers and names
84
+
85
+ const sqLines = samHeader.filter(l => l.tag === 'SQ')
86
+ sqLines.forEach((sqLine, refId) => {
87
+ sqLine.data.forEach(item => {
88
+ if (item.tag === 'SN') {
89
+ // this is the ref name
90
+ const refName = item.value
91
+ nameToId[refName] = refId
92
+ idToName[refId] = refName
116
93
  }
117
94
  })
118
- }
95
+ })
119
96
 
120
- run()
97
+ const records = await indexedFile.getRecordsForRange(
98
+ nameToId['chr1'],
99
+ 10000,
100
+ 20000,
101
+ )
102
+ records.forEach(record => {
103
+ console.log(`got a record named ${record.readName}`)
104
+ if (record.readFeatures != undefined) {
105
+ record.readFeatures.forEach(({ code, pos, refPos, ref, sub }) => {
106
+ // process the read features. this can be used similar to
107
+ // CIGAR/MD strings in SAM. see CRAM specs for more details.
108
+ if (code === 'X') {
109
+ console.log(
110
+ `${record.readName} shows a base substitution of ${ref}->${sub} at ${refPos}`,
111
+ )
112
+ }
113
+ })
114
+ }
115
+ })
121
116
 
122
117
  // can also pass `cramUrl` (for the IndexedCramFile class), and `url` (for the CraiIndex) params to open remote URLs
123
118
  // alternatively `cramFilehandle` (for the IndexedCramFile class) and `filehandle` (for the CraiIndex) can be used, see for examples https://github.com/gmod/generic-filehandle2