Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RenderGridsToKinemage.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/qsar/RenderGridsToKinemage.cc
11 /// @author Sam DeLuca
12 
13 
19 
20 #include <core/pose/Pose.hh>
22 #include <utility/vector0.hh>
23 #include <utility/vector1.hh>
24 #include <utility/io/ozstream.hh>
25 #include <utility/tag/Tag.hh>
26 
27 #include <numeric/xyz.functions.hh>
28 #include <numeric/color_util.hh>
29 
30 #include <utility/excn/Exceptions.hh>
31 #include <iostream>
32 
33 
34 
35 
36 
37 namespace protocols {
38 namespace qsar {
39 
41 {
43 }
44 
45 
47 {
48  return new RenderGridsToKinemage;
49 }
50 
52 {
53  return "RenderGridsToKinemage";
54 }
55 
57  core::Real const & lower,
58  core::Real const & upper,
59  std::string const & name)
60 {
61  color_value = value;
62  lower_bound = lower;
63  upper_bound = upper;
64  color_name = name;
65 }
66 
68  protocols::moves::Mover("RenderGridsToKinemage"),
69  color_mode_(0),
70  gradient_bins_(10),
71  stride_(1)
72  //low_cut_(0.0),
73  //high_cut_(0.0)
74 {
75 
76 }
77 
79  RenderGridsToKinemage const & mover
80  ) :
81  protocols::moves::Mover(mover),
82  filename_(mover.filename_),
83  color_mode_(mover.color_mode_),
84  gradient_bins_(mover.gradient_bins_),
85  stride_(mover.stride_),
86  grid_name_(mover.grid_name_),
87  color_(mover.color_),
88  low_color_(mover.low_color_),
89  zero_color_(mover.zero_color_),
90  high_color_(mover.high_color_),
91  color_data_(mover.color_data_),
92  grid_(mover.grid_)
93 {
94 
95 }
96 
98 {
99  //
100 }
101 
103 {
104  return new RenderGridsToKinemage(*this);
105 }
106 
108 {
109  return "RenderGridsToKinemage";
110 }
111 
112 
114 {
115 
116  setup_colors();
117 
118  //we can write multiple grids to one file, peek at the first line and see if the header has been written
119  utility::io::izstream infile;
120  infile.open(filename_);
121  std::string first_word;
122  infile >> first_word;
123  infile.close();
124 
125  utility::io::ozstream outfile;
126  if(first_word == "@kinemage")
127  {
128  //header already written, append to existing file, don't write the header
129  outfile.open(filename_,std::ios::app);
130  }else
131  {
132  //header not written, open for overwrite, write the header
133  outfile.open(filename_,std::ios::out);
134  write_header(outfile);
135  }
136  write_colors(outfile);
137  write_points(outfile);
138  outfile.close();
139 
140 }
141 
143  moves::DataMap &,
144  filters::Filters_map const &,
146  core::pose::Pose const &
147 )
148 {
149 
150  if(!tag->hasOption("grid_name"))
151  {
152  throw utility::excn::EXCN_RosettaScriptsOption("RenderGridsToKinemage requires the'grid name' option");
153  }
154 
155  grid_name_ = tag->getOption<std::string>("grid_name");
157  if( ! grid_ )
158  {
159  throw utility::excn::EXCN_RosettaScriptsOption("RenderGridsToKinemage is currently unable to output the contents of a metagrid. Sorry.");
160  }
161 
162  if(!tag->hasOption("file_name"))
163  {
164  throw utility::excn::EXCN_RosettaScriptsOption("RenderGridsToKinemage requires the 'file_name' option");
165  }
166  filename_ = tag->getOption<std::string>("file_name");
167 
168  if(!tag->hasOption("low_color") && !tag->hasOption("zero_color") && !tag->hasOption("high_color"))
169  {
170  if(tag->hasOption("color"))
171  {
172  //one color
173  color_mode_ = 1;
174  color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("color"));
175 
176  }else
177  {
178  throw utility::excn::EXCN_RosettaScriptsOption("To use RenderGridsToKinemage you must specify color for a 1 color grid plot, "
179  "high_color and low_color for a two color gradient, or high_color, low_color and "
180  "zero_color for a three color gradient centered at zero");
181  }
182  }else if(tag->hasOption("low_color") && tag->hasOption("zero_color") && tag->hasOption("high_color"))
183  {
184  //3 part gradient
185  color_mode_ = 3;
186  low_color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("low_color"));
187  zero_color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("zero_color"));
188  high_color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("high_color"));
189 
190  }else if(tag->hasOption("low_color")&& tag->hasOption("high_color"))
191  {
192  //2 part gradient
193  color_mode_ = 2;
194  low_color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("low_color"));
195  high_color_ = numeric::comma_seperated_string_to_xyz<core::Real>(tag->getOption<std::string>("high_color"));
196  }else
197  {
198  throw utility::excn::EXCN_RosettaScriptsOption("To use RenderGridsToKinemage you must specify color for a 1 color grid plot, "
199  "high_color and low_color for a two color gradient, or high_color, low_color and "
200  "zero_color for a three color gradient centered at zero");
201  }
202 
203  gradient_bins_ = tag->getOption<core::Size>("gradient_bins",10);
204  stride_ = tag->getOption<core::Size>("stride",2);
205  std::cout << stride_ <<std::endl;
206 
207 }
208 
209 
211 {
212  if(color_mode_ == 1)
213  {
215  }else if(color_mode_ == 2)
216  {
218  }else if(color_mode_ == 3)
219  {
221  }else
222  {
223  assert(false); //This should never happen
224  }
225 
226 }
227 
229 {
230  core::Real grid_min =grid_->get_min_value();
231  core::Real grid_max = grid_->get_max_value();
232 
233  ColorGradient new_color(color_,grid_min,grid_max,grid_name_+"_color");
234  color_data_.push_back(new_color);
235 }
236 
238 {
239  core::Real grid_min = grid_->get_min_value();
240  core::Real grid_max = grid_->get_max_value();
241 
242  core::Real red_step = (high_color_.x() - low_color_.x())/static_cast<core::Real>(gradient_bins_);
243  core::Real green_step = (high_color_.y() - low_color_.y())/static_cast<core::Real>(gradient_bins_);
244  core::Real blue_step = (high_color_.z() - low_color_.z())/static_cast<core::Real>(gradient_bins_);
245 
246  core::Real value_step = (grid_max - grid_min)/gradient_bins_;
247 
248 
249  core::Vector current_color = low_color_;
250  core::Real current_low = grid_min;
251  core::Real current_high = current_low+value_step;
252  for(core::Size bin = 1; bin <= gradient_bins_; ++bin)
253  {
254  current_color.x(current_color.x()+red_step);
255  current_color.y(current_color.y()+green_step);
256  current_color.z(current_color.z()+blue_step);
257  std::string color_bin_string(utility::to_string<core::Size>(bin));
258  std::string color_name(grid_name_+"_"+color_bin_string+"_color");
259  ColorGradient new_color(current_color,current_low,current_high,color_name);
260  color_data_.push_back(new_color);
261  current_low = current_high;
262  current_high = current_low+value_step;
263  }
264 
265 }
266 
268 {
269  core::Real grid_min = grid_->get_min_value();
270  core::Real grid_max = grid_->get_max_value();
271  if(grid_min >= 0)
272  {
273  utility_exit_with_message("This grid does not have any scores greater than 0, a three color gradient makes no sense here");
274  }
275 
276  core::Size half_bins = gradient_bins_/2;
277 
278  core::Real red_step_low = (zero_color_.x()-low_color_.x())/static_cast<core::Real>(half_bins);
279  core::Real blue_step_low = (zero_color_.y()-low_color_.y())/static_cast<core::Real>(half_bins);
280  core::Real green_step_low = (zero_color_.z()-low_color_.z())/static_cast<core::Real>(half_bins);
281 
282  core::Real red_step_high = (high_color_.x()-zero_color_.x())/static_cast<core::Real>(half_bins);
283  core::Real blue_step_high = (high_color_.y()-zero_color_.y())/static_cast<core::Real>(half_bins);
284  core::Real green_step_high = (high_color_.z()-zero_color_.z())/static_cast<core::Real>(half_bins);
285 
286  core::Real low_value_step = (-grid_min)/gradient_bins_;
287  core::Real high_value_step = (grid_max)/gradient_bins_;
288 
289  core::Vector current_color = low_color_;
290  core::Real current_low = grid_min;
291  core::Real current_high = current_low+low_value_step;
292  //set up colors between low_color and zero
293  for(core::Size bin = 1; bin <= half_bins; ++bin)
294  {
295  current_color.x(current_color.x()+red_step_low);
296  current_color.y(current_color.y()+green_step_low);
297  current_color.z(current_color.z()+blue_step_low);
298  std::string color_bin_string(utility::to_string<core::Size>(bin));
299  std::string color_name(grid_name_+"_"+color_bin_string+"_color");
300  ColorGradient new_color(current_color,current_low,current_high,color_name);
301  color_data_.push_back(new_color);
302  current_low = current_high;
303  current_high = current_low+low_value_step;
304  }
305 
306 
307  //set up colors between zero and high color
308  current_color = zero_color_;
309  current_low = 0.0;
310  current_high = current_low+high_value_step;
311 
312  for(core::Size bin = 1; bin <= half_bins;++bin)
313  {
314  current_color.x(current_color.x()+red_step_high);
315  current_color.y(current_color.y()+green_step_high);
316  current_color.z(current_color.z()+blue_step_high);
317  std::string color_bin_string(utility::to_string<core::Size>(bin));
318  ColorGradient new_color(current_color,current_low,current_high,grid_name_+"_"+color_bin_string+"_color");
319  color_data_.push_back(new_color);
320  current_low = current_high;
321  current_high = current_low+high_value_step;
322  }
323 
324 }
325 
326 void RenderGridsToKinemage::write_points(utility::io::ozstream & kin_file)
327 {
328  //setup grid master
329  std::string master_name = grid_name_;
330  kin_file << "@master {" << master_name << "}" <<std::endl;
331  //setup dotlist
332  kin_file << "@dotlist {" << master_name << " dots} color=white master={" <<master_name << "}" <<std::endl;
333 
334  //loop through each color in color_data
335  for(core::Size color_index = 1; color_index <= color_data_.size();++color_index)
336  {
337  //write dot for each point in color_range
338  ColorGradient current_color(color_data_[color_index]);
339  std::list<std::pair<core::Vector, core::Real> > point_list(
340  grid_->get_point_value_list_within_range(current_color.lower_bound,current_color.upper_bound,stride_));
341 
342  std::list<std::pair<core::Vector, core::Real> >::iterator point_iterator = point_list.begin();
343  for(; point_iterator != point_list.end(); ++point_iterator)
344  {
345  core::Vector coords(point_iterator->first);
346  core::Real value(point_iterator->second);
347 
348  //if it is a 1 color grid, don't output zero points
349  if( !(color_mode_ = 1 && value == 0) )
350  {
351  kin_file <<"{" << master_name << "_points} " << current_color.color_name << " " <<
352  coords.x() << " " << coords.y() << " " << coords.z() <<std::endl;
353  }
354  }
355  }
356 
357 }
358 
359 void RenderGridsToKinemage::write_colors(utility::io::ozstream & kin_file)
360 {
361  for(core::Size color_index = 1; color_index <= color_data_.size();++color_index)
362  {
363  ColorGradient current_color(color_data_[color_index]);
364  //we asked the user to specify in RGB, but the kinemage needs to be in HSV
365  core::Vector hsv_color(numeric::rgb_to_hsv(current_color.color_value));
366  kin_file << "@hsvcolor {" << current_color.color_name << "} " <<
367  hsv_color.x() << " "<< hsv_color.y()*100 << " " << hsv_color.z()*100 << std::endl;
368  }
369 }
370 
371 void RenderGridsToKinemage::write_header(utility::io::ozstream & kin_file)
372 {
373  kin_file << "@kinemage 1" <<std::endl;
374  kin_file << "@group dominant {dots}" <<std::endl;
375 }
376 
377 }
378 }
379 
380