UNPKG

@gmod/indexedfasta

Version:

read indexed fasta and bgzipped fasta formats

60 lines 2.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IndexedFasta = exports.BgzipIndexedFasta = exports.FetchableSmallFasta = void 0; exports.parseSmallFasta = parseSmallFasta; const generic_filehandle2_1 = require("generic-filehandle2"); function parseSmallFasta(text) { return text .split('>') .filter(t => /\S/.test(t)) .map(entryText => { const [defLine, ...seqLines] = entryText.split('\n'); const [id, ...description] = defLine.split(' '); const sequence = seqLines.join('').replace(/\s/g, ''); return { id: id, description: description.join(' '), sequence, }; }); } class FetchableSmallFasta { constructor({ fasta, path }) { if (fasta) { this.fasta = fasta; } else if (path) { this.fasta = new generic_filehandle2_1.LocalFile(path); } else { throw new Error('Need to pass fasta or path'); } this.data = this.fasta.readFile().then(buffer => { const decoder = new TextDecoder('utf8'); const text = decoder.decode(buffer); return parseSmallFasta(text); }); } async fetch(id, start, end) { const data = await this.data; const entry = data.find(iter => iter.id === id); const length = end - start; if (!entry) { throw new Error(`no sequence with id ${id} exists`); } return entry.sequence.slice(start, length); } async getSequenceNames() { const data = await this.data; return data.map(entry => entry.id); } } exports.FetchableSmallFasta = FetchableSmallFasta; var bgzipIndexedFasta_ts_1 = require("./bgzipIndexedFasta.js"); Object.defineProperty(exports, "BgzipIndexedFasta", { enumerable: true, get: function () { return __importDefault(bgzipIndexedFasta_ts_1).default; } }); var indexedFasta_ts_1 = require("./indexedFasta.js"); Object.defineProperty(exports, "IndexedFasta", { enumerable: true, get: function () { return __importDefault(indexedFasta_ts_1).default; } }); //# sourceMappingURL=index.js.map