Thomas 3 years ago
parent
commit
be0e8f470c
3 changed files with 34 additions and 15 deletions
  1. 3 2
      dist/index.d.ts
  2. 25 10
      dist/index.js
  3. 6 3
      index.ts

+ 3 - 2
dist/index.d.ts

@@ -1,3 +1,4 @@
-declare const makeReference: (sequenceName: string, sequence: string, filePath: string, lineN?: number) => Promise<boolean>;
+declare const writeSequence: (sequenceName: string, sequence: string, filePath: string, lineN?: number) => Promise<boolean>;
+declare const makeReference: (sequenceName: string, sequence: string, filePath: string, lineN?: number) => Promise<void>;
 declare const asyncBwaMem: (refPath: string, reads: string | Array<string> | Array<Array<string>>, runName: string, libName: string, outputDir: string, onData: Function, options?: any) => Promise<string[]>;
-export { asyncBwaMem, makeReference };
+export { asyncBwaMem, writeSequence, makeReference };

+ 25 - 10
dist/index.js

@@ -59,7 +59,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.makeReference = exports.asyncBwaMem = void 0;
+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"));
@@ -77,7 +77,7 @@ 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 makeReference = function (sequenceName, sequence, filePath, lineN) {
+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) {
@@ -87,30 +87,45 @@ var makeReference = function (sequenceName, sequence, filePath, lineN) {
                     return __generator(this, function (_b) {
                         switch (_b.label) {
                             case 0:
-                                _b.trys.push([0, 3, , 4]);
+                                _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')))];
                             case 1:
-                                _b.sent();
-                                return [4 /*yield*/, async_exec('bwa', ['index', filePath], function () { return console.log; })];
-                            case 2:
                                 _b.sent();
                                 resolve(true);
-                                return [3 /*break*/, 4];
-                            case 3:
+                                return [3 /*break*/, 3];
+                            case 2:
                                 error_1 = _b.sent();
                                 console.log(error_1);
                                 reject(false);
-                                return [3 /*break*/, 4];
-                            case 4: return [2 /*return*/];
+                                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>,

+ 6 - 3
index.ts

@@ -16,14 +16,13 @@ const async_exec = (prog: string, args: string[], onData: Function) => {
 }
 const invReplace = (regex: RegExp, string: string, by = '_') => string.split('').map(letter => letter.match(regex) ? letter : by).join('')
 
-const makeReference = async (sequenceName:string, sequence:string, filePath: string, lineN = 80) => {
+const writeSequence = async (sequenceName:string, sequence:string, filePath: string, lineN = 80) => {
   return new Promise<boolean>(async(resolve, reject) => {
     try {
       const r = new RegExp(".{1," + lineN + "}","g");
       const regex_sam_restriction: RegExp = /[>0-9A-Za-z!#$%&+\./:;?@^_|~-]|[\n\t]/g;
       const nSeqName = invReplace(regex_sam_restriction, sequenceName)
       await fs.promises.writeFile(filePath, '>' + nSeqName + '\n' + sequence.match(r)?.join('\n'))
-      await async_exec('bwa', ['index',filePath], () => console.log)
       resolve(true)
     } catch (error) {
       console.log(error)
@@ -32,6 +31,10 @@ const makeReference = async (sequenceName:string, sequence:string, filePath: str
   })
 }
 
+const makeReference = async (sequenceName:string, sequence:string, filePath: string, lineN = 80) => {
+  if (await writeSequence(sequenceName, sequence, filePath, lineN)) await async_exec('bwa', ['index',filePath], () => console.log)
+}
+
 const asyncBwaMem = (
     refPath   : string,
     reads     : string | Array<string> | Array<Array<string>>,
@@ -180,4 +183,4 @@ const asyncBwaMem = (
     })
 }
 
-export { asyncBwaMem, makeReference }
+export { asyncBwaMem, writeSequence, makeReference }