Skip to content

Commit

Permalink
[Platform]: Migrate Chembl, IMPC and ClinVar Somatic to server side p…
Browse files Browse the repository at this point in the history
…agination (#306)
  • Loading branch information
carcruz authored Nov 29, 2023
1 parent 085b63a commit 1406e10
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 161 deletions.
186 changes: 134 additions & 52 deletions packages/sections/src/evidence/Chembl/Body.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { useQuery } from "@apollo/client";
import { useState, useEffect } from "react";
import { makeStyles } from "@mui/styles";
import {
Link,
SectionItem,
Tooltip,
ChipList,
DataTable,
TableDrawer,
Table,
useCursorBatchDownloader,
getComparator,
getPage,
} from "ui";
import {
defaultRowsPerPageOptions,
phaseMap,
sourceMap,
naLabel,
} from "../../constants";
import { defaultRowsPerPageOptions, phaseMap, sourceMap, naLabel } from "../../constants";
import { dataTypesMap } from "../../dataTypes";
import Description from "./Description";
import { definition } from ".";
import client from "../../client";

import CHEMBL_QUERY from "./ChemblQuery.gql";

Expand All @@ -38,9 +37,7 @@ function getColumns(classes) {
{
id: "disease.name",
label: "Disease/phenotype",
renderCell: ({ disease }) => (
<Link to={`/disease/${disease.id}`}>{disease.name}</Link>
),
renderCell: ({ disease }) => <Link to={`/disease/${disease.id}`}>{disease.name}</Link>,
},
{
label: "Targets",
Expand Down Expand Up @@ -72,10 +69,7 @@ function getColumns(classes) {
title={
<>
Reported target:{" "}
<Link
external
to={`https://identifiers.org/uniprot/${targetFromSourceId}`}
>
<Link external to={`https://identifiers.org/uniprot/${targetFromSourceId}`}>
{targetFromSourceId}
</Link>
</>
Expand All @@ -85,9 +79,7 @@ function getColumns(classes) {
<Link to={`/target/${target.id}`}>{symbol}</Link>
</Tooltip>
{otherTargets.size > 0
? ` and ${otherTargets.size} other target${
otherTargets.size > 1 ? "s" : ""
}`
? ` and ${otherTargets.size} other target${otherTargets.size > 1 ? "s" : ""}`
: null}
</>
);
Expand All @@ -96,9 +88,7 @@ function getColumns(classes) {
{
id: "drug.name",
label: "Drug",
renderCell: ({ drug }) => (
<Link to={`/drug/${drug.id}`}>{drug.name}</Link>
),
renderCell: ({ drug }) => <Link to={`/drug/${drug.id}`}>{drug.name}</Link>,
},
{
id: "drug.drugType",
Expand Down Expand Up @@ -133,9 +123,7 @@ function getColumns(classes) {
return acc;
}, new Set());

return `${anchorMa || naLabel}${
mas.size > 0 ? ` and ${mas.size} other MoA` : ""
}`;
return `${anchorMa || naLabel}${mas.size > 0 ? ` and ${mas.size} other MoA` : ""}`;
},
},
{
Expand All @@ -148,11 +136,7 @@ function getColumns(classes) {
{
id: "clinicalStatus",
label: "Status",
renderCell: ({
studyStopReason,
clinicalStatus,
studyStopReasonCategories,
}) => {
renderCell: ({ studyStopReason, clinicalStatus, studyStopReasonCategories }) => {
if (clinicalStatus && studyStopReason)
return (
<Tooltip
Expand All @@ -165,7 +149,7 @@ function getColumns(classes) {
<div className={classes.chipContainer}>
{studyStopReasonCategories ? (
<ChipList
items={studyStopReasonCategories.map((reason) => ({
items={studyStopReasonCategories.map(reason => ({
label: reason,
customClass: classes.chipStyle,
}))}
Expand Down Expand Up @@ -209,43 +193,141 @@ function getColumns(classes) {
];
}

function fetchData({ ensemblId, efoId, cursor, size }) {
return client.query({
query: CHEMBL_QUERY,
variables: {
ensemblId,
efoId,
cursor,
size,
},
});
}

function Body({ id, label, entity }) {
const { ensgId, efoId } = id;
const { ensgId: ensemblId, efoId } = id;
const [initialLoading, setInitialLoading] = useState(true);
const [loading, setLoading] = useState(false);
const [count, setCount] = useState(0);
const [cursor, setCursor] = useState("");
const [rows, setRows] = useState([]);
const [page, setPage] = useState(0);
const [size, setPageSize] = useState(10);
const [sortColumn, setSortColumn] = useState(null);
const [sortOrder, setSortOrder] = useState("asc");

const variables = {
ensemblId: ensgId,
efoId,
const classes = useStyles();
const columns = getColumns(classes);

useEffect(() => {
let isCurrent = true;

fetchData({ ensemblId, efoId, cursor: "", size }).then(res => {
const { cursor: newCursor, rows: newRows, count: newCount } = res.data.disease.evidences;
if (isCurrent) {
setInitialLoading(false);
setCursor(newCursor);
setCount(newCount);
setRows(newRows);
}
});

return () => {
isCurrent = false;
};
}, []);

const getWholeDataset = useCursorBatchDownloader(
CHEMBL_QUERY,
{
ensemblId,
efoId,
},
`data.disease.evidence`
);

const handlePageChange = newPage => {
const newPageInt = Number(newPage);
if (size * newPageInt + size > rows.length && cursor !== null) {
setLoading(true);
fetchData({ ensemblId, efoId, cursor, size }).then(res => {
const { cursor: newCursor, rows: newRows } = res.data.disease.evidences;
setRows([...rows, ...newRows]);
setLoading(false);
setCursor(newCursor);
setPage(newPageInt);
});
} else {
setPage(newPageInt);
}
};

const request = useQuery(CHEMBL_QUERY, {
variables,
});
const handleRowsPerPageChange = newPageSize => {
const newPageSizeInt = Number(newPageSize);
if (newPageSizeInt > rows.length && cursor !== null) {
setLoading(true);
fetchData({ ensemblId, efoId, cursor, size: newPageSizeInt }).then(res => {
const { cursor: newCursor, rows: newRows } = res.data.disease.evidences;
setRows([...rows, ...newRows]);
setLoading(false);
setCursor(newCursor);
setPage(0);
setPageSize(newPageSizeInt);
});
} else {
setPage(0);
setPageSize(newPageSizeInt);
}
};

const classes = useStyles();
const columns = getColumns(classes);
const handleSortBy = sortBy => {
setSortColumn(sortBy);
setSortOrder(
// eslint-disable-next-line
sortColumn === sortBy ? (sortOrder === "asc" ? "desc" : "asc") : "asc"
);
};

const processedRows = [...rows];

if (sortColumn) {
processedRows.sort(getComparator(columns, sortOrder, sortColumn));
}

return (
<SectionItem
definition={definition}
chipText={dataTypesMap.known_drug}
entity={entity}
request={request}
renderDescription={() => (
<Description symbol={label.symbol} name={label.name} />
)}
renderBody={({ disease }) => {
const { rows } = disease.chemblSummary;
request={{
loading: initialLoading,
data: { [entity]: { chembl: { rows, count: rows.length } } },
}}
renderDescription={() => <Description symbol={label.symbol} name={label.name} />}
renderBody={() => {
return (
<DataTable
<Table
loading={loading}
columns={columns}
rows={rows}
rows={getPage(processedRows, page, size)}
rowCount={count}
rowsPerPageOptions={defaultRowsPerPageOptions}
sortBy="clinicalPhase"
order="desc"
dataDownloader
showGlobalFilter
page={page}
pageSize={size}
onPageChange={handlePageChange}
onRowsPerPageChange={handleRowsPerPageChange}
onSortBy={handleSortBy}
query={CHEMBL_QUERY.loc.source.body}
variables={variables}
dataDownloader
dataDownloaderRows={getWholeDataset}
dataDownloaderFileStem="chembl-evidence"
variables={{
ensemblId,
efoId,
cursor,
size,
}}
/>
);
}}
Expand Down
7 changes: 5 additions & 2 deletions packages/sections/src/evidence/Chembl/ChemblQuery.gql
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
query ChemblQuery($ensemblId: String!, $efoId: String!) {
query ChemblQuery($ensemblId: String!, $efoId: String!, $size: Int!, $cursor: String) {
disease(efoId: $efoId) {
id
chemblSummary: evidences(
evidences(
ensemblIds: [$ensemblId]
enableIndirect: true
datasourceIds: ["chembl"]
size: $size
cursor: $cursor
) {
cursor
count
rows {
disease {
Expand Down
2 changes: 1 addition & 1 deletion packages/sections/src/evidence/Chembl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const definition = {
id,
name: "ChEMBL",
shortName: "CE",
hasData: (data) => data.chemblSummary.count > 0,
hasData: data => data.chembl.count > 0,
isPrivate: isPrivateEvidenceSection(id),
};

Expand Down
Loading

0 comments on commit 1406e10

Please sign in to comment.