Skip to content

Commit

Permalink
implement peptfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jun 19, 2024
1 parent e8b6773 commit 6791e0c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/peptfilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

import { Peptfilter } from '../lib/commands/peptfilter.js';

const command = new Peptfilter();
command.run();
53 changes: 53 additions & 0 deletions lib/commands/peptfilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Option } from 'commander';
import { createInterface } from 'node:readline';
import { BaseCommand } from './base_command.js';

export class Peptfilter extends BaseCommand {

readonly description = `The peptfilter command filters a list of peptides according to specific criteria. The command expects a list of peptides that are passed to standard input.
The input should have one peptide per line. FASTA headers are preserved in the output, so that peptides remain bundled.`;

constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) {
super(options);

this.program
.summary("Filter peptides based on specific criteria.")
.description(this.description)
.option("--minlen <length>", "only retain peptides having at least this many amino acids", (d) => parseInt(d, 10), 5)
.option("--maxlen <length>", "only retain peptides having at most this many amino acids", (d) => parseInt(d, 10), 50)
.option("-l, --lacks <amino acids>", "only retain peptides that lack all of the specified amino acids", (d) => d.split(""))
.option("-c, --contains <amino acids>", "only retain peptides that contain all of the specified amino acids", (d) => d.split(""));
}

async run() {
this.parseArguments();
console.log(this.program.opts())
const minLen = this.program.opts().minLen;
const maxlen = this.program.opts().maxLen;
const lacks = this.program.opts().lacks || [];
const contains = this.program.opts().contains || [];

for await (const line of createInterface({ input: process.stdin })) {
if (line.startsWith(">")) {
process.stdout.write(line + "\n");
continue;
}
if (Peptfilter.checkLength(line, minLen, maxlen) && Peptfilter.checkLacks(line, lacks) && Peptfilter.checkContains(line, contains)) {
process.stdout.write(line + "\n");
}
}
}

static checkLength(line: string, minLen: number, maxlen: number): boolean {
return line.length >= minLen && line.length <= maxlen;
}

static checkLacks(line: string, lacks: string[]): boolean {
return lacks.every((aa: string) => !line.includes(aa));
}

static checkContains(line: string, contains: string[]): boolean {
return contains.every((aa: string) => line.includes(aa));
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"private": false,
"type": "module",
"bin": {
"peptfilter": "./bin/peptfilter.js",
"uniprot": "./bin/uniprot.js"
},
"scripts": {
"build": "yarn run tsc",
"lint": "yarn run eslint",
"test": "yarn run jest",
"typecheck": "yarn tsc --skipLibCheck --noEmit",
"peptfilter": "yarn run tsx bin/peptfilter.ts",
"uniprot": "yarn run tsx bin/uniprot.ts"
},
"dependencies": {
Expand Down

0 comments on commit 6791e0c

Please sign in to comment.