| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- /*
- * 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
- ) => {
- return new Promise<string>(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 = []
-
- 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] ? '.fq' : '.fa'))
- tmpSubReads.push(tmp)
- await async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], console.log, console.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], console.log, console.log)
- if(!isDone(rsltDir) && isPairedEnd) {
- await rmList([rsltDir])
- console.log('Trying meta')
- await async_exec(spadesPath, ['-t', threads, '--meta', correction, ...args, '-o', rsltDir], console.log, console.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/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));
- })()
- */
|