Skip to content

Commit

Permalink
split up function for clarity and reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig authored and corneliusroemer committed Oct 17, 2024
1 parent ac63762 commit 3f5f678
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
20 changes: 2 additions & 18 deletions website/src/pages/seq/[accessionVersion].fa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
},
);
2 changes: 1 addition & 1 deletion website/src/pages/seq/sequenceDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion website/src/services/lapisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof lapisApi> {
Expand Down Expand Up @@ -53,7 +54,7 @@ export class LapisClient extends ZodiosWrapperClient<typeof lapisApi> {
});
}

public getSequenceEntryVersionDetailsTsv(accessionVersion: string) {
public getSequenceEntryVersionDetailsTsv(accessionVersion: string): Promise<Result<string, ProblemDetail>> {
return this.call('details', {
[this.schema.primaryKey]: accessionVersion,
dataFormat: 'TSV',
Expand Down Expand Up @@ -165,4 +166,30 @@ export class LapisClient extends ZodiosWrapperClient<typeof lapisApi> {
);
return Result.combine(results);
}

public getSequenceFasta(accessionVersion: string): Promise<Result<string, ProblemDetail>> {
return this.getUnalignedSequences(accessionVersion);
}

public async getMultiSegmentSequenceFasta(
accessionVersion: string,
segmentNames: string[],
): Promise<Result<string, ProblemDetail>> {
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(''),
);
}
}

0 comments on commit 3f5f678

Please sign in to comment.