def __init__(self, file_path=None, file_object=None, input_string=None, cif_object=None, builder=None, raise_if_errors=True, strict=True): assert [file_path, file_object, input_string].count(None) == 2 self.file_path = file_path if builder is None: builder = builders.cif_model_builder(cif_object) else: assert cif_object is None self.builder = builder if file_path is not None: file_object = smart_open.for_reading(file_path) else: file_path = "memory" if file_object is not None: input_string = file_object.read() # check input_string for binary, and abort if necessary binary_detector = detect_binary_file() binary_detector.monitor_initial = min(len(input_string), binary_detector.monitor_initial) if binary_detector.is_binary_file(block=input_string): raise CifParserError("Binary file detected, aborting parsing.") self.parser = ext.fast_reader(builder, input_string, file_path, strict) if raise_if_errors and len(self.parser.lexer_errors()): raise CifParserError(self.parser.lexer_errors()[0]) if raise_if_errors and len(self.parser.parser_errors()): raise CifParserError(self.parser.parser_errors()[0])
def extract_from(file_name=None, file=None, monitor_initial=None): assert [file_name, file].count(None) == 1 if (file is None): file = open(file_name) lines = file.readlines() file.close() detect_binary = detect_binary_file(monitor_initial=monitor_initial) unit_cell = None space_group_symbol = None for line in lines: if (detect_binary is not None): is_binary = detect_binary.is_binary_file(block=line) if (is_binary is not None): if (is_binary): break detect_binary = None flds = line.split("!", 1)[0].split() if (len(flds) > 0): keyword = flds[0].upper() if (keyword == "CELL"): assert len(flds) > 1 unit_cell = " ".join(flds[1:]) if (space_group_symbol is not None): break elif (keyword == "SYMFILE"): assert len(flds) > 1 space_group_symbol = flds[1].replace("\\", "/").split("/")[-1] if (space_group_symbol.lower().endswith(".sym")): space_group_symbol = space_group_symbol[:-4] if (unit_cell is not None): break assert [unit_cell, space_group_symbol].count(None) < 2 return crystal.symmetry(unit_cell=unit_cell, space_group_symbol=space_group_symbol)
def run(args): if (len(args) != 2): raise Usage("""\ iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file ********************************************* NOTE: the cns_input_file is changed in place. *********************************************""") # for file_name in args: if (not os.path.exists(file_name)): raise Sorry("No such file: %s" % show_string(file_name)) source, target = args crystal_symmetry = crystal_symmetry_from_any.extract_from(source) if (crystal_symmetry is None): raise Sorry( "Unknown file format or unit cell and/or space group" " missing from file: " + show_string(source)) cns_space_group_symbol = cns.space_group_symbols.cns_format( space_group_info=crystal_symmetry.space_group_info()) if (cns_space_group_symbol is None): raise Sorry("Space group not available in CNS: %s" % show_string(str(crystal_symmetry.space_group_info()))) sg = '"%s"' % cns_space_group_symbol a,b,c,alpha,beta,gamma = ["%.6g" % p for p in crystal_symmetry.unit_cell().parameters()] parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"] parameters_found = dict(zip(parameter_names, [0]*len(parameter_names))) parameters_changed = [] lines_out = [] detect_binary = detect_binary_file(monitor_initial=100) try: cns_inp = open(target).read().splitlines() except IOError, e: raise Sorry("Error reading file %s (%s)" % (show_string(target), str(e)))
def __init__(self, file_path=None, file_object=None, input_string=None, cif_object=None, builder=None, raise_if_errors=True, strict=True): assert [file_path, file_object, input_string].count(None) == 2 self.file_path = file_path if builder is None: builder = builders.cif_model_builder(cif_object) else: assert cif_object is None self.builder = builder if file_path is not None: file_object = smart_open.for_reading(file_path) else: file_path = "memory" if file_object is not None: input_string = file_object.read() # check input_string for binary, and abort if necessary binary_detector = detect_binary_file() binary_detector.monitor_initial = min( len(input_string), binary_detector.monitor_initial) if binary_detector.is_binary_file(block=input_string): raise CifParserError("Binary file detected, aborting parsing.") self.parser = ext.fast_reader(builder, input_string, file_path, strict) if raise_if_errors and len(self.parser.lexer_errors()): raise CifParserError(self.parser.lexer_errors()[0]) if raise_if_errors and len(self.parser.parser_errors()): raise CifParserError(self.parser.parser_errors()[0])
def extract_from(file_name=None, file=None, monitor_initial=None): assert [file_name, file].count(None) == 1 if (file is None): file = open(file_name) detect_binary = detect_binary_file(monitor_initial=monitor_initial) unit_cell = None space_group_symbol = None for line in file: if (detect_binary is not None): is_binary = detect_binary.is_binary_file(block=line) if (is_binary is not None): if (is_binary): break detect_binary = None flds = line.split("!",1)[0].split() if (len(flds) > 0): keyword = flds[0].upper() if (keyword == "CELL"): assert len(flds) > 1 unit_cell = " ".join(flds[1:]) if (space_group_symbol is not None): break elif (keyword == "SYMFILE"): assert len(flds) > 1 space_group_symbol = flds[1].replace("\\","/").split("/")[-1] if (space_group_symbol.lower().endswith(".sym")): space_group_symbol = space_group_symbol[:-4] if (unit_cell is not None): break assert [unit_cell, space_group_symbol].count(None) < 2 return crystal.symmetry( unit_cell=unit_cell, space_group_symbol=space_group_symbol)
def run(args): if (len(args) != 2): raise Usage("""\ iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file ********************************************* NOTE: the cns_input_file is changed in place. *********************************************""") # for file_name in args: if (not os.path.exists(file_name)): raise Sorry("No such file: %s" % show_string(file_name)) source, target = args crystal_symmetry = crystal_symmetry_from_any.extract_from(source) if (crystal_symmetry is None): raise Sorry("Unknown file format or unit cell and/or space group" " missing from file: " + show_string(source)) cns_space_group_symbol = cns.space_group_symbols.cns_format( space_group_info=crystal_symmetry.space_group_info()) if (cns_space_group_symbol is None): raise Sorry("Space group not available in CNS: %s" % show_string(str(crystal_symmetry.space_group_info()))) sg = '"%s"' % cns_space_group_symbol a, b, c, alpha, beta, gamma = [ "%.6g" % p for p in crystal_symmetry.unit_cell().parameters() ] parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"] parameters_found = dict(zip(parameter_names, [0] * len(parameter_names))) parameters_changed = [] lines_out = [] detect_binary = detect_binary_file(monitor_initial=100) try: cns_inp = open(target).read().splitlines() except IOError, e: raise Sorry("Error reading file %s (%s)" % (show_string(target), str(e)))
def extract_from(file_name=None, file=None, monitor_initial=None): assert [file_name, file].count(None) == 1 if (file is None): file = smart_open.for_reading(file_name=file_name) detect_binary = detect_binary_file(monitor_initial=monitor_initial) line_number = 0 for line in file: line_number += 1 if (detect_binary is not None): is_binary = detect_binary.is_binary_file(block=line) if (is_binary is not None): if (is_binary): break detect_binary = None if (line.startswith("CRYST1")): return cryst1_interpretation.crystal_symmetry(cryst1_record=line) crystal_symmetry = cns_pdb_remarks.extract_symmetry(pdb_record=line) if (crystal_symmetry is not None): return crystal_symmetry raise RuntimeError("No CRYST1 record.")
def run(args): if (len(args) != 2): raise Usage("""\ iotbx.cns.transfer_crystal_symmetry any_symmetry_source_file cns_input_file ********************************************* NOTE: the cns_input_file is changed in place. *********************************************""") # for file_name in args: if (not os.path.exists(file_name)): raise Sorry("No such file: %s" % show_string(file_name)) source, target = args crystal_symmetry = crystal_symmetry_from_any.extract_from(source) if (crystal_symmetry is None): raise Sorry("Unknown file format or unit cell and/or space group" " missing from file: " + show_string(source)) cns_space_group_symbol = cns.space_group_symbols.cns_format( space_group_info=crystal_symmetry.space_group_info()) if (cns_space_group_symbol is None): raise Sorry("Space group not available in CNS: %s" % show_string(str(crystal_symmetry.space_group_info()))) sg = '"%s"' % cns_space_group_symbol a, b, c, alpha, beta, gamma = [ "%.6g" % p for p in crystal_symmetry.unit_cell().parameters() ] parameter_names = ["sg", "a", "b", "c", "alpha", "beta", "gamma"] parameters_found = dict(zip(parameter_names, [0] * len(parameter_names))) parameters_changed = [] lines_out = [] detect_binary = detect_binary_file(monitor_initial=100) try: cns_inp = open(target).read().splitlines() except IOError as e: raise Sorry("Error reading file %s (%s)" % (show_string(target), str(e))) end_block_parameter_definition = False for line in cns_inp: if (detect_binary is not None): is_binary = detect_binary.is_binary_file(block=line) if (is_binary is not None): if (is_binary): raise Sorry("%s appears to be a binary file." % show_string(target)) detect_binary = None if (end_block_parameter_definition): lines_out.append(line) else: l = line.strip().replace(" ", "") if (l == "){-endblockparameterdefinition-}"): lines_out.append(line) end_block_parameter_definition = True else: line_out = line for p in parameter_names: if (l.startswith("{===>}%s=" % p) and l.endswith(";")): parameters_found[p] += 1 line_out = '{===>} %s=%s;' % (p, vars()[p]) if (line_out != line): parameters_changed.append(p) break lines_out.append(line_out) if (list(parameters_found.values()).count(1) != 7): raise Sorry("Unexpected set of variable names in %s:\n counts: %s" % (show_string(target), str(parameters_found))) elif (len(parameters_changed) == 0): print("Info: no changes, %s was not modified." % show_string(target)) else: string_out = "\n".join(lines_out) print("Info: %d change%s" % plural_s(len(parameters_changed)), \ "(%s)," % ", ".join(parameters_changed), \ "writing modified file %s." % show_string(target)) try: print(string_out, file=open(target, "w")) except IOError as e: raise Sorry("Error writing file %s (%s)" % (show_string(target), str(e)))