Skip to content

Commit

Permalink
Fix a bug introduced in #5ae04d2955fa (#787)
Browse files Browse the repository at this point in the history
It broke the use of

reflection_file_reader.any_reflection_file(hklfn+'=hklf+ins/res')

This feature had no test and it did therefore went unnoticed.
  • Loading branch information
luc-j-bourhis committed Sep 6, 2022
1 parent 667cb53 commit 085f6d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion iotbx/reflection_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def as_miller_arrays(self,
raise Sorry("Can't open files %s.ins or %s.res"
"required by the option hklf+ins/res" % ((name,)*2))
crystal_symmetry = crystal_symmetry_from_ins.extract_from(
file=shelx_file)
file=shelx_file, close_file=False)
shelx_file.seek(0)
remaining = shelx_file.read()
shelx_file.close()
Expand Down
46 changes: 46 additions & 0 deletions iotbx/regression/tst_reflection_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,51 @@ def exercise_get_experimental_phases():
"""
else: raise Exception_expected

def exercise_hklf_plus_ins_or_res():
import textwrap
from os import path
import shutil
from libtbx.test_utils import approx_equal

try:
hklf4 = [' 1 0 0 1.11 0.11',
' 0 1 -1 2.22 0.22',
' -1 0 1 4.44 0.44']
hklf4 = [li + '\n' for li in hklf4]
ins = textwrap.dedent(
"""\
TITL 03srv209 in Pbca
CELL 0.71073 7.35 9.541 12.842 90 90 90
ZERR 4 0.002 0.002 0.003 0 0 0
LATT 1
SYMM 0.5-X,-Y,0.5+Z
SYMM -X,0.5+Y,0.5-Z
SYMM 0.5+X,0.5-Y,-Z
SFAC C H O N
UNIT 32 40 16 8
HKLF 4
""")
folder = 'temp_data_hklf_plus_ins_res'
if not path.exists(folder):
os.mkdir(folder)
insfn = path.join(folder, '03srv209.ins')
hklfn = path.join(folder, '03srv209.hkl')
with open(insfn, 'w') as f:
f.write(ins)
with open(hklfn, 'w') as f:
f.writelines(hklf4)
rf = reflection_file_reader.any_reflection_file(hklfn+'=hklf+ins/res')
ma = rf.as_miller_arrays()[0]
cs = ma.crystal_symmetry()
assert cs.is_identical_symmetry(
crystal.symmetry(unit_cell='7.35 9.541 12.842 90 90 90',
space_group_symbol='Pbca'))
assert all(ma.indices() == flex.miller_index([(1,0,0), (0,1,-1), (-1,0,1)]))
assert approx_equal(ma.data(), [1.11, 2.22, 4.44])
assert approx_equal(ma.sigmas(), [0.11, 0.22, 0.44])
finally:
shutil.rmtree(folder, ignore_errors=True)

def exercise_extract_miller_array_from_file():
from iotbx import reflection_file_utils as rfu
from libtbx.test_utils import approx_equal
Expand Down Expand Up @@ -828,6 +873,7 @@ def exercise():
if (mtz is None):
print("Skipping iotbx/tst_reflection_file_utils.py: ccp4io not available")
return
exercise_hklf_plus_ins_or_res()
exercise_get_amplitudes_and_get_phases_deg()
exercise_get_xtal_data()
exercise_get_r_free_flags()
Expand Down
6 changes: 4 additions & 2 deletions iotbx/shelx/crystal_symmetry_from_ins.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import absolute_import, division, print_function
from iotbx import shelx, builders

def extract_from(file_name=None, file=None, max_characters=100000):
def extract_from(file_name=None, file=None, max_characters=100000,
close_file=True):
# XXX backward compatibility 2009-09-17 - keep max_characters keyword
assert [file_name, file].count(None) == 1
stream = shelx.command_stream(file=file, filename=file_name)
stream = shelx.command_stream(file=file, filename=file_name,
close_when_deleted=close_file)
parser = shelx.crystal_symmetry_parser(
stream, builder=builders.crystal_symmetry_builder())
parser.parse()
Expand Down
8 changes: 5 additions & 3 deletions iotbx/shelx/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ class command_stream(object):
shelx_commands.update(commands_allowing_atom_names)


def __init__(self, file=None, filename=None):
def __init__(self, file=None, filename=None, close_when_deleted=True):
assert [file, filename].count(None) == 1
if file is None: file = open(filename)
self.file = file
self.close_when_deleted = close_when_deleted

def __del__(self):
if hasattr(self,'file') and hasattr(self.file,'close'):
self.file.close()
if self.close_when_deleted:
if hasattr(self,'file') and hasattr(self.file,'close'):
self.file.close()

_cmd_pat = re.compile(r"""
^
Expand Down

0 comments on commit 085f6d8

Please sign in to comment.