index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 isDone = (rsltDir:string) => {
  21. let done = false
  22. if(fs.existsSync(path.join(rsltDir, 'contigs.fasta'))) {
  23. if(fs.statSync(path.join(rsltDir, 'contigs.fasta')).size > 1) {
  24. done = true
  25. }
  26. }
  27. return done
  28. }
  29. const denovoAssemblage = (
  30. reads : string | string[],
  31. rnames: string[],
  32. spadesPath: string
  33. ) => {
  34. return new Promise<string>(async (resolve, reject) => {
  35. let readsIn: string[] = []
  36. let isPairedEnd = false
  37. const threads = String(os.cpus().length)
  38. if (Array.isArray(reads)) {
  39. if (reads.length > 2) {
  40. reject('Only R1 and R2 path are required')
  41. } else {
  42. console.log('Assuming paired end reads');
  43. isPairedEnd = true
  44. readsIn = reads
  45. }
  46. } else {
  47. readsIn = [reads]
  48. }
  49. // Prepare the fastq files for spades inputs
  50. const rnamesSel = '\'' + rnames.join('\\|') + '\''
  51. const tmpSubReads: string[] = []
  52. let isfq = []
  53. for (const R of readsIn) {
  54. const parsedPath = path.parse(R)
  55. const greper = parsedPath.ext === '.gz' ? 'zgrep' : 'grep'
  56. isfq.push(R.match(/\.fq/) ? true : false)
  57. const tmp = path.join(os.tmpdir(), (+new Date) + '_' + parsedPath.name + (isfq ? '.fq' : '.fa'))
  58. tmpSubReads.push(tmp)
  59. await async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], console.log, console.log)
  60. }
  61. const correction = (isfq.filter(e=>e).length !== isfq.length || isfq.filter(e=>e).length === 0) ? '--only-assembler' : ''
  62. const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]]
  63. const rsltDir = path.join(os.tmpdir(), (+new Date) + '_spades')
  64. await async_exec(spadesPath, ['-t', threads, '--isolate', correction, ...args, '-o', rsltDir], console.log, console.log)
  65. if(!isDone(rsltDir)) {
  66. await rmList([rsltDir])
  67. console.log('Trying meta')
  68. await async_exec(spadesPath, ['-t', threads, '--meta', correction, ...args, '-o', rsltDir], console.log, console.log)
  69. if(!isDone(rsltDir)) {
  70. await rmList([rsltDir, ...tmpSubReads])
  71. reject('No convergence')
  72. }
  73. }
  74. await rmList(tmpSubReads)
  75. resolve(rsltDir)
  76. })
  77. }
  78. export { denovoAssemblage }
  79. /*
  80. (async()=>{
  81. const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py'
  82. const reads2 = '/home/thomas/Documents/Programmes/ttest/71-mer.fa'
  83. const rnames = ['33530080_3', '30971394_3', '77190111_3', '89825138_3',
  84. '22481866_3', '111937620_4', '24308941_6', '27758147_3',
  85. '87242990_14', '41688638_4', '114699822_3', '48573844_4',
  86. '91080996_3', '99644261_3', '77207124_3', '28986564_10',
  87. '84400117_4', '10884880_6', '116082011_12', '1739367_3',
  88. '13550404_3', '68446023_10', '50560660_3', '9046992_3']
  89. console.log(await denovoAssemblage(reads2, rnames, spadesPath));
  90. })()
  91. */