def __init__(self, cif_block): crystal_symmetry_builder.__init__(self, cif_block) self.hierarchy = hierarchy.root() # These items are mandatory for the _atom_site loop, all others are optional type_symbol = self._wrap_loop_if_needed(cif_block, "_atom_site.type_symbol") atom_labels = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_atom_id") if atom_labels is None: atom_labels = self._wrap_loop_if_needed( cif_block, "_atom_site.label_atom_id" ) # corresponds to chem comp atom name alt_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_alt_id") # alternate conformer id label_asym_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_asym_id") # chain id auth_asym_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_asym_id") if label_asym_id is None: label_asym_id = auth_asym_id if auth_asym_id is None: auth_asym_id = label_asym_id comp_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_comp_id") if comp_id is None: comp_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_comp_id") # residue name entity_id = self._wrap_loop_if_needed(cif_block, "_atom_site.label_entity_id") seq_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_seq_id") if seq_id is None: seq_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_seq_id") # residue number assert [atom_labels, alt_id, auth_asym_id, comp_id, entity_id, seq_id].count(None) == 0, "someting is not present" assert type_symbol is not None atom_site_fp = cif_block.get('_atom_site.phenix_scat_dispersion_real') atom_site_fdp = cif_block.get('_atom_site.phenix_scat_dispersion_imag') pdb_ins_code = cif_block.get( "_atom_site.pdbx_PDB_ins_code") # insertion code model_ids = cif_block.get("_atom_site.pdbx_PDB_model_num") atom_site_id = cif_block.get("_atom_site.id") # only permitted values are ATOM or HETATM group_PDB = cif_block.get("_atom_site.group_PDB") # TODO: read esds B_iso_or_equiv = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.B_iso_or_equiv")) cart_x = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_x")) cart_y = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_y")) cart_z = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_z")) occu = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.occupancy")) formal_charge = self._wrap_loop_if_needed( cif_block, "_atom_site.pdbx_formal_charge") # anisotropic b-factors # TODO: read esds anisotrop_id = self._wrap_loop_if_needed(cif_block, "_atom_site_anisotrop.id") adps = None if anisotrop_id is not None: u_ij = [ self._wrap_loop_if_needed( cif_block, "_atom_site_anisotrop.U[%s][%s]" % (ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23") ] assert u_ij.count(None) in (0, 6) if u_ij.count(None) == 0: adps = u_ij else: assert u_ij.count(None) == 6 b_ij = [ self._wrap_loop_if_needed( cif_block, "_atom_site_anisotrop.B[%s][%s]" % (ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23") ] assert b_ij.count(None) in (0, 6) if b_ij.count(None) == 0: adps = adptbx.b_as_u(b_ij) assert not (u_ij.count(None) and b_ij.count(None) ) # illegal for both to be present if adps is not None: try: adps = [flex.double(adp) for adp in adps] except ValueError, e: raise CifBuilderError("Error interpreting ADPs: " + str(e)) adps = flex.sym_mat3_double(*adps)
def __init__(self, cif_block): crystal_symmetry_builder.__init__(self, cif_block) self.hierarchy = hierarchy.root() # These items are mandatory for the _atom_site loop, all others are optional type_symbol = self._wrap_loop_if_needed(cif_block, "_atom_site.type_symbol") atom_labels = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_atom_id") if atom_labels is None: atom_labels = self._wrap_loop_if_needed( cif_block, "_atom_site.label_atom_id" ) # corresponds to chem comp atom name alt_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_alt_id") # alternate conformer id label_asym_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_asym_id") # chain id auth_asym_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_asym_id") if label_asym_id is None: label_asym_id = auth_asym_id if auth_asym_id is None: auth_asym_id = label_asym_id comp_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_comp_id") if comp_id is None: comp_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_comp_id") # residue name entity_id = self._wrap_loop_if_needed(cif_block, "_atom_site.label_entity_id") seq_id = self._wrap_loop_if_needed(cif_block, "_atom_site.auth_seq_id") if seq_id is None: seq_id = self._wrap_loop_if_needed( cif_block, "_atom_site.label_seq_id") # residue number assert [atom_labels, alt_id, auth_asym_id, comp_id, entity_id, seq_id].count(None) == 0, "someting is not present" assert type_symbol is not None atom_site_fp = cif_block.get('_atom_site.phenix_scat_dispersion_real') atom_site_fdp = cif_block.get('_atom_site.phenix_scat_dispersion_imag') pdb_ins_code = cif_block.get( "_atom_site.pdbx_PDB_ins_code") # insertion code model_ids = cif_block.get("_atom_site.pdbx_PDB_model_num") atom_site_id = cif_block.get("_atom_site.id") # only permitted values are ATOM or HETATM group_PDB = cif_block.get("_atom_site.group_PDB") # TODO: read esds B_iso_or_equiv = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.B_iso_or_equiv")) cart_x = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_x")) cart_y = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_y")) cart_z = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.Cartn_z")) occu = flex.double( self._wrap_loop_if_needed(cif_block, "_atom_site.occupancy")) formal_charge = self._wrap_loop_if_needed( cif_block, "_atom_site.pdbx_formal_charge") # anisotropic b-factors # TODO: read esds anisotrop_id = self._wrap_loop_if_needed(cif_block, "_atom_site_anisotrop.id") adps = None if anisotrop_id is not None: u_ij = [ self._wrap_loop_if_needed( cif_block, "_atom_site_anisotrop.U[%s][%s]" % (ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23") ] assert u_ij.count(None) in (0, 6) if u_ij.count(None) == 0: adps = u_ij else: assert u_ij.count(None) == 6 b_ij = [ self._wrap_loop_if_needed( cif_block, "_atom_site_anisotrop.B[%s][%s]" % (ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23") ] assert b_ij.count(None) in (0, 6) if b_ij.count(None) == 0: adps = adptbx.b_as_u(b_ij) assert not (u_ij.count(None) and b_ij.count(None) ) # illegal for both to be present if adps is not None: try: adps = [flex.double(adp) for adp in adps] except ValueError as e: raise CifBuilderError("Error interpreting ADPs: " + str(e)) adps = flex.sym_mat3_double(*adps) py_adps = {} if anisotrop_id is not None and adps is not None: for an_id, adp in zip(list(anisotrop_id), list(adps)): py_adps[an_id] = adp current_model_id = None current_label_asym_id = None current_auth_asym_id = None current_residue_id = None current_ins_code = None for i_atom in range(atom_labels.size()): # model(s) last_model_id = current_model_id current_model_id = model_ids[i_atom] assert current_model_id is not None if current_model_id != last_model_id: model = hierarchy.model(id=current_model_id) self.hierarchy.append_model(model) # chain(s) last_label_asym_id = current_label_asym_id current_label_asym_id = label_asym_id[i_atom] assert current_label_asym_id is not None last_auth_asym_id = current_auth_asym_id current_auth_asym_id = auth_asym_id[i_atom] assert current_auth_asym_id not in [".", "?", " "], "mmCIF file contains " + \ "record with empty auth_asym_id, which is wrong." assert current_label_asym_id is not None if (current_auth_asym_id != last_auth_asym_id or current_model_id != last_model_id): chain = hierarchy.chain(id=current_auth_asym_id) model.append_chain(chain) else: assert current_auth_asym_id == last_auth_asym_id # residue_group(s) # defined by residue id and insertion code last_residue_id = current_residue_id current_residue_id = seq_id[i_atom] assert current_residue_id is not None last_ins_code = current_ins_code if pdb_ins_code is not None: current_ins_code = pdb_ins_code[i_atom] if current_ins_code in ("?", ".", None): current_ins_code = " " if (current_residue_id != last_residue_id or current_ins_code != last_ins_code or current_auth_asym_id != last_auth_asym_id or current_model_id != last_model_id): try: resseq = hy36encode(width=4, value=int(current_residue_id)) except ValueError as e: resseq = current_residue_id assert len(resseq) == 4 residue_group = hierarchy.residue_group(resseq=resseq, icode=current_ins_code) chain.append_residue_group(residue_group) atom_groups = OrderedDict() # reset atom_groups cache # atom_group(s) # defined by resname and altloc id current_altloc = alt_id[i_atom] if current_altloc == "." or current_altloc == "?": current_altloc = "" # Main chain atoms current_resname = comp_id[i_atom] if (current_altloc, current_resname) not in atom_groups: atom_group = hierarchy.atom_group(altloc=current_altloc, resname=current_resname) atom_groups[(current_altloc, current_resname)] = atom_group if current_altloc == "": residue_group.insert_atom_group(0, atom_group) else: residue_group.append_atom_group(atom_group) else: atom_group = atom_groups[(current_altloc, current_resname)] # atom(s) atom = hierarchy.atom() atom_group.append_atom(atom) atom.set_element(type_symbol[i_atom]) atom.set_name( format_pdb_atom_name(atom_labels[i_atom], type_symbol[i_atom])) atom.set_xyz(new_xyz=(cart_x[i_atom], cart_y[i_atom], cart_z[i_atom])) atom.set_b(B_iso_or_equiv[i_atom]) atom.set_occ(occu[i_atom]) # hy36encode should go once the pdb.hierarchy has been # modified to no longer store fixed-width strings atom.set_serial( hy36encode(width=5, value=int(atom_site_id[i_atom]))) # some code relies on an empty segid being 4 spaces atom.set_segid(" ") if group_PDB is not None and group_PDB[i_atom] == "HETATM": atom.hetero = True if formal_charge is not None: charge = formal_charge[i_atom] if charge not in ("?", "."): if charge.endswith("-") or charge.startswith("-"): sign = "-" else: sign = "+" charge = charge.strip(" -+") charge = int(charge) if charge == 0: sign = "" atom.set_charge("%i%s" % (charge, sign)) if atom_site_fp is not None: fp = atom_site_fp[i_atom] if fp not in ("?", "."): atom.set_fp(new_fp=float(fp)) if atom_site_fdp is not None: fdp = atom_site_fdp[i_atom] if fdp not in ("?", "."): atom.set_fdp(new_fdp=float(fdp)) if anisotrop_id is not None and adps is not None: py_u_ij = py_adps.get(atom.serial.strip(), None) if py_u_ij is not None: atom.set_uij(py_u_ij) if len(self.hierarchy.models()) == 1: # for compatibility with single-model PDB files self.hierarchy.models()[0].id = ""
def __init__(self, cif_block): crystal_symmetry_builder.__init__(self, cif_block) self.hierarchy = hierarchy.root() # These items are mandatory for the _atom_site loop, all others are optional type_symbol = cif_block.get("_atom_site.type_symbol") atom_labels = cif_block.get("_atom_site.auth_atom_id") if atom_labels is None: atom_labels = cif_block.get("_atom_site.label_atom_id") # corresponds to chem comp atom name alt_id = cif_block.get("_atom_site.label_alt_id") # alternate conformer id label_asym_id = cif_block.get("_atom_site.label_asym_id") # chain id auth_asym_id = cif_block.get("_atom_site.auth_asym_id") if label_asym_id is None: label_asym_id = auth_asym_id if auth_asym_id is None: auth_asym_id = label_asym_id comp_id = cif_block.get("_atom_site.auth_comp_id") if comp_id is None: comp_id = cif_block.get("_atom_site.label_comp_id") # residue name entity_id = cif_block.get("_atom_site.label_entity_id") seq_id = cif_block.get("_atom_site.auth_seq_id") if seq_id is None: seq_id = cif_block.get("_atom_site.label_seq_id") # residue number assert [atom_labels, alt_id, auth_asym_id, comp_id, entity_id, seq_id].count(None) == 0 assert type_symbol is not None atom_site_fp = cif_block.get('_atom_site.phenix_scat_dispersion_real') atom_site_fdp = cif_block.get('_atom_site.phenix_scat_dispersion_imag') pdb_ins_code = cif_block.get("_atom_site.pdbx_PDB_ins_code") # insertion code model_ids = cif_block.get("_atom_site.pdbx_PDB_model_num") atom_site_id = cif_block.get("_atom_site.id") # only permitted values are ATOM or HETATM group_PDB = cif_block.get("_atom_site.group_PDB") # TODO: read esds B_iso_or_equiv = flex.double(cif_block.get("_atom_site.B_iso_or_equiv")) cart_x = flex.double(cif_block.get("_atom_site.Cartn_x")) cart_y = flex.double(cif_block.get("_atom_site.Cartn_y")) cart_z = flex.double(cif_block.get("_atom_site.Cartn_z")) occu = flex.double(cif_block.get("_atom_site.occupancy")) formal_charge = cif_block.get("_atom_site.pdbx_formal_charge") # anisotropic b-factors # TODO: read esds anisotrop_id = cif_block.get("_atom_site_anisotrop.id") adps = None if anisotrop_id is not None: u_ij = [cif_block.get("_atom_site_anisotrop.U[%s][%s]" %(ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23")] assert u_ij.count(None) in (0, 6) if u_ij.count(None) == 0: adps = u_ij else: assert u_ij.count(None) == 6 b_ij = [cif_block.get("_atom_site_anisotrop.B[%s][%s]" %(ij[0], ij[1])) for ij in ("11", "22", "33", "12", "13", "23")] assert b_ij.count(None) in (0, 6) if b_ij.count(None) == 0: adps = adptbx.b_as_u(b_ij) assert not (u_ij.count(None) and b_ij.count(None)) # illegal for both to be present if adps is not None: try: adps = [flex.double(adp) for adp in adps] except ValueError, e: raise CifBuilderError("Error interpreting ADPs: " + str(e)) adps = flex.sym_mat3_double(*adps)