Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SingleGrid.cc
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 protocols/ligand_docking/scoring_grid/GridBase.cc
11 /// @author Sam DeLuca
12 
14 
16 #include <core/grid/CartGrid.hh>
17 #include <core/pose/util.hh>
19 
20 #include <basic/Tracer.hh>
21 
22 #include <utility/vector1.hh>
23 #include <utility/io/ozstream.hh>
24 #include <utility/tools/make_vector.hh>
25 #include <utility/json_spirit/json_spirit_value.h>
26 
27 #include <numeric/xyz.json.hh>
28 
29 #include <algorithm>
30 
31 namespace protocols {
32 namespace qsar {
33 namespace scoring_grid {
34 
35 static basic::Tracer GridBaseTracer("protocols.qsar.scoring_grid.SingleGrid");
36 
37 
38 SingleGrid::SingleGrid(std::string type) : type_(type),chain_('A')
39 {
40 
41 }
42 
44 {
45 
46 }
47 
48 void SingleGrid::initialize(core::Vector const & center, core::Real width,core::Real resolution )
49 {
50  center_=center;
51 
52  core::Size num_pts = static_cast<core::Size>(width/resolution);
53  //core::Size num_pts = 160;
54  //core::Real const resolution = 0.1;
55  core::Real const grid_halfwidth = width / 2.0;
56  grid_.setBase(
57  center.x() - grid_halfwidth,
58  center.y() - grid_halfwidth,
59  center.z() - grid_halfwidth
60  );
61  grid_.setDimensions(num_pts,num_pts,num_pts,resolution,resolution,resolution);
62  grid_.setupZones();
63  grid_.zero();
64 }
65 
66 utility::json_spirit::Value SingleGrid::serialize()
67 {
68  using utility::json_spirit::Value;
69  using utility::json_spirit::Pair;
70 
71  Pair type_record("type",Value(type_));
72  Pair center_record("center",numeric::serialize(center_));
73  Pair chain_record("chain",Value(chain_));
74  Pair grid_record("grid_data",grid_.serialize());
75 
76  return Value(utility::tools::make_vector(type_record,center_record,chain_record,grid_record));
77 
78 }
79 
80 void SingleGrid::deserialize(utility::json_spirit::mObject data)
81 {
82 
83 
84  type_ = data["type"].get_str();
85  center_ = numeric::deserialize<core::Real>(data["center"].get_array());
86 
87  grid_.deserialize(data["grid_data"].get_obj());
88 
89 }
90 
91 void SingleGrid::set_chain(char chain)
92 {
93  chain_ = chain;
94 }
95 
97 {
98  return chain_;
99 }
100 
102 {
103  return grid_;
104 }
105 
107 {
108  type_ = type;
109 }
110 
112 {
113  return type_;
114 }
115 
117 {
118  center_ = center;
119 }
120 
122 {
123  return center_;
124 }
125 
127 {
128  return grid_.getMaxValue();
129 }
130 
132 {
133  return grid_.getMinValue();
134 }
135 
137 {
138  if(grid_.is_in_grid(x,y,z))
139  {
140  return grid_.getValue(x,y,z);
141  }else
142  {
143  return 0.0;
144  }
145 }
146 
148 {
149  return grid_.getValue(coords);
150 }
151 
152 
154 {
155  int x_size(0);
156  int y_size(0);
157  int z_size(0);
158 
159  grid_.getNumberOfPoints(x_size,y_size,z_size);
160  return numeric::xyzVector<core::Size>(x_size,y_size,z_size);
161 }
162 
164 {
166  return get_pdb_coords(gridpt);
167 }
168 
170 {
171  return grid_.coords(gridpt);
172 }
173 
174 core::Real SingleGrid::score(core::conformation::Residue const & residue, core::Real const max_score,qsarMapOP /*qsar_map*/)
175 {
176  core::Real score = 0.0;
177  //GridBaseTracer << "map size is: " << qsar_map->size() <<std::endl;
178  for(core::Size atom_index = 1; atom_index <= residue.nheavyatoms() && score < max_score;++atom_index)
179  {
180  //TODO qsar map is broken, comment it out until it works right
181  //qsarPointOP qsar_info(qsar_map->get_point(atom_index,type_));
182 
183  //if(qsar_info != 0)
184  //{
185  core::Vector const & atom = residue.xyz(atom_index);
186  if(grid_.is_in_grid(atom.x(),atom.y(), atom.z()))
187  {
188 
189  core::Real grid_score = grid_.getValue(atom.x(),atom.y(),atom.z());
190 
191  score = score+ grid_score; //*qsar_info->get_value();
192  }
193  //}
194 
195  }
196  return score;
197 }
198 
200 {
201  core::Vector const & atom = residue.xyz(atomno);
202  if(grid_.is_in_grid(atom.x(),atom.y(), atom.z()))
203  {
204 
205  core::Real grid_score = grid_.getValue(atom.x(),atom.y(),atom.z());
206  return grid_score;
207  }else
208  {
209  return 0;
210  }
211 
212 }
213 
214 
215 std::list<std::pair<core::Vector, core::Real> >
217  core::Real lower_bound,
218  core::Real upper_bound,
219  core::Size stride)
220 {
221  std::list<std::pair<core::Vector, core::Real> > point_list;
222  int x_size(0);
223  int y_size(0);
224  int z_size(0);
225  grid_.getNumberOfPoints(x_size, y_size, z_size);
226  for(int x_index =0; x_index < x_size; x_index+= stride)
227  {
228  for(int y_index = 0; y_index < y_size; y_index += stride)
229  {
230  for(int z_index = 0; z_index < z_size; z_index+= stride)
231  {
232  core::grid::CartGrid<core::Real>::GridPt grid_point(x_index,y_index,z_index);
233  core::Vector pdb_coords(grid_.coords(grid_point));
234  core::Real value = grid_.getValue(grid_point);
235  //std::cout << value <<std::endl;
236  if(value >= lower_bound && value <= upper_bound)
237  {
238  std::pair<core::Vector,core::Real> data(pdb_coords,value);
239  point_list.push_back(data);
240  }
241  }
242  }
243  }
244  return point_list;
245 }
246 
247 void SingleGrid::grid_to_kin(utility::io::ozstream & out, core::Real min_val, core::Real max_val, core::Size stride)
248 {
249  int x_size(0);
250  int y_size(0);
251  int z_size(0);
252  grid_.getNumberOfPoints(x_size, y_size, z_size);
253  for(int x_index =0; x_index < x_size;x_index +=stride)
254  {
255  for(int y_index = 0; y_index < y_size; y_index += stride)
256  {
257  for(int z_index = 0; z_index < z_size; z_index += stride)
258  {
259  core::grid::CartGrid<core::Real>::GridPt grid_point(x_index,y_index,z_index);
260  core::Vector box_counter = grid_.coords(grid_point);
261  core::Real value = grid_.getValue(grid_point);
262  if(min_val <= value && value <= max_val)
263  {
264  out << '{' << x_index << ' ' << y_index << ' ' << z_index << "}U "
265  << box_counter.x() << ' ' << box_counter.y() << ' ' << box_counter.z() << '\n';
266  }
267  }
268  }
269  }
270 }
271 
272 
274 {
275  for(core::Size atom_index = 1; atom_index <= residue.natoms();++atom_index)
276  {
277  core::Vector atom_coords = residue.xyz(atom_index);
278  if(!grid_.is_in_grid(atom_coords.x(), atom_coords.y(), atom_coords.z()))
279  {
280  return false;
281  }
282  }
283  return true;
284 }
285 
286 
288 {
289  grid_.setFullOccupied(value);
290 }
291 
293  core::Vector const & coords,
294  core::Real radius,
295  core::Real value
296 ){
297  set_ring(coords, 0, radius, value);
298 }
299 
301  core::Vector const & coords,
302  core::Real inner_radius,
303  core::Real outer_radius,
304  core::Real value
305 ){
306  //TR <<"making sphere of radius " << radius << "and value " << value <<std::endl;
307  core::Real inner_radius2 = inner_radius*inner_radius;
308  core::Real outer_radius2 = outer_radius*outer_radius;
309  int x_count(0);
310  int y_count(0);
311  int z_count(0);
312  grid_.getNumberOfPoints(x_count,y_count,z_count);
313  core::Vector vector_radius (outer_radius);
314  core::grid::CartGrid<core::Real>::GridPt grid_min = grid_.gridpt(coords - outer_radius);
315  core::grid::CartGrid<core::Real>::GridPt grid_max = grid_.gridpt(coords + outer_radius);
316  //TR <<"min: " <<grid_min.x() << " "<<grid_min.y() << " " <<grid_min.z() <<std::endl;
317  //TR <<"max: "<<grid_max.x() << " "<<grid_max.y() << " " <<grid_max.z() <<std::endl;
318  //TR <<"counts: " << x_count <<" "<< y_count << " " << z_count <<std::endl;
319  for(int x_index = std::max(0,grid_min.x()); x_index <= std::min(x_count-1,grid_max.x());++x_index)
320  {
321  for(int y_index = std::max(0,grid_min.y());y_index <= std::min(y_count-1,grid_max.y());++y_index)
322  {
323  for(int z_index = std::max(0,grid_min.z()); z_index <= std::min(z_count-1,grid_max.z());++z_index)
324  {
325  core::grid::CartGrid<core::Real>::GridPt point(x_index, y_index, z_index);
326  core::Vector box_center = grid_.coords(point);
327  //TR <<x_index << " "<<y_index << " " <<z_index <<std::endl;
328  if(
329  box_center.distance_squared(coords) >= inner_radius2
330  && box_center.distance_squared(coords) <= outer_radius2
331  ){
332  grid_.setValue(point, value);
333  }
334  }
335  }
336  }
337  //TR << "done making sphere "<<std::endl;
338 }
339 
340 void SingleGrid::set_distance_sphere_for_atom(core::Real const & atom_shell,core::Vector const & coords,core::Real cutoff)
341 {
342  core::Real cutoff2 = cutoff*cutoff;
343  int x_count(0);
344  int y_count(0);
345  int z_count(0);
346 
347  grid_.getNumberOfPoints(x_count,y_count,z_count);
348  core::Vector vector_radius (cutoff);
349  core::grid::CartGrid<core::Real>::GridPt grid_min = grid_.gridpt(coords - cutoff);
350  core::grid::CartGrid<core::Real>::GridPt grid_max = grid_.gridpt(coords + cutoff);
351  for(int x_index = std::max(0,grid_min.x()); x_index <= std::min(x_count-1,grid_max.x());++x_index)
352  {
353  for(int y_index = std::max(0,grid_min.y());y_index <= std::min(y_count-1,grid_max.y());++y_index)
354  {
355  for(int z_index = std::max(0,grid_min.z()); z_index <= std::min(z_count-1,grid_max.z());++z_index)
356  {
357  core::grid::CartGrid<core::Real>::GridPt point(x_index, y_index, z_index);
358  core::Vector box_center(grid_.coords(point));
359  core::Real distance2 = box_center.distance_squared(coords);
360  if(distance2 <= cutoff2)
361  {
362  core::Real distance = sqrt(distance2);
363  core::Real current_value = grid_.getValue(point);
364  if(distance - atom_shell <= current_value)
365  {
366  grid_.setValue(point,distance - atom_shell);
367  }
368  }
369  }
370  }
371  }
372 }
373 
374 void SingleGrid::set_score_sphere_for_atom(numeric::interpolation::spline::InterpolatorOP lj_spline,core::Vector const & coords, core::Real cutoff)
375 {
376  core::Real cutoff2 = cutoff*cutoff;
377  int x_count(0);
378  int y_count(0);
379  int z_count(0);
380 
381  grid_.getNumberOfPoints(x_count,y_count,z_count);
382  core::Vector vector_radius (cutoff);
383  core::grid::CartGrid<core::Real>::GridPt grid_min = grid_.gridpt(coords - cutoff);
384  core::grid::CartGrid<core::Real>::GridPt grid_max = grid_.gridpt(coords + cutoff);
385  for(int x_index = std::max(0,grid_min.x()); x_index <= std::min(x_count-1,grid_max.x());++x_index)
386  {
387  for(int y_index = std::max(0,grid_min.y());y_index <= std::min(y_count-1,grid_max.y());++y_index)
388  {
389  for(int z_index = std::max(0,grid_min.z()); z_index <= std::min(z_count-1,grid_max.z());++z_index)
390  {
391  core::grid::CartGrid<core::Real>::GridPt point(x_index, y_index, z_index);
392  core::Vector box_center(grid_.coords(point));
393  core::Real distance2 = box_center.distance_squared(coords);
394  if(distance2 <= cutoff2)
395  {
396  core::Real distance = sqrt(distance2);
397  core::Real current_value = grid_.getValue(point);
398  core::Real spline_score = 0.0;
399  core::Real spline_score_deriv = 0.0;
400 
401  lj_spline->interpolate(distance,spline_score,spline_score_deriv);
402  if(spline_score <= current_value)
403  {
404  grid_.setValue(point,spline_score);
405  }
406 
407  }
408  }
409  }
410  }
411 
412 }
413 
414 void SingleGrid::diffuse_ring(core::Vector const & coords, core::Real radius, core::Real width, core::Real magnitude)
415 {
416  core::Real radius_squared = radius*radius;
417  core::Real half_width = width/2;
418  core::Real half_width_squared = half_width*half_width;
419  int x_count(0);
420  int y_count(0);
421  int z_count(0);
422  grid_.getNumberOfPoints(x_count,y_count,z_count);
423  core::Vector vector_radius (radius);
424  core::grid::CartGrid<core::Real>::GridPt grid_min = grid_.gridpt(coords - radius);
425  core::grid::CartGrid<core::Real>::GridPt grid_max = grid_.gridpt(coords + radius);
426  for(int x_index = std::max(0,grid_min.x()); x_index <= std::min(x_count-1,grid_max.x());++x_index)
427  {
428  for(int y_index = std::max(0,grid_min.y());y_index <= std::min(y_count-1,grid_max.y());++y_index)
429  {
430  for(int z_index = std::max(0,grid_min.z()); z_index <= std::min(z_count-1,grid_max.z());++z_index)
431  {
432  core::grid::CartGrid<core::Real>::GridPt point(x_index,y_index,z_index);
433  core::Vector origin = grid_.coords(point);
434  core::Real distance_squared = origin.distance_squared(coords);
435  //core::Real rad_distance_squared = abs(radius2-distance_squared);
436 
437  //if((distance_squared < radius2 + half_width) || (distance_squared > radius2-half_width))
438  //if(distance_squared <radius2)
439  if((distance_squared >radius_squared-half_width_squared && distance_squared < radius_squared)|| (distance_squared <radius_squared+half_width_squared && distance_squared > radius_squared))
440  {
441  //TR << "inserting value " <<std::endl;
442  grid_.setValue(point,magnitude);
443  }else
444  {
445 
446  //TR<< "d^2 is " << distance_squared <<std::endl;
447  core::Real distance_from_ring = 0.0;
448  if(distance_squared < radius_squared)
449  {
450  distance_from_ring = (radius_squared-half_width_squared)-distance_squared;
451  }else
452  {
453  distance_from_ring = distance_squared-(radius_squared+half_width_squared);
454  }
455 
456  if(distance_from_ring <= std::abs(magnitude))
457  {
458  grid_.setValue(point,magnitude+distance_from_ring);
459  }
460  else
461  {
462  }
463 
464  }
465 
466  }
467  }
468  }
469 }
470 
471 void SingleGrid::set_point(core::Vector const & coords, core::Real value)
472 {
473  grid_.setValue(coords,value);
474 }
475 
476 void SingleGrid::dump_BRIX(std::string const & prefix)
477 {
478  //std::string grid_type_string(qsar::qsarTypeManager::name_from_qsar_type(type_));
479  grid_.write_to_BRIX(prefix + type_ + ".omap");
480 }
481 
482 }
483 }
484 }