"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.getInteractionsFromEntry = exports.getEntryFromGeneName = 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 jsonata_1 = __importDefault(require("jsonata")); 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("^"); const valSel = new RegExp(''); 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("(.*?)")[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; 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; const getEntryFromGeneName = (idmappingPath, dbPath, geneName) => __awaiter(void 0, void 0, void 0, function* () { var e_3, _g; const sel = new RegExp('Gene_Name\t' + geneName); let accessions = []; try { for (var _h = __asyncValues(line$(idmappingPath)), _j; _j = yield _h.next(), !_j.done;) { const line = _j.value; if (sel.test(line)) accessions.push(line.split('\t')[0]); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_j && !_j.done && (_g = _h.return)) yield _g.call(_h); } finally { if (e_3) throw e_3.error; } } return yield getEnrty(dbPath, accessions[0]); }); exports.getEntryFromGeneName = getEntryFromGeneName; const getInteractionsFromEntry = (json) => __awaiter(void 0, void 0, void 0, function* () { const uniprotIDs = Array.isArray(json.entry.accession) ? json.entry.accession : [json.entry.accession]; // Comment interactant const res_inter = (0, jsonata_1.default)(`entry.comment[type="interaction"].interactant`).evaluate(json); let genes_interactant = []; if (res_inter) { genes_interactant = [...new Set(res_inter .filter((e) => !uniprotIDs.includes(e.label)) .map((e) => e.label) .filter((e) => e))]; } // Reference scope = INTERACTION WITH const scope_inter = (0, jsonata_1.default)(`entry.reference[scope ~> /INTERACTION WITH/i ]`).evaluate(json); let genes_scope_inter = []; if (scope_inter) { const comment_scope_inters = Array.isArray(scope_inter) ? scope_inter : [scope_inter]; 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))); genes_scope_inter = [...new Set(comment_scope_inters_genes.map((e) => e.interaction))].flatMap((e) => e.split(/; | AND /)); } // Comment subunit const comment_subunit = (0, jsonata_1.default)(`entry.comment[type="subunit"].text.value`).evaluate(json); let comment_subunits_genes = []; if (comment_subunit) { const comment_subunits = Array.isArray(comment_subunit) ? comment_subunit : [comment_subunit]; comment_subunits_genes = comment_subunits .flatMap((e) => e.replace(/ *\([^)]*\) */g, '').split(/\n/)) .filter((e) => /Interacts/.test(e)) .flatMap((e) => e.match(/ [A-Z][A-Z0-9\-]{2,}/g)); } let res = [...new Set([...genes_scope_inter, ...genes_interactant, ...comment_subunits_genes])].sort().filter((e) => typeof e === 'string').filter(_ => _); let filterOut = ['PHOSPHOSERINE', 'MOTIFS', 'INFECTION', 'PROTEIN', 'PROTEINS', 'MICROBIAL', 'VIRUS', 'HEPATITIS', 'HERPES', 'SIMPLEX', 'RELATED', 'ARE-']; if (res.length > 0) res = res.flatMap((e) => e.match(/[A-Z]{2,}[A-Z0-9\-]{1,}/g)).filter((e) => !(filterOut.includes(e))).filter((e) => !/-$/.test(e)); return [...new Set(res.filter(_ => _))]; }); exports.getInteractionsFromEntry = getInteractionsFromEntry;