index.js 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.clusterSam = void 0;
  13. const child_process_1 = require("child_process");
  14. /* (c) Thomas Steimlé 2022
  15. * cat bwa_mem_splitters_on_HG38_Viral.sam | awk '$0~/^@/{next}{lxa=split($0,xa,"XA:Z:"); print $1"\t"$3"\t"$4; if(lxa>1){split(xa[2],xap,","); print $1"\t"xap[1]"\t"substr(xap[2],2)"\tXA"}}' | more
  16. * require os : cat, awk, sort, uniq
  17. *
  18. */
  19. const async_exec = (prog, args, onData, onErr) => {
  20. return new Promise((resolve, reject) => {
  21. const child = (0, child_process_1.spawn)(prog, args, { shell: true });
  22. child.stdout.on('data', data => onData(data /*.toString().trim()*/));
  23. child.stderr.on('data', data => onErr(data.toString().trim()));
  24. child.on('error', err => reject(err));
  25. child.on('exit', code => resolve(code));
  26. });
  27. };
  28. const clusterSam = (input_sam, threshold, minReads) => {
  29. return new Promise((resolve, _reject) => __awaiter(void 0, void 0, void 0, function* () {
  30. let inputSam = Array.isArray(input_sam) ? input_sam.join(' ') : input_sam;
  31. let lineAcc = '';
  32. let byContigs = {};
  33. yield async_exec('cat', [
  34. inputSam,
  35. '|',
  36. 'awk', '\'$0~/^@/{next}{lxa=split($0,xa,"XA:Z:"); print $1"\t"$3"\t"$4; if(lxa>1){split(xa[2],xap,","); print $1"\t"xap[1]"\t"substr(xap[2],2)"\tXA"}}\'',
  37. '|',
  38. 'sort',
  39. '|',
  40. 'uniq'
  41. ], (m) => {
  42. let tmpSeq = (lineAcc + m).split(/\n/);
  43. lineAcc = tmpSeq.pop(); // 'uck typescript
  44. tmpSeq.map(e => {
  45. let tmpName = '';
  46. let tmpPos = { rname: '', position: 0 };
  47. e.split(/\t/).map((el, i) => {
  48. switch (i) {
  49. case 0:
  50. tmpPos['rname'] = el;
  51. break;
  52. case 1:
  53. tmpName = el;
  54. break;
  55. case 2:
  56. tmpPos['position'] = Number(el);
  57. break;
  58. default:
  59. break;
  60. }
  61. });
  62. if (Array.isArray(byContigs[tmpName])) {
  63. byContigs[tmpName].push(tmpPos);
  64. }
  65. else {
  66. byContigs[tmpName] = [tmpPos];
  67. }
  68. });
  69. }, console.log);
  70. let byReads = {};
  71. Object
  72. .keys(byContigs)
  73. .map(name => {
  74. let cluster = 0;
  75. byContigs[name]
  76. .sort((a, b) => a.position - b.position)
  77. .map((e, i, a) => {
  78. var _a;
  79. cluster = Math.abs(e.position - ((_a = a[i - 1]) === null || _a === void 0 ? void 0 : _a.position)) > threshold ? cluster + 1 : cluster;
  80. const clutserName = cluster + '@' + name;
  81. byReads[e.rname] = Array.isArray(byReads[e.rname]) ? [...new Set([...byReads[e.rname], clutserName])] : [clutserName];
  82. });
  83. });
  84. let byClusters = {};
  85. Object.keys(byReads).map(rname => {
  86. const tmpClusterName = byReads[rname].sort().join('<--->');
  87. byClusters[tmpClusterName] = Array.isArray(byClusters[tmpClusterName]) ? [...new Set([...byClusters[tmpClusterName], rname])] : [rname];
  88. });
  89. Object.keys(byClusters).map(e => byClusters[e].length < minReads ? delete byClusters[e] : null);
  90. resolve((Object.keys(byClusters).map(clusterName => ({ clusterName, rname: byClusters[clusterName] })).sort((a, b) => b.rname.length - a.rname.length)));
  91. }));
  92. };
  93. exports.clusterSam = clusterSam;
  94. /*
  95. (async () => {
  96. console.log(await clusterSam('/home/thomas/Documents/Programmes/ttest/bwa_mem_splitters_on_HG38_Viral.sam', 333, 55));
  97. })()
  98. */