File: split.cpp

package info (click to toggle)
autodock-vina 1.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 660 kB
  • sloc: cpp: 6,512; makefile: 45
file content (245 lines) | stat: -rw-r--r-- 7,784 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*

   Copyright (c) 2006-2010, The Scripps Research Institute

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

   Author: Dr. Oleg Trott <ot14@columbia.edu>, 
           The Olson Lab, 
           The Scripps Research Institute

*/

#include <iostream>
#include <string>
#include <exception>
#include <vector> // ligand paths
#include <list>
#include <fstream> //  getline?
#include <cmath> // for ceila
#include <boost/program_options.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/convenience.hpp> // filesystem::basename

#include "file.h"
#include "parse_error.h"

using boost::filesystem::path;

path make_path(const std::string& str) {
	return path(str);
}

std::string default_prefix(const std::string& input_name, const std::string& add) {
	std::string tmp = input_name;
	if(tmp.size() >= 6 && tmp.substr(tmp.size()-6, 6) == ".pdbqt")
		tmp.resize(tmp.size() - 6); // FIXME?
	return tmp + add;
}

struct usage_error : public std::runtime_error {
	usage_error(const std::string& message) : std::runtime_error(message) {}
};

typedef std::list<std::string> strl;

struct model {
	strl ligand;
	strl flex;
};

typedef std::list<model> models;

models parse_multimodel_pdbqt(const std::string& input) {
	const path p = make_path(input);
	ifile in(p);
	models tmp;
	unsigned count = 0;
	std::string str;
	bool parsing_model = false;
	bool parsing_ligand = true;
	while(std::getline(in, str)) {
		++count;
		if(starts_with(str, "MODEL")) {
			if(parsing_model == true || parsing_ligand == false) 
				throw parse_error(p, count, "Misplaced MODEL tag");
			tmp.push_back(model());
			parsing_model = true;
		}
		else if(starts_with(str, "ENDMDL")) {
			if(parsing_model == false || parsing_ligand == false)
				throw parse_error(p, count, "Misplaced ENDMDL tag");
			parsing_model = false;
		}
		else if(starts_with(str, "BEGIN_RES")) {
			if(parsing_model == false || parsing_ligand == false)
				throw parse_error(p, count, "Misplaced BEGIN_RES tag");
			parsing_ligand = false;
			tmp.back().flex.push_back(str);
		}
		else if(starts_with(str, "END_RES")) {
			if(parsing_model == false || parsing_ligand == true)
				throw parse_error(p, count, "Misplaced END_RES tag");
			parsing_ligand = true;
			tmp.back().flex.push_back(str);
		}
		else {
			if(parsing_model == false)
				throw parse_error(p, count, "Input occurs outside MODEL");
			if(parsing_ligand) {
				tmp.back().ligand.push_back(str);
			}
			else {
				tmp.back().flex.push_back(str);
			}
		}
	}
	if(parsing_model == true)
		throw parse_error(p, count+1, "Missing ENDMDL tag");
	return tmp;
}

void write_pdbqt(const strl& lines, const std::string& name) {
	if(lines.size() > 0) {
		ofile out(make_path(name));
		for(strl::const_iterator it = lines.begin(); it != lines.end(); ++it)
			out << *it << '\n';
	}
}

void write_multimodel_pdbqt(const models& m, const std::string& ligand_prefix, const std::string& flex_prefix) {
	sz how_many = m.size();
	std::streamsize w = static_cast<std::streamsize>(to_string(how_many).size());
	sz counter = 0;
	for(models::const_iterator it = m.begin(); it != m.end(); ++it) {
		++counter;
		const std::string add = to_string(counter, w, '0') + ".pdbqt";
		write_pdbqt(it->ligand, ligand_prefix + add);
		write_pdbqt(it->flex,   flex_prefix   + add);
	}
}

int main(int argc, char* argv[]) {
	using namespace boost::program_options;
	const bool advanced = false;
	const std::string version_string = "AutoDock Vina PDBQT Split 1.1.2 (May 11, 2011)";

	const std::string error_message = "\n\n\
Please contact the author, Dr. Oleg Trott <ot14@columbia.edu>, so\n\
that this problem can be resolved. The reproducibility of the\n\
error may be vital, so please remember to include the following in\n\
your problem report:\n\
* the EXACT error message,\n\
* your version of the program,\n\
* the computer system you are running it on,\n\
* command line and configuration file options,\n\
* input (if possible),\n\
\n\
Thank you!\n";

	try {
		std::string input_name, ligand_prefix = "ligand_", flex_prefix = "flex_";
		bool help = false, version = false;
		options_description inputs("Input");
		inputs.add_options()
			("input", value<std::string>(&input_name), "input to split (PDBQT)")
		;
		options_description outputs("Output (optional) - defaults are chosen based on the input file name");
		outputs.add_options()
			("ligand", value<std::string>(&ligand_prefix), "prefix for ligands")
			("flex", value<std::string>(&flex_prefix), "prefix for side chains")
		;
		options_description info("Information (optional)");
		info.add_options()
			("help", bool_switch(&help), "print this message")
			("version", bool_switch(&version), "print program version")
		;
		options_description desc;
		desc.add(inputs).add(outputs).add(info);

		positional_options_description positional; // remains empty
		variables_map vm;
		try {
			store(command_line_parser(argc, argv)
				.options(desc)
				.style(command_line_style::default_style ^ command_line_style::allow_guessing)
				.positional(positional)
				.run(), 
				vm);
			notify(vm); 
		}
		catch(boost::program_options::error& e) {
			std::cerr << "Command line parse error: " << e.what() << '\n' << "\nCorrect usage:\n" << desc << '\n';
			return 1;
		}
		if(help) {
			std::cout << desc << '\n';
			return 0;
		}
		if(version) {
			std::cout << version_string << '\n';
			return 0;
		}

		if(vm.count("input") <= 0) {
			std::cerr << "Missing input.\n" << "\nCorrect usage:\n" << desc << '\n';
			return 1;
		}
		if(vm.count("ligand") <= 0) {
			ligand_prefix = default_prefix(input_name, "_ligand_");
			std::cout << "Prefix for ligands will be " << ligand_prefix << '\n';
		}
		if(vm.count("flex") <= 0) {
			flex_prefix = default_prefix(input_name, "_flex_");
			std::cout << "Prefix for flexible side chains will be " << flex_prefix << '\n';
		}
		const models tmp = parse_multimodel_pdbqt(input_name);
		write_multimodel_pdbqt(tmp, ligand_prefix, flex_prefix);
	}
	catch(file_error& e) {
		std::cerr << "\n\nError: could not open \"" << e.name.string() << "\" for " << (e.in ? "reading" : "writing") << ".\n";
		return 1;
	}
	catch(boost::filesystem::filesystem_error& e) {
		std::cerr << "\n\nFile system error: " << e.what() << '\n';
		return 1;
	}
	catch(usage_error& e) {
		std::cerr << "\n\nUsage error: " << e.what() << ".\n";
		return 1;
	}
	catch(parse_error& e) {
		std::cerr << "\n\nParse error on line " << e.line << " in file \"" << e.file.string() << "\": " << e.reason << '\n';
		return 1;
	}
	catch(std::bad_alloc&) {
		std::cerr << "\n\nError: insufficient memory!\n";
		return 1;
	}

	// Errors that shouldn't happen:

	catch(std::exception& e) { 
		std::cerr << "\n\nAn error occurred: " << e.what() << ". " << error_message;
		return 1; 
	}
	catch(internal_error& e) {
		std::cerr << "\n\nAn internal error occurred in " << e.file << "(" << e.line << "). " << error_message;
		return 1;
	}
	catch(...) {
		std::cerr << "\n\nAn unknown error occurred. " << error_message;
		return 1;
	}
}