61 |
|
return OBConversion::GetDefaultFormat()->TargetClassDescription(); |
62 |
|
else |
63 |
|
return ""; |
64 |
< |
}; |
64 |
> |
} |
65 |
|
const type_info& OBFormat::GetType() |
66 |
|
{ |
67 |
|
//Provides info on class of default format unless overridden |
69 |
|
return OBConversion::GetDefaultFormat()->GetType(); |
70 |
|
else |
71 |
|
return typeid(this); //rubbish return if DefaultFormat not set |
72 |
< |
}; |
73 |
< |
|
74 |
< |
//*************************************************** |
75 |
< |
|
76 |
< |
/** @class OBConversion |
77 |
< |
OBConversion maintains a list of the available formats, |
78 |
< |
provides information on them, and controls the conversion process. |
79 |
< |
|
80 |
< |
A conversion is carried out by the calling routine, usually in a |
81 |
< |
user interface or an application program, making an instance of |
82 |
< |
OBConversion. It is loaded with the in and out formats, any options |
83 |
< |
and (usually) the default streams for input and output. Then either |
84 |
< |
the Convert() function is called, which allows a single input file |
85 |
< |
to be converted, or the extended functionality of FullConvert() |
86 |
< |
is used. This allows multiple input and output files, allowing: |
87 |
< |
- aggregation - the contents of many input files converted |
88 |
< |
and sent to one output file; |
89 |
< |
- splitting - the molecules from one input file sent to |
90 |
< |
separate output files; |
91 |
< |
- batch conversion - each input file converted to an output file. |
92 |
< |
|
93 |
< |
These procedures constitute the "Convert" interface. OBConversion |
94 |
< |
and the user interface or application program do not need to be |
95 |
< |
aware of any other part of OpenBabel - mol.h is not \#included. This |
96 |
< |
allows any chemical object derived from OBBase to be converted; |
97 |
< |
the type of object is decided by the input format class. |
98 |
< |
However,currently, almost all the conversions are for molecules of |
99 |
< |
class OBMol. |
100 |
< |
/// |
101 |
< |
OBConversion can also be used with an "API" interface |
102 |
< |
called from programs which manipulate chemical objects. Input/output is |
103 |
< |
done with the Read() and Write() functions which work with any |
104 |
< |
chemical object, but need to have its type specified. (The |
105 |
< |
ReadMolecule() and WriteMolecule() functions of the format classes |
106 |
< |
can also be used directly.) |
107 |
< |
|
108 |
< |
|
109 |
< |
Example code using OBConversion |
110 |
< |
|
111 |
< |
<b>To read in a molecule, manipulate it and write it out.</b> |
112 |
< |
|
113 |
< |
Set up an istream and an ostream, to and from files or elsewhere. |
114 |
< |
(cin and cout are used in the example). Specify the file formats. |
115 |
< |
|
116 |
< |
@code |
117 |
< |
OBConversion conv(&cin,&cout); |
118 |
< |
if(conv.SetInAndOutFormats("SMI","MOL")) |
119 |
< |
{ |
120 |
< |
OBMol mol; |
121 |
< |
if(conv.Read(&mol)) |
122 |
< |
...manipulate molecule |
123 |
< |
|
124 |
< |
conv->Write(&mol); |
125 |
< |
} |
126 |
< |
@endcode |
127 |
< |
|
128 |
< |
A two stage construction is used to allow error handling |
129 |
< |
if the format ID is not recognized. This is necessary now that the |
130 |
< |
formats are dynamic and errors are not caught at compile time. |
131 |
< |
OBConversion::Read() is a templated function so that objects derived |
132 |
< |
from OBBase can also be handled, in addition to OBMol, if the format |
133 |
< |
routines are written appropriately. |
134 |
< |
|
135 |
< |
<b>To make a molecule from a SMILES string.</b> |
136 |
< |
@code |
137 |
< |
std::string SmilesString; |
138 |
< |
OBMol mol; |
139 |
< |
stringstream ss(SmilesString) |
140 |
< |
OBConversion conv(&ss); |
141 |
< |
if(conv.SetInFormat("smi") && conv.Read(&mol)) |
142 |
< |
... |
143 |
< |
@endcode |
144 |
< |
|
145 |
< |
<b>To do a file conversion without manipulating the molecule.</b> |
146 |
< |
|
147 |
< |
@code |
148 |
< |
#include "obconversion.h" //mol.h is not needed |
149 |
< |
...set up an istream is and an ostream os |
150 |
< |
OBConversion conv(&is,&os); |
151 |
< |
if(conv.SetInAndOutFormats("SMI","MOL")) |
152 |
< |
{ |
153 |
< |
conv.SetOptions("h"); //Optional; (h adds expicit hydrogens) |
154 |
< |
conv.Convert(); |
155 |
< |
} |
156 |
< |
@endcode |
157 |
< |
|
158 |
< |
<b>To add automatic format conversion to an existing program.</b> |
159 |
< |
|
160 |
< |
The existing program inputs from the file identified by the |
161 |
< |
const char* filename into the istream is. The file is assumed to have |
162 |
< |
a format ORIG, but otherformats, identified by their file extensions, |
163 |
< |
can now be used. |
164 |
< |
|
165 |
< |
@code |
166 |
< |
ifstream ifs(filename); //Original code |
167 |
< |
|
168 |
< |
OBConversion conv; |
169 |
< |
OBFormat* inFormat = conv.FormatFromExt(filename); |
170 |
< |
OBFormat* outFormat = conv.GetFormat("ORIG"); |
171 |
< |
istream* pIn = &ifs; |
172 |
< |
stringstream newstream; |
173 |
< |
if(inFormat && outFormat) |
174 |
< |
{ |
175 |
< |
conv.SetInAndOutFormats(inFormat,outFormat); |
176 |
< |
conv.Convert(pIn,&newstream); |
177 |
< |
pIn=&newstream; |
178 |
< |
} |
179 |
< |
//else error; new features not available; fallback to original functionality |
72 |
> |
} |
73 |
|
|
181 |
– |
...Carry on with original code using pIn |
182 |
– |
@endcode |
74 |
|
|
184 |
– |
In Windows a degree of independence from OpenBabel can be achieved using DLLs. |
185 |
– |
This code would be linked with obconv.lib. |
186 |
– |
At runtime the following DLLs would be in the executable directory: |
187 |
– |
obconv.dll, obdll.dll, one or more *.obf format files. |
188 |
– |
*/ |
189 |
– |
|
75 |
|
int OBConversion::FormatFilesLoaded = 0; |
76 |
|
|
77 |
|
OBFormat* OBConversion::pDefaultFormat=NULL; |