diff --git a/lib/commands/peptfilter.ts b/lib/commands/peptfilter.ts index 997801f2..48bba41f 100644 --- a/lib/commands/peptfilter.ts +++ b/lib/commands/peptfilter.ts @@ -22,9 +22,8 @@ The input should have one peptide per line. FASTA headers are preserved in the o async run() { this.parseArguments(); - console.log(this.program.opts()) - const minLen = this.program.opts().minLen; - const maxlen = this.program.opts().maxLen; + const minLen = this.program.opts().minlen; + const maxlen = this.program.opts().maxlen; const lacks = this.program.opts().lacks || []; const contains = this.program.opts().contains || []; @@ -33,6 +32,7 @@ The input should have one peptide per line. FASTA headers are preserved in the o 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"); } diff --git a/tests/commands/peptfilter.test.ts b/tests/commands/peptfilter.test.ts new file mode 100644 index 00000000..07bb7f20 --- /dev/null +++ b/tests/commands/peptfilter.test.ts @@ -0,0 +1,98 @@ +import { Peptfilter } from '../../lib/commands/peptfilter'; +import * as mock from 'mock-stdin'; + +let output: string[]; +let error: string[]; +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(writeSpy).toHaveBeenCalledTimes(1); + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output.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(writeSpy).toHaveBeenCalledTimes(1); + expect(errorSpy).toHaveBeenCalledTimes(0); + 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(writeSpy).toHaveBeenCalledTimes(1); + expect(errorSpy).toHaveBeenCalledTimes(0); + expect(output[0]).toBe("CCCCCA\n"); +});