Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
serialize_pose.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 /// @buf
11 /// @brief
12 /// @author will sheffler
13 
15 
16 #include <core/pose/Pose.hh>
18 // AUTO-REMOVED #include <core/chemical/VariantType.hh>
23 #include <numeric/xyzVector.hh>
24 
25 // AUTO-REMOVED #include <ObjexxFCL/string.functions.hh>
26 
27 // AUTO-REMOVED #include <cstdio>
28 // AUTO-REMOVED #include <cstring>
29 // AUTO-REMOVED #include <fstream>
30 
31 #include <utility/vector1.hh>
32 #include <sstream>
33 
34 //Auto Headers
36 
37 #define THROW_EXCEPTION(X) {std::cerr << "OVERFLOW ERROR: " << X;return;}
38 
39 namespace core {
40 namespace io {
41 namespace serialization {
42 
43  //const std::string PSEUDORESIDUE_NAME = "VRT";
44 
45  bool
47  {
48  // check the residue name against the pseudoresidue name
49  return (residue.aa() == core::chemical::aa_vrt);
50  }
51 
52 
53  /// Helper function to write all binary files as little endian.
54  /// If the current architecture is big endian, swap the bytes.
55 #ifdef IS_BIG_ENDIAN
56  static void
57  swap_bytes(char * x, unsigned int n)
58  {
59  for (unsigned int i = 0; i < n / 2; ++ i) {
60  char tmp = x[i];
61  x[i] = x[n - i - 1];
62  x[n - i - 1] = tmp;
63  }
64  }
65 #else
66  static void swap_bytes(char *, unsigned int) {}
67 #endif
68 
69 
70  /// Helper functions to read/write raw memory
71 
72  void
73  write_bytes(char * x, unsigned int n, BUFFER & buf)
74  {
75  //std::cerr << "write bytes in : '" << x << "' " << n << std::endl;
76  swap_bytes(x, n);
77  if (buf.write(x, n) != 1) {
78  THROW_EXCEPTION("Error writing\n");
79  }
80  //std::cerr << "write bytes out: '" << x << "'" << std::endl;
81  }
82 
83  void
84  read_bytes(char * x, unsigned int n, BUFFER & buf)
85  {
86  //std::cerr << "read bytes in : '" << x << "' " << n << std::endl;
87  if (buf.read(x, n) != 1) {
88  THROW_EXCEPTION("Error reading\n");
89  }
90  swap_bytes(x, n);
91  //std::cerr << "read bytes out : '" << x << "'" << std::endl;
92  }
93 
94 
95  /// Helper functions to read/write any single primitive type
96 
97  template <typename T>
98  static void
99  write_bytes(T x, BUFFER & buf)
100  {
101  write_bytes((char *)&x, sizeof(T), buf);
102  }
103 
104  template <typename T>
105  static void
106  read_bytes(T & x, BUFFER & buf)
107  {
108  read_bytes((char *)&x, sizeof(T), buf);
109  }
110 
111 
112 
113  /// Read/write a single primitive type to a buf.
114 
115  void
116  write_binary(char x, BUFFER & buf)
117  {
118  write_bytes<char>(x, buf);
119  }
120 
121  void
122  read_binary(char & x, BUFFER & buf)
123  {
124  read_bytes<char>(x, buf);
125  }
126 
127  void
128  write_binary(bool x, BUFFER & buf)
129  {
130  char x_char = x ? 0xFF : 0x00;
131  write_bytes(x_char, buf);
132  }
133 
134  void
135  read_binary(bool & x, BUFFER & buf)
136  {
137  char x_char;
138  read_bytes(x_char, buf);
139  x = (x_char != 0x00);
140  }
141 
142  void
143  write_binary(unsigned int x, BUFFER & buf)
144  {
145  write_bytes(x, buf);
146  }
147 
148  void
149  read_binary(unsigned int & x, BUFFER & buf)
150  {
151  read_bytes(x, buf);
152  }
153 
154  void
155  write_binary(float x, BUFFER & buf)
156  {
157  write_bytes(x, buf);
158  }
159 
160  void
161  read_binary(float & x, BUFFER & buf)
162  {
163  read_bytes(x, buf);
164  }
165 
166 
167  void
168  write_binary(double x, BUFFER & buf)
169  {
170  write_bytes(x, buf);
171  }
172 
173  void
174  read_binary(double & x, BUFFER & buf)
175  {
176  read_bytes(x, buf);
177  }
178 
179 
180 
181  /// Read/write simple structure to a buf.
182 
183  void
184  write_binary(const utility::vector1_bool & x, BUFFER & buf)
185  {
186  write_binary((unsigned int)(x.size()), buf);
187  for (unsigned int ii = 1; ii <= x.size(); ++ ii) {
188  write_binary(bool(x[ii]), buf);
189  }
190  }
191 
192  void
193  read_binary(utility::vector1_bool & x, BUFFER & buf)
194  {
195  unsigned int size;
196  read_binary(size, buf);
197 
198  x.resize(size);
199  for (unsigned int ii = 1; ii <= x.size(); ++ ii) {
200  bool x_ii;
201  read_binary(x_ii, buf);
202  x[ii] = x_ii;
203  }
204  }
205 
206  void
207  write_binary(const std::vector<std::string> & x, BUFFER & buf)
208  {
209  write_binary((unsigned int)(x.size()), buf);
210  for (unsigned int ii = 0; ii < x.size(); ++ ii) {
211  write_binary(std::string(x[ii]), buf);
212  }
213  }
214 
215  void
216  read_binary(std::vector<std::string> & x, BUFFER & buf)
217  {
218  unsigned int size;
219  read_binary(size, buf);
220 
221  x.resize(size);
222  for (unsigned int ii = 0; ii < x.size(); ++ ii) {
223  std::string x_ii;
224  read_binary(x_ii, buf);
225  x[ii] = x_ii;
226  }
227  }
228 
229  void
230  write_binary(const std::string & x, BUFFER & buf)
231  {
232  write_binary((unsigned int)(x.size()), buf);
233  for (unsigned int ii = 0; ii < x.size(); ++ ii) {
234  write_binary(x[ii], buf);
235  }
236  }
237 
238  void
240  {
241  unsigned int size;
242  read_binary(size, buf);
243 
244  x.resize(size);
245  for (unsigned int ii = 0; ii < x.size(); ++ ii) {
246  char x_ii;
247  read_binary(x_ii, buf);
248  x[ii] = x_ii;
249  }
250  }
251 
252  void
253  write_binary(const core::Vector & x, BUFFER & buf)
254  {
255  write_binary(x.x(), buf);
256  write_binary(x.y(), buf);
257  write_binary(x.z(), buf);
258  }
259 
260  void
262  {
263  read_binary(x.x(), buf);
264  read_binary(x.y(), buf);
265  read_binary(x.z(), buf);
266  }
267 
268 
269 
270  /// Utility read/write.
271 
272  void
273  check_binary_unsigned_int(unsigned int x, BUFFER & buf)
274  {
275  unsigned int input = 0;
276  read_binary(input, buf);
277  if (input != x) {
278  THROW_EXCEPTION("Error reading .\n");
279  }
280  }
281 
282  void
283  write_binary_chars(const char *x, BUFFER & buf)
284  {
285  size_t len = strlen(x);
286 
287  for (unsigned int ii = 0; ii < len; ++ ii) {
288  write_binary(x[ii], buf);
289  }
290  }
291 
292  void
293  check_binary_chars(const char *x, BUFFER & buf)
294  {
295  size_t len = strlen(x);
296  std::vector< char > input(len, 0);
297 
298  for (unsigned int ii = 0; ii < len; ++ ii) {
299  read_binary(input[ii], buf);
300  }
301 
302  if (memcmp(&input[0], x, len)) {
303  THROW_EXCEPTION("Error reading .\n");
304  }
305  }
306 
307 
308 
309 
310  void
311  write_binary(const core::pose::Pose & pose, BUFFER & buf)
312  {
313  const unsigned int WRITE_VERSION = 2;
314  write_binary(WRITE_VERSION, buf);
315 
316 // residues
317  write_binary((unsigned int)pose.total_residue(), buf);
318  for (size_t j = 1; j <= pose.total_residue(); ++ j) {
319  const core::conformation::Residue & residue = pose.residue(j);
320  // if (is_pseudoresidue(residue)) continue;
321 // residue name
322  //write_binary(residue.name(), buf);
323 // residue type set
324  write_binary(residue.type().residue_type_set().name(), buf);
325 // residue type
326  write_binary(residue.type().name(), buf);
327 // residue chain
328  write_binary((unsigned int)residue.chain(), buf);
329 // atoms xyz
330  write_binary((unsigned int)residue.atoms().size(), buf);
331  for (size_t k = 1; k <= residue.atoms().size(); ++ k) {
332  write_binary(residue.atoms()[k].xyz(), buf);
333  }
334  }
335 
336 // fold tree
337  write_binary_chars("FOLDTREE", buf);
338  std::ostringstream foldtree_outs;
339  foldtree_outs << pose.fold_tree();
340  write_binary( foldtree_outs.str(), buf);
341 
342 // jumps
343  write_binary_chars("JUMP", buf);
344  write_binary((unsigned int)pose.num_jump(), buf);
345  for (core::Size j=1; j <= pose.num_jump(); ++j) {
346  std::ostringstream jump_outs;
347  jump_outs << pose.jump(j);
348  write_binary( jump_outs.str(), buf);
349  }
350 
351 // SS
352  write_binary_chars("SSTR", buf);
353  write_binary((unsigned int)pose.total_residue(), buf);
354  for (size_t j = 1; j <= pose.total_residue(); ++ j) {
355  write_binary(pose.secstruct(j), buf);
356  }
357  }
358 
359 
360  void
362  {
363 
364  using namespace core::chemical;
365  using namespace core::conformation;
366  unsigned int read_version = 0;
367  read_binary(read_version, buf);
368 
369  switch (read_version) {
370  case 2:
371  {
372 
373  pose.clear();
374 
377 // total residue
378  unsigned int total_residue = 0;
379  read_binary( total_residue, buf);
380  AtomVector atom_vec;
381  atom_vec.reserve(total_residue*15);
382 
383  unsigned int prevchain = 1;
384  for (size_t j = 1; j <= total_residue; ++ j) {
385 // type set
386  std::string type_set;
387  read_binary(type_set, buf);
388 // type name
389  std::string type_name;
390  read_binary(type_name, buf);
391 // chain
392  unsigned int chainid;
393  read_binary(chainid, buf);
394  bool const is_lower_terminus( j==1 || chainid != prevchain );
395 // new residue
396  ResidueTypeSetCAP target_residue_type_set( ChemicalManager::get_instance()->residue_type_set( type_set ) );
397  ResidueOP new_rsd = ResidueFactory::create_residue( target_residue_type_set->name_map( type_name ) ); //, residue, pose.conformation() );
398  new_rsd->chain(chainid);
399  if (j > 1 && (is_lower_terminus || !new_rsd->is_polymer() || !pose.residue_type(j-1).is_polymer() )){
400  pose.append_residue_by_jump( *new_rsd, 1 );
401  } else {
402  pose.append_residue_by_bond( *new_rsd ); // pose.append_residue_by_jump( *new_rsd, j-1,"", "", true );
403  }
404  prevchain = chainid;
405  // update the pose-internal chain label if necessary
406  if ( is_lower_terminus && pose.total_residue() > 1 ) {
407  pose.conformation().insert_chain_ending( pose.total_residue() - 1 );
408  }
409 // atoms xyz
410  unsigned int natoms;
411  read_binary(natoms, buf);
412  for (size_t k = 1; k <= natoms; ++ k) {
414  read_binary(xyz, buf);
415  atom_vec.push_back(std::make_pair(id::AtomID(k, j), xyz));
416  }
417  }
418 // fold tree
419  check_binary_chars("FOLDTREE", buf);
420  std::string foldtree_str;
421  read_binary( foldtree_str, buf );
422  std::istringstream foldtree_in;
423  foldtree_in.str(foldtree_str);
425  foldtree_in >> f;
426  pose.fold_tree( f );
427 // jumps
428  check_binary_chars("JUMP", buf);
429  unsigned int jumps;
430  read_binary(jumps, buf);
432  for (size_t j = 1; j <= jumps; ++ j) {
433  std::string jump_str;
434  read_binary( jump_str, buf );
435  std::istringstream jump_in;
436  jump_in.str( jump_str );
437  kinematics::Jump jump;
438  jump_in >> jump;
439  jumpsv.push_back( jump );
440  }
441 // set jumps
442  for ( Size nr = 1; nr <= f.num_jump(); nr++) {
443  pose.set_jump( nr, jumpsv[nr] );
444  }
445 
446 // SS
447  check_binary_chars("SSTR", buf);
449  for (size_t j = 1; j <= pose.total_residue(); ++ j) {
450  char sstr;
451  read_binary(sstr, buf);
452  pose.set_secstruct(j, sstr);
453  }
454 
455 // set atom xyz
456  for (AtomVector::iterator ii = atom_vec.begin(); ii != atom_vec.end(); ++ ii) {
457  pose.set_xyz(ii->first, ii->second);
458  }
459 
460  } break;
461 
462  default:
463  THROW_EXCEPTION(std::string("Error reading ; bad pose version.\n"));
464  break;
465  }
466  }
467 
468 } // pose
469 } // io
470 } // core