Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MolecularSurfaceCalculator.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/scoring/sc/ShapeComplementarityCalculator.hh
11 /// @brief Headers for the Molecular Surface Calculator
12 /// @author Luki Goldschmidt (luki@mbi.ucla.edu), refactored by Alex Ford (fordas@uw.edu)
13 
14 #ifndef INCLUDED_core_scoring_sc_MolecularSurfaceCalculator_hh
15 #define INCLUDED_core_scoring_sc_MolecularSurfaceCalculator_hh
16 
17 // This code contains support for GPU acceleration using OpenCL.
18 // Build with scons option extras=opencl to enable GPU support.
19 //
20 // Note: Original Fotran code used floats rather than doubles. This code works
21 // correctly with either floats or core::Real, though core::Real is about 50%
22 // slower (tested on 64-bit machines). The code can be compiled with double (Real)
23 // precision floats by defining SC_PRECISION_REAL.
24 
25 // #define SC_PRECISION_REAL
26 // #define USEOPENCL
27 
28 // Core Headers
29 #include <core/types.hh>
30 #include <core/pose/Pose.fwd.hh>
33 #include <numeric/xyzVector.hh>
34 
35 //// C++ headers
36 #include <vector>
37 #include <string>
38 #include <utility/vector1.hh>
39 
40 // OpenCL headers
41 #include <basic/gpu/Timer.hh>
42 #ifdef USEOPENCL
43 #include <basic/gpu/GPU.hh>
44 #endif
45 
46 namespace core {
47 namespace scoring {
48 namespace sc {
49 
50 ////////////////////////////////////////////////////////////
51 // core::scoring::sc namespace
52 ////////////////////////////////////////////////////////////
53 
54 ////////////////////////////////////////////////////////////
55 // Types
56 //
57 //TODO Need to refactor to remove sc-specific results from this typedef
58 // Calculation results
59 typedef struct _RESULTS {
60  core::Real sc; // Interface shape complementarity statistic
61  core::Real area; // Area of interface (A^2)
62  core::Real distance; // Interface separation (A)
63  core::Size nAtoms; // Number of Atoms used in calculation
64  struct { // These detailed counts are for for both molecules (0, 1) and sum/average for both (2)
65  core::Real d_mean; // Mean seperation of molecules
66  core::Real d_median; // Median separation of molecules
67  core::Real s_mean; // Mean shape complementarity
68  core::Real s_median; // Median shape complementarity (this is sc above)
69  core::Size nAtoms; // Number of atoms in molecule
70  core::Size nBuriedAtoms; // Number of buried atoms in molecule
71  core::Size nBlockedAtoms; // Number of blocked (covered) atoms in molecule
72  core::Size nAllDots; // Number of molecule surface all dots
73  core::Size nTrimmedDots; // Number of molecule surface dots after trimming
74  core::Size nBuriedDots; // Number of buried dots (not on surface)
75  core::Size nAccessibleDots; // Number of accessible dots (on surface)
76  core::Real trimmedArea; // Trimmed area in Angstrom^2
77  } surface[3];
78  struct { // Surface dot counts by type (from Connolly algorithm)
79  core::Size convex; // Number of convex surface dots
80  core::Size concave; // Number of concace surface dots
81  core::Size toroidal; // NUmber of toroidal surfac dots
82  } dots; // True if computed results are valid
83  int valid;
84 } RESULTS;
85 
86 // Atom radius definition
87 typedef struct _ATOM_RADIUS {
88  char residue[4];
89  char atom[5];
90 #ifdef SC_PRECISION_REAL
92 #else
93  float radius;
94 #endif
95 } ATOM_RADIUS;
96 
97 // Molecular dot
98 class Atom;
99 typedef struct _DOT {
100 #ifdef SC_PRECISION_REAL
103 #else
105  float area;
106 #endif
107  int buried;
108  int type;
109  Atom const * atom;
110 } DOT;
111 
112 // Molecular probe
113 typedef struct _PROBE {
114  Atom const * pAtoms[3];
115 #ifdef SC_PRECISION_REAL
118 #else
119  float height;
121 #endif
122 } PROBE;
123 
124 // PDB Atom
125 class Atom :
126 #ifdef SC_PRECISION_REAL
127  public numeric::xyzVector < core::Real >
128 #else
129  public numeric::xyzVector < float >
130 #endif
131 {
132 public:
135  char atom[4];
136  char residue[4];
137 
139 
141 #ifdef SC_PRECISION_REAL
144 #else
145  float radius;
146  float density;
147 #endif
148  int atten;
149  int access;
150  std::vector<Atom*> neighbors;
151  std::vector<Atom*> buried;
152 
153 public:
154  Atom();
155  ~Atom();
156 
157  int operator ==(Atom const &atom2) {
158  // <EVIL>
159  // For speed reasons, rather than comparing the coordinates,
160  // we'll compare pointers as all atoms are part of the same atoms
161  // vector, so the same atom will have the same address
162  // </EVIL>
163  return this == &atom2;
164  }
165  int operator <=(Atom &atom2) {
166  return this <= &atom2;
167  }
168 };
169 
170 ////////////////////////////////////////////////////////////
171 // Molecular Surface Calculator class definition
172 ////////////////////////////////////////////////////////////
173 
175 
176 public:
177 #ifdef SC_PRECISION_REAL
178  typedef core::Real ScValue;
180 #else
181  typedef float ScValue;
183 #endif
184 
185  struct {
186  // From sc source
194 
195 #ifdef USEOPENCL
196  core::SSize gpu;
197  core::Size gpu_threads;
198 #endif
199  } settings;
200 
202  virtual ~MolecularSurfaceCalculator();
203  virtual int Init();
204  virtual void Reset();
205 
206  int add_atom(int molecule, Atom &atom);
207  core::Size AddResidue(int molecule, core::conformation::Residue const &residue);
208 
209  /// @begin MolecularSurfaceCalculator::Calc(core::pose::Pose const & pose, core::Size jump_id = 0)
210  /// @brief Generate molecular surfaces for the given pose.
211  ///// @detailed
212  // This function initializes the calculator, adds all residues in the given pose, and generates molecular surfaces.
213  //
214  // The pose is partitioned into separate molecules across the given jump. If the given jump is 0, the entire pose is
215  // loaded as molecule 1.
216  virtual int Calc(core::pose::Pose const & pose, core::Size jump_id = 0);
217 
218  /// @begin MolecularSurfaceCalculator::Calc()
219  /// @brief Generate molecular surfaces for loaded atoms.
220  ///// @detailed
221  // This function generates molecular surfaces for atoms added via add_atom and AddResidue.
222  //
223  // Init() must be called before this function.
224  virtual int Calc();
225 
226  std::vector<Atom> const & GetAtoms() { return run_.atoms; }
227  std::vector<DOT> const & GetDots(int const moleculeid) { return run_.dots[moleculeid]; }
228  RESULTS const & GetResults() { return run_.results; }
229 
230 protected:
231  /// @begin MolecularSurfaceCalculator::ComputeMolecularSurfaces
232  /// @brief Generate untrimmed surfaces for the defined molecules.
233  ///// @detailed
234  /// This function should be called within a try/catch block for ShapeComplementarityCalculatorException.
235  /// Raises exception on error.
237 
238  // This is a constant list of atom radii; declared static to avoid re-loading
239  // between computations
240  static std::vector<ATOM_RADIUS> radii_;
241 
242  int AssignAtomRadius(Atom &atom);
243  int WildcardMatch(char const *r, char const *pattern, int const l);
244  int ReadScRadii();
245  void AddDot(int const molecule, int const type, Vec3 const coor, ScValue const area, Vec3 const pcen, Atom const &atom);
246 
247  struct {
250  std::vector<Atom> atoms;
251  std::vector<DOT> dots[2];
252  std::vector<PROBE> probes;
255 
256  } run_;
257 
258  // Surface generation configuration
259  virtual int AssignAttentionNumbers(std::vector<Atom>& atom);
260 
261 private:
262 
263  // Molecular surface generation
264  int CalcDotsForAllAtoms(std::vector<Atom>& atoms);
265  int CalcDotsForAtoms(std::vector<Atom>& atoms);
267  int FindNeighborsForAtom(Atom& atom1);
268 
269  int GenerateToroidalSurface(Atom& atom1, Atom& atom2, Vec3 const uij, Vec3 const tij, ScValue rij, int between);
270  int GenerateConvexSurface(Atom const & atom1);
272 
273  // Function names similar to original source
274  int SecondLoop(Atom &pAtom1);
275  int ThirdLoop(Atom &pAtom1, Atom &pAtom, Vec3 const &uij, Vec3 const &tij, ScValue const rij);
276  int CheckAtomCollision2(Vec3 const &pijk, Atom const &atom1, Atom const &atom2, std::vector<Atom*> const &atoms);
277  int CheckPointCollision(Vec3 const &pcen, std::vector<Atom*> const &atoms);
278  int CheckProbeCollision(Vec3 const &point, std::vector<const PROBE*> const nears, ScValue const r2);
279 
280 
281  // Elementary functions
282  ScValue DistancePointToLine(Vec3 const &cen, Vec3 const &axis, Vec3 const &pnt);
283  ScValue SubArc(Vec3 const &cen, ScValue const rad, Vec3 const &axis, ScValue const density, Vec3 const &x, Vec3 const &v, std::vector<Vec3> &points);
284  ScValue SubDiv(Vec3 const &cen, ScValue const rad, Vec3 const &x, Vec3 const &y, ScValue angle, ScValue density, std::vector<Vec3> &points);
285  ScValue SubCir(Vec3 const &cen, ScValue const rad, Vec3 const &north, ScValue const density, std::vector<Vec3> &points);
286 
287  // Sort callback
288  static int _atom_distance_cb(void *a1, void *a2);
289 };
290 
291 } //namespace sc
292 } //namespace filters
293 } //namespace protocols
294 
295 #endif
296