Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpartaUtil.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 /* */
3 /* ---- SPARTA ---- */
4 /* Shifts Prediction from Analogue of Residue type and Torsion Angle */
5 /* Yang Shen and Ad Bax */
6 /* J. Biomol. NMR, xx, xxx-xxx (2010) */
7 /* NIH, NIDDK, Laboratory of Chemical Physics */
8 /* version, 1.00 (build 2010.0607.00) */
9 /* */
10 /* for any problem, please contact */
11 /* shenyang@niddk.nih.gov */
12 /* */
13 /******************************************************************************/
14 /// modified for use inside CS-Rosetta by Oliver Lange
15 ///
16 // vi: set ts=2 noet:
17 //
18 // (c) Copyright Rosetta Commons Member Institutions.
19 // (c) This file is part of the Rosetta software suite and is made available under license.
20 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
21 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
22 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
23 
24 /// @author James Thompson
25 
26 // Unit Headers
27 #include <boost/unordered_map.hpp>
29 // AUTO-REMOVED #include <protocols/sparta/constants.hh>
30 // AUTO-REMOVED #include <protocols/sparta/util.hh>
31 
32 #include <core/types.hh>
33 
34 #include <basic/Tracer.hh>
35 #include <utility/vector0.hh>
36 #include <utility/vector1.hh>
37 #include <ObjexxFCL/string.functions.hh>
38 
39 // AUTO-REMOVED #include <cstdlib>
40 #include <cstdio>
41 #include <string>
42 // AUTO-REMOVED #include <cmath>
43 
44 // AUTO-REMOVED #include <iostream>
45 
46 #ifdef WIN32
47 #include <direct.h>
48 #else
49 #include <dirent.h>
50 #include <sys/stat.h>
51 
52 //Auto Headers
53 #endif
54 static basic::Tracer tr("protocols.sparta");
55 
56 namespace protocols {
57 namespace sparta {
58 
59 using namespace core;
60 using namespace std;
61 
62 void
64  boost::unordered_map< int, std::string > & atom_names,
65  GDB & Pred_Sum,
66  GDB & REF_CS_Tab,
67  GDB & COMP_Tab,
68  utility::vector1< float > & per_residue_scores
69 ) {
70  using boost::unordered_map;
71 
72  COMP_Tab.REMARKS = Pred_Sum.REMARKS;
73  COMP_Tab.DATA = Pred_Sum.DATA;
74 
75  // to compute chemshift score take CS_DIFF^2 / SIGMA ^2
76 
77  COMP_Tab.VARS_str_parser(" RESID RESNAME ATOMNAME CS_OBS SHIFT RC_SHIFT CS_DIFF SIGMA W");
78  COMP_Tab.FORMAT_str_parser(" %4d %4s %4s %9.3f %9.3f %9.3f %9.3f %9.3f %.2f");
79 
80  boost::unordered_map< int, string >::iterator itN;
81  boost::unordered_map< int, boost::unordered_map< string, string > >::iterator it;
82 
83  //std::cout << "begin_scoring: scores.size() = " << per_residue_scores.size() << std::endl;
84 
85  utility::vector0< float > OBS_V, PRED_V, DIFF_V, OBS_V_CORRECTED;
86  for ( itN = atom_names.begin(); itN != atom_names.end(); itN++ ) {
87  string aName = itN->second;
88  if ( aName == "H" ) aName="HN";
89  bool floating_sign( REF_CS_Tab.isVarFloat("SHIFT2") );
90  if ( floating_sign ) tr.Info << " use floating sign1 " << std::endl;
91  else tr.Info << " no floating sign " << std::endl;
92  for ( it = REF_CS_Tab.Entries.begin(); it != REF_CS_Tab.Entries.end(); it++ ) {
93  float obs_shift, pred_shift, obs_shift2( 0.0 );
94  string aName_ref = it->second["ATOMNAME"];
95  if ( aName_ref == "H" ) aName_ref = "HN";
96 
97  if ( aName == "HA" && aName_ref == "G") {
98  if ( aName_ref.find("HA") != 0 ) continue;
99  } else if ( aName_ref != aName ) continue;
100 
101  GDB_Entry temp = Pred_Sum.getEntry("RESID",it->second["RESID"],"ATOMNAME",aName,1);
102 
103  if (temp["SHIFT"].length() <= 0) continue;
104 
105  obs_shift = atof( (it->second["SHIFT"]).c_str() );
106  if ( floating_sign ) obs_shift2 = atof( (it->second["SHIFT2"]).c_str() );
107 
108  pred_shift = atof( (temp["SHIFT"]).c_str() );
109 
110  if ( aName == "HA" && it->second["RESNAME"] == "G") { //for HA of Gly
111  if ( aName_ref == "HA2" ) { // assign HA2 to the one with smaller shift
112  float shift_HA3 = 9999.000;
113  float shift_HA3_2 = 9999;
114  GDB_Entry HA3 = REF_CS_Tab.getEntry("RESID",it->second["RESID"],"ATOMNAME","HA3",1);
115  if ( HA3["ATOMNAME"] == "HA3") shift_HA3 = atof( (HA3["SHIFT"]).c_str() );
116  if ( floating_sign && HA3["ATOMNAME"] == "HA3") shift_HA3_2 = atof( (HA3["SHIFT2"]).c_str() );
117 
118  if ( shift_HA3 < obs_shift) obs_shift = shift_HA3;
119  if ( floating_sign && shift_HA3_2 < obs_shift2 ) obs_shift2 = shift_HA3_2;
120 
121  shift_HA3 = 9999.000;
122  HA3 = Pred_Sum.getEntry("RESID",it->second["RESID"],"ATOMNAME","HA3",1);
123  if ( HA3["ATOMNAME"] == "HA3") shift_HA3 = atof( (HA3["SHIFT"]).c_str() );
124 
125  if ( shift_HA3 < pred_shift) pred_shift = shift_HA3;
126  } else if ( aName_ref == "HA3" ) { // assign HA3 to the one with larger shift
127 
128  float shift_HA2 = -9999.000;
129  float shift_HA2_2 = -9999;
130  GDB_Entry HA2 = REF_CS_Tab.getEntry("RESID",it->second["RESID"],"ATOMNAME","HA2",1);
131  if ( HA2["ATOMNAME"] == "HA2") shift_HA2 = atof( (HA2["SHIFT"]).c_str() );
132  if ( floating_sign && HA2["ATOMNAME"] == "HA2") shift_HA2_2 = atof( (HA2["SHIFT2"]).c_str() );
133 
134  if ( shift_HA2 > obs_shift) obs_shift = shift_HA2;
135  if ( floating_sign && shift_HA2_2 > obs_shift2 ) obs_shift2 = shift_HA2_2;
136 
137  shift_HA2 = -9999.000;
138  HA2 = Pred_Sum.getEntry("RESID",it->second["RESID"],"ATOMNAME","HA2",1);
139  if ( HA2["ATOMNAME"] == "HA2") shift_HA2 = atof( (HA2["SHIFT"]).c_str() );
140 
141  if ( shift_HA2 > pred_shift) pred_shift = shift_HA2;
142  }
143  }
144 
145  if ( pred_shift > 999.0 ) continue;
146 
147  if ( std::fabs( pred_shift-obs_shift ) > std::fabs( pred_shift-obs_shift2 ) ) {
148  obs_shift=obs_shift2;
149  }
150  OBS_V.push_back( obs_shift );
151  PRED_V.push_back( pred_shift );
152  DIFF_V.push_back( pred_shift-obs_shift );
153 
154  Real sigma( atof( (temp["SIGMA"]).c_str() ) );
155  if ( sigma > 0.1 ) {
156  Size const res_id( ObjexxFCL::ulong_of( it->second["RESID"] ) );
157  Real const score(
158  (obs_shift-pred_shift)*(obs_shift-pred_shift)/(sigma*sigma)
159  );
160  //std::cout << "score(" << res_id << "," << aName << ") = " << score << std::endl;
161  //if ( per_residue_scores.size() < res_id ) {
162  // per_residue_scores.resize( res_id, 0.0 );
163  //}
164 
165  // don't run off the end of the per_residue_scores, as the per_residue_scores tell
166  // you how many residues actually exist in your Pose.
167  if ( per_residue_scores.size() > res_id ) {
168  //std::cout << per_residue_scores[res_id] << " += " << score << std::endl;
169  per_residue_scores[res_id] += score;
170  }
171  }
172  } // REF_CS_Tab
173 
174  if ( tr.Info.visible() ) {
175  tr.Info << "RMS(OBS, PRED) for " << aName << " (n=" << OBS_V.size() << "): "
176  << getRMS(OBS_V, PRED_V) << " ppm " << std::endl;
177  }
178 
179  float avg_diff = getAVG(DIFF_V);
180  for( Size i = 0; i< OBS_V.size(); i++) {
181  OBS_V_CORRECTED.push_back( OBS_V[i] + avg_diff );
182  }
183 
184  OBS_V.clear(); PRED_V.clear(); DIFF_V.clear(); OBS_V_CORRECTED.clear();
185  } // for ( itN = atom_names.begin(); itN != atom_names.end(); itN++ )
186 
187  if ( tr.Debug.visible() ) {
188  tr.Debug << " ============== COMP_Tab ==================== " << std::endl;
189  COMP_Tab.showGDB( tr.Debug );
190  tr.Debug << " ============== END COMP_Tab ==================== " << std::endl;
191  tr.Debug << " ============== REF_CS_TAB ==================== " << std::endl;
192  REF_CS_Tab.showGDB( tr.Debug );
193  tr.Debug << " ============== END_CS_TAB ==================== " << std::endl;
194  }
195 }
196 
198  boost::unordered_map< int, std::string > & names,
199  GDB & Pred_Sum,
200  GDB & REF_CS_Tab,
201  GDB & COMP_Tab
202 ) {
203  using utility::vector1;
204  vector1< float > scores( Pred_Sum.Entries.size(), 0.0 );
205  //vector1< float > scores;
206  calc_per_residue_scores( names, Pred_Sum, REF_CS_Tab, COMP_Tab, scores );
207 
209  Real SCORE_SUM(0.0);
210  for ( iter it = scores.begin(), end = scores.end(); it != end; ++it ) {
211  SCORE_SUM += *it;
212  }
213  return SCORE_SUM/4;
214 } // compareRef_fxn
215 
216 float getDiff( float ang1, float ang2 ) {
217  float a = fabs(ang1-ang2);
218 
219  if ( a < 180 ) return a;
220  else if ( a > 180 && a < 360.1 && ang1*ang2 < 0 ) {
221  return 360.0-a;
222  }
223 
224  return 0;
225 }
226 
227 
229  int n = v1.size();
230 
231  float sum = 0.0;
232 
233  for(int i = 0; i < n; i++) {
234  sum += v1[i];
235  }
236 
237  return sum/(float)n;
238 }
239 
241  int n = v1.size();
242  float avg = getAVG(v1);
243 
244  float sum = 0.0;
245 
246  for (int i = 0; i < n; i++) {
247  sum += (v1[i]-avg)*(v1[i]-avg);
248  }
249 
250  return sqrt(sum/(float)(n-1));
251 }
252 
254  if (v1.size() != v2.size() ) return -1.0;
255 
256  int n = v1.size();
257 
258  float sum = 0.0;
259  // using iterators should speed this up ...
260  for(int i = 0; i < n; i++) {
261  sum += ( (v1[i]-v2[i])*(v1[i]-v2[i]) );
262  }
263 
264  return sqrt(sum/(float)(n-1));
265 }
266 
267 
268 char * itoa( int n, char *buff, int /*base*/ )
269 {
270  sprintf(buff, "%d", n);
271  return buff;
272 }
273 
274 
275 
276 char * ftoa( float n, char *buff, char f, int prec )
277 {
278  if ( !(f=='f' || f=='F' || f=='e' || f=='E' || f=='g' || f=='G') ) {
279  f = 'f';
280  }
281  char format[20];
282  char *fs = format; // generate format string
283  *fs++ = '%'; // "%.<prec>l<f>"
284  if ( prec >= 0 ) {
285  if ( prec > 99 ) // buf big enough for precision?
286  prec = 99;
287  *fs++ = '.';
288  if ( prec >= 10 ) {
289  *fs++ = prec / 10 + '0';
290  *fs++ = prec % 10 + '0';
291  } else {
292  *fs++ = prec + '0';
293  }
294  }
295  *fs++ = 'l';
296  *fs++ = f;
297  *fs = '\0';
298  sprintf( buff, format, n );
299 
300  return buff;
301 }
302 
303 int MKDIR(const char *dirName)
304 {
305 #ifndef __native_client__
306 #ifdef WIN32
307  return mkdir(dirName);
308 #else
309  // 777? dangerous ...
310  return mkdir(dirName, 0777);
311 #endif
312 #endif
313 }
314 
315 bool isDirExists(const string &Dir) {
316 #ifdef WIN32
317  char oldDir[100];
318  if ( NULL == getcwd(oldDir, _MAX_PATH) ) return false;
319 
320  if ( chdir( Dir.c_str() ) == -1 ) return false;
321 
322  if ( chdir( oldDir ) == -1 ) return false;
323 
324  return true;
325 #else
326  DIR *dp;
327  dp = opendir(Dir.c_str());
328  if (dp != NULL) {
329  (void) closedir (dp);
330  return true;
331  } else {
332  return false;
333  }
334 #endif
335 }
336 
337 } // namespace sparta
338 } // namespace protocols