-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -1,21 +1,38 @@ | ||
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 { | ||
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; | ||
} | ||
} |
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,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); | ||
}); |
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,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); | ||
}); |