-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): add direct fasta download of a sequence (#807)
- Loading branch information
1 parent
4cdda5d
commit 4fa930e
Showing
6 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
website/src/pages/[organism]/seq/[accessionVersion].fa/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { APIRoute } from 'astro'; | ||
import { err, type Result } from 'neverthrow'; | ||
|
||
import { getReferenceGenomes } from '../../../../config.ts'; | ||
import { routes } from '../../../../routes.ts'; | ||
import { LapisClient } from '../../../../services/lapisClient.ts'; | ||
import type { ProblemDetail } from '../../../../types/backend.ts'; | ||
import { parseAccessionVersionFromString } from '../../../../utils/extractAccessionVersion.ts'; | ||
import { fastaEntryToString, parseFasta } from '../../../../utils/parseFasta.ts'; | ||
|
||
export const GET: APIRoute = async ({ params, redirect }) => { | ||
const accessionVersion = params.accessionVersion!; | ||
const organism = params.organism!; | ||
|
||
const result = await getSequenceDetailsUnalignedFasta(accessionVersion, organism); | ||
if (!result.isOk()) { | ||
return new Response(undefined, { | ||
status: 404, | ||
}); | ||
} | ||
|
||
if (result.value.type === ResultType.REDIRECT) { | ||
return redirect(result.value.redirectUrl); | ||
} | ||
|
||
return new Response(result.value.fasta, { | ||
headers: { | ||
'Content-Type': 'text/x-fasta', | ||
}, | ||
}); | ||
}; | ||
|
||
enum ResultType { | ||
DATA = 'data', | ||
REDIRECT = 'redirect', | ||
} | ||
|
||
type Data = { | ||
type: ResultType.DATA; | ||
fasta: string; | ||
}; | ||
|
||
type Redirect = { | ||
type: ResultType.REDIRECT; | ||
redirectUrl: string; | ||
}; | ||
|
||
const getSequenceDetailsUnalignedFasta = async ( | ||
accessionVersion: string, | ||
organism: string, | ||
): Promise<Result<Data | Redirect, ProblemDetail>> => { | ||
const { accession, version } = parseAccessionVersionFromString(accessionVersion); | ||
|
||
const lapisClient = LapisClient.createForOrganism(organism); | ||
|
||
if (version === undefined) { | ||
const latestVersionResult = await lapisClient.getLatestAccessionVersion(accession); | ||
return latestVersionResult.map((latestVersion) => ({ | ||
type: ResultType.REDIRECT, | ||
redirectUrl: routes.sequencesFastaPage(organism, latestVersion), | ||
})); | ||
} | ||
|
||
const referenceGenomes = getReferenceGenomes(organism); | ||
const segmentNames = referenceGenomes.nucleotideSequences.map((s) => s.name); | ||
const isMultiSegmented = segmentNames.length > 1; | ||
|
||
const fastaResult: Result<string, ProblemDetail> = !isMultiSegmented | ||
? await 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('\n'), | ||
); | ||
if (fastaResult.isOk()) { | ||
if (fastaResult.value.trim().length === 0) { | ||
return err({ | ||
type: 'about:blank', | ||
title: 'Not Found', | ||
status: 0, | ||
detail: 'No data found for accession version ' + accessionVersion, | ||
instance: '/seq/' + accessionVersion + '.fa', | ||
}); | ||
} | ||
} | ||
const withNewLineTermination = fastaResult.map((fasta) => `${fasta}\n`); | ||
|
||
return withNewLineTermination.map((fasta) => ({ | ||
type: ResultType.DATA, | ||
fasta, | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { routes } from '../../../src/routes.ts'; | ||
import { baseUrl, dummyOrganism, expect, test, testSequenceEntry } from '../../e2e.fixture'; | ||
|
||
test.describe('The sequence.fa page', () => { | ||
test('can load and show fasta file', async () => { | ||
const url = `${baseUrl}${routes.sequencesFastaPage(dummyOrganism.key, testSequenceEntry)}`; | ||
const content = await (await fetch(url)).text(); | ||
expect(content).toBe(`>${testSequenceEntry.name}\n${testSequenceEntry.unaligned}\n`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters