1 |
/********************************************************************** |
2 |
atom2md.cpp - OpenBabel-based conversion program to OOPSE MD file, |
3 |
command-line handling. |
4 |
|
5 |
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
6 |
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison |
7 |
Some portions Copyright (C) 2004-2005 by Chris Morley |
8 |
|
9 |
This file is part of the OOPSE and Open Babel projects. |
10 |
For more information, see <http://oopse.org> and <http://openbabel.sourceforge.net/> |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation version 2 of the License. |
15 |
|
16 |
This program is distributed in the hope that it will be useful, |
17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
GNU General Public License for more details. |
20 |
***********************************************************************/ |
21 |
|
22 |
#include "config.h" |
23 |
#if HAVE_IOSTREAM |
24 |
#include <iostream> |
25 |
#elif HAVE_IOSTREAM_H |
26 |
#include <iostream.h> |
27 |
#endif |
28 |
#if HAVE_FSTREAM |
29 |
#include <fstream> |
30 |
#elif HAVE_FSTREAM_H |
31 |
#include <fstream.h> |
32 |
#endif |
33 |
#if HAVE_SSTREAM |
34 |
#include <sstream> |
35 |
#elif |
36 |
#include <sstream.h> |
37 |
#endif |
38 |
|
39 |
#include <string> |
40 |
#include <map> |
41 |
#if HAVE_CONIO_H |
42 |
#include <conio.h> |
43 |
#endif |
44 |
|
45 |
#if !HAVE_STRNCASECMP |
46 |
extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); |
47 |
#endif |
48 |
|
49 |
#include "openbabel/obconversion.hpp" |
50 |
#include "brains/Register.hpp" |
51 |
|
52 |
using namespace std; |
53 |
using namespace OpenBabel; |
54 |
using namespace oopse; |
55 |
void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ, |
56 |
int& arg, int argc, char *argv[]); |
57 |
void usage(); |
58 |
void help(); |
59 |
|
60 |
// There isn't a great way to do this -- we need to save argv[0] for usage() |
61 |
static char *program_name; |
62 |
|
63 |
int main(int argc,char *argv[]) |
64 |
{ |
65 |
registerOBFormats(); |
66 |
OBConversion Conv(&cin, &cout); //default input and output are console |
67 |
|
68 |
// string GenOptions; |
69 |
OBFormat* pInFormat = NULL; |
70 |
OBFormat* pOutFormat = NULL; |
71 |
vector<string> FileList, OutputFileList; |
72 |
string OutputFileName; |
73 |
// obMessageLevel filterLevel = obWarning; // 2 out of 5 |
74 |
|
75 |
// Parse commandline |
76 |
bool gotInType = false, gotOutType = false; |
77 |
bool UseSavedOptions = false; |
78 |
|
79 |
char *oext; |
80 |
char *iext; |
81 |
string inputExt; |
82 |
string outputExt; |
83 |
|
84 |
//Save name of program without its path (and .exe) |
85 |
string pn(argv[0]); |
86 |
unsigned int pos; |
87 |
#ifdef _WIN32 |
88 |
pos = pn.find(".exe"); |
89 |
if(pos!=string::npos) |
90 |
argv[0][pos]='\0'; |
91 |
#endif |
92 |
pos = pn.find_last_of("/\\"); |
93 |
if(pos==string::npos) |
94 |
program_name=argv[0]; |
95 |
else |
96 |
program_name=argv[0]+pos+1; |
97 |
|
98 |
const char* p; |
99 |
int arg; |
100 |
for (arg = 1; arg < argc; arg++) |
101 |
{ |
102 |
if (argv[arg]) |
103 |
{ |
104 |
if (argv[arg][0] == '-') |
105 |
{ |
106 |
switch (argv[arg][1]) |
107 |
{ |
108 |
|
109 |
case 'V': |
110 |
{ |
111 |
cout << "atom2md: part of OOPSE " << |
112 |
OOPSE_VERSION_MAJOR << "." << OOPSE_VERSION_MINOR << "." << |
113 |
OOPSE_VERSION_TINY << " and Open Babel " << BABEL_VERSION << " -- " |
114 |
<< __DATE__ << " -- " << __TIME__ << endl; |
115 |
exit(0); |
116 |
} |
117 |
|
118 |
case 'i': |
119 |
gotInType = true; |
120 |
iext = argv[arg] + 2; |
121 |
if(!*iext) |
122 |
iext = argv[++arg]; //space left after -i: use next argument |
123 |
|
124 |
if (strncasecmp(iext, "MIME", 4) == 0) |
125 |
{ |
126 |
// get the MIME type from the next argument |
127 |
iext = argv[++arg]; |
128 |
pInFormat = Conv.FormatFromMIME(iext); |
129 |
} |
130 |
else |
131 |
{ |
132 |
//The ID provided by the OBFormat class is used as the identifying file extension |
133 |
pInFormat = Conv.FindFormat(iext); |
134 |
} |
135 |
if(pInFormat==NULL) |
136 |
{ |
137 |
cerr << program_name << ": cannot read input format!" << endl; |
138 |
usage(); |
139 |
} |
140 |
inputExt = iext; |
141 |
break; |
142 |
|
143 |
case 'o': |
144 |
gotOutType = true; |
145 |
oext = argv[arg] + 2; |
146 |
if(!*oext) |
147 |
oext = argv[++arg]; //space left after -i: use next argument |
148 |
|
149 |
if (strncasecmp(oext, "MIME", 4) == 0) |
150 |
{ |
151 |
// get the MIME type from the next argument |
152 |
oext = argv[++arg]; |
153 |
pOutFormat = Conv.FormatFromMIME(oext); |
154 |
} |
155 |
else |
156 |
pOutFormat = Conv.FindFormat(oext); |
157 |
|
158 |
if(pOutFormat==NULL) |
159 |
{ |
160 |
cerr << program_name << ": cannot write output format!" << endl; |
161 |
usage(); |
162 |
} |
163 |
outputExt = oext; |
164 |
break; |
165 |
|
166 |
case '?': |
167 |
case 'H': |
168 |
if(isalnum(argv[arg][2])) |
169 |
{ |
170 |
if(strncasecmp(argv[arg]+2,"all",3)) |
171 |
{ |
172 |
OBFormat* pFormat = Conv.FindFormat(argv[arg]+2); |
173 |
if(pFormat) |
174 |
{ |
175 |
cout << argv[arg]+2 << " " << pFormat->Description() << endl; |
176 |
if(strlen(pFormat->SpecificationURL())) |
177 |
cout << "Specification at: " << pFormat->SpecificationURL() << endl; |
178 |
} |
179 |
else |
180 |
cout << "Format type: " << argv[arg]+2 << " was not recognized" <<endl; |
181 |
} |
182 |
else |
183 |
{ |
184 |
Formatpos pos; |
185 |
OBFormat* pFormat; |
186 |
const char* str=NULL; |
187 |
while(OBConversion::GetNextFormat(pos,str,pFormat)) |
188 |
{ |
189 |
if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE)) |
190 |
continue; |
191 |
cout << str << endl; |
192 |
const char* p = strchr(pFormat->Description(),'\n'); |
193 |
cout << p+1; //second line of description |
194 |
if(strlen(pFormat->SpecificationURL())) |
195 |
cout << "Specification at: " << pFormat->SpecificationURL(); |
196 |
cout << endl << endl; |
197 |
} |
198 |
} |
199 |
} |
200 |
else |
201 |
help(); |
202 |
exit(0); |
203 |
|
204 |
|
205 |
default: //single character general option |
206 |
p = argv[arg]+1; |
207 |
DoOption(p,Conv,OBConversion::GENOPTIONS,arg,argc,argv); |
208 |
break; |
209 |
} |
210 |
} |
211 |
else |
212 |
{ |
213 |
//filenames |
214 |
if(!gotOutType) |
215 |
FileList.push_back(argv[arg]); |
216 |
else |
217 |
OutputFileName = argv[arg]; |
218 |
} |
219 |
} |
220 |
} |
221 |
|
222 |
//user didn't specify input and output format in commandline option |
223 |
//try to parse it from program name (pdb2mdin means input format is pdb, output format is mdin) |
224 |
string formatName(program_name); |
225 |
pos = formatName.find_first_of("2"); |
226 |
if(pos!=string::npos) { |
227 |
if (!gotInType) |
228 |
{ |
229 |
string tmpExt = formatName.substr(0, pos); |
230 |
pInFormat = Conv.FindFormat(tmpExt.c_str()); |
231 |
if(pInFormat==NULL) |
232 |
{ |
233 |
cerr << program_name << ": cannot read input format!" << endl; |
234 |
usage(); |
235 |
} else |
236 |
{ |
237 |
gotInType = true; |
238 |
inputExt = tmpExt; |
239 |
} |
240 |
} |
241 |
|
242 |
if (!gotOutType) |
243 |
{ |
244 |
string tmpExt = formatName.substr(pos+1, string::npos); |
245 |
pOutFormat = Conv.FindFormat(tmpExt.c_str()); |
246 |
if(pOutFormat==NULL) |
247 |
{ |
248 |
cerr << program_name << ": cannot write output format!" << endl; |
249 |
usage(); |
250 |
}else { |
251 |
gotOutType = true; |
252 |
outputExt = tmpExt; |
253 |
} |
254 |
} |
255 |
} |
256 |
|
257 |
if(FileList.empty()) |
258 |
{ |
259 |
cerr << "No input file or format spec!" <<endl; |
260 |
usage(); |
261 |
} |
262 |
|
263 |
if (OutputFileName.empty()) |
264 |
{ |
265 |
pos = FileList.front().rfind("."); |
266 |
if(pos==string::npos) |
267 |
OutputFileName = FileList.front()+ "." + outputExt; |
268 |
else |
269 |
OutputFileName = FileList.front().substr(0, pos) + "." + outputExt; |
270 |
} |
271 |
|
272 |
Conv.SetInAndOutFormats(pInFormat,pOutFormat); |
273 |
|
274 |
|
275 |
// send info message to clog -- don't mess up cerr or cout for user programs |
276 |
int count = Conv.FullConvert(FileList, OutputFileName, OutputFileList); |
277 |
if ( count == 1 ) |
278 |
clog << count << " molecule converted" << endl; |
279 |
else |
280 |
clog << count << " molecules converted" << endl; |
281 |
|
282 |
if(OutputFileList.size()>1) |
283 |
{ |
284 |
clog << OutputFileList.size() << " files output. The first is " << OutputFileList[0] <<endl; |
285 |
} |
286 |
|
287 |
#ifdef _DEBUG |
288 |
//CM keep window open |
289 |
cout << "Press any key to finish" <<endl; |
290 |
getch(); |
291 |
#endif |
292 |
|
293 |
return 0; |
294 |
} |
295 |
|
296 |
void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ, int& arg, int argc, char *argv[]) |
297 |
{ |
298 |
while(p && *p) //can have multiple single char options |
299 |
{ |
300 |
char ch[2]="?"; |
301 |
*ch = *p++; |
302 |
const char* txt=NULL; |
303 |
//Get the option text if needed |
304 |
int nParams = Conv.GetOptionParams(ch, typ); |
305 |
if(nParams) |
306 |
{ |
307 |
if(*p) |
308 |
{ |
309 |
txt = p; //use text immediately following the option letter |
310 |
p=NULL; //no more single char options |
311 |
} |
312 |
else if(arg<argc-1) |
313 |
{ |
314 |
txt = argv[++arg]; //use text from next arg |
315 |
if(*txt=='-') |
316 |
{ |
317 |
//...unless it is another option |
318 |
cerr << "Option -" << ch << " takes a parameter" << endl; |
319 |
exit(0); |
320 |
} |
321 |
} |
322 |
} |
323 |
Conv.AddOption(ch, typ, txt); |
324 |
} |
325 |
} |
326 |
|
327 |
void usage() |
328 |
{ |
329 |
cout << "atom2md: part of OOPSE " << |
330 |
OOPSE_VERSION_MAJOR << "." << OOPSE_VERSION_MINOR << "." << |
331 |
OOPSE_VERSION_TINY << " and Open Babel " << BABEL_VERSION << " -- " |
332 |
<< __DATE__ << " -- " << __TIME__ << endl; |
333 |
cout << "Usage: " << program_name |
334 |
<< " [-i<input-type>] <name> [-o<output-type>] <name>" << endl; |
335 |
cout << "Try -H option for more information." << endl; |
336 |
/* |
337 |
#ifdef _DEBUG |
338 |
//CM keep window open |
339 |
cout << "Press any key to finish" <<endl; |
340 |
getch(); |
341 |
#endif |
342 |
*/ |
343 |
exit (0); |
344 |
} |
345 |
|
346 |
void help() |
347 |
{ |
348 |
cout << "Open Babel converts chemical structures from one file format to another"<< endl << endl; |
349 |
cout << "Usage: " << program_name << " <input spec> <output spec> [Options]" << endl << endl; |
350 |
cout << "Each spec can be a file whose extension decides the format." << endl; |
351 |
cout << "Optionally the format can be specified by preceding the file by" << endl; |
352 |
cout << "-i<format-type> e.g. -ipdb, for input and -o<format-type> for output" << endl << endl; |
353 |
cout << "See below for available format-types, which are the same as the " << endl; |
354 |
cout << "file extensions and are case independent." << endl; |
355 |
cout << "If no input or output file is given stdin or stdout are used instead." << endl << endl; |
356 |
cout << "More than one input file can be specified and their names can contain" <<endl; |
357 |
cout << "wildcard chars (* and ?).The molecules are aggregated in the output file.\n" << endl; |
358 |
cout << OBConversion::Description(); // Conversion options |
359 |
cout << " -H Outputs this help text" << endl; |
360 |
cout << " -Hxxx (xxx is file format ID e.g. -Hpdb) gives format info" <<endl; |
361 |
cout << " -Hall Outputs details of all formats" <<endl; |
362 |
cout << " -V Outputs version number" <<endl; |
363 |
|
364 |
|
365 |
OBFormat* pDefault = OBConversion::GetDefaultFormat(); |
366 |
if(pDefault) |
367 |
cout << pDefault->TargetClassDescription();// some more options probably for OBMol |
368 |
|
369 |
OBFormat* pAPI= OBConversion::FindFormat("obapi"); |
370 |
if(pAPI) |
371 |
cout << pAPI->Description(); |
372 |
|
373 |
cout << "The following file formats are recognized:" << endl; |
374 |
Formatpos pos; |
375 |
OBFormat* pFormat; |
376 |
const char* str=NULL; |
377 |
while(OBConversion::GetNextFormat(pos,str,pFormat)) |
378 |
{ |
379 |
if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE)) |
380 |
continue; |
381 |
cout << " " << str << endl; |
382 |
} |
383 |
cout << "\nSee further specific info and options using -H<format-type>, e.g. -Hpdb" << endl; |
384 |
} |
385 |
|