index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.getInteractionsFromEntry = exports.getEntryFromGeneName = 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 jsonata_1 = __importDefault(require("jsonata"));
  28. const line$ = (path) => readline_1.default.createInterface({
  29. input: fs_1.default.createReadStream(path),
  30. crlfDelay: Infinity
  31. });
  32. const makeIndex = (filePath, indexPath) => __awaiter(void 0, void 0, void 0, function* () {
  33. var e_1, _a;
  34. indexPath = indexPath || filePath + '.jsi';
  35. let byteAcc = 0;
  36. const fromSel = new RegExp("^<entry");
  37. const toSel = new RegExp("^</entry>");
  38. const valSel = new RegExp('<accession>');
  39. let tmp = { values: [] };
  40. try {
  41. for (var _b = __asyncValues(line$(filePath)), _c; _c = yield _b.next(), !_c.done;) {
  42. const line = _c.value;
  43. if (fromSel.test(line))
  44. tmp['from'] = byteAcc;
  45. byteAcc += (line.length + 1);
  46. if (valSel.test(line))
  47. tmp['values'].push(line.match("<accession>(.*?)</accession>")[1]); // 'uck
  48. if (toSel.test(line)) {
  49. yield fs_1.default.promises.appendFile(indexPath, tmp.values.join(';') + '\t' + tmp.from + '\t' + byteAcc + '\n');
  50. tmp = { values: [] };
  51. }
  52. }
  53. }
  54. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  55. finally {
  56. try {
  57. if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
  58. }
  59. finally { if (e_1) throw e_1.error; }
  60. }
  61. });
  62. exports.makeIndex = makeIndex;
  63. const readOffset = (path, from, to) => {
  64. return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
  65. const size = to - from;
  66. const buffer = Buffer.alloc(size);
  67. let filehandle = null;
  68. try {
  69. filehandle = yield fs_1.default.promises.open(path, 'r+');
  70. yield filehandle.read(buffer, 0, buffer.length, from);
  71. }
  72. finally {
  73. if (filehandle) {
  74. yield filehandle.close();
  75. resolve(buffer.toString());
  76. }
  77. }
  78. }));
  79. };
  80. exports.readOffset = readOffset;
  81. const getEntryOffset = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
  82. var e_2, _d;
  83. const indexPath = dbPath + '.jsi';
  84. if (!fs_1.default.existsSync(indexPath))
  85. yield makeIndex(dbPath);
  86. const lineSel = new RegExp(accession);
  87. try {
  88. for (var _e = __asyncValues(line$(indexPath)), _f; _f = yield _e.next(), !_f.done;) {
  89. const line = _f.value;
  90. if (lineSel.test(line))
  91. return [Number(line.split('\t')[1]), Number(line.split('\t')[2])];
  92. }
  93. }
  94. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  95. finally {
  96. try {
  97. if (_f && !_f.done && (_d = _e.return)) yield _d.call(_e);
  98. }
  99. finally { if (e_2) throw e_2.error; }
  100. }
  101. return [0, 0];
  102. });
  103. const getEnrty = (dbPath, accession) => __awaiter(void 0, void 0, void 0, function* () {
  104. const parser = new fast_xml_parser_1.XMLParser({
  105. ignoreAttributes: false,
  106. alwaysCreateTextNode: false,
  107. attributeNamePrefix: "",
  108. textNodeName: "value",
  109. allowBooleanAttributes: true,
  110. });
  111. const offsets = yield getEntryOffset(dbPath, accession);
  112. return parser.parse(yield readOffset(dbPath, offsets[0], offsets[1]));
  113. });
  114. exports.getEnrty = getEnrty;
  115. const getEntryFromGeneName = (idmappingPath, dbPath, geneName) => __awaiter(void 0, void 0, void 0, function* () {
  116. var e_3, _g;
  117. const sel = new RegExp('Gene_Name\t' + geneName);
  118. let accessions = [];
  119. try {
  120. for (var _h = __asyncValues(line$(idmappingPath)), _j; _j = yield _h.next(), !_j.done;) {
  121. const line = _j.value;
  122. if (sel.test(line))
  123. accessions.push(line.split('\t')[0]);
  124. }
  125. }
  126. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  127. finally {
  128. try {
  129. if (_j && !_j.done && (_g = _h.return)) yield _g.call(_h);
  130. }
  131. finally { if (e_3) throw e_3.error; }
  132. }
  133. return yield getEnrty(dbPath, accessions[0]);
  134. });
  135. exports.getEntryFromGeneName = getEntryFromGeneName;
  136. const getInteractionsFromEntry = (json) => __awaiter(void 0, void 0, void 0, function* () {
  137. const uniprotIDs = Array.isArray(json.entry.accession) ? json.entry.accession : [json.entry.accession];
  138. // Comment interactant
  139. const res_inter = (0, jsonata_1.default)(`entry.comment[type="interaction"].interactant`).evaluate(json);
  140. let genes_interactant = [];
  141. if (res_inter) {
  142. genes_interactant = [...new Set(res_inter
  143. .filter((e) => !uniprotIDs.includes(e.label))
  144. .map((e) => e.label)
  145. .filter((e) => e))];
  146. }
  147. // Reference scope = INTERACTION WITH
  148. const scope_inter = (0, jsonata_1.default)(`entry.reference[scope ~> /INTERACTION WITH/i ]`).evaluate(json);
  149. let genes_scope_inter = [];
  150. if (scope_inter) {
  151. const comment_scope_inters = Array.isArray(scope_inter) ? scope_inter : [scope_inter];
  152. const comment_scope_inters_genes = comment_scope_inters.map((e) => (Object.assign({ txt: Array.isArray(e.scope) ? e.scope.filter((ee) => ee.match(/INTERACTION\ WITH/)).join() : e.scope }, e))).map((e) => (Object.assign({ interaction: e.txt.substring(e.txt.indexOf("INTERACTION WITH ") + "INTERACTION WITH ".length, e.txt.length) }, e)));
  153. genes_scope_inter = [...new Set(comment_scope_inters_genes.map((e) => e.interaction))].flatMap((e) => e.split(/; | AND /));
  154. }
  155. // Comment subunit
  156. const comment_subunit = (0, jsonata_1.default)(`entry.comment[type="subunit"].text.value`).evaluate(json);
  157. let comment_subunits_genes = [];
  158. if (comment_subunit) {
  159. const comment_subunits = Array.isArray(comment_subunit) ? comment_subunit : [comment_subunit];
  160. comment_subunits_genes = comment_subunits
  161. .flatMap((e) => e.replace(/ *\([^)]*\) */g, '').split(/\n/))
  162. .filter((e) => /Interacts/.test(e))
  163. .flatMap((e) => e.match(/[A-Z][A-Z0-9]{2,}/g));
  164. }
  165. let res = [...new Set([...genes_scope_inter, ...genes_interactant, ...comment_subunits_genes])].sort().filter((e) => typeof e === 'string').filter(_ => _);
  166. console.log(res);
  167. if (res.length > 0)
  168. res = res.flatMap((e) => e.match(/[A-Z][A-Z0-9]{2,}/g)).filter(_ => _);
  169. return res;
  170. });
  171. exports.getInteractionsFromEntry = getInteractionsFromEntry;