-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup_hybrid
79 lines (63 loc) · 2.27 KB
/
setup_hybrid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
"""Set up MICOM 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()
COBRAPY_URL = "git+https://github.com/cdiener/cobrapy@feature/hybrid_solver"
MICOM_URL = "git+https://github.com/micom-dev/micom@feature/hybrid_solver"
OPTLANG_URL = "git+https://github.com/cdiener/optlang@feature/hybrid_solver"
def has_hybrid():
try:
import micom as mm
from cobra.util.solver import interface_to_str
com = mm.Community(mm.data.test_taxonomy(), solver="hybrid")
assert "hybrid" in interface_to_str(com.solver.problem)
except Exception:
return False
return True
def cleanup():
"""Remove downloaded files."""
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)
if __name__ == "__main__":
if not has_hybrid():
run_and_check(
["python", "-m", "pip", "install", COBRAPY_URL, MICOM_URL, "Cython", "biom-format"],
"done",
":stew: Installing MICOM...",
"failed installing MICOM :sob:",
":white_check_mark: Done."
)
run_and_check(
["pip", "install", "--no-deps", "highspy", OPTLANG_URL],
"done",
":stew: Installing the hybrid solver...",
"failed installing the solver :sob:",
":white_check_mark: Done."
)
else:
con.log(":snake: Everything already installed and working. Skipped.")
cleanup()
con.log("[green]Everything is A-OK. "
"You can start using MICOM now :thumbs_up:[/green]")