diff --git a/lib/commands/uniprot.ts b/lib/commands/uniprot.ts index e0946251..c09b3d8e 100644 --- a/lib/commands/uniprot.ts +++ b/lib/commands/uniprot.ts @@ -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. * diff --git a/tests/commands/uniprot.test.ts b/tests/commands/uniprot.test.ts index 8afd461d..3077cdc5 100644 --- a/tests/commands/uniprot.test.ts +++ b/tests/commands/uniprot.test.ts @@ -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"]);