"""Environment-variable configuration for cBioPortal, OncoKB, and the requests cache.
Module-level ``get_*``/``set_*`` functions read and write ``os.environ`` for the
cBioPortal instance and token, OncoKB token, and requests-cache path, re-exporting the
output-directory helpers from :mod:`mkt.schema.config`.
"""
import os
import sys
from mkt.schema.config import OUTPUT_DIR_VAR, get_output_dir, set_output_dir
# enables `mkt.databases.config import *`
__all__ = [
"OUTPUT_DIR_VAR",
"get_output_dir",
"set_output_dir",
]
CBIOPORTAL_INSTANCE_VAR = "CBIOPORTAL_INSTANCE"
"""str: Environment variable for cBioPortal instance; \
if none provided, default is `www.cbioportal.org` in CLI scripts"""
CBIOPORTAL_TOKEN_VAR = "CBIOPORTAL_TOKEN"
"""str: Environment variable for cBioPortal token; if none provided, default is `None` in CLI scripts"""
REQUESTS_CACHE_VAR = "REQUESTS_CACHE"
"""str: Environment variable for request cache file prefix; \
if none provided, default is requests_cache in CLI scripts"""
ONCOKB_TOKEN_VAR = "ONCOKB_TOKEN"
"""str: Environment variable for OncoKB token; if none provided, default is `None` in CLI scripts"""
[docs]
def set_cbioportal_instance(val: str) -> None:
"""Set the cBioPortal instance in the environment variables.
Parameters
----------
val : str
cBioPortal instance; e.g., "cbioportal.mskcc.org" for MSKCC or "www.cbioportal.org"
Returns
-------
None
"""
os.environ[CBIOPORTAL_INSTANCE_VAR] = val
[docs]
def get_cbioportal_instance() -> str | None:
"""Get the cBioPortal instance from the environment.
Returns
-------
str | None
cBioPortal instance as string if exists, otherwise None
"""
try:
return os.environ[CBIOPORTAL_INSTANCE_VAR]
except KeyError:
print(
"cBioPortal instance not found in environment variables. This is necessary to run analysis. Exiting..."
)
sys.exit(1)
[docs]
def set_cbioportal_token(val: str) -> None:
"""Set the cBioPortal token in the environment variables.
Parameters
----------
val : str
cBioPortal token
Returns
-------
None
"""
os.environ[CBIOPORTAL_TOKEN_VAR] = val
[docs]
def maybe_get_cbioportal_token() -> str | None:
"""Get the cBioPortal token from the environment.
Returns
-------
str | None
cBioPortal token as string if exists, otherwise None
"""
try:
return os.environ[CBIOPORTAL_TOKEN_VAR]
except KeyError:
return None
[docs]
def set_request_cache(val: str) -> None:
"""Set the request cache path in environment variables.
Parameters
----------
val : str
Request cache path
Returns
-------
None
"""
os.environ[REQUESTS_CACHE_VAR] = val
[docs]
def maybe_get_request_cache() -> str | None:
"""Get the request cache path from the environment.
Returns
-------
str | None
Request cache path as string if exists, otherwise None
"""
try:
return os.environ[REQUESTS_CACHE_VAR]
except KeyError:
return None
[docs]
def set_oncokb_token(val: str) -> None:
"""Set the OncoKB token in the environment variables.
Parameters
----------
val : str
OncoKB token
Returns
-------
None
"""
os.environ[ONCOKB_TOKEN_VAR] = val
[docs]
def maybe_get_oncokb_token() -> str | None:
"""Get the OncoKB token from the environment.
Returns
-------
str | None
OncoKB token as string if exists, otherwise None
"""
try:
return os.environ[ONCOKB_TOKEN_VAR]
except KeyError:
return None