|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +################################ run_notebook.sh ############################### |
| 4 | +# Josh Mitchell, Open Force Field Consortium, 2023 |
| 5 | +# |
| 6 | +# Run a notebook in the specified Conda environment, even if Conda/Mamba is not |
| 7 | +# installed. |
| 8 | +# |
| 9 | +# `bash`, `uname`, `dirname`, `tar`, `curl`, and `grep` must be available. |
| 10 | +# |
| 11 | +# `run_notebook.sh` should work on MacOS and most Linux distros. Windows and |
| 12 | +# other OSes are not supported. Architectures other than x86_64/AMD64 should |
| 13 | +# use an ISA emulation layer such as Rosetta for MacOS ARM64 (Apple Silicon). |
| 14 | +# |
| 15 | +# Micromamba, which is used by this script to make notebook dependencies |
| 16 | +# available, installs packages by: |
| 17 | +# |
| 18 | +# 1. Downloading the package TAR archive |
| 19 | +# 2. Extracting it to a package cache |
| 20 | +# 3. Hard linking to it from the virtual environment (or copying if hard linking |
| 21 | +# fails) |
| 22 | +# |
| 23 | +# This saves storage space and network activity when installing the same package |
| 24 | +# in multiple environments. As a result, deleting the generated environment |
| 25 | +# will not delete the micromamba package cache, which may be several gigabytes |
| 26 | +# in size. To remove unused packages from the cache after deleting an |
| 27 | +# environment, run: |
| 28 | +# |
| 29 | +# ./micromamba clean -a |
| 30 | +# |
| 31 | +# The location of the cache can be configured by setting the `MAMBA_ROOT_PREFIX` |
| 32 | +# environment variable before creating any environments. You may want to keep |
| 33 | +# the cache to save download time the next time you run an example, but be |
| 34 | +# aware that if a package is updated in the meanwhile it will need to be |
| 35 | +# re-updated anyway. |
| 36 | +# |
| 37 | +# Please report any issues with this script to our GitHub issue tracker: |
| 38 | +# https://github.com/openforcefield/openff-docs/issues |
| 39 | +# |
| 40 | +################################################################################ |
| 41 | + |
| 42 | +# Exit on failed commands |
| 43 | +# We don't set -u because GMXRC relies on some unset variables, so the |
| 44 | +# micromamba activate step will fail if set -u is called and GROMACS is in the |
| 45 | +# environment |
| 46 | +set -Ee -o pipefail |
| 47 | +shopt -s failglob |
| 48 | + |
| 49 | +# Make sure we're in the same directory as this script, |
| 50 | +# the environment file, and the notebook |
| 51 | +cd "$(dirname "$0")" |
| 52 | + |
| 53 | +################################ CONFIG ################################ |
| 54 | + |
| 55 | +# Choose a name for the new environment |
| 56 | +# Note that deleting this will not clean up the micromamba cache, which may be |
| 57 | +# substantial. |
| 58 | +ENVNAME='ccpbiosim-protprep' |
| 59 | + |
| 60 | +# The filename of the Conda environment file to run the notebook in |
| 61 | +ENV_FILE='environment.yaml' |
| 62 | + |
| 63 | +# The filename of the Jupyter notebook we want to run |
| 64 | +NOTEBOOK="*.ipynb" |
| 65 | + |
| 66 | +ARCH=$(uname -m) |
| 67 | + |
| 68 | +# The next section only applies if you have a dependency on Ambertools |
| 69 | +## For now, we always want x64, because Ambertools doesn't support other ISAs |
| 70 | +## In the future, we can configure this when we select the OS |
| 71 | +ARCH="64" |
| 72 | + |
| 73 | +################################################################################ |
| 74 | + |
| 75 | +# Choose the platform we're on |
| 76 | +OS=$(uname) |
| 77 | +if [[ "$OS" == "Linux" ]]; then |
| 78 | + PLATFORM="linux" |
| 79 | +elif [[ "$OS" == "Darwin" ]]; then |
| 80 | + PLATFORM="osx"; |
| 81 | +else |
| 82 | + echo "OS $OS is not supported by $0" |
| 83 | + echo "For detailed install instructions, see" |
| 84 | + echo "https://docs.openforcefield.org/en/latest/install.html" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +# Download the appropriate Micromamba binary if it's missing |
| 89 | +if [[ ! -f ./micromamba ]]; then |
| 90 | + echo "Downloading micromamba for $PLATFORM-$ARCH" |
| 91 | + curl -L https://micro.mamba.pm/api/micromamba/$PLATFORM-$ARCH/latest \ |
| 92 | + | tar -xvj --strip-components=1 bin/micromamba |
| 93 | +fi |
| 94 | +# Create an alias for micromamba (rather than put this directory in PATH): |
| 95 | +alias micromamba=`pwd`/micromamba |
| 96 | + |
| 97 | +# Hook Micromamba into the script's subshell (this only lasts for as long as the |
| 98 | +# script is running) |
| 99 | +eval "$(./micromamba shell hook --shell=bash)" |
| 100 | + |
| 101 | +# Create the new environment, or install and update packages from the YAML file |
| 102 | +# if it already exists |
| 103 | +if micromamba env list | grep "^\s*${ENVNAME}\s*"; then |
| 104 | + micromamba install --file "$ENV_FILE" --name "$ENVNAME" --yes |
| 105 | + micromamba update --file "$ENV_FILE" --name "$ENVNAME" --yes |
| 106 | +else |
| 107 | + micromamba create --file "$ENV_FILE" --name "$ENVNAME" --yes |
| 108 | +fi |
| 109 | + |
| 110 | +# Unset environment variables that might confuse Python |
| 111 | +unset PYTHONPATH |
| 112 | +unset PYTHONHOME |
| 113 | + |
| 114 | +# Activate the new environment |
| 115 | +micromamba activate "$ENVNAME" |
| 116 | +# Install pypi-only packages: |
| 117 | +# pip install whatever_you_need |
| 118 | +pip install git+https://github.com/CharlieLaughton/Alphafix.git |
| 119 | +# any other bootstrapping? |
| 120 | +mkdir -p blastp |
| 121 | +export BLASTDB=$PWD/blastp |
| 122 | +pwd=$PWD |
| 123 | +cd $BLASTDB |
| 124 | +update_blastdb.pl --decompress swissprot |
| 125 | +cd $pwd |
| 126 | +#t Open the notebook in the new environment |
| 127 | +jupyter lab $NOTEBOOK |
0 commit comments