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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""Test preprocessing operations."""
import os
from itertools import zip_longest
from pathlib import Path

import pytest

from haddock.gear import preprocessing as pp

from . import broken_pdb, corrected_pdb, residues_top


def test_open_or_give():
    in1 = open(broken_pdb)
    input_ = [
        broken_pdb,  # path inside list
        str(broken_pdb),  # path inside list
        in1,
        broken_pdb.read_text().split(os.linesep),  # list
        tuple(broken_pdb.read_text().split(os.linesep)),  # tuple
        ]

    result = pp._open_or_give(input_)
    assert len(result) == 5
    for r in result:
        assert len(r) == 192
    in1.close()


@pytest.mark.parametrize(
    "value",
    [1, 1.1, {1: None}, None],
    )
def test_open_or_give_wrong(value):
    with pytest.raises(TypeError):
        pp._open_or_give(value)


def test_read_additional_residues():
    result = pp.read_additional_residues(residues_top)
    assert isinstance(result, tuple)
    assert result == ("DA2", "DE3", "DI", "DO", "DU1")


def test_read_additional_residues_lines():
    lines = Path(residues_top).read_text().split(os.linesep)
    result = pp.read_additional_residues.original(lines)
    assert isinstance(result, tuple)
    assert result == ("DA2", "DE3", "DI", "DO", "DU1")


def test_wrep_pdb_tidy():
    pp.wrep_pdb_tidy(pp._open_or_give([broken_pdb])[0], strict=False)


def test_wrep_pdb_tidy_strict():
    pp.wrep_pdb_tidy(pp._open_or_give([broken_pdb])[0], strict=True)


models_okay = """MODEL        1
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        2
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        3
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
"""


models_wrong_2 = """MODEL        1
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        2
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        3
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
"""

models_wrong_3 = """MODEL        1
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        2
ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
MODEL        3
ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C
ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C
TER
ENDMDL
"""


def test_check_models():
    pp.models_should_have_the_same_labels(models_okay.split(os.linesep))


def test_check_models_wrong_2():
    _lines = models_wrong_2.split(os.linesep)
    with pytest.raises(pp.ModelsDifferError) as error:
        pp.models_should_have_the_same_labels(_lines)
    assert str(error.value) == "Labels in MODEL 2 differ from MODEL 1."


def test_check_models_wrong_3():
    _lines = models_wrong_3.split(os.linesep)
    with pytest.raises(pp.ModelsDifferError) as error:
        pp.models_should_have_the_same_labels(_lines)
    assert str(error.value) == "Labels in MODEL 3 differ from MODEL 1."


rep_chains_1 = [
    [
        'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C  ',
        'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
        'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C  ',
        ],
    [
        'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C  ',
        'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
        'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C  ',
        ],
    ]

expected_rep_chains_1 = [
    [
        'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C  ',
        'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
        'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C  ',
        ],
    [
        'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00      A    C  ',
        'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00      A    C  ',
        'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00      A    C  ',
        ],
    ]

rep_chains_no_rep = [
    [
        'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00           C  ',
        'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00           C  ',
        'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00           C  ',
        ],
    [
        'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00           C  ',
        'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
        'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00           C  ',
        ],
    ]


@pytest.mark.parametrize(
    'in_, expected',
    [
        (rep_chains_1, expected_rep_chains_1),
        (rep_chains_no_rep, rep_chains_no_rep),
        ],
    )
def test_correct_equal_chain_segids(in_, expected):
    result = pp.correct_equal_chain_segids(in_)

    # made for loop to facilitate visualization of errors
    for r, e in zip(result, expected):
        assert r == e


multiple_chainIDs_1 = [
    'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00           C  ',
    'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
    'TER',
    'ATOM      3  CA  ALA C   7      35.081  45.036   1.305  1.00  0.00           C  ',
    ]

multiple_chainIDs_2 = [
    'ATOM      3  CA  ARG C   4      37.080  43.455  -3.421  1.00  0.00           C  ',
    'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00           C  ',
    'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00           C  ',
    ]

# mind the added segID
expected_multiple_chainIDs_1 = [
    'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00      A    C  ',
    'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00      A    C  ',
    'TER                  A                                                           ',  # why this extra space?
    'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00      A    C  ',
    ]

expected_multiple_chainIDs_2 = [
    'ATOM      3  CA  ARG C   4      37.080  43.455  -3.421  1.00  0.00      C    C  ',
    'ATOM      3  CA  GLU C   6      33.861  45.127  -2.233  1.00  0.00      C    C  ',
    'ATOM      3  CA  ALA C   7      35.081  45.036   1.305  1.00  0.00      C    C  ',
    ]


@pytest.mark.parametrize(
    "in_,expected",
    [
        (multiple_chainIDs_1, expected_multiple_chainIDs_1),
        (multiple_chainIDs_2, expected_multiple_chainIDs_2),
        # returns the same thing
        (expected_multiple_chainIDs_2, expected_multiple_chainIDs_2),
        ]
    )
def test_homogenize_chainIDs(in_, expected):
    """Test homogenize chain IDs."""
    result = pp.homogenize_chains(in_)

    # assert line by line to facilitate finding errors
    for r_line, e_line in zip(result, expected):
        assert len(r_line) == len(e_line)
    for r_line, e_line in zip(result, expected):
        assert r_line == e_line


nochain_chainIDs_1 = [
    'ATOM      3  CA  ARG     4      37.080  43.455  -3.421  1.00  0.00              ',
    'ATOM      3  CA  GLU     6      33.861  45.127  -2.233  1.00  0.00              ',
    'ATOM      3  CA  ALA     7      35.081  45.036   1.305  1.00  0.00              ',
    ]

nochain_chainIDs_2 = [
    'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00              ',
    'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00              ',
    'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00              ',
    ]

nochain_chainIDs_3 = [
    'ATOM      3  CA  ARG     4      37.080  43.455  -3.421  1.00  0.00      A       ',
    'ATOM      3  CA  GLU     6      33.861  45.127  -2.233  1.00  0.00      A       ',
    'ATOM      3  CA  ALA     7      35.081  45.036   1.305  1.00  0.00      A       ',
    ]

nochain_chainIDs_4 = [
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      A       ',
    'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00      A       ',
    'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00      A       ',
    ]

expected_nochain_chainIDs_1 = [
    'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00      A       ',
    'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00      A       ',
    'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00      A       ',
    ]

expected_nochain_chainIDs_4 = [
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      B       ',
    'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00      B       ',
    'ATOM      3  CA  ALA B   7      35.081  45.036   1.305  1.00  0.00      B       ',
    ]

chain_and_seg_IDs = [
    'ATOM      3  CA  ARG A   4      37.080  43.455  -3.421  1.00  0.00      A       ',
    'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00      A       ',
    'ATOM      3  CA  ALA A   7      35.081  45.036   1.305  1.00  0.00      A       ',
    ]


@pytest.mark.parametrize(
    'in_,expected',
    [
        (nochain_chainIDs_1, expected_nochain_chainIDs_1),
        (nochain_chainIDs_2, expected_nochain_chainIDs_1),
        (nochain_chainIDs_3, expected_nochain_chainIDs_1),
        (nochain_chainIDs_4, expected_nochain_chainIDs_4),
        (chain_and_seg_IDs, chain_and_seg_IDs),
        ]
    )
def test_solve_nochainID(in_, expected):
    """Test solve nochainID function."""
    result = pp.solve_no_chainID_no_segID(in_)
    for r, e in zip(result, expected):
        assert r == e


# test cases to test adding the ion charges
# even indexes are the input, odd indexes are the expected results
ion_cases = [
    # 0
    'HETATM 3833 ZN+2 ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN  ',
    'HETATM 3833 ZN+2 ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN+2',

    # 1
    'HETATM 3833 ZN   ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN  ',
    'HETATM 3833 ZN+2 ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN+2',

    # 2
    'HETATM 3833 ZN+2 ZN  A  42      21.391  -8.794  33.944  1.00 24.37          ZN  ',
    'HETATM 3833 ZN+2 ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN+2',

    # 3
    'HETATM 3833 ZN   ZN  A  42      21.391  -8.794  33.944  1.00 24.37          ZN  ',
    'HETATM 3833 ZN    ZN A  42      21.391  -8.794  33.944  1.00 24.37          ZN  ',

    # 4
    'HETATM 3833 ZN    ZN A  42      21.391  -8.794  33.944  1.00 24.37          ZN+2',
    'HETATM 3833 ZN+2 ZN2 A  42      21.391  -8.794  33.944  1.00 24.37          ZN+2',

    # 5
    'HETATM 3834  K    K1 A  42      21.391  -8.794  33.944  1.00 24.37              ',
    'HETATM 3834  K+1  K1 A  42      21.391  -8.794  33.944  1.00 24.37           K+1',

    # 6
    'HETATM 3835 NI    NI A  42      21.391  -8.794  33.944  1.00 24.37              ',
    'HETATM 3835 NI    NI A  42      21.391  -8.794  33.944  1.00 24.37          NI  ',

    # 7
    'HETATM 3834  F-1   F A  42      21.391  -8.794  33.944  1.00 24.37              ',
    'HETATM 3834  F-1  F1 A  42      21.391  -8.794  33.944  1.00 24.37           F-1',

    # 8
    'HETATM    3 CA    CA B   4      37.080  43.455  -3.421  1.00  0.00      B   CA+2',
    'HETATM    3 CA+2 CA2 B   4      37.080  43.455  -3.421  1.00  0.00      B   CA+2',

    # 9 lines not concerning ions - note element is not defined
    # carbon alpha conflicting with calcium
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      B       ',
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      B       ',

    # 10 other lines not concerning ions - atom not CA (carbon alpha)
    'ATOM      1  N   ALA A   1       0.000   0.000   0.000  0.00  0.00           N  ',
    'ATOM      1  N   ALA A   1       0.000   0.000   0.000  0.00  0.00           N  ',

    # 11 lines not concerning ions - here the element is defined
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      B    C  ',
    'ATOM      3  CA  ARG B   4      37.080  43.455  -3.421  1.00  0.00      B    C  ',

    # 12 exception
    'HETATM 3834  F    FA A  42      21.391  -8.794  33.944  1.00 24.37              ',
    'HETATM 3834  F     F A  42      21.391  -8.794  33.944  1.00 24.37           F  ',
    ]


# made this way to make each line a separate tests
@pytest.fixture(params=list(zip(ion_cases[::2], ion_cases[1::2])))
def ion_cases_fixture(request):
    """Give ion test cases."""
    return request.param[0], request.param[1]


def test_correct_ion_charges(ion_cases_fixture):
    input_, expected = ion_cases_fixture

    # 1. puts input_ inside a list because the function only accepts lists
    # 2. the function is wrapped around list() because it is a generator
    result = list(pp.add_charges_to_ions([input_]))

    assert len(result) == 1
    # compares two strings
    assert result[0] == expected


def test_process_ion_case_atom():
    """Test processing an exception case."""
    inp = 'HETATM 3834  F    FA A  42      21.391  -8.794  33.944  1.00 24.37              '  # noqa: E501
    exp = 'HETATM 3834  F     F A  42      21.391  -8.794  33.944  1.00 24.37           F  '  # noqa: E501
    result = pp._process_ion_case_atom(inp)
    assert result == exp


@pytest.mark.parametrize(
    "lines, expected",
    [
        (
            'ATOM      3  CA  GLU A   6      33.861  45.127  -2.233  1.00  0.00      A       ',
            'ATOM      3  CA  GLU B   6      33.861  45.127  -2.233  1.00  0.00      A       ',
            ),
        ]
    )
def test_wrep_chain(lines, expected):
    """Test if dry report works."""
    result = pp.wrep_pdb_chain([lines], "B", report=True)
    assert result[0] == expected


def test_process_pdbs():
    """."""
    result = pp.process_pdbs(broken_pdb)
    assert len(result) == 1

    expected = corrected_pdb.read_text().rstrip(os.linesep).split(os.linesep)

    for i, (rline, eline) in enumerate(zip_longest(result[0], expected)):
        assert rline == eline, i