Skip to content

Commit

Permalink
- updated data download (not final)
Browse files Browse the repository at this point in the history
  • Loading branch information
boriskovar-m2ms committed Dec 15, 2021
1 parent 03804b8 commit cbaa408
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 48 deletions.
22 changes: 21 additions & 1 deletion js/components/preview/tags/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ import {
setVectorOnList,
updateTag
} from '../../../../reducers/selection/actions';
import { setMolGroupOn, updateMoleculeTag, setAllMolLists, setMoleculeTags } from '../../../../reducers/api/actions';
import {
setMolGroupOn,
updateMoleculeTag,
setAllMolLists,
setMoleculeTags,
setDownloadTags
} from '../../../../reducers/api/actions';
import { setSortDialogOpen } from '../../molecule/redux/actions';
import { resetCurrentCompoundsSettings } from '../../compounds/redux/actions';
import { getRandomColor } from '../../molecule/utils/color';
Expand All @@ -39,6 +45,7 @@ import {
getCategoryIds
} from '../utils/tagUtils';
import { DJANGO_CONTEXT } from '../../../../utils/djangoContext';
import { diffBetweenDatesInDays } from '../../../../utils/common';

export const setTagSelectorData = (categories, tags) => dispatch => {
dispatch(setCategoryList(categories));
Expand Down Expand Up @@ -201,6 +208,7 @@ export const unselectTag = tag => (dispatch, getState) => {
export const loadMoleculesAndTags = targetId => async (dispatch, getState) => {
return getAllData(targetId).then(data => {
let tags_info = [];
let downloadTags = [];
data.tags_info.forEach(tag => {
let newObject = {};
Object.keys(tag.data[0]).forEach(prop => {
Expand All @@ -216,6 +224,17 @@ export const loadMoleculesAndTags = targetId => async (dispatch, getState) => {

if (!newObject.additional_info) {
tags_info.push(newObject);
} else if (newObject.additional_info.requestObject && newObject.additional_info.downloadName) {
if (DJANGO_CONTEXT.pk) {
if (newObject.user_id === DJANGO_CONTEXT.pk) {
downloadTags.push(newObject);
}
} else {
const diffInDays = diffBetweenDatesInDays(new Date(newObject.create_date), new Date());
if (diffInDays <= 5) {
downloadTags.push(newObject);
}
}
}
});

Expand All @@ -239,6 +258,7 @@ export const loadMoleculesAndTags = targetId => async (dispatch, getState) => {
return 0;
});
dispatch(setAllMolLists([...allMolecules]));
dispatch(setDownloadTags(downloadTags));

// const categories = data.tag_categories;
//need to do this this way because only categories which have at least one tag assigned are sent from backend
Expand Down
198 changes: 156 additions & 42 deletions js/components/snapshot/modals/downloadStructuresDialog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useState } from 'react';
import React, { memo, useState, useContext } from 'react';
import Modal from '../../common/Modal';
import { useDispatch, useSelector } from 'react-redux';
import {
Expand All @@ -14,11 +14,14 @@ import {
Radio,
Button,
Box,
Typography
Typography,
Select,
MenuItem
} from '@material-ui/core';
import { selectJoinedMoleculeList } from '../../preview/molecule/redux/selectors';
import { getDownloadStructuresUrl, downloadStructuresZip, getDownloadFileSize } from '../api/api';
import { setDownloadStructuresDialogOpen } from '../redux/actions';
import { setDownloadStructuresDialogOpen, setDontShowShareSnapshot, setSharedSnapshot } from '../redux/actions';
import { saveAndShareSnapshot } from '../redux/dispatchActions';
import { getFileSizeString } from '../../../utils/api';
import { v4 as uuidv4 } from 'uuid';
import { createMoleculeTagObject, DEFAULT_TAG_COLOR } from '../../preview/tags/utils/tagUtils';
Expand All @@ -27,12 +30,32 @@ import { DJANGO_CONTEXT } from '../../../utils/djangoContext';
import { createNewDownloadTag } from '../../preview/tags/api/tagsApi';
import { base_url } from '../../routes/constants';
import { updateClipboard } from '../helpers';
import { NglContext } from '../../nglView/nglProvider';
import { initSharedSnapshot } from '../redux/reducer';
import moment from 'moment';
import { appendToDownloadTags } from '../../../reducers/api/actions';

const useStyles = makeStyles(theme => ({}));
const useStyles = makeStyles(theme => ({
select: {
color: 'inherit',
fill: 'inherit',
'&:hover:not(.Mui-disabled):before': {
borderColor: 'inherit'
},
'&:before': {
borderColor: 'inherit'
},
'&:not(.Mui-disabled)': {
fill: theme.palette.white
}
}
}));

export const DownloadStructureDialog = memo(({}) => {
const newDownload = '--- NEW DOWNLOAD ---';
const dispatch = useDispatch();
const classes = useStyles();
const { nglViewList } = useContext(NglContext);

const isOpen = useSelector(state => state.snapshotReducers.downloadStructuresDialogOpen);
const targetId = useSelector(state => state.apiReducers.target_on);
Expand All @@ -41,6 +64,7 @@ export const DownloadStructureDialog = memo(({}) => {
const ligandsTurnedOnIds = useSelector(state => state.selectionReducers.proteinList);
const selectedMoleculesIds = useSelector(state => state.selectionReducers.moleculesToEdit);
const taggedMolecules = useSelector(state => selectJoinedMoleculeList(state));
const downloadTags = useSelector(state => state.apiReducers.downloadTags);

const [structuresSelection, setStructuresSelection] = useState('allStructures');
const [bound, setBound] = useState(true);
Expand All @@ -60,6 +84,7 @@ export const DownloadStructureDialog = memo(({}) => {
const [fileSize, setFileSize] = useState(null);
const [zipPreparing, setZipPreparing] = useState(false);
const [downloadTagUrl, setDownloadTagUrl] = useState(null);
const [selectedDownload, setSelectedDownload] = useState(newDownload);

const getRequestObject = (structuresToDownload, allStructures = false) => {
let proteinNames = '';
Expand Down Expand Up @@ -92,52 +117,86 @@ export const DownloadStructureDialog = memo(({}) => {
return requestObject;
};

const prepareDownloadClicked = () => {
setZipPreparing(true);
let structuresToDownload = [];
let isAllStructures = false;
if (structuresSelection === 'allStructures') {
structuresToDownload = allMolecules;
isAllStructures = true;
} else if (structuresSelection === 'displayedStructures') {
structuresToDownload = allMolecules.filter(m => ligandsTurnedOnIds.some(id => id === m.id));
} else if (structuresSelection === 'selectedStructures') {
structuresToDownload = allMolecules.filter(m => selectedMoleculesIds.some(id => id === m.id));
} else if (structuresSelection === 'tagged') {
structuresToDownload = taggedMolecules;
}
const prepareDownloadClicked = () => (dispatch, getState) => {
if (selectedDownload !== newDownload) {
const donwloadTag = findDownload(selectedDownload);
if (donwloadTag) {
setZipPreparing(true);
getDownloadStructuresUrl(donwloadTag.additional_info.requestObject)
.then(resp => {
setDownloadUrl(resp.data.file_url);
return getDownloadFileSize(resp.data.file_url);
})
.then(resp => {
const fileSizeInBytes = resp.headers['content-length'];
setFileSize(getFileSizeString(fileSizeInBytes));
setDownloadTagUrl(generateUrlFromTagName(donwloadTag.tag));
setZipPreparing(false);
});
}
} else {
setZipPreparing(true);
let structuresToDownload = [];
let isAllStructures = false;
if (structuresSelection === 'allStructures') {
structuresToDownload = allMolecules;
isAllStructures = true;
} else if (structuresSelection === 'displayedStructures') {
structuresToDownload = allMolecules.filter(m => ligandsTurnedOnIds.some(id => id === m.id));
} else if (structuresSelection === 'selectedStructures') {
structuresToDownload = allMolecules.filter(m => selectedMoleculesIds.some(id => id === m.id));
} else if (structuresSelection === 'tagged') {
structuresToDownload = taggedMolecules;
}

const requestObject = getRequestObject(structuresToDownload, isAllStructures);
getDownloadStructuresUrl(requestObject)
.then(resp => {
setDownloadUrl(resp.data.file_url);
return getDownloadFileSize(resp.data.file_url);
})
.then(resp => {
const fileSizeInBytes = resp.headers['content-length'];
setFileSize(getFileSizeString(fileSizeInBytes));
})
.then(resp => {
return createDownloadTag(requestObject);
})
.then(molTag => {
setDownloadTagUrl(generateUrl(molTag));
setZipPreparing(false);
})
.catch(e => {
console.log(e);
});
const requestObject = getRequestObject(structuresToDownload, isAllStructures);
const tagData = { requestObject: requestObject, structuresSelection: structuresSelection };
dispatch(setDontShowShareSnapshot(true));
dispatch(saveAndShareSnapshot(nglViewList, false))
.then(() => {
const state = getState();
const sharedSnapshot = state.snapshotReducers.sharedSnapshot;
tagData['snapshot'] = sharedSnapshot;
tagData['downloadName'] = moment().format('-- YYYY-MM-DD -- HH:mm:ss');
dispatch(setSharedSnapshot(initSharedSnapshot));
dispatch(setDontShowShareSnapshot(false));
return getDownloadStructuresUrl(requestObject);
})
.then(resp => {
setDownloadUrl(resp.data.file_url);
return getDownloadFileSize(resp.data.file_url);
})
.then(resp => {
const fileSizeInBytes = resp.headers['content-length'];
setFileSize(getFileSizeString(fileSizeInBytes));
})
.then(resp => {
return createDownloadTag(tagData);
})
.then(molTag => {
dispatch(appendToDownloadTags(molTag));
setDownloadTagUrl(generateUrl(molTag));
setZipPreparing(false);
})
.catch(e => {
console.log(e);
});
}
};

const generateTagName = () => {
return uuidv4();
};

const generateUrl = tag => {
return `${base_url}/viewer/react/download/tag/${tag.tag}`;
return generateUrlFromTagName(tag.tag);
};

const createDownloadTag = requestObject => {
const generateUrlFromTagName = tagName => {
return `${base_url}/viewer/react/download/tag/${tagName}`;
};

const createDownloadTag = tagData => {
const tagName = generateTagName();
const tagObject = createMoleculeTagObject(
tagName,
Expand All @@ -148,7 +207,7 @@ export const DownloadStructureDialog = memo(({}) => {
'something/something/something',
[],
new Date(),
{ requestObject: requestObject }
tagData
);

return createNewDownloadTag(tagObject);
Expand All @@ -166,6 +225,40 @@ export const DownloadStructureDialog = memo(({}) => {
dispatch(setDownloadStructuresDialogOpen(false));
};

const findDownload = downloadName => {
const selectedTag = downloadTags.find(t => t.additional_info.downloadName === downloadName);
return selectedTag;
};

const onUpdateExistingDownload = event => {
setSelectedDownload(event.target.value);
if (event.target.value !== newDownload) {
const selectedTag = findDownload(event.target.value);
if (selectedTag) {
setStructuresSelection(selectedTag.additional_info.structuresSelection || 'allStructures');
setBound(selectedTag.additional_info.requestObject.bound_info);
setCif(selectedTag.additional_info.requestObject.cif_info);
setDiff(selectedTag.additional_info.requestObject.diff_info);
setEvent(selectedTag.additional_info.requestObject.event_info);
setSigmaa(selectedTag.additional_info.requestObject.sigmaa_info);
setSdf(selectedTag.additional_info.requestObject.sdf_info);
setTransformMatrix(selectedTag.additional_info.requestObject.trans_matrix_info);
setMetadata(selectedTag.additional_info.requestObject.metadata_info);
setSmiles(selectedTag.additional_info.requestObject.smiles_info);
setPdb(selectedTag.additional_info.requestObject.pdb_info);
setMtz(selectedTag.additional_info.requestObject.mtz_info);
setSingleSdf(selectedTag.additional_info.requestObject.single_sdf_file);
}
}
};

const showSnapshotClicked = () => {
const download = findDownload(selectedDownload);
if (download && download.additional_info && download.additional_info.snapshot) {
window.open(download.additional_info.snapshot.url, '_blank');
}
};

return (
<Modal open={isOpen}>
{!zipPreparing && (
Expand All @@ -181,6 +274,27 @@ export const DownloadStructureDialog = memo(({}) => {
)}
<DialogContent>
<Grid container direction="column">
<Grid item container direction="row">
<Grid item>
<Select className={classes.select} value={selectedDownload} onChange={onUpdateExistingDownload}>
<MenuItem value={newDownload}>{newDownload}</MenuItem>
{downloadTags.map(dt => (
<MenuItem value={dt.additional_info.downloadName}>{dt.additional_info.downloadName}</MenuItem>
))}
</Select>
</Grid>
<Grid item>
<Button
color="primary"
disabled={!(selectedDownload && selectedDownload !== newDownload)}
onClick={() => {
showSnapshotClicked();
}}
>
Show snapshot
</Button>
</Grid>
</Grid>
<Grid container item direction="row">
<Grid container item direction="column" xs={4}>
<RadioGroup
Expand Down Expand Up @@ -325,7 +439,7 @@ export const DownloadStructureDialog = memo(({}) => {
disabled={zipPreparing}
color="primary"
onClick={() => {
prepareDownloadClicked();
dispatch(prepareDownloadClicked());
}}
>
Prepare download
Expand Down
14 changes: 14 additions & 0 deletions js/reducers/api/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ export const setNoTagsReceived = noTagsReceived => {
};
};

export const setDownloadTags = downloadTags => {
return {
type: constants.SET_DOWNLOAD_TAGS,
downloadTags: downloadTags
};
};

export const appendToDownloadTags = tag => {
return {
type: constants.APPEND_TO_DOWNLOAD_TAGS,
tag: tag
};
};

export const appendMoleculeTag = moleculeTag => {
return {
type: constants.APPEND_MOLECULE_TAG,
Expand Down
12 changes: 10 additions & 2 deletions js/reducers/api/apiReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const INITIAL_STATE = {
direct_access: {},
direct_access_processed: false,
open_discourse_error_modal: false,
noTagsReceived: true
noTagsReceived: true,
downloadTags: []
};

export const RESET_TARGET_STATE = {
Expand Down Expand Up @@ -75,8 +76,9 @@ export const RESET_TARGET_STATE = {
sessionTitle: undefined,
user_id: undefined,
direct_access: {},
open_discourse_error_modal: false
open_discourse_error_modal: false,
// direct_access_processed: false
downloadTags: []
};

export default function apiReducers(state = INITIAL_STATE, action = {}) {
Expand Down Expand Up @@ -215,6 +217,12 @@ export default function apiReducers(state = INITIAL_STATE, action = {}) {
case constants.SET_DIRECT_ACCESS_PROCESSED:
return { ...state, direct_access_processed: action.direct_access_processed };

case constants.SET_DOWNLOAD_TAGS:
return { ...state, downloadTags: [...action.downloadTags] };

case constants.APPEND_TO_DOWNLOAD_TAGS:
return { ...state, downloadTags: [...state.downloadTags, action.tag] };

case constants.SET_SESSION_ID_LIST:
let sessionSummaryNew = [];
for (var key in action.sessionIdList) {
Expand Down
Loading

0 comments on commit cbaa408

Please sign in to comment.