|
|
@@ -1,259 +0,0 @@
|
|
|
-"use strict";
|
|
|
-var __assign = (this && this.__assign) || function () {
|
|
|
- __assign = Object.assign || function(t) {
|
|
|
- for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
|
- s = arguments[i];
|
|
|
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
|
- t[p] = s[p];
|
|
|
- }
|
|
|
- return t;
|
|
|
- };
|
|
|
- return __assign.apply(this, arguments);
|
|
|
-};
|
|
|
-var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
- return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
- step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
- });
|
|
|
-};
|
|
|
-var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
|
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
|
- function verb(n) { return function (v) { return step([n, v]); }; }
|
|
|
- function step(op) {
|
|
|
- if (f) throw new TypeError("Generator is already executing.");
|
|
|
- while (_) try {
|
|
|
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
|
- if (y = 0, t) op = [op[0] & 2, t.value];
|
|
|
- switch (op[0]) {
|
|
|
- case 0: case 1: t = op; break;
|
|
|
- case 4: _.label++; return { value: op[1], done: false };
|
|
|
- case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
|
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
|
- default:
|
|
|
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
|
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
|
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
|
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
|
- if (t[2]) _.ops.pop();
|
|
|
- _.trys.pop(); continue;
|
|
|
- }
|
|
|
- op = body.call(thisArg, _);
|
|
|
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
|
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
|
- }
|
|
|
-};
|
|
|
-var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
|
- if (ar || !(i in from)) {
|
|
|
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
|
- ar[i] = from[i];
|
|
|
- }
|
|
|
- }
|
|
|
- return to.concat(ar || Array.prototype.slice.call(from));
|
|
|
-};
|
|
|
-var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
- return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
-};
|
|
|
-Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
-exports.makeReference = exports.writeSequence = exports.asyncBwaMem = void 0;
|
|
|
-var child_process_1 = require("child_process");
|
|
|
-var os_1 = require("os");
|
|
|
-var fs_1 = __importDefault(require("fs"));
|
|
|
-var path_1 = __importDefault(require("path"));
|
|
|
-var async_exec = function (prog, args, onData) {
|
|
|
- return new Promise(function (resolve, reject) {
|
|
|
- var child = (0, child_process_1.spawn)(prog, args, { shell: true });
|
|
|
- child.stdout.on('data', function (data) { return onData(data.toString().trim()); });
|
|
|
- child.stderr.on('data', function (data) { return onData(data.toString().trim()); });
|
|
|
- child.on('error', function (err) { return reject(err); });
|
|
|
- child.on('exit', function (code) { return resolve(code); });
|
|
|
- });
|
|
|
-};
|
|
|
-var invReplace = function (regex, string, by) {
|
|
|
- if (by === void 0) { by = '_'; }
|
|
|
- return string.split('').map(function (letter) { return letter.match(regex) ? letter : by; }).join('');
|
|
|
-};
|
|
|
-var writeSequence = function (sequenceName, sequence, filePath, lineN) {
|
|
|
- if (lineN === void 0) { lineN = 80; }
|
|
|
- return __awaiter(void 0, void 0, void 0, function () {
|
|
|
- return __generator(this, function (_a) {
|
|
|
- return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
|
- var r, regex_sam_restriction, nSeqName, error_1;
|
|
|
- var _a;
|
|
|
- return __generator(this, function (_b) {
|
|
|
- switch (_b.label) {
|
|
|
- case 0:
|
|
|
- _b.trys.push([0, 2, , 3]);
|
|
|
- r = new RegExp(".{1," + lineN + "}", "g");
|
|
|
- regex_sam_restriction = /[>0-9A-Za-z!#$%&+\./:;?@^_|~-]|[\n\t]/g;
|
|
|
- nSeqName = invReplace(regex_sam_restriction, sequenceName);
|
|
|
- return [4 /*yield*/, fs_1.default.promises.writeFile(filePath, '>' + nSeqName + '\n' + ((_a = sequence.match(r)) === null || _a === void 0 ? void 0 : _a.join('\n').toUpperCase()))];
|
|
|
- case 1:
|
|
|
- _b.sent();
|
|
|
- resolve(true);
|
|
|
- return [3 /*break*/, 3];
|
|
|
- case 2:
|
|
|
- error_1 = _b.sent();
|
|
|
- console.log(error_1);
|
|
|
- reject(false);
|
|
|
- return [3 /*break*/, 3];
|
|
|
- case 3: return [2 /*return*/];
|
|
|
- }
|
|
|
- });
|
|
|
- }); })];
|
|
|
- });
|
|
|
- });
|
|
|
-};
|
|
|
-exports.writeSequence = writeSequence;
|
|
|
-var makeReference = function (sequenceName, sequence, filePath, lineN) {
|
|
|
- if (lineN === void 0) { lineN = 80; }
|
|
|
- return __awaiter(void 0, void 0, void 0, function () {
|
|
|
- return __generator(this, function (_a) {
|
|
|
- switch (_a.label) {
|
|
|
- case 0: return [4 /*yield*/, writeSequence(sequenceName, sequence, filePath, lineN)];
|
|
|
- case 1:
|
|
|
- if (!_a.sent()) return [3 /*break*/, 3];
|
|
|
- return [4 /*yield*/, async_exec('bwa', ['index', filePath], function () { return console.log; })];
|
|
|
- case 2:
|
|
|
- _a.sent();
|
|
|
- _a.label = 3;
|
|
|
- case 3: return [2 /*return*/];
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
-};
|
|
|
-exports.makeReference = makeReference;
|
|
|
-var asyncBwaMem = function (refPath, reads,
|
|
|
-// R1 : string | Array<string>,
|
|
|
-// R2 : string | Array<string>,
|
|
|
-runName, libName, outputDir, onData, options) {
|
|
|
- return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
|
- var defaultOptions, refName, bwa, samblaster, samtools, sambamba, readsIn, isPairedEnd, R1, R2, R1_arr, R2_arr, R1_kitty, R2_kitty, R1_in, R2_in, bam, bamSorted, retObj, threads, samblasterCmd, discordantFile, splitterFile, unmappedFile, code, code_sort, err_1;
|
|
|
- return __generator(this, function (_a) {
|
|
|
- switch (_a.label) {
|
|
|
- case 0:
|
|
|
- _a.trys.push([0, 6, , 7]);
|
|
|
- defaultOptions = {
|
|
|
- output_discordant: true,
|
|
|
- output_splitted: true,
|
|
|
- output_unmapped: true
|
|
|
- };
|
|
|
- if (typeof options === 'undefined') {
|
|
|
- options = defaultOptions;
|
|
|
- }
|
|
|
- else {
|
|
|
- options = __assign(__assign({}, defaultOptions), options);
|
|
|
- }
|
|
|
- refName = path_1.default.parse(refPath).name;
|
|
|
- bwa = 'bwa';
|
|
|
- samblaster = 'samblaster';
|
|
|
- samtools = 'samtools';
|
|
|
- sambamba = 'sambamba';
|
|
|
- readsIn = void 0;
|
|
|
- isPairedEnd = false;
|
|
|
- if (Array.isArray(reads)) {
|
|
|
- isPairedEnd = true;
|
|
|
- console.log('Assuming paired end reads');
|
|
|
- R1 = reads[0], R2 = reads[1];
|
|
|
- R1_arr = Array.isArray(R1) ? R1.join(' ') : R1;
|
|
|
- R2_arr = Array.isArray(R2) ? R2.join(' ') : R2;
|
|
|
- R1_kitty = R1_arr.slice(-2) === 'gz' ? 'zcat' : 'cat';
|
|
|
- R2_kitty = R2_arr.slice(-2) === 'gz' ? 'zcat' : 'cat';
|
|
|
- R1_in = "'< ".concat(R1_kitty, " ").concat(R1_arr, "'");
|
|
|
- R2_in = "'< ".concat(R2_kitty, " ").concat(R2_arr, "'");
|
|
|
- readsIn = R1_in + ' ' + R2_in;
|
|
|
- }
|
|
|
- else {
|
|
|
- readsIn = reads;
|
|
|
- }
|
|
|
- bam = path_1.default.join(outputDir, "bwa_mem_properly_on_".concat(refName, ".bam"));
|
|
|
- bamSorted = path_1.default.join(outputDir, "bwa_mem_properly_on_".concat(refName, ".sorted.bam"));
|
|
|
- retObj = { bamSorted: bamSorted };
|
|
|
- if (options === null || options === void 0 ? void 0 : options.remove_mapped) {
|
|
|
- bam = '/dev/null';
|
|
|
- delete retObj.bamSorted;
|
|
|
- }
|
|
|
- threads = String((0, os_1.cpus)().length);
|
|
|
- samblasterCmd = [];
|
|
|
- // https://github.com/GregoryFaust/samblaster
|
|
|
- samblasterCmd = ['|', samblaster,
|
|
|
- '--addMateTags',
|
|
|
- '-a',
|
|
|
- '-e', // Exclude reads marked as duplicates from discordant, splitter, and/or unmapped
|
|
|
- ];
|
|
|
- if ((options === null || options === void 0 ? void 0 : options.output_discordant) || (options === null || options === void 0 ? void 0 : options.output_splitted)) {
|
|
|
- console.log('Using samblaster');
|
|
|
- if (options === null || options === void 0 ? void 0 : options.output_discordant) {
|
|
|
- if (!isPairedEnd) {
|
|
|
- console.log('Discordant reads can be found only in paired reads, skipping');
|
|
|
- }
|
|
|
- else {
|
|
|
- discordantFile = path_1.default.join(outputDir, "bwa_mem_discordants_on_".concat(refName, ".sam"));
|
|
|
- console.log('Discordant reads file path: ', discordantFile);
|
|
|
- samblasterCmd = __spreadArray(__spreadArray([], samblasterCmd, true), ['-d', discordantFile], false);
|
|
|
- retObj = __assign(__assign({}, retObj), { discordantFile: discordantFile });
|
|
|
- }
|
|
|
- }
|
|
|
- if (!isPairedEnd) {
|
|
|
- samblasterCmd = __spreadArray(__spreadArray([], samblasterCmd, true), ['--ignoreUnmated'], false);
|
|
|
- }
|
|
|
- if (options === null || options === void 0 ? void 0 : options.output_splitted) {
|
|
|
- splitterFile = path_1.default.join(outputDir, "bwa_mem_splitters_on_".concat(refName, ".sam"));
|
|
|
- console.log('Splitted reads file path: ', splitterFile);
|
|
|
- samblasterCmd = __spreadArray(__spreadArray([], samblasterCmd, true), ['-s', splitterFile], false);
|
|
|
- retObj = __assign(__assign({}, retObj), { splitterFile: splitterFile });
|
|
|
- }
|
|
|
- }
|
|
|
- if (options === null || options === void 0 ? void 0 : options.output_unmapped) {
|
|
|
- unmappedFile = path_1.default.join(outputDir, "bwa_mem_unmapped_on_".concat(refName, ".fq"));
|
|
|
- console.log('Unmapped reads file path: ', unmappedFile);
|
|
|
- samblasterCmd = __spreadArray(__spreadArray([], samblasterCmd, true), ['-u', unmappedFile], false);
|
|
|
- retObj = __assign(__assign({}, retObj), { unmappedFile: unmappedFile });
|
|
|
- }
|
|
|
- if (!!fs_1.default.existsSync(refPath + '.amb')) return [3 /*break*/, 2];
|
|
|
- return [4 /*yield*/, async_exec(bwa, ['index', refPath], function (message) { return onData('[BWA-INDEX] ' + message); })];
|
|
|
- case 1:
|
|
|
- _a.sent();
|
|
|
- _a.label = 2;
|
|
|
- case 2:
|
|
|
- console.log(options, samblasterCmd);
|
|
|
- return [4 /*yield*/, async_exec(bwa, __spreadArray(__spreadArray(['mem',
|
|
|
- '-t', threads,
|
|
|
- '-R', "\"@RG\\tPL:Illumina\\tID:".concat(+(new Date), "\\tSM:").concat(runName, "\\tLB:").concat(libName, "\""), refPath,
|
|
|
- readsIn], samblasterCmd, true), ['|',
|
|
|
- samtools,
|
|
|
- 'view',
|
|
|
- '-Sb',
|
|
|
- '-',
|
|
|
- '>',
|
|
|
- bam], false), function (message) { return onData('[BWA-MEM] ' + message); })];
|
|
|
- case 3:
|
|
|
- code = _a.sent();
|
|
|
- onData('[BWA-MEM][EXIT CODE] ' + code);
|
|
|
- if (!retObj.bamSorted) return [3 /*break*/, 5];
|
|
|
- return [4 /*yield*/, async_exec(sambamba, ['sort',
|
|
|
- '-t', threads,
|
|
|
- bam
|
|
|
- ], function (message) { return onData('[SAMBAMBA-SORT] ' + message); })];
|
|
|
- case 4:
|
|
|
- code_sort = _a.sent();
|
|
|
- onData('[SAMBAMBA-SORT][EXIT CODE] ' + code_sort);
|
|
|
- fs_1.default.unlinkSync(bam);
|
|
|
- _a.label = 5;
|
|
|
- case 5:
|
|
|
- resolve(retObj);
|
|
|
- return [3 /*break*/, 7];
|
|
|
- case 6:
|
|
|
- err_1 = _a.sent();
|
|
|
- reject(err_1);
|
|
|
- return [3 /*break*/, 7];
|
|
|
- case 7: return [2 /*return*/];
|
|
|
- }
|
|
|
- });
|
|
|
- }); });
|
|
|
-};
|
|
|
-exports.asyncBwaMem = asyncBwaMem;
|