/* * Require in path : zgrep, grep */ import { spawn } from 'child_process'; import os from 'os'; import path from 'path' import fs from 'fs' const async_exec = (prog: string, args: string[], onData: Function, onErr: Function) => { return new Promise((resolve, reject) => { const child = 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: string[]) => { return Promise.all(list.map(e => fs.promises.rm(e, {recursive: true}))) } const isDone = (rsltDir:string) => { let done = false if(fs.existsSync(path.join(rsltDir, 'contigs.fasta'))) { if(fs.statSync(path.join(rsltDir, 'contigs.fasta')).size > 1) { done = true } } return done } const denovoAssemblage = ( reads : string | string[], rnames: string[], spadesPath: string, log: Function ) => { return new Promise(async (resolve, reject) => { let readsIn: string[] = [] let isPairedEnd = false const threads = String(os.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: string[] = [] let isfq: boolean[] = [] for (const R of readsIn) { const parsedPath = path.parse(R) const greper = parsedPath.ext === '.gz' ? 'zgrep' : 'grep' isfq.push(R.match(/\.fq/) ? true : false) const tmp = path.join(os.tmpdir(), (+new Date) + '_' + parsedPath.name + (isfq[isfq.length-1] ? '.fq' : '.fa')) tmpSubReads.push(tmp) await async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], log, log) } const correction = (isfq.filter(e=>e).length !== isfq.length || isfq.filter(e=>e).length === 0) ? '--only-assembler' : '' const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]] const rsltDir = path.join(os.tmpdir(), (+new Date) + '_spades') await async_exec(spadesPath, ['-t', threads, '--isolate', correction, ...args, '-o', rsltDir], log, log) if(!isDone(rsltDir) && isPairedEnd) { await rmList([rsltDir]) console.log('Trying meta') await async_exec(spadesPath, ['-t', threads, '--meta', correction, ...args, '-o', rsltDir], log, log) if(!isDone(rsltDir)) { await rmList([rsltDir, ...tmpSubReads]) reject('No convergence') } } else if (!isDone(rsltDir) && !isPairedEnd) { await rmList([rsltDir, ...tmpSubReads]) reject('No convergence') } else if (isDone(rsltDir)) { await rmList(tmpSubReads) resolve(rsltDir) } }) } export { denovoAssemblage } /* (async()=>{ const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py' const reads2 = ['/home/thomas/Documents/Programmes/ttest/R1r.fq', '/home/thomas/Documents/Programmes/ttest/R2r.fq'] const rnames = [ 'A00680:166:HYCYGDMXX:1:1356:7663:10723', 'A00680:166:HYCYGDMXX:1:2475:28284:20384', 'A00680:166:HYCYGDMXX:1:1274:10646:6339', 'A00680:166:HYCYGDMXX:1:1314:10303:30921', 'A00680:166:HYCYGDMXX:1:1365:11966:21277', 'A00680:166:HYCYGDMXX:1:1425:21151:6778', 'A00680:166:HYCYGDMXX:1:2288:25301:5822', 'A00680:166:HYCYGDMXX:1:2312:24758:12587', 'A00680:166:HYCYGDMXX:1:2410:17110:13808', 'A00680:166:HYCYGDMXX:1:2410:17761:15468', 'A00680:166:HYCYGDMXX:1:2425:19777:2237', 'A00680:166:HYCYGDMXX:1:2429:13575:20149', 'A00680:166:HYCYGDMXX:2:1374:20971:34773', 'A00680:166:HYCYGDMXX:2:2350:16920:14293', 'A00680:166:HYCYGDMXX:1:2274:12707:16736', 'A00680:166:HYCYGDMXX:1:2274:12717:16720', 'A00680:166:HYCYGDMXX:2:1329:3839:27367', 'A00680:166:HYCYGDMXX:2:1330:1416:7827' ] console.log(await denovoAssemblage(reads2, rnames, spadesPath, console.log)); })() */