forked from MDAnalysis/mdanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDBx.py
More file actions
101 lines (76 loc) · 3.58 KB
/
Copy pathPDBx.py
File metadata and controls
101 lines (76 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the GNU Public Licence, v2 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
"""
PDBx (mmcif) files in MDAnalysis --- :mod:`MDAnalysis.coordinates.PDBx`
=======================================================================
Reads coordinates from a PDBx_ (mmcif) format file. Will populate the Universe positions from the
``_atom_site.Cartn_x`` field in the PDBx file. Will populate the unitcell dimensions from the ``_cell`` section.
.. _PDBx:
https://pdb101.rcsb.org/learn/guide-to-understanding-pdb-data/beginner’s-guide-to-pdb-structures-and-the-pdbx-mmcif-format
"""
import gemmi
import numpy as np
from gemmi import FractionalBox
from . import base
class PDBxReader(base.SingleFrameReaderBase):
format = ['cif', 'pdbx']
units = {'time': None, 'length': 'Angstrom'}
def __init__(self, filename, convert_units=True, **kwargs):
super().__init__(filename, convert_units=convert_units, **kwargs)
def _read_first_frame(self):
doc = gemmi.cif.read(self.filename)
block = doc.sole_block()
# PDBx/mmCIF with _cell. sections
box = block.find('_cell.', ['length_a', 'length_b', 'length_c',
'angle_alpha', 'angle_beta', 'angle_gamma'])
# CIF file with _cell_ sections
if not box:
box = block.find('_cell_', ['length_a', 'length_b', 'length_c',
'angle_alpha', 'angle_beta', 'angle_gamma'])
if box:
unitcell = np.zeros(6, dtype=np.float64)
unitcell[:] = box[0]
# PDBx/mmCIF with _cell. sections
coords = block.find('_atom_site.', ['Cartn_x', 'Cartn_y', 'Cartn_z'])
fractional = lambda xyz: xyz
# CIF file with _cell_ sections
if not coords:
coords = block.find('_atom_site_', ['fract_x', 'fract_y', 'fract_z'])
fractional = lambda xyz: np.multiply(xyz, unitcell[:3])
if not coords:
raise ValueError("No coordinates found in the file")
self.n_atoms = len(coords)
xyz = np.zeros((self.n_atoms, 3), dtype=np.float32)
for i, (x, y, z) in enumerate(coords):
xyz[i, :] = x, y, z
# for CIF: multiply fractional coordinates by unitcell lengths
xyz = fractional(xyz)
ts = self.ts = base.Timestep.from_coordinates(xyz, **self._ts_kwargs)
ts.frame = 0
ts.dimensions = unitcell
if self.convert_units:
# in-place !
self.convert_pos_from_native(self.ts._pos)
if self.ts.dimensions is not None:
self.convert_pos_from_native(self.ts.dimensions[:3])
return ts