Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CSTalosIO.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 // This file is part of the Rosetta software suite and is made available under license.
5 // The Rosetta software is developed by the contributing members of the Rosetta Commons consortium.
6 // (C) 199x-2009 Rosetta Commons participating institutions and developers.
7 // For more information, see http://www.rosettacommons.org/.
8 
9 /// @file protocols/frag_picker/CSTalosIO.cc
10 /// @brief
11 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
12 
13 // unit headers
15 
16 // project headers
17 #include <core/types.hh>
18 
19 #include <basic/Tracer.hh>
20 
21 // utility headers
22 #include <utility/io/izstream.hh>
23 
24 #include <string>
25 #include <map>
26 #include <utility/exit.hh>
27 
28 // boost headers
29 #include <boost/tuple/tuple.hpp>
30 #include <utility/vector1.hh>
31 
32 namespace protocols {
33 namespace frag_picker {
34 
35 static basic::Tracer tr("protocols.frag_picker.TalosReader");
36 
38 
39  Size len = get_sequence().length();
40 
42  for (Size i = 1; i <= entries_.size(); ++i) {
43  // Size res_id = entries_[i].get<0> ();
44  std::string& at_name = entries_[i].get<2> ();
45  Real shift = entries_[i].get<3> ();
46  Size atom_id = order_of_atoms_.find(at_name)->second;
47 
48  data[i].push_back(std::pair<Size, Real>(atom_id, shift));
49  }
50 
51  return data;
52 }
53 
54 void CSTalosIO::read(std::string const & file_name) {
55 
56  utility::io::izstream data(file_name.c_str());
57  tr.Info << "read talos data from " << file_name << std::endl;
58  if (!data)
59  utility_exit_with_message("[ERROR] Unable to open talos file: "
60  + file_name);
61  std::string line;
62  std::string keyword;
63  std::string subkeyword;
64  std::string entry;
65  sequence_ = "";
66  bool header_done = false;
67  bool first_not_found = false;
68  while (!header_done) {
69  getline(data, line);
70  std::istringstream line_stream(line);
71  line_stream >> keyword;
72  if (keyword == "DATA") {
73  line_stream >> subkeyword;
74  if (subkeyword == "SEQUENCE") {
75  while (line_stream >> entry) {
76  sequence_ += entry;
77  }
78  } else if (subkeyword == "FIRST_RESID") {
79  first_not_found = true;
80  line_stream >> first_residue_index_;
81  tr.Info << "first residue is set to " << first_residue_index_ << " by FIRST_RESID entry in chemical shift file." << std::endl;
82  } else {
83  tr.Warning << "Unrecognized DATA entry:" << line
84  << std::endl;
85  }
86  }
87  if (keyword == "VARS") {
88  while ( line_stream >> entry ) {
89  column_names_.push_back(entry);
90  }
91  }
92  if (keyword == "FORMAT") {
93  line_stream >> entry;
94  data_format_ = line.substr(7);
95  header_done = true;
96  }
97  }
98  if (!first_not_found) {
99  //this is quite normal...
100  tr.Debug << "FIRST_RESID keyword didn't show up in a file header\n\tAssuming the first residue id is 1"
101  << std::endl;
102  }
103 
104  while (getline(data, line)) {
105  if (line.length() > 7) {
106  std::istringstream line_stream(line);
107  Size ires;
108  char aa;
109  std::string atom_name;
110  Real shift;
111  line_stream >> ires >> aa >> atom_name >> shift;
112 
113  if (atom_name == "H")
114  atom_name = "HN";
115 
116  //std::cout << "READ_SHIFTS " << ires << " " << aa << " " << atom_name << " " << shift << std::endl;
117 
118  boost::tuple<Size, char, std::string, Real> t(ires, aa, atom_name,
119  shift);
120  entries_.push_back(t);
121  }
122  }
123 
124  for (Size i = 1; i <= entries_.size(); i++) {
125  //std::cout << "ENTRIES: " << entries_[i].get<0>() << " " << entries_[i].get<1>() << " " << entries_[i].get<2>() << std::endl;
126  resids_to_entries_map_.insert(std::make_pair(entries_[i].get<0> (), i));
127  }
128 }
129 
130 void CSTalosIO::write(std::ostream& out) {
131 
132  out << "DATA FIRST_RESID " << first_residue_index_ << std::endl;
133  out << "DATA SEQUENCE " << sequence_ << std::endl;
134  out << "VARS";
135  for (Size i = 1; i <= column_names_.size(); i++)
136  out << " " << column_names_[i];
137  out << std::endl << "FORMAT " << data_format_ << std::endl << std::endl;
138  char buffer[50];
139  char c1[2];
140  c1[1] = 0;
141  for (Size i = 1; i <= entries_.size(); i++) {
142  c1[0] = entries_[i].get<1> ();
143  sprintf(buffer, data_format_.c_str(), entries_[i].get<0> (), c1,
144  entries_[i].get<2> ().c_str(), entries_[i].get<3> ());
145  out << buffer << std::endl;
146  }
147 }
148 
149 void CSTalosIO::get_tuples(Size residue_id, utility::vector1<boost::tuple<Size,
150  char, std::string, Real> > results) {
151 
152  std::multimap<Size, Size>::iterator iter = resids_to_entries_map_.find(
153  residue_id);
154 
155  //while (iter != resids_to_entries_map_.end()) {
156  while (iter->first == residue_id) {
157  results.push_back(entries_[iter->second]);
158  ++iter;
159  }
160 }
161 
162 void CSTalosIO::get_tuples(Size residue_id, utility::vector1<boost::tuple<Size,
163  char, std::string, Real> > & results) const {
164 
165  std::multimap<Size, Size>::const_iterator iter =
166  resids_to_entries_map_.find(residue_id);
167 
168  //while (iter != resids_to_entries_map_.end()) {
169  while (iter->first == residue_id) {
170  //std::cout << "GET_TUPLES" << residue_id << " " << iter->first << " " << iter->second << std::endl;
171  results.push_back(entries_[iter->second]);
172  ++iter;
173  }
174 }
175 
176 bool CSTalosIO::has_atom(Size residue_id, std::string const & atom_name) const {
177 
178  std::multimap<Size, Size>::const_iterator iterat =
179  resids_to_entries_map_.find(residue_id);
180  if (iterat == resids_to_entries_map_.end())
181  return false;
182 
185 
186  get_tuples(residue_id, used_for_searching_);
187  for (Size i = 1; i <= used_for_searching_.size(); i++) {
188  //std::cout << "FROMTUPLE " << residue_id << " " << used_for_searching_[i].get<0>() << " " << used_for_searching_[i].get<2>() << " " << used_for_searching_[i].get<1>() << std::endl;
189 
190  //Seriously, get_tuples(residue_id,...) also fetches tuples for OTHER residue IDs.
191  //I have no idea why.
192  if ((used_for_searching_[i].get<0>() == residue_id) && (used_for_searching_[i].get<2> () == atom_name)) {
193  used_for_searching_.clear();
194  return true;
195  }
196  }
197  used_for_searching_.clear();
198 
199  return false;
200 }
201 
202 Real CSTalosIO::get_shift(Size residue_id, std::string const & atom_name) const {
203 
204  std::multimap<Size, Size>::const_iterator iterat =
205  resids_to_entries_map_.find(residue_id);
206  if (iterat == resids_to_entries_map_.end())
207  return false;
208 
211 
212  get_tuples(residue_id, used_for_searching_);
213  for (Size i = 1; i <= used_for_searching_.size(); i++){
214  //std::cout << "GET_SHIFT " << residue_id << " " << atom_name << " " << used_for_searching_[i].get<2>() << std::endl;
215 
216  //Seriously, get_tuples(residue_id,...) also fetches tuples for OTHER residue IDs.
217  //I have no idea why.
218  if ((used_for_searching_[i].get<0>() == residue_id) && (used_for_searching_[i].get<2> () == atom_name)) {
219  Real ret = used_for_searching_[i].get<3> ();
220  used_for_searching_.clear();
221  return ret;
222  }
223  }
224 
225  utility_exit_with_message(
226  "[ERROR] Unable locate chemical shift for an atom " + atom_name
227  + " within a residue");
228  return 0.0;
229 }
230 
232 
233  order_of_atoms_.insert(std::pair<std::string, Size>("N", 1));
234  order_of_atoms_.insert(std::pair<std::string, Size>("HA", 2));
235  order_of_atoms_.insert(std::pair<std::string, Size>("HA2", 5));
236  order_of_atoms_.insert(std::pair<std::string, Size>("HA3", 2));
237  order_of_atoms_.insert(std::pair<std::string, Size>("C", 3));
238  order_of_atoms_.insert(std::pair<std::string, Size>("CA", 4));
239  order_of_atoms_.insert(std::pair<std::string, Size>("CB", 5));
240  order_of_atoms_.insert(std::pair<std::string, Size>("HN", 6));
241  order_of_atoms_.insert(std::pair<std::string, Size>("H", 6));
242 }
243 
244 } // frag_picker
245 } // protocols