1 |
|
/********************************************************************** |
2 |
< |
atom2md.cpp - OpenBabel-based conversion program to OpenMD MD file, |
2 |
> |
atom2md.cpp - OpenBabel-based conversion program to OpenMD file, |
3 |
|
command-line handling. |
4 |
|
|
5 |
|
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
6 |
|
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison |
7 |
|
Some portions Copyright (C) 2004-2006 by Chris Morley |
8 |
< |
Some portions Copyright (C) 2008 by J. Daniel Gezelter |
8 |
> |
Some portions Copyright (C) 2008-2009 by J. Daniel Gezelter |
9 |
|
|
10 |
|
This file is part of both the OpenMD and Open Babel projects. |
11 |
|
For more information, see <http://openmd.net> and <http://openbabel.sourceforge.net/> |
38 |
|
#if HAVE_CONIO_H |
39 |
|
#include <conio.h> |
40 |
|
#endif |
41 |
– |
#include <cstdlib> |
41 |
|
|
42 |
|
#if !HAVE_STRNCASECMP |
43 |
|
extern "C" int strncasecmp(const char *s1, const char *s2, size_t n); |
44 |
|
#endif |
45 |
|
|
46 |
|
#include <openbabel/obconversion.h> |
47 |
+ |
#include <openbabel/plugin.h> |
48 |
+ |
#include <cstdlib> |
49 |
+ |
#include <cstring> |
50 |
|
|
51 |
|
using namespace std; |
52 |
|
using namespace OpenBabel; |
53 |
|
|
54 |
< |
void DoOption(const char* p, OBConversion& Conv, |
55 |
< |
OBConversion::Option_type typ, int& arg, int argc, |
54 |
< |
char *argv[]); |
54 |
> |
void DoOption(const char* p, OBConversion& Conv, OBConversion::Option_type typ, |
55 |
> |
int& arg, int argc, char *argv[]); |
56 |
|
void usage(); |
57 |
|
void help(); |
58 |
|
|
72 |
|
bool gotInType = false, gotOutType = false; |
73 |
|
bool SplitOrBatch=false; |
74 |
|
|
75 |
< |
char *oext; |
76 |
< |
char *iext; |
75 |
> |
char *oext = NULL; |
76 |
> |
char *iext = NULL; |
77 |
> |
|
78 |
> |
// for use with command name to type conversion |
79 |
|
string inputExt; |
80 |
|
string outputExt; |
81 |
|
|
118 |
|
gotInType = true; |
119 |
|
iext = argv[arg] + 2; |
120 |
|
if(!*iext) |
121 |
< |
iext = argv[++arg]; // space left after -i: use next |
119 |
< |
// argument |
121 |
> |
iext = argv[++arg]; //space left after -i: use next argument |
122 |
|
|
123 |
|
if (strncasecmp(iext, "MIME", 4) == 0) |
124 |
|
{ |
128 |
|
} |
129 |
|
else |
130 |
|
{ |
131 |
< |
// The ID provided by the OBFormat class is used as the |
130 |
< |
// identifying file extension |
131 |
> |
//The ID provided by the OBFormat class is used as the identifying file extension |
132 |
|
pInFormat = Conv.FindFormat(iext); |
133 |
|
} |
134 |
|
if(pInFormat==NULL) |
135 |
|
{ |
136 |
< |
cerr << program_name << ": cannot read input format!" |
136 |
< |
<< endl; |
136 |
> |
cerr << program_name << ": cannot read input format!" << endl; |
137 |
|
usage(); |
138 |
|
} |
139 |
|
break; |
142 |
|
gotOutType = true; |
143 |
|
oext = argv[arg] + 2; |
144 |
|
if(!*oext) |
145 |
< |
oext = argv[++arg]; // space left after -i: use next |
146 |
< |
// argument |
145 |
> |
oext = argv[++arg]; //space left after -i: use next argument |
146 |
|
|
147 |
|
if (strncasecmp(oext, "MIME", 4) == 0) |
148 |
|
{ |
155 |
|
|
156 |
|
if(pOutFormat==NULL) |
157 |
|
{ |
158 |
< |
cerr << program_name << ": cannot write output format!" |
160 |
< |
<< endl; |
158 |
> |
cerr << program_name << ": cannot write output format!" << endl; |
159 |
|
usage(); |
160 |
|
} |
161 |
|
break; |
162 |
< |
|
163 |
< |
case 'F': |
162 |
> |
|
163 |
> |
/*case 'F': |
164 |
|
if(!Conv.SetOutFormat("fpt")) |
165 |
|
cout << "FingerprintFormat needs to be loaded" << endl; |
166 |
|
else |
169 |
|
Conv.Write(NULL); |
170 |
|
} |
171 |
|
return 0; |
172 |
< |
|
172 |
> |
*/ |
173 |
> |
case 'L': //display a list of plugin type or classes |
174 |
> |
{ |
175 |
> |
const char* param=NULL; |
176 |
> |
if(argc>arg+1) |
177 |
> |
param = argv[arg+2]; |
178 |
> |
OBPlugin::List(argv[arg+1], param); |
179 |
> |
return 0; |
180 |
> |
} |
181 |
|
case '?': |
182 |
|
case 'H': |
183 |
< |
if(isalnum(argv[arg][2])) |
183 |
> |
if(isalnum(argv[arg][2]) || arg==argc-2) |
184 |
|
{ |
185 |
|
if(strncasecmp(argv[arg]+2,"all",3)) |
186 |
|
{ |
187 |
< |
OBFormat* pFormat = Conv.FindFormat(argv[arg]+2); |
187 |
> |
const char* pID= (arg==argc-2) ? argv[arg+1] : argv[arg]+2; |
188 |
> |
OBFormat* pFormat = Conv.FindFormat(pID); |
189 |
|
if(pFormat) |
190 |
|
{ |
191 |
< |
cout << argv[arg]+2 << " " << pFormat->Description() << endl; |
191 |
> |
cout << pID << " " << pFormat->Description() << endl; |
192 |
> |
if(pFormat->Flags() & NOTWRITABLE) |
193 |
> |
cout << " This format is Read-only" << endl; |
194 |
> |
if(pFormat->Flags() & NOTREADABLE) |
195 |
> |
cout << " This format is Write-only" << endl; |
196 |
> |
|
197 |
|
if(strlen(pFormat->SpecificationURL())) |
198 |
|
cout << "Specification at: " << pFormat->SpecificationURL() << endl; |
199 |
|
} |
200 |
|
else |
201 |
< |
cout << "Format type: " << argv[arg]+2 << " was not recognized" <<endl; |
201 |
> |
cout << "Format type: " << pID << " was not recognized" <<endl; |
202 |
|
} |
203 |
|
else |
204 |
|
{ |
205 |
< |
Formatpos pos; |
194 |
< |
OBFormat* pFormat; |
195 |
< |
const char* str=NULL; |
196 |
< |
while(OBConversion::GetNextFormat(pos,str,pFormat)) |
197 |
< |
{ |
198 |
< |
if((pFormat->Flags() & NOTWRITABLE) && (pFormat->Flags() & NOTREADABLE)) |
199 |
< |
continue; |
200 |
< |
cout << str << endl; |
201 |
< |
const char* p = strchr(pFormat->Description(),'\n'); |
202 |
< |
cout << p+1; //second line of description |
203 |
< |
if(strlen(pFormat->SpecificationURL())) |
204 |
< |
cout << "Specification at: " << pFormat->SpecificationURL(); |
205 |
< |
cout << endl << endl; |
206 |
< |
} |
205 |
> |
OBPlugin::List("formats","verbose"); |
206 |
|
} |
207 |
|
} |
208 |
|
else |
209 |
|
help(); |
210 |
< |
exit(0); |
210 |
> |
return 0; |
211 |
|
|
212 |
|
case '-': //long option --name text |
213 |
|
{ |
276 |
|
} |
277 |
|
|
278 |
|
// user didn't specify input and output format in commandline option |
279 |
< |
// try to parse it from program name (pdb2mdin means input format is pdb, |
280 |
< |
// output format is mdin) |
279 |
> |
// try to parse it from program name (pdb2md means input format is pdb, |
280 |
> |
// output format is md) |
281 |
|
|
282 |
|
string formatName(program_name); |
283 |
|
pos = formatName.find_first_of("2"); |
310 |
|
outputExt = tmpExt; |
311 |
|
} |
312 |
|
} |
313 |
< |
} |
313 |
> |
} |
314 |
> |
|
315 |
> |
if(!gotOutType) //the last file is the output |
316 |
> |
{ |
317 |
> |
if(FileList.empty()) |
318 |
> |
{ |
319 |
> |
cerr << "No output file or format spec!" << endl; |
320 |
> |
usage(); |
321 |
> |
} |
322 |
> |
OutputFileName = FileList.back(); |
323 |
> |
FileList.pop_back(); |
324 |
> |
} |
325 |
> |
|
326 |
> |
#ifdef _WIN32 |
327 |
> |
//Expand wildcards in input filenames and add to FileList |
328 |
> |
vector<string> tempFileList(FileList); |
329 |
> |
FileList.clear(); |
330 |
> |
vector<string>::iterator itr; |
331 |
> |
for(itr=tempFileList.begin();itr!=tempFileList.end();++itr) |
332 |
> |
DLHandler::findFiles (FileList, *itr); |
333 |
> |
#endif |
334 |
|
|
335 |
|
if (!gotInType) |
336 |
|
{ |
340 |
|
usage(); |
341 |
|
} |
342 |
|
} |
343 |
< |
|
344 |
< |
if (!gotOutType) |
343 |
> |
|
344 |
> |
if (!gotOutType) |
345 |
|
{ |
346 |
|
pOutFormat = Conv.FormatFromExt(OutputFileName.c_str()); |
347 |
|
if(pOutFormat==NULL) |
351 |
|
} |
352 |
|
} |
353 |
|
|
354 |
< |
Conv.SetInAndOutFormats(pInFormat, pOutFormat); |
355 |
< |
|
354 |
> |
if(!Conv.SetInFormat(pInFormat)) |
355 |
> |
{ |
356 |
> |
cerr << "Invalid input format" << endl; |
357 |
> |
usage(); |
358 |
> |
} |
359 |
> |
if(!Conv.SetOutFormat(pOutFormat)) |
360 |
> |
{ |
361 |
> |
cerr << "Invalid output format" << endl; |
362 |
> |
usage(); |
363 |
> |
} |
364 |
> |
|
365 |
|
if(SplitOrBatch) |
366 |
|
{ |
367 |
|
//Put * into output file name before extension (or ext.gz) |
368 |
|
if(OutputFileName.empty()) |
369 |
|
{ |
370 |
|
OutputFileName = "*."; |
371 |
< |
OutputFileName += oext; |
371 |
> |
if (oext != NULL) |
372 |
> |
OutputFileName += oext; |
373 |
|
} |
374 |
|
else |
375 |
|
{ |
386 |
|
} |
387 |
|
|
388 |
|
int count = Conv.FullConvert(FileList, OutputFileName, OutputFileList); |
389 |
< |
|
390 |
< |
// send info message to clog -- don't mess up cerr or cout for user programs |
391 |
< |
//Get the last word on the first line of the description which should |
363 |
< |
//be "molecules", "reactions", etc and remove the s if only one object converted |
364 |
< |
std::string objectname(pOutFormat->TargetClassDescription()); |
365 |
< |
pos = objectname.find('\n'); |
366 |
< |
if(count==1) --pos; |
367 |
< |
objectname.erase(pos); |
368 |
< |
pos = objectname.rfind(' '); |
369 |
< |
if(pos==std::string::npos) |
370 |
< |
pos=0; |
371 |
< |
std::clog << count << objectname.substr(pos) << " converted" << endl; |
389 |
> |
|
390 |
> |
Conv.ReportNumberConverted(count); |
391 |
> |
|
392 |
|
if(OutputFileList.size()>1) |
393 |
|
{ |
394 |
|
clog << OutputFileList.size() << " files output. The first is " << OutputFileList[0] <<endl; |
395 |
|
} |
396 |
< |
|
396 |
> |
|
397 |
|
std::string messageSummary = obErrorLog.GetMessageSummary(); |
398 |
|
if (messageSummary.size()) |
399 |
|
{ |
405 |
|
cout << "Press any key to finish" <<endl; |
406 |
|
getch(); |
407 |
|
#endif |
408 |
+ |
|
409 |
|
return 0; |
410 |
|
} |
411 |
|
|
450 |
|
cout << "Usage: " << program_name |
451 |
|
<< " [-i<input-type>] <name> [-o<output-type>] <name>" << endl; |
452 |
|
cout << "Try -H option for more information." << endl; |
453 |
< |
/* |
453 |
> |
|
454 |
|
#ifdef _DEBUG |
455 |
|
//CM keep window open |
456 |
|
cout << "Press any key to finish" <<endl; |
457 |
|
getch(); |
458 |
|
#endif |
438 |
– |
*/ |
459 |
|
exit (0); |
460 |
|
} |
461 |
|
|