LOOS  v2.3.2
xplor-edm-writer.hpp
1 // -------------------------------------------------
2 // ASCII Xplor-formatted Electron Density Map writer
3 // -------------------------------------------------
4 
5 /*
6  This file is part of LOOS.
7 
8  LOOS (Lightweight Object-Oriented Structure library)
9  Copyright (c) 2008 Tod D. Romo, Alan Grossfield
10  Department of Biochemistry and Biophysics
11  School of Medicine & Dentistry, University of Rochester
12 
13  This package (LOOS) is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation under version 3 of the License.
16 
17  This package is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 
27 
28 
29 
30 
31 #if !defined(LOOS_XPLOREDMWRITER_HPP)
32 #define LOOS_XPLOREDMWRITER_HPP
33 
34 #include <loos.hpp>
35 
36 #include <DensityGrid.hpp>
37 #include <SimpleMeta.hpp>
38 
39 namespace loos {
40 
41  namespace DensityTools {
42 
44  template<class T>
45  struct XEDMWriter {
46  XEDMWriter(std::ostream& os) : i(0), mos(os), fmt(5)
47  {
48  fmt.scientific().width(12).right();
49  }
50 
51  void operator()(const T d) {
52  mos << fmt(d);
53  if (i++ >= 5) {
54  mos << std::endl;
55  i = 0;
56  }
57  }
58 
60  void frame(const int k) {
61  if (i != 0) {
62  i = 0;
63  mos << std::endl;
64  }
65  mos << std::setw(8) << k << std::endl;
66  }
67 
68 
69  int i;
70  std::ostream& mos;
71  loos::Fmt fmt;
72  };
73 
74 
75 
77  template<class T> void writeXplorEDM(std::ostream& os, DensityTools::DensityGrid<T>& grid) {
78  loos::GCoord gridmin = grid.minCoord();
79  loos::GCoord gridmax = grid.maxCoord();
80  loos::GCoord delta = grid.gridDelta();
81  loos::GCoord gridsize;
82  DensityTools::DensityGridpoint dims = grid.gridDims();
83 
84  DensityTools::DensityGridpoint mins, maxs, nas;
85 
86  // Handle the header...
87  for (int i=0; i<3; i++) {
88  mins[i] = static_cast<int>(floor(gridmin[i] * delta[i]));
89  maxs[i] = static_cast<int>(floor(gridmax[i] * delta[i]));
90  gridsize[i] = dims[i] / delta[i];
91  }
92  nas = dims;
93 
94  // Special handling for grid meta-data...
95  SimpleMeta meta = grid.metadata();
96  os << std::endl << std::setw(8) << meta.size() << std::endl;
97  for (SimpleMeta::iterator i = meta.begin(); i != meta.end(); ++i)
98  os << *i << std::endl;
99 
100  for (int i=0; i<3; i++)
101  os << std::setw(8) << nas[i] << std::setw(8) << mins[i] << std::setw(8) << maxs[i];
102  os << std::endl;
103 
104  loos::Fmt fc(5);
105  fc.width(12).scientific();
106 
107  // Assume our "crystal" is orthonormal...
108  os << fc(gridsize[0]) << fc(gridsize[1]) << fc(gridsize[2]) << fc(90.0) << fc(90.0) << fc(90.0) << std::endl;
109  os << "ZYX\n";
110 
111  // Instantiate the writing functor...
112  XEDMWriter<T> writer(os);
113 
114  // The format writes out the map a plane at a time, so we extract
115  // a plane via operator[] and operate on that...
116 
117  for (int k=0; k<dims[2]; k++) {
118  DensityTools::DensityGridPlane<T> plane = grid[k];
119 
120  // Prime the output
121  writer.frame(k);
122 
123  for (int j=0; j<dims[1]; j++)
124  for (int i=0; i<dims[0]; i++)
125  writer(plane[j][i]);
126  }
127 
128  os << std::endl << std::endl;
129  }
130 
131  };
132 
133 };
134 
135 
136 #endif
A simple 3D grid class of arbitrary types.
Definition: DensityGrid.hpp:53
void writeXplorEDM(std::ostream &os, DensityTools::DensityGrid< T > &grid)
Write out an DensityTools::DensityGrid as an ASCII formatted X-PLOR electron density map...
Fmt & width(const uint)
Set the output field width.
Definition: Fmt.cpp:18
Fmt & scientific(void)
Output in scientific format.
Definition: Fmt.cpp:13
Output formatter class, adapted from Stroustrup's book.
Definition: Fmt.hpp:25
void frame(const int k)
Special m.f. for starting a new frame.
Namespace for most things not already encapsulated within a class.
Fmt & right(void)
Align right.
Definition: Fmt.cpp:25
Encapsulates an i,j-plane from an DensityGrid.
Definition: DensityGrid.hpp:85
Simple class for handling metadata.
Definition: SimpleMeta.hpp:51
Functor for writing out ASCII formatted X-Plor electron density maps.
iterator begin()
Allow STL-iteration.
Definition: SimpleMeta.hpp:68