def test_read_write_5cvz_final(self, via_cif=False): path = full_path('5cvz_final.pdb') with open(path) as f: expected = [line.rstrip() for line in f if is_written_to_pdb(line, via_cif) and # SCALE is not written b/c CRYST1 has more precision. line[:5] != 'SCALE'] st = gemmi.read_structure(path) if via_cif: # input file w/o TER record -> subchains not setup automatically st.setup_entities() doc = st.make_mmcif_document() st = gemmi.make_structure_from_block(doc[0]) # First MTRIX which is identity is not stored. Let's add it here. identity_ncs = gemmi.NcsOp() identity_ncs.id = '1' identity_ncs.given = True st.ncs.insert(0, identity_ncs) out_lines = self.write_and_read(st, via_cif=False) if via_cif: out_lines = [line for line in out_lines # input file has no REMARK 2, but it gets generated # from REMARK 3 when going pdb->cif->pdb if line[:10] != 'REMARK 2' and line[:5] != 'TER '] self.assertEqual(expected, [line.rstrip() for line in out_lines])
def write_and_read(self, st, via_cif): if via_cif: doc = st.make_mmcif_document() st = gemmi.make_structure_from_block(doc[0]) out_name = get_path_for_tempfile() st.write_pdb(out_name) return read_lines_and_remove(out_name)
def test_read_5i55_again(self): block = gemmi.cif.read(full_path('5i55.cif'))[0] st = gemmi.make_structure_from_block(block) self.assertEqual(st.info['_entry.id'], '5I55') center = st[0].calculate_center_of_mass() # PyMOL>print cmd.centerofmass() pymol_ctr = [15.468438991742687, 4.8312495347721045, 20.607400844016833] self.assertTrue(center.dist(gemmi.Position(*pymol_ctr)) < 1e-7) chain, = st[0] a, b, c, d = chain.subchains() ent_a = st.get_entity_of(a) self.assertEqual(ent_a.name, '1') self.assertEqual(ent_a.entity_type, gemmi.EntityType.Polymer) self.assertEqual(ent_a.polymer_type, gemmi.PolymerType.PeptideL) ent_b = st.get_entity_of(b) self.assertEqual(ent_b.entity_type, gemmi.EntityType.NonPolymer) self.assertEqual(ent_b.polymer_type, gemmi.PolymerType.Unknown) ent_d = st.get_entity('4') self.assertEqual(ent_d.subchains, ['D']) self.assertEqual(ent_d.entity_type, gemmi.EntityType.Water) self.assertEqual(ent_d.polymer_type, gemmi.PolymerType.Unknown) output_block = st.make_mmcif_document().sole_block() cnames = block.get_mmcif_category_names() cnames_out = [name for name in output_block.get_mmcif_category_names() if len(output_block.find_mmcif_category(name)) > 0] common_categories = [name for name in cnames_out if name in cnames] common_categories.sort() cc = ['_atom_site.', '_atom_type.', '_cell.', '_chem_comp.', '_diffrn.', '_diffrn_detector.', '_diffrn_radiation.', '_diffrn_source.', '_entity.', '_entity_poly.', '_entity_poly_seq.', '_entry.', '_exptl.', '_exptl_crystal.', '_pdbx_database_status.', '_pdbx_struct_assembly.', '_pdbx_struct_assembly_gen.', '_pdbx_struct_oper_list.', '_refine.', '_reflns.', '_software.', '_struct.', '_struct_asym.', '_struct_conf.', '_struct_conf_type.', '_struct_conn.', '_struct_conn_type.', '_struct_keywords.', '_struct_ref.', '_struct_ref_seq.', '_symmetry.'] self.assertEqual(common_categories, cc) for name in common_categories: cat_in = block.get_mmcif_category(name) cat_out = output_block.get_mmcif_category(name) for tag, values_out in cat_out.items(): if tag == 'ccp4_link_id': continue values_in = cat_in[tag] self.assertEqual(len(values_in), len(values_out)) for (a, b) in zip(values_in, values_out): try: if a == b or abs(float(a) - float(b)) < 2e-4: continue except ValueError: pass self.assertTrue(name+tag in ['_struct_conf.id', '_chem_comp.type']) for name_out in cnames_out: self.assertTrue(name_out in cnames)
def write_back_and_compare(self, path, via_cif): st = gemmi.read_structure(path) if via_cif: doc = st.make_mmcif_document() st = gemmi.make_structure_from_block(doc[0]) handle, out_name = tempfile.mkstemp() os.close(handle) st.write_pdb(out_name) return read_lines_and_remove(out_name)
def get_center_of_mass(data_block): try: st = gemmi.make_structure_from_block(data_block) model = st[0] center_of_mass = model.calculate_center_of_mass() return center_of_mass except Exception as e: logging.error(e) return False
def write_and_read(self, st, via_cif): if via_cif: st.setup_entities() st.assign_label_seq_id() doc = st.make_mmcif_document() st = gemmi.make_structure_from_block(doc[0]) out_name = get_path_for_tempfile() st.write_pdb(out_name) return read_lines_and_remove(out_name)
def test_software_category(self): doc = gemmi.cif.read_file(full_path('3dg1_final.cif')) input_block = doc.sole_block() st = gemmi.make_structure_from_block(input_block) output_block = st.make_mmcif_document().sole_block() software = output_block.get_mmcif_category('_software') del software['date'] assert software['version'][0] is False software['version'][0] = None self.assertEqual(input_block.get_mmcif_category('_software'), software)
def parse_structure3d(struct_str, filename, logger): t_start = timer() output_filename = filename if filename.endswith('.gz'): struct_str = gzip.decompress(struct_str) output_filename = filename[:-3] if output_filename.endswith('.cif'): cif_block = gemmi.cif.read_string(struct_str)[0] output_string = gemmi.make_structure_from_block(cif_block) elif output_filename.endswith('.pdb'): output_string = gemmi.read_pdb_string(struct_str) else: # Should never reach here raise RnaspiderUserError('Unknown file format was provided.') t_end = timer() logger.info( f"Finished parsing input. Elapsed time: {int((t_end - t_start) * 1000)} ms" ) return output_string
def test_ssbond_again(self): st = gemmi.read_pdb_string(SSBOND_FRAGMENT) doc = st.make_mmcif_document() st2 = gemmi.make_structure_from_block(doc[0]) out = st2.make_pdb_headers() self.assertEqual(out.splitlines(), SSBOND_FRAGMENT.splitlines()[:3])
def test_blank_mmcif(self): input_block = gemmi.cif.Block('empty') st = gemmi.make_structure_from_block(input_block) self.assertEqual(st.name, 'empty') output_block = st.make_mmcif_document().sole_block() self.assertEqual(output_block.get_mmcif_category_names(), [])