Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TailsScoreMover.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 TailsScoreMover.cc
11 ///
12 /// @brief
13 /// @author Monica Berrondo
14 
15 // unit headers
17 
18 // type headers
19 #include <core/types.hh>
20 
21 // project headers
22 #include <basic/options/option.hh>
23 #include <core/pose/Pose.hh>
24 #include <core/pose/util.hh>
25 #include <basic/Tracer.hh>
26 
27 
28 // utility headers
29 #include <utility/file/FileName.hh>
30 
31 #include <basic/options/keys/krassk.OptionKeys.gen.hh>
32 
33 // C++ headers
34 #include <fstream>
35 #include <iostream>
36 #include <string>
37 
38 #include <utility/vector1.hh>
39 
40 using basic::T;
41 using basic::Error;
42 using basic::Warning;
43 
44 static basic::Tracer TR("protocols.simple_moves.TailsScoreMover");
45 using namespace core;
46 using namespace std;
47 
48 namespace protocols {
49 namespace simple_moves {
50 
51 const int VISITED = 1;
52 const int PREVIOUS_HILL = 2;
53 const int HILL = 3;
54 const int VISITED_HILL = 4;
55 const double ED = 2;
56 const double KT = 15;
57 
58 double TailsScoreMover::visit(
59  double in_current_min,
60  int in_current_min_ltail,
61  int in_current_min_rtail,
62  int in_ltail,
63  int in_rtail,
64  int in_array_of_visits[][200],
65  int &out_min_ltail,
66  int &out_min_rtail,
67  int in_sequence_length,
69  pose::Pose & pose,std::ofstream& area_file
70 )
71 {
72  using namespace scoring;
73  using namespace moves;
74  using namespace basic::options;
75  using namespace core::pose;
76 
77  if(in_ltail > in_sequence_length/2 || in_ltail >= 200 || in_rtail > in_sequence_length/2 || in_rtail>= 200)
78  {
79  m_done_all = true;
80  TR<< "done" << std::endl;
81  }
82 
83  if(in_ltail < 0 || in_ltail > in_sequence_length/2 || in_ltail >= 200 || in_rtail < 0 || in_rtail > in_sequence_length/2 || in_rtail>= 200)// check that we are not outside of boundaries
84  {
85  out_min_ltail = in_current_min_ltail;
86  out_min_rtail = in_current_min_rtail;
87  return in_current_min;
88  }
89  if(in_array_of_visits[in_ltail][in_rtail] == VISITED || in_array_of_visits[in_ltail][in_rtail] == VISITED_HILL || in_array_of_visits[in_ltail][in_rtail] ==HILL) //if this spot was already visited or it is a hill
90  {
91  out_min_ltail = in_current_min_ltail;
92  out_min_rtail = in_current_min_rtail;
93  return in_current_min;
94  }
95  make_tail(tail,in_ltail,in_rtail, in_sequence_length);
96  Real tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
97  Real updated_tail_score = tail_score - ED*(in_ltail+in_rtail);
98 
99  //If you are not at the edge check if you are on the hill
100  if(in_ltail!=0 && in_array_of_visits[in_ltail][in_rtail] != PREVIOUS_HILL && in_array_of_visits[in_ltail][in_rtail] != HILL && in_array_of_visits[in_ltail][in_rtail] != VISITED_HILL)
101  {
102  make_tail(tail,in_ltail+1,in_rtail, in_sequence_length);
103  Real up_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail) - ED*(in_ltail+1+in_rtail);
104  make_tail(tail,in_ltail-1,in_rtail, in_sequence_length);
105  Real down_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail) - ED*(in_ltail-1+in_rtail);
106  if(up_updated_tail_score < updated_tail_score && down_updated_tail_score < updated_tail_score )
107  {
108  // TR<< "Hill" << " " << in_ltail<< " " << in_rtail << " " << updated_tail_score << " "<< up_updated_tail_score << " " << down_updated_tail_score << " "<< in_array_of_visits[in_ltail][in_rtail] << std::endl;
109  m_hill_size = m_hill_size + updated_tail_score;
110  m_number_of_hill_points+=1;
111  // TR<< "Updating hill size " <<m_hill_size<<" "<<m_number_of_hill_points << std::endl;
112  in_array_of_visits[in_ltail][in_rtail] = HILL;
113  out_min_ltail = in_current_min_ltail;
114  out_min_rtail = in_current_min_rtail;
115  return in_current_min;
116  }
117  }
118  if(in_rtail!=0 && in_array_of_visits[in_ltail][in_rtail] != PREVIOUS_HILL && in_array_of_visits[in_ltail][in_rtail] != HILL && in_array_of_visits[in_ltail][in_rtail] != VISITED_HILL)
119  {
120  make_tail(tail,in_ltail,in_rtail+1, in_sequence_length);
121  Real right_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail) - ED*(in_ltail+in_rtail+1);
122  make_tail(tail,in_ltail,in_rtail-1, in_sequence_length);
123  Real left_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail) - ED*(in_ltail+in_rtail-1);
124  if(left_updated_tail_score < updated_tail_score && right_updated_tail_score < updated_tail_score)
125  {
126  // TR<< "Hill" << " " << in_ltail<< " " << in_rtail << " " << updated_tail_score << " "<< left_updated_tail_score << " " << right_updated_tail_score << " "<< in_array_of_visits[in_ltail][in_rtail] << std::endl;
127  m_hill_size = m_hill_size + updated_tail_score;
128  m_number_of_hill_points+=1;
129  // TR<< "Updating hill size " <<m_hill_size<<" "<<m_number_of_hill_points << std::endl;
130  in_array_of_visits[in_ltail][in_rtail] = HILL;
131  out_min_ltail = in_current_min_ltail;
132  out_min_rtail = in_current_min_rtail;
133  return in_current_min;
134  }
135  }
136 
137  //Mark this as visited
138  if(in_array_of_visits[in_ltail][in_rtail] != PREVIOUS_HILL && in_array_of_visits[in_ltail][in_rtail] != HILL && in_array_of_visits[in_ltail][in_rtail] != VISITED_HILL)
139  {
140  in_array_of_visits[in_ltail][in_rtail] = VISITED;
141  }
142  if(in_array_of_visits[in_ltail][in_rtail] == PREVIOUS_HILL)
143  {
144  in_array_of_visits[in_ltail][in_rtail] = VISITED_HILL;
145  }
146 
147  area_file<< in_ltail<< " " << in_rtail << " " << updated_tail_score << std::endl;
148  Real min_updated_tail_score = updated_tail_score;
149  out_min_ltail = in_ltail;
150  out_min_rtail = in_rtail;
151 
152  int t_ltail;
153  int t_rtail;
154  updated_tail_score = visit(in_current_min,in_current_min_ltail, in_current_min_rtail, in_ltail-1,in_rtail, in_array_of_visits,t_ltail, t_rtail,in_sequence_length,tail, pose,area_file);
155  if(updated_tail_score < min_updated_tail_score)
156  {
157  out_min_ltail = t_ltail;
158  out_min_rtail = t_rtail;
159  min_updated_tail_score = updated_tail_score;
160  }
161  updated_tail_score = visit(in_current_min, in_current_min_ltail, in_current_min_rtail, in_ltail,in_rtail+1, in_array_of_visits,t_ltail, t_rtail,in_sequence_length,tail, pose,area_file);
162  if(updated_tail_score < min_updated_tail_score)
163  {
164  out_min_ltail = t_ltail;
165  out_min_rtail = t_rtail;
166  min_updated_tail_score = updated_tail_score;
167  }
168  updated_tail_score = visit(in_current_min,in_current_min_ltail, in_current_min_rtail, in_ltail+1,in_rtail, in_array_of_visits,t_ltail, t_rtail,in_sequence_length,tail, pose,area_file);
169  if(updated_tail_score < min_updated_tail_score)
170  {
171  out_min_ltail = t_ltail;
172  out_min_rtail = t_rtail;
173  min_updated_tail_score = updated_tail_score;
174  }
175  updated_tail_score = visit(in_current_min,in_current_min_ltail, in_current_min_rtail, in_ltail,in_rtail-1, in_array_of_visits,t_ltail, t_rtail,in_sequence_length,tail, pose,area_file);
176  if(updated_tail_score < min_updated_tail_score)
177  {
178  out_min_ltail = t_ltail;
179  out_min_rtail = t_rtail;
180  min_updated_tail_score = updated_tail_score;
181  }
182  if( min_updated_tail_score < in_current_min)
183  return min_updated_tail_score;
184  else
185  {
186  return in_current_min;
187  out_min_ltail = in_current_min_ltail;
188  out_min_rtail = in_current_min_rtail;
189  }
190 }
191 void TailsScoreMover::make_tail(utility::vector1< core::Size > & tail,int in_ltaillength, int in_rtaillength, int in_sequence_length)
192 {
193  tail.clear();
194  //populating tail with left tail residues
195  for(int rcount = 1; rcount<=in_ltaillength;rcount++)
196  {
197  tail.push_back(rcount);
198  }
199  //Putting in right tail
200  for(int rcount = 1; rcount < in_rtaillength+1; rcount++)
201  {
202  tail.push_back(in_sequence_length - in_rtaillength+rcount);
203  }
204 }
205 //////////////////////////////////////////////////
206 //Searches through all combinations of tails //
207 //////////////////////////////////////////////////
208 double TailsScoreMover::score_mode1(int& out_min_ltail_length, int& out_min_rtail_length,std::ofstream & in_tail_output, pose::Pose & pose)
209 {
210  TR<< "mode 1" << std::endl;
211  int sequence_length = pose.total_residue(); // Need to get protein sequence length
213  Real min_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
214  Real updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
215  Real min_ltail_length = 0;
216  Real min_rtail_length = 0;
217  int number_of_ltail_steps = sequence_length/2;
218  int number_of_rtail_steps = sequence_length/2;
219  for( int ltaillength = 0; ltaillength < number_of_ltail_steps; ltaillength ++ )
220  {
221  tail.clear();
222  for(int rcount = 1; rcount<=ltaillength;rcount++) //populating tail with left tail residues
223  {
224  tail.push_back(rcount);
225  }
226  for(int rtaillength = 0; rtaillength < number_of_rtail_steps; rtaillength ++)
227  {
228  //removing previous residues
229  for(int rcount = 1; rcount < rtaillength; rcount++)
230  {
231  if(!tail.empty())
232  tail.pop_back();
233  }
234  //adding residues from the right tail
235  for(int rcount = 1; rcount < rtaillength+1; rcount++)
236  {
237  tail.push_back(sequence_length - rtaillength+rcount);
238  }
239  Real tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
240  updated_tail_score = tail_score - ED*(ltaillength+rtaillength);
241  in_tail_output << updated_tail_score << " " << ltaillength << " " << rtaillength <<" "<<tail_score << std::endl;
242  if(updated_tail_score < min_updated_tail_score)
243  {
244  min_updated_tail_score = updated_tail_score;
245  min_ltail_length = ltaillength;
246  min_rtail_length = rtaillength;
247  }
248  }
249  }
250  in_tail_output << " Min updated score " << min_updated_tail_score << " " << min_ltail_length << " " << min_rtail_length << std::endl;
251  in_tail_output.close();
252  out_min_ltail_length = (int)min_ltail_length;
253  out_min_rtail_length = (int)min_rtail_length;
254  return min_updated_tail_score;
255 }
256 
257 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
258 //finds left tail first then right tail //
259 //to stay in local minimum we are going to figure out the number of tail steps as following: //
260 //cut tail until the energy doesn't increase by KT from current min value or reach the middle of protein sequence //
261 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
262 double TailsScoreMover::score_mode2(int& out_min_ltail_length, int& out_min_rtail_length,std::ofstream & in_tail_output, pose::Pose & pose)
263 {
264  TR<< "mode 2" << std::endl;
265  int sequence_length = pose.total_residue(); // Need to get protein sequence length
266  //search for left tail first
267  int number_of_tail_steps = sequence_length/2;
269  Real min_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
270  //Real original_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
271  Real updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
272  Real min_tail_length = 0;
273  Real min_ltail_length = 0;
274  Real min_rtail_length = 0;
275  int taillength = 0;
276 
277  while(updated_tail_score < min_updated_tail_score + KT && taillength < number_of_tail_steps)
278  {
279  tail.push_back(taillength);
280  Real tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
281  updated_tail_score = tail_score - ED*taillength;
282  if(updated_tail_score < min_updated_tail_score)
283  {
284  min_updated_tail_score = updated_tail_score;
285  min_tail_length = taillength;
286  }
287  taillength++;
288  }
289  min_ltail_length = min_tail_length;
290 
291  //Clean the tail vector first
292  tail.clear();
293  //Put in left tail that was found
294  for(int rcount = 1; rcount<=min_ltail_length;rcount++) //populating tail with left tail residues
295  {
296  tail.push_back(rcount);
297  }
298  min_updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
299  //original_tail_score = score_function()->get_sub_score_exclude_res(pose, tail); // set but never used ~Labonte
300  updated_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
301  min_tail_length = 0;
302  //search for right tail now
303  taillength = 0;
304  while(updated_tail_score < min_updated_tail_score + KT && taillength < number_of_tail_steps)
305  {
306  //removing previous residues
307  for(int rcount = 1; rcount < taillength; rcount++)
308  {
309  if(!tail.empty())
310  tail.pop_back();
311  }
312  //adding residues from the right tail
313  for(int rcount = 1; rcount < taillength+1; rcount++)
314  {
315  tail.push_back(sequence_length - taillength+rcount);
316  }
317  Real tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
318  updated_tail_score = tail_score - ED*(min_ltail_length + taillength);
319  if(updated_tail_score < min_updated_tail_score)
320  {
321  min_updated_tail_score = updated_tail_score;
322  min_tail_length = taillength;
323  }
324  taillength++;
325  }
326  min_rtail_length = min_tail_length;
327  in_tail_output << min_updated_tail_score << " " << min_ltail_length << " " << min_rtail_length << std::endl;
328  in_tail_output.close();
329 
330  out_min_ltail_length = (int)min_ltail_length;
331  out_min_rtail_length = (int)min_rtail_length;
332  return min_updated_tail_score;
333 }
334 
335 //Looks for local min
336 double TailsScoreMover::score_mode3(int& out_min_ltail_length, int& out_min_rtail_length,std::ofstream & in_tail_output, pose::Pose & pose)
337 {
338  TR<< "mode 3" << std::endl;
339  int array_of_visits[200][200] = {{0}};
340  int sequence_length = pose.total_residue(); // Need to get protein sequence length
342 
343  int min_ltail_length = 0;
344  int min_rtail_length = 0;
345  Real min_updated_tail_score = 0;
346 
347  double hill_size = 0;
348  m_done_all = false;
349  while(hill_size < KT && !m_done_all)
350  {
351  min_ltail_length = 0;
352  min_rtail_length = 0;
353  core::Real m_hill_size = 0;
354  core::Real m_number_of_hill_points = 0;
355  make_tail(tail, 0,0,sequence_length);
356  double min_current_tail_score = score_function()->get_sub_score_exclude_res(pose, tail);
357  //Real barrier = min_current_tail_score + 2;
358 
359  min_updated_tail_score = visit(min_current_tail_score, 0, 0,
360  0, 0, array_of_visits, min_ltail_length,
361  min_rtail_length, sequence_length, tail, pose,in_tail_output);
362 
363  hill_size = (m_hill_size - min_updated_tail_score*m_number_of_hill_points)/m_number_of_hill_points;
364  // TR<< "Hill size is " << hill_size <<" "<<m_hill_size<<" "<< min_updated_tail_score <<" "<<m_number_of_hill_points<< std::endl;
365 
366  // tail_output_file << " Min updated score " << min_updated_tail_score << " " << min_ltail_length << " " << min_rtail_length << std::endl;
367  //Clean visits array
368  // TR<< "Updating array of visits..." << std::endl;
369  for (int i = 0; i < 200; i++)
370  {
371  for (int j=0; j < 200; j++)
372  {
373  if(array_of_visits [i][j]==HILL)
374  {
375  // TR<< "Hill at" << " " << i<< " " << j << std::endl;
376  array_of_visits [i][j] = PREVIOUS_HILL;
377  }
378  else if(array_of_visits [i][j]==PREVIOUS_HILL || array_of_visits [i][j]==VISITED_HILL)
379  {
380  //TR<< "Hill at" << " " << i<< " " << j << std::endl;
381  array_of_visits [i][j] = PREVIOUS_HILL;
382  }
383  else
384  {
385  array_of_visits [i][j] = 0;
386  }
387  }
388  }
389  }
390  in_tail_output << " Final Min updated score " << min_updated_tail_score << " " << min_ltail_length << " " << min_rtail_length << std::endl;
391  in_tail_output.close();
392 
393  out_min_ltail_length = min_ltail_length;
394  out_min_rtail_length = min_rtail_length;
395  return min_updated_tail_score;
396 }
397 
398 ////////////////////////////////////////////////////////////////////////////////
399 /// @begin apply for ScoreMover
400 ///
401 /// @brief Simply score a pdb
402 void
403 TailsScoreMover::apply( pose::Pose & pose )
404 {
405  using namespace scoring;
406  using namespace moves;
407  using namespace basic::options;
408  using namespace core::pose;
409  //using core::pose::datacache::CacheableDataType::SCORE_MAP;
410 
411  (*score_function())(pose);
412 
413 
414  int tail_mode_name = basic::options::option[ basic::options::OptionKeys::krassk::tail_mode_name ](); // What algorithm to use for tail prediction
415  if(tail_mode_name < 1 || tail_mode_name > 3)
416  {
417  Error() << "No such tail algorithm supported. " << std::endl;
418  }
419 
420  string tail_output_file_name = basic::options::option[ basic::options::OptionKeys::krassk::tail_output_file_name ](); // Name of the tail output file
421  const char * tt = tail_output_file_name.c_str();
422 
423  std::ofstream tail_output_file(tt, std::ios::app );
424  if(!tail_output_file.good())
425  {
426  Error() << "Unable to open tail output file for writing. " << std::endl;
427  }
428 
429  double free_energy = 0;
430  int out_min_ltail_length = 0;
431  int out_min_rtail_length = 0;
432 
433  switch(tail_mode_name)
434  {
435  case 1:
436  {
437  free_energy = score_mode1(out_min_ltail_length, out_min_rtail_length,tail_output_file,pose);
438  break;
439  }
440  case 2:
441  {
442  free_energy = score_mode2(out_min_ltail_length, out_min_rtail_length,tail_output_file,pose);
443  break;
444  }
445  case 3:
446  {
447  free_energy = score_mode3( out_min_ltail_length, out_min_rtail_length,tail_output_file,pose);
448  break;
449  }
450  default:
451  {
452  free_energy = score_mode1(out_min_ltail_length,out_min_rtail_length,tail_output_file,pose);
453  }
454  }
455 
456  setPoseExtraScores( pose, "free_energy", free_energy);
457  setPoseExtraScores( pose, "left_tail", out_min_ltail_length);
458  setPoseExtraScores( pose, "right_tail", out_min_rtail_length);
459 }//apply
460 
462 TailsScoreMover::get_name() const {
463  return "TailsScoreMover";
464 }
465 
466 } // moves
467 } // protocols