index.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { spawn } from 'child_process';
  2. import { cpus } from 'os';
  3. import fs from 'fs'
  4. import path from 'path';
  5. const async_exec = (prog: string, args: string[], onData: Function) => {
  6. return new Promise((resolve, reject) => {
  7. const child = spawn(prog, args, {shell: true})
  8. child.stdout.on('data', data => onData(data.toString().trim()))
  9. child.stderr.on('data', data => onData(data.toString().trim()))
  10. child.on('error', err => reject(err))
  11. child.on('exit', code => resolve(code))
  12. })
  13. }
  14. const makeReference = async (sequenceName:string, sequence:string, filePath: string) => {
  15. return new Promise<boolean>(async(resolve, reject) => {
  16. try {
  17. await fs.promises.writeFile(filePath, '>'+sequenceName+'\n'+sequence)
  18. await async_exec('bwa', ['index',filePath], () => console.log)
  19. resolve(true)
  20. } catch (error) {
  21. console.log(error)
  22. reject(false)
  23. }
  24. })
  25. }
  26. const asyncBwaMem = (
  27. refPath : string,
  28. reads : string | Array<string> | Array<Array<string>>,
  29. // R1 : string | Array<string>,
  30. // R2 : string | Array<string>,
  31. runName : string,
  32. libName : string,
  33. outputDir : string,
  34. onData : Function,
  35. options? : any,
  36. ) => {
  37. return new Promise<string[]>(async (resolve, reject) => {
  38. try {
  39. const defaultOptions = {
  40. output_discordant: true,
  41. output_splitted: true,
  42. output_unmapped: true
  43. }
  44. if (typeof options === 'undefined') {
  45. options = defaultOptions
  46. } else {
  47. options = {...defaultOptions, ...options}
  48. }
  49. const refName = path.parse(refPath).name
  50. const bwa = 'bwa'
  51. const samblaster = 'samblaster'
  52. const samtools = 'samtools'
  53. const sambamba = 'sambamba'
  54. let readsIn: string
  55. let isPairedEnd = false
  56. if (Array.isArray(reads) ) {
  57. isPairedEnd = true
  58. console.log('Assuming paired end reads');
  59. const [R1, R2] = reads
  60. const R1_arr = Array.isArray(R1) ? R1.join(' ') : R1
  61. const R2_arr = Array.isArray(R2) ? R2.join(' ') : R2
  62. const R1_kitty = R1_arr.slice(-2) === 'gz' ? 'zcat' : 'cat'
  63. const R2_kitty = R2_arr.slice(-2) === 'gz' ? 'zcat' : 'cat'
  64. const R1_in = `'< ${R1_kitty} ${R1_arr}'`
  65. const R2_in = `'< ${R2_kitty} ${R2_arr}'`
  66. readsIn = R1_in + ' ' + R2_in
  67. } else {
  68. readsIn = reads
  69. }
  70. let bam = path.join(outputDir, `bwa_mem_properly_on_${refName}.bam`)
  71. let bamSorted = path.join(outputDir, `bwa_mem_properly_on_${refName}.sorted.bam`)
  72. let retObj: any = { bamSorted }
  73. if(options?.remove_mapped) {
  74. bam = '/dev/null'
  75. delete retObj.bamSorted
  76. }
  77. const threads = String(cpus().length)
  78. let samblasterCmd: Array<string> = []
  79. // https://github.com/GregoryFaust/samblaster
  80. samblasterCmd = ['|', samblaster,
  81. '--addMateTags',
  82. '-a', // Accept duplicate marks already in input file
  83. '-e', // Exclude reads marked as duplicates from discordant, splitter, and/or unmapped
  84. ]
  85. if (options?.output_discordant || options?.output_splitted) {
  86. console.log('Using samblaster');
  87. if(options?.output_discordant) {
  88. if(!isPairedEnd) {
  89. console.log('Discordant reads can be found only in paired reads, skipping')
  90. } else {
  91. const discordantFile = path.join(outputDir, `bwa_mem_discordants_on_${refName}.sam`)
  92. console.log('Discordant reads file path: ', discordantFile);
  93. samblasterCmd = [...samblasterCmd, '-d', discordantFile]
  94. retObj = {...retObj, discordantFile}
  95. }
  96. }
  97. if (!isPairedEnd) {
  98. samblasterCmd = [...samblasterCmd, '--ignoreUnmated']
  99. }
  100. if(options?.output_splitted) {
  101. const splitterFile = path.join(outputDir, `bwa_mem_splitters_on_${refName}.sam`)
  102. console.log('Splitted reads file path: ', splitterFile);
  103. samblasterCmd = [...samblasterCmd, '-s', splitterFile]
  104. retObj = {...retObj, splitterFile}
  105. }
  106. }
  107. if(options?.output_unmapped) {
  108. const unmappedFile = path.join(outputDir, `bwa_mem_unmapped_on_${refName}.fq`)
  109. console.log('Unmapped reads file path: ', unmappedFile);
  110. samblasterCmd = [...samblasterCmd, '-u', unmappedFile]
  111. retObj = {...retObj, unmappedFile}
  112. }
  113. if(!fs.existsSync(refPath+'.amb')) {
  114. await async_exec(bwa, ['index', refPath], (message: string) => onData('[BWA-INDEX] ' + message))
  115. }
  116. console.log(options, samblasterCmd);
  117. const code = await async_exec(
  118. bwa, ['mem',
  119. '-t', threads,
  120. '-R', `"@RG\\tPL:Illumina\\tID:${+(new Date)}\\tSM:${runName}\\tLB:${libName}"`,
  121. refPath,
  122. readsIn,
  123. ...samblasterCmd,
  124. '|',
  125. samtools,
  126. 'view',
  127. '-Sb',
  128. '-',
  129. '>',
  130. bam
  131. ], (message: string) => onData('[BWA-MEM] ' + message))
  132. onData('[BWA-MEM][EXIT CODE] ' + code)
  133. if(retObj.bamSorted) {
  134. const code_sort = await async_exec(
  135. sambamba, ['sort',
  136. '-t', threads,
  137. bam
  138. ], (message: string) => onData('[SAMBAMBA-SORT] ' + message))
  139. onData('[SAMBAMBA-SORT][EXIT CODE] ' + code_sort)
  140. fs.unlinkSync(bam)
  141. }
  142. resolve(retObj)
  143. } catch (err) {
  144. reject(err)
  145. }
  146. })
  147. }
  148. export { asyncBwaMem, makeReference }