From 3f5f678f6e5e8dde62258dd87c74b94d5c8b2274 Mon Sep 17 00:00:00 2001 From: Felix Hennig Date: Thu, 17 Oct 2024 11:23:00 +0200 Subject: [PATCH] split up function for clarity and reusability --- .../pages/seq/[accessionVersion].fa/index.ts | 20 ++----------- website/src/pages/seq/sequenceDownload.ts | 2 +- website/src/services/lapisClient.ts | 29 ++++++++++++++++++- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/website/src/pages/seq/[accessionVersion].fa/index.ts b/website/src/pages/seq/[accessionVersion].fa/index.ts index 814054e6c..7a4bec632 100644 --- a/website/src/pages/seq/[accessionVersion].fa/index.ts +++ b/website/src/pages/seq/[accessionVersion].fa/index.ts @@ -3,7 +3,6 @@ import type { APIRoute } from 'astro'; import { getReferenceGenomes } from '../../../config.ts'; import { routes } from '../../../routes/routes.ts'; import { LapisClient } from '../../../services/lapisClient.ts'; -import { fastaEntryToString, parseFasta } from '../../../utils/parseFasta.ts'; import { createDownloadAPIRoute } from '../sequenceDownload.ts'; export const GET: APIRoute = createDownloadAPIRoute( @@ -17,22 +16,7 @@ export const GET: APIRoute = createDownloadAPIRoute( const isMultiSegmented = segmentNames.length > 1; return !isMultiSegmented - ? lapisClient.getUnalignedSequences(accessionVersion) - : (await lapisClient.getUnalignedSequencesMultiSegment(accessionVersion, segmentNames)).map( - (segmentFastas) => - segmentFastas - .map((fasta, i) => { - const parsed = parseFasta(fasta); - if (parsed.length === 0) { - return ''; - } - const withSegmentSuffix = { - name: `${parsed[0].name}_${segmentNames[i]}`, - sequence: parsed[0].sequence, - }; - return fastaEntryToString([withSegmentSuffix]); - }) - .join(''), - ); + ? lapisClient.getSequenceFasta(accessionVersion) + : lapisClient.getMultiSegmentSequenceFasta(accessionVersion, segmentNames); }, ); diff --git a/website/src/pages/seq/sequenceDownload.ts b/website/src/pages/seq/sequenceDownload.ts index 99a120b28..848221451 100644 --- a/website/src/pages/seq/sequenceDownload.ts +++ b/website/src/pages/seq/sequenceDownload.ts @@ -24,7 +24,7 @@ export type DataDownloader = (accessionVersion: string, organism: string) => Pro /** * Given a data fetcher function and some information about the data, this will create an API route - * That fetches the data. + * that fetches the data. * @param contentType The content type to use in the HTTP header * @param fileSuffix the file suffix for this data type (i.e. 'fa' or 'tsv') * @param undefinedVersionRedirectUrl In case the accessionVersion has no version and it needs to be determined diff --git a/website/src/services/lapisClient.ts b/website/src/services/lapisClient.ts index d66220676..3682e8f0f 100644 --- a/website/src/services/lapisClient.ts +++ b/website/src/services/lapisClient.ts @@ -21,6 +21,7 @@ import { type SequenceEntryHistory, versionStatuses, } from '../types/lapis.ts'; +import { fastaEntryToString, parseFasta } from '../utils/parseFasta.ts'; import type { BaseType } from '../utils/sequenceTypeHelpers.ts'; export class LapisClient extends ZodiosWrapperClient { @@ -53,7 +54,7 @@ export class LapisClient extends ZodiosWrapperClient { }); } - public getSequenceEntryVersionDetailsTsv(accessionVersion: string) { + public getSequenceEntryVersionDetailsTsv(accessionVersion: string): Promise> { return this.call('details', { [this.schema.primaryKey]: accessionVersion, dataFormat: 'TSV', @@ -165,4 +166,30 @@ export class LapisClient extends ZodiosWrapperClient { ); return Result.combine(results); } + + public getSequenceFasta(accessionVersion: string): Promise> { + return this.getUnalignedSequences(accessionVersion); + } + + public async getMultiSegmentSequenceFasta( + accessionVersion: string, + segmentNames: string[], + ): Promise> { + const segments = await this.getUnalignedSequencesMultiSegment(accessionVersion, segmentNames); + return segments.map((segmentFastas) => + segmentFastas + .map((fasta, i) => { + const parsed = parseFasta(fasta); + if (parsed.length === 0) { + return ''; + } + const withSegmentSuffix = { + name: `${parsed[0].name}_${segmentNames[i]}`, + sequence: parsed[0].sequence, + }; + return fastaEntryToString([withSegmentSuffix]); + }) + .join(''), + ); + } }