Skip to content

Commit

Permalink
add a few utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jul 2, 2024
1 parent d135611 commit 5da999c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/commands/unipept/pept2lca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ The command will give priority to the first way tryptic peptides are passed, in
.action((args, options) => this.run(args, options));
}

defaultBatchSize(): number {
return 100;
requiredFields(): string[] {
return ["peptide"];
}

async run(args: string[], options: object) {
await super.run(args, options);
defaultBatchSize(): number {
return 100;
}
}
35 changes: 30 additions & 5 deletions lib/commands/unipept/unipept_subcommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export abstract class UnipeptSubcommand {
user_agent: string;
host = "https://api.unipept.ugent.be";
url?: string;
selectedFields?: RegExp[];
fasta: boolean;

constructor(name: string) {
this.name = name;
Expand All @@ -22,6 +24,10 @@ export abstract class UnipeptSubcommand {
}
abstract defaultBatchSize(): number;

requiredFields(): string[] {
return [];
}

create(name: string): Command {
const command = new Command(name);

Expand All @@ -47,7 +53,7 @@ export abstract class UnipeptSubcommand {

for await (const input of this.getInputIterator(args, options.input)) {
slice.push(input);
if (slice.length >= this.defaultBatchSize()) {
if (slice.length >= this.batchSize) {
await this.processBatch(slice);
slice = [];
}
Expand All @@ -68,7 +74,7 @@ export abstract class UnipeptSubcommand {
}

private constructRequestBody(slice: string[]): URLSearchParams {
const names = this.constructSelectedFields().length === 0 || this.constructSelectedFields().some(regex => regex.toString().includes("name") || regex.toString().includes(".*$"));
const names = this.getSelectedFields().length === 0 || this.getSelectedFields().some(regex => regex.toString().includes("name") || regex.toString().includes(".*$"));
return new URLSearchParams({
input: JSON.stringify(slice),
equate_il: this.options.equate,
Expand All @@ -77,9 +83,24 @@ export abstract class UnipeptSubcommand {
});
}

// TODO: implement
private constructSelectedFields(): RegExp[] {
return [];
private getSelectedFields(): RegExp[] {
if (this.selectedFields) return this.getSelectedFields();

const fields = (this.options.fields as string[]).flatMap(f => f.split(","));
if (this.fasta && fields.length > 0) {
fields.push(...this.requiredFields());
}
this.selectedFields = fields.map(f => this.globToRegex(f));

return this.selectedFields;
}

private get batchSize(): number {
if (this.options.batch) {
return +this.options.batch;
} else {
return this.defaultBatchSize();
}
}

/**
Expand Down Expand Up @@ -108,4 +129,8 @@ export abstract class UnipeptSubcommand {
return `http://${host}`;
}
}

private globToRegex(glob: string): RegExp {
return new RegExp(glob.replace(/\*/g, ".*"));
}
}

0 comments on commit 5da999c

Please sign in to comment.