-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDataSelection.py
199 lines (177 loc) · 8.86 KB
/
TestDataSelection.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 20 14:26:03 2018
@author: dimitricoukos
"""
import unittest
import cobra
import json
from DataTreatment import DataType, openJson, matchById, write,\
Enzyme, Metabolite, MetaboliteCandidate, writeEnzymes, selectBestData,\
getData
class MatchById(unittest.TestCase):
'''Contains functions to test Match By Id
Note:
openJson already checked in TestFileIO.py
'''
def loadMadeUpModel(path):
'''Load a cobra model using a structure based on Enzymes and Metabolite
Candidates
Args:
path: filepath to made up mode.
Note:
This function, based on DataTreatment.storeBiGGRepresentation is
for testing purposes, and allows to load a made up model.
'''
made_up_model = cobra.io.load_json_model(path)
local_repr = {}
for reaction in made_up_model.reactions:
if reaction.id == 'CSND' or reaction.id == 'DHPM1':
local_repr[reaction.id] = Enzyme(reaction.id)
for reactant in reaction.reactants:
local_repr[reaction.id].forward[reactant.name] = \
Metabolite(reactant.name, bigg=reactant.id)
for product in reaction.products:
local_repr[reaction.id].backward[product.name] = \
Metabolite(product.name, bigg=product.id)
return local_repr
brenda_keggs = openJson('Unit Tests/sample_brenda_keggs.json')
treated_brenda_output = openJson(
'Unit Tests/sample_simple_brenda_output.json')
data_type = DataType.turnover
potential_updates_dict = openJson(
'Unit Tests/correct_potential_updates.json')
simple_test_model = loadMadeUpModel('Unit Tests/simple_test_model.json')
simple_test_model['CSND'].with_kegg['C00380'] = 'cyt'
simple_test_model['CSND'].with_kegg['D00323'] = '5-fluorocyt'
simple_test_model['DHPM1'].with_kegg['C00148'] = 'DL-p'
correct_potential_updates = {}
for reaction in potential_updates_dict:
correct_potential_updates.update({reaction: Enzyme(reaction)})
if reaction in brenda_keggs:
for kegg in brenda_keggs[reaction]:
brenda_name = brenda_keggs[reaction][kegg]
if kegg in simple_test_model[reaction].with_kegg:
if simple_test_model[reaction].with_kegg[kegg] in\
simple_test_model[reaction].forward:
if treated_brenda_output[reaction][
brenda_keggs[reaction][kegg]] != []:
name = simple_test_model[reaction].with_kegg[kegg]
correct_potential_updates[reaction].forward[
name] = []
for entry in treated_brenda_output[reaction][
brenda_name]:
data = {
'organism': entry['organism'],
'wild-type': entry['wild-type'],
'turnover': entry['turnoverNumber']
}
correct_potential_updates[reaction].forward[
name].append(MetaboliteCandidate(
brenda_name, data))
elif simple_test_model[reaction].with_kegg[kegg] in\
simple_test_model[reaction].backward:
name = simple_test_model[reaction].with_kegg[kegg]
correct_potential_updates[reaction].backward[name] = []
for entry in treated_brenda_output[reaction][brenda_name]:
data = {
'organism': entry['organism'],
'wild-type': entry['wild-type'],
'turnover': entry['turnoverNumber']
}
correct_potential_updates[reaction].backward[
name].append(MetaboliteCandidate(
brenda_name, data))
correct_unmatched = openJson('Unit Tests/correct_unmatched.json')
def test_matchById_potential_updates(self):
'''matchByName should match BiGG metabolites with BRENDA metabolites
given a file containing their respective KEGG Ids.
'''
potential_updates = {}
matchById(potential_updates, MatchById.brenda_keggs,
MatchById.treated_brenda_output, MatchById.data_type,
MatchById.simple_test_model)
writeEnzymes('Unit Tests/return_matchById_potential_updates.json',
potential_updates)
potential_updates_as_dict = {}
correct_potential_updates_as_dict = {}
for enzyme in potential_updates:
potential_updates_as_dict[enzyme] = \
potential_updates[enzyme].getDict()
for enzyme in MatchById.correct_potential_updates:
correct_potential_updates_as_dict[enzyme] = \
MatchById.correct_potential_updates[enzyme].getDict()
self.assertDictEqual(potential_updates_as_dict,
correct_potential_updates_as_dict,
msg='Potential updates incorrect.')
class SelectBestData(unittest.TestCase):
'''contains unit tests for selectBestData
TODO:
Organism:
* Write test with many options.
* Write test with incorrect options.
* Write test with one option.
* Write test with no options.
* Write test with wild type, and no wild type.
* Write test with negative turnover etc...
'''
def toEnzymeStructure_f(data_dict):
'''Converts dictionary structure to DataTreatment.Enzyme based
structure to be able to test data selection functions.'''
organized_data = {}
for enzyme in data_dict:
organized_data[enzyme] = Enzyme(enzyme)
for metabolite in data_dict[enzyme]:
organized_data[enzyme].forward[metabolite] = []
data = getData(data_dict[enzyme], metabolite,
DataType.turnover)
for index, entry in enumerate(data_dict[enzyme][metabolite]):
organized_data[enzyme].forward[metabolite].append(
MetaboliteCandidate(metabolite, **data[index]))
return organized_data
def toEnzymeStructure_b(data_dict):
'''Converts dictionary structure to DataTreatment.Enzyme based
structure to be able to test data selection functions.'''
organized_data = {}
for enzyme in data_dict:
organized_data[enzyme] = Enzyme(enzyme, '1.1.1.1')
for metabolite in data_dict[enzyme]:
organized_data[enzyme].backward[metabolite] = []
data = getData(data_dict[enzyme], metabolite,
DataType.turnover)
for index, entry in enumerate(data_dict[enzyme][metabolite]):
organized_data[enzyme].backward[metabolite].append(
MetaboliteCandidate(metabolite, **data[index]))
return organized_data
def test_selectBestData_many_options(self):
self.maxDiff = None
correct_data_dict = openJson('Unit Tests/selectData_many_organisms'
'_correct.json')
data = openJson('Unit Tests/selectData_many_organisms.json')
data_test_forward = SelectBestData.toEnzymeStructure_f(data)
data_test_backward = SelectBestData.toEnzymeStructure_b(data)
selectBestData(data_test_forward, DataType.turnover)
selectBestData(data_test_backward, DataType.turnover)
data_test_forward_dict = {}
data_test_backward_dict = {}
for enzyme in data_test_forward:
enzyme_dict = data_test_forward[
enzyme].getSimpleDict()
if enzyme_dict != {}:
data_test_forward_dict[enzyme] = enzyme_dict
for enzyme in data_test_backward:
enzyme_dict = data_test_backward[
enzyme].getSimpleDict()
if enzyme_dict != {}:
data_test_backward_dict[enzyme] = enzyme_dict
with open('Unit Tests/test_data_selection_output.json', 'w') \
as outfile:
json.dump(data_test_forward_dict, outfile, indent=4)
with open('Unit Tests/test_data_selection_output_2.json', 'w')\
as outfile:
json.dump(data_test_backward_dict, outfile, indent=4)
self.assertDictEqual(correct_data_dict, data_test_forward_dict)
self.assertDictEqual(correct_data_dict, data_test_backward_dict)
if __name__ == '__main__':
unittest.main()