index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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) => {
  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] ? '.fq' : '.fa'));
  71. tmpSubReads.push(tmp);
  72. yield async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], console.log, console.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], console.log, console.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], console.log, console.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/71-mer.fa'
  102. const rnames = ['33530080_3', '30971394_3', '77190111_3', '89825138_3',
  103. '22481866_3', '111937620_4', '24308941_6', '27758147_3',
  104. '87242990_14', '41688638_4', '114699822_3', '48573844_4',
  105. '91080996_3', '99644261_3', '77207124_3', '28986564_10',
  106. '84400117_4', '10884880_6', '116082011_12', '1739367_3',
  107. '13550404_3', '68446023_10', '50560660_3', '9046992_3']
  108. console.log(await denovoAssemblage(reads2, rnames, spadesPath));
  109. })()
  110. */