Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SingleResidueDunbrackLibrary.tmpl.hh
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
11 /// @brief
12 /// @author
13 
14 #ifndef INCLUDED_core_pack_dunbrack_SingleResidueDunbrackLibrary_tmpl_hh
15 #define INCLUDED_core_pack_dunbrack_SingleResidueDunbrackLibrary_tmpl_hh
16 
17 // Unit Headers
19 
20 // Package Headers
21 // AUTO-REMOVED #include <core/pack/dunbrack/DunbrackRotamer.hh>
22 
23 // Project Headers
24 #include <core/chemical/AA.hh>
25 
26 // C++ Headers
27 #include <algorithm>
28 #include <iostream>
29 
30 //Auto Headers
31 #include <platform/types.hh>
32 #include <core/types.hh>
34 //XRW_B_T1
35 //#include <core/coarse/Translator.fwd.hh>
36 //XRW_E_T1
38 #include <core/graph/Graph.fwd.hh>
40 #include <core/pose/Pose.fwd.hh>
48 #include <utility/assert.hh>
49 #include <utility/down_cast.hh>
50 #include <utility/fixedsizearray1.fwd.hh>
51 #include <utility/vector1.fwd.hh>
52 #include <utility/vector1_bool.hh>
53 #include <utility/io/izstream.fwd.hh>
54 #include <utility/io/ozstream.fwd.hh>
55 #include <utility/pointer/ReferenceCount.fwd.hh>
56 #include <utility/pointer/ReferenceCount.hh>
57 #include <utility/pointer/access_ptr.fwd.hh>
58 #include <utility/pointer/access_ptr.hh>
59 #include <utility/pointer/owning_ptr.functions.hh>
60 #include <utility/pointer/owning_ptr.fwd.hh>
61 #include <utility/pointer/owning_ptr.hh>
62 #include <numeric/numeric.functions.hh>
63 #include <numeric/xyzVector.fwd.hh>
64 #include <numeric/random/random.fwd.hh>
65 #include <cassert>
66 #include <cmath>
67 #include <cstddef>
68 #include <iosfwd>
69 #include <iterator>
70 #include <limits>
71 #include <map>
72 #include <ostream>
73 
74 
75 // Boost Headers
76 
77 namespace core {
78 namespace pack {
79 namespace dunbrack {
80 
81 /*
82 
83 /// DO NOT USE THIS CONSTRUCTOR
84 template < class T >
85 SingleResidueDunbrackLibraryConcrete< T >::SingleResidueDunbrackLibraryConcrete()
86 :
87  SingleResidueDunbrackLibrary( chemical::aa_ala, 1, 1, false )
88 {}
89 
90 
91 template < class T >
92 SingleResidueDunbrackLibraryConcrete< T >::SingleResidueDunbrackLibraryConcrete(
93  chemical::AA const aa_in,
94  Size const nchi_aa_in,
95  Size const nrots_per_bin,
96  bool bClassicRotvectorAssignment
97 )
98 :
99  SingleResidueDunbrackLibrary( aa_in, nchi_aa_in, nrots_per_bin, bClassicRotvectorAssignment ),
100  rotset_( nrots_per_bin, nbins(), nbins() ), // column major ordering of FArrays
101  max_rotprob_( nbins(), nbins(), (DunbrackReal) 0 )
102 {}
103 
104 template < class T >
105 void
106 SingleResidueDunbrackLibraryConcrete< T >::set_chi_mean(
107  Size phi_bin,
108  Size psi_bin,
109  Size which_rotamer,
110  Size which_chi,
111  DunbrackReal chi_mean_in )
112 {
113  rotset_( which_rotamer, psi_bin, phi_bin ).chi_mean( which_chi, chi_mean_in );
114 }
115 
116 template < class T >
117 void
118 SingleResidueDunbrackLibraryConcrete< T >::set_chi_sd(
119  Size phi_bin,
120  Size psi_bin,
121  Size which_rotamer,
122  Size which_chi,
123  DunbrackReal chi_sd_in
124 )
125 {
126  rotset_( which_rotamer, psi_bin, phi_bin ).chi_sd( which_chi, chi_sd_in );
127 }
128 
129 template < class T >
130 void
131 SingleResidueDunbrackLibraryConcrete< T >::set_rotnum(
132  Size phi_bin,
133  Size psi_bin,
134  Size which_rotamer,
135  Size which_chi,
136  Size rotnum_in )
137 {
138  rotset_( which_rotamer, psi_bin, phi_bin ).rotnum( which_chi, rotnum_in );
139 }
140 
141 template < class T >
142 void
143 SingleResidueDunbrackLibraryConcrete< T >::set_rotprob(
144  Size phi_bin,
145  Size psi_bin,
146  Size which_rotamer,
147  DunbrackReal rotprob_in
148 )
149 {
150  //std::cout << "set rotprob: " << T::size << " rotno: ";
151  //for ( Size ii = 1; ii <= T::size; ++ii ) {
152  // std::cout << rotset_( phi_bin, psi_bin, which_rotamer ).rotnum( ii ) << " ";
153  //}
154  //std::cout << std::endl;
155  rotset_( which_rotamer, psi_bin, phi_bin ).rotamer_probability( rotprob_in );
156  max_rotprob_( psi_bin, phi_bin ) = std::max( rotprob_in, max_rotprob_( psi_bin, phi_bin ) );
157 }
158 
159 template < class T >
160 DunbrackReal
161 SingleResidueDunbrackLibraryConcrete< T >::get_max_rotprob(
162  Size phi_bin,
163  Size psi_bin
164 ) const
165 {
166  return max_rotprob_( psi_bin, phi_bin );
167 }
168 
169 template < class T >
170 DunbrackRotamer< FOUR >
171 SingleResidueDunbrackLibraryConcrete< T >::find_rotamer(
172  Size const phi_bin,
173  Size const psi_bin,
174  RotVector const & rot
175 ) const
176 {
177  Size correct_rot = 0;
178  // replace this with templated functions
179  for ( Size ii = 1; ii <= nrots_per_bin(); ++ii ) {
180  bool all_match = true;
181  for ( Size jj = 1; jj <= T::size; ++jj ) {
182  if ( rotset_( ii, psi_bin, phi_bin ).rotnum( jj ) != (Size) rot[ jj ] ) {
183  all_match = false;
184  break;
185  }
186  }
187  if ( all_match ) { correct_rot = ii; break; }
188  }
189 
190 
191  if ( correct_rot == 0 ) {
192 
193  std::cerr << "Failed to find rotamer: ";
194  for ( Size ii = 1; ii <= T::size; ++ii ) {
195  std::cerr << rot[ ii ] << " ";
196  }
197  std::cerr << std::endl << "Amongst options: ";
198  for ( Size ii = 1; ii <= nrots_per_bin(); ++ii ) {
199  for ( Size jj = 1; jj <= T::size; ++jj ) {
200  std::cerr << rotset_( ii, psi_bin, phi_bin ).rotnum( jj ) << " ";
201  }
202  std::cerr << std::endl;
203  }
204  utility_exit();
205  }
206 
207  return get_rotamer( phi_bin, psi_bin, correct_rot );
208 }
209 
210 template < class T >
211 DunbrackRotamer< FOUR >
212 SingleResidueDunbrackLibraryConcrete< T >::find_rotamer(
213  Size const phi_bin,
214  Size const psi_bin,
215  DunbrackRotamer< FOUR > const & dunrot
216 ) const
217 {
218  Size correct_rot = 0;
219  // replace this with templated functions
220  for ( Size ii = 1; ii <= nrots_per_bin(); ++ii ) {
221  bool all_match = true;
222  for ( Size jj = 1; jj <= T::size; ++jj ) {
223  if ( rotset_( ii, psi_bin, phi_bin ).rotnum( jj ) != dunrot.rotnum( jj ) ) {
224  all_match = false;
225  break;
226  }
227  }
228  if ( all_match ) { correct_rot = ii; break; }
229  }
230 
231  if ( correct_rot == 0 ) {
232  std::cerr << "Failed to find rotamer: ";
233  for ( Size ii = 1; ii <= T::size; ++ii ) {
234  std::cerr << dunrot.rotnum( ii ) << " ";
235  }
236  std::cerr << std::endl << "Amongst options: ";
237  for ( Size ii = 1; ii <= nrots_per_bin(); ++ii ) {
238  for ( Size jj = 1; jj <= T::size; ++jj ) {
239  std::cerr << rotset_( ii, psi_bin, phi_bin ).rotnum( jj ) << " ";
240  }
241  std::cerr << std::endl;
242  }
243  utility_exit();
244  }
245 
246  return get_rotamer( phi_bin, psi_bin, correct_rot );
247 }
248 
249 template < class T >
250 DunbrackRotamer< FOUR >
251 SingleResidueDunbrackLibraryConcrete< T >::retrieve_rotamer(
252  Size const phi_bin,
253  Size const psi_bin,
254  Size const which_rotamer
255 ) const
256 {
257  return get_rotamer( phi_bin, psi_bin, which_rotamer );
258 }
259 
260 
261 template < class T >
262 DunbrackRotamer< FOUR >
263 SingleResidueDunbrackLibraryConcrete< T >::get_rotamer(
264  Size const phi_bin,
265  Size const psi_bin,
266  Size const which_rotamer
267 ) const
268 {
269  DunbrackRotamer< FOUR > rotamer;
270 
271  // replace this with templated functions
272  for ( Size jj = 1; jj <= T::size; ++jj ) {
273  rotamer.chi_mean( jj ) = rotset_( which_rotamer, psi_bin, phi_bin ).chi_mean( jj );
274  rotamer.chi_sd( jj ) = rotset_( which_rotamer, psi_bin, phi_bin ).chi_sd( jj );
275  rotamer.rotnum( jj ) = rotset_( which_rotamer, psi_bin, phi_bin ).rotnum( jj );
276  }
277  rotamer.rotamer_probability() = rotset_( which_rotamer, psi_bin, phi_bin ).rotamer_probability();
278  return rotamer;
279 }
280 
281 template < class T >
282 void
283 SingleResidueDunbrackLibraryConcrete< T >::write_to_binary(
284  std::ofstream &out
285 ) const
286 {
287  using namespace boost;
288 
289  // 1. figure out how large each of the tables has to be
290  // 2. allocate the c-style tables, fill them with data
291  // 3. write the c-style tables in binary
292  // 4. delete the c-style tables
293 
294  // 1.
295  Size const nphipsibins = nbins() * nbins();
296  Size nrotamers = nrots_per_bin();
297  Size const rotamer_entries_needed = nphipsibins * nrotamers;
298  Size const chi_entries_needed = rotamer_entries_needed * T::size;
299 
300  //SRDL_TR << "Writing " << nrotamers << " rotamers, with " << nchi_aa_ << " chi angles for " << aa_ << std::endl;
301 
302  // 2.
303  DunbrackReal * prob_entries = new DunbrackReal[ rotamer_entries_needed ];
304  boost::int32_t * rot_entries = new boost::int32_t[ chi_entries_needed ];
305  DunbrackReal * chi_mean_entries = new DunbrackReal[ chi_entries_needed ];
306  DunbrackReal * chi_sdev_entries = new DunbrackReal[ chi_entries_needed ];
307 
308  Size count_rots( 0 ), count_chis( 0 );
309  for ( Size phi_bin = 1; phi_bin <= nbins(); phi_bin++ ) {
310  for ( Size psi_bin = 1; psi_bin <= nbins(); psi_bin++ ) {
311  for ( Size rot_id = 1; rot_id <= nrotamers; ++rot_id ) {
312  DunbrackRotamer< T > const & rotamer = rotset_( rot_id, psi_bin, phi_bin );
313  assert( count_rots < rotamer_entries_needed );
314  prob_entries[ count_rots ] = rotamer.rotamer_probability();
315  for ( Size chi_id = 1; chi_id <= T::size; ++chi_id ) {
316  assert( count_chis < chi_entries_needed );
317  rot_entries[ count_chis ] = rotamer.rotnum( chi_id );
318  chi_mean_entries[ count_chis ] = rotamer.chi_mean( chi_id );
319  chi_sdev_entries[ count_chis ] = rotamer.chi_sd( chi_id );
320  ++count_chis;
321  }
322  ++count_rots;
323  }
324  }
325  }
326 
327  // 3.
328  out.write( (char*) prob_entries, sizeof( DunbrackReal ) * rotamer_entries_needed );
329  out.write( (char*) rot_entries, sizeof( boost::int32_t ) * chi_entries_needed );
330  out.write( (char*) chi_mean_entries, sizeof( DunbrackReal ) * chi_entries_needed );
331  out.write( (char*) chi_sdev_entries, sizeof( DunbrackReal ) * chi_entries_needed );
332 
333  // 4.
334  delete [] prob_entries;
335  delete [] rot_entries;
336  delete [] chi_mean_entries;
337  delete [] chi_sdev_entries;
338 
339 }
340 
341 
342 template < class T >
343 void
344 SingleResidueDunbrackLibraryConcrete< T >::read_from_binary(
345  std::ifstream &in
346 )
347 {
348  using namespace boost;
349 
350  // 1. figure out how large each of the tables has to be
351  // 2. allocate the c-style tables, read the binary file into the c-style tables
352  // 3. store data from the c-style tables in the 3-D FArray
353  // 4. delete the c-style tables
354 
355  // 1.
356  Size nrotamers( nrots_per_bin() );
357 
358  Size const nphipsibins = nbins() * nbins();
359  Size const rotamer_entries_needed = nphipsibins * nrotamers;
360  Size const chi_entries_needed = rotamer_entries_needed * T::size;
361 
362  //SRDL_TR << "Reading " << nrotamers << " rotamers, with " << nchi_aa_ << " chi angles for " << aa_ << std::endl;
363 
364 
365  // 2.
366  DunbrackReal * prob_entries = new DunbrackReal[ rotamer_entries_needed ];
367  boost::int32_t * rot_entries = new boost::int32_t[ chi_entries_needed ];
368  DunbrackReal * chi_mean_entries = new DunbrackReal[ chi_entries_needed ];
369  DunbrackReal * chi_sdev_entries = new DunbrackReal[ chi_entries_needed ];
370 
371  in.read( (char*) prob_entries, sizeof( DunbrackReal ) * rotamer_entries_needed );
372  in.read( (char*) rot_entries, sizeof( boost::int32_t ) * chi_entries_needed );
373  in.read( (char*) chi_mean_entries, sizeof( DunbrackReal ) * chi_entries_needed );
374  in.read( (char*) chi_sdev_entries, sizeof( DunbrackReal ) * chi_entries_needed );
375 
376  // 3.
377  Size count_rots( 0 ), count_chis( 0 );
378  for ( Size phi_bin = 1; phi_bin <= nbins(); phi_bin++ ) {
379  for ( Size psi_bin = 1; psi_bin <= nbins(); psi_bin++ ) {
380  for ( Size rot_id = 1; rot_id <= nrotamers; ++rot_id ) {
381  assert( count_rots < rotamer_entries_needed );
382 
383  DunbrackRotamer< T > & rotamer( rotset_( rot_id, psi_bin, phi_bin ));
384  //rotamer.rotamer_probability() = prob_entries[ count_rots ];
385  set_rotprob(phi_bin, psi_bin, rot_id, prob_entries[ count_rots ]);
386  for ( Size chi_id = 1; chi_id <= T::size; ++chi_id ) {
387  assert( count_chis < chi_entries_needed );
388  rotamer.rotnum( chi_id ) = rot_entries[ count_chis ];
389  rotamer.chi_mean( chi_id ) = chi_mean_entries[ count_chis ];
390  rotamer.chi_sd( chi_id ) = chi_sdev_entries[ count_chis ];
391  ++count_chis;
392  }
393  ++count_rots;
394  }
395  }
396  }
397 
398  // 4.
399  delete [] prob_entries;
400  delete [] rot_entries;
401  delete [] chi_mean_entries;
402  delete [] chi_sdev_entries;
403 
404 
405 }
406 
407 */
408 
409 } // namespace dunbrack
410 } // namespace scoring
411 } // namespace core
412 
413 
414 #endif