Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 17 additions & 15 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import { CreateSetBody, Set, SetSqon, UpdateSetContentBody, UpdateSetTagBody } f
import { getStatistics, getStudiesStatistics } from './endpoints/statistics';
import transcriptomicsRouter from './endpoints/transcriptomics/route';
import { computeUpset } from './endpoints/upset';
import { reformatVenn, venn, VennOutputReformatted } from './endpoints/venn/venn';
import { reformatVenn, venn, VennOutput } from './endpoints/venn/venn';
import { esHost, keycloakURL, userApiURL } from './env';
import { globalErrorHandler, globalErrorLogger } from './errors';
import { flushAllCache, STATISTICS_CACHE_ID, STATISTICS_PUBLIC_CACHE_ID, twineWithCache } from './middleware/cache';
import { injectBodyHttpHeaders } from './middleware/injectBodyHttpHeaders';
import { resolveSetIdMiddleware } from './middleware/resolveSetIdInSqon';
import { replaceIdsWithSetId, resolveSetsInAllSqonsWithMapper } from './sqon/resolveSetInSqon';
import { Sqon } from './sqon/types';

export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerProject): Express => {
const app = express();
Expand Down Expand Up @@ -202,20 +203,23 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP
});

app.post('/venn', keycloak.protect(), async (req, res, next) => {
const lengthOk = (l: Sqon[]) => [2, 3].includes(l.length);
try {
if (![2, 3].includes(req.body?.sqons?.length) || ![2, 3].includes(req.body?.entityCentricSqons?.length)) {
res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs');
return;
const rawSqons = req.body?.sqons;
const rawEntitySqons = req.body?.entitySqons;

if (!lengthOk(rawSqons) || !lengthOk(rawEntitySqons)) {
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).send('Bad Inputs');
}

const data: VennOutputReformatted[] = [];
for (const [rawSqons, rawIndex, noOpCounts] of [
[req.body.sqons, 'participant', true],
[req.body.entityCentricSqons, req.body?.index, false],
const data: VennOutput[][] = [];
for (const [inputSqons, rawIndex, noOpCounts] of [
[rawSqons, 'participant', true],
[rawEntitySqons, req.body?.index, false],
]) {
// Convert sqon(s) with set_id if exists to intelligible sqon for ES query translation.
const { resolvedSqons: sqons, m: mSetItToIds } = await resolveSetsInAllSqonsWithMapper(
rawSqons,
inputSqons,
null,
req.headers.authorization,
);
Expand All @@ -224,14 +228,12 @@ export default (keycloak: Keycloak, getProject: (projectId: string) => ArrangerP

const datum1 = await venn(sqons, index, noOpCounts);
const datum2 = datum1.map(x => ({ ...x, sqon: replaceIdsWithSetId(x.sqon, mSetItToIds) }));
data.push(reformatVenn(datum2));
data.push(datum2);
}

const indexOfParticipantEntity = 0;
const indexOfEntity = 1;
res.send({
data: {
participantCentric: data[0],
entityCentric: data[1],
},
data: reformatVenn(data[indexOfParticipantEntity], data[indexOfEntity]),
});
} catch (e) {
next(e);
Expand Down
47 changes: 42 additions & 5 deletions src/endpoints/venn/venn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ import { getNestedFieldsForIndex } from '../../sqon/getNestedFieldsForIndex';
import { and, not } from '../../sqon/manipulateSqon';
import { Sqon } from '../../sqon/types';

type VennOutput = {
export type VennOutput = {
operation: string;
count: number;
sqon: Sqon;
};

type VennOutputReformattedElement = VennOutput & {
setId?: string;
type VennEntityOutput = {
operation: string;
entityCount: number;
entitySqon: Sqon;
};

type VennParticipantOutput = {
operation: string;
sqon: Sqon;
};

type VennOutputReformattedElement = VennEntityOutput &
VennParticipantOutput & {
setId?: string;
};

export type VennOutputReformatted = {
summary: VennOutputReformattedElement[];
operations: VennOutputReformattedElement[];
Expand Down Expand Up @@ -134,7 +146,24 @@ export const venn = async (sqons: Sqon[], index: string, noOpCounts = false): Pr
}));
};

export const reformatVenn = (data: VennOutput[]): VennOutputReformatted => {
export const reformatWhenSpecifiedEntity = (os: VennOutput[]): VennEntityOutput[] =>
os.map(o => ({
operation: o.operation,
entitySqon: o.sqon,
entityCount: o.count,
}));

const reformatWhenUnspecifiedSqon = (os: VennOutput[]): VennParticipantOutput[] =>
os.map(o => ({
operation: o.operation,
sqon: o.sqon,
}));

const mergeOutputs = (xs: VennEntityOutput[], ys: VennParticipantOutput[]) =>
// Assumes that lists are of same length as well as "operation" value for same index loop
xs.map((x, i) => ({ ...x, ...ys[i] }));

const reformatToTables = (data: VennOutputReformattedElement[]): VennOutputReformatted => {
const tables = data.reduce(
(xs: VennOutputReformatted, x: VennOutputReformattedElement) => {
if (['Q₁', 'Q₂', 'Q₃'].some(y => y === x.operation)) {
Expand All @@ -147,6 +176,14 @@ export const reformatVenn = (data: VennOutput[]): VennOutputReformatted => {

return {
summary: tables.summary,
operations: tables.operations.map((x: VennOutput, i: number) => ({ ...x, setId: `set-${i}` })),
operations: tables.operations.map((x: VennOutputReformattedElement, i: number) => ({
...x,
setId: `set-${i}`,
})),
};
};

export const reformatVenn = (participantOutputs: VennOutput[], entityOutputs: VennOutput[]): VennOutputReformatted =>
reformatToTables(
mergeOutputs(reformatWhenSpecifiedEntity(entityOutputs), reformatWhenUnspecifiedSqon(participantOutputs)),
);
Loading