def test_array_conversion(path, model): mmtf_file = mmtf.MMTFFile.read(path) try: a1 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) except biotite.InvalidFileError: if model is None: # The file cannot be parsed into an AtomArrayStack, # as the models contain different numbers of atoms # -> skip this test case return else: raise mmtf_file = mmtf.MMTFFile() mmtf.set_structure(mmtf_file, a1) temp = TemporaryFile("w+b") mmtf_file.write(temp) temp.seek(0) mmtf_file = mmtf.MMTFFile.read(temp) temp.close() a2 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) for category in a1.get_annotation_categories(): assert a1.get_annotation(category).tolist() == \ a2.get_annotation(category).tolist() assert a1.coord.flatten().tolist() == \ approx(a2.coord.flatten().tolist(), abs=1e-3) assert a1.bonds == a2.bonds if a1.box is not None: assert np.allclose(a1.box, a2.box)
def test_extra_fields(): path = join(data_dir, "1l2y.mmtf") mmtf_file = mmtf.MMTFFile() mmtf_file.read(path) stack1 = mmtf.get_structure( mmtf_file, extra_fields=[ "atom_id", "b_factor", "occupancy", "charge" ] ) mmtf_file == mmtf.MMTFFile() mmtf.set_structure(mmtf_file, stack1) stack2 = mmtf.get_structure( mmtf_file, extra_fields=[ "atom_id", "b_factor", "occupancy", "charge" ] ) assert stack1.atom_id.tolist() == stack2.atom_id.tolist() assert stack1.b_factor.tolist() == approx(stack2.b_factor.tolist()) assert stack1.occupancy.tolist() == approx(stack2.occupancy.tolist()) assert stack1.charge.tolist() == stack2.charge.tolist()
def test_array_conversion(path, single_model): model = 1 if single_model else None mmtf_file = mmtf.MMTFFile() mmtf_file.read(path) a1 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) mmtf_file = mmtf.MMTFFile() mmtf.set_structure(mmtf_file, a1) a2 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) for category in a1.get_annotation_categories(): assert a1.get_annotation(category).tolist() == \ a2.get_annotation(category).tolist() assert a1.coord.flatten().tolist() == \ approx(a2.coord.flatten().tolist(), abs=1e-3) assert a1.bonds == a2.bonds
def test_array_conversion(path, single_model): model = 1 if single_model else None mmtf_file = mmtf.MMTFFile() mmtf_file.read(path) a1 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) mmtf_file = mmtf.MMTFFile() mmtf.set_structure(mmtf_file, a1) temp_file_name = biotite.temp_file("mmtf") mmtf_file.write(temp_file_name) mmtf_file = mmtf.MMTFFile() mmtf_file.read(temp_file_name) a2 = mmtf.get_structure(mmtf_file, model=model, include_bonds=True) for category in a1.get_annotation_categories(): assert a1.get_annotation(category).tolist() == \ a2.get_annotation(category).tolist() assert a1.coord.flatten().tolist() == \ approx(a2.coord.flatten().tolist(), abs=1e-3) assert a1.bonds == a2.bonds if a1.box is not None: assert np.allclose(a1.box, a2.box)
# several multiples. # The usage is similar to :class:`PDBxFile`: The :class:`MMTFFile` class # decodes the file and makes it raw information accessible. # Via :func:`get_structure()` the data can be loaded into an atom array # (stack) and :func:`set_structure()` is used to save it back into a # MMTF file. import numpy as np import biotite.structure.io.mmtf as mmtf mmtf_file_path = rcsb.fetch("1l2y", "mmtf", biotite.temp_dir()) file = mmtf.MMTFFile() file.read(mmtf_file_path) stack = mmtf.get_structure(file) array = mmtf.get_structure(file, model=1) # Do some fancy stuff mmtf.set_structure(file, array) ######################################################################## # A more low level access to MMTF files is also possible: # An MMTF file is structured as dictionary, with each key being a # strutural feature like the coordinates, the residue ID or the # secondary structure. If a field is encoded the decoded # :class:`ndarray` is returned, otherwise the dictionary value is # directly returned. # A list of all MMTF fields (keys) can be found in the # `specification <https://github.com/rcsb/mmtf/blob/master/spec.md#fields>`_. # The implementation of :class:`MMTFFile` decodes the encoded fields # only when you need them, so no computation time is wasted on fields # you are not interested in. # Field is not encoded