Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PhiPsiTalosIO.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/PhiPsiTalosIO.cc
10 /// @brief
11 /// @author Dominik Gront (dgront@chem.uw.edu.pl)
12 
13 // unit headers
15 
16 // project headers
17 
18 #include <core/types.hh>
19 #include <basic/Tracer.hh>
20 
21 // utility headers
22 #include <utility/exit.hh>
23 // AUTO-REMOVED #include <utility/vector1.hh>
24 #include <utility/io/izstream.hh>
25 // AUTO-REMOVED #include <utility/io/ozstream.hh>
26 
27 // C++ headers
28 #include <string>
29 #include <map>
30 
31 // boost headers
32 #include <boost/tuple/tuple.hpp>
33 // AUTO-REMOVED #include <boost/algorithm/string.hpp>
34 
35 #include <utility/vector1.hh>
36 
37 
38 namespace protocols {
39 namespace frag_picker {
40 
41 static basic::Tracer
42  tr("protocols.frag_picker.PhiPsiTalosIO");
43 
44 void PhiPsiTalosIO::read(std::string const & file_name) {
45 
46  utility::io::izstream data(file_name.c_str());
47  tr.Info << "read talos data from " << file_name << std::endl;
48  if (!data)
49  utility_exit_with_message("[ERROR] Unable to open talos file: "
50  + file_name);
51 
52  std::string line;
55  bool first_not_found = true;
57  while ( getline(data, line) ) {
58  std::istringstream line_stream(line);
60  std::string token;
61  while ( line_stream >> token ) {
62  strs.push_back(token);
63  }
64  if ( strs.size()==0 ) continue;
65  tr.Trace << "token: " << strs.size() << " in line " << line << std::endl;
66  if (strs[1] == "DATA") {
67  if (strs[2] == "SEQUENCE") {
68  for (Size i = 3; i <= strs.size(); ++i) {
69  sequence_ += strs[i];
70  }
71  } else if (strs[2] == "FIRST_RESID") {
72  first_not_found = false;
73  line_stream >> first_residue_index_;
74  tr.Info << "FIRST_RESID entry in TALOS file. Setting first-residue to " << first_residue_index_ << std::endl;
75  } else {
76  tr.Warning << "Unrecognized DATA entry:" << line
77  << std::endl;
78  }
79  }
80  if (strs[1] == "VARS") {
81  for (Size i = 2; i <= strs.size(); ++i) {
82  vars.push_back(strs[i]);
83  }
84  if ( ( vars.size()!=10 && vars.size()!=11 ) || vars[1]!="RESID" || vars[2]!="RESNAME" || vars[9]!="COUNT" || vars.back()!="CLASS" ) {
85  tr.Warning << "incompatible format in TALOS+ file "+file_name
86  +".\n Expected VARS RESID RESNAME PHI PSI DPHI DPSI DIST S2 COUNT CS_COUNT CLASS\n "
87  +" or VARS RESID RESNAME PHI PSI DPHI DPSI DIST S2 COUNT CLASS\n "
88  +" found instead: ";
89  tr.Warning << vars.size() << " VARS: ";
90  for (Size i=1; i<=vars.size(); ++i) {
91  tr.Warning << vars[i] << " ";
92  }
93  tr.Warning << " LAST VAR: ->" << vars.back() << ":";
94  tr.Warning << std::endl;
95  }
96  }
97  if (strs[1] == "FORMAT") {
98  data_format_ = line.substr(7);
99  }
100 
101  if ((strs.size() == vars.size()) && (strs[1] != "REMARK")) {
102  char aa;
103  std::istringstream line_stream(line);
104  Real phi, psi, d_phi, d_psi, dist, s2;
105  Size res_id, count;
106  std::string cls;
107  if ( vars.size()==10 ) {
108  line_stream >> res_id >> aa >> phi >> psi >> d_phi >> d_psi >> dist
109  >> s2 >> count >> cls;
110  } else {
111  Size cs_count;
112  line_stream >> res_id >> aa >> phi >> psi >> d_phi >> d_psi >> dist
113  >> s2 >> count >> cs_count >> cls;
114  }
115  data_format_ = " %4d %s %8.3f %8.3f %8.3f %8.3f %8.3f %5.3f %2d %s";
116  //this adds sequence twice sequence_ += aa;
117  typedef boost::tuple<Size, char, Real,Real, Real, Real, Real, Real, Size, std::string> PhiPsiTalosLineEntry;
118  PhiPsiTalosLineEntry t(res_id, aa, phi, psi, d_phi, d_psi, dist,
119  s2, count, cls);
120  entries_.insert(std::pair<Size, PhiPsiTalosLineEntry > ( res_id, t) );
121  if (last_residue_index_ < res_id)
122  last_residue_index_ = res_id;
123  }
124  }
125 
126  if (first_not_found) {
127  tr.Warning
128  << "FIRST_RESID keyword didn't show up in a file header\n\tAssuming the first residue id is 1"
129  << std::endl;
130  }
131  if (sequence_.length() == 0) {
132  for (Size i = first_residue_index_; i <= last_residue_index_; ++i) {
133  if (has_entry(i)) {
134  sequence_ += entries_.find(i)->second.get<1> ();
135  } else {
136  sequence_ += 'X';
137  }
138  }
139  tr.Warning << "Could not find DATA SEQUENCE in file "<<file_name << std::endl
140  << "Sequence based on entires is:" << std::endl
141  << sequence_ << std::endl;
142  }
143 }
144 
145 
146 void PhiPsiTalosIO::write(std::ostream& out) {
147 
148  out << "DATA FIRST_RESID " << first_residue_index_ << "\n";
149  out << "DATA SEQUENCE " << sequence_ << "\n";
150  out << "VARS";
151  for (Size i = 1; i <= column_names_.size(); i++)
152  out << " " << column_names_[i];
153  out << "\n" << "FORMAT " << data_format_ << std::endl << "\n";
154  char buffer[100];
155  char c1[2];
156  c1[1] = 0;
157  for (Size i = 1; i <= entries_.size(); i++) {
158  c1[0] = entries_[i].get<1> ();
159  sprintf(buffer, data_format_.c_str(), entries_[i].get<0> (), c1,
160  entries_[i].get<2> (), entries_[i].get<3> (),
161  entries_[i].get<4> (), entries_[i].get<5> (),
162  entries_[i].get<6> (), entries_[i].get<7> (),
163  entries_[i].get<8> (), entries_[i].get<9> ().c_str());
164  out << buffer << "\n";
165  }
166  out << std::endl;
167 }
168 
169 } // frag_picker
170 } // protocols