Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GridManager.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 src/protocols/ligand_docking/scoring_grid/GridManager.cc
11 /// @author Sam DeLuca
12 
16 #include <protocols/jd2/Job.hh>
18 
19 #include <core/pose/util.hh>
21 #include <basic/Tracer.hh>
22 
23 #include <basic/options/option.hh>
24 #include <basic/options/keys/OptionKeys.hh>
25 #include <basic/options/keys/qsar.OptionKeys.gen.hh>
26 
27 // Utility headers
28 #include <utility/io/izstream.hh>
29 #include <utility/io/ozstream.hh>
30 #include <utility/vector0.hh>
31 #include <utility/vector1.hh>
32 #include <utility/tag/Tag.hh>
33 #include <utility/exit.hh>
34 #include <utility/tools/make_vector.hh>
35 #include <utility/file/file_sys_util.hh>
36 #include <utility/json_spirit/json_spirit_writer.h>
37 #include <utility/json_spirit/json_spirit_reader.h>
38 
39 //STL headers
40 #include <iostream>
41 #include <fstream>
42 #include <map>
43 
44 //Auto Headers
45 #include <boost/bind.hpp>
46 
47 namespace protocols {
48 namespace qsar {
49 namespace scoring_grid {
50 
51 static basic::Tracer GridManagerTracer("protocols.qsar.scoring_grid.GridManager");
52 
53 GridManager* GridManager::instance_(0);
54 
56 {
57  if(instance_ == 0)
58  {
59  instance_ = new GridManager();
60  }
61  return instance_;
62 }
63 
65 {
66  grid_map_.clear();
67  score_map_.clear();
68  last_tag_ = "";
69  width_ = 40;
70  resolution_ = 0.25;
71  qsar_map_ = 0;
72  initialized_ = false;
73  chain_ = 'X';
74  normalized_ = false;
75 }
76 
78  last_tag_(""),
79  width_(40),
80  resolution_(0.25),
81  qsar_map_(0),
82  initialized_(false),
83  chain_('X'),
84  normalized_(false)
85 {
86  grid_map_.clear();
87  score_map_.clear();
88 }
89 
90 void GridManager::set_normalized(bool normalized)
91 {
92  normalized_ = normalized;
93 }
94 
96 {
97  width_ = width;
98 }
99 
101 {
102  resolution_=resolution;
103 }
104 
105 void GridManager::set_chain(char chain)
106 {
107  chain_ = chain;
108 }
109 
111 {
112  qsar_map_ = qsar_map;
113 }
114 
116 {
117  if(qsar_map_ != 0)
118  {
119  return true;
120  }else
121  {
122  return false;
123  }
124 }
125 
127 {
128 
129  std::string name= tag->getName();
130  core::Real weight = tag->getOption<core::Real>("weight");
131  grid_weights_.insert(std::make_pair(name,weight));
132  GridManagerTracer.Debug << name <<std::endl;
133  GridBaseOP new_grid(GridFactory::get_instance()->new_grid(tag));
134  insert_grid(name, new_grid);
135 
136 
137 }
138 
139 void GridManager::insert_grid(std::string const name, GridBaseOP const grid)
140 {
141  grid_map_[name] = grid;
142 }
143 
145 {
146  return grid_map_.find(grid_type)->second;
147 }
148 
150 {
152 
153  std::map<std::string,GridBaseOP>::const_iterator it;
154  for(it = grid_map_.begin();it != grid_map_.end();++it)
155  {
156  grid_names.push_back(it->first);
157  }
158  return grid_names;
159 }
160 
162 {
163  score_map_.clear();
164 
165  core::Real total_score =0.0;
166  const core::Real max_score = 9999.0;
167  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
168  for(;map_iterator != grid_map_.end();++map_iterator)
169  {
170  core::Real component_score =0;
171  GridBaseOP current_grid(*map_iterator->second);
172 
173  //for(core::Size atom_index = 1; atom_index <= residue.nheavyatoms();++atom_index)
174  //{
175  core::Real current_score(current_grid->score(residue,max_score,qsar_map_));
176  component_score += current_score;
177  //}
178  total_score += component_score;
179  std::pair<std::string, core::Real> new_score(current_grid->get_type(),component_score);
180  score_map_.insert(new_score);
181  }
182 
183  if(normalized_)
184  {
185  return total_score/static_cast<core::Real>(residue.natoms());
186  }
187  return total_score;
188 }
189 
190 
191 
193 {
194  score_map_.clear();
195  core::Real total_score = 0.0;
196  const core::Real max_score = 9999.0;
197 
198  core::conformation::ResidueCOPs residue_vector = core::pose::get_chain_residues(pose,chain_id);
199 
200  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
201  for(;map_iterator != grid_map_.end();++map_iterator)
202  {
203  core::Real component_score = 0;
204  GridBaseOP current_grid(*map_iterator->second);
205  core::Real weight(grid_weights_[map_iterator->first]);
206  for(core::Size residue_count = 1; residue_count <= residue_vector.size(); ++residue_count )
207  {
208  core::conformation::ResidueCOP residue(residue_vector[residue_count]);
209  core::Real current_score(current_grid->score(*residue,max_score,qsar_map_));
210  component_score += current_score;
211  }
212  total_score += component_score*weight;
213  std::pair<std::string, core::Real> new_score(current_grid->get_type(),component_score);
214  score_map_.insert(new_score);
215  }
216 
217  if(normalized_)
218  {
219  core::Size n_atoms = 0;
220  for(core::Size i = 1; i < residue_vector.size();++i)
221  {
222  n_atoms += residue_vector[i]->natoms();
223  }
224  return total_score/static_cast<core::Real>(n_atoms);
225  }
226 
227  return total_score;
228 
229 }
230 
231 
232 std::map<std::string,core::Real> GridManager::atom_score(core::pose::Pose const & /*pose*/, core::conformation::Residue const & residue, core::Size atomindex )
233 {
234  std::map<std::string,core::Real> score_map;
235  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
236  for(;map_iterator != grid_map_.end();++map_iterator)
237  {
238  GridBaseOP current_grid(*map_iterator->second);
239  core::Real weight(grid_weights_[map_iterator->first]);
240  core::Real atom_score = current_grid->atom_score(residue,atomindex,qsar_map_);
241  std::string grid_type = current_grid->get_type();
242  score_map.insert(std::make_pair(grid_type,atom_score*weight));
243  }
244  return score_map;
245 }
246 
247 void GridManager::update_grids(core::pose::Pose const & pose, core::Vector const & center,utility::vector1<core::Size> ligand_chain_ids_to_exclude)
248 {
249  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
250  for(;map_iterator != grid_map_.end(); ++map_iterator)
251  {
252  GridBaseOP current_grid(*map_iterator->second);
253  current_grid->initialize(center,width_,resolution_);
254  current_grid->set_chain(chain_);
255  current_grid->refresh(pose,center,ligand_chain_ids_to_exclude);
256  }
257 }
258 
259 
260 void GridManager::update_grids(core::pose::Pose const & pose, core::Vector const & center, core::Size const & ligand_chain_id_to_exclude)
261 {
262  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
263  for(;map_iterator != grid_map_.end();++map_iterator)
264  {
265  GridBaseOP current_grid(*map_iterator->second);
266  current_grid->initialize(center,width_,resolution_);
267  current_grid->set_chain(chain_);
268  current_grid->refresh(pose,center,ligand_chain_id_to_exclude);
269  }
270 }
271 
272 
273 void GridManager::update_grids(core::pose::Pose const & pose, core::Vector const & center)
274 {
275 
277  std::map<std::string,GridMap>::const_iterator grid_cache_entry(grid_map_cache_.find(chain_hash));
278 
279  bool grid_directory_active = basic::options::option[basic::options::OptionKeys::qsar::grid_dir].user();
280 
281  if(!grid_directory_active)
282  {
283  GridManagerTracer << "WARNING: option -qsar:grid_dir is not set. Use this flag to specify a directory to store scoring grids. This will save you a huge amount of time" <<std::endl;
284  }
285 
286  if(grid_cache_entry != grid_map_cache_.end()) //we've already seen this conformation, load the associated grid out of the map
287  {
288  GridManagerTracer << "Found a conformation matching hash: " << chain_hash << " Loading from grid cache" <<std::endl;
289  grid_map_ = grid_cache_entry->second;
290  }else // This is a new conformation
291  {
292 
293  //Try to read it off the disk
294  if(grid_directory_active)
295  {
296  //files are in the format grid_directory/hash.json.gz
297  std::string directory_path(basic::options::option[basic::options::OptionKeys::qsar::grid_dir]());
298  utility::io::izstream grid_file(directory_path+"/"+chain_hash+".json.gz");
299  if(grid_file)
300  {
301  utility::json_spirit::mValue gridmap_data;
302  utility::json_spirit::read(grid_file,gridmap_data);
303  deserialize(gridmap_data.get_array());
304  //Now grid_map_ is whatever was in that file. We never want to do this again, put it in the cache
305  GridManagerTracer << "successfully read grids from the disk for conformation matching hash" << chain_hash <<std::endl;
306  grid_map_cache_.insert(std::make_pair(chain_hash,grid_map_));
307  return;
308 
309  }
310  }
311 
312  GridManagerTracer << "No conformation matching hash: " << chain_hash << " Updating grid and adding it to the cache" <<std::endl;
313 
314  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
315 
316  for(;map_iterator != grid_map_.end();++map_iterator)
317  {
318 
319  GridBaseOP current_grid(*map_iterator->second);
320  GridManagerTracer.Debug <<"updating grid " << map_iterator->first << std::endl;
321  current_grid->initialize(center,width_,resolution_);
322  current_grid->set_chain(chain_);
323  current_grid->refresh(pose,center);
324  GridManagerTracer.Debug <<"done updating grid" <<std::endl;
325  }
326 
327  if(basic::options::option[basic::options::OptionKeys::qsar::max_grid_cache_size].user() &&
328  grid_map_cache_.size() >= core::Size(basic::options::option[basic::options::OptionKeys::qsar::max_grid_cache_size]() ) )
329  {
330  GridManagerTracer << "Grid cache exceeds max_cache_size, clearing old scoring grids to save memory." <<std::endl;
331  grid_map_cache_.clear();
332  }
333  grid_map_cache_.insert(std::make_pair(chain_hash,grid_map_));
334 
335  if(grid_directory_active)
336  {
337  //if we just made a grid, we should write it to the disk for safekeeping.
338  std::string directory_path(basic::options::option[basic::options::OptionKeys::qsar::grid_dir]());
339  std::string temp_path(directory_path+"/"+chain_hash+".inprogress");
340  if(!utility::file::file_exists(temp_path)) //If the inprogress file is there something else is busy writing
341  {
342  utility::io::ozstream progress_file(temp_path);
343  progress_file << "temp" <<std::endl;
344  progress_file.close();
345 
346  utility::io::ozstream grid_file(directory_path+"/"+chain_hash+".json.gz");
347 
348  grid_file << utility::json_spirit::write(serialize()) << std::endl;
349  grid_file.close();
350  utility::file::file_delete(temp_path);
351 
352  GridManagerTracer << "wrote grid matching hash: " << chain_hash << " to disk" <<std::endl;
353 
354  }
355 
356  }
357 
358  }
359 }
360 
362 {
363  if(grid_map_.size() == 0)
364  utility_exit_with_message("hmm, no grids in the grid manager. Are they defined in the XML script?");
365  if(!initialized_)
366  {
367  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
368  for(;map_iterator != grid_map_.end();++map_iterator)
369  {
370  GridBaseOP current_grid(*map_iterator->second);
371  current_grid->initialize(center,width_,resolution_);
372  }
373  initialized_ = true;
374  }
375 
376 }
377 
379 {
380  return grid_map_.size();
381 }
382 
384 {
385  return score_map_;
386 }
387 
389 {
390  std::map<std::string,core::Real>::iterator map_iterator(score_map_.begin());
391  int total_score =0;
392  for(;map_iterator !=score_map_.end(); ++map_iterator)
393  {
394  std::string type_name(map_iterator->first);
395  int score(static_cast<int>(map_iterator->second));
396  total_score +=score;
397 
398  //jd2::PDBJobOutputter::
399 
400  job->add_string_real_pair("grid_"+type_name,score);
401  }
402  job->add_string_real_pair("grid_total",total_score);
403 }
404 
406 {
407  std::map<std::string, GridBaseOP>::iterator map_iterator(grid_map_.begin());
408  for(;map_iterator != grid_map_.end(); ++map_iterator)
409  {
410  GridBaseOP current_grid(map_iterator->second);
411  current_grid->dump_BRIX(prefix);
412  }
413 }
414 
415 utility::json_spirit::Value GridManager::serialize()
416 {
417  using utility::json_spirit::Value;
418  std::vector<Value> gridmap_data;
419  for(GridMap::iterator it = grid_map_.begin(); it != grid_map_.end();++it)
420  {
421  Value grid_name(it->first);
422  Value grid(it->second->serialize());
423  std::vector<Value> grid_pair_values;
424  grid_pair_values.push_back(grid_name);
425  grid_pair_values.push_back(grid);
426  Value grid_pair(grid_pair_values);
427  gridmap_data.push_back(grid_pair);
428  }
429  return Value(gridmap_data);
430 }
431 
432 void GridManager::deserialize(utility::json_spirit::mArray data)
433 {
434  grid_map_.clear();
435  for(utility::json_spirit::mArray::iterator it = data.begin(); it != data.end();++it)
436  {
437  utility::json_spirit::mArray grid_data(it->get_array());
438  std::string grid_name = grid_data[0].get_str();
439  GridBaseOP grid(GridFactory::get_instance()->new_grid(grid_data[1].get_obj()));
440  grid_map_[grid_name] = grid;
441  }
442 }
443 
445 {
446  std::map<std::string,GridBaseOP>::iterator map_iterator(grid_map_.begin());
447  for(;map_iterator != grid_map_.end();++map_iterator)
448  {
449  GridBaseOP current_grid(*map_iterator->second);
450  if(!current_grid->is_in_grid(residue))
451  {
452  return false;
453  }
454  }
455  return true;
456 }
457 
458 }
459 }
460 }