GoToNeo4j.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __asyncValues = (this && this.__asyncValues) || function (o) {
  35. if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
  36. var m = o[Symbol.asyncIterator], i;
  37. 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);
  38. 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); }); }; }
  39. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  40. };
  41. var __importDefault = (this && this.__importDefault) || function (mod) {
  42. return (mod && mod.__esModule) ? mod : { "default": mod };
  43. };
  44. Object.defineProperty(exports, "__esModule", { value: true });
  45. const fs_1 = __importDefault(require("fs"));
  46. const readline_1 = __importDefault(require("readline"));
  47. const neo4j = __importStar(require("neo4j-driver"));
  48. const line$ = (path) => readline_1.default.createInterface({
  49. input: fs_1.default.createReadStream(path),
  50. crlfDelay: Infinity
  51. });
  52. const templateGTnode = (args) => {
  53. let tmp = 'CREATE (gt:GoTerm {';
  54. Object.keys(args).forEach((dd, ii) => {
  55. const delim = ii === 0 ? '' : ', ';
  56. tmp += `${delim}${dd}: "${args[dd].replace(/"/g, '')}"`;
  57. });
  58. tmp += '})';
  59. return tmp;
  60. };
  61. const templateEdge = (from, to) => {
  62. return `MATCH (from:GoTerm {id: '${from}'})
  63. MATCH (to:GoTerm {id: '${to}'})
  64. MERGE (from)-[rel:is_a]->(to)`;
  65. };
  66. const templateSymbol = (args) => {
  67. let tmp = 'CREATE (sy:Symbol {';
  68. Object.keys(args).forEach((dd, ii) => {
  69. const delim = ii === 0 ? '' : ', ';
  70. tmp += `${delim}${dd}: "${args[dd]}"`;
  71. });
  72. tmp += `})`;
  73. return tmp;
  74. };
  75. const templateEdgeGoa = (fromSymbol, toGoTerm, relName, args) => {
  76. const tmpArgs = Object.keys(args).reduce((p, c) => p += `${c}: "${args[c]}", `, "").slice(0, -2);
  77. return `MATCH (from:Symbol {name: '${fromSymbol}'})
  78. MATCH (to:GoTerm {id: '${toGoTerm}'})
  79. MERGE (from)-[rel:${relName} {${tmpArgs}}]->(to)`;
  80. };
  81. const readObo = (oboPath) => __awaiter(void 0, void 0, void 0, function* () {
  82. var e_1, _a;
  83. let delim = false;
  84. const vertexes = [];
  85. const edges = [];
  86. let result = {};
  87. try {
  88. for (var _b = __asyncValues(line$(oboPath)), _c; _c = yield _b.next(), !_c.done;) {
  89. const line = _c.value;
  90. if (line === '[Term]') {
  91. delim = true;
  92. }
  93. else if (line === '' && delim) {
  94. delim = false;
  95. vertexes.push(templateGTnode(result));
  96. if (result === null || result === void 0 ? void 0 : result.is_a) {
  97. if (/^GO:[0-9]*/.test(result.is_a)) {
  98. edges.push(templateEdge(result.id, result.is_a.match(/^GO:[0-9]*/)[0]));
  99. }
  100. }
  101. result = {};
  102. }
  103. else if (delim)
  104. result[line.split(': ')[0]] = line.split(': ')[1].replace("\"", "");
  105. }
  106. }
  107. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  108. finally {
  109. try {
  110. if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
  111. }
  112. finally { if (e_1) throw e_1.error; }
  113. }
  114. return { vertexes, edges };
  115. });
  116. const readGoa = (goaPath) => __awaiter(void 0, void 0, void 0, function* () {
  117. var e_2, _d;
  118. const header = [
  119. 'database', 'ID', 'Symbol', 'Qualifier',
  120. 'GO_Term', 'Evidence', 'Evidence_Code',
  121. 'With', 'From', 'Name', 'Alternative_symbols',
  122. 'Class', 'Taxon', 'Date', 'Origin'
  123. ];
  124. const vertexes = [];
  125. const edges = [];
  126. const allSymbols = {};
  127. try {
  128. for (var _e = __asyncValues(line$(goaPath)), _f; _f = yield _e.next(), !_f.done;) {
  129. const line = _f.value;
  130. if (/^[^!]/.test(line)) {
  131. const obj = line.split('\t').filter((e) => e).reduce((p, c, i) => (Object.assign(Object.assign({}, p), { [header[i]]: /*separator.test(c) ? c.split('|') :*/ c })), {});
  132. if (typeof allSymbols[obj.Symbol] === 'undefined') {
  133. vertexes.push(templateSymbol({
  134. name: obj.Symbol,
  135. class: obj.Class,
  136. fullName: obj.Name,
  137. alternativeName: obj.Alternative_symbols,
  138. taxon: obj.Taxon,
  139. goaID: obj.ID,
  140. goaDB: obj.Origin
  141. }));
  142. allSymbols[obj.Symbol] = '';
  143. }
  144. edges.push(templateEdgeGoa(obj.Symbol, obj.GO_Term, obj.Qualifier.replace("|", "_or_"), {
  145. goaEvidence: obj.Evidence,
  146. goaEvidenceCode: obj.Evidence_Code,
  147. goaWith: obj.With,
  148. goaFrom: obj.From,
  149. goaDate: obj.Date
  150. }));
  151. }
  152. }
  153. }
  154. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  155. finally {
  156. try {
  157. if (_f && !_f.done && (_d = _e.return)) yield _d.call(_e);
  158. }
  159. finally { if (e_2) throw e_2.error; }
  160. }
  161. return { vertexes, edges };
  162. });
  163. const runNeo = (session, sql) => {
  164. return new Promise((resolve, reject) => {
  165. try {
  166. session.run(sql).then((result) => resolve(result)).catch((err) => reject(err));
  167. }
  168. catch (error) {
  169. reject(error);
  170. }
  171. });
  172. };
  173. (() => __awaiter(void 0, void 0, void 0, function* () {
  174. const oboPath = '/home/thomas/NGS/ref/GO/go-basic.obo';
  175. const goaPath = '/home/thomas/NGS/ref/GO/goa_human.gaf';
  176. var driver = neo4j.driver('neo4j://localhost', neo4j.auth.basic('neo4j', '123456'));
  177. var session = driver.session();
  178. // OBO
  179. /*
  180. const rr = await readObo(oboPath)
  181. for (const v of rr.vertexes) {
  182. console.log('Inserting OBO vertexes...');
  183. await runNeo(session, v)
  184. }
  185. for (const e of rr.edges) {
  186. console.log('Inserting OBO edges...');
  187. await runNeo(session, e)
  188. }
  189. */
  190. // GOA
  191. const goaAll = yield readGoa(goaPath);
  192. console.log(goaAll.edges[0]);
  193. // for (const v of goaAll.vertexes) {
  194. // console.log('Inserting GOA vertexes...');
  195. // await runNeo(session, v)
  196. // }
  197. for (const e of goaAll.edges) {
  198. // console.log('Inserting GOA edges...');
  199. yield runNeo(session, e);
  200. }
  201. yield driver.close();
  202. }))();