1 |
|
/********************************************************************** |
2 |
< |
main.cpp - Main conversion program, command-line handling. |
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 Open Babel project. |
10 |
< |
For more information, see <http://openbabel.sourceforge.net/> |
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 |
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[]); |
56 |
> |
int& arg, int argc, char *argv[]); |
57 |
|
void usage(); |
58 |
|
void help(); |
59 |
|
|
64 |
|
{ |
65 |
|
registerOBFormats(); |
66 |
|
OBConversion Conv(&cin, &cout); //default input and output are console |
67 |
< |
|
68 |
< |
// string GenOptions; |
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 |
< |
|
73 |
> |
// obMessageLevel filterLevel = obWarning; // 2 out of 5 |
74 |
> |
|
75 |
|
// Parse commandline |
76 |
|
bool gotInType = false, gotOutType = false; |
77 |
|
bool UseSavedOptions = false; |
78 |
< |
|
78 |
> |
|
79 |
|
char *oext; |
80 |
|
char *iext; |
81 |
|
string inputExt; |
82 |
|
string outputExt; |
83 |
< |
|
83 |
> |
|
84 |
|
//Save name of program without its path (and .exe) |
85 |
|
string pn(argv[0]); |
86 |
|
unsigned int pos; |
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 << "Open Babel " << BABEL_VERSION << " -- " |
112 |
< |
<< __DATE__ << " -- " << __TIME__ << endl; |
113 |
< |
exit(0); |
114 |
< |
} |
115 |
< |
|
116 |
< |
case 'i': |
117 |
< |
gotInType = true; |
118 |
< |
iext = argv[arg] + 2; |
119 |
< |
if(!*iext) |
120 |
< |
iext = argv[++arg]; //space left after -i: use next argument |
121 |
< |
|
122 |
< |
if (strncasecmp(iext, "MIME", 4) == 0) |
123 |
< |
{ |
124 |
< |
// get the MIME type from the next argument |
125 |
< |
iext = argv[++arg]; |
126 |
< |
pInFormat = Conv.FormatFromMIME(iext); |
127 |
< |
} |
128 |
< |
else |
129 |
< |
{ |
130 |
< |
//The ID provided by the OBFormat class is used as the identifying file extension |
131 |
< |
pInFormat = Conv.FindFormat(iext); |
132 |
< |
} |
133 |
< |
if(pInFormat==NULL) |
134 |
< |
{ |
135 |
< |
cerr << program_name << ": cannot read input format!" << endl; |
136 |
< |
usage(); |
137 |
< |
} |
138 |
< |
inputExt = iext; |
139 |
< |
break; |
140 |
< |
|
141 |
< |
case 'o': |
142 |
< |
gotOutType = true; |
143 |
< |
oext = argv[arg] + 2; |
144 |
< |
if(!*oext) |
145 |
< |
oext = argv[++arg]; //space left after -i: use next argument |
146 |
< |
|
147 |
< |
if (strncasecmp(oext, "MIME", 4) == 0) |
148 |
< |
{ |
149 |
< |
// get the MIME type from the next argument |
150 |
< |
oext = argv[++arg]; |
151 |
< |
pOutFormat = Conv.FormatFromMIME(oext); |
152 |
< |
} |
153 |
< |
else |
154 |
< |
pOutFormat = Conv.FindFormat(oext); |
155 |
< |
|
156 |
< |
if(pOutFormat==NULL) |
157 |
< |
{ |
158 |
< |
cerr << program_name << ": cannot write output format!" << endl; |
159 |
< |
usage(); |
160 |
< |
} |
161 |
< |
outputExt = oext; |
162 |
< |
break; |
163 |
< |
|
164 |
< |
case '?': |
165 |
< |
case 'H': |
166 |
< |
if(isalnum(argv[arg][2])) |
167 |
< |
{ |
168 |
< |
if(strncasecmp(argv[arg]+2,"all",3)) |
169 |
< |
{ |
170 |
< |
OBFormat* pFormat = Conv.FindFormat(argv[arg]+2); |
171 |
< |
if(pFormat) |
172 |
< |
{ |
173 |
< |
cout << argv[arg]+2 << " " << pFormat->Description() << endl; |
174 |
< |
if(strlen(pFormat->SpecificationURL())) |
175 |
< |
cout << "Specification at: " << pFormat->SpecificationURL() << endl; |
176 |
< |
} |
177 |
< |
else |
178 |
< |
cout << "Format type: " << argv[arg]+2 << " was not recognized" <<endl; |
179 |
< |
} |
180 |
< |
else |
181 |
< |
{ |
182 |
< |
Formatpos pos; |
183 |
< |
OBFormat* pFormat; |
184 |
< |
const char* str=NULL; |
185 |
< |
while(OBConversion::GetNextFormat(pos,str,pFormat)) |
186 |
< |
{ |
187 |
< |
if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE)) |
188 |
< |
continue; |
189 |
< |
cout << str << endl; |
190 |
< |
const char* p = strchr(pFormat->Description(),'\n'); |
191 |
< |
cout << p+1; //second line of description |
192 |
< |
if(strlen(pFormat->SpecificationURL())) |
193 |
< |
cout << "Specification at: " << pFormat->SpecificationURL(); |
194 |
< |
cout << endl << endl; |
195 |
< |
} |
196 |
< |
} |
197 |
< |
} |
198 |
< |
else |
199 |
< |
help(); |
200 |
< |
exit(0); |
201 |
< |
|
202 |
< |
|
203 |
< |
default: //single character general option |
204 |
< |
p = argv[arg]+1; |
205 |
< |
DoOption(p,Conv,OBConversion::GENOPTIONS,arg,argc,argv); |
206 |
< |
break; |
207 |
< |
} |
208 |
< |
} |
209 |
< |
else |
210 |
< |
{ |
211 |
< |
//filenames |
212 |
< |
if(!gotOutType) |
213 |
< |
FileList.push_back(argv[arg]); |
214 |
< |
else |
215 |
< |
OutputFileName = argv[arg]; |
216 |
< |
} |
217 |
< |
} |
218 |
< |
} |
219 |
< |
|
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 |
< |
{ |
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 |
< |
{ |
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 |
< |
|
239 |
> |
} |
240 |
> |
} |
241 |
> |
|
242 |
|
if (!gotOutType) |
243 |
< |
{ |
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 |
< |
} |
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()) |
256 |
> |
|
257 |
> |
if(FileList.empty()) |
258 |
|
{ |
259 |
< |
cerr << "No input file or format spec!" <<endl; |
260 |
< |
usage(); |
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; |
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 |
< |
|
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 ) |
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 |
< |
|
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; |
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 |
< |
} |
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 << "Open Babel " << BABEL_VERSION << " -- " << __DATE__ << " -- " |
330 |
< |
<< __TIME__ << endl; |
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; |
363 |
|
|
364 |
|
|
365 |
|
OBFormat* pDefault = OBConversion::GetDefaultFormat(); |
366 |
< |
if(pDefault) |
367 |
< |
cout << pDefault->TargetClassDescription();// some more options probably for OBMol |
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; |
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 |
< |
} |
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 |
|
|