Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DisulfideFile.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 sw=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 core/io/raw_data/DisulfideFile.hh
11 /// @brief A simple wrapper for a Disulfide File suitable for the -fix_disulf option.
12 /// @author Spencer Bliven <blivens@u.washington.edu>
13 
15 
16 #include <core/types.hh>
17 // AUTO-REMOVED #include <basic/database/open.hh>
18 #include <basic/Tracer.hh>
19 #include <core/pose/Pose.hh>
20 #include <core/pose/PDBInfo.hh>
22 
23 // AUTO-REMOVED #include <utility/vector1.hh>
24 #include <utility/io/izstream.hh>
25 
26 #include <utility/vector1.hh>
27 
28 
29 
30 namespace core {
31 namespace io {
32 namespace raw_data {
33 
34 using namespace std;
35 using core::Size;
36 using utility::vector1;
37 using core::pose::Pose;
38 
39 static basic::Tracer TR("core.io.raw_data.DisulfideFile");
40 
41 /// @brief Get a list of disulfide bonds declared in the file
42 /// @details
43 /// The first time this is called it reads
44 /// the file and caches the resulting pairs of residues.
45 /// Subsequent calls to disulfides() are fast since they don't reparse the file
46 /// but merely reinterpret the results in terms of the specified.
47 ///
48 /// This version ignores the possibility of PDB numbering and just reports on the file itself
49 ///
50 /// @param[out] disulfides Appends pairs of residues specified by the file to this list
51 /// @param[in] none
52 ///
53 /// @postcondition For each pair of residues (l,u) in this list, l<u
54 void DisulfideFile::disulfides(vector1< pair<Size,Size> > & disulfides ) const {
55  disulfides.resize(disulfides.size()+disulfides_.size());
56 
57  if(! up_to_date_ ) {
58  parse_disulf_file();
59  //mark cache as up to date
60  up_to_date_ = true;
61  }
62  for(vector1< pair<ResNum,ResNum> >::const_iterator disulf = disulfides_.begin(),
63  end_disulf = disulfides_.end();
64  disulf != end_disulf; ++disulf)
65  {
66  Size l = disulf->first.n;
67  Size u = disulf->second.n;
68  disulfides.push_back(std::make_pair(l,u) );
69  }
70 }
71 
72 
73 /// @brief Get a list of disulfide bonds declared in the file
74 /// @details
75 /// The first time this is called it reads
76 /// the file and caches the resulting pairs of residues.
77 /// Subsequent calls to disulfides() are fast since they don't reparse the file
78 /// but merely reinterpret the results in terms of the specified.
79 ///
80 /// @param[out] disulfides Appends pairs of residues specified by the file to this list
81 /// @param[in] pose Only used if PDB numbering is used in the disulfide file
82 ///
83 /// @postcondition For each pair of residues (l,u) in this list, l<u
84 void DisulfideFile::disulfides(vector1< pair<Size,Size> > & disulfides, Pose const& pose) const {
85  disulfides.resize(disulfides.size()+disulfides_.size());
86 
87  if(! up_to_date_ ) {
88  parse_disulf_file();
89  //mark cache as up to date
90  up_to_date_ = true;
91  }
92  for(vector1< pair<ResNum,ResNum> >::const_iterator disulf = disulfides_.begin(),
93  end_disulf = disulfides_.end();
94  disulf != end_disulf; ++disulf)
95  {
96  Size l = resnum_to_rosetta_num(pose, disulf->first);
97  Size u = resnum_to_rosetta_num(pose, disulf->second);
98  disulfides.push_back(std::make_pair(l,u) );
99  }
100 }
101 
102 /// @brief Get a list of disulfide bonds declared in the file and manually set them in
103 /// the conformation
104 /// @details
105 /// The first time this is called it reads
106 /// the file and caches the resulting pairs of residues.
107 /// Subsequent calls to disulfides() are fast since they don't reparse the file
108 /// but merely reinterpret the results in terms of the specified.
109 /// (this is a necessary workaround for dealing with multiple disulfide specification files
110 /// in PyRosetta) Because there is no equivalent of std::pair in python this method
111 /// avoids needing to provide a vector1 of std::pairs as arguments
112 ///
113 /// @param[out] disulfides Appends pairs of residues specified by the file to this list
114 /// @param[in] pose is used if PDB numbering is used in the disulfide file and is also
115 /// needed to manually set the disulfides in the conformation
116 ///
117 /// @postcondition For each pair of residues (l,u) in this list, l<u
120 
121 
122  if(! up_to_date_ ) {
123  parse_disulf_file();
124  //mark cache as up to date
125  up_to_date_ = true;
126  }
127  for(vector1< pair<ResNum,ResNum> >::const_iterator disulf = disulfides_.begin(),
128  end_disulf = disulfides_.end();
129  disulf != end_disulf; ++disulf)
130  {
131  Size l = resnum_to_rosetta_num(pose, disulf->first);
132  Size u = resnum_to_rosetta_num(pose, disulf->second);
133  disulfides.push_back(std::make_pair(l,u) );
134  }
135  pose.conformation().fix_disulfides( disulfides );
136 }
137 
138 
139 /// @brief Convert a ResNum object into the rosetta residue index
140 /// @details For ResNums with the rosetta_num type, this just return the n field.
141 /// For pdb_num ResNums the pose's PDBInfo is used to translate to rosetta numbering.
142 ///
143 /// This function exits with an error message if it is unable to do the conversion.
144 Size DisulfideFile::resnum_to_rosetta_num(Pose const& pose, ResNum const& resnum) const
145 {
146  using namespace core::pose;
147 
148  // Rosetta number
149  if( resnum.type == rosetta_num ||
150  (resnum.type == unknown_num && resnum.chain == 0) )
151  {
152  return resnum.n;
153  }
154 
155  //PDB number
156  PDBInfoCOP info( pose.pdb_info() );
157  if( info == 0 ) {
158  TR.Error << "[ERROR] PDB Number expected from format, but no PDB Info present."
159  << std::endl;
160  utility_exit();
161  }
162  Size n( info->pdb2pose(resnum.chain, resnum.n) );
163  if( n == 0 ) {
164  TR.Error << "[ERROR] PDB Number " << resnum.n << resnum.chain
165  << " does not correspond to a valid residue." << std::endl;
166  utility_exit();
167  }
168  return n;
169 
170 }
171 
172 /// @brief Parses residue numbers out of a disulfide file
173 /// See \link core::io::raw_data::DisulfideFile DisulfideFile \endlink for more
174 /// details on the file format.
176  //Open disulfide file
177  utility::io::izstream disulf_stm;
178  disulf_stm.open( filename_ );
179  if( disulf_stm.fail() ) {
180  TR.Error << "[ERROR] Unable to open disulfide file " << filename_ << "."<< std::endl;
181  utility_exit();
182  }
183 
184  disulfides_.clear();
185 
186  //skip whitespace
187  disulf_stm >> skipws;
188  while(disulf_stm.good() ) {
189  //Decide what to do based on the next character
190  int next_char = disulf_stm.peek();
191  if(disulf_stm.eof())
192  break;
193  if(disulf_stm.fail() || disulf_stm.bad() ) {
194  TR.Error << "[ERROR] Error reading disulfide file " << filename_ << "." << std::endl;
195  utility_exit();
196  }
197  switch( next_char ) {
198  case '#': {
199  //Ignore comments
200  std::string line;
201  disulf_stm.getline(line);
202  continue;
203  }
204  default: {
205  //Read pair of residue indices.
206  Size l,u;
207  disulf_stm >> l >> u >> std::ws;
208  if(disulf_stm.fail() || disulf_stm.bad() ) {
209  TR.Error << "[ERROR] Error reading disulfide file " << filename_ << "." << std::endl;
210  utility_exit();
211  }
212 
213  //Guarantee that l < u
214  if( u < l) {
215  Size tmp = u;
216  u = l;
217  l = tmp;
218  }
219  TR.Info << "Fixing a disulfide between "
220  << l << " and " << u << std::endl;
221  ResNum lres = { l,0,rosetta_num };
222  ResNum ures = { u,0,rosetta_num };
223  disulfides_.push_back(make_pair(lres,ures));
224 
225  continue;
226  }
227  }
228  }
229  //clean up
230  disulf_stm.close();
231 
232  up_to_date_ = true;
233 }
234 
235 } //raw_data
236 } //io
237 } //core