Skip to content

Commit 260a174

Browse files
committed
add all used queries
1 parent 6eae110 commit 260a174

File tree

2 files changed

+116
-58
lines changed

2 files changed

+116
-58
lines changed

src/lib/AnalysisApps/GWAS/Utils/cohortApi.tsx

Lines changed: 96 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,29 @@ export const gwasCohortApiTags = gen3Api.enhanceEndpoints({
1111

1212
// Types for API calls
1313

14-
interface ConceptOutcome {
15-
size: number,
16-
variable_type: string,
17-
concept_id: number,
18-
concept_name: string,
19-
}
20-
21-
interface CohortDefinitionQueryParams {
22-
sourceId: string;
23-
selectedTeamProject: string;
14+
interface CohortOverlap {
15+
cohort_overlap: {
16+
case_control_overlap: number;
17+
};
2418
}
2519

26-
interface CovariateQueryParams {
27-
sourceId: number;
28-
cohortDefinitionId: string;
29-
selectedCovariateIds: Array<string>;
20+
interface ConceptOutcome {
21+
size: number;
22+
variable_type: string;
23+
concept_id: number;
24+
concept_name: string;
3025
}
3126

3227
interface Covariates {
3328
variable_type: string;
34-
provided_name: string,
35-
cohort_sizes: [number, number],
36-
cohort_names: [string, string],
37-
cohort_ids: [number, number],
29+
provided_name: string;
30+
cohort_ids: [number, number];
3831
}
3932

40-
interface HistogramQueryParams {
41-
sourceId: number,
42-
cohortId: number,
43-
selectedCovariates: Array<Covariates>,
44-
outcome: ConceptOutcome,
45-
selectedConceptId: number,
33+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
34+
interface CovariateStats extends Covariates {
35+
cohort_sizes: [number, number];
36+
cohort_names: [string, string];
4637
}
4738

4839
interface ConceptStatsByHareSubset {
@@ -52,22 +43,12 @@ interface ConceptStatsByHareSubset {
5243
outcome: Array<ConceptOutcome>;
5344
}
5445

55-
56-
5746
export interface GWASCohortDefinition {
5847
cohort_definition_id: number;
5948
cohort_name: string;
6049
size: number;
6150
}
6251

63-
export interface GWASCohortDefinitionResponse {
64-
cohort_definitions_and_stats: Array<GWASCohortDefinition>;
65-
}
66-
67-
interface SourcesResponse {
68-
sources: Array<{ source_id: string; source_name: string }>;
69-
}
70-
7152
interface CovariateConceptTypeAndId {
7253
concept_type: string;
7354
concept_id: number;
@@ -79,11 +60,53 @@ interface CovariateInformation extends CovariateConceptTypeAndId {
7960
concept_name: string;
8061
}
8162

63+
/**
64+
* Query Parameters for the rtkQueries below
65+
*/
66+
67+
interface CohortDefinitionQueryParams {
68+
sourceId: string;
69+
selectedTeamProject: string;
70+
}
71+
72+
interface CovariateQueryParams {
73+
sourceId: number;
74+
cohortDefinitionId: string;
75+
selectedCovariateIds: Array<string>;
76+
}
77+
78+
interface HistogramQueryParams {
79+
sourceId: number;
80+
cohortId: number;
81+
selectedCovariates: Array<Covariates>;
82+
outcome: ConceptOutcome;
83+
selectedConceptId: number;
84+
}
85+
86+
interface OverlapQueryParams {
87+
sourceId: number;
88+
cohortAId: number;
89+
cohortBId: number;
90+
selectedCovariates: Array<Covariates>;
91+
outcome: ConceptOutcome;
92+
}
93+
94+
/**
95+
* Responses from queries below
96+
*/
97+
export interface GWASCohortDefinitionResponse {
98+
cohort_definitions_and_stats: Array<GWASCohortDefinition>;
99+
}
100+
101+
interface SourcesResponse {
102+
sources: Array<{ source_id: string; source_name: string }>;
103+
}
104+
82105
interface CovariateResponse {
83106
covariates: Array<CovariateInformation>;
84107
}
85108

86-
export interface GWASHistogramBin {
109+
export interface GWASHistogramBin {
87110
start: number;
88111
end: number;
89112
personCount: number;
@@ -93,12 +116,16 @@ export interface GWASHistogramResponse {
93116
bins: Array<GWASHistogramBin>;
94117
}
95118

96-
export const addCDFilter = (cohortId: number, otherCohortId: number, covariateArray: Array<CovariateInformation>) => {
119+
export const addCDFilter = (
120+
cohortId: number,
121+
otherCohortId: number,
122+
covariateArray: Array<Covariates>,
123+
) => {
97124
// adding an extra filter on top of the given covariateArr
98125
// to ensure that any person that belongs to _both_ cohorts
99126
// [cohortId, otherCohortId] also gets filtered out:
100127
const covariateRequest = [...covariateArray];
101-
const cdFilter = {
128+
const cdFilter: Covariates = {
102129
variable_type: 'custom_dichotomous',
103130
cohort_ids: [cohortId, otherCohortId],
104131
provided_name:
@@ -108,7 +135,6 @@ export const addCDFilter = (cohortId: number, otherCohortId: number, covariateAr
108135
return covariateRequest;
109136
};
110137

111-
112138
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
113139
endpoints: (builder) => ({
114140
getCohortDefinitions: builder.query<
@@ -167,9 +193,9 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
167193
string,
168194
ConceptStatsByHareSubset
169195
>({
170-
query: ({ outcome, cohortDefinitionId, subsetCovariates, sourceId }) => {
196+
query: ({ outcome, cohortDefinitionId, subsetCovariates, sourceId }) => {
171197
const variablesPayload = {
172-
variables: [.outcome, ...subsetCovariates],
198+
variables: [outcome, ...subsetCovariates],
173199
};
174200
return {
175201
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${sourceId}/by-cohort-definition-id/${cohortDefinitionId}/breakdown-by-concept-id/${hareConceptId}`,
@@ -209,6 +235,34 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
209235
};
210236
},
211237
}),
238+
getSimpleOverlapInfo: builder.query<CohortOverlap, OverlapQueryParams>({
239+
query: ({
240+
sourceId,
241+
cohortAId,
242+
cohortBId,
243+
selectedCovariates,
244+
outcome,
245+
}) => {
246+
const variablesPayload = {
247+
variables: [
248+
...selectedCovariates,
249+
outcome,
250+
// add extra filter to make sure we only count persons that have a HARE group as well:
251+
{
252+
variable_type: 'concept',
253+
concept_id: hareConceptId,
254+
},
255+
].filter(Boolean), // filter out any undefined or null items (e.g. in some
256+
// scenarios "outcome" and "selectedCovariates" are still null and/or empty)
257+
};
258+
259+
return {
260+
url: `${GEN3_COHORT_MIDDLEWARE_API}/cohort-stats/check-overlap/by-source-id/${sourceId}/by-cohort-definition-ids/${cohortAId}/${cohortBId}`,
261+
method: 'POST',
262+
body: JSON.stringify(variablesPayload),
263+
};
264+
},
265+
}),
212266
}),
213267
});
214268

@@ -219,4 +273,6 @@ export const {
219273
useGetCovariatesQuery,
220274
useGetCovariateStatsQuery,
221275
useGetConceptStatsByHareSubsetQuery,
276+
useGetHistogramInfoQuery,
277+
useGetSimpleOverlapInfoQuery,
222278
} = gwasCohortApi;
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import { useCallback, useState, useEffect } from 'react';
1+
import { useState, useEffect } from 'react';
22
import { useGetSourcesQuery } from '@/lib/AnalysisApps/GWAS/Utils/cohortApi';
33

44
export const useSourceFetch = () => {
55
const [sourceId, setSourceId] = useState<string | undefined>(undefined);
66
const { data, error, isError, isSuccess, isFetching } = useGetSourcesQuery();
7-
const getSources = () => {
8-
// fetch sources on initialization
97

10-
if (isSuccess) {
11-
if (Array.isArray(data?.sources) && data.sources.length === 1) {
12-
setSourceId(data.sources[0].source_id);
13-
} else {
14-
const message = `Data source received in an invalid format: ${JSON.stringify(
15-
data?.sources,
16-
)}`;
8+
useEffect(() => {
9+
const getSources = () => {
10+
// fetch sources on initialization
11+
12+
if (isSuccess) {
13+
if (Array.isArray(data?.sources) && data.sources.length === 1) {
14+
setSourceId(data.sources[0].source_id);
15+
} else {
16+
const message = `Data source received in an invalid format: ${JSON.stringify(
17+
data?.sources,
18+
)}`;
19+
throw new Error(message);
20+
}
21+
}
22+
if (isError) {
23+
const message = `Data source received an error: ${error}`;
1724
throw new Error(message);
1825
}
19-
}
20-
if (isError) {
21-
const message = `Data source received an error: ${error}`;
22-
throw new Error(message);
23-
}
24-
};
25-
useEffect(() => {
26+
};
27+
2628
getSources();
27-
}, [getSources]);
29+
}, [data?.sources, error, isError, isSuccess]);
2830
return { isFetching, sourceId };
2931
};

0 commit comments

Comments
 (0)