index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. // https://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/taxonomic_divisions/uniprot_sprot_human.xml.gz
  3. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  4. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  5. return new (P || (P = Promise))(function (resolve, reject) {
  6. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  7. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  8. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  9. step((generator = generator.apply(thisArg, _arguments || [])).next());
  10. });
  11. };
  12. var __asyncValues = (this && this.__asyncValues) || function (o) {
  13. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  14. var m = o[Symbol.asyncIterator], i;
  15. 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);
  16. 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); }); }; }
  17. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  18. };
  19. var __importDefault = (this && this.__importDefault) || function (mod) {
  20. return (mod && mod.__esModule) ? mod : { "default": mod };
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. exports.getEnrty = exports.readOffset = exports.makeIndex = void 0;
  24. const fs_1 = __importDefault(require("fs"));
  25. const readline_1 = __importDefault(require("readline"));
  26. const fast_xml_parser_1 = require("fast-xml-parser");
  27. const line$ = (path) => readline_1.default.createInterface({
  28. input: fs_1.default.createReadStream(path),
  29. crlfDelay: Infinity
  30. });
  31. const makeIndex = (filePath, indexPath) => __awaiter(void 0, void 0, void 0, function* () {
  32. var e_1, _a;
  33. indexPath = indexPath || filePath + '.jsi';
  34. let byteAcc = 0;
  35. const fromSel = new RegExp("^<entry");
  36. const toSel = new RegExp("^</entry>");
  37. const valSel = new RegExp('<accession>');
  38. let tmp = { values: [] };
  39. try {
  40. for (var _b = __asyncValues(line$(filePath)), _c; _c = yield _b.next(), !_c.done;) {
  41. const line = _c.value;
  42. if (fromSel.test(line))
  43. tmp['from'] = byteAcc;
  44. byteAcc += (line.length + 1);
  45. if (valSel.test(line))
  46. tmp['values'].push(line.match("<accession>(.*?)</accession>")[1]); // 'uck
  47. if (toSel.test(line)) {
  48. yield fs_1.default.promises.appendFile(indexPath, tmp.values.join(';') + '\t' + tmp.from + '\t' + byteAcc + '\n');
  49. tmp = { values: [] };
  50. }
  51. }
  52. }
  53. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  54. finally {
  55. try {
  56. if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
  57. }
  58. finally { if (e_1) throw e_1.error; }
  59. }
  60. });
  61. exports.makeIndex = makeIndex;
  62. const readOffset = (path, from, to) => {
  63. return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
  64. const size = to - from;
  65. const buffer = Buffer.alloc(size);
  66. let filehandle = null;
  67. try {
  68. filehandle = yield fs_1.default.promises.open(path, 'r+');
  69. yield filehandle.read(buffer, 0, buffer.length, from);
  70. }
  71. finally {
  72. if (filehandle) {
  73. yield filehandle.close();
  74. resolve(buffer.toString());
  75. }
  76. }
  77. }));
  78. };
  79. exports.readOffset = readOffset;
  80. const getEntryOffset = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
  81. var e_2, _d;
  82. const indexPath = dbPath + '.jsi';
  83. if (!fs_1.default.existsSync(indexPath))
  84. yield makeIndex(dbPath);
  85. const lineSel = new RegExp(accession);
  86. try {
  87. for (var _e = __asyncValues(line$(indexPath)), _f; _f = yield _e.next(), !_f.done;) {
  88. const line = _f.value;
  89. if (lineSel.test(line))
  90. return [Number(line.split('\t')[1]), Number(line.split('\t')[2])];
  91. }
  92. }
  93. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  94. finally {
  95. try {
  96. if (_f && !_f.done && (_d = _e.return)) yield _d.call(_e);
  97. }
  98. finally { if (e_2) throw e_2.error; }
  99. }
  100. return [0, 0];
  101. });
  102. const getEnrty = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
  103. const parser = new fast_xml_parser_1.XMLParser({
  104. ignoreAttributes: false,
  105. alwaysCreateTextNode: false,
  106. attributeNamePrefix: "",
  107. textNodeName: "value",
  108. allowBooleanAttributes: true,
  109. });
  110. const offsets = yield getEntryOffset(dbPath, accession);
  111. return parser.parse(yield readOffset(dbPath, offsets[0], offsets[1]));
  112. });
  113. exports.getEnrty = getEnrty;