index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. let posAll = {};
  72. Object
  73. .keys(byContigs)
  74. .map(name => {
  75. let cluster = 0;
  76. let firstPos = 0;
  77. byContigs[name]
  78. .sort((a, b) => a.position - b.position)
  79. .map((e, i, a) => {
  80. var _a, _b;
  81. if (i === 0) {
  82. firstPos = e.position;
  83. }
  84. if (Math.abs(e.position - ((_a = a[i - 1]) === null || _a === void 0 ? void 0 : _a.position)) > threshold) {
  85. if (typeof posAll[name] === 'undefined')
  86. posAll[name] = {};
  87. posAll[name][String(cluster)] = firstPos + '-' + ((_b = a[i - 1]) === null || _b === void 0 ? void 0 : _b.position);
  88. cluster = cluster + 1;
  89. firstPos = e.position;
  90. }
  91. // cluster = Math.abs(e.position - a[i-1]?.position) > threshold ? cluster + 1 : cluster
  92. const clutserName = cluster + '@' + name;
  93. byReads[e.rname] = Array.isArray(byReads[e.rname]) ? [...new Set([...byReads[e.rname], clutserName])] : [clutserName];
  94. });
  95. });
  96. let byClusters = {};
  97. Object.keys(byReads).map(rname => {
  98. const tmpClusterName = byReads[rname].sort().map(e => {
  99. const splited = e.split(/@/);
  100. return splited[1] + ':' + posAll[splited[1]][splited[0]] + '(' + splited[0] + ')';
  101. }).join('<--->');
  102. byClusters[tmpClusterName] = Array.isArray(byClusters[tmpClusterName]) ? [...new Set([...byClusters[tmpClusterName], rname])] : [rname];
  103. });
  104. Object.keys(byClusters).map(e => byClusters[e].length < minReads ? delete byClusters[e] : null);
  105. resolve((Object.keys(byClusters).map(clusterName => ({ clusterName, rnames: byClusters[clusterName] })).sort((a, b) => b.rnames.length - a.rnames.length)));
  106. }));
  107. };
  108. exports.clusterSam = clusterSam;
  109. /*
  110. (async () => {
  111. console.log(await clusterSam('/home/thomas/Documents/Programmes/ttest/bwa_mem_splitters_on_HG38_Viral.sam', 333, 55));
  112. })()
  113. */