Skip to content

Commit

Permalink
Update Gnomad model handling to use factory functions
Browse files Browse the repository at this point in the history
  • Loading branch information
frewmack committed May 21, 2024
1 parent 51a4526 commit 83441ca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
45 changes: 25 additions & 20 deletions server/src/models/GnomadAnnotationModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose, { Model, model } from 'mongoose';
import logger from '../logger';
import {
AssemblyId,
CMHVariantIndelCoordinate,
GnomadBaseAnnotation,
GnomadGenomeAnnotation,
Expand Down Expand Up @@ -155,7 +156,7 @@ GnomadGRCh37AnnotationSchema.statics.getAnnotations = async function (ids: Annot
'gene',
'transcript',
]);
const genomeAnnotations = await getAnnotations(GnomadGRCh37GenomeAnnotationModel, ids, [
const genomeAnnotations = await getAnnotations(getGnomadGRCh37GenomeAnnotationModel(), ids, [
'cdna',
'gene',
'source',
Expand Down Expand Up @@ -194,26 +195,30 @@ GnomadGRCh38AnnotationSchema.statics.getAnnotations = async function (ids: Annot
};
};

export const GnomadGRCh37AnnotationModel = model<
GnomadGRCh37ExomeAnnotation,
GnomadGRCh37ExomeAnnotationModel
>('GnomadGRCh37ExomeAnnotation', GnomadGRCh37AnnotationSchema, 'GRCh37ExomeAnnotations');

const GnomadGRCh37GenomeAnnotationModel = model<
const getGnomadGRCh37GenomeAnnotationModel = () => model<
GnomadGenomeAnnotation,
GnomadGenomeAnnotationModel
>('GnomadGRCh37GenomeAnnotation', GnomadGRCh37GenomeAnnotationSchema, 'GRCh37GenomeAnnotations');

export const GnomadGRCh38AnnotationModels = [
...Array.from({ length: 22 }, (_, i) => `${i + 1}`),
'X',
'Y',
].reduce((modelMapping, chr) => {
modelMapping[chr] = model<GnomadGenomeAnnotation, GnomadGenomeAnnotationModel>(
`GnomadGRCh38GenomeAnnotation_chr${chr}`,
GnomadGRCh38AnnotationSchema,
`GRCh38GenomeAnnotations_chr${chr}`
);

return modelMapping;
}, {} as { [key: string]: GnomadGenomeAnnotationModel });
export const getGnomadAnnotationModel = (assembly: AssemblyId, chromosome: string) => {
chromosome = chromosome.replace('chr', '');
if (assembly.includes('38')) {
// GRCh38
if ([...Array.from({ length: 22 }, (_, i) => `${i + 1}`), 'X', 'Y'].includes(chromosome)) {
return model<GnomadGenomeAnnotation, GnomadGenomeAnnotationModel>(
`GnomadGRCh38GenomeAnnotation_chr${chromosome}`,
GnomadGRCh38AnnotationSchema,
`GRCh38GenomeAnnotations_chr${chromosome}`
);
} else {
throw Error(`Chromosome '${chromosome}' invalid; cannot fetch Gnomad annotation model`);
}
} else {
// GRCh37
return model<GnomadGRCh37ExomeAnnotation, GnomadGRCh37ExomeAnnotationModel>(
'GnomadGRCh37ExomeAnnotation',
GnomadGRCh37AnnotationSchema,
'GRCh37ExomeAnnotations'
);
}
};
8 changes: 6 additions & 2 deletions server/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { GnomadGRCh37AnnotationModel, GnomadGRCh38AnnotationModels } from './GnomadAnnotationModel';
import {
getGnomadAnnotationModel,
} from './GnomadAnnotationModel';

export { GnomadGRCh37AnnotationModel, GnomadGRCh38AnnotationModels };
export {
getGnomadAnnotationModel,
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GnomadGRCh37AnnotationModel, GnomadGRCh38AnnotationModels } from '../../../models';
import { getGnomadAnnotationModel } from '../../../models';
import getCoordinates from '../../../models/utils/getCoordinates';
import resolveAssembly from './resolveAssembly';
import resolveChromosome from './resolveChromosome';
import { GnomadAnnotationQueryResponse, VariantQueryDataResult } from '../../../types';
import { QueryResponseError } from './queryResponseError';
import { timeitAsync } from '../../../utils/timeit';
import logger from '../../../logger';

const fetchGnomadAnnotations = timeitAsync('annotateGnomad')(
async (
Expand All @@ -18,11 +19,8 @@ const fetchGnomadAnnotations = timeitAsync('annotateGnomad')(
const resolvedAssemblyId = resolveAssembly(assemblyId);
const { chromosome } = resolveChromosome(position);
const annotationCoordinates = getCoordinates(queryResponse);
const GnomadAnnotationModel =
resolvedAssemblyId === 'GRCh37'
? GnomadGRCh37AnnotationModel
: GnomadGRCh38AnnotationModels[chromosome];

const GnomadAnnotationModel = getGnomadAnnotationModel(resolvedAssemblyId, chromosome)
logger.debug(`model: '${typeof GnomadAnnotationModel}'`);
const annotations = await GnomadAnnotationModel.getAnnotations(annotationCoordinates);

return { source, data: annotations };
Expand Down

0 comments on commit 83441ca

Please sign in to comment.