Diff of /openomics/__init__.py [000000] .. [548210]

Switch to unified view

a b/openomics/__init__.py
1
from __future__ import print_function, division, absolute_import
2
3
import json
4
import logging
5
import os
6
import sys
7
from os.path import join
8
9
import astropy
10
import dask.dataframe as dd
11
import pandas as pd
12
13
"""Top-level package for openomics."""
14
15
__author__ = """Nhat (Jonny) Tran"""
16
__email__ = 'nhat.tran@mavs.uta.edu'
17
__version__ = '0.8.9'
18
19
20
# Initialize configurations
21
this = sys.modules[__name__]
22
this.config = {}
23
24
# Set pandas backend
25
this.config["backend"] = pd
26
27
# Set cache download directory
28
this.config["cache_dir"] = astropy.config.get_cache_dir(this.__name__)
29
logging.info("Cache directory is", this.config["cache_dir"])
30
31
home_dir = os.path.expanduser('~')
32
user_conf_path = join(home_dir, ".openomics/conf.json")
33
34
# Initialize user configuration file at ~/.openomics/conf.json
35
if not os.path.exists(user_conf_path):
36
    if not os.path.exists(join(home_dir, ".openomics")):
37
        os.makedirs(join(home_dir, ".openomics"))
38
39
    if not os.path.isfile(user_conf_path):
40
        base_conf = {}
41
        base_conf['cache_dir'] = astropy.config.get_cache_dir(this.__name__)
42
43
        with open(user_conf_path, 'w', encoding='utf-8') as file:
44
            json.dump(base_conf, fp=file, indent=4)
45
46
# Read configuration from ~/.openomics/conf.json
47
if os.path.isfile(user_conf_path):
48
    try:
49
        with open(user_conf_path, 'r', encoding='utf-8') as file:
50
            user_conf = json.load(fp=file)
51
52
        if user_conf:
53
            for p in user_conf.get('database', []):
54
                this.config.update(p)
55
56
    except Exception as e:
57
        logging.info("Could not import configurations from", user_conf_path)
58
59
# Import submodules
60
61
from .transcriptomics import *
62
from .genomics import *
63
from .proteomics import *
64
from .clinical import *
65
from .multiomics import *
66
67
68
def set_backend(new: str = "pandas"):
69
    """Set the dataframe processing backend to either Pandas or Dask.
70
71
    Args:
72
        new (str): Either "dask" or "pandas". Default "pandas.
73
    """
74
    assert new in ["dask", "pandas", "modin"]
75
76
    if new == "dask":
77
        this.config["backend"] = dd
78
    else:
79
        this.config["backend"] = pd
80
81
82
def set_cache_dir(path: str, delete_temp: bool = False):
83
    """Set the path where external databases downloaded from URL are saved.
84
85
    Args:
86
        path (str):
87
        delete_temp (bool):
88
    """
89
    if not os.path.exists(path):
90
        raise NotADirectoryError(path)
91
92
    this.config["cache_dir"] = path
93
    astropy.config.set_temp_cache(path=path, delete=delete_temp)
94
    logging.info("Cache directory is", this.config["cache_dir"])