-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhelpers.py
53 lines (42 loc) · 1.68 KB
/
helpers.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
# -*- coding: utf-8 -*-
import logging
import os
import requests
import pickle
import json
from configparser import ConfigParser
logger = logging.getLogger(__name__)
project_dir = os.path.abspath(os.path.dirname(__file__)) + "/"
config = ConfigParser()
config.read(project_dir + "/config.cfg")
def get_or_download_file(filename, k, value, config):
folder_path = f"{project_dir}/" + config.get(k, value)
file_path = f"{folder_path}/{filename}"
if not os.path.exists(folder_path):
logger.warning("mkdir: %s", folder_path)
os.makedirs(folder_path)
if not os.path.exists(file_path):
logger.warning("downloading data file to: %s", file_path)
url = "https://bioseed.mcs.anl.gov/~fliu/modelseedpy/" + filename
r = requests.get(url, allow_redirects=True)
with open(file_path, "wb") as fh:
fh.write(r.content)
return file_path
def get_file(filename, k, value):
return get_or_download_file(filename, k, value, config)
def get_classifier(classifier_id):
from modelseedpy.core.msgenomeclassifier import MSGenomeClassifier
cls_pickle = get_file(f"{classifier_id}.pickle", "data", "classifier_folder")
cls_features = get_file(
f"{classifier_id}_features.json", "data", "classifier_folder"
)
with open(cls_pickle, "rb") as fh:
model_filter = pickle.load(fh)
with open(cls_features, "r") as fh:
features = json.load(fh)
return MSGenomeClassifier(model_filter, features)
def get_template(template_id):
# we need a mstemplate object!
template_file = get_file(f"{template_id}.json", "data", "template_folder")
with open(template_file, "r") as fh:
return json.load(fh)