Skip to content

Commit

Permalink
add basic csv formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jul 8, 2024
1 parent 5da999c commit bcd9535
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
20 changes: 17 additions & 3 deletions lib/commands/unipept/unipept_subcommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Command, Option } from "commander";
import { createReadStream, readFileSync } from "fs";
import { createInterface } from "node:readline";
import { Interface } from "readline";
import { Formatter } from "../../formatters/formatter.js";
import { FormatterFactory } from "../../formatters/formatter_factory.js";

export abstract class UnipeptSubcommand {
public command: Command;
Expand All @@ -14,6 +16,8 @@ export abstract class UnipeptSubcommand {
url?: string;
selectedFields?: RegExp[];
fasta: boolean;
formatter?: Formatter;
firstBatch = true;

constructor(name: string) {
this.name = name;
Expand Down Expand Up @@ -48,6 +52,7 @@ export abstract class UnipeptSubcommand {
this.options = options;
this.host = this.getHost();
this.url = `${this.host}/api/v2/${this.name}.json`;
this.formatter = FormatterFactory.getFormatter(this.options.format);

let slice = [];

Expand All @@ -62,6 +67,8 @@ export abstract class UnipeptSubcommand {
}

async processBatch(slice: string[]): Promise<void> {
if (!this.formatter) throw new Error("Formatter not set");

const r = await fetch(this.url as string, {
method: "POST",
body: this.constructRequestBody(slice),
Expand All @@ -70,7 +77,14 @@ export abstract class UnipeptSubcommand {
"User-Agent": this.user_agent,
}
});
console.log(await r.json());
const result = await r.json();

if (this.firstBatch) {
this.firstBatch = false;
process.stdout.write(this.formatter.header(result, this.fasta));
}

process.stdout.write(this.formatter.format(result, this.fasta));
}

private constructRequestBody(slice: string[]): URLSearchParams {
Expand All @@ -84,9 +98,9 @@ export abstract class UnipeptSubcommand {
}

private getSelectedFields(): RegExp[] {
if (this.selectedFields) return this.getSelectedFields();
if (this.selectedFields) return this.selectedFields;

const fields = (this.options.fields as string[]).flatMap(f => f.split(","));
const fields = (this.options.select as string[])?.flatMap(f => f.split(",")) ?? [];
if (this.fasta && fields.length > 0) {
fields.push(...this.requiredFields());
}
Expand Down
21 changes: 21 additions & 0 deletions lib/formatters/csv_formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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)]);
}

footer(): string {
return "";
}

convert(data: object[]): string {
return stringify(data);
}

getKeys(data: { [key: string]: string }[], fastaMapper?: boolean | undefined): string[] {
return fastaMapper ? ["fasta_header", ...Object.keys(data[0])] : Object.keys(data[0]);
}
}
19 changes: 19 additions & 0 deletions lib/formatters/formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CSVFormatter } from "./csv_formatter.js";

export abstract class Formatter {

abstract header(sampleData: object, fastaMapper?: boolean): string;
abstract footer(): string;
abstract convert(data: any, first?: boolean): string;

format(data, fastaMapper?: boolean, first?: boolean): string {
if (fastaMapper) {
data = this.integrateFastaHeaders(data, fastaMapper);
}
return this.convert(data, first);
}

integrateFastaHeaders(data: any, fastaMapper: boolean): any {
return data;
}
}
11 changes: 11 additions & 0 deletions lib/formatters/formatter_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CSVFormatter } from "./csv_formatter.js";
import { Formatter } from "./formatter.js";

export class FormatterFactory {
static getFormatter(name: string): Formatter {
if (name === "csv") {
return new CSVFormatter();
}
return new CSVFormatter();
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"uniprot": "yarn run tsx bin/uniprot.ts"
},
"dependencies": {
"commander": "^12.1.0"
"commander": "^12.1.0",
"csv-stringify": "^6.5.0"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,11 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

csv-stringify@^6.5.0:
version "6.5.0"
resolved "https://registry.yarnpkg.com/csv-stringify/-/csv-stringify-6.5.0.tgz#7b1491893c917e018a97de9bf9604e23b88647c2"
integrity sha512-edlXFVKcUx7r8Vx5zQucsuMg4wb/xT6qyz+Sr1vnLrdXqlLD1+UKyWNyZ9zn6mUW1ewmGxrpVwAcChGF0HQ/2Q==

debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
Expand Down

0 comments on commit bcd9535

Please sign in to comment.