index.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __asyncValues = (this && this.__asyncValues) || function (o) {
  12. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  13. var m = o[Symbol.asyncIterator], i;
  14. 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);
  15. 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); }); }; }
  16. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  17. };
  18. var __importDefault = (this && this.__importDefault) || function (mod) {
  19. return (mod && mod.__esModule) ? mod : { "default": mod };
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.getGOterms = exports.getSymbol = void 0;
  23. const fs_1 = __importDefault(require("fs"));
  24. const readline_1 = __importDefault(require("readline"));
  25. const line$ = (path) => readline_1.default.createInterface({
  26. input: fs_1.default.createReadStream(path),
  27. crlfDelay: Infinity
  28. });
  29. // http://geneontology.org/docs/guide-go-evidence-codes/
  30. // http://wiki.geneontology.org/index.php/Category:Evidence_Codes
  31. const getSymbol = (symbol, goaPath, oboPath) => __awaiter(void 0, void 0, void 0, function* () {
  32. var e_1, _a;
  33. var _b;
  34. const header = [
  35. 'database', 'ID', 'Symbol', 'Qualifier',
  36. 'GO_Term', 'Evidence', 'Evidence_Code',
  37. 'With', 'From', 'Name', 'Alternative_symbols',
  38. 'Class', 'Taxon', 'Date', 'Origin'
  39. ];
  40. const tester = new RegExp('\t' + symbol + '\t');
  41. const separator = new RegExp('\\|');
  42. const results = [];
  43. try {
  44. for (var _c = __asyncValues(line$(goaPath)), _d; _d = yield _c.next(), !_d.done;) {
  45. const line = _d.value;
  46. if (tester.test(line))
  47. results.push(line.split('\t').reduce((p, c, i) => (Object.assign(Object.assign({}, p), { [header[i]]: separator.test(c) ? c.split('|') : c })), {}));
  48. }
  49. }
  50. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  51. finally {
  52. try {
  53. if (_d && !_d.done && (_a = _c.return)) yield _a.call(_c);
  54. }
  55. finally { if (e_1) throw e_1.error; }
  56. }
  57. const subTerms = results.map(e => e === null || e === void 0 ? void 0 : e.GO_Term);
  58. const cacheTerms = yield getGOterms(subTerms, oboPath);
  59. for (let index = 0; index < results.length; index++) {
  60. const goTerm = (_b = results[index]) === null || _b === void 0 ? void 0 : _b.GO_Term;
  61. results[index]['GO_Term'] = cacheTerms.filter(e => e.id === goTerm)[0];
  62. }
  63. return results;
  64. });
  65. exports.getSymbol = getSymbol;
  66. const getGOterms = (terms, oboPath) => __awaiter(void 0, void 0, void 0, function* () {
  67. var e_2, _e;
  68. const testerList = terms.map(e => new RegExp('id: ' + e));
  69. let delim = false;
  70. const results = [];
  71. let result = {};
  72. try {
  73. for (var _f = __asyncValues(line$(oboPath)), _g; _g = yield _f.next(), !_g.done;) {
  74. const line = _g.value;
  75. if (testerList.some(rx => rx.test(line)))
  76. delim = true;
  77. if (line === '' && delim) {
  78. delim = false;
  79. results.push(result);
  80. result = {};
  81. }
  82. if (delim)
  83. result[line.split(': ')[0]] = line.split(': ')[1];
  84. }
  85. }
  86. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  87. finally {
  88. try {
  89. if (_g && !_g.done && (_e = _f.return)) yield _e.call(_f);
  90. }
  91. finally { if (e_2) throw e_2.error; }
  92. }
  93. return results;
  94. });
  95. exports.getGOterms = getGOterms;