forked from igvteam/igv.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgzLineReader.js
76 lines (64 loc) · 2.19 KB
/
bgzLineReader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {buildOptions} from "../util/igvUtils.js"
import {BGZip, igvxhr} from "../../node_modules/igv-utils/src/index.js"
/**
* Class to iterate line-by-line over a BGZipped text file. This class is useful for iterating from the start of
* the file. Not useful for indexed queries.
*/
class BGZLineReader {
constructor(config) {
this.config = config
this.filePtr = 0
this.bufferPtr = 0
this.buffer
}
async nextLine() {
let result = undefined
try {
while (true) {
const length = this.buffer ? this.buffer.length : 0
while (this.bufferPtr < length) {
const c = String.fromCharCode(this.buffer[this.bufferPtr++])
if (c === '\r') continue
if (c === '\n') {
return result
}
result = result ? result + c : c
}
if (this.eof) {
return result
} else {
await this.readNextBlock()
}
}
} catch (e) {
console.warn(e)
this.eof = true
return result
}
}
async readNextBlock() {
const bsizeOptions = buildOptions(this.config, {
range: {
start: this.filePtr,
size: 26
}
})
const abuffer = await igvxhr.loadArrayBuffer(this.config.url, bsizeOptions)
const bufferSize = BGZip.bgzBlockSize(abuffer)
//console.log(`next block ${this.filePtr} ${bufferSize}`);
if (bufferSize === 0) {
this.eof = true
this.buffer = undefined
} else {
const options = buildOptions(this.config, {range: {start: this.filePtr, size: bufferSize}})
const data = await igvxhr.loadArrayBuffer(this.config.url, options)
if (data.byteLength < bufferSize) {
this.eof = true // Assumption
}
this.buffer = BGZip.unbgzf(data)
this.bufferPtr = 0
this.filePtr += data.byteLength //data.byteLength;
}
}
}
export default BGZLineReader