"use strict"; /* * Require in path : zgrep, grep */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.denovoAssemblage = void 0; const child_process_1 = require("child_process"); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const async_exec = (prog, args, onData, onErr) => { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(prog, args, { shell: true }); child.stdout.on('data', data => onData(data.toString().trim())); child.stderr.on('data', data => onErr(data.toString().trim())); child.on('error', err => reject(err)); child.on('exit', code => resolve(code)); }); }; const rmList = (list) => { return Promise.all(list.map(e => fs_1.default.promises.rm(e, { recursive: true }))); }; const denovoAssemblage = (reads, rnames, spadesPath) => { return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () { let readsIn = []; let isPairedEnd = false; const threads = String(os_1.default.cpus().length); if (Array.isArray(reads)) { if (reads.length > 2) { reject('Only R1 and R2 path are required'); } else { console.log('Assuming paired end reads'); isPairedEnd = true; readsIn = reads; } } else { readsIn = [reads]; } // Prepare the fastq files for spades inputs const rnamesSel = '\'' + rnames.join('\\|') + '\''; const tmpSubReads = []; for (const R of readsIn) { const tmp = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_' + path_1.default.parse(R).base); tmpSubReads.push(tmp); const greper = R.match(/gz$/) ? 'zgrep' : 'grep'; yield async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], console.log, console.log); } const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]]; const rsltDir = path_1.default.join(os_1.default.tmpdir(), (+new Date) + '_spades'); yield async_exec(spadesPath, ['-t', threads, '--isolate', ...args, '-o', rsltDir], console.log, console.log); if (!fs_1.default.existsSync(path_1.default.join(rsltDir, 'contigs.fasta'))) { yield rmList([rsltDir]); console.log('Trying meta'); yield async_exec(spadesPath, ['-t', threads, '--meta', ...args, '-o', rsltDir], console.log, console.log); if (!fs_1.default.existsSync(path_1.default.join(rsltDir, 'contigs.fasta'))) { yield rmList([rsltDir, ...tmpSubReads]); reject('No convergence'); } } yield rmList(tmpSubReads); resolve(rsltDir); })); }; exports.denovoAssemblage = denovoAssemblage; /* (async()=>{ const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py' const reads2 = '/home/thomas/Documents/Programmes/ttest/71-mer.fa' const rnames = ['33530080_3', '30971394_3', '77190111_3', '89825138_3', '22481866_3', '111937620_4', '24308941_6', '27758147_3', '87242990_14', '41688638_4', '114699822_3', '48573844_4', '91080996_3', '99644261_3', '77207124_3', '28986564_10', '84400117_4', '10884880_6', '116082011_12', '1739367_3', '13550404_3', '68446023_10', '50560660_3', '9046992_3'] console.log(await denovoAssemblage(reads2, rnames, spadesPath)); })() */