index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. /*
  3. * Require in path : zgrep, grep
  4. */
  5. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. var __importDefault = (this && this.__importDefault) || function (mod) {
  15. return (mod && mod.__esModule) ? mod : { "default": mod };
  16. };
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.denovoAssemblage = void 0;
  19. const child_process_1 = require("child_process");
  20. const os_1 = __importDefault(require("os"));
  21. const path_1 = __importDefault(require("path"));
  22. const fs_1 = __importDefault(require("fs"));
  23. const async_exec = (prog, args, onData, onErr) => {
  24. return new Promise((resolve, reject) => {
  25. const child = (0, child_process_1.spawn)(prog, args, { shell: true });
  26. child.stdout.on('data', data => onData(data.toString().trim()));
  27. child.stderr.on('data', data => onErr(data.toString().trim()));
  28. child.on('error', err => reject(err));
  29. child.on('exit', code => resolve(code));
  30. });
  31. };
  32. const rmList = (list) => {
  33. return Promise.all(list.map(e => fs_1.default.promises.rm(e, { recursive: true })));
  34. };
  35. const isDone = (rsltDir) => {
  36. let done = false;
  37. if (fs_1.default.existsSync(path_1.default.join(rsltDir, 'contigs.fasta'))) {
  38. if (fs_1.default.statSync(path_1.default.join(rsltDir, 'contigs.fasta')).size > 1) {
  39. done = true;
  40. }
  41. }
  42. return done;
  43. };
  44. const denovoAssemblage = (reads, rnames, spadesPath, log) => {
  45. return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
  46. let readsIn = [];
  47. let isPairedEnd = false;
  48. const threads = String(os_1.default.cpus().length);
  49. if (Array.isArray(reads)) {
  50. if (reads.length > 2) {
  51. reject('Only R1 and R2 path are required');
  52. }
  53. else {
  54. console.log('Assuming paired end reads');
  55. isPairedEnd = true;
  56. readsIn = reads;
  57. }
  58. }
  59. else {
  60. readsIn = [reads];
  61. }
  62. // Prepare the fastq files for spades inputs
  63. const rnamesSel = '\'' + rnames.join('\\|') + '\'';
  64. const tmpSubReads = [];
  65. let isfq = [];
  66. for (const R of readsIn) {
  67. const parsedPath = path_1.default.parse(R);
  68. const greper = parsedPath.ext === '.gz' ? 'zgrep' : 'grep';
  69. isfq.push(R.match(/\.fq/) ? true : false);
  70. const tmp = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_' + parsedPath.name + (isfq[isfq.length - 1] ? '.fq' : '.fa'));
  71. tmpSubReads.push(tmp);
  72. yield async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], log, log);
  73. }
  74. const correction = (isfq.filter(e => e).length !== isfq.length || isfq.filter(e => e).length === 0) ? '--only-assembler' : '';
  75. const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]];
  76. const rsltDir = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_spades');
  77. yield async_exec(spadesPath, ['-t', threads, '--isolate', correction, ...args, '-o', rsltDir], log, log);
  78. if (!isDone(rsltDir) && isPairedEnd) {
  79. yield rmList([rsltDir]);
  80. console.log('Trying meta');
  81. yield async_exec(spadesPath, ['-t', threads, '--meta', correction, ...args, '-o', rsltDir], log, log);
  82. if (!isDone(rsltDir)) {
  83. yield rmList([rsltDir, ...tmpSubReads]);
  84. reject('No convergence');
  85. }
  86. }
  87. else if (!isDone(rsltDir) && !isPairedEnd) {
  88. yield rmList([rsltDir, ...tmpSubReads]);
  89. reject('No convergence');
  90. }
  91. else if (isDone(rsltDir)) {
  92. yield rmList(tmpSubReads);
  93. resolve(rsltDir);
  94. }
  95. }));
  96. };
  97. exports.denovoAssemblage = denovoAssemblage;
  98. /*
  99. (async()=>{
  100. const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py'
  101. const reads2 = ['/home/thomas/Documents/Programmes/ttest/R1r.fq', '/home/thomas/Documents/Programmes/ttest/R2r.fq']
  102. const rnames = [
  103. 'A00680:166:HYCYGDMXX:1:1356:7663:10723',
  104. 'A00680:166:HYCYGDMXX:1:2475:28284:20384',
  105. 'A00680:166:HYCYGDMXX:1:1274:10646:6339',
  106. 'A00680:166:HYCYGDMXX:1:1314:10303:30921',
  107. 'A00680:166:HYCYGDMXX:1:1365:11966:21277',
  108. 'A00680:166:HYCYGDMXX:1:1425:21151:6778',
  109. 'A00680:166:HYCYGDMXX:1:2288:25301:5822',
  110. 'A00680:166:HYCYGDMXX:1:2312:24758:12587',
  111. 'A00680:166:HYCYGDMXX:1:2410:17110:13808',
  112. 'A00680:166:HYCYGDMXX:1:2410:17761:15468',
  113. 'A00680:166:HYCYGDMXX:1:2425:19777:2237',
  114. 'A00680:166:HYCYGDMXX:1:2429:13575:20149',
  115. 'A00680:166:HYCYGDMXX:2:1374:20971:34773',
  116. 'A00680:166:HYCYGDMXX:2:2350:16920:14293',
  117. 'A00680:166:HYCYGDMXX:1:2274:12707:16736',
  118. 'A00680:166:HYCYGDMXX:1:2274:12717:16720',
  119. 'A00680:166:HYCYGDMXX:2:1329:3839:27367',
  120. 'A00680:166:HYCYGDMXX:2:1330:1416:7827'
  121. ]
  122. console.log(await denovoAssemblage(reads2, rnames, spadesPath, console.log));
  123. })()
  124. */