Skip to content

Commit

Permalink
add pept2ec test
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Aug 6, 2024
1 parent 931f810 commit d544627
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default [
{
rules: {
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
"@typescript-eslint/ban-ts-comment": "off",
},
ignores: ["dist/"]
}
Expand Down
23 changes: 20 additions & 3 deletions lib/formatters/csv_formatter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { get } from "http";

Check failure on line 1 in lib/formatters/csv_formatter.ts

View workflow job for this annotation

GitHub Actions / Lint

'get' is defined but never used
import { Formatter } from "./formatter.js";
import { stringify } from "csv-stringify/sync";

export class CSVFormatter extends Formatter {

header(sampleData: { [key: string]: string }[], fastaMapper?: boolean | undefined): string {
return stringify([this.getKeys(sampleData, fastaMapper)]);
return stringify([this.getKeys(this.flatten(sampleData), fastaMapper)]);
}

footer(): string {
return "";
}

convert(data: object[]): string {
return stringify(data);
return stringify(this.flatten(data as { [key: string]: unknown }[]));
}

getKeys(data: { [key: string]: string }[], fastaMapper?: boolean | undefined): string[] {
getKeys(data: { [key: string]: unknown }[], fastaMapper?: boolean | undefined): string[] {
return fastaMapper ? ["fasta_header", ...Object.keys(data[0])] : Object.keys(data[0]);
}

flatten(data: { [key: string]: unknown }[]): { [key: string]: unknown }[] {
if (this.getKeys(data).includes("ec")) {
// @ts-ignore
const keys = Object.keys(data[0].ec[0]);
data.forEach(row => {
keys.forEach(key => {
const newKey = key.startsWith("ec") ? key : `ec_${key}`;
// @ts-ignore
row[newKey] = row.ec.map(e => e[key]).join(" ");
});
delete row.ec;
});
}
return data;
}
}
27 changes: 27 additions & 0 deletions tests/commands/unipept/pept2ec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { jest } from '@jest/globals';
import { Pept2ec } from "../../../lib/commands/unipept/pept2ec";

let output: string[];
const writeSpy = jest

Check failure on line 5 in tests/commands/unipept/pept2ec.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'writeSpy' is assigned a value but never used
.spyOn(process.stdout, "write")
.mockImplementation((data: unknown) => { output.push(data as string); return true; });

beforeEach(() => {
output = [];
});

test('test with default args', async () => {
const command = new Pept2ec();
await command.run(["AALTER"], { header: true, format: "csv" });
expect(output[0].startsWith("peptide,total_protein_count,ec_number,ec_protein_count")).toBeTruthy();
expect(output[1].startsWith("AALTER,3310,2.3.2.27 3.1.3.3")).toBeTruthy();
expect(output.length).toBe(2);
});

test('test with fasta', async () => {
const command = new Pept2ec();
await command.run([">test", "AALTER"], { header: true, format: "csv" });
expect(output[0].startsWith("fasta_header,peptide,total_protein_count,ec_number,ec_protein_count")).toBeTruthy();
expect(output[1].startsWith(">test,AALTER,3310,2.3.2.27 3.1.3.3")).toBeTruthy();
expect(output.length).toBe(2);
});
27 changes: 27 additions & 0 deletions tests/commands/unipept/pept2lca.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { jest } from '@jest/globals';
import { Pept2lca } from "../../../lib/commands/unipept/pept2lca";

let output: string[];
const writeSpy = jest

Check failure on line 5 in tests/commands/unipept/pept2lca.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'writeSpy' is assigned a value but never used
.spyOn(process.stdout, "write")
.mockImplementation((data: unknown) => { output.push(data as string); return true; });

beforeEach(() => {
output = [];
});

test('test with default args', async () => {
const command = new Pept2lca();
await command.run(["AALTER"], { header: true, format: "csv" });
expect(output[0].startsWith("peptide,taxon_id")).toBeTruthy();
expect(output[1].startsWith("AALTER,1,root,no rank")).toBeTruthy();
expect(output.length).toBe(2);
});

test('test with fasta', async () => {
const command = new Pept2lca();
await command.run([">test", "AALTER"], { header: true, format: "csv" });
expect(output[0].startsWith("fasta_header,peptide,taxon_id")).toBeTruthy();
expect(output[1].startsWith(">test,AALTER,1,root,no rank")).toBeTruthy();
expect(output.length).toBe(2);
});

0 comments on commit d544627

Please sign in to comment.