This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_lomap.py
243 lines (170 loc) · 9.55 KB
/
test_lomap.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os.path, sys
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
import unittest
from unittest import skipIf
from lomap.dbmol import DBMolecules
from lomap.graphgen import GraphGen
from lomap.mcs import MCS
import argparse
import multiprocessing
import networkx as nx
import networkx.algorithms.isomorphism as iso
import pickle
from rdkit import RDLogger
# Python graph section must be update to fix a bug
py_ver = int(sys.version[0])
GR_COMP = False
if py_ver < 3:
GR_COMP=True
class TestLomap(unittest.TestCase):
def setUp(self):
self.inst = DBMolecules('test/basic/', parallel=1, verbose='off', output=False, time=20, ecrscore=0.0 ,name='out', display=False, max=6, cutoff=0.4, radial=False, hub=None)
# Test class Inizialitazion
# Check wrong number of argumetns or wrong options
def test_InsufficientArgs(self):
self.assertRaises(TypeError, self.inst.__init__)
# Check passed input Types
def test_TypeArgs(self):
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/nodir/')
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', time=-1)
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', parallel=-1)
self.assertRaises(SystemExit, self.inst.__init__, 'test/basic', parallel=-1.5)
self.assertRaises(SystemExit, self.inst.__init__, 'test/basic', verbose='err_option')
self.assertRaises(SystemExit, self.inst.__init__, 'test/basic', time=-1.5)
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', ecrscore=-1.5)
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', ecrscore=2.0)
self.assertRaises(TypeError, self.inst.__init__, 'test/basic', output=-5.0)
self.assertRaises(TypeError, self.inst.__init__, 'test/basic', radial=-5.0)
self.assertRaises(TypeError, self.inst.__init__, 'test/basic', display=-5.0)
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', max=-5)
self.assertRaises(SystemExit, self.inst.__init__, 'test/basic', max=-5.0)
self.assertRaises(argparse.ArgumentTypeError, self.inst.__init__, 'test/basic', cutoff=-5)
self.assertRaises(SystemExit, self.inst.__init__, 'test/basic', max='string')
# Test class methods
# Check iter and next
def test_iter_next(self):
for i in range(0,self.inst.nums()):
self.inst.next()
self.assertRaises(StopIteration, self.inst.next)
# Check get, set and add
def test_get_set_add(self):
self.assertRaises(IndexError, self.inst.__getitem__, self.inst.nums()+1)
self.assertRaises(IndexError, self.inst.__setitem__, self.inst.nums()+1, self.inst[1])
self.assertRaises(ValueError, self.inst.__setitem__, 0, 'no_mol_obj')
self.assertRaises(ValueError, self.inst.__add__, 'no_mol_obj')
# Check read mol2 files
def test_read_mol2_files(self):
db = DBMolecules('test/basic')
db.options.directory = 'test/'
self.assertRaises(IOError, db.read_mol2_files)
# Check serial and parallel mode
def test_serial_parallel(self):
db = DBMolecules('test/basic')
s_strict, s_loose = db.build_matrices()
db.options.paralell = multiprocessing.cpu_count()
p_strict, p_loose = db.build_matrices()
self.assertEqual(True, all(s_strict == p_strict))
self.assertEqual(True, all(s_loose == p_loose))
# Check Graph
@skipIf(not GR_COMP, 'The graph test has been skipped untill a bug in the graph generation between py2 and py3 will be fixed')
def test_graph(self):
db = self.inst
strict,loose = db.build_matrices()
graph = db.build_graph()
self.assertRaises(IOError, nx.nx_agraph.write_dot, graph, '/check.dot')
mol2_graph = nx.read_gpickle("test/basic/molecules.gpickle")
dic1_nodes = graph.nodes(data=True)
dic1_edges = graph.edges(data=True)
dic2_nodes = mol2_graph.nodes(data=True)
dic2_edges = mol2_graph.edges(data=True)
self.assertEqual(True, dic1_nodes == dic2_nodes)
self.assertEqual(True, dic2_edges == dic2_edges)
nm = iso.categorical_node_match(['fname_comp','ID'],['noname',-1])
em = iso.categorical_edge_match(['strict_flag','similarity'],[False,-1.0])
self.assertEqual(True, nx.is_isomorphic(graph, mol2_graph , node_match=nm, edge_match=em))
#check the radial graph option
@skipIf(not GR_COMP, 'The graph test has been skipped untill a bug in the graph generation between py2 and py3 will be fixed')
def test_graph_radial(self):
db = DBMolecules('test/radial/', radial = True)
strict,loose = db.build_matrices()
graph = db.build_graph()
mol2_graph = nx.read_gpickle("test/radial/radial.gpickle")
dic1_nodes = graph.nodes(data=True)
dic1_edges = graph.edges(data=True)
dic2_nodes = mol2_graph.nodes(data=True)
dic2_edges = mol2_graph.edges(data=True)
self.assertEqual(True, dic1_nodes == dic2_nodes)
self.assertEqual(True, dic2_edges == dic2_edges)
nm = iso.categorical_node_match(['fname_comp','ID'],['noname',-1])
em = iso.categorical_edge_match(['strict_flag','similarity'],[False,-1.0])
self.assertEqual(True, nx.is_isomorphic(graph, mol2_graph , node_match=nm, edge_match=em))
#check the radial hub graph option
@skipIf(not GR_COMP, 'The graph test has been skipped untill a bug in the graph generation between py2 and py3 will be fixed')
def test_graph_radial_hub(self):
db = DBMolecules('test/radial/', radial = True, hub="ejm_46.mol2")
strict,loose = db.build_matrices()
graph = db.build_graph()
mol2_graph = nx.read_gpickle("test/radial/radial_hub.gpickle")
dic1_nodes = graph.nodes(data=True)
dic1_edges = graph.edges(data=True)
dic2_nodes = mol2_graph.nodes(data=True)
dic2_edges = mol2_graph.edges(data=True)
self.assertEqual(True, dic1_nodes == dic2_nodes)
self.assertEqual(True, dic2_edges == dic2_edges)
nm = iso.categorical_node_match(['fname_comp','ID'],['noname',-1])
em = iso.categorical_edge_match(['strict_flag','similarity'],[False,-1.0])
self.assertEqual(True, nx.is_isomorphic(graph, mol2_graph , node_match=nm, edge_match=em))
@skipIf(not GR_COMP, 'The graph test has been skipped untill a bug in the graph generation between py2 and py3 will be fixed')
def test_graph_radial_hub_fingerprint(self):
db = DBMolecules('test/radial/', radial = True, fingerprint=True, hub="ejm_46.mol2")
strict,loose = db.build_matrices()
graph = db.build_graph()
mol2_graph = nx.read_gpickle("test/radial/radial_hub_fingerprint.gpickle")
dic1_nodes = graph.nodes(data=True)
dic1_edges = graph.edges(data=True)
dic2_nodes = mol2_graph.nodes(data=True)
dic2_edges = mol2_graph.edges(data=True)
self.assertEqual(True, dic1_nodes == dic2_nodes)
self.assertEqual(True, dic2_edges == dic2_edges)
nm = iso.categorical_node_match(['fname_comp','ID'],['noname',-1])
em = iso.categorical_edge_match(['strict_flag','similarity'],[False,-1.0])
self.assertEqual(True, nx.is_isomorphic(graph, mol2_graph , node_match=nm, edge_match=em))
@skipIf(not GR_COMP, 'The graph test has been skipped untill a bug in the graph generation between py2 and py3 will be fixed')
def test_graph_radial_hub_fingerprint(self):
db = DBMolecules('test/radial/', radial = True, fingerprint=True, fast=True, hub="ejm_46.mol2")
strict,loose = db.build_matrices()
graph = db.build_graph()
mol2_graph = nx.read_gpickle("test/radial/radial_hub_fingerprint_fast.gpickle")
dic1_nodes = graph.nodes(data=True)
dic1_edges = graph.edges(data=True)
dic2_nodes = mol2_graph.nodes(data=True)
dic2_edges = mol2_graph.edges(data=True)
self.assertEqual(True, dic1_nodes == dic2_nodes)
self.assertEqual(True, dic2_edges == dic2_edges)
nm = iso.categorical_node_match(['fname_comp','ID'],['noname',-1])
em = iso.categorical_edge_match(['strict_flag','similarity'],[False,-1.0])
self.assertEqual(True, nx.is_isomorphic(graph, mol2_graph , node_match=nm, edge_match=em))
def test_mcs(self):
f = open('test/basic/MCS.pickle','rb')
data = pickle.load(f)
data_no_hydrogens = data[0]
data_hydrogens = data[1]
db = self.inst
nohyds = {}
hyds = {}
lg = RDLogger.logger()
lg.setLevel(RDLogger.CRITICAL)
for i in range(0,db.nums()):
for j in range(i+1,db.nums()):
MCS_no_hyds = MCS.getMapping(db[i].getMolecule(), db[j].getMolecule())
MCS_hyds = MCS.getMapping(db[i].getMolecule(), db[j].getMolecule(), hydrogens=True)
if py_ver < 3:
nohyds[(i,j)] = MCS_no_hyds
hyds[(i,j)] = MCS_hyds
else:
nohyds[(i,j)] = list(MCS_no_hyds)
hyds[(i,j)] = list(MCS_hyds)
self.assertEqual(True, nohyds == data_no_hydrogens)
self.assertEqual(True, hyds == data_hydrogens)
if __name__ =='__main__':
unittest.main()