-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port peptfilter to typescript #175
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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("")); | ||
} | ||
|
||
/** | ||
* Performance note: this implementation takes 4 seconds to run on swissprot. It can be made faster by using line events instead of | ||
* async iterators. This alternative implementation runs in 2.5 seconds. However, I decided that the async iterator implementation is | ||
* both more readable and more in line with the implementation of the other commands. | ||
*/ | ||
async run() { | ||
this.parseArguments(); | ||
const minLen = this.program.opts().minlen; | ||
const maxlen = this.program.opts().maxlen; | ||
const lacks = this.program.opts().lacks || []; | ||
const contains = this.program.opts().contains || []; | ||
|
||
// buffering output makes a big difference in performance | ||
let output = []; | ||
let i = 0; | ||
|
||
for await (const line of createInterface({ input: process.stdin })) { | ||
i++; | ||
if (line.startsWith(">")) { // pass through FASTA headers | ||
output.push(line); | ||
} else if (Peptfilter.checkLength(line, minLen, maxlen) && Peptfilter.checkLacks(line, lacks) && Peptfilter.checkContains(line, contains)) { | ||
output.push(line); | ||
} | ||
if (i % 1000 === 0) { | ||
output.push(""); //add a newline at the end of the buffer without additional string copy | ||
process.stdout.write(output.join("\n")); | ||
output = []; | ||
} | ||
} | ||
|
||
output.push(""); | ||
process.stdout.write(output.join("\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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Peptfilter } from '../../lib/commands/peptfilter'; | ||
import { jest } from '@jest/globals'; | ||
import * as mock from 'mock-stdin'; | ||
|
||
let output: string[]; | ||
let error: string[]; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const writeSpy = jest | ||
.spyOn(process.stdout, "write") | ||
.mockImplementation((data: unknown) => { output.push(data as string); return true; }); | ||
const errorSpy = jest | ||
.spyOn(process.stderr, "write") | ||
.mockImplementation((data: unknown) => { error.push(data as string); return true; }); | ||
|
||
beforeEach(() => { | ||
output = []; | ||
error = []; | ||
}); | ||
|
||
test('test length filter', async () => { | ||
// min length | ||
expect(Peptfilter.checkLength('AALER', 4, 10)).toBe(true); | ||
expect(Peptfilter.checkLength('AALER', 5, 10)).toBe(true); | ||
expect(Peptfilter.checkLength('AALER', 6, 10)).toBe(false); | ||
|
||
// max length | ||
expect(Peptfilter.checkLength('AALER', 1, 4)).toBe(false); | ||
expect(Peptfilter.checkLength('AALER', 1, 5)).toBe(true); | ||
expect(Peptfilter.checkLength('AALER', 1, 6)).toBe(true); | ||
}); | ||
|
||
test('test lacks filter', async () => { | ||
expect(Peptfilter.checkLacks('AALER', ''.split(""))).toBe(true); | ||
expect(Peptfilter.checkLacks('AALER', 'BCD'.split(""))).toBe(true); | ||
expect(Peptfilter.checkLacks('AALER', 'A'.split(""))).toBe(false); | ||
expect(Peptfilter.checkLacks('AALER', 'AE'.split(""))).toBe(false); | ||
}); | ||
|
||
test('test contains filter', async () => { | ||
expect(Peptfilter.checkContains('AALER', ''.split(""))).toBe(true); | ||
expect(Peptfilter.checkContains('AALER', 'A'.split(""))).toBe(true); | ||
expect(Peptfilter.checkContains('AALER', 'AE'.split(""))).toBe(true); | ||
expect(Peptfilter.checkContains('AALER', 'BCD'.split(""))).toBe(false); | ||
expect(Peptfilter.checkContains('AALER', 'AB'.split(""))).toBe(false); | ||
}); | ||
|
||
test('test default filter from stdin', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Peptfilter(); | ||
const run = command.run(); | ||
|
||
stdin.send("AAAA\n"); | ||
stdin.send("AAAAA\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd().split("\n").length).toBe(1); | ||
}); | ||
|
||
test('test if it passes fasta from stdin', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Peptfilter(); | ||
const run = command.run(); | ||
|
||
stdin.send(">AA\n"); | ||
stdin.send("AAA\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd().split("\n").length).toBe(1); | ||
expect(output[0]).toBe(">AA\n"); | ||
}); | ||
|
||
test('test complex example from stdin', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Peptfilter({ args: ["--minlen", "4", "--maxlen", "10", "--lacks", "B", "--contains", "A"] }); | ||
const run = command.run(); | ||
|
||
stdin.send("A\n"); | ||
stdin.send("AAAAAAAAAAA\n"); | ||
stdin.send("AAAAB\n"); | ||
stdin.send("BBBBB\n"); | ||
stdin.send("CCCCC\n"); | ||
stdin.send("CCCCCA\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd().split("\n").length).toBe(1); | ||
expect(output[0]).toBe("CCCCCA\n"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this import from a "js" file? Shouldn't this be "ts"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can make .js, .ts and nothing work with the right settings. I would have prefered using no extension, but couldn't get it to work in all conditions: the typescript compiler itself, running it after compilation, running it with yarn and running it through tests. This is the only way I managed to get it to work under all conditions.