Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mol_writer.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 src/core/chemical/sdf/mol_writer.cc
11 /// @author Sam DeLuca
12 /// @details this class outputs a residue in the form of a V3000 molfile, for details of the spec, see http://www.symyx.com/downloads/public/ctfile/ctfile.pdf
18 #include <utility/string_util.hh>
19 // AUTO-REMOVED #include <core/chemical/ChemicalManager.hh>
20 #include <set>
21 
22 // Boost Headers
23 #include <boost/foreach.hpp>
24 
25 #include <utility/vector1.hh>
26 #include <utility/io/ozstream.hh>
27 
28 #define foreach BOOST_FOREACH
29 
30 namespace core {
31 namespace chemical {
32 namespace sdf {
33 
34 
35 
36 MolWriter::MolWriter() : line_header_("M V30 ")
37 {}
38 
39 
40 void MolWriter::output_residue(utility::io::ozstream & output_stream, core::conformation::ResidueCOP residue)
41 {
42  std::list<std::string> prepared_lines;
43  std::list<std::string> metadata = this->compose_metadata(residue);
44  std::list<std::string> ctab = this->compose_ctab(residue);
45  std::list<std::string> typeinfo = this->compose_typeinfo(residue);
46  std::list<std::string> nbr_atom = this->compose_nbr_atom(residue);
47 
48  prepared_lines.insert(prepared_lines.end(),metadata.begin(),metadata.end());
49  prepared_lines.insert(prepared_lines.end(),ctab.begin(),ctab.end());
50  prepared_lines.insert(prepared_lines.end(),typeinfo.begin(),typeinfo.end());
51  prepared_lines.insert(prepared_lines.end(),nbr_atom.begin(),nbr_atom.end());
52 
53  foreach(std::string line, prepared_lines){
54  output_stream << line;
55  }
56 
57 }
58 
59 
60 
61 
62 
63 void MolWriter::output_residue(utility::io::ozstream & output_stream, core::chemical::ResidueTypeOP residue_type)
64 {
65  core::conformation::ResidueCOP residue_ptr(new core::conformation::Residue(*residue_type,false) );
66  //core::conformation::ResidueCOP residue_ptr= &residue;
67  //std::cout <<residue_ptr->name3() <<std::endl;
68  output_residue(output_stream,residue_ptr);
69 }
70 
71 
73 {
74  utility::io::ozstream outfile;
75  outfile.open(file_name.c_str());
76  output_residue(outfile,residue);
77  outfile.close();
78 }
79 
80 
82 {
83  utility::io::ozstream outfile;
84  outfile.open(file_name.c_str());
85  output_residue(outfile,residue_type);
86  outfile.close();
87 }
88 
90 {
91  std::list<std::string> lines;
92 
93  std::string const name_line = residue->name()+"\n";
94  //for specification see page 35 of the sybl molfile specification document
95  std::string const info_line = " Rosetta 3D \n";
96  std::string const name3_line = residue->name3()+"\n";
97  std::string const counts_line = " 0 0 0 0 0 999 V3000\n";
98 
99  lines.push_back(name_line);
100  lines.push_back(info_line);
101  lines.push_back(name3_line);
102  lines.push_back(counts_line);
103  return lines;
104 }
105 
107 {
108  std::list<std::string> lines;
109  std::string begin_header = line_header_+"BEGIN CTAB\n";
110  std::string end_header = line_header_+"END CTAB\n";
111 
112  core::Size n_atoms = residue->natoms();
113  core::Size n_bonds = 0;
114  for(core::Size index = 1; index <= n_atoms;++index)
115  {
116  n_bonds += residue->bonded_neighbor(index).size();
117  }
118  //bonds are seen twice in the bond matrix (a-b and b-a)
119  n_bonds = n_bonds/2;
120 
121  std::string counts = line_header_+"COUNTS "+ utility::to_string<core::Size>(n_atoms)+" "+
122  utility::to_string<core::Size>(n_bonds)+" " + "0" + " " + "0" + " "+ "0"+"\n";
123 
124  //compose bonds and atoms, append all this to the ctab
125 
126  std::list<std::string> atom_lines = this->compose_atoms(residue);
127  std::list<std::string> bond_lines = this->compose_bonds(residue);
128 
129  lines.push_back(begin_header);
130  lines.push_back(counts);
131  lines.insert(lines.end(),atom_lines.begin(),atom_lines.end());
132  lines.insert(lines.end(),bond_lines.begin(),bond_lines.end());
133  lines.push_back(end_header);
134  lines.push_back("M END\n");
135 
136  return lines;
137 }
138 
140 {
141  std::list<std::string> lines;
142  std::string begin_header = line_header_+"BEGIN ATOM"+"\n";
143  std::string end_header = line_header_+"END ATOM"+"\n";
144 
145  lines.push_back(begin_header);
146 
147  for(core::Size index = 1; index <= residue->natoms(); ++index)
148  {
149  core::Vector xyz_coords(residue->xyz(index));
150  core::chemical::AtomType const atom_type = residue->atom_type(index);
151  core::chemical::ResidueTypeCOP residue_type = & residue->type();
152  std::string element = atom_type.element();
153  core::Real charge = residue_type->atom(index).charge();
154 
155  std::string atom_string = line_header_ + " " + utility::to_string<core::Size>(index)+" "+
156  element+" "+ utility::to_string<core::Real>(xyz_coords.x())+ " "+
157  utility::to_string<core::Real>(xyz_coords.y())+ " " +
158  utility::to_string<core::Real>(xyz_coords.z())+ " " +
159  "0"+" "+"CHG="+utility::to_string<core::Real>(charge)+"\n";
160  lines.push_back(atom_string);
161  }
162  lines.push_back(end_header);
163  return lines;
164 }
165 
167 {
168  std::list<std::string> lines;
169  std::string begin_header = line_header_+"BEGIN BOND"+"\n";
170  std::string end_header = line_header_+"END BOND"+"\n";
171 
172  lines.push_back(begin_header);
173 
174  //bonds are non-directional so we want to make sure they only get counted once.
175  //(bond from atom 1 to atom 5 is the same as the one from 5 to 1)
176  //stick all the bonds in a set and then iterate through the set to output
177  //the smaller atom index is always designated lower in the struct
178 
179 
180  std::set<BondData> bond_data_set;
181  bond_data_set.clear();
182  for(core::Size index = 1; index <= residue->natoms();++index)
183  {
184  core::chemical::AtomIndices const bonded_neighbors = residue->bonded_neighbor(index);
185  utility::vector1<core::chemical::BondName> const bonded_neighbor_types = residue->type().bonded_neighbor_types(index);
186  assert(bonded_neighbors.size()== bonded_neighbor_types.size());
187 
188  for(core::Size neighbor_index = 1; neighbor_index <= bonded_neighbors.size();++neighbor_index)
189  {
190  core::Size type = bonded_neighbor_types[neighbor_index];
191  core::Size neighbor = bonded_neighbors[neighbor_index];
192  BondData bond(index,neighbor,type);
193  if(bond_data_set.find(bond)!= bond_data_set.end())
194  {
195  //This is a terrible hack. If it's not here, and there
196  //is a bond from index 1 to index 4, it gets inserted twice
197  //I don't know why, I'll fix it later, I promise.
198 
199  //Sorry :(
200  continue;
201  }
202  // Unused variable, but preserve the insertion.
203  //std::pair<std::set<BondData>::iterator,bool > bond_set_return =
204  bond_data_set.insert(bond);
205  //if(bond_set_return.second)
206  //{
207  // std::cout <<bond.lower << " " <<bond.upper << " " << bond.bondType <<std::endl;
208  //}
209  }
210  }
211 
212  core::Size bond_index = 1;
213  foreach(BondData current_bond, bond_data_set){
214  std::string bond_line = line_header_ + utility::to_string<core::Size>(bond_index) + " " +
215  utility::to_string<core::Size>(current_bond.bondType) + " " +
216  utility::to_string<core::Size>(current_bond.lower) + " " +
217  utility::to_string<core::Size>(current_bond.upper) + " " + "\n";
218  lines.push_back(bond_line);
219  ++bond_index;
220  }
221 
222  lines.push_back(end_header);
223 
224  return lines;
225 }
226 
228 {
229  std::list<std::string> lines;
230 
231  std::string header = "> <Rosetta AtomTypes>\n";
232  std::string type_data = "";
233  core::chemical::ResidueTypeCOP residue_type = & residue->type();
234  for(core::Size index =1; index <= residue->natoms();++index)
235  {
236  std::string atom_type_name = residue_type->atom_type(index).name();
237  std::string data_string = "("+utility::to_string<core::Size>(index)+","+atom_type_name+") ";
238  type_data.append(data_string);
239  }
240  type_data.append("\n");
241 
242  lines.push_back(header);
243  lines.push_back(type_data);
244  lines.push_back("\n");
245 
246  return lines;
247 }
248 
250 {
251  std::list<std::string> lines;
252 
253  std::string header = "> <Rosetta nbr_atom>\n";
254  std::string nbr_atom = utility::to_string<core::Size>(residue->nbr_atom()) + "\n";
255 
256  lines.push_back(header);
257  lines.push_back(nbr_atom);
258  lines.push_back("\n");
259 
260  return lines;
261 }
262 
263 }
264 }
265 }