-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore_tests_chain_collection.py
364 lines (303 loc) · 24.2 KB
/
core_tests_chain_collection.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import unittest
from abpytools import ChainCollection, Chain
import operator
import os
from glob import glob
from . import read_sequence, check_connection, ABNUM_URL, IGBLAST_URL
class ChainCollectionCore(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.antibody_collection_1_name = 'test'
cls.chain_test_sequence = read_sequence('./tests/Data/chain_collection_fasta_test.fasta')
def test_ChainCollection_length_0(self):
antibody_collection = ChainCollection()
self.assertEqual(len(antibody_collection), 0)
def test_ChainCollection_input_exception_1(self):
# when ChainCollection.object_list is instantiated with
# something other than a list it throws an error
self.assertRaises(ValueError, ChainCollection, 0)
def test_ChainCollection_input_exception_2(self):
# when ChainCollection.object_list is instantiated with
# a list with non Chain objects it throws an error
self.assertRaises(ValueError, ChainCollection, [Chain(sequence=""), 0])
def test_ChainCollection_input_exception_3(self):
# when ChainCollection is instantiated with static method
# .load_from_fasta with an invalid file path it throws an error
self.assertRaises(ValueError, ChainCollection.load_from_fasta, './tests/Data/NonExistentFile.fasta')
def test_ChainCollection_input_exception_4(self):
# when ChainCollection is instantiated with .load_from_file with
# path to a file that does not have a .fasta or .json extension
# it throws an error
self.assertRaises(ValueError, ChainCollection.load_from_file, './tests/Data/__init__.py')
def test_ChainCollection_input_1(self):
# instantiate ChainCollection with a Chain (empty) object
# this doesn't make any sense and raises an error
self.assertRaises(ValueError, ChainCollection, [Chain(sequence="")])
def test_ChainCollection_input_2(self):
# instantiate ChainCollection with a loaded Chain object
test_chain = Chain(sequence=self.chain_test_sequence)
test_collection = ChainCollection(antibody_objects=[test_chain])
self.assertIsInstance(test_collection, ChainCollection)
def test_ChainCollection_load_fasta_exception(self):
# throws error when reading a file with fasta extention,
# but with the wrong format
self.assertRaises(ValueError, ChainCollection.load_from_file, './tests/Data/NotAFASTAFile.fasta')
def test_ChainCollection_chain(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json')
self.assertEqual(antibody_collection_1.chain, 'heavy')
@unittest.skipUnless(check_connection(URL=IGBLAST_URL), 'No internet connection, skipping test.')
def test_ChainCollection_chain_2(self):
# checks if the chain type is read properly from a Chain object
test_chain = Chain(sequence=self.chain_test_sequence)
test_chain.load()
test_collection = ChainCollection(antibody_objects=[test_chain])
self.assertEqual(test_collection.chain, 'heavy')
def test_ChainCollection_proto_io_1(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.save(file_format='pb2', path='./tests/chain_collection_1_heavy')
self.assertTrue(os.path.isfile('./tests/chain_collection_1_heavy.pb2'))
def test_ChainCollection_proto_io_2(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/chain_collection_1_heavy.pb2',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.names[0], 'test')
def test_ChainCollection_n_ab(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.n_ab, 1)
def test_ChainCollection_numbering_scheme(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_scheme, 'chothia')
def test_ChainCollection_numbering_scheme_kabat(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.set_numbering_scheme('kabat', realign=False)
self.assertEqual(antibody_collection_1.numbering_scheme, 'kabat')
def test_ChainCollection_Hmatrix_shape(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
# if this fails it means that abysis has been updated
self.assertEqual(antibody_collection_1.hydrophobicity_matrix().shape, (1, 158))
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_ChainCollection_Hmatrix_calculation(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='chothia')
self.assertEqual(antibody_collection_1.hydrophobicity_matrix().shape, (1, 158))
def test_ChainCollection_sequence_length(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(len(antibody_collection_1.sequences), 1)
def test_ChainCollection_obj_length(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(len(antibody_collection_1), 1)
def test_ChainCollection_slicing_1_obj(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
# if returning a single chain abpytools automatically creates a new Chain object
self.assertIsInstance(antibody_collection_1[0], Chain)
def test_ChainCollection_slicing_2_obj(self):
antibody_collection_1 = ChainCollection.load_from_file(
path='./tests/Data/chain_collection_heavy_2_sequences.json', show_progressbar=False, verbose=False)
# slicing multiple sequences returns a ChainCollection object
self.assertIsInstance(antibody_collection_1[[0, 1]], ChainCollection)
def test_ChainCollection_cdr_regions_part1(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertCountEqual(antibody_collection_1.ab_region_index().keys(),
[self.antibody_collection_1_name])
def test_ChainCollection_cdr_regions_part2(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertCountEqual(antibody_collection_1.ab_region_index()[self.antibody_collection_1_name],
['CDR', 'FR'])
def test_ChainCollection_cdr_regions_part3_cdr(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertCountEqual(antibody_collection_1.ab_region_index()[self.antibody_collection_1_name]['CDR'],
['CDR1', 'CDR2', 'CDR3'])
def test_ChainCollection_cdr_regions_part3_fr(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertCountEqual(antibody_collection_1.ab_region_index()[self.antibody_collection_1_name]['FR'],
['FR1', 'FR2', 'FR3', 'FR4'])
def test_ChainCollection_total_charge(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.total_charge[self.antibody_collection_1_name], 1.3278508)
def test_ChainCollection_igblast_parser_germline(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.igblast_local_query('tests/Data/chain_collection_1_igblast.html')
self.assertEqual(antibody_collection_1.germline[self.antibody_collection_1_name][0], 'IGHV4-34*01')
def test_ChainCollection_igblast_parser_germline_score(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.igblast_local_query('tests/Data/chain_collection_1_igblast.html')
self.assertAlmostEqual(antibody_collection_1.germline[self.antibody_collection_1_name][1], 9.11e-69,
delta=10e-9)
@unittest.skipUnless(check_connection(URL=IGBLAST_URL), 'No internet connection, skipping test.')
def test_ChainCollection_igblast_server_query_germline(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.igblast_server_query(show_progressbar=False)
self.assertEqual(antibody_collection_1.germline[self.antibody_collection_1_name][0], 'IGHV4-34*01')
@unittest.skipUnless(check_connection(URL=IGBLAST_URL), 'No internet connection, skipping test.')
def test_ChainCollection_igblast_server_query_score(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.igblast_server_query(show_progressbar=False)
self.assertAlmostEqual(antibody_collection_1.germline[self.antibody_collection_1_name][1], 9.11e-69,
delta=10e-9)
@unittest.skipUnless(check_connection(URL=IGBLAST_URL), 'No internet connection, skipping test.')
def test_ChainCollection_igblast_server_query_identity(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.igblast_server_query(show_progressbar=False)
self.assertEqual(antibody_collection_1.germline_identity[self.antibody_collection_1_name]['Total'], 96.9)
def test_ChainCollection_slicing(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertIsInstance(antibody_collection_1.get_object('test'), Chain)
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_Chain_abysis_parser(self):
antibody = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='chothia', verbose=False, show_progressbar=False)
self.assertEqual(antibody.chain, 'heavy')
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_Chain_abysis_parser_chothia(self):
antibody = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='chothia', verbose=False, show_progressbar=False)
self.assertEqual(antibody.numbering_table(as_array=True)[0][-1], '-')
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_Chain_abysis_parser_kabat(self):
antibody = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='kabat', verbose=False, show_progressbar=False)
self.assertEqual(antibody.numbering_table(as_array=True)[0][-1], '-')
def test_ChainCollection_numbering_tableDataFrame(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_table(as_array=False)['CDR1']['H32'].values[0], 'Y')
def test_ChainCollection_numbering_table_shape_np(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_table(as_array=True).shape, (1, 158))
def test_ChainCollection_numbering_table_shape_pd(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_table(as_array=False).shape, (1, 158))
def test_ChainCollection_numbering_table_region_pd(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(
antibody_collection_1.numbering_table(region='CDR1').loc[self.antibody_collection_1_name].values[-1], 'Y')
def test_ChainCollection_numbering_table_region_np(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_table(as_array=True, region='CDR1')[0][-1], 'Y')
def test_ChainCollection_numbering_table_fr_region(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.numbering_table(region='FR1').loc['test'].values[0], 'Q')
def test_ChainCollection_molecular_weight(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.molecular_weights(monoisotopic=False)[0], 20029.85217699999)
def test_ChainCollection_molecular_weight_monoisotopic(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.molecular_weights(monoisotopic=True)[0], 20042.1121)
def test_ChainCollection_ec(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.extinction_coefficients(reduced=False)[0], 52410.0)
def test_ChainCollection_ec_reduced(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.extinction_coefficients(reduced=True)[0], 52160.0)
def test_ChainCollection_charge(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertAlmostEqual(antibody_collection_1.charge.sum(), 1.7497642167513607)
def test_ChainCollection_get_object_exception(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertRaises(ValueError, antibody_collection_1.get_object, 'foo')
def test_ChainCollection_get_object_1(self):
# check if get_object returns a Chain object
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertIsInstance(antibody_collection_1.get_object('test'), Chain)
def test_ChainCollection_get_object_2(self):
# check if get_object returns a Chain object and keeps the information (i.e. name)
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.get_object('test').name, 'test')
def test_ChainCollection_add(self):
# check if adding two ChainCollection objects with one sequence each
# results in a ChainCollection object with two sequences
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_2 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_2_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_3 = antibody_collection_1 + antibody_collection_2
self.assertEqual(antibody_collection_3.n_ab, 2)
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_ChainCollection_add_exception_1(self):
# check if adding two ChainCollection objects with one sequence each
# results in a ChainCollection object with two sequences
antibody_chothia = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='chothia',
show_progressbar=False, verbose=False)
antibody_kabat = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='kabat',
show_progressbar=False, verbose=False)
self.assertRaises(ValueError, operator.add, antibody_chothia, antibody_kabat)
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_ChainCollection_add_exception_2(self):
antibody_chothia = ChainCollection.load_from_file(path='./tests/Data/chain_collection_fasta_test.fasta',
numbering_scheme='chothia', show_progressbar=False,
verbose=False)
antibody_kabat = Chain(sequence=read_sequence('./tests/Data/chain_collection_fasta_test.fasta'),
numbering_scheme='kabat')
antibody_kabat.load()
self.assertRaises(ValueError, operator.add, antibody_chothia, antibody_kabat)
def test_ChainCollection_add_exception_3(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
self.assertRaises(ValueError, operator.add, antibody_collection_1, 0)
@unittest.skipUnless(check_connection(URL=ABNUM_URL), 'No internet connection, skipping test.')
def test_ChainCollection_fasta(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.save(file_format='fasta', path='./tests/SaveTest')
antibody_collection_2 = ChainCollection.load_from_file(path='./tests/SaveTest.fasta',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.sequences[0], antibody_collection_2.sequences[0])
def test_ChainCollection_json(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.save(file_format='json', path='./tests/SaveTest')
antibody_collection_2 = ChainCollection.load_from_file(path='./tests/SaveTest.json',
show_progressbar=False, verbose=False)
self.assertEqual(antibody_collection_1.sequences[0], antibody_collection_2.sequences[0])
def test_ChainCollection_append_1(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_2 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_2_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.append(antibody_collection_2)
self.assertEqual(antibody_collection_1.n_ab, 2)
def test_ChainCollection_append_2(self):
antibody_collection_1 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_1_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_2 = ChainCollection.load_from_file(path='./tests/Data/chain_collection_2_heavy.json',
show_progressbar=False, verbose=False)
antibody_collection_1.append(antibody_collection_2)
self.assertEqual(antibody_collection_1.hydrophobicity_matrix().shape, (2, 158))
@classmethod
def tearDownClass(cls):
for name in glob('./tests/*'):
if name.split('.')[-1] != 'py' and os.path.isfile(name):
os.remove(name)