Skip to content

Commit

Permalink
add sample data to nextJS API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrbarnes committed Feb 7, 2025
1 parent 61689e9 commit 098a394
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 257 deletions.
32 changes: 30 additions & 2 deletions src/lib/AnalysisApps/GWAS/Utils/cohortApi.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gen3Api, GEN3_API } from '@gen3/core';

const TAGS = 'GWASApp';
export const GEN3_COHORT_MIDDLEWARE_API = `${GEN3_API}/cohort-middleware`;
export const GEN3_COHORT_MIDDLEWARE_API = process.env.NEXT_PUBLIC_GEN3_COHORT_MIDDLEWARE_API || `${GEN3_API}/cohort-middleware`;

const hareConceptId = 2000007027;

Expand All @@ -11,7 +11,7 @@ export const gwasCohortApiTags = gen3Api.enhanceEndpoints({

// Types for API calls

interface CohortOverlap {
export interface CohortOverlap {
cohort_overlap: {
case_control_overlap: number;
};
Expand Down Expand Up @@ -116,6 +116,18 @@ export interface GWASHistogramResponse {
bins: Array<GWASHistogramBin>;
}

/**
* Adds a filter to the given covariate array to exclude individuals
* who are members of both specified cohorts from analysis. This ensures that
* overlapping members in the two cohorts are filtered out based on a
* custom dichotomous variable.
*
* @param {number} cohortId - The identifier for the first cohort.
* @param {number} otherCohortId - The identifier for the second cohort.
* @param {Array<Covariates>} covariateArray - An array of covariate objects to which
* the new filter will be added.
* @returns {Array<Covariates>} A new array of covariate objects with the added filter.
*/
export const addCDFilter = (
cohortId: number,
otherCohortId: number,
Expand All @@ -135,6 +147,22 @@ export const addCDFilter = (
return covariateRequest;
};

/**
* gwasCohortApi is a configuration object used to interact with the GWAS Cohort API.
* It provides endpoints to query and transform data related to cohort definitions, sources,
* covariates, histogram data, and cohort overlaps. Each endpoint is implemented as a query using
* RTK Query `builder.query` and supports fetching and transforming of data for specific use cases.
*
* Endpoints included:
* - getCohortDefinitions: Fetches cohort definitions and statistics by source ID and team project.
* - getSources: Retrieves available data sources from the middleware API.
* - getSourceId: Extracts the single source ID if only one source is available.
* - getCovariates: Fetches covariates based on source ID and predefined concept types.
* - getCovariateStats: Retrieves statistics for selected covariates for a given cohort definition.
* - getConceptStatsByHareSubset: Fetches concept statistics for a subset of covariates broken down by HARE concept.
* - getHistogramInfo: Retrieves histogram data for selected covariates, outcomes, and cohorts.
* - getSimpleOverlapInfo: Checks the overlap between two cohorts for selected covariates and outcomes.
*/
export const gwasCohortApi = gwasCohortApiTags.injectEndpoints({
endpoints: (builder) => ({
getCohortDefinitions: builder.query<
Expand Down
254 changes: 0 additions & 254 deletions src/lib/AnalysisApps/GWAS/Utils/data/histogram.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { CohortOverlap } from '@/lib/AnalysisApps/GWAS/Utils/cohortApi';

interface PathParameters {
sourceId: string;
cohortAId: string;
cohortBId: string;
}
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
// Get the sourceId from the slug in the path
const { query } = req;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { sourceId, cohortAId, cohortBId } = query as unknown as PathParameters; // messy but this is for development

const data: Record<string, Record<string, CohortOverlap>> = {
'1537': {
'446': { cohort_overlap: { case_control_overlap: 236021 } },
'2356': { cohort_overlap: { case_control_overlap: 236021 } },
},
'446': {
'2356': { cohort_overlap: { case_control_overlap: 648242 } },
},
};

if (data[cohortAId] && data[cohortAId][cohortBId]) {
return res.status(200).json(data[cohortAId][cohortBId]);
}
if (data[cohortBId] && data[cohortBId][cohortAId]) {
return res.status(200).json(data[cohortBId][cohortAId]);
}
return res.status(200).json({ cohort_overlap: { case_control_overlap: 648242 }});

};
export default handler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextApiRequest, NextApiResponse } from 'next';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {

// Get the sourceId from the slug in the path
// const { query } = req;
// const { sourceId } = query;
return res.status(200).json({
cohort_definitions_and_stats: [
{
cohort_definition_id: 573,
cohort_name: 'team2 - test new cohort - catch all',
size: 70240,
},
{
cohort_definition_id: 559,
cohort_name: 'test new cohort - catch all',
size: 70240,
},
{
cohort_definition_id: 574,
cohort_name: 'team2 - test new cohort - medium + large',
size: 23800,
},
{
cohort_definition_id: 575,
cohort_name: 'team2 - test new cohort - small',
size: 80,
},
],
});
};
export default handler;
Loading

0 comments on commit 098a394

Please sign in to comment.