Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/commands/uniprot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,38 @@ The uniprot command yields just the protein sequences as a default, but can retu

// alternatively, we can also wrap the array in a Readable stream with ReadableStream.from()
const input = accessions.length !== 0 ? accessions : createInterface({ input: process.stdin });

const BATCH_SIZE = 5;
let batch: string[] = [];

for await (const line of input) {
await Uniprot.processUniprotEntry(line.trim(), format);
const accession = line.trim();
if (!accession) continue;

batch.push(accession);
if (batch.length >= BATCH_SIZE) {
await Uniprot.processBatch(batch, format);
batch = [];
}
}

if (batch.length > 0) {
await Uniprot.processBatch(batch, format);
}
}

/**
* Fetches a batch of UniProt entries and writes them to standard output in order.
*
* @param accessions List of UniProt Accession Numbers
* @param format output format
*/
static async processBatch(accessions: string[], format: string) {
const promises = accessions.map(acc => Uniprot.getUniprotEntry(acc, format));
const results = await Promise.all(promises);
results.forEach(result => process.stdout.write(result + "\n"));
}

/**
* Fetches a UniProt entry and writes it to standard output.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/commands/uniprot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ test('test double line stdin', async () => {
expect(output.length).toBe(2);
});

test('test multiple batches', async () => {
const command = new Uniprot();
// 7 items > BATCH_SIZE of 5
await command.run(["Q6GZX3", "Q6GZX4", "Q6GZX3", "Q6GZX4", "Q6GZX3", "Q6GZX4", "Q6GZX3"]);

expect(writeSpy).toHaveBeenCalledTimes(7);
expect(errorSpy).toHaveBeenCalledTimes(0);
expect(output.length).toBe(7);
});

test('test on invalid id', async () => {
const command = new Uniprot();
await command.run(["Bart"]);
Expand Down