Skip to content

Instantly share code, notes, and snippets.

@cdiener
Last active August 10, 2023 13:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cdiener/be2e4caded84dcdcba8b2b0c0e0771d3 to your computer and use it in GitHub Desktop.
Save cdiener/be2e4caded84dcdcba8b2b0c0e0771d3 to your computer and use it in GitHub Desktop.
Set up Qiime 2 on Google colab
#!/usr/bin/env python3
"""Set up Qiime 2 on Google colab.
Do not use this on o local machine, especially not as an admin!
"""
import os
import sys
import shutil
from subprocess import Popen, PIPE
r = Popen(["pip", "install", "rich"])
r.wait()
from rich.console import Console # noqa
con = Console()
has_conda = "conda version" in os.popen("conda info").read()
has_qiime = "QIIME 2 release:" in os.popen("qiime info").read()
MINICONDA_PATH = (
"https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh"
)
QIIME_YAML_TEMPLATE = (
"https://data.qiime2.org/distro/core/qiime2-{version}-py{python}-linux-conda.yml"
)
if len(sys.argv) == 2:
version = sys.argv
else:
version = "2022.8"
if float(version) < 2021.4:
pyver = "36"
else:
pyver = "38"
QIIME_YAML_URL = QIIME_YAML_TEMPLATE.format(version=version, python=pyver)
QIIME_YAML = os.path.basename(QIIME_YAML_URL)
def cleanup():
"""Remove downloaded files."""
if os.path.exists("Miniconda3-latest-Linux-x86_64.sh"):
os.remove("Miniconda3-latest-Linux-x86_64.sh")
if os.path.exists(QIIME_YAML):
os.remove(QIIME_YAML)
if os.path.exists("/content/sample_data"):
shutil.rmtree("/content/sample_data")
con.log(":broom: Cleaned up unneeded files.")
def run_and_check(args, check, message, failure, success, console=con):
"""Run a command and check that it worked."""
console.log(message)
r = Popen(args, env=os.environ, stdout=PIPE, stderr=PIPE,
universal_newlines=True)
o, e = r.communicate()
out = o + e
if r.returncode == 0 and check in out:
console.log("[blue]%s[/blue]" % success)
else:
console.log("[red]%s[/red]" % failure, out)
cleanup()
sys.exit(1)
def _hack_in_the_plugins():
"""Add the plugins to QIIME."""
import qiime2.sdk as sdk
from importlib.metadata import entry_points
pm = sdk.PluginManager(add_plugins=False)
for entry in entry_points()["qiime2.plugins"]:
plugin = entry.load()
package = entry.value.split(':')[0].split('.')[0]
pm.add_plugin(plugin, package, entry.name)
if __name__ == "__main__":
if not has_conda:
run_and_check(
["wget", MINICONDA_PATH],
"saved",
":snake: Downloading miniconda...",
"failed downloading miniconda :sob:",
":snake: Done."
)
run_and_check(
["bash", "Miniconda3-latest-Linux-x86_64.sh", "-bfp", "/usr/local"],
"installation finished.",
":snake: Installing miniconda...",
"could not install miniconda :sob:",
":snake: Installed miniconda to `/usr/local`."
)
else:
con.log(":snake: Miniconda is already installed. Skipped.")
if not has_qiime:
run_and_check(
["wget", QIIME_YAML_URL],
"saved",
":mag: Downloading Qiime 2 package list...",
"could not download package list :sob:",
":mag: Done."
)
run_and_check(
["conda", "env", "update", "-n", "base", "--file", QIIME_YAML],
"To activate this environment, use",
":mag: Installing Qiime 2. This may take a little bit.\n :clock1:",
"could not install Qiime 2 :sob:",
":mag: Done."
)
run_and_check(
["pip", "install", "empress"],
"Successfully installed empress-",
":evergreen_tree: Installing Empress...",
"could not install Empress :sob:",
":evergreen_tree: Done."
)
else:
con.log(":mag: Qiime 2 is already installed. Skipped.")
run_and_check(
["qiime", "info"],
"QIIME 2 release:",
":bar_chart: Checking that Qiime 2 command line works...",
"Qiime 2 command line does not seem to work :sob:",
":bar_chart: Qiime 2 command line looks good :tada:"
)
if sys.version_info[0:2] == (int(pyver[0]), int(pyver[1])):
sys.path.append("/usr/local/lib/python3.{}/site-packages".format(pyver[1]))
con.log(":mag: Fixed import paths to include Qiime 2.")
con.log(":bar_chart: Checking if Qiime 2 import works...")
try:
import qiime2 # noqa
except Exception:
con.log("[red]Qiime 2 can not be imported :sob:[/red]")
sys.exit(1)
con.log("[blue]:bar_chart: Qiime 2 can be imported :tada:[/blue]")
con.log(":bar_chart: Setting up QIIME 2 plugins...")
try:
_hack_in_the_plugins()
from qiime2.plugins import feature_table # noqa
except Exception:
con.log("[red]Could not add the plugins :sob:[/red]")
sys.exit(1)
con.log("[blue]:bar_chart: Plugins are working :tada:[/blue]")
cleanup()
con.log("[green]Everything is A-OK. "
"You can start using Qiime 2 now :thumbs_up:[/green]")
@cappelchi
Copy link

Great code, thank you! how to import plugins?
'from qiime2.plugins import diversity' is not working.

@cdiener
Copy link
Author

cdiener commented Oct 8, 2020

On you own machine just restart jupyter. This will not work on colab though due to a limitation in Python itself. Python registers plugins on startup and AFAIK you can't trigger this manually. So any plugin installed after colab is started can not be loaded.

@Vlasovets
Copy link

Vlasovets commented Oct 21, 2020

Dear Christian, thank you for the code!

Can you elaborate on how to import plugins, please? "On you own machine just restart jupyter" is not fully clear to me.
The steps I follow:

  1. Connect to hosted runtime
  2. Run your code
  3. Try to import plugin from qiime2.plugins import feature_table
    Here, I'm getting the ImportError.

I'm a windows user and were not able to make qiime2 work on my local machine that is why I switched to Colab.
Thank you!

P.S.
If you cannot import plugins from qiime2 to Colab, what are the use cases of using qiime2 in Colab then?

@cdiener
Copy link
Author

cdiener commented Oct 21, 2020

Yep you can't import plugins with python on colab like stated above. You can still run every qiime command from the command line though. Have a look at https://colab.research.google.com/github/Gibbons-Lab/isb_course_2020/blob/master/16S.ipynb for an example.

@cdiener
Copy link
Author

cdiener commented Sep 8, 2022

Correction. As of 2022 you can use the plugins from the command line as long as the internal colab Python version and Qiime2 python version are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment