Skip to content

Commit

Permalink
#429 Restore current state as a result of the actions applied on the …
Browse files Browse the repository at this point in the history
…default state
  • Loading branch information
Adriána Kohanová committed Nov 5, 2020
1 parent 383f7e7 commit 15effdf
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 40 deletions.
34 changes: 34 additions & 0 deletions js/components/preview/moleculeGroups/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ export const saveMoleculeGroupsToNglView = (molGroupList, stage, projectId) => d
}
};

export const saveMoleculeGroupsToNglViewWithoutProject = (molGroupList, stage) => dispatch => {
if (molGroupList) {
molGroupList.map(data =>
dispatch(loadObject({ target: Object.assign({ display_div: VIEWS.SUMMARY_VIEW }, generateSphere(data)), stage }))
);
}
};

export const selectMoleculeGroup = (moleculeGroup, summaryViewStage) => (dispatch, getState) => {
const state = getState();
const moleculeGroupSelection = state.selectionReducers.mol_group_selection.slice();
Expand Down Expand Up @@ -171,6 +179,32 @@ export const loadMoleculeGroups = ({ summaryView, setOldUrl, oldUrl, onCancel, i
return Promise.resolve();
};

export const loadMoleculeGroupsOfTarget = ({ summaryView, setOldUrl, isStateLoaded, target_on }) => (
dispatch,
getState
) => {
const state = getState();
const group_type = state.apiReducers.group_type;
const list_type = OBJECT_TYPE.MOLECULE_GROUP;

if (target_on && !isStateLoaded) {
return loadFromServer({
url: getUrl({ list_type, target_on, group_type }),
afterPush: data_list => dispatch(saveMoleculeGroupsToNglViewWithoutProject(data_list, summaryView)),
list_type,
setOldUrl,
setObjectList: mol_group_list => {
mol_group_list.sort((a, b) => a.id - b.id);
dispatch(setMolGroupList(mol_group_list));
}
});
} else if (target_on && isStateLoaded) {
// to enable user interaction with application
dispatch(setCountOfRemainingMoleculeGroups(0));
}
return Promise.resolve();
};

export const clearMoleculeGroupSelection = ({ getNglView }) => (dispatch, getState) => {
const state = getState();
const molGroupList = state.apiReducers.mol_group_list;
Expand Down
195 changes: 165 additions & 30 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { actionType, actionObjectType } from './constants';
import { VIEWS } from '../../../js/constants/constants';
import { setCurrentVector } from '../selection/actions';
import { unmountPreviewComponent, shouldLoadProtein } from '../../components/preview/redux/dispatchActions';
import { selectMoleculeGroup } from '../../components/preview/moleculeGroups/redux/dispatchActions';
import { setMolGroupOn, setTargetOn } from '../api/actions';
import {
selectMoleculeGroup,
loadMoleculeGroupsOfTarget
} from '../../components/preview/moleculeGroups/redux/dispatchActions';
import { resetTargetState, setTargetOn } from '../api/actions';
import {
addComplex,
addLigand,
Expand All @@ -13,6 +16,18 @@ import {
addVector
} from '../../components/preview/molecule/redux/dispatchActions';
import { colourList } from '../../components/preview/molecule/moleculeView';
import {
addDatasetComplex,
addDatasetLigand,
addDatasetHitProtein,
addDatasetSurface,
loadDataSets,
loadDatasetCompoundsWithScores
} from '../../components/datasets/redux/dispatchActions';
import { appendMoleculeToCompoundsOfDatasetToBuy } from '../../components/datasets/redux/actions';
import { setAllMolLists } from '../api/actions';
import { getUrl, loadAllMolsFromMolGroup } from '../../../js/utils/genericList';
import * as listType from '../../constants/listTypes';

export const selectCurrentActionsList = () => (dispatch, getState) => {
const state = getState();
Expand Down Expand Up @@ -197,33 +212,104 @@ const getCollectionOfDatasetOfRepresentation = dataList => {
};

export const restoreCurrentActionsList = (stages = []) => (dispatch, getState) => {
dispatch(unmountPreviewComponent(stages));
dispatch(resetTargetState());
dispatch(restoreStateBySavedActionList(stages));
};

const restoreStateBySavedActionList = stages => (dispatch, getState) => {
const state = getState();

const currentActionList = state.trackingReducers.current_actions_list;
const orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp);

let majorStages = stages.filter(view => view.id !== VIEWS.SUMMARY_VIEW);
dispatch(restoreTargetActions(orderedActionList, stages));
};

dispatch(unmountPreviewComponent(majorStages));
dispatch(setMolGroupOn(undefined));
//dispatch(setTargetOn(undefined));
const restoreTargetActions = (orderedActionList, stages, state) => (dispatch, getState) => {
const state = getState();

const summaryView = stages.find(view => view.id === VIEWS.SUMMARY_VIEW);
const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW);

dispatch(restoreStateBySavedActionList(currentActionList, state, summaryView, majorView, majorStages));
};

const restoreStateBySavedActionList = (currentActionList, state, summaryView, majorView, nglViewList) => dispatch => {
let stage = majorView.stage;
let orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp);

let targetAction = orderedActionList.find(action => action.action_type === actionType.TARGET_LOADED);
if (targetAction) {
let target = getTarget(targetAction.object_name, state);
if (target) {
dispatch(setTargetOn(target.id));
dispatch(shouldLoadProtein({ nglViewList, currentSnapshotID: null, isLoadingCurrentSnapshot: false }));
dispatch(shouldLoadProtein({ nglViewList: stages, currentSnapshotID: null, isLoadingCurrentSnapshot: false }));

dispatch(
loadMoleculeGroupsOfTarget({
summaryView: summaryView.stage,
isStateLoaded: false,
setOldUrl: url => {},
target_on: target.id
})
)
.catch(error => {
throw error;
})
.finally(() => {
const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW);
const stage = majorView.stage;

dispatch(restoreSitesActions(orderedActionList, summaryView));
dispatch(loadAllMolecules(orderedActionList, target.id, stage));
});

dispatch(loadAllDatasaets);
}
}
};

const loadAllDatasaets = (orderedActionList, target_on, stage) => (dispatch, getState) => {
//dispatch(setMoleculeListIsLoading(true));
dispatch(loadDataSets(target_on))
.then(results => {
//setSelectedDatasetIndex(0);

return dispatch(loadDatasetCompoundsWithScores());
})
.catch(error => {
throw new Error(error);
})
.finally(() => {
//dispatch(setMoleculeListIsLoading(false));
dispatch(restoreCompoundsActions(orderedActionList, stage));
});
};

const loadAllMolecules = (orderedActionList, target_on, stage) => (dispatch, getState) => {
const state = getState();
const list_type = listType.MOLECULE;

let molGroupList = state.apiReducers.mol_group_list;

let promises = [];
molGroupList.forEach(molGroup => {
let id = molGroup.id;
let url = getUrl({ list_type, target_on, mol_group_on: id });
promises.push(
loadAllMolsFromMolGroup({
url,
mol_group: id
})
);
});
Promise.all(promises)
.then(results => {
let listToSet = {};
results.forEach(molResult => {
listToSet[molResult.mol_group] = molResult.molecules;
});
dispatch(setAllMolLists(listToSet));
dispatch(restoreMoleculesActions(orderedActionList, stage));
})
.catch(err => console.log(err));
};

const restoreSitesActions = (orderedActionList, summaryView) => (dispatch, getState) => {
const state = getState();

let sitesAction = orderedActionList.filter(action => action.action_type === actionType.SITE_TURNED_ON);
if (sitesAction) {
Expand All @@ -234,17 +320,10 @@ const restoreStateBySavedActionList = (currentActionList, state, summaryView, ma
}
});
}

dispatch(restoreMoleculesActions(orderedActionList, stage, state));
dispatch(restoreCompoundsActions(orderedActionList, stage, state));

let vectorAction = orderedActionList.find(action => action.action_type === actionType.VECTOR_SELECTED);
if (vectorAction) {
dispatch(setCurrentVector(vectorAction.object_name));
}
};

const restoreMoleculesActions = (orderedActionList, stage, state) => dispatch => {
const restoreMoleculesActions = (orderedActionList, stage) => (dispatch, getState) => {
const state = getState();
let moleculesAction = orderedActionList.filter(
action => action.object_type === actionObjectType.MOLECULE || action.object_type === actionObjectType.INSPIRATION
);
Expand All @@ -256,16 +335,38 @@ const restoreMoleculesActions = (orderedActionList, stage, state) => dispatch =>
dispatch(addNewType(moleculesAction, actionType.SURFACE_TURNED_ON, 'surface', stage, state));
dispatch(addNewType(moleculesAction, actionType.VECTOR_SELECTED, 'vector', stage, state));
}

let vectorAction = orderedActionList.find(action => action.action_type === actionType.VECTOR_SELECTED);
if (vectorAction) {
dispatch(setCurrentVector(vectorAction.object_name));
}
};

const restoreCompoundsActions = (orderedActionList, stage, state) => dispatch => {
const restoreCompoundsActions = (orderedActionList, stage) => (dispatch, getState) => {
const state = getState();

let compoundsAction = orderedActionList.filter(
action =>
action.object_type === actionObjectType.COMPOUND || action.action_type === actionObjectType.CROSS_REFERENCE
action.object_type === actionObjectType.COMPOUND || action.object_type === actionObjectType.CROSS_REFERENCE
);

if (compoundsAction) {
dispatch(addNewTypeCompound(compoundsAction, actionType.LIGAND_TURNED_ON, 'ligand', stage, state));
dispatch(addNewTypeCompound(compoundsAction, actionType.SIDECHAINS_TURNED_ON, 'protein', stage, state));
dispatch(addNewTypeCompound(compoundsAction, actionType.INTERACTIONS_TURNED_ON, 'complex', stage, state));
dispatch(addNewTypeCompound(compoundsAction, actionType.SURFACE_TURNED_ON, 'surface', stage, state));
}

let compoundsSelectedAction = compoundsAction.filter(
action => action.action_type === actionObjectType.COMPOUND_SELECTED
);

compoundsSelectedAction.forEach(action => {
let data = getCompound(action.object_name, state);
if (data) {
dispatch(appendMoleculeToCompoundsOfDatasetToBuy(data.datasetID, data.id, data.name));
}
});
};

const addType = {
Expand All @@ -276,13 +377,32 @@ const addType = {
vector: addVector
};

const addTypeCompound = {
ligand: addDatasetLigand,
protein: addDatasetHitProtein,
complex: addDatasetComplex,
surface: addDatasetSurface
};

const addNewType = (moleculesAction, actionType, type, stage, state) => dispatch => {
let actions = moleculesAction.filter(action => action.action_type === actionType);
if (actions) {
actions.forEach(action => {
let molecule = getMolecule(action.object_name, state);
if (molecule) {
dispatch(addType[type](stage, molecule, colourList[molecule.id % colourList.length]), true);
let data = getMolecule(action.object_name, state);
if (data) {
dispatch(addType[type](stage, data, colourList[data.id % colourList.length]), true);
}
});
}
};

const addNewTypeCompound = (moleculesAction, actionType, type, stage, state) => dispatch => {
let actions = moleculesAction.filter(action => action.action_type === actionType);
if (actions) {
actions.forEach(action => {
let data = getCompound(action.object_name, state);
if (data) {
dispatch(addTypeCompound[type](stage, data, colourList[data.id % colourList.length]), data.datasetID);
}
});
}
Expand All @@ -307,7 +427,6 @@ const getMolecule = (moleculeName, state) => {
if (moleculeList) {
for (const group in moleculeList) {
let molecules = moleculeList[group];

molecule = molecules.find(m => m.protein_code === moleculeName);
if (molecule && molecule != null) {
break;
Expand All @@ -316,3 +435,19 @@ const getMolecule = (moleculeName, state) => {
}
return molecule;
};

const getCompound = (name, state) => {
let moleculeList = state.datasetsReducers.moleculeLists;
let molecule = null;

if (moleculeList) {
for (const group in moleculeList) {
let molecules = moleculeList[group];
molecule = molecules.find(m => m.name === name);
if (molecule && molecule != null) {
break;
}
}
}
return molecule;
};
Loading

0 comments on commit 15effdf

Please sign in to comment.