-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
73 lines (49 loc) · 1.98 KB
/
utils.py
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
import os
import logging
import ConfigParser
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def safe_open(filename, mode='r'):
try:
if mode == 'w':
dirname = os.path.dirname(filename)
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
if filename.endswith('.gz'):
import gzip
mode += 'b'
return gzip.open(filename, mode)
return open(filename, mode)
except Exception as e:
get_logger().error('file not exists: {}'.format(filename))
exit(1)
def get_logger(log_format='[%(asctime)s %(levelname)s] %(message)s', log_level=logging.INFO):
logging.basicConfig(
level=log_level, format=log_format, datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
return logger
def get_config(logger, configfile=None):
print BASE_DIR
if not configfile:
if 'NJ' in BASE_DIR:
configfile = os.path.join(BASE_DIR, 'config_nanjing.ini')
elif 'TJ' in BASE_DIR:
configfile = os.path.join(BASE_DIR, 'config_tianjin.ini')
else:
configfile = os.path.join(BASE_DIR, 'config.ini')
if not os.path.exists(configfile):
logger.error('config file not exists, please check: {}'.format(configfile))
exit(1)
logger.info('use config file: {}'.format(configfile))
config = ConfigParser.ConfigParser()
config.read(configfile)
annovar_dir = config.get('software', 'annovar_dir')
humandb = config.get('software', 'humandb')
if not os.path.exists(annovar_dir):
logger.error('annovar_dir not exists, please check: {}'.format(annovar_dir))
exit(2)
if not os.path.exists(humandb):
logger.error('humandb not exists, please check: {}'.format(humandb))
exit(2)
logger.info('find annovar directory: {annovar_dir}'.format(**locals()))
logger.info('find humandb directory: {humandb}'.format(**locals()))
return config