Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mol_parser.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 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // This file is part of the Rosetta software suite and is made available under license.
6 // The Rosetta software is developed by the contributing members of the Rosetta Commons consortium.
7 // (C) 199x-2009 Rosetta Commons participating institutions and developers.
8 // For more information, see http://www.rosettacommons.org/.
9 
10 /// @file core/chemical/sdf/mol_parser.cc
11 ///
12 /// @brief Implementation of molfile parser
13 /// @author Sam DeLuca
14 
15 #include <string>
16 #include <algorithm>
17 #include <utility/vector1.hh>
18 #include <core/chemical/Atom.hh>
19 #include <core/chemical/Orbital.hh>
25 #include <basic/Tracer.hh>
26 #include <utility/io/izstream.hh>
27 #include <utility/string_util.hh>
28 #include <fstream>
29 
30 namespace core {
31 namespace chemical {
32 namespace sdf {
33 
34 static basic::Tracer MolParserTracer("core.chemical.sdf.mol_parser");
35 
36 MolFileParser::MolFileParser(const utility::vector1<std::string> mol_file_lines) : mol_file_lines_(mol_file_lines)
37 {
38  MolParserTracer << "init mol file parser" << std::endl;
39 }
40 
42 {
43  utility::io::izstream infile;
45  infile.open(file_name.c_str());
46  while(!infile.eof())
47  {
48  std::string line;
49  getline(infile,line);
50  lines.push_back(line);
51  }
52  infile.close();
53  mol_file_lines_= lines;
54 }
55 
57 {
58  return *molecule_container_;
59 }
60 
62 {
63  MolParserTracer.Debug << "returning molecule name " << molecule_container_->name() <<std::endl;
64  return molecule_container_;
65 }
66 
68 {
69 
70  MolParserTracer << "**WARNING**: this is extremely experimental, don't use for production purposes yet"<<std::endl;
71  //molecule name
73  //molecule info
75  //molecule comments
77 
79  //mol_data_.print();
80  //make a new residue object, give it a name
81  molecule_container_ = new core::chemical::ResidueType(atom_types, elements, mm_atom_types, orbital_type_set);
83  molecule_container_->name3(molecule_comments_.substr(0,3));
85 
86  molecule_container_->set_mol_data(mol_data_);
87  molecule_container_->add_property("LIGAND");
88 
89  //pass the ctab and the new residue object to the ctab parser
90 
91  //first figure out if we have a V2000 or V3000 connection table
92  MolParserTracer <<mol_file_lines_[4] <<std::endl;
93  std::string ctab_version(mol_file_lines_[4].substr(34,5));
94  MolParserTracer <<"ctab version is \"" << ctab_version <<"\"" << std::endl;
95  if(ctab_version == "V2000")
96  {
97  MolParserTracer.Debug << "found a V2000 ctab" <<std::endl;
98  //the rest is the ctab, copy it
99  utility::vector1<std::string> connection_table;
100  connection_table.resize(mol_file_lines_.size()-3);
101  std::copy(mol_file_lines_.begin()+3,mol_file_lines_.end(),connection_table.begin());
102  MolParserTracer.Debug << "connection table of size " << connection_table.size() << std::endl;
103  V2Parser connection_table_parser(connection_table, molecule_container_,mol_data_);
104  MolParserTracer.Debug << "Passing molecule " << molecule_container_->name() << " to ctab parser" <<std::endl;
105  connection_table_parser.ParseTable();
106  molecule_container_ = connection_table_parser.GetResidueType();
107  }else if(ctab_version == "V3000")
108  {
109  //the rest is the ctab, copy it
110  utility::vector1<std::string> connection_table;
111  connection_table.resize(mol_file_lines_.size()-3);
112  std::copy(mol_file_lines_.begin()+3,mol_file_lines_.end(),connection_table.begin());
113 
114  V3Parser connection_table_parser(connection_table, molecule_container_,mol_data_);
115  connection_table_parser.ParseTable();
116  molecule_container_ = connection_table_parser.GetResidueType();
117  }
118 
119  //MolParserTracer <<"finished"<<std::endl;
120 
121  //fix the order and sort everything
122  //molecule_container_->finalize();
123 
124  //set neighbor atom and internal coordinates
125 
126 
127 
128  molecule_container_->assign_internal_coordinates();
129 
130  //the ctab parser only sets default charges, charges need to be normalized to zero
131 
132  core::Real total_charge = 0.0;
133  for(core::Size index =1; index <= molecule_container_->natoms(); ++index)
134  {
135  total_charge += molecule_container_->atom(index).charge();
136  }
137 
138  core::Real charge_offset = -total_charge/static_cast<core::Real>(molecule_container_->natoms());
139  for(core::Size index = 1;index <= molecule_container_->natoms();++index)
140  {
141  std::string atom_name = molecule_container_->atom_name(index);
142  core::Real starting_charge = molecule_container_->atom(index).charge();
143  molecule_container_->atom( atom_name ).charge(starting_charge+charge_offset);
144  }
145  molecule_container_->finalize();
146 
147  std::string preset_nbr = FindNbrAtom();
148  if(preset_nbr != "")
149  {
150 
151  //if you uncomment this tracer statement ChemicalManager segfaults and i have no idea why
152  MolParserTracer.Debug << "assigning \"" <<preset_nbr<< "\" as nbr atom" <<std::endl;
153  molecule_container_->nbr_atom(preset_nbr);
154  //molecule_container_->assign_neighbor_atom();
155  }else
156  {
157  molecule_container_->assign_neighbor_atom();
158  }
159 
160 }
161 
163 {
164  return molecule_comments_;
165 }
166 
168 {
169  return molecule_name_;
170 }
171 
173 {
174  return molecule_info_;
175 }
176 
178 {
179  std::string nbr_atom_data = mol_data_.get_mol_data("Rosetta nbr_atom");
180  if(nbr_atom_data == "")
181  {
182  return nbr_atom_data;
183  }
184  core::Size nbr_atom_index(utility::from_string<core::Size>(nbr_atom_data,core::Size(0) ) );
185  std::string nbr_atom = molecule_container_->atom_name(nbr_atom_index);
186  /*
187  for(core::Size index=1;index <= mol_file_lines_.size();++index)
188  {
189  std::string line = mol_file_lines_[index];
190 
191  if(line.find("> <Rosetta nbr_atom>")!=std::string::npos)
192  {
193  return mol_file_lines_[index+1];
194  }
195 
196  }
197  */
198  return nbr_atom;
199 }
200 
201 
202 
203 } // sdf
204 } // io
205 } // core