index.ts 3.4 KB

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