Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SJIP-1059 add endpoint to check genes in trasncriptomics data #215

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 93 additions & 4 deletions src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
} from './endpoints/sets/setsFeature';
import { Set, UpdateSetContentBody, UpdateSetTagBody } from './endpoints/sets/setsTypes';
import { getStatistics, getStudiesStatistics, Statistics } from './endpoints/statistics';
import { checkSampleIdsAndGene, fetchDiffGeneExp, fetchFacets, fetchSampleGeneExp } from './endpoints/transcriptomics';
import {
checkGenesExist,
checkSampleIdsAndGene,
fetchDiffGeneExp,
fetchFacets,
fetchSampleGeneExp,
} from './endpoints/transcriptomics';
import {
DiffGeneExpVolcano,
Facets as TranscriptomicsFacets,
Expand Down Expand Up @@ -745,8 +751,8 @@ describe('Express app (without Arranger)', () => {
.expect(403));

it('should return 200 if Authorization header contains valid token and no error occurs', async () => {
const sample_ids = ['bs-aa000aaa', 'bs-bbbb11b1'];
(checkSampleIdsAndGene as jest.Mock).mockImplementation(() => sample_ids);
const sampleIds = ['bs-aa000aaa', 'bs-bbbb11b1'];
(checkSampleIdsAndGene as jest.Mock).mockImplementation(() => sampleIds);

const token = getToken();

Expand All @@ -755,7 +761,7 @@ describe('Express app (without Arranger)', () => {
.set('Content-type', 'application/json')
.set({ Authorization: `Bearer ${token}` })
.send(requestBody)
.expect(200, sample_ids);
.expect(200, sampleIds);
expect((checkSampleIdsAndGene as jest.Mock).mock.calls.length).toEqual(1);
expect((checkSampleIdsAndGene as jest.Mock).mock.calls[0][0]).toEqual(requestBody.sample_ids);
expect((checkSampleIdsAndGene as jest.Mock).mock.calls[0][1]).toEqual(requestBody.ensembl_gene_id);
Expand All @@ -780,4 +786,87 @@ describe('Express app (without Arranger)', () => {
expect((checkSampleIdsAndGene as jest.Mock).mock.calls[0][1]).toEqual(requestBody.ensembl_gene_id);
});
});

describe('POST /transcriptomics/checkGenesExist', () => {
beforeEach(() => {
(checkGenesExist as jest.Mock).mockReset();
});

const requestBody = {
genes:
'CYB5R1,TBCA,TOMM5,NRXN2,ENSG00000163462.18,ENSG00000211592,ENSG000002137410,FUT7,AL139424,ENSG00000204882.4',
};

it('should return 403 if no Authorization header', () =>
request(app)
.post('/transcriptomics/checkGenesExist')
.set('Content-type', 'application/json')
.send(requestBody)
.expect(403));

it('should return 200 if Authorization header contains valid token and no error occurs', async () => {
const matchedGenes = [
{
gene_symbol: 'GPR20',
ensembl_gene_id: 'ENSG00000204882.4',
},
{
gene_symbol: 'TRIM46',
ensembl_gene_id: 'ENSG00000163462.18',
},
];
(checkGenesExist as jest.Mock).mockImplementation(() => matchedGenes);

const token = getToken();

await request(app)
.post('/transcriptomics/checkGenesExist')
.set('Content-type', 'application/json')
.set({ Authorization: `Bearer ${token}` })
.send(requestBody)
.expect(200, matchedGenes);
expect((checkGenesExist as jest.Mock).mock.calls.length).toEqual(1);
expect((checkGenesExist as jest.Mock).mock.calls[0][0]).toEqual([
'CYB5R1',
'TBCA',
'TOMM5',
'NRXN2',
'ENSG00000163462.18',
'ENSG00000211592',
'ENSG000002137410',
'FUT7',
'AL139424',
'ENSG00000204882.4',
]);
});

it('should return 500 if Authorization header contains valid token but an error occurs', async () => {
const expectedError = new Error('OOPS');
(checkGenesExist as jest.Mock).mockImplementation(() => {
throw expectedError;
});

const token = getToken();

await request(app)
.post('/transcriptomics/checkGenesExist')
.set('Content-type', 'application/json')
.set({ Authorization: `Bearer ${token}` })
.send(requestBody)
.expect(500, { error: 'Internal Server Error' });
expect((checkGenesExist as jest.Mock).mock.calls.length).toEqual(1);
expect((checkGenesExist as jest.Mock).mock.calls[0][0]).toEqual([
'CYB5R1',
'TBCA',
'TOMM5',
'NRXN2',
'ENSG00000163462.18',
'ENSG00000211592',
'ENSG000002137410',
'FUT7',
'AL139424',
'ENSG00000204882.4',
]);
});
});
});
16 changes: 15 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
} from './endpoints/sets/setsFeature';
import { CreateSetBody, Set, SetSqon, UpdateSetContentBody, UpdateSetTagBody } from './endpoints/sets/setsTypes';
import { getStatistics, getStudiesStatistics } from './endpoints/statistics';
import { checkSampleIdsAndGene, fetchDiffGeneExp, fetchFacets, fetchSampleGeneExp } from './endpoints/transcriptomics';
import {
checkGenesExist,
checkSampleIdsAndGene,
fetchDiffGeneExp,
fetchFacets,
fetchSampleGeneExp,
} from './endpoints/transcriptomics';
import { cacheTTL, esHost, keycloakURL, userApiURL } from './env';
import { globalErrorHandler, globalErrorLogger } from './errors';
import {
Expand Down Expand Up @@ -187,6 +193,14 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP
res.json(data);
});

app.postAsync('/transcriptomics/checkGenesExist', keycloak.protect(), async (req, res) => {
const genes: string = req.body.genes;

const data = await checkGenesExist(genes.split(','));

res.json(data);
});

app.postAsync('/authorized-studies', keycloak.protect(), computeAuthorizedStudiesForAllFences);

app.use(globalErrorLogger, globalErrorHandler);
Expand Down
78 changes: 76 additions & 2 deletions src/endpoints/transcriptomics/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EsInstance from '../../ElasticSearchClientInstance';
import { checkSampleIdsAndGene, fetchDiffGeneExp, fetchFacets, fetchSampleGeneExp } from '.';
import { DiffGeneExpVolcano, Facets, SampleGeneExpVolcano } from './types';
import { checkGenesExist, checkSampleIdsAndGene, fetchDiffGeneExp, fetchFacets, fetchSampleGeneExp } from '.';
import { DiffGeneExpVolcano, Facets, MatchedGene, SampleGeneExpVolcano } from './types';

jest.mock('../../ElasticSearchClientInstance');

Expand Down Expand Up @@ -493,4 +493,78 @@ describe('Transcriptomics', () => {
expect(result).toEqual(expectedResponse);
});
});

describe('checkGenesExist', () => {
beforeEach(() => {
(EsInstance.getInstance as jest.Mock).mockReset();
});

it('should return gene_symbol and ensembl_gene_id for the list of gene_symbol and or ensembl_gene_id received in param', async () => {
const mockEsResponseBody = {
took: 10,
timed_out: false,
_shards: {
total: 5,
successful: 5,
skipped: 0,
failed: 0,
},
hits: [],
aggregations: {
distinct_genes: {
after_key: {
gene_symbol: 'TRIM46',
ensembl_gene_id: 'ENSG00000163462.18',
},
buckets: [
{
key: {
gene_symbol: 'GPR20',
ensembl_gene_id: 'ENSG00000204882.4',
},
doc_count: 400,
},
{
key: {
gene_symbol: 'TRIM46',
ensembl_gene_id: 'ENSG00000163462.18',
},
doc_count: 400,
},
],
},
},
};

const expectedResponse: MatchedGene[] = [
{
gene_symbol: 'GPR20',
ensembl_gene_id: 'ENSG00000204882.4',
},
{
gene_symbol: 'TRIM46',
ensembl_gene_id: 'ENSG00000163462.18',
},
];

(EsInstance.getInstance as jest.Mock).mockImplementation(() => ({
search: async () => ({ body: mockEsResponseBody }),
}));

const result = await checkGenesExist([
'CYB5R1',
'TBCA',
'TOMM5',
'NRXN2',
'ENSG00000163462.18',
'ENSG00000211592',
'ENSG000002137410',
'FUT7',
'AL139424',
'ENSG00000204882.4',
]);

expect(result).toEqual(expectedResponse);
});
});
});
49 changes: 47 additions & 2 deletions src/endpoints/transcriptomics/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { max, min } from 'lodash';

import EsInstance from '../../ElasticSearchClientInstance';
import {
ES_CHROMOSOME_AGG_SIZE,
Expand All @@ -13,7 +11,9 @@ import {
DiffGeneExpVolcano,
Facets,
FetchDiffGeneExpResponse,
FetchDistinctGenesBySymbolOrEnsemblId,
FetchSampleGeneExpBySampleIdResponse,
MatchedGene,
SampleGeneExpPoint,
SampleGeneExpVolcano,
} from './types';
Expand Down Expand Up @@ -207,3 +207,48 @@ export const checkSampleIdsAndGene = async (sample_ids: string[], ensembl_gene_i

return sampleGeneExpBySample.by_sample.buckets.map(b => b.key);
};

export const checkGenesExist = async (genes: string[]): Promise<MatchedGene[]> => {
const client = EsInstance.getInstance();

const { body } = await client.search({
index: esSampleGeneExpIndex,
body: {
query: {
bool: {
should: [
{
terms: {
symbol: genes,
},
},
{
terms: {
ensembl_gene_id: genes,
},
},
],
},
},
size: 0,
aggs: {
distinct_genes: {
composite: {
sources: [
{ gene_symbol: { terms: { field: 'gene_symbol' } } },
{ ensembl_gene_id: { terms: { field: 'ensembl_gene_id' } } },
],
size: genes.length,
},
},
},
},
});

const distinctGenesBySymbolOrEnsemblId: FetchDistinctGenesBySymbolOrEnsemblId = body?.aggregations;

return distinctGenesBySymbolOrEnsemblId.distinct_genes.buckets.map(bucket => ({
gene_symbol: bucket.key.gene_symbol,
ensembl_gene_id: bucket.key.ensembl_gene_id,
}));
};
16 changes: 16 additions & 0 deletions src/endpoints/transcriptomics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ export type FetchSampleGeneExpBySampleIdResponse = {
}[];
};
};

export type MatchedGene = {
gene_symbol: string;
ensembl_gene_id: string;
};

export type FetchDistinctGenesBySymbolOrEnsemblId = {
distinct_genes: {
buckets: {
key: {
gene_symbol: string;
ensembl_gene_id: string;
};
}[];
};
};
Loading