index.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 denovoAssemblage = (reads, rnames, spadesPath) => {
  36. return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
  37. let readsIn = [];
  38. let isPairedEnd = false;
  39. const threads = String(os_1.default.cpus().length);
  40. if (Array.isArray(reads)) {
  41. if (reads.length > 2) {
  42. reject('Only R1 and R2 path are required');
  43. }
  44. else {
  45. console.log('Assuming paired end reads');
  46. isPairedEnd = true;
  47. readsIn = reads;
  48. }
  49. }
  50. else {
  51. readsIn = [reads];
  52. }
  53. // Prepare the fastq files for spades inputs
  54. const rnamesSel = '\'' + rnames.join('\\|') + '\'';
  55. const tmpSubReads = [];
  56. for (const R of readsIn) {
  57. const tmp = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_' + path_1.default.parse(R).base);
  58. tmpSubReads.push(tmp);
  59. const greper = R.match(/gz$/) ? 'zgrep' : 'grep';
  60. yield async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], console.log, console.log);
  61. }
  62. const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]];
  63. const rsltDir = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_spades');
  64. yield async_exec(spadesPath, ['-t', threads, '--isolate', ...args, '-o', rsltDir], console.log, console.log);
  65. if (!fs_1.default.existsSync(path_1.default.join(rsltDir, 'contigs.fasta'))) {
  66. yield rmList([rsltDir]);
  67. console.log('Trying meta');
  68. yield async_exec(spadesPath, ['-t', threads, '--meta', ...args, '-o', rsltDir], console.log, console.log);
  69. if (!fs_1.default.existsSync(path_1.default.join(rsltDir, 'contigs.fasta'))) {
  70. yield rmList([rsltDir, ...tmpSubReads]);
  71. reject('No convergence');
  72. }
  73. }
  74. yield rmList(tmpSubReads);
  75. resolve(rsltDir);
  76. }));
  77. };
  78. exports.denovoAssemblage = denovoAssemblage;
  79. /*
  80. (async()=>{
  81. const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py'
  82. const reads2 = '/home/thomas/Documents/Programmes/ttest/71-mer.fa'
  83. const rnames = ['33530080_3', '30971394_3', '77190111_3', '89825138_3',
  84. '22481866_3', '111937620_4', '24308941_6', '27758147_3',
  85. '87242990_14', '41688638_4', '114699822_3', '48573844_4',
  86. '91080996_3', '99644261_3', '77207124_3', '28986564_10',
  87. '84400117_4', '10884880_6', '116082011_12', '1739367_3',
  88. '13550404_3', '68446023_10', '50560660_3', '9046992_3']
  89. console.log(await denovoAssemblage(reads2, rnames, spadesPath));
  90. })()
  91. */