Skip to content

Commit

Permalink
caching
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Dec 3, 2020
1 parent 021aaa9 commit e7d95e7
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 72 deletions.
26 changes: 21 additions & 5 deletions js/components/datasets/datasetMoleculeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ import {
removeMoleculeFromCompoundsOfDatasetToBuy,
setCrossReferenceCompoundName,
setIsOpenCrossReferenceDialog,
setInspirationFragmentList
setInspirationFragmentList,
setInspirationMoleculeDataList
} from './redux/actions';
import { centerOnLigandByMoleculeID } from '../../reducers/ngl/dispatchActions';
import { ArrowDownward, ArrowUpward, MyLocation } from '@material-ui/icons';
import { isNumber, isString } from 'lodash';
import { isString } from 'lodash';
import { SvgTooltip } from '../common';
import { OBJECT_TYPE } from '../nglView/constants';
import { getRepresentationsByType } from '../nglView/generatingObjects';
Expand Down Expand Up @@ -265,6 +266,7 @@ export const DatasetMoleculeView = memo(
const ref = useRef(null);
const dispatch = useDispatch();
const compoundsToBuyList = useSelector(state => state.datasetsReducers.compoundsToBuyDatasetMap[datasetID]);
const allInspirations = useSelector(state => state.datasetsReducers.allInspirations);

const datasets = useSelector(state => state.datasetsReducers.datasets);
const filteredScoreProperties = useSelector(state => state.datasetsReducers.filteredScoreProperties);
Expand Down Expand Up @@ -525,6 +527,16 @@ export const DatasetMoleculeView = memo(
});
};

const getInspirationsForMol = (datasetId, molId) => {
let inspirations = [];

if (allInspirations && allInspirations.hasOwnProperty(datasetId) && allInspirations[datasetId].hasOwnProperty(molId)) {
inspirations = allInspirations[datasetId][molId];
}

return inspirations;
};

const handleClickOnDownArrow = () => {
const refNext = ref.current.nextSibling;
scrollToElement(refNext);
Expand All @@ -538,7 +550,9 @@ export const DatasetMoleculeView = memo(

moveSelectedMoleculeSettings(nextItem, nextDatasetID);
dispatch(moveSelectedMoleculeInspirationsSettings(data, nextItem));
dispatch(setInspirationFragmentList(nextItem.computed_inspirations));
const inspirations = getInspirationsForMol(datasetID, nextItem.id);
dispatch(setInspirationMoleculeDataList(inspirations));
// dispatch(setInspirationFragmentList(nextItem.computed_inspirations));
dispatch(setCrossReferenceCompoundName(moleculeTitleNext));
if (setRef && ref.current) {
setRef(refNext);
Expand All @@ -560,7 +574,9 @@ export const DatasetMoleculeView = memo(

moveSelectedMoleculeSettings(previousItem, previousDatasetID);
dispatch(moveSelectedMoleculeInspirationsSettings(data, previousItem));
dispatch(setInspirationFragmentList(previousItem.computed_inspirations));
const inspirations = getInspirationsForMol(datasetID, previousItem.id);
dispatch(setInspirationMoleculeDataList(inspirations));
// dispatch(setInspirationFragmentList(previousItem.computed_inspirations));
dispatch(setCrossReferenceCompoundName(moleculeTitlePrev));
if (setRef && ref.current) {
setRef(refPrevious);
Expand Down Expand Up @@ -752,7 +768,7 @@ export const DatasetMoleculeView = memo(
clickOnInspirations({
datasetID,
currentID,
computed_inspirations: data && data.computed_inspirations
computed_inspirations: getInspirationsForMol(datasetID, currentID)
})
);
if (setRef) {
Expand Down
20 changes: 1 addition & 19 deletions js/components/datasets/inspirationDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import {
removeDensity,
removeVector
} from '../preview/molecule/redux/dispatchActions';
import { loadInspirationMoleculesDataList } from './redux/dispatchActions';
import MoleculeView from '../preview/molecule/moleculeView';
import { moleculeProperty } from '../preview/molecule/helperConstants';
import { debounce } from 'lodash';
import { setInspirationMoleculeDataList, setIsOpenInspirationDialog } from './redux/actions';
import { setIsOpenInspirationDialog } from './redux/actions';
import { Button } from '../common/Inputs/Button';
import classNames from 'classnames';
// import { useDisableUserInteraction } from '../helpers/useEnableUserInteracion';
Expand All @@ -37,7 +36,6 @@ import { NglContext } from '../nglView/nglProvider';
import { VIEWS } from '../../constants/constants';
import { Panel } from '../common/Surfaces/Panel';
import { changeButtonClassname } from './helpers';
import { getMoleculeList } from '../preview/molecule/redux/selectors';

const useStyles = makeStyles(theme => ({
paper: {
Expand Down Expand Up @@ -143,14 +141,10 @@ export const InspirationDialog = memo(
const { getNglView } = useContext(NglContext);
const stage = getNglView(VIEWS.MAJOR_VIEW) && getNglView(VIEWS.MAJOR_VIEW).stage;

const inspirationFragmentList = useSelector(state => state.datasetsReducers.inspirationFragmentList);

const isLoadingInspirationListOfMolecules = useSelector(
state => state.datasetsReducers.isLoadingInspirationListOfMolecules
);
const inspirationMoleculeDataList = useSelector(state => state.datasetsReducers.inspirationMoleculeDataList);
const allInspirationMoleculeDataList = useSelector(state => state.datasetsReducers.allInspirationMoleculeDataList);
const getJoinedMoleculeList = useSelector(state => getMoleculeList(state));

const ligandList = useSelector(state => state.selectionReducers.fragmentDisplayList);
const proteinList = useSelector(state => state.selectionReducers.proteinList);
Expand All @@ -162,16 +156,6 @@ export const InspirationDialog = memo(
const dispatch = useDispatch();
// const disableUserInteraction = useDisableUserInteraction();

useEffect(() => {
if (inspirationFragmentList && inspirationFragmentList.length > 0) {
dispatch(loadInspirationMoleculesDataList(inspirationFragmentList)).catch(error => {
throw new Error(error);
});
} else {
dispatch(setInspirationMoleculeDataList([]));
}
}, [dispatch, inspirationFragmentList]);

let debouncedFn;

const handleSearch = event => {
Expand Down Expand Up @@ -224,8 +208,6 @@ export const InspirationDialog = memo(
const selectMoleculeSite = moleculeGroupSite => {};

const removeOfAllSelectedTypes = () => {
let molecules = [...getJoinedMoleculeList, ...allInspirationMoleculeDataList];

proteinList?.forEach(moleculeID => {
let foundedMolecule = moleculeList?.find(mol => mol.id === moleculeID);
foundedMolecule = foundedMolecule && Object.assign({ isInspiration: true }, foundedMolecule);
Expand Down
5 changes: 5 additions & 0 deletions js/components/datasets/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export const setInspirationMoleculeDataList = (moleculeList = []) => ({
payload: moleculeList
});

export const setAllInspirations = (allInspirationsMap) => ({
type: constants.SET_ALL_INSPIRATIONS,
payload: allInspirationsMap
});

export const setAllInspirationMoleculeDataList = (moleculeList = []) => ({
type: constants.SET_ALL_INSPIRATION_MOLECULE_DATA_LIST,
payload: moleculeList
Expand Down
1 change: 1 addition & 0 deletions js/components/datasets/redux/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const constants = {
SET_INSPIRATION_LIST: prefix + 'SET_INSPIRATION_LIST',
APPEND_INSPIRATION_LIST: prefix + 'APPEND_INSPIRATION_LIST',
REMOVE_FROM_INSPIRATION_LIST: prefix + 'REMOVE_FROM_INSPIRATION_LIST',
SET_ALL_INSPIRATIONS: prefix + 'SET_ALL_INSPIRATIONS',

APPEND_TO_SCORE_DATASET_MAP: prefix + 'APPEND_TO_SCORE_DATASET_MAP',
REMOVE_FROM_SCORE_DATASET_MAP: prefix + 'REMOVE_FROM_SCORE_DATASET_MAP',
Expand Down
2 changes: 1 addition & 1 deletion js/components/datasets/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export const clearDatasetSettings = datasetID => dispatch => {

export const clickOnInspirations = ({ datasetID, currentID, computed_inspirations = [] }) => dispatch => {
dispatch(setInspirationList(datasetID, [currentID]));
dispatch(setInspirationFragmentList(computed_inspirations));
dispatch(setInspirationMoleculeDataList(computed_inspirations));
dispatch(setIsOpenInspirationDialog(true));
};

Expand Down
4 changes: 4 additions & 0 deletions js/components/datasets/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const INITIAL_STATE = {
isLoadingInspirationListOfMolecules: false,
inspirationMoleculeDataList: [],
allInspirationMoleculeDataList: [],
allInspirations: {},

// cross reference
isOpenCrossReferenceDialog: false,
Expand Down Expand Up @@ -341,6 +342,9 @@ export const datasetsReducers = (state = INITIAL_STATE, action = {}) => {
}
return Object.assign({}, state, { inspirationFragmentList: [...diminishedInspirationFragmentList] });

case constants.SET_ALL_INSPIRATIONS:
return {...state, allInspirations: action.payload};

case constants.APPEND_MOLECULE_TO_COMPOUNDS_TO_BUY_OF_DATASET:
const setOfMolecules = new Set(state.compoundsToBuyDatasetMap[action.payload.datasetID]);
setOfMolecules.add(action.payload.moleculeID);
Expand Down
54 changes: 45 additions & 9 deletions js/components/nglView/renderingObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultFocus
} from './generatingObjects';
import { concatStructures, Selection, Shape, Matrix4 } from 'ngl';
import {addToPdbCache} from '../../reducers/ngl/actions';

const showSphere = ({ stage, input_dict, object_name, representations }) => {
let colour = input_dict.colour;
Expand Down Expand Up @@ -80,18 +81,53 @@ const renderHitProtein = (ol, representations, orientationMatrix) => {
return assignRepresentationArrayToComp(reprArray, comp);
};

const showHitProtein = ({ stage, input_dict, object_name, representations, orientationMatrix }) => {
const loadPdbFile = (url) => {
return fetch(url).then(response => response.text()).then(str => {
return new Blob([str], { type: 'text/plain' })
});
};

const getNameOfPdb = (url) => {
const parts = url.split('/');
const last = parts[parts.length - 1];
return last;
};

const getPdb = (url) => (dispatch, getState) => {
const state = getState();

const pdbCache = state.nglReducers.pdbCache;
const pdbName = getNameOfPdb(url);
if (pdbCache.hasOwnProperty(pdbName)) {
return new Promise((resolve, reject) => {
resolve(pdbCache[pdbName])
});
} else {
return loadPdbFile(url).then(b => {
dispatch(addToPdbCache(pdbName, b));
return b;
});
};
};

const showHitProtein = ({ stage, input_dict, object_name, representations, orientationMatrix, dispatch }) => {
let stringBlob = new Blob([input_dict.sdf_info], { type: 'text/plain' });
return Promise.all([
stage.loadFile(input_dict.prot_url, { ext: 'pdb', defaultAssembly: 'BU1' }),
stage.loadFile(stringBlob, { ext: 'sdf' }),
stage,
defaultFocus,
object_name,
input_dict.colour
]).then(ol => renderHitProtein(ol, representations, orientationMatrix));

return dispatch(getPdb(input_dict.prot_url)).then(pdbBlob => {
return Promise.all([
stage.loadFile(pdbBlob, { ext: 'pdb', defaultAssembly: 'BU1' }),
stage.loadFile(stringBlob, { ext: 'sdf' }),
stage,
defaultFocus,
object_name,
input_dict.colour
]);
}).then(ol => {
renderHitProtein(ol, representations, orientationMatrix)
});
};


const renderComplex = (ol, representations, orientationMatrix) => {
let cs = concatStructures(
ol[4],
Expand Down
9 changes: 4 additions & 5 deletions js/components/preview/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { loadDatasetCompoundsWithScores, loadDataSets } from '../datasets/redux/
import { SelectedCompoundList } from '../datasets/selectedCompoundsList';
import { DatasetSelectorMenuButton } from '../datasets/datasetSelectorMenuButton';
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
import { setMoleculeListIsLoading, replaceAllMoleculeLists } from '../datasets/redux/actions';
import { setMoleculeListIsLoading, setAllInspirations } from '../datasets/redux/actions';

const hitNavigatorWidth = 504;

Expand Down Expand Up @@ -130,19 +130,18 @@ const Preview = memo(({ isStateLoaded, hideProjects }) => {
const keys = Object.keys(moleculeLists);
keys.forEach(key => {
let dataset = moleculeLists[key];
let mols = [];
let mols = {};
dataset.forEach(dsMol => {
let inspirations = [];
dsMol.computed_inspirations.forEach(id => {
let lhsMol = allMolsMap[id];
inspirations.push(lhsMol);
});
dsMol.inspirations = inspirations;
mols.push(dsMol);
mols[dsMol.id] = inspirations;
});
allDatasets[key] = mols;
});
dispatch(replaceAllMoleculeLists(allDatasets));
dispatch(setAllInspirations(allDatasets));
}
}, [all_mol_lists, moleculeLists, isLoadingMoleculeList, linearizeMoleculesLists, dispatch]);

Expand Down
7 changes: 7 additions & 0 deletions js/reducers/ngl/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Created by abradley on 03/03/2018.
*/
import { constant } from 'lodash';
import { constants } from '../selection/constants';
import { CONSTANTS } from './constants';

export const loadNglObject = (target, representations) => ({ type: CONSTANTS.LOAD_OBJECT, target, representations });
Expand Down Expand Up @@ -85,3 +87,8 @@ export const removeMoleculeOrientation = moleculeGroupID => ({
type: CONSTANTS.REMOVE_MOLECULE_ORIENTATION,
payload: moleculeGroupID
});

export const addToPdbCache = (name, cacheItem) => ({
type: CONSTANTS.ADD_TO_PDB_CACHE,
payload: {name: name, cacheItem: cacheItem}
});
29 changes: 0 additions & 29 deletions js/reducers/ngl/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,35 +297,6 @@ describe("testing ngl reducer's actions", () => {
expect(result.nglOrientations).toStrictEqual(defaultScene[SCENES.defaultScene].nglOrientations);
});

/*
it('should reset current state to session scene', () => {
expect.hasAssertions();
const sessionScene = {
objectsInView: {
molecule_1: {
properties: [{ a: 'dfg' }],
representations: [
{
lastKnownID: 78904,
uuid: 9903,
other: 'fdjsdj'
},
{
lastKnownID: 178904,
uuid: 19903,
other: '1fdjsdj'
}
]
}
},
nglOrientations: { a: [24, 566] }
};
let result = nglReducers(initialState, actions.setNglStateFromCurrentSnapshot(sessionScene));
expect(result.objectsInView).toStrictEqual(sessionScene.objectsInView);
expect(result.nglOrientations).toStrictEqual(sessionScene.nglOrientations);
});
*/
it('should remove all ngl view components', () => {
expect.hasAssertions();
let result = nglReducers(initialState, actions.removeAllNglComponents());
Expand Down
4 changes: 3 additions & 1 deletion js/reducers/ngl/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const CONSTANTS = {

SET_MOLECULE_ORIENTATIONS: prefix + 'SET_MOLECULE_ORIENTATIONS',
APPEND_MOLECULE_ORIENTATION: prefix + 'SET_MOLECULE_ORIENTATION',
REMOVE_MOLECULE_ORIENTATION: prefix + 'REMOVE_MOLECULE_ORIENTATION'
REMOVE_MOLECULE_ORIENTATION: prefix + 'REMOVE_MOLECULE_ORIENTATION',

ADD_TO_PDB_CACHE: prefix + 'ADD_TO_PDB_CACHE'
};

export const SCENES = {
Expand Down
3 changes: 2 additions & 1 deletion js/reducers/ngl/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export const loadObject = ({
object_name: versionFixedTarget.name,
representations: previousRepresentations,
orientationMatrix,
markAsRightSideLigand
markAsRightSideLigand,
dispatch
})
.then(representations => {
dispatch(loadNglObject(versionFixedTarget, representations))
Expand Down
9 changes: 7 additions & 2 deletions js/reducers/ngl/nglReducers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BACKGROUND_COLOR, NGL_PARAMS } from '../../components/nglView/constants';
import { CONSTANTS } from './constants';
import NglView from '../../components/nglView/nglView';
import { VIEWS } from '../../constants/constants';

export const INITIAL_STATE = {
Expand Down Expand Up @@ -39,7 +38,8 @@ export const INITIAL_STATE = {
[VIEWS.MAJOR_VIEW]: 0,
[VIEWS.SUMMARY_VIEW]: 0
},
moleculeOrientations: {}
moleculeOrientations: {},
pdbCache: {}
};

export default function nglReducers(state = INITIAL_STATE, action = {}) {
Expand Down Expand Up @@ -174,6 +174,11 @@ export default function nglReducers(state = INITIAL_STATE, action = {}) {
}
return Object.assign({}, state, { moleculeOrientations: diminishedMoleculeOrientations });

case CONSTANTS.ADD_TO_PDB_CACHE:
return {...state, pdbCache: {
...state.pdbCache, [action.payload.name]: action.payload.cacheItem
}};

default:
return state;
}
Expand Down

0 comments on commit e7d95e7

Please sign in to comment.