# Job: DeepEMhancer (Wrapper)

## Description

This job is a wrapper for [DeepEMhancer](https://github.com/rsanchezgarc/deepEMhancer), a deep learning model trained to perform masking-like and sharpening-like postprocessing on cryo-EM maps \[1].

## DeepEMhancer License

Structura Biotechnology Inc. and CryoSPARC do not license [DeepEMhancer](https://github.com/rsanchezgarc/deepEMhancer) nor distribute DeepEMhancer binaries. Please ensure you have your own copy of DeepEMhancer licensed and installed under the terms of its [Apache License 2.0](https://github.com/rsanchezgarc/deepEMhancer/blob/master/LICENSE).

## Installation

{% hint style="danger" %}
For the following instructions, do not use the `conda` command or environment that is installed by CryoSPARC. This installation is destroyed and recreated with CryoSPARC updates, so new environments created by it will be erased and any changes made to it may break other CryoSPARC jobs.
{% endhint %}

DeepEMhancer depends on other python packages outside of what CryoSPARC provides. In order to install DeepEMhancer using a conda environment, you must use a conda environment manager and a conda environment that is separate from the CryoSPARC conda/python installation, and use that environment to install dependencies.

Install deepEMhancer following one of the methods described in the deepEMhancer repository at <https://github.com/rsanchezgarc/deepEMhancer>.

{% hint style="warning" %}
Important considerations for Master/Worker or Cluster installations:\
\
The path to the conda installation on the machine hosting `cryosparc_master` must exactly match the path on machines hosting `cryosparc_worker`. This may be achieved by creating the conda environment in a shared directory that is mounted under the same path on the CryoSPARC master and all workers.

The conda installation directory must be accessible by the CryoSPARC Linux user account with the required permissions for executing the deepemhancer script.
{% endhint %}

### Finding the executable path

With deepEMhancer installed and its conda environment active in the current shell, run

`which deepemhancer`

to find the full path to the deepEMhancer script. It should be similar to this:

`/home/cryosparcuser/miniforge3/envs/deepEMhancer_env/bin/deepemhancer`

{% hint style="info" %}
Instead of `miniforge3`, the conda environment manager may have been installed in a differently named directory, like `anaconda3` or `miniconda3`, depending on which conda environment manager you chose to install. Use the applicable directory name when specifying the *Path to deepEMhancer executable* job parameter or inside the (optional) wrapper shell script.
{% endhint %}

When running the CryoSPARC wrapper, input this as the value for the ‘Path to deepEMhancer executable’ parameter.

### (Optional) Create a shell script

The CryoSPARC and deepEMhancer environments and dependencies may conflict. As a workaround, you may need to wrap deepEMhancer in a shell script that deactivates the CryoSPARC environment and activates the deepEMhancer one.

Create a [`deepemhancer.sh`](http://deepemhancer.sh) file in a well known location, such as the home directory (e.g., `~/deepemhancer.sh`). The file should contain the following, making the noted substitutions:

{% code lineNumbers="true" %}

```bash
#!/usr/bin/env bash
if command -v conda > /dev/null 2>&1; then
conda deactivate > /dev/null 2>&1 || true  # ignore any errors
conda deactivate > /dev/null 2>&1 || true  # ignore any errors
fi
unset _CE_CONDA
unset CONDA_DEFAULT_ENV
unset CONDA_EXE
unset CONDA_PREFIX
unset CONDA_PROMPT_MODIFIER
unset CONDA_PYTHON_EXE
unset CONDA_SHLVL
unset PYTHONPATH
unset LD_PRELOAD
unset LD_LIBRARY_PATH

source $HOME/miniforge3/etc/profile.d/conda.sh
conda activate deepEMhancer_env
exec deepemhancer $@
```

{% endcode %}

* Replace `$HOME/miniforge3` on line 17 with the path to your conda installation.

Make this file executable by the CryoSPARC user from the command line

`chmod +x deepemhancer.sh`

Then, when running the CryoSPARC wrapper, input the full path to `deepemhancer.sh` as the value for the ‘Path to deepEMhancer executable’ parameter. This file needs to be accessible at the same path from both the master and worker nodes.

### (Optional) Set DeepEMhancer executable path as project-level default parameter

To avoid having to locate and set the executable path when building every DeepEMhancer job, in v4.1 onwards you can set a project-level default that will apply to all newly created DeepEMhancer jobs.

Navigate to the projects view, select a project and choose the DeepEMhancer executable path under the 'Project Level Parameters' module within the sidebar details panel:

<figure><img src="https://1916621962-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M7DGv3GkRvGGpbVPCgg%2Fuploads%2FqKZMNU1qKeKEzQb5MCv9%2Fv4_1_DeepEMhancer_project_level_path.png?alt=media&#x26;token=3ec9e7e3-a01b-4842-a137-9f49b2ccfc34" alt=""><figcaption><p>You can use the input field or file browser to enter a path to the DeepEMhancer executable.</p></figcaption></figure>

## Inputs

* Volume map
  * half maps (`map_half_A` and `map_half_B`) or full map (`map`), as specified by the `Use half maps` parameter
* Input mask (optional)
  * used to calculate the noise mean and standard deviation if the `Normalization mode` parameter is `1: Noise statistics`, or provided as a binary mask if the normalization parameter is `2: Binary Mask`

## Outputs

* Sharpened volume map (`map_sharp`)

## Common Parameters

* Path to deepEMhancer executable: full path to the deepEMhancer script, for example `/home/cryosparcuser/miniforge3/envs/deepEMhancer_env/bin/deepemhancer`
* Path to deepEMhancer models: full path to where the deepEMhancer pretrained models were installed, for example `/home/cryosparcuser/.local/share/deepEMhancerModels/production_checkpoints`

For details on the other processing parameters, see the deepEMhancer [repository](https://github.com/rsanchezgarc/deepEMhancer), or the help command (`deepemhancer -h` with the conda environment activated)

## Next Steps

Downloading and viewing the postprocessed map in a software such as UCSF Chimera, or in CryoSPARC’s built in volume viewer.

## References

\[1] R. Sanchez-Garcia, J. Gomez-Blanco, A. Cuervo et al., \*“\*DeepEMhancer: a deep learning solution for cryo-EM volume post-processing”, Communications Biology, vol. 4, no. 874, 2021. Available: 10.1038/s42003-021-02399-1.
