瀏覽代碼

first commit

Thomas 3 年之前
當前提交
11f95e98fe
共有 7 個文件被更改,包括 433 次插入0 次删除
  1. 0 0
      README.md
  2. 128 0
      index.js
  3. 114 0
      index.ts
  4. 51 0
      package-lock.json
  5. 21 0
      package.json
  6. 101 0
      tsconfig.json
  7. 18 0
      yarn.lock

+ 0 - 0
README.md


+ 128 - 0
index.js

@@ -0,0 +1,128 @@
+"use strict";
+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 __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.asyncReadSmallFasta = exports.asyncReadFasta = void 0;
+const child_process_1 = require("child_process");
+const fs_1 = __importDefault(require("fs"));
+const zlib = require('zlib');
+const readline = require('readline');
+const Papa = require('papaparse');
+// Read fasta/fa/fna
+const asyncReadSmallFasta = (path) => {
+    return new Promise((resolve, reject) => {
+        const rs = fs_1.default.createReadStream(path);
+        const rl = readline.createInterface({
+            input: rs, crlfDelay: Infinity
+        });
+        rs.once('error', err => reject(err));
+        let tmpObj = {};
+        let results = [];
+        rl.on('line', (line) => {
+            if (line.match(/>/g)) {
+                if (tmpObj === null || tmpObj === void 0 ? void 0 : tmpObj.name)
+                    results.push(tmpObj);
+                tmpObj = { name: line, sequence: '' };
+            }
+            else {
+                tmpObj.sequence += line;
+            }
+        });
+        rl.on('close', (_) => {
+            results.push(tmpObj);
+            resolve(results);
+        });
+    });
+};
+exports.asyncReadSmallFasta = asyncReadSmallFasta;
+// https://www.biostars.org/p/98885/
+const async_save_fai = (fasta_path) => {
+    console.log('Creating new fai from : ', fasta_path);
+    return new Promise((resolve, reject) => {
+        const child = (0, child_process_1.spawn)('samtools', ['faidx', fasta_path], {
+            shell: true,
+        });
+        let result = '';
+        child.stdout.on('data', (data) => {
+            result += data.toString();
+        });
+        child.on('error', (err) => {
+            reject(err.toString('utf8'));
+        });
+        child.on('exit', () => {
+            resolve(result);
+        });
+    });
+};
+const async_paparse = (path, opt) => {
+    return new Promise((resolve, reject) => {
+        const output = [];
+        const parseStream = Papa.parse(Papa.NODE_STREAM_INPUT, opt);
+        let rs = fs_1.default.createReadStream(path);
+        if ((new RegExp(/\.gz$/)).test(path)) {
+            rs = rs.pipe(zlib.createGunzip());
+        }
+        rs.pipe(parseStream)
+            .on('error', (error) => reject(error))
+            .on('data', (row) => { output.push(row); })
+            .on('finish', () => resolve(output));
+    });
+};
+const async_read_bytes = (path, start, length) => {
+    length = length - 1;
+    return new Promise((resolve, reject) => {
+        let data = Buffer.alloc(0);
+        fs_1.default.createReadStream(path, { start: start, end: start + length })
+            .on('error', error => reject(error))
+            .on('data', (d) => {
+            data = Buffer.concat([data, d]);
+        })
+            .on('end', () => resolve(data.toString()));
+    });
+};
+// Create fai if not present return sequences names and if provided return : obj {name, sequence}
+const asyncReadFasta = (path, sequences) => {
+    sequences = Array.isArray(sequences) ? sequences : [sequences];
+    return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
+        const path_fai = (new RegExp('fai$')).test(path) ? path : path + '.fai';
+        if (!fs_1.default.existsSync(path_fai)) {
+            try {
+                yield async_save_fai(path);
+            }
+            catch (e) {
+                reject(e);
+            }
+        }
+        try {
+            const fai = yield async_paparse(path_fai, { header: false, dynamicTyping: true });
+            if (sequences.length === 0) {
+                if (Array.isArray(fai))
+                    resolve(fai.map((it) => it[0]));
+            }
+            else {
+                let results = [];
+                for (let index = 0; index < sequences.length; index++) {
+                    const seqName = sequences[index];
+                    const tmpFai = Array.isArray(fai) ? fai.filter((it) => it[0] === seqName)[0] : [];
+                    const sequence = yield async_read_bytes(path, tmpFai[2], (Math.trunc(tmpFai[1] / tmpFai[3]) * tmpFai[4]) + (tmpFai[1] % tmpFai[3])); // fu, but better use samtools faidx !
+                    results.push({ name: tmpFai[0], sequence });
+                }
+                resolve(results);
+            }
+        }
+        catch (e) {
+            reject(e);
+        }
+    }));
+};
+exports.asyncReadFasta = asyncReadFasta;

+ 114 - 0
index.ts

@@ -0,0 +1,114 @@
+import { spawn } from 'child_process'
+import fs from 'fs'
+const zlib = require('zlib')
+
+const readline = require('readline')
+const Papa     = require('papaparse')
+
+// Read fasta/fa/fna
+const asyncReadSmallFasta = (path: string) => {
+  return new Promise<any[]>((resolve, reject) => {
+    const rs = fs.createReadStream(path)
+    const rl = readline.createInterface({
+      input: rs, crlfDelay: Infinity
+    })
+
+    rs.once('error', err => reject(err))
+    let tmpObj = {} as {name: string, sequence: string}
+    let results: any[] = []
+    rl.on('line', (line: string) => {
+      if (line.match(/>/g)) {
+        if (tmpObj?.name) results.push(tmpObj)
+        tmpObj = {name: line, sequence: ''}
+      } else {
+        tmpObj.sequence += line
+      }
+    })
+
+    rl.on('close', (_: any) => {
+      results.push(tmpObj)
+      resolve(results)
+    })
+  })
+}
+
+// https://www.biostars.org/p/98885/
+
+const async_save_fai = (fasta_path: string) => {
+  console.log('Creating new fai from : ', fasta_path)
+  return new Promise((resolve, reject) => {
+    const child = spawn('samtools', ['faidx', fasta_path], {
+      shell: true,
+    })
+    let result = ''
+    child.stdout.on('data', (data: { toString: () => string }) => {
+      result += data.toString()
+    })
+    child.on('error', (err: { toString: (arg0: string) => any }) => {
+      reject(err.toString('utf8'))
+    })
+    child.on('exit', () => {
+      resolve(result)
+    })
+  })
+}
+
+const async_paparse = (path: string, opt: { header: boolean; dynamicTyping: boolean }) => {
+  return new Promise((resolve, reject) => {
+    const output: any[] = []
+    const parseStream = Papa.parse(Papa.NODE_STREAM_INPUT, opt)
+
+    let rs = fs.createReadStream(path)
+
+    if((new RegExp(/\.gz$/)).test(path)) {
+      rs = rs.pipe(zlib.createGunzip())
+    }
+
+    rs.pipe(parseStream)
+    .on('error', (error: any) => reject(error))
+    .on('data', (row: any) => {output.push(row)})
+    .on('finish', () => resolve(output))
+  })
+}
+
+const async_read_bytes = (path: fs.PathLike, start: any, length: number) => {
+  length = length - 1
+  return new Promise((resolve, reject) => {
+    let data = Buffer.alloc(0)
+    fs.createReadStream(path, { start : start, end: start + length })
+    .on('error', error => reject(error))
+    .on('data', (d: Buffer) => {
+      data = Buffer.concat([data, d])
+    })
+    .on('end', () => resolve(data.toString()))
+  })
+}
+
+// Create fai if not present return sequences names and if provided return : obj {name, sequence}
+const asyncReadFasta = (path: string, sequences: string[] | string) => {
+  sequences = Array.isArray(sequences) ? sequences : [sequences]
+  return new Promise(async (resolve, reject) => {
+    const path_fai = (new RegExp('fai$')).test(path) ? path : path + '.fai'
+    if(!fs.existsSync(path_fai)) {
+      try { await async_save_fai(path) } catch (e) { reject(e) }
+    }
+    try {
+      const fai = await async_paparse(path_fai, {header: false, dynamicTyping: true })
+      if(sequences.length === 0) {
+        if (Array.isArray(fai)) resolve(fai.map((it: any[]) => it[0]))
+      } else {
+        let results = []
+        for (let index = 0; index < sequences.length; index++) {
+          const seqName = sequences[index]
+          const tmpFai = Array.isArray(fai) ? fai.filter((it: any[]) => it[0] === seqName)[0] : []
+          const sequence = await async_read_bytes(path, tmpFai[2],
+          (Math.trunc(tmpFai[1] / tmpFai[3]) * tmpFai[4]) + (tmpFai[1] % tmpFai[3])) // fu, but better use samtools faidx !
+          results.push({name: tmpFai[0], sequence})
+        }
+        resolve(results)
+      }
+    } catch (e) { reject(e) }
+  })
+}
+
+export { asyncReadFasta, asyncReadSmallFasta }

+ 51 - 0
package-lock.json

@@ -0,0 +1,51 @@
+{
+  "name": "fasta",
+  "version": "1.0.0",
+  "lockfileVersion": 2,
+  "requires": true,
+  "packages": {
+    "": {
+      "name": "fasta",
+      "version": "1.0.0",
+      "license": "ISC",
+      "devDependencies": {
+        "@types/node": "^17.0.19",
+        "typescript": "^4.5.5"
+      }
+    },
+    "node_modules/@types/node": {
+      "version": "17.0.19",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.19.tgz",
+      "integrity": "sha512-PfeQhvcMR4cPFVuYfBN4ifG7p9c+Dlh3yUZR6k+5yQK7wX3gDgVxBly4/WkBRs9x4dmcy1TVl08SY67wwtEvmA==",
+      "dev": true
+    },
+    "node_modules/typescript": {
+      "version": "4.5.5",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz",
+      "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==",
+      "dev": true,
+      "license": "Apache-2.0",
+      "bin": {
+        "tsc": "bin/tsc",
+        "tsserver": "bin/tsserver"
+      },
+      "engines": {
+        "node": ">=4.2.0"
+      }
+    }
+  },
+  "dependencies": {
+    "@types/node": {
+      "version": "17.0.19",
+      "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.19.tgz",
+      "integrity": "sha512-PfeQhvcMR4cPFVuYfBN4ifG7p9c+Dlh3yUZR6k+5yQK7wX3gDgVxBly4/WkBRs9x4dmcy1TVl08SY67wwtEvmA==",
+      "dev": true
+    },
+    "typescript": {
+      "version": "4.5.5",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz",
+      "integrity": "sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==",
+      "dev": true
+    }
+  }
+}

+ 21 - 0
package.json

@@ -0,0 +1,21 @@
+{
+  "name": "fasta",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "prepublish": "npm run build",
+    "build": "tsc",
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "keywords": [],
+  "author": "",
+  "license": "ISC",
+  "devDependencies": {
+    "@types/node": "^17.0.19",
+    "typescript": "^4.5.5"
+  },
+  "dependencies": {
+    "papaparse": "^5.3.1"
+  }
+}

+ 101 - 0
tsconfig.json

@@ -0,0 +1,101 @@
+{
+  "compilerOptions": {
+    /* Visit https://aka.ms/tsconfig.json to read more about this file */
+
+    /* Projects */
+    // "incremental": true,                              /* Enable incremental compilation */
+    // "composite": true,                                /* Enable constraints that allow a TypeScript project to be used with project references. */
+    // "tsBuildInfoFile": "./",                          /* Specify the folder for .tsbuildinfo incremental compilation files. */
+    // "disableSourceOfProjectReferenceRedirect": true,  /* Disable preferring source files instead of declaration files when referencing composite projects */
+    // "disableSolutionSearching": true,                 /* Opt a project out of multi-project reference checking when editing. */
+    // "disableReferencedProjectLoad": true,             /* Reduce the number of projects loaded automatically by TypeScript. */
+
+    /* Language and Environment */
+    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
+    // "lib": [],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */
+    // "jsx": "preserve",                                /* Specify what JSX code is generated. */
+    // "experimentalDecorators": true,                   /* Enable experimental support for TC39 stage 2 draft decorators. */
+    // "emitDecoratorMetadata": true,                    /* Emit design-type metadata for decorated declarations in source files. */
+    // "jsxFactory": "",                                 /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
+    // "jsxFragmentFactory": "",                         /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
+    // "jsxImportSource": "",                            /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
+    // "reactNamespace": "",                             /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
+    // "noLib": true,                                    /* Disable including any library files, including the default lib.d.ts. */
+    // "useDefineForClassFields": true,                  /* Emit ECMAScript-standard-compliant class fields. */
+
+    /* Modules */
+    "module": "commonjs",                                /* Specify what module code is generated. */
+    // "rootDir": "./",                                  /* Specify the root folder within your source files. */
+    // "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */
+    // "baseUrl": "./",                                  /* Specify the base directory to resolve non-relative module names. */
+    // "paths": {},                                      /* Specify a set of entries that re-map imports to additional lookup locations. */
+    // "rootDirs": [],                                   /* Allow multiple folders to be treated as one when resolving modules. */
+    // "typeRoots": [],                                  /* Specify multiple folders that act like `./node_modules/@types`. */
+    // "types": [],                                      /* Specify type package names to be included without being referenced in a source file. */
+    // "allowUmdGlobalAccess": true,                     /* Allow accessing UMD globals from modules. */
+    // "resolveJsonModule": true,                        /* Enable importing .json files */
+    // "noResolve": true,                                /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
+
+    /* JavaScript Support */
+    // "allowJs": true,                                  /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
+    // "checkJs": true,                                  /* Enable error reporting in type-checked JavaScript files. */
+    // "maxNodeModuleJsDepth": 1,                        /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
+
+    /* Emit */
+    // "declaration": true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
+    // "declarationMap": true,                           /* Create sourcemaps for d.ts files. */
+    // "emitDeclarationOnly": true,                      /* Only output d.ts files and not JavaScript files. */
+    // "sourceMap": true,                                /* Create source map files for emitted JavaScript files. */
+    // "outFile": "./",                                  /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
+    // "outDir": "./",                                   /* Specify an output folder for all emitted files. */
+    // "removeComments": true,                           /* Disable emitting comments. */
+    // "noEmit": true,                                   /* Disable emitting files from a compilation. */
+    // "importHelpers": true,                            /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
+    // "importsNotUsedAsValues": "remove",               /* Specify emit/checking behavior for imports that are only used for types */
+    // "downlevelIteration": true,                       /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
+    // "sourceRoot": "",                                 /* Specify the root path for debuggers to find the reference source code. */
+    // "mapRoot": "",                                    /* Specify the location where debugger should locate map files instead of generated locations. */
+    // "inlineSourceMap": true,                          /* Include sourcemap files inside the emitted JavaScript. */
+    // "inlineSources": true,                            /* Include source code in the sourcemaps inside the emitted JavaScript. */
+    // "emitBOM": true,                                  /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
+    // "newLine": "crlf",                                /* Set the newline character for emitting files. */
+    // "stripInternal": true,                            /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
+    // "noEmitHelpers": true,                            /* Disable generating custom helper functions like `__extends` in compiled output. */
+    // "noEmitOnError": true,                            /* Disable emitting files if any type checking errors are reported. */
+    // "preserveConstEnums": true,                       /* Disable erasing `const enum` declarations in generated code. */
+    // "declarationDir": "./",                           /* Specify the output directory for generated declaration files. */
+    // "preserveValueImports": true,                     /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
+
+    /* Interop Constraints */
+    // "isolatedModules": true,                          /* Ensure that each file can be safely transpiled without relying on other imports. */
+    // "allowSyntheticDefaultImports": true,             /* Allow 'import x from y' when a module doesn't have a default export. */
+    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
+    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
+    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
+
+    /* Type Checking */
+    "strict": true,                                      /* Enable all strict type-checking options. */
+    // "noImplicitAny": true,                            /* Enable error reporting for expressions and declarations with an implied `any` type.. */
+    // "strictNullChecks": true,                         /* When type checking, take into account `null` and `undefined`. */
+    // "strictFunctionTypes": true,                      /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
+    // "strictBindCallApply": true,                      /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
+    // "strictPropertyInitialization": true,             /* Check for class properties that are declared but not set in the constructor. */
+    // "noImplicitThis": true,                           /* Enable error reporting when `this` is given the type `any`. */
+    // "useUnknownInCatchVariables": true,               /* Type catch clause variables as 'unknown' instead of 'any'. */
+    // "alwaysStrict": true,                             /* Ensure 'use strict' is always emitted. */
+    // "noUnusedLocals": true,                           /* Enable error reporting when a local variables aren't read. */
+    // "noUnusedParameters": true,                       /* Raise an error when a function parameter isn't read */
+    // "exactOptionalPropertyTypes": true,               /* Interpret optional property types as written, rather than adding 'undefined'. */
+    // "noImplicitReturns": true,                        /* Enable error reporting for codepaths that do not explicitly return in a function. */
+    // "noFallthroughCasesInSwitch": true,               /* Enable error reporting for fallthrough cases in switch statements. */
+    // "noUncheckedIndexedAccess": true,                 /* Include 'undefined' in index signature results */
+    // "noImplicitOverride": true,                       /* Ensure overriding members in derived classes are marked with an override modifier. */
+    // "noPropertyAccessFromIndexSignature": true,       /* Enforces using indexed accessors for keys declared using an indexed type */
+    // "allowUnusedLabels": true,                        /* Disable error reporting for unused labels. */
+    // "allowUnreachableCode": true,                     /* Disable error reporting for unreachable code. */
+
+    /* Completeness */
+    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
+    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
+  }
+}

+ 18 - 0
yarn.lock

@@ -0,0 +1,18 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+
+"@types/node@^17.0.19":
+  version "17.0.19"
+  resolved "https://registry.npmjs.org/@types/node/-/node-17.0.19.tgz"
+  integrity sha512-PfeQhvcMR4cPFVuYfBN4ifG7p9c+Dlh3yUZR6k+5yQK7wX3gDgVxBly4/WkBRs9x4dmcy1TVl08SY67wwtEvmA==
+
+papaparse@^5.3.1:
+  version "5.3.1"
+  resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.3.1.tgz#770b7a9124d821d4b2132132b7bd7dce7194b5b1"
+  integrity sha512-Dbt2yjLJrCwH2sRqKFFJaN5XgIASO9YOFeFP8rIBRG2Ain8mqk5r1M6DkfvqEVozVcz3r3HaUGw253hA1nLIcA==
+
+typescript@^4.5.5:
+  version "4.5.5"
+  resolved "https://registry.npmjs.org/typescript/-/typescript-4.5.5.tgz"
+  integrity sha512-TCTIul70LyWe6IJWT8QSYeA54WQe8EjQFU4wY52Fasj5UKx88LNYKCgBEHcOMOrFF1rKGbD8v/xcNWVUq9SymA==