Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EntityCorrespondence.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 protocols/pack_daemon/EntityCorrespondence.cc
11 /// @brief Implementation for class EntityCorrespondence
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 // Unit headers
16 
17 // C/C++ headers
18 // AUTO-REMOVED #include <ctype.h>
19 
20 // Project headers
21 #include <core/pose/Pose.hh>
22 #include <core/pose/PDBInfo.hh>
23 #include <core/pose/PDBPoseMap.hh>
24 
25 // Utility headers
26 #include <utility/exit.hh>
27 #include <utility/string_util.hh>
28 #include <utility/vector1.hh>
29 #include <utility/excn/Exceptions.hh>
30 #include <utility/io/izstream.hh>
31 
32 namespace protocols {
33 namespace pack_daemon {
34 
36 {
42 };
43 
45  funcnames_( func_residues_for_entity_end )
46 {
47  funcnames_[ func_add_resid_to_entity_list ] = "add_resid_to_entity_list";
48  funcnames_[ func_entity_for_residue ] = "entity_for_residue";
49  funcnames_[ func_n_residues_for_entity ] = "n_residues_for_entity";
50  funcnames_[ func_residues_for_entity_begin ] = "residues_for_entity_begin";
51  funcnames_[ func_residues_for_entity_end ] = "residues_for_entity_end";
52 
53 }
55 
57  parent(),
58  pose_( src.pose_ ),
59  pdb_pose_map_( src.pdb_pose_map_ ),
60  entity_id_2_resids_( src.entity_id_2_resids_ ),
61  resid_2_entity_id_( src.resid_2_entity_id_ ),
62  funcnames_( src.funcnames_ )
63 {}
64 
67 {
68  if ( this != &other ) {
69  pose_ = other.pose_;
73  funcnames_ = other.funcnames_;
74  }
75  return *this;
76 }
77 
78 
80 {
81  using namespace core::pose;
82 
83  if ( pose_ == 0 || pose_->total_residue() != pose->total_residue() ) {
84  resid_2_entity_id_.resize( pose->total_residue() );
85  std::fill( resid_2_entity_id_.begin(), resid_2_entity_id_.end(), 0 );
86  }
87  pose_ = pose;
88  if ( ! pose_->pdb_info() ) {
89  PDBInfo info(*pose_);
90  pdb_pose_map_ = new PDBPoseMap( info ); //use PDBInfo ctor for PDBPoseMap
91  } else {
92  pdb_pose_map_ = new PDBPoseMap( pose_->pdb_info()->pdb2pose());
93  }
94 }
95 
97 {
98  entity_id_2_resids_.resize( num_entities );
99  std::fill( resid_2_entity_id_.begin(), resid_2_entity_id_.end(), 0 );
100 }
101 
102 /// @details File format for entity correspondence:
103 /// Column 1: entity id. Columns 2 and 3 PDB id (resid+[optional insertion code] chain)
104 void
106 {
107  Size count_correspondences( 0 );
108  Size line_count( 0 );
109  int PDBnum = 0;
110  std::string PDBnum_string;
111  char chain = '_';
112  Size entity_id( 0 ), residue_index( 0 );
113  while ( instream ) {
114  char icode = ' ';
115  ++line_count;
116  std::string line;
117  getline( instream, line );
118  if ( line.size() == 0 ) continue; // Ignore blank lines
119 
120  std::istringstream linestream( line );
121  if (( linestream >> entity_id ).fail() ) {
122  throw utility::excn::EXCN_Msg_Exception(
123  "Failed to read entity id on line " + utility::to_string( line_count ) +
124  " of the EntityCorrespondence file:\n" + line );
125  }
126  ++count_correspondences;
127  if ( entity_id > num_entities() ) {
128  throw utility::excn::EXCN_Msg_Exception(
129  "Entity ID read on line " + utility::to_string( line_count ) +
130  " of the EntityCorrespondence file exceeds the number of entities: " +
131  utility::to_string( entity_id ) + " vs " + utility::to_string( num_entities() ) );
132  }
133 
134  if (( linestream >> PDBnum_string ).fail() ) {
135  throw utility::excn::EXCN_Msg_Exception(
136  "Failed to read PDB id for a residue on line " + utility::to_string( line_count ) +
137  " of the EntityCorrespondence file:\n" + line );
138  }
139  /* resfile code */
140  bool condition;
141 #ifndef _WIN32
142  condition = std::isalpha(*PDBnum_string.rbegin());
143 #else
144  condition = isalpha(*PDBnum_string.rbegin());
145 #endif
146  if (condition) {
147  /// The last character is the insertion code.
148  for ( Size ch = 0; ch < PDBnum_string.length()-1; ++ch ) {
149  if ( ! isdigit( PDBnum_string[ ch ] ) && ( ch != 0 || PDBnum_string[ ch ] != '-' )) {
150  throw utility::excn::EXCN_Msg_Exception(
151  "Failed to read PDB residue id on line " + utility::to_string( line_count ) +
152  " of the EntityCorrespondence file:\n" + line +"\nCharacter" +
153  utility::to_string( ch + 1 ) + " of '" + PDBnum_string + "' is not a digit");
154  }
155  }
156  PDBnum = atoi( PDBnum_string.substr( 0, PDBnum_string.length() - 1 ).c_str() );
157  icode = *PDBnum_string.rbegin();
158  } else { // no insertion code
159  for ( Size ch = 0; ch < PDBnum_string.length(); ++ch ) {
160  if ( ! isdigit( PDBnum_string[ ch ] ) && ( ch != 0 || PDBnum_string[ ch ] != '-' )) {
161  throw utility::excn::EXCN_Msg_Exception(
162  "Failed to read PDB residue id on line " + utility::to_string( line_count ) +
163  " of the EntityCorrespondence file:\n" + line +"\nCharacter" +
164  utility::to_string( ch + 1 ) + " of '" + PDBnum_string + "' is not a digit");
165  }
166  }
167  PDBnum = atoi( PDBnum_string.c_str() );
168  }
169  if ((linestream >> chain ).fail() ) {
170  throw utility::excn::EXCN_Msg_Exception(
171  "Failed to read a chain id for a PDB residue on line " + utility::to_string( line_count ) +
172  " of the EntityCorrespondence file:\n" + line );
173  }
174  if (chain == '_') chain = ' ';
175  residue_index = pdb_pose_map_->find( chain, PDBnum, icode );
176  /* end resfile code */
177 
178  if ( residue_index == 0 ) {
179  throw utility::excn::EXCN_Msg_Exception(
180  "Residue ID read on line " + utility::to_string( line_count ) +
181  " of the EntityCorrespondence file is not present in the pose: " +
182  PDBnum_string + " ch: " + chain + " vs pose.total_residue()= " + utility::to_string( num_residues() ) );
183  }
184  add_resid_to_entity_list( entity_id, residue_index );
185  }
186 
187  /*if ( count_correspondences == 0 ) {
188  throw utility::excn::EXCN_Msg_Exception(
189  "Failed to find any correspondences while reading from EntityCorrespondence file" );
190  }*/
191 }
192 
193 void
195  Size EntityID,
196  Size ResID
197 )
198 {
200  bounds_check_entity( funcnames_[ func_add_resid_to_entity_list ], EntityID );
201  if ( resid_2_entity_id_[ ResID ] != 0 ) {
202  if ( resid_2_entity_id_[ ResID ] == EntityID ) return;
203  entity_id_2_resids_[ resid_2_entity_id_[ ResID ] ].remove( ResID );
204  }
205  entity_id_2_resids_[ EntityID ].push_back( ResID );
206  resid_2_entity_id_[ ResID ] = EntityID;
207 }
208 
209 
212 {
213  return entity_id_2_resids_.size();
214 }
215 
216 
219 {
220  return pose_->total_residue();
221 }
222 
225 {
227  return resid_2_entity_id_[ resid ];
228 }
229 
232 {
234  return entity_id_2_resids_[ entity_id ].size();
235 }
236 
239 {
241  return entity_id_2_resids_[ entity_id ].begin();
242 
243 }
244 
247 {
249  return entity_id_2_resids_[ entity_id ].end();
250 
251 }
252 
254  std::string const & funcname,
255  Size entity_id
256 ) const
257 {
258  if ( entity_id > num_entities() ) {
259  throw utility::excn::EXCN_Msg_Exception( "EntityCorrespondence::" + funcname + " "
260  "attempted to access information for entity " + utility::to_string( entity_id ) +
261  " (only " + utility::to_string( num_entities() ) + " entities exist)" );
262  }
263 }
264 
266  std::string const & funcname,
267  Size resid
268 ) const
269 {
270  if ( ! pose_ ) {
271  throw utility::excn::EXCN_Msg_Exception( "EntityCorrespondence::" + funcname + " "
272  "invoked before pose_ pointer was set." );
273  }
274  if ( resid > num_residues() ) {
275  throw utility::excn::EXCN_Msg_Exception( "EntityCorrespondence::" + funcname + " "
276  "attempted to access information for residue " + utility::to_string( resid ) +
277  " (only " + utility::to_string( num_residues() ) + " residues exist)" );
278  }
279 }
280 
281 
282 }
283 }