LOOS  v2.3.2
sfactories.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 #include <string>
24 
25 #include <sfactories.hpp>
26 #include <sys/stat.h>
27 
28 
29 #include <boost/algorithm/string.hpp>
30 
31 #include <AtomicGroup.hpp>
32 #include <pdb.hpp>
33 #include <psf.hpp>
34 #include <amber.hpp>
35 
36 #include <Trajectory.hpp>
37 #include <dcd.hpp>
38 #include <amber_traj.hpp>
39 
40 #if defined(HAS_NETCDF)
41 #include <amber_netcdf.hpp>
42 #endif
43 
44 #include <amber_rst.hpp>
45 #include <ccpdb.hpp>
46 #include <charmm.hpp>
47 #include <tinkerxyz.hpp>
48 #include <tinker_arc.hpp>
49 #include <gro.hpp>
50 #include <xtc.hpp>
51 #include <trr.hpp>
52 
53 
54 #include <trajwriter.hpp>
55 #include <dcdwriter.hpp>
56 #include <xtcwriter.hpp>
57 
58 namespace loos {
59 
60 
61  namespace internal {
63  std::string suffix;
64  std::string type;
65  pAtomicGroup (*creator)(const std::string& fname);
66  };
67 
68  SystemNameBindingType system_name_bindings[] = {
69  { "prmtop", "Amber", &Amber::create },
70  { "crd", "CHARMM CRD", &CHARMM::create },
71  { "pdb", "CHARMM/NAMD PDB", &PDB::create },
72  { "psf", "CHARMM/NAMD PSF", &PSF::create },
73  { "gro", "Gromacs", &Gromacs::create },
74  { "xyz", "Tinker", &TinkerXYZ::create },
75  { "", "", 0}
76  };
77  }
78 
79 
80 
81 
82  std::string availableSystemFileTypes(const std::string& prefix) {
83  std::string types;
84  for (internal::SystemNameBindingType* p = internal::system_name_bindings; p->creator != 0; ++p) {
85  types += prefix + p->suffix + " = " + p->type + "\n";
86  }
87 
88  return(types);
89  }
90 
91 
92  pAtomicGroup createSystemPtr(const std::string& filename, const std::string& filetype) {
93 
94  for (internal::SystemNameBindingType* p = internal::system_name_bindings; p->creator != 0; ++p)
95  if (p->suffix == filetype)
96  return(*(p->creator))(filename);
97 
98  throw(std::runtime_error("Error- unknown output system file type '" + filetype + "' for file '" + filename + "'. Try --help to see available types."));
99  }
100 
101 
102 
103  pAtomicGroup createSystemPtr(const std::string& filename) {
104 
105  boost::tuple<std::string, std::string> names = splitFilename(filename);
106  std::string suffix = boost::get<1>(names);
107  if (suffix.empty())
108  throw(std::runtime_error("Error- system filename must end in an extension or the filetype must be explicitly specified"));
109 
110  boost::to_lower(suffix);
111  return(createSystemPtr(filename, suffix));
112  }
113 
114 
115  AtomicGroup createSystem(const std::string& filename) {
116  return(*(createSystemPtr(filename)));
117  }
118 
119  AtomicGroup createSystem(const std::string& filename, const std::string& filetype) {
120  return(*(createSystemPtr(filename, filetype)));
121  }
122 
123 
124  namespace internal {
126  std::string suffix;
127  std::string type;
128  pTraj (*creator)(const std::string& fname, const AtomicGroup& model);
129  };
130 
131  TrajectoryNameBindingType trajectory_name_bindings[] = {
132 #if defined(HAS_NETCDF)
133  { "crd", "Amber Traj (NetCDF/Amber)", &AmberNetcdf::create},
134  { "mdcrd", "Amber Traj (NetCDF/Amber)", &AmberNetcdf::create},
135  { "nc", "Amber Traj (NetCDF)", &AmberNetcdf::create},
136  { "netcdf", "Amber Traj (NetCDF)", &AmberNetcdf::create},
137 #else
138  { "crd", "Amber Traj", &AmberTraj::create},
139  { "mdcrd", "Amber Traj", &AmberTraj::create},
140 #endif
141  { "inpcrd", "Amber Restart", &AmberRst::create},
142  { "rst", "Amber Restart", &AmberRst::create},
143  { "rst7", "Amber Restart", &AmberRst::create},
144  { "dcd", "CHARMM/NAMD DCD", &DCD::create},
145  { "pdb", "Concatenated PDB", &CCPDB::create},
146  { "trr", "Gromacs TRR", &TRR::create},
147  { "xtc", "Gromacs XTC", &XTC::create},
148  { "arc", "Tinker ARC", &TinkerArc::create},
149  { "", "", 0}
150  };
151 
152 
153 
154  }
155 
156 
157  std::string availableTrajectoryFileTypes(const std::string& prefix) {
158  std::string types;
159  for (internal::TrajectoryNameBindingType* p = internal::trajectory_name_bindings; p->creator != 0; ++p) {
160  types += prefix + p->suffix + " = " + p->type + "\n";
161  }
162 
163  return(types);
164 
165  }
166 
167 
168  pTraj createTrajectory(const std::string& filename, const std::string& filetype, const AtomicGroup& g) {
169 
170  // First, check to make sure AtomicGroup has index information...
171  if (!g.allHaveProperty(Atom::indexbit))
172  throw(LOOSError("Model passed to createTrajectory() does not have atom index information."));
173 
174  for (internal::TrajectoryNameBindingType* p = internal::trajectory_name_bindings; p->creator != 0; ++p) {
175  if (p->suffix == filetype) {
176  return((*(p->creator))(filename, g) );
177  }
178  }
179  throw(std::runtime_error("Error- unknown input trajectory file type '" + filetype + "' for file '" + filename + "'. Try --help to see available types."));
180 
181  }
182 
183 
184  pTraj createTrajectory(const std::string& filename, const AtomicGroup& g) {
185  boost::tuple<std::string, std::string> names = splitFilename(filename);
186  std::string suffix = boost::get<1>(names);
187 
188  if (suffix.empty())
189  throw(std::runtime_error("Error- trajectory filename must end in an extension or the filetype must be explicitly specified"));
190 
191  boost::to_lower(suffix);
192  return(createTrajectory(filename, suffix, g));
193  }
194 
195 
196  namespace internal {
198  std::string suffix;
199  std::string type;
200  pTrajectoryWriter (*creator)(const std::string& fname, const bool append);
201  };
202 
203  OutputTrajectoryNameBindingType output_trajectory_name_bindings[] = {
204  { "dcd", "NAMD DCD", &DCDWriter::create},
205  { "xtc", "Gromacs XTC (compressed trajectory)", &XTCWriter::create},
206  { "", "", 0}
207  };
208 
209  }
210 
211 
212  std::string availableOutputTrajectoryFileTypes(const std::string& prefix) {
213  std::string types;
214  for (internal::OutputTrajectoryNameBindingType* p = internal::output_trajectory_name_bindings; p->creator != 0; ++p) {
215  types += prefix + p->suffix + " = " + p->type + "\n";
216  }
217 
218  return(types);
219  }
220 
221 
222  pTrajectoryWriter createOutputTrajectory(const std::string& filename, const std::string& type, const bool append) {
223 
224  for (internal::OutputTrajectoryNameBindingType* p = internal::output_trajectory_name_bindings; p->creator != 0; ++p) {
225  if (p->suffix == type) {
226  return((*(p->creator))(filename, append));
227  }
228  }
229 
230  throw(std::runtime_error("Error- unknown output trajectory file type '" + type + "' for file '" + filename + "'. Try --help to see available types."));
231  }
232 
233 
234  pTrajectoryWriter createOutputTrajectory(const std::string& filename, const bool append) {
235  boost::tuple<std::string, std::string> names = splitFilename(filename);
236  std::string suffix = boost::get<1>(names);
237  if (suffix.empty())
238  throw(std::runtime_error("Error- output trajectory filename must end in an extension or the filetype must be explicitly specified"));
239 
240  return(createOutputTrajectory(filename, suffix, append));
241  }
242 
243 
244 }
245 
246 
247 
AtomicGroup createSystem(const std::string &filename)
Factory function for reading in structure files.
Definition: sfactories.cpp:115
static pTrajectoryWriter create(const std::string &s, const bool append=false)
Class factory function.
Definition: xtcwriter.hpp:64
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.