Skip to content

Commit 6eae110

Browse files
committed
add more queries: histogram
1 parent adb96e9 commit 6eae110

File tree

3 files changed

+350
-10
lines changed

3 files changed

+350
-10
lines changed

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

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ 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+
1421
interface CohortDefinitionQueryParams {
1522
sourceId: string;
1623
selectedTeamProject: string;
@@ -22,13 +29,31 @@ interface CovariateQueryParams {
2229
selectedCovariateIds: Array<string>;
2330
}
2431

32+
interface Covariates {
33+
variable_type: string;
34+
provided_name: string,
35+
cohort_sizes: [number, number],
36+
cohort_names: [string, string],
37+
cohort_ids: [number, number],
38+
}
39+
40+
interface HistogramQueryParams {
41+
sourceId: number,
42+
cohortId: number,
43+
selectedCovariates: Array<Covariates>,
44+
outcome: ConceptOutcome,
45+
selectedConceptId: number,
46+
}
47+
2548
interface ConceptStatsByHareSubset {
2649
sourceId: number;
27-
cohortDefinitionId: string;
50+
cohortDefinitionId: number;
2851
subsetCovariates: string;
29-
outcome: Array<string>;
52+
outcome: Array<ConceptOutcome>;
3053
}
3154

55+
56+
3257
export interface GWASCohortDefinition {
3358
cohort_definition_id: number;
3459
cohort_name: string;
@@ -43,18 +68,47 @@ interface SourcesResponse {
4368
sources: Array<{ source_id: string; source_name: string }>;
4469
}
4570

46-
interface CovariateInformation {
71+
interface CovariateConceptTypeAndId {
72+
concept_type: string;
4773
concept_id: number;
74+
}
75+
76+
interface CovariateInformation extends CovariateConceptTypeAndId {
4877
prefixed_concept_id: string;
4978
concept_code: string;
5079
concept_name: string;
51-
concept_type: string;
5280
}
5381

5482
interface CovariateResponse {
5583
covariates: Array<CovariateInformation>;
5684
}
5785

86+
export interface GWASHistogramBin {
87+
start: number;
88+
end: number;
89+
personCount: number;
90+
}
91+
92+
export interface GWASHistogramResponse {
93+
bins: Array<GWASHistogramBin>;
94+
}
95+
96+
export const addCDFilter = (cohortId: number, otherCohortId: number, covariateArray: Array<CovariateInformation>) => {
97+
// adding an extra filter on top of the given covariateArr
98+
// to ensure that any person that belongs to _both_ cohorts
99+
// [cohortId, otherCohortId] also gets filtered out:
100+
const covariateRequest = [...covariateArray];
101+
const cdFilter = {
102+
variable_type: 'custom_dichotomous',
103+
cohort_ids: [cohortId, otherCohortId],
104+
provided_name:
105+
'auto_generated_extra_item_to_filter_out_case_control_overlap',
106+
};
107+
covariateRequest.push(cdFilter);
108+
return covariateRequest;
109+
};
110+
111+
58112
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
59113
endpoints: (builder) => ({
60114
getCohortDefinitions: builder.query<
@@ -101,24 +155,55 @@ export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
101155
}),
102156
}),
103157
getCovariateStats: builder.query<string, CovariateQueryParams>({
104-
query: (params: CovariateQueryParams) => ({
105-
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${params.sourceId}/by-cohort-definition-id/${params.cohortDefinitionId}`,
158+
query: ({ sourceId, cohortDefinitionId, selectedCovariateIds }) => ({
159+
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${sourceId}/by-cohort-definition-id/${cohortDefinitionId}`,
106160
method: 'POST',
107161
body: JSON.stringify({
108-
ConceptIds: params.selectedCovariateIds,
162+
ConceptIds: selectedCovariateIds,
109163
}),
110164
}),
111165
}),
112166
getConceptStatsByHareSubset: builder.query<
113167
string,
114168
ConceptStatsByHareSubset
115169
>({
116-
query: (params: ConceptStatsByHareSubset) => {
170+
query: ({ outcome, cohortDefinitionId, subsetCovariates, sourceId }) => {
171+
const variablesPayload = {
172+
variables: [.outcome, ...subsetCovariates],
173+
};
174+
return {
175+
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${sourceId}/by-cohort-definition-id/${cohortDefinitionId}/breakdown-by-concept-id/${hareConceptId}`,
176+
method: 'POST',
177+
body: JSON.stringify(variablesPayload),
178+
};
179+
},
180+
}),
181+
getHistogramInfo: builder.query<
182+
GWASHistogramResponse,
183+
HistogramQueryParams
184+
>({
185+
query: ({
186+
sourceId,
187+
cohortId,
188+
selectedCovariates,
189+
outcome,
190+
selectedConceptId,
191+
}) => {
117192
const variablesPayload = {
118-
variables: [params.outcome, ...params.subsetCovariates],
193+
variables: [
194+
...selectedCovariates,
195+
outcome,
196+
// add extra filter to make sure we only count persons that have a HARE group as well:
197+
{
198+
variable_type: 'concept',
199+
concept_id: hareConceptId,
200+
},
201+
].filter(Boolean), // filter out any undefined or null items (e.g. in some
202+
// scenarios "outcome" and "selectedCovariates" are still null and/or empty)
119203
};
204+
120205
return {
121-
url: `${GEN3_COHORT_MIDDLEWARE_API}/concept-stats/by-source-id/${params.sourceId}/by-cohort-definition-id/${params.cohortDefinitionId}/breakdown-by-concept-id/${hareConceptId}`,
206+
url: `${GEN3_COHORT_MIDDLEWARE_API}/histogram/by-source-id/${sourceId}/by-cohort-definition-id/${cohortId}/by-histogram-concept-id/${selectedConceptId}`,
122207
method: 'POST',
123208
body: JSON.stringify(variablesPayload),
124209
};
@@ -133,4 +218,5 @@ export const {
133218
useGetSourceIdQuery,
134219
useGetCovariatesQuery,
135220
useGetCovariateStatsQuery,
221+
useGetConceptStatsByHareSubsetQuery,
136222
} = gwasCohortApi;

src/lib/AnalysisApps/GWAS/Utils/cohortUtlis.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)