clustering  0.12
Clustering suite for molecular dynamics trajectories.
 All Classes Namespaces Files Functions Variables Typedefs
state_filter.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 "coords_file/coords_file.hpp"
27 
28 #include <boost/program_options.hpp>
29 
30 #include <iostream>
31 #include <string>
32 #include <queue>
33 
34 #include "state_filter.hpp"
35 
36 namespace {
37  int io_error(std::string fname) {
38  std::cerr << "error: cannot open file " << fname << "." << std::endl;
39  return EXIT_FAILURE;
40  }
41 } // end local namespace
42 
43 
44 namespace Clustering {
45 namespace Filter {
46  void
47  main(boost::program_options::variables_map args) {
48  // load states
49  std::string fname_states = args["states"].as<std::string>();
50  std::vector<std::size_t> states;
51  {
52  std::ifstream ifs(fname_states);
53  if (ifs.fail()) {
54  exit(io_error(fname_states));
55  } else {
56  while (ifs.good()) {
57  std::size_t buf;
58  ifs >> buf;
59  if (ifs.good()) {
60  states.push_back(buf);
61  }
62  }
63  }
64  }
65  if (args["list"].as<bool>()) {
66  std::priority_queue<std::pair<std::size_t, std::size_t>> pops;
67  // list states with pops
68  std::set<std::size_t> state_ids(states.begin(), states.end());
69  for (std::size_t id: state_ids) {
70  std::size_t pop = std::count(states.begin(), states.end(), id);
71  pops.push({pop, id});
72  }
73  while ( ! pops.empty()) {
74  auto pop_id = pops.top(); // get top element
75  pops.pop(); // remove top element
76  std::cout << pop_id.second << " " << pop_id.first << "\n";
77  }
78  } else {
79  // filter data
80  std::size_t selected_state = args["state"].as<std::size_t>();
81  CoordsFile::FilePointer coords_in = CoordsFile::open(args["coords"].as<std::string>(), "r");
82  CoordsFile::FilePointer coords_out = CoordsFile::open(args["output"].as<std::string>(), "w");
83  for (std::size_t s: states) {
84  if (s == selected_state) {
85  coords_out->write(coords_in->next());
86  } else {
87  coords_in->next();
88  }
89  }
90  }
91  }
92 } // end namespace Filter
93 } // end namespace Clustering
94 
void main(boost::program_options::variables_map args)