IMP  2.0.1
The Integrative Modeling Platform
validate_profile.cpp
1 /**
2  This is the program for validation of SAXS profiles.
3  */
4 #include <IMP/Model.h>
5 #include <IMP/atom/pdb.h>
6 
7 #include <IMP/saxs/Profile.h>
8 
9 #include <fstream>
10 #include <vector>
11 #include <string>
12 
13 #include <boost/program_options.hpp>
14 namespace po = boost::program_options;
15 
16 #if defined(_WIN32) || defined(_WIN64)
17 // Simple basename implementation on platforms that don't have libgen.h
18 const char *basename(const char *path)
19 {
20  int i;
21  for (i = path ? strlen(path) : 0; i > 0; --i) {
22  if (path[i] == '/' || path[i] == '\\') {
23  return &path[i + 1];
24  }
25  }
26  return path;
27 }
28 #else
29 #include <libgen.h>
30 #endif
31 
32 namespace {
33 std::string trim_extension(const std::string file_name) {
34  if(file_name[file_name.size()-4] == '.')
35  return file_name.substr(0, file_name.size() - 4);
36  return file_name;
37 }
38 }
39 
40 int main(int argc, char **argv)
41 {
42  // output arguments
43  for (int i = 0; i < argc; i++) std::cerr << argv[i] << " ";
44  std::cerr << std::endl;
45  float max_q = 0.0;
46  po::options_description desc("Usage: <profile_file1> <profile_file2> ...");
47  desc.add_options()
48  ("help", "Any number of input profiles is supported. \
49 Each profile is read and written back, with simulated error added if necessary")
50  ("max_q,q", po::value<float>(&max_q)->default_value(0.0),
51  "maximal q value")
52  ("input-files", po::value< std::vector<std::string> >(),
53  "input PDB and profile files")
54  ;
55  po::positional_options_description p;
56  p.add("input-files", -1);
57  po::variables_map vm;
58  po::store(
59  po::command_line_parser(argc,argv).options(desc).positional(p).run(), vm);
60  po::notify(vm);
61 
62  std::vector<std::string> files, dat_files;
63  if(vm.count("input-files")) {
64  files = vm["input-files"].as< std::vector<std::string> >();
65  }
66  if(vm.count("help") || files.size() == 0) {
67  std::cout << desc << "\n";
68  return 0;
69  }
70 
71  std::vector<IMP::saxs::Profile *> exp_profiles;
72  for(unsigned int i=0; i<files.size(); i++) {
73  // check if file exists
74  std::ifstream in_file(files[i].c_str());
75  if(!in_file) {
76  std::cerr << "Can't open file " << files[i] << std::endl;
77  exit(1);
78  }
79 
80  IMP::saxs::Profile *profile = new IMP::saxs::Profile(files[i]);
81  if(profile->size() == 0) {
82  std::cerr << "can't parse input file " << files[i] << std::endl;
83  return 1;
84  } else {
85  dat_files.push_back(files[i]);
86  exp_profiles.push_back(profile);
87  std::cout << "Profile read from file " << files[i] << " size = "
88  << profile->size() << std::endl;
89  // write back
90  std::string profile_name =
91  trim_extension(basename(const_cast<char *>(files[i].c_str())));
92  std::string file_name = profile_name + "_v.dat";
93  profile->write_SAXS_file(file_name, max_q);
94  std::cout << "Profile written to file " << file_name << std::endl;
95  }
96  }
97  return 0;
98 }