14 #ifndef INCLUDED_core_pack_dunbrack_SingleResidueDunbrackLibrary_tmpl_hh
15 #define INCLUDED_core_pack_dunbrack_SingleResidueDunbrackLibrary_tmpl_hh
31 #include <platform/types.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>
85 SingleResidueDunbrackLibraryConcrete< T >::SingleResidueDunbrackLibraryConcrete()
87 SingleResidueDunbrackLibrary( chemical::aa_ala, 1, 1, false )
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
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 )
106 SingleResidueDunbrackLibraryConcrete< T >::set_chi_mean(
111 DunbrackReal chi_mean_in )
113 rotset_( which_rotamer, psi_bin, phi_bin ).chi_mean( which_chi, chi_mean_in );
118 SingleResidueDunbrackLibraryConcrete< T >::set_chi_sd(
123 DunbrackReal chi_sd_in
126 rotset_( which_rotamer, psi_bin, phi_bin ).chi_sd( which_chi, chi_sd_in );
131 SingleResidueDunbrackLibraryConcrete< T >::set_rotnum(
138 rotset_( which_rotamer, psi_bin, phi_bin ).rotnum( which_chi, rotnum_in );
143 SingleResidueDunbrackLibraryConcrete< T >::set_rotprob(
147 DunbrackReal rotprob_in
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 ) << " ";
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 ) );
161 SingleResidueDunbrackLibraryConcrete< T >::get_max_rotprob(
166 return max_rotprob_( psi_bin, phi_bin );
170 DunbrackRotamer< FOUR >
171 SingleResidueDunbrackLibraryConcrete< T >::find_rotamer(
174 RotVector const & rot
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 ] ) {
187 if ( all_match ) { correct_rot = ii; break; }
191 if ( correct_rot == 0 ) {
193 std::cerr << "Failed to find rotamer: ";
194 for ( Size ii = 1; ii <= T::size; ++ii ) {
195 std::cerr << rot[ ii ] << " ";
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 ) << " ";
202 std::cerr << std::endl;
207 return get_rotamer( phi_bin, psi_bin, correct_rot );
211 DunbrackRotamer< FOUR >
212 SingleResidueDunbrackLibraryConcrete< T >::find_rotamer(
215 DunbrackRotamer< FOUR > const & dunrot
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 ) ) {
228 if ( all_match ) { correct_rot = ii; break; }
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 ) << " ";
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 ) << " ";
241 std::cerr << std::endl;
246 return get_rotamer( phi_bin, psi_bin, correct_rot );
250 DunbrackRotamer< FOUR >
251 SingleResidueDunbrackLibraryConcrete< T >::retrieve_rotamer(
254 Size const which_rotamer
257 return get_rotamer( phi_bin, psi_bin, which_rotamer );
262 DunbrackRotamer< FOUR >
263 SingleResidueDunbrackLibraryConcrete< T >::get_rotamer(
266 Size const which_rotamer
269 DunbrackRotamer< FOUR > rotamer;
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 );
277 rotamer.rotamer_probability() = rotset_( which_rotamer, psi_bin, phi_bin ).rotamer_probability();
283 SingleResidueDunbrackLibraryConcrete< T >::write_to_binary(
287 using namespace boost;
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
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;
300 //SRDL_TR << "Writing " << nrotamers << " rotamers, with " << nchi_aa_ << " chi angles for " << aa_ << std::endl;
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 ];
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 );
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 );
334 delete [] prob_entries;
335 delete [] rot_entries;
336 delete [] chi_mean_entries;
337 delete [] chi_sdev_entries;
344 SingleResidueDunbrackLibraryConcrete< T >::read_from_binary(
348 using namespace boost;
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
356 Size nrotamers( nrots_per_bin() );
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;
362 //SRDL_TR << "Reading " << nrotamers << " rotamers, with " << nchi_aa_ << " chi angles for " << aa_ << std::endl;
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 ];
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 );
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 );
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 ];
399 delete [] prob_entries;
400 delete [] rot_entries;
401 delete [] chi_mean_entries;
402 delete [] chi_sdev_entries;