index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. log: Function
  34. ) => {
  35. return new Promise<string>(async (resolve, reject) => {
  36. let readsIn: string[] = []
  37. let isPairedEnd = false
  38. const threads = String(os.cpus().length)
  39. if (Array.isArray(reads)) {
  40. if (reads.length > 2) {
  41. reject('Only R1 and R2 path are required')
  42. } else {
  43. console.log('Assuming paired end reads');
  44. isPairedEnd = true
  45. readsIn = reads
  46. }
  47. } else {
  48. readsIn = [reads]
  49. }
  50. // Prepare the fastq files for spades inputs
  51. const rnamesSel = '\'' + rnames.join('\\|') + '\''
  52. const tmpSubReads: string[] = []
  53. let isfq: boolean[] = []
  54. for (const R of readsIn) {
  55. const parsedPath = path.parse(R)
  56. const greper = parsedPath.ext === '.gz' ? 'zgrep' : 'grep'
  57. isfq.push(R.match(/\.fq/) ? true : false)
  58. const tmp = path.join(os.tmpdir(), (+new Date) + '_' + parsedPath.name + (isfq[isfq.length-1] ? '.fq' : '.fa'))
  59. tmpSubReads.push(tmp)
  60. await async_exec(greper, [rnamesSel, R, '-A3', '--no-group-separator', '>', tmp], log, log)
  61. }
  62. const correction = (isfq.filter(e=>e).length !== isfq.length || isfq.filter(e=>e).length === 0) ? '--only-assembler' : ''
  63. const args = isPairedEnd ? ['-1', tmpSubReads[0], '-2', tmpSubReads[1]] : ['-s', tmpSubReads[0]]
  64. const rsltDir = path.join(os.tmpdir(), (+new Date) + '_spades')
  65. await async_exec(spadesPath, ['-t', threads, '--isolate', correction, ...args, '-o', rsltDir], log, log)
  66. if(!isDone(rsltDir) && isPairedEnd) {
  67. await rmList([rsltDir])
  68. console.log('Trying meta')
  69. await async_exec(spadesPath, ['-t', threads, '--meta', correction, ...args, '-o', rsltDir], log, log)
  70. if(!isDone(rsltDir)) {
  71. await rmList([rsltDir, ...tmpSubReads])
  72. reject('No convergence')
  73. }
  74. } else if (!isDone(rsltDir) && !isPairedEnd) {
  75. await rmList([rsltDir, ...tmpSubReads])
  76. reject('No convergence')
  77. } else if (isDone(rsltDir)) {
  78. await rmList(tmpSubReads)
  79. resolve(rsltDir)
  80. }
  81. })
  82. }
  83. export { denovoAssemblage }
  84. /*
  85. (async()=>{
  86. const spadesPath = '/home/thomas/NGS/tools/SPAdes-3.15.0-Linux/bin/spades.py'
  87. const reads2 = ['/home/thomas/Documents/Programmes/ttest/R1r.fq', '/home/thomas/Documents/Programmes/ttest/R2r.fq']
  88. const rnames = [
  89. 'A00680:166:HYCYGDMXX:1:1356:7663:10723',
  90. 'A00680:166:HYCYGDMXX:1:2475:28284:20384',
  91. 'A00680:166:HYCYGDMXX:1:1274:10646:6339',
  92. 'A00680:166:HYCYGDMXX:1:1314:10303:30921',
  93. 'A00680:166:HYCYGDMXX:1:1365:11966:21277',
  94. 'A00680:166:HYCYGDMXX:1:1425:21151:6778',
  95. 'A00680:166:HYCYGDMXX:1:2288:25301:5822',
  96. 'A00680:166:HYCYGDMXX:1:2312:24758:12587',
  97. 'A00680:166:HYCYGDMXX:1:2410:17110:13808',
  98. 'A00680:166:HYCYGDMXX:1:2410:17761:15468',
  99. 'A00680:166:HYCYGDMXX:1:2425:19777:2237',
  100. 'A00680:166:HYCYGDMXX:1:2429:13575:20149',
  101. 'A00680:166:HYCYGDMXX:2:1374:20971:34773',
  102. 'A00680:166:HYCYGDMXX:2:2350:16920:14293',
  103. 'A00680:166:HYCYGDMXX:1:2274:12707:16736',
  104. 'A00680:166:HYCYGDMXX:1:2274:12717:16720',
  105. 'A00680:166:HYCYGDMXX:2:1329:3839:27367',
  106. 'A00680:166:HYCYGDMXX:2:1330:1416:7827'
  107. ]
  108. console.log(await denovoAssemblage(reads2, rnames, spadesPath, console.log));
  109. })()
  110. */