LOOS  v2.3.2
grid2xplor.cpp
1 /*
2  grid2xplor.cpp
3 
4 
5  Converts a grid (with a number of types) into an Xplor map...
6 */
7 
8 /*
9  This file is part of LOOS.
10 
11  LOOS (Lightweight Object-Oriented Structure library)
12  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
13  Department of Biochemistry and Biophysics
14  School of Medicine & Dentistry, University of Rochester
15 
16  This package (LOOS) is free software: you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation under version 3 of the License.
19 
20  This package is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 
30 
31 #include <loos.hpp>
32 #include <boost/format.hpp>
33 #include <boost/program_options.hpp>
34 
35 #include <DensityGrid.hpp>
36 #include <xplor-edm-writer.hpp>
37 
38 
39 namespace opts = loos::OptionsFramework;
40 namespace po = boost::program_options;
41 
42 using namespace std;
43 using namespace loos;
44 using namespace loos::DensityTools;
45 
46 
47 // @cond TOOLS_INTERNAL
48 
49 enum GridType { CHAR, INT, FLOAT, DOUBLE };
50 
51 GridType gtype;
52 double scaling;
53 
54 string fullHelpMessage(void) {
55  string msg =
56  "\n"
57  "SYNOPSIS\n"
58  "\n"
59  "\tConvert a LOOS grid into an ASCII XPLOR/CNS electron density map\n"
60  "\n"
61  "DESCRIPTION\n"
62  "\n"
63  "\tThis tool converts a LOOS density grid into an XPLOR/CNS formatted electron density map\n"
64  "that can be use for visualization in PyMol, VMD, Coot, etc. By default, the grid is\n"
65  "assumed to contain double-precision floating point data (i.e. what is normally written\n"
66  "out by the various LOOS tools). Different data types can be converted by specifying\n"
67  "what the grid contains on the command-line.\n"
68  "\nEXAMPLES\n"
69  "\tgrid2xplor <foo.grid >foo.xplor\n"
70  "This converts a typical LOOS grid into an XPLOR density map\n\n"
71  "\tgrid2xplor --type int <foo_id.grid >foo.xplor\n"
72  "This converts an int-grid (from blobid, for example) into a density map\n";
73 
74  return(msg);
75 }
76 
77 
78 
79 class ToolOptions : public opts::OptionsPackage {
80 public:
81 
82  void addGeneric(po::options_description& o) {
83  o.add_options()
84  ("type", po::value<string>(&type)->default_value("double"), "Set the grid type (char, int, float, double)")
85  ("scale", po::value<double>(&scaling)->default_value(1.0), "Scale the grid data");
86  }
87 
88  bool postConditions(po::variables_map& map) {
89  if (type == "double")
90  gtype = DOUBLE;
91  else if (type == "float")
92  gtype = FLOAT;
93  else if (type == "int")
94  gtype = INT;
95  else if (type == "char")
96  gtype = CHAR;
97  else {
98  cerr << "Error- unknown grid type " << type << endl;
99  return(false);
100  }
101  return(true);
102  }
103 
104  string help() const {
105  return(" <foo.grid >foo.xplor");
106  }
107 
108  string print() const {
109  ostringstream oss;
110  oss << boost::format("type='%s',scale='%f'") % type % scaling;
111  return(oss.str());
112  }
113 
114 private:
115  string type;
116 
117 };
118 
119 
120 
121 template<typename T>
122 DensityGrid<double> scaleGrid(DensityGrid<T>& g, const double scale) {
123  DensityGridpoint dims = g.gridDims();
124  long k = dims[0] * dims[1] * dims[2];
125  DensityGrid<double> out(g.minCoord(), g.maxCoord(), g.gridDims());
126 
127  for (long i = 0; i<k; i++)
128  out(i) = g(i) * scale;
129 
130  out.metadata(g.metadata());
131  return(out);
132 }
133 
134 
135 // @endcond
136 
137 int main(int argc, char *argv[]) {
138  string header = invocationHeader(argc, argv);
139  opts::BasicOptions* bopts = new opts::BasicOptions(fullHelpMessage());
140  ToolOptions* topts = new ToolOptions();
141 
142  opts::AggregateOptions options;
143  options.add(bopts).add(topts);
144  if (!options.parse(argc, argv))
145  exit(-1);
146 
148  if (gtype == CHAR) {
149  DensityGrid<char> grid;
150  cin >> grid;
151  edm = scaleGrid(grid, scaling);
152 
153  } else if (gtype == INT) {
154  DensityGrid<int> grid;
155  cin >> grid;
156  edm = scaleGrid(grid, scaling);
157 
158  } else if (gtype == FLOAT) {
159  DensityGrid<float> grid;
160  cin >> grid;
161  edm = scaleGrid(grid, scaling);
162 
163  } else if (gtype == DOUBLE) {
164  DensityGrid<double> grid;
165  cin >> grid;
166  edm = scaleGrid(grid, scaling);
167 
168  } else {
169  cerr << "ERROR- bad grid type internally...\n";
170  exit(-2);
171  }
172 
173  edm.addMetadata(header);
174  GCoord min = edm.minCoord();
175  GCoord max = edm.maxCoord();
176  DensityGridpoint dim = edm.gridDims();
177  cerr << "Read in a grid of size " << dim << endl;
178  cerr << "Grid range is from " << min << " to " << max << endl;
179 
180  writeXplorEDM<double>(cout, edm);
181 
182 }
Namespace for encapsulating options processing.
STL namespace.
bool parse(int argc, char *argv[])
Parses a command line, returning true if parsing was ok.
Options common to all tools (including –fullhelp)
std::string invocationHeader(int argc, char *argv[])
Create an invocation header.
Definition: utils.cpp:124
Namespace for Density package.
Definition: DensityGrid.hpp:48
Namespace for most things not already encapsulated within a class.
Combines a set of OptionsPackages.
AggregateOptions & add(OptionsPackage *pack)
Add a pointer to an OptionsPackage that will be used for options.