index.js 4.5 KB

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