-
Notifications
You must be signed in to change notification settings - Fork 18
/
organisms.py
78 lines (63 loc) · 3.69 KB
/
organisms.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
74
75
76
77
78
"""
Defines organisms as chemical_compounds_state objects.
Unpickled after calculation when setting up RP3.
"""
# General utilities
import logging
import pickle
import os
import csv
import sys
from config import *
# RP3 specific objects
from compound import Compound, unpickle
from chemical_compounds_state import ChemicalCompoundState
from rdkit.Chem import AllChem
from utilities.reactor.Utils import standardize_chemical, standardize_results, handle_results, ChemConversionError
from utilities.reactor.cli import worker_match, worker_fire, kill, RuleConversionError
class NotReady(Exception):
"""Raised when organisms or rules have not been caculated in advance"""
def __init__(self, msg = "Not Ready. Need to run set-up scripts"):
self._msg = msg
def __str__(self):
return self._msg
def import_organism_from_csv(csv_file, add_Hs = True):
with open(csv_file) as csv_handle:
dict_reader = csv.DictReader(csv_handle, delimiter = ',')
compound_list = []
for row in dict_reader:
name = row["name"]
inchi = row["inchi"]
if inchi is None or inchi == "None" or inchi =="":
pass
else:
try:
if name.startswith("InChI"):
compound = Compound(InChI = inchi, heavy_standardisation = True, force_add_H = add_Hs)
else:
compound = Compound(InChI = inchi, name = name, heavy_standardisation = True, force_add_H = add_Hs)
if not compound.in_list(compound_list, main_layer = False):
compound_list.append(compound)
except ChemConversionError as e:
logging.error("For compound {} with inchi {}: error ChemConversionError".format(name, inchi))
organism = ChemicalCompoundState(compound_list, main_layer = False)
# organism.set_main_layer(True)
return(organism)
organisms_data_path = "{}/organisms".format(DATA_PATH)
if not os.path.exists(organisms_data_path):
os.mkdir(organisms_data_path)
if not os.path.exists(organisms_data_path + '/state_iML1515_chassis_H.pkl'):
logging.error("Please run calculate_organisms script")
raise NotReady
Test_organism_H = unpickle(file_name = "{}".format('Test_organism_H'), type = 'state', folder_address = organisms_data_path)
ecoli_chassis_H = unpickle(file_name = "{}".format('iML1515_chassis_H'), type = 'state', folder_address = organisms_data_path)
detectable_cmpds_H = unpickle(file_name = "{}".format('detectable_cmpds_H'), type = 'state', folder_address = organisms_data_path)
core_ecoli_H = unpickle(file_name = "{}".format('core_ecoli_H'), type = 'state', folder_address = organisms_data_path)
bsubtilis_H = unpickle(file_name = "{}".format('bsubtilis_H'), type = 'state', folder_address = organisms_data_path)
iJO1366_chassis_H = unpickle(file_name = "{}".format('iJO1366_chassis_H'), type = 'state', folder_address = organisms_data_path)
Test_organism_noH = unpickle(file_name = "{}".format('Test_organism_noH'), type = 'state', folder_address = organisms_data_path)
ecoli_chassis_noH = unpickle(file_name = "{}".format('iML1515_chassis_noH'), type = 'state', folder_address = organisms_data_path)
detectable_cmpds_noH = unpickle(file_name = "{}".format('detectable_cmpds_noH'), type = 'state', folder_address = organisms_data_path)
core_ecoli_noH = unpickle(file_name = "{}".format('core_ecoli_noH'), type = 'state', folder_address = organisms_data_path)
bsubtilis_noH = unpickle(file_name = "{}".format('bsubtilis_noH'), type = 'state', folder_address = organisms_data_path)
iJO1366_chassis_noH = unpickle(file_name = "{}".format('iJO1366_chassis_noH'), type = 'state', folder_address = organisms_data_path)