-
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 prot2pept to typescript #176
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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 { Prot2pept } from '../lib/commands/prot2pept.js'; | ||
|
||
const command = new Prot2pept(); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { createInterface } from 'node:readline'; | ||
import { BaseCommand } from './base_command.js'; | ||
|
||
export class Prot2pept extends BaseCommand { | ||
|
||
readonly description = `The prot2pept command splits each protein sequence into a list of peptides according to a given cleavage-pattern. The command expects a list of protein sequences that are passed to standard input. | ||
|
||
The input should have either one protein sequence per line or contain a FASTA formatted list of protein sequences. FASTA headers are preserved in the output, so that peptides can be bundled per protein sequence. | ||
`; | ||
|
||
constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { | ||
super(options); | ||
|
||
this.program | ||
.summary("Splits each protein sequence into a list of peptides.") | ||
.description(this.description) | ||
.option("-p, --pattern <regex>", "specify cleavage-pattern (regex) as the pattern after which the next peptide will be cleaved. By default, it will create tryptic peptides.", "([KR])([^P])") | ||
} | ||
|
||
/** | ||
* Performance note: Just as with peptfilter, this implementation can be made faster by using line events instead of | ||
* async iterators. | ||
*/ | ||
async run() { | ||
this.parseArguments(); | ||
|
||
let pattern; | ||
try { | ||
pattern = new RegExp(this.program.opts().pattern, "g"); | ||
} catch (e) { | ||
this.program.error(`Your pattern was invalid: ${(e as Error).message}`); | ||
} | ||
|
||
let fasta = false; | ||
let protein = []; | ||
|
||
// buffering output makes a big difference in performance | ||
let output = []; | ||
let i = 0; | ||
|
||
for await (const line of createInterface({ input: process.stdin })) { | ||
if (i === 0 && line.startsWith(">")) { | ||
fasta = true; | ||
} | ||
|
||
i++; | ||
|
||
if (fasta) { // if we're in fasta mode, a protein could be split over multiple lines | ||
if (line.startsWith(">")) { // if we encounter a new header, process the previous protein and output the current header | ||
if (protein.length > 0) { | ||
output.push(Prot2pept.splitProtein(protein.join(""), pattern)); | ||
} | ||
output.push(line.trimEnd()); | ||
protein = []; | ||
} else { | ||
protein.push(line.trimEnd()); | ||
} | ||
} else { // if we're not in fasta mode, each line is a protein sequence | ||
output.push(Prot2pept.splitProtein(line.trimEnd(), pattern)); | ||
} | ||
|
||
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 = []; | ||
} | ||
} | ||
|
||
if (fasta) { // if in fasta mode, process the last protein | ||
output.push(Prot2pept.splitProtein(protein.join(""), pattern)); | ||
} | ||
output.push(""); | ||
process.stdout.write(output.join("\n")); | ||
} | ||
|
||
static splitProtein(line: string, pattern: RegExp): string { | ||
return line.replaceAll(pattern, "$1\n$2").replaceAll(pattern, "$1\n$2").replaceAll("\n\n", "\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
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,124 @@ | ||
import { Prot2pept } from '../../lib/commands/prot2pept'; | ||
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 single line input 1', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send("AALTERAALTERPAALTER\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER"); | ||
}); | ||
|
||
test('test single line input 2', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send("KRKPR\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe("K\nR\nKPR"); | ||
}); | ||
|
||
test('test multi line input', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send("AALTERAALTERPAALTER\n"); | ||
stdin.send("AALTERAA\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe("AALTER\nAALTERPAALTER\nAALTER\nAA"); | ||
}); | ||
|
||
test('test fasta input 1', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send(">AKA\nAALTERAALTERPAALTER\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER"); | ||
}); | ||
|
||
test('test fasta input 2', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send(">AKA\nAAL\nT\nERAALTER\nP\nAALTER\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe(">AKA\nAALTER\nAALTERPAALTER"); | ||
}); | ||
|
||
test('test fasta input 3', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept(); | ||
const run = command.run(); | ||
|
||
stdin.send(">AKA\nAAL\nT\n>\nERAALTER\nP\nAALTER"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe(">AKA\nAALT\n>\nER\nAALTERPAALTER"); | ||
}); | ||
|
||
test('test custom pattern', async () => { | ||
const stdin = mock.stdin(); | ||
|
||
const command = new Prot2pept({ args: ["--pattern", "([KR])([^A])"] }); | ||
const run = command.run(); | ||
|
||
stdin.send("AALTERAALTERPAALTER\n"); | ||
stdin.end(); | ||
|
||
await run; | ||
|
||
expect(errorSpy).toHaveBeenCalledTimes(0); | ||
expect(output.join("").trimEnd()).toBe("AALTERAALTER\nPAALTER"); | ||
}); |
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 |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
"module": "NodeNext", | ||
"strict": true, | ||
"resolveJsonModule": true, | ||
"target": "esnext" | ||
} | ||
} |
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.
I'm probably overlooking something, but why do we perform
replaceAll(pattern, "$1\n$2")
twice with the same pattern?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.
It's to catch overlapping patterns, like KRKRK