Skip to content

Commit

Permalink
Merge pull request #134 from rigdenlab/changes
Browse files Browse the repository at this point in the history
Fixed select chain function and added in more tests
  • Loading branch information
hlasimpk committed Oct 1, 2020
2 parents c1bcc80 + 11944b1 commit 766686a
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 22 deletions.
4 changes: 2 additions & 2 deletions simbad/parsers/anode_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def __init__(self, logfile):
self.z = None
self.peak_height = None
self.nearest_atom = None
self._parse()
self.parse()

def _parse(self):
def parse(self):
with open(self.fname, "r") as f:
line = f.readline()
while line:
Expand Down
92 changes: 92 additions & 0 deletions simbad/parsers/tests/test_anode_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Test functions for simbad.parsers.anode_parser"""

__author__ = "Adam Simpkin"
__date__ = "17 Aug 2017"

import tempfile
import unittest

from simbad.parsers import anode_parser


class Test(unittest.TestCase):
def test_anode_parser(self):
content = """ANODE - ANOmalous DEnsity analysis - version 2013/1
===================================================
Command line: anode -b10.0 -d1.0 -h80 -m20 -r5.0 -n99.0 -s4.0 4WKI
16 threads running in parallel on 16 CPUs
1909 Atoms read from PDB file 4WKI.pdb
Cell: 40.037 68.193 74.013 90.00 90.00 90.00
Space group: P 21 21 21
16870 Reflections read from file 4WKI_fa.hkl
Highest resolution = 1.600 A
Averaged anomalous densities (sigma)
39.51 ZN_ZN
7.71 CA_CA
3.23 SD_MET
2.53 SG_CYS
1.69 CL2_3PW
1.60 N18_3PW
1.49 C4_3PW
1.29 C9_3PW
1.09 CB_TRP
1.08 OH_TYR
0.91 O25_3PW
0.89 CZ_TYR
0.69 N_TRP
0.68 CA_TRP
0.68 O_ARG
0.61 C11_3PW
0.61 C12_3PW
0.51 C3_3PW
0.50 O_ILE
0.44 CH2_TRP
Strongest unique anomalous peaks
X Y Z Height(sig) SOF Nearest atom
ZN1 -0.14568 0.10187 0.31976 39.59 1.000 0.049 ZN_A:ZN501
ZN2 -0.26378 -0.06058 0.37985 9.30 1.000 0.149 CA_A:CA504
ZN3 0.34895 0.18006 0.48898 7.78 1.000 0.138 CA_A:CA502
ZN4 0.40560 0.16274 0.52466 6.57 1.000 0.279 CA_A:CA503
ZN5 -0.00596 -0.12591 0.45283 5.44 1.000 0.295 SD_A:MET230
ZN6 0.17604 0.01967 0.46853 4.33 1.000 2.242 CG2_A:VAL224
ZN7 0.01378 0.08476 0.39220 4.32 1.000 1.685 C_A:GLU362
ZN8 -0.07702 0.06676 0.45484 4.27 1.000 1.911 N_A:ILE347
ZN9 0.32379 0.28692 0.35855 4.25 1.000 0.770 NH2_A:ARG218
ZN10 0.17778 -0.17610 0.28304 4.19 1.000 3.146 O_A:HOH816
ZN11 -0.03473 -0.08544 0.34792 4.15 1.000 1.888 OD2_A:ASP351
ZN12 -0.19532 -0.08462 0.46678 4.11 1.000 1.144 CB_A:GLN319
ZN13 0.33612 0.27935 0.34665 4.09 1.000 1.165 NH2_A:ARG218
ZN14 0.07757 0.15593 0.28974 4.07 1.000 0.483 SD_A:MET369
ZN15 0.38938 0.20639 0.37188 4.05 1.000 1.223 CA_A:ARG218
15 Peaks output to file 4WKI_fa.res
16870 Reflections written to file 4WKI.pha
Listing file 4WKI.lsa"""

anode_log = tempfile.NamedTemporaryFile("w", delete=False)
anode_log.write(content)
anode_log.close()

ap = anode_parser.AnodeParser(anode_log.name)

self.assertEqual(ap.x, '-0.14568')
self.assertEqual(ap.y, '0.10187')
self.assertEqual(ap.z, '0.31976')
self.assertEqual(ap.peak_height, '39.59')
self.assertEqual(ap.nearest_atom, 'ZN_A:ZN501')


if __name__ == "__main__":
unittest.main()
77 changes: 77 additions & 0 deletions simbad/parsers/tests/test_mtz_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import os
import numpy as np
import unittest

from simbad.parsers.mtz_parser import MtzParser

try:
ROOT_DIR = os.environ['SIMBAD_ROOT']
EXAMPLE_DIR = os.path.join(ROOT_DIR, "test_data")
except KeyError:
from simbad.command_line import CCP4RootDirectory
ROOT_DIR = str(CCP4RootDirectory())
EXAMPLE_DIR = os.path.join(ROOT_DIR, "examples")


class Test(unittest.TestCase):
"""Unit test"""
def test_mtz_parser_1(self):
"""Test case for MtzParser"""
input_mtz = os.path.join(EXAMPLE_DIR, "toxd", "toxd.mtz")
mp = MtzParser(input_mtz)
mp.parse()
self.assertEqual(np.round(mp.resolution, 1), 2.3)
self.assertEqual(np.round(mp.cell.a, 4), 73.5820)
self.assertEqual(np.round(mp.cell.b, 4), 38.7330)
self.assertEqual(np.round(mp.cell.c, 4), 23.1890)
self.assertEqual(np.round(mp.cell.alpha, 4), 90.0000)
self.assertEqual(np.round(mp.cell.beta, 4), 90.0000)
self.assertEqual(np.round(mp.cell.gamma, 4), 90.0000)
self.assertEqual(mp.spacegroup_symbol, "P 21 21 21")
self.assertEqual(mp.nreflections, 3235)
self.assertEqual(mp.f, "FTOXD3")
self.assertEqual(mp.sigf, "SIGFTOXD3")
self.assertEqual(mp.free, "FreeR_flag")

def test_mtz_parser_2(self):
"""Test case for MtzParser"""
input_mtz = os.path.join(EXAMPLE_DIR, "rnase", "rnase25.mtz")
mp = MtzParser(input_mtz)
mp.parse()
self.assertEqual(np.round(mp.resolution, 1), 2.5)
self.assertEqual(np.round(mp.cell.a, 4), 64.8970)
self.assertEqual(np.round(mp.cell.b, 4), 78.3230)
self.assertEqual(np.round(mp.cell.c, 4), 38.7920)
self.assertEqual(np.round(mp.cell.alpha, 4), 90.0000)
self.assertEqual(np.round(mp.cell.beta, 4), 90.0000)
self.assertEqual(np.round(mp.cell.gamma, 4), 90.0000)
self.assertEqual(mp.spacegroup_symbol, "P 21 21 21")
self.assertEqual(mp.nreflections, 7262)
self.assertEqual(mp.f, "FNAT")
self.assertEqual(mp.sigf, "SIGFNAT")
self.assertEqual(mp.free, "FreeR_flag")

def test_mtz_parser_3(self):
"""Test case for MtzParser"""
input_mtz = os.path.join(EXAMPLE_DIR, "rnase", "rnase25F+F-.mtz")
mp = MtzParser(input_mtz)
mp.parse()
self.assertEqual(np.round(mp.resolution, 1), 2.5)
self.assertEqual(np.round(mp.cell.a, 4), 64.8970)
self.assertEqual(np.round(mp.cell.b, 4), 78.3230)
self.assertEqual(np.round(mp.cell.c, 4), 38.7920)
self.assertEqual(np.round(mp.cell.alpha, 4), 90.0000)
self.assertEqual(np.round(mp.cell.beta, 4), 90.0000)
self.assertEqual(np.round(mp.cell.gamma, 4), 90.0000)
self.assertEqual(mp.spacegroup_symbol, "P 21 21 21")
self.assertEqual(mp.nreflections, 7262)
self.assertEqual(mp.f, "FNAT")
self.assertEqual(mp.sigf, "SIGFNAT")
self.assertEqual(mp.f_plus, "FPTNCD25(+)")
self.assertEqual(mp.sigf_plus, "SIGFPTNCD25(+)")
self.assertEqual(mp.f_minus, "FPTNCD25(-)")
self.assertEqual(mp.sigf_minus, "SIGFPTNCD25(-)")
self.assertEqual(mp.free, "FreeR_flag")



4 changes: 2 additions & 2 deletions simbad/util/mtz_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def reindex(hklin, hklout, sg):
cexec(cmd, stdin=stdin)


@deprecate('0.2.2', msg="Use simbad.parsers.mtz_parser.MtzParser instead")
@deprecate('0.2.5', msg="Use simbad.parsers.mtz_parser.MtzParser instead")
class GetLabels(object):
def __init__(self, hklin):
self.f = None
Expand Down Expand Up @@ -154,7 +154,7 @@ def run(self, hklin):
self.free = mp.free


@deprecate('0.2.2', msg="Use simbad.parsers.mtz_parser.MtzParser instead")
@deprecate('0.2.5', msg="Use simbad.parsers.mtz_parser.MtzParser instead")
def crystal_data(hklin):
mp = MtzParser(hklin)
space_group = "".join(mp.spacegroup_symbol.encode("ascii").split())
Expand Down
12 changes: 6 additions & 6 deletions simbad/util/pdb_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def integration_box(self):

@property
def nchains(self):
self.standardize()
return len(self.structure[0])

@property
Expand Down Expand Up @@ -136,16 +137,15 @@ def remove_empty_models(self):
def select_chain_by_idx(self, chain_idx):
self.keep_first_model_only()
model = self.structure[0]
for i, chain in enumerate(model):
if i != chain_idx:
model.remove_chain(chain.name)
del model[chain_idx + 1:]
del model[:chain_idx]

def select_chain_by_id(self, chain_id):
self.keep_first_model_only()
model = self.structure[0]
for chain in model:
if chain.name != chain_id:
model.remove_chain(chain.name)
names = {c.name for c in model if c.name != chain_id}
for name in names:
model.remove_chain(name)

def select_residues(self, delete=None, to_keep=None, delete_idx=None, to_keep_idx=None):
self.keep_first_model_only()
Expand Down
2 changes: 1 addition & 1 deletion simbad/util/simbad_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def top_files(self, num_results=3):
elif self.lattice_mr_results is not None:
return self.get_files(LATTICE_ID, num_results=num_results)
else:
None
return None

def get_files(self, search_type, num_results=10):
"""Return the best num_results results files from search_type
Expand Down
135 changes: 125 additions & 10 deletions simbad/util/tests/test_pdb_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ def internet_on():
class Test(unittest.TestCase):
"""Unit test"""

def test_calculate_integration_box(self):
"""Test case for PdbStructure.integration_box"""

input_model = os.path.join(EXAMPLE_DIR, "toxd", "toxd.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.integration_box
reference_data = (48.306749999999994, 56.73474999999999, 48.589749999999995, 19.84575)

self.assertEqual(data, reference_data)

def test_molecular_weight_1(self):
"""Test case for PdbStructure.molecular_weight"""

Expand Down Expand Up @@ -78,6 +68,131 @@ def test_molecular_weight_3(self):

self.assertAlmostEqual(np.round(data, 0), np.round(reference_data, 0))

def test_calculate_integration_box(self):
"""Test case for PdbStructure.integration_box"""

input_model = os.path.join(EXAMPLE_DIR, "toxd", "toxd.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.integration_box
reference_data = (48.306749999999994, 56.73474999999999, 48.589749999999995, 19.84575)

self.assertEqual(data, reference_data)

def test_nchains_1(self):
"""Test case for PdbStructure.nchains"""
input_model = os.path.join(EXAMPLE_DIR, "toxd", "toxd.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.nchains
reference_data = 1

self.assertEqual(data, reference_data)

def test_nchains_2(self):
"""Test case for PdbStructure.nchains"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.nchains
reference_data = 2

self.assertEqual(data, reference_data)

def test_nres_1(self):
"""Test case for PdbStructure.nres"""
input_model = os.path.join(EXAMPLE_DIR, "toxd", "toxd.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.nres
reference_data = 59

self.assertEqual(data, reference_data)

def test_nres_2(self):
"""Test case for PdbStructure.nres"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
data = pdb_struct.nres
reference_data = 192

self.assertEqual(data, reference_data)

def test_keep_first_chain(self):
"""Test case for PdbStructure.keep_first_chain_only"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
pdb_struct.keep_first_chain_only()
data = pdb_struct.nchains
reference_data = 1
self.assertEqual(data, reference_data)

data = pdb_struct.structure[0][0].name
reference_data = 'A'
self.assertEqual(data, reference_data)

def test_select_chain_by_idx_1(self):
"""Test case for PdbStructure.select_chain_by_idx"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
pdb_struct.select_chain_by_idx(0)
data = pdb_struct.structure[0][0].name
reference_data = 'A'
self.assertEqual(data, reference_data)

data = pdb_struct.nchains
reference_data = 1
self.assertEqual(data, reference_data)

def test_select_chain_by_idx_2(self):
"""Test case for PdbStructure.select_chain_by_idx"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
pdb_struct.select_chain_by_idx(1)
data = pdb_struct.structure[0][0].name
reference_data = 'B'
self.assertEqual(data, reference_data)

data = pdb_struct.nchains
reference_data = 1
self.assertEqual(data, reference_data)

def test_select_chain_by_id_1(self):
"""Test case for PdbStructure.select_chain_by_id"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
pdb_struct.select_chain_by_id('A')
data = pdb_struct.structure[0][0].name
reference_data = 'A'
self.assertEqual(data, reference_data)

data = pdb_struct.nchains
reference_data = 1
self.assertEqual(data, reference_data)

def test_select_chain_by_id_2(self):
"""Test case for PdbStructure.select_chain_by_id"""
input_model = os.path.join(EXAMPLE_DIR, "rnase", "rnase.pdb")
pdb_struct = PdbStructure.from_file(input_model)
pdb_struct.select_chain_by_id('B')
data = pdb_struct.structure[0][0].name
reference_data = 'B'
self.assertEqual(data, reference_data)

data = pdb_struct.nchains
reference_data = 1
self.assertEqual(data, reference_data)

def test_select_residues(self):
"""Test case for PdbStructure.select_residues"""
input_model = os.path.join(EXAMPLE_DIR, "toxd", "toxd.pdb")
pdb_struct = PdbStructure.from_file(input_model)
seqid_range = range(0, 5)
pdb_struct.select_residues(to_keep_idx=seqid_range)
data = pdb_struct.nres
reference_data = 5
self.assertEqual(data, reference_data)

data = [res.seqid.num for res in pdb_struct.structure[0][0]]
reference_data = [1, 2, 3, 4, 5]
self.assertListEqual(data, reference_data)

@unittest.skipIf('THIS_IS_TRAVIS' in os.environ, "not implemented in Travis CI")
def test_standardise_1(self):
"""Test case for PdbStructure.standardise"""
Expand Down
2 changes: 1 addition & 1 deletion simbad/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Maintain sematantic versioning. Further information can
# be found here [http://semver.org/]
__version_info__ = (0, 2, 1)
__version_info__ = (0, 2, 2)

# ======================================================
# Do __NOT__ change anything below here
Expand Down

0 comments on commit 766686a

Please sign in to comment.