Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DarcParticleSwarmMinimizer.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/pockets/DarcParticleSwarmMinimizer.cc
11 /// @brief
12 /// @author Karen R. Khar
13 
14 
17 #include <utility/exit.hh>
18 #include <numeric/random/random.hh>
19 #include <basic/options/option.hh>
20 #include <basic/options/keys/OptionKeys.hh>
21 #include <basic/options/keys/fingerprint.OptionKeys.gen.hh>
22 #include <basic/database/open.hh>
23 #include <core/pose/Pose.hh>
24 #include <core/id/AtomID_Map.hh>
28 #include <ObjexxFCL/format.hh>
29 #include <algorithm>
30 #include <utility/vector1.hh>
31 
32 static numeric::random::RandomGenerator my_RG(6172108); // <- Magic number, do not change it!!!
33 
34 namespace protocols {
35 namespace pockets {
36 
37 using namespace ObjexxFCL::fmt;
38 
39 // note: f_fitness is not used, rather we hard-code the objective function here to enable parellization across particles (for GPU)
40 //void DarcParticleSwarmMinimizer::score_all_particles(core::optimization::Multifunc & f_fitness, core::optimization::ParticleOPs & particles) {
42 
43  using namespace core::optimization;
44 
45  Size const N = particles.size();
46  ligand_natoms_ = pfp_.compute_ligand_natoms();
47 
48  for (Size j = 1; j <= N; ++j) {
49  core::optimization::Multivec const vars = particles[j]->p_;
50  numeric::xyzVector<core::Real> origin_offset;
51  origin_offset.x() = vars[1];
52  origin_offset.y() = vars[2];
53  origin_offset.z() = vars[3];
54  core::conformation::ResidueCOP ligand_rsd = pfp_.move_ligand_( nfp_, origin_offset, vars[4], vars[5], vars[6] );
55  fill_atom_arrays_(j-1, ligand_rsd );
56  }
57 
58 #ifdef USEOPENCL
59 
60  if(nfp_.gpu_.use()){
61 
62  int actual_atom_array_size = ligand_natoms_ * N;
63  nfp_.gpu_.WriteData(nfp_.gpu_atoms_, nfp_.atom_, sizeof(*nfp_.atom_) * actual_atom_array_size);
64 
65  if(!nfp_.gpu_.ExecuteKernel("Check_for_intersection", nfp_.gpu_num_rays_, 256, 64, NULL)) {
66  std::cout << "Failed to launch kernel: " << nfp_.gpu_.lastErrorStr() << std::endl;
67  exit(1);
68  }
69 
70  if(!nfp_.gpu_.ExecuteKernel("Get_scores", N, 256, 64, NULL)) {
71  std::cout << "Failed to launch kernel: " << nfp_.gpu_.lastErrorStr() << std::endl;
72  exit(1);
73  }
74 
75  nfp_.gpu_.ReadData(nfp_.particle_scores_, nfp_.gpu_particle_scores_, sizeof(*nfp_.particle_scores_) * N);
76 
77  for (core::Size j = 0; j < N; ++j) {
78  core::Real particle_score = nfp_.particle_scores_[j];
79  particles[j+1]->set_score(particle_score);
80  }
81 
82  } else {
83 
84 #endif
85 
86  for (core::Size j = 1; j <= N; ++j) {
87  core::Real particle_score = DarcPSO_fp_compare_( j-1, missing_pt_, steric_, extra_pt_ );
88  particles[j]->set_score(particle_score);
89  }
90 
91 #ifdef USEOPENCL
92  }
93 #endif
94 
95 }
96 
98 
99  using namespace basic::options;
100  core::Real const radius_scale = option[ OptionKeys::fingerprint::atom_radius_scale ];
101  core::Real const atom_buffer = option[ OptionKeys::fingerprint::atom_radius_buffer ];
102 
103  for (Size i = 1; i <= ligand_natoms_; ++i) {
104 
105  core::Size const curr_array_inx = ( ligand_natoms_ * particle_inx ) + ( i-1 );
106 
107  numeric::xyzVector<core::Real> this_atomcoors = ligand_rsd->atom(i).xyz() - pfp_.origin();
108  core::Real const this_atom_radius = ( ligand_rsd->atom_type(i).lj_radius() - atom_buffer ) * radius_scale;
109  nfp_.atom_[curr_array_inx].x = this_atomcoors.x();
110  nfp_.atom_[curr_array_inx].y = this_atomcoors.y();
111  nfp_.atom_[curr_array_inx].z = this_atomcoors.z();
112  nfp_.atom_[curr_array_inx].w = this_atom_radius;
113 
114  // find the atom center (in spherical coors)
115  spherical_coor_triplet atom_center;
116  convert_cartesian_to_spherical_coor_triplet( this_atomcoors, atom_center );
117 
118  core::Real const tmp_atomx=this_atomcoors.x();
119  core::Real const tmp_atomy=this_atomcoors.y();
120  core::Real const tmp_atomz=this_atomcoors.z();
121 
122  core::Real curr_max_phi( 999 );
123  core::Real curr_min_phi( -999 );
124  if ( std::abs(tmp_atomz) > 0.00001 ) {
125  core::Real const inside_phi_asin = this_atom_radius / tmp_atomz;
126  if ( std::abs(inside_phi_asin) < 1. ) {
127  core::Real const max_angular_displacement_phi = std::abs( asin( inside_phi_asin ) );
128  curr_max_phi = atom_center.phi + max_angular_displacement_phi;
129  curr_min_phi = atom_center.phi - max_angular_displacement_phi;
130  }
131  }
132 
133  core::Real curr_max_psi( 999 );
134  core::Real curr_min_psi( -999 );
135  if ( ( std::abs(tmp_atomx) > 0.00001 ) || ( std::abs(tmp_atomx) > 0.00001 ) ) {
136  core::Real const inside_psi_asin = this_atom_radius / sqrt( (tmp_atomx*tmp_atomx) + (tmp_atomy*tmp_atomy) );
137  if ( std::abs(inside_psi_asin) < 1. ) {
138  core::Real const max_angular_displacement_psi = std::abs( asin( inside_psi_asin ) );
139  curr_max_psi = atom_center.psi + max_angular_displacement_psi;
140  curr_min_psi = atom_center.psi - max_angular_displacement_psi;
141  }
142  }
143 
144  if ( curr_max_phi > nfp_.ligand_maxmin_phipsi_[particle_inx].x ) nfp_.ligand_maxmin_phipsi_[particle_inx].x = curr_max_phi;
145  if ( curr_max_psi > nfp_.ligand_maxmin_phipsi_[particle_inx].y ) nfp_.ligand_maxmin_phipsi_[particle_inx].y = curr_max_psi;
146  if ( curr_min_phi < nfp_.ligand_maxmin_phipsi_[particle_inx].z ) nfp_.ligand_maxmin_phipsi_[particle_inx].z = curr_min_phi;
147  if ( curr_min_psi < nfp_.ligand_maxmin_phipsi_[particle_inx].w ) nfp_.ligand_maxmin_phipsi_[particle_inx].w = curr_min_psi;
148  nfp_.atom_maxmin_phipsi_[curr_array_inx].x = curr_max_phi;
149  nfp_.atom_maxmin_phipsi_[curr_array_inx].y = curr_max_psi;
150  nfp_.atom_maxmin_phipsi_[curr_array_inx].z = curr_min_phi;
151  nfp_.atom_maxmin_phipsi_[curr_array_inx].w = curr_min_psi;
152 
153  }
154 
155 }
156 
157 core::Real DarcParticleSwarmMinimizer::DarcPSO_fp_compare_( core::Size particle_inx, core::Real const & missing_point_weight, core::Real const & steric_weight, core::Real const & extra_point_weight ) {
158 
159  core::Real Total_score = 0;
160  core::Size num_rays = 0;
161 
162  for (std::list<spherical_coor_triplet>::const_iterator ni = nfp_.triplet_fingerprint_data().begin(); ni != nfp_.triplet_fingerprint_data().end(); ++ni) {
163 
164  core::Real curr_phi = ni->phi;
165  core::Real curr_psi = ni->psi;
166  core::Real best_rho_sq(9999.);
167  core::Size best_intersecting_atom(0);
168  for (Size i = 1, i_end = ligand_natoms_; i <= i_end; ++i) {
169 
170  core::Size const curr_array_inx = ( ligand_natoms_ * particle_inx ) + ( i-1 );
171 
172  if ( nfp_.atom_[curr_array_inx].w < 0.001 ) continue;
173 
174  while ( curr_phi < nfp_.atom_maxmin_phipsi_[curr_array_inx].z ) {
175  curr_phi += numeric::constants::r::pi_2;
176  }
177  while ( curr_phi > nfp_.atom_maxmin_phipsi_[curr_array_inx].x ) {
178  curr_phi -= numeric::constants::r::pi_2;
179  }
180  while ( curr_psi < nfp_.atom_maxmin_phipsi_[curr_array_inx].w ) {
181  curr_psi += numeric::constants::r::pi_2;
182  }
183  while ( curr_psi > nfp_.atom_maxmin_phipsi_[curr_array_inx].y ) {
184  curr_psi -= numeric::constants::r::pi_2;
185  }
186  if ( curr_phi < nfp_.atom_maxmin_phipsi_[curr_array_inx].z ) continue;
187  if ( curr_psi < nfp_.atom_maxmin_phipsi_[curr_array_inx].w ) continue;
188  if ( curr_phi > nfp_.atom_maxmin_phipsi_[curr_array_inx].x ) continue;
189  if ( curr_psi > nfp_.atom_maxmin_phipsi_[curr_array_inx].y ) continue;
190 
191  core::Real const min_intersect_SQ = Find_Closest_Intersect_SQ(curr_phi,curr_psi,nfp_.atom_[curr_array_inx].x,nfp_.atom_[curr_array_inx].y,nfp_.atom_[curr_array_inx].z,nfp_.atom_[curr_array_inx].w);
192 
193  if ( min_intersect_SQ < best_rho_sq ) {
194  best_rho_sq = min_intersect_SQ;
195  }
196  }
197 
198  core::Real plaid_rho = 9999.;
199  if ( best_rho_sq < 9998. ) {
200  plaid_rho = sqrt(best_rho_sq);
201  }
202 
203  if ( (plaid_rho > 9998.) && (ni->rho > 0.001) ) {
204  Total_score += missing_point_weight;
205  num_rays++;
206  }
207  if ( (plaid_rho < 9999.) && (ni->rho < 0.001 ) ) {
208  Total_score += extra_point_weight;
209  num_rays++;
210  }
211  if ( (plaid_rho < 9999.) && (ni->rho > 0.001 ) ) {
212  core::Real dist_deviation = ( plaid_rho - ni->rho );
213  if (dist_deviation < 0.0) dist_deviation = ( ni->rho - plaid_rho ) * steric_weight;
214  Total_score += dist_deviation;
215  num_rays++;
216  }
217  }
218 
219  return (Total_score/num_rays);
220 }
221 
222 } // namespace pockets
223 } // namespace protocols