diff --git a/eslint.config.js b/eslint.config.js index 3f8a7e2e..195212d1 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -10,6 +10,7 @@ export default [ { rules: { "@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }], + "@typescript-eslint/ban-ts-comment": "off", }, ignores: ["dist/"] } diff --git a/lib/formatters/csv_formatter.ts b/lib/formatters/csv_formatter.ts index 39b8657f..7cd36212 100644 --- a/lib/formatters/csv_formatter.ts +++ b/lib/formatters/csv_formatter.ts @@ -1,10 +1,11 @@ +import { get } from "http"; 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 { @@ -12,10 +13,26 @@ export class CSVFormatter extends Formatter { } 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; + } } diff --git a/tests/commands/unipept/pept2ec.test.ts b/tests/commands/unipept/pept2ec.test.ts new file mode 100644 index 00000000..ee789bed --- /dev/null +++ b/tests/commands/unipept/pept2ec.test.ts @@ -0,0 +1,27 @@ +import { jest } from '@jest/globals'; +import { Pept2ec } from "../../../lib/commands/unipept/pept2ec"; + +let output: string[]; +const writeSpy = jest + .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); +}); diff --git a/tests/commands/unipept/pept2lca.test.ts b/tests/commands/unipept/pept2lca.test.ts new file mode 100644 index 00000000..0b18c6ac --- /dev/null +++ b/tests/commands/unipept/pept2lca.test.ts @@ -0,0 +1,27 @@ +import { jest } from '@jest/globals'; +import { Pept2lca } from "../../../lib/commands/unipept/pept2lca"; + +let output: string[]; +const writeSpy = jest + .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); +});