| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- "use strict";
- // https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/taxonomic_divisions/uniprot_sprot_human.xml.gz
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __asyncValues = (this && this.__asyncValues) || function (o) {
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
- var m = o[Symbol.asyncIterator], i;
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getEnrty = exports.readOffset = exports.makeIndex = void 0;
- const fs_1 = __importDefault(require("fs"));
- const readline_1 = __importDefault(require("readline"));
- const fast_xml_parser_1 = require("fast-xml-parser");
- const line$ = (path) => readline_1.default.createInterface({
- input: fs_1.default.createReadStream(path),
- crlfDelay: Infinity
- });
- const makeIndex = (filePath, indexPath) => __awaiter(void 0, void 0, void 0, function* () {
- var e_1, _a;
- indexPath = indexPath || filePath + '.jsi';
- let byteAcc = 0;
- const fromSel = new RegExp("^<entry");
- const toSel = new RegExp("^</entry>");
- const valSel = new RegExp('<accession>');
- let tmp = { values: [] };
- try {
- for (var _b = __asyncValues(line$(filePath)), _c; _c = yield _b.next(), !_c.done;) {
- const line = _c.value;
- if (fromSel.test(line))
- tmp['from'] = byteAcc;
- byteAcc += (line.length + 1);
- if (valSel.test(line))
- tmp['values'].push(line.match("<accession>(.*?)</accession>")[1]); // 'uck
- if (toSel.test(line)) {
- yield fs_1.default.promises.appendFile(indexPath, tmp.values.join(';') + '\t' + tmp.from + '\t' + byteAcc + '\n');
- tmp = { values: [] };
- }
- }
- }
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
- finally {
- try {
- if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
- }
- finally { if (e_1) throw e_1.error; }
- }
- });
- exports.makeIndex = makeIndex;
- const readOffset = (path, from, to) => {
- return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
- const size = to - from;
- const buffer = Buffer.alloc(size);
- let filehandle = null;
- try {
- filehandle = yield fs_1.default.promises.open(path, 'r+');
- yield filehandle.read(buffer, 0, buffer.length, from);
- }
- finally {
- if (filehandle) {
- yield filehandle.close();
- resolve(buffer.toString());
- }
- }
- }));
- };
- exports.readOffset = readOffset;
- const getEntryOffset = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
- var e_2, _d;
- const indexPath = dbPath + '.jsi';
- if (!fs_1.default.existsSync(indexPath))
- yield makeIndex(dbPath);
- const lineSel = new RegExp(accession);
- try {
- for (var _e = __asyncValues(line$(indexPath)), _f; _f = yield _e.next(), !_f.done;) {
- const line = _f.value;
- if (lineSel.test(line))
- return [Number(line.split('\t')[1]), Number(line.split('\t')[2])];
- }
- }
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
- finally {
- try {
- if (_f && !_f.done && (_d = _e.return)) yield _d.call(_e);
- }
- finally { if (e_2) throw e_2.error; }
- }
- return [0, 0];
- });
- const getEnrty = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
- const parser = new fast_xml_parser_1.XMLParser({
- ignoreAttributes: false,
- alwaysCreateTextNode: false,
- attributeNamePrefix: "",
- textNodeName: "value",
- allowBooleanAttributes: true,
- });
- const offsets = yield getEntryOffset(dbPath, accession);
- return parser.parse(yield readOffset(dbPath, offsets[0], offsets[1]));
- });
- exports.getEnrty = getEnrty;
|