clustering  0.12
Clustering suite for molecular dynamics trajectories.
 All Classes Namespaces Files Functions Variables Typedefs
tools.cpp
1 /*
2 Copyright (c) 2015, Florian Sittel (www.lettis.net)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 
26 #include "tools.hpp"
27 
28 #include <stdarg.h>
29 
30 namespace Clustering {
31 namespace Tools {
32 
33 void
34 write_fes(std::string fname, std::vector<float> fes) {
35  std::ofstream ofs(fname);
36  if (ofs.fail()) {
37  std::cerr << "error: cannot open file '" << fname << "'" << std::endl;
38  exit(EXIT_FAILURE);
39  } else {
40  ofs << std::scientific;
41  for (float f: fes) {
42  ofs << f << "\n";
43  }
44  }
45 }
46 
47 void
48 write_pops(std::string fname, std::vector<std::size_t> pops) {
49  // technically the same ...
50  write_clustered_trajectory(fname, pops);
51 }
52 
53 std::vector<std::size_t>
54 read_clustered_trajectory(std::string filename) {
55  std::vector<std::size_t> traj;
56  std::ifstream ifs(filename);
57  if (ifs.fail()) {
58  std::cerr << "error: cannot open file '" << filename << "'" << std::endl;
59  exit(EXIT_FAILURE);
60  } else {
61  while (ifs.good()) {
62  std::size_t buf;
63  ifs >> buf;
64  if ( ! ifs.fail()) {
65  traj.push_back(buf);
66  }
67  }
68  }
69  return traj;
70 }
71 
72 void
73 write_clustered_trajectory(std::string filename, std::vector<std::size_t> traj) {
74  std::ofstream ofs(filename);
75  if (ofs.fail()) {
76  std::cerr << "error: cannot open file '" << filename << "'" << std::endl;
77  exit(EXIT_FAILURE);
78  } else {
79  for (std::size_t c: traj) {
80  ofs << c << "\n";
81  }
82  }
83 }
84 
86 
94 std::string
95 stringprintf(const std::string& str, ...) {
96  unsigned int size = 256;
97  va_list args;
98  char* buf = (char*) malloc(size * sizeof(char));
99  va_start(args, str);
100  while (size <= (unsigned int) vsnprintf(buf, size, str.c_str(), args)) {
101  size *= 2;
102  buf = (char*) realloc(buf, size * sizeof(char));
103  }
104  va_end(args);
105  std::string result(buf);
106  free(buf);
107  return result;
108 }
109 
110 std::vector<float>
111 read_free_energies(std::string filename) {
112  return read_single_column<float>(filename);
113 }
114 
115 std::pair<Neighborhood, Neighborhood>
116 read_neighborhood(const std::string fname) {
117  Neighborhood nh;
118  Neighborhood nh_high_dens;
119  std::ifstream ifs(fname);
120  if (ifs.fail()) {
121  std::cerr << "error: cannot open file '" << fname << "' for reading." << std::endl;
122  exit(EXIT_FAILURE);
123  } else {
124  std::size_t i=0;
125  while (ifs.good()) {
126  std::size_t buf1;
127  float buf2;
128  std::size_t buf3;
129  float buf4;
130  ifs >> buf1;
131  ifs >> buf2;
132  ifs >> buf3;
133  ifs >> buf4;
134  if ( ! ifs.fail()) {
135  nh[i] = std::pair<std::size_t, float>(buf1, buf2);
136  nh_high_dens[i] = std::pair<std::size_t, float>(buf3, buf4);
137  ++i;
138  }
139  }
140  }
141  return {nh, nh_high_dens};
142 }
143 
144 void
145 write_neighborhood(const std::string fname,
146  const Neighborhood& nh,
147  const Neighborhood& nh_high_dens) {
148  std::ofstream ofs(fname);
149  auto p = nh.begin();
150  auto p_hd = nh_high_dens.begin();
151  while (p != nh.end() && p_hd != nh_high_dens.end()) {
152  // first: key (not used)
153  // second: neighbor
154  // second.first: id; second.second: squared dist
155  ofs << p->second.first << " " << p->second.second << " "
156  << p_hd->second.first << " " << p_hd->second.second << "\n";
157  ++p;
158  ++p_hd;
159  }
160 }
161 
162 std::map<std::size_t, std::size_t>
163 microstate_populations(std::vector<std::size_t> traj) {
164  std::map<std::size_t, std::size_t> populations;
165  for (std::size_t state: traj) {
166  if (populations.count(state) == 0) {
167  populations[state] = 1;
168  } else {
169  ++populations[state];
170  }
171  }
172  return populations;
173 }
174 
175 } // end namespace Tools
176 } // end namespace Clustering
177 
std::map< std::size_t, std::size_t > microstate_populations(std::vector< std::size_t > traj)
compute microstate populations from clustered trajectory
Definition: tools.cpp:163
std::vector< float > read_free_energies(std::string filename)
read free energies from plain text file
Definition: tools.cpp:111
void write_pops(std::string fname, std::vector< std::size_t > pops)
write populations as column into given file
Definition: tools.cpp:48
std::map< std::size_t, Clustering::Tools::Neighbor > Neighborhood
map frame id to neighbors
Definition: tools.hpp:55
void write_clustered_trajectory(std::string filename, std::vector< std::size_t > traj)
write state trajectory into plain text file
Definition: tools.cpp:73
void write_fes(std::string fname, std::vector< float > fes)
write free energies as column into given file
Definition: tools.cpp:34
std::pair< Neighborhood, Neighborhood > read_neighborhood(const std::string fname)
Definition: tools.cpp:116
std::vector< std::size_t > read_clustered_trajectory(std::string filename)
read states from trajectory (given as plain text file)
Definition: tools.cpp:54
std::string stringprintf(const std::string &str,...)
printf-version for std::string
Definition: tools.cpp:95
void write_neighborhood(const std::string fname, const Neighborhood &nh, const Neighborhood &nh_high_dens)
Definition: tools.cpp:145