-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reference service slice + gene search hook
- Loading branch information
1 parent
50ea993
commit a7662f4
Showing
7 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
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,45 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useAuthorizationHeader } from 'bento-auth-js'; | ||
import { referenceGenomesUrl } from '@/constants/configConstants'; | ||
import { RequestStatus } from '@/types/requests'; | ||
import type { GenomeFeature } from './types'; | ||
|
||
export const useGeneNameSearch = (referenceGenomeID: string | undefined, nameQuery: string | null | undefined) => { | ||
const authHeader = useAuthorizationHeader(); | ||
|
||
const [status, setStatus] = useState<RequestStatus>(RequestStatus.Idle); | ||
const [data, setData] = useState<GenomeFeature[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if (!referenceGenomeID || !nameQuery) return; | ||
|
||
const params = new URLSearchParams({ name: nameQuery, name_fzy: 'true', limit: '10' }); | ||
const searchUrl = `${referenceGenomesUrl}/${referenceGenomeID}/features?${params.toString()}`; | ||
|
||
setError(null); | ||
|
||
(async () => { | ||
setStatus(RequestStatus.Pending); | ||
|
||
try { | ||
const res = await fetch(searchUrl, { headers: { Accept: 'application/json', ...authHeader } }); | ||
const resData = await res.json(); | ||
if (res.ok) { | ||
console.debug('Genome feature search - got results:', resData.results); | ||
setData(resData.results); | ||
setStatus(RequestStatus.Fulfilled); | ||
} else { | ||
setError(`Genome feature search failed with message: ${resData.message}`); | ||
setStatus(RequestStatus.Rejected); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
setError(`Genome feature search failed: ${(e as Error).toString()}`); | ||
setStatus(RequestStatus.Rejected); | ||
} | ||
})(); | ||
}, [referenceGenomeID, nameQuery, authHeader]); | ||
|
||
return { status, data, error }; | ||
}; |
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,60 @@ | ||
import axios from 'axios'; | ||
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; | ||
|
||
import { referenceGenomesUrl } from '@/constants/configConstants'; | ||
import type { RootState } from '@/store'; | ||
import { RequestStatus } from '@/types/requests'; | ||
import { printAPIError } from '@/utils/error.util'; | ||
|
||
import type { Genome } from './types'; | ||
|
||
const storeName = 'reference'; | ||
|
||
export type ReferenceState = { | ||
genomesStatus: RequestStatus; | ||
genomes: Genome[]; | ||
genomesByID: Record<string, Genome>; | ||
}; | ||
|
||
const initialState: ReferenceState = { | ||
genomesStatus: RequestStatus.Idle, | ||
genomes: [], | ||
genomesByID: {}, | ||
}; | ||
|
||
export const getGenomes = createAsyncThunk<Genome[], void, { state: RootState }>( | ||
`${storeName}/getGenomes`, | ||
(_, { rejectWithValue }) => { | ||
return axios | ||
.get(referenceGenomesUrl) | ||
.then((res) => res.data) | ||
.catch(printAPIError(rejectWithValue)); | ||
}, | ||
{ | ||
condition(_, { getState }) { | ||
const { genomesStatus } = getState().reference; | ||
return genomesStatus === RequestStatus.Idle; | ||
}, | ||
} | ||
); | ||
|
||
const reference = createSlice({ | ||
name: storeName, | ||
initialState, | ||
reducers: {}, | ||
extraReducers(builder) { | ||
builder.addCase(getGenomes.pending, (state) => { | ||
state.genomesStatus = RequestStatus.Pending; | ||
}); | ||
builder.addCase(getGenomes.fulfilled, (state, { payload }) => { | ||
state.genomes = payload; | ||
state.genomesByID = Object.fromEntries(payload.map((g) => [g.id, g])); | ||
state.genomesStatus = RequestStatus.Fulfilled; | ||
}); | ||
builder.addCase(getGenomes.rejected, (state) => { | ||
state.genomesStatus = RequestStatus.Rejected; | ||
}); | ||
}, | ||
}); | ||
|
||
export default reference.reducer; |
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,41 @@ | ||
import type { OntologyTerm } from '@/types/ontology'; | ||
|
||
// See also: https://github.com/bento-platform/bento_reference_service/blob/main/bento_reference_service/models.py | ||
|
||
export type Contig = { | ||
name: string; | ||
aliases: string[]; | ||
md5: string; | ||
ga4gh: string; | ||
length: number; | ||
circular: boolean; | ||
refget_uris: string[]; | ||
}; | ||
|
||
export type Genome = { | ||
id: string; | ||
aliases: string[]; | ||
md5: string; | ||
ga4gh: string; | ||
fasta: string; | ||
fai: string; | ||
gff3_gz: string; | ||
gff3_gz_tbi: string; | ||
taxon: OntologyTerm; | ||
contigs: Contig[]; | ||
uri: string; | ||
}; | ||
|
||
export type GenomeFeature = { | ||
genome_id: string; | ||
contig_name: string; | ||
|
||
strand: '-' | '+' | '?' | '.'; | ||
|
||
feature_id: string; | ||
feature_name: string; | ||
feature_type: string; | ||
|
||
source: string; | ||
entries: { start_pos: number; end_pos: number; score: number | null; phase: number | null }[]; | ||
}; |
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,4 @@ | ||
export type OntologyTerm = { | ||
id: string; | ||
label: string; | ||
}; |