1# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
2# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
3#
4# MDAnalysis --- https://www.mdanalysis.org
5# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
6# (see the file AUTHORS for the full list of names)
7#
8# Released under the GNU Public Licence, v2 or any higher version
9#
10# Please cite your use of MDAnalysis in published work:
11#
12# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
13# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
14# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
15# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
16# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
17#
18# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
19# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
20# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
21#
22from __future__ import absolute_import
23
24import MDAnalysis as mda
25from MDAnalysisTests.datafiles import PDBQT_input, PDBQT_querypdb
26from MDAnalysis.lib.NeighborSearch import AtomNeighborSearch
27
28from numpy.testing import (
29    assert_equal,
30)
31import pytest
32
33from MDAnalysisTests import make_Universe
34
35
36class TestPDBQT(object):
37    @pytest.fixture()
38    def universe(self):
39        """Set up the standard AdK system in implicit solvent."""
40        return mda.Universe(PDBQT_input)
41
42    def test_segid(self, universe):
43        sel = universe.select_atoms('segid A')
44        assert_equal(sel.n_atoms, 909, "failed to select segment A")
45        sel = universe.select_atoms('segid B')
46        assert_equal(sel.n_atoms, 896, "failed to select segment B")
47
48    def test_protein(self, universe):
49        sel = universe.select_atoms('protein')
50        assert_equal(sel.n_atoms, 1805, "failed to select protein")
51        assert_equal(sel.atoms.ix, universe.atoms.ix,
52                     "selected protein is not the same as auto-generated protein segment A+B")
53
54    def test_backbone(self, universe):
55        sel = universe.select_atoms('backbone')
56        assert_equal(sel.n_atoms, 796)
57
58    def test_neighborhood(self, universe):
59        '''test KDTree-based distance search around query atoms
60
61        Creates a KDTree of the protein and uses the coordinates of
62        the atoms in the query pdb to create a list of protein
63        residues within 4.0A of the query atoms.
64        '''
65        query_universe = mda.Universe(PDBQT_querypdb)  # PDB file
66
67        protein = universe.select_atoms("protein")
68        ns_protein = AtomNeighborSearch(protein)
69        query_atoms = query_universe.atoms
70        residue_neighbors = ns_protein.search(query_atoms, 4.0)
71        assert_equal(len(residue_neighbors), 80)
72
73    def test_n_frames(self, universe):
74        assert_equal(universe.trajectory.n_frames, 1,
75                     "wrong number of frames in pdb")
76
77    def test_time(self, universe):
78        assert_equal(universe.trajectory.time, 0.0, "wrong time of the frame")
79
80    def test_frame(self, universe):
81        assert_equal(universe.trajectory.frame, 0,
82                     "wrong frame number (0-based, should be 0 for single frame readers)")
83
84
85class TestPDBQTWriter(object):
86    reqd_attributes = ['names', 'types', 'resids', 'resnames', 'radii',
87                       'charges']
88
89    @pytest.fixture()
90    def outfile(self, tmpdir):
91        return str(tmpdir) + 'out.pdbqt'
92
93    def test_roundtrip_writing_coords(self, outfile):
94        u = mda.Universe(PDBQT_input)
95        u.atoms.write(outfile)
96        u2 = mda.Universe(outfile)
97
98        assert_equal(u2.atoms.positions, u.atoms.positions,
99                     "Round trip does not preserve coordinates")
100
101    def test_roundtrip_formatting(self, outfile):
102        # Compare formatting of first line
103        u = mda.Universe(PDBQT_input)
104        u.atoms.write(outfile)
105
106        with open(PDBQT_input, 'r') as inf:
107            l_ref = inf.readline().strip()
108        with open(outfile, 'r') as inf:
109            inf.readline()  # header
110            inf.readline()  # cryst
111            l_new = inf.readline().strip()
112        assert l_ref == l_new
113
114    @staticmethod
115    def assert_writing_warns(u, outfile):
116        with pytest.warns(UserWarning):
117            u.atoms.write(outfile)
118
119    def test_write_no_charges(self, outfile):
120        attrs = self.reqd_attributes
121        attrs.remove('charges')
122        u = make_Universe(attrs, trajectory=True)
123
124        self.assert_writing_warns(u, outfile)
125
126        u2 = mda.Universe(outfile)
127
128        assert all(u2.atoms.charges == 0.0)
129
130    def test_write_no_chainids_with_segids(self, outfile):
131        attrs = self.reqd_attributes
132        attrs.append('segids')
133        u = make_Universe(attrs, trajectory=True)
134
135        u.atoms.write(outfile)
136        u2 = mda.Universe(outfile)
137
138        # Should have used last letter of segid as chainid
139        assert all(u2.atoms[:25].segids == 'A')
140        assert all(u2.atoms[25:50].segids == 'B')
141
142    def test_get_writer(self, outfile):
143        u = mda.Universe(PDBQT_input)
144        w = u.trajectory.Writer(outfile)
145
146        assert isinstance(w, mda.coordinates.PDBQT.PDBQTWriter)
147        w.close()
148