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
/**********************************************************************
Copyright (C) 2007 by Daniel Mansfield
Some portions Copyright (C) 2004-2006 by Chris Morley
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
/*
* File extension module for CaRIne's ASCII Crystal (ACR)
* By Daniel Mansfield
* 30th January 2007
*/
#include <openbabel/babelconfig.h>
#include <openbabel/mol.h>
#include <openbabel/atom.h>
#include <openbabel/elements.h>
#include <openbabel/obmolecformat.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
namespace OpenBabel
{
class ACRFormat : public OBMoleculeFormat
{
public:
//Register this format type ID in the constructor
ACRFormat()
{
OBConversion::RegisterFormat("acr", this, "chemical/x-acr");
// OBConversion::RegisterOptionParam("f", this, 1);
// OBConversion::RegisterOptionParam("n", this);
OBConversion::RegisterOptionParam("s", this, 0, OBConversion::INOPTIONS);
}
virtual const char* Description() //required
{
return
"ACR format\n"
"CaRIne ASCII Crystal format (ACR)\n"
// "Write Options e.g. -xf3 \n"
// " f# Number of (fictional) levels \n"
// " n Omit (virtual) title\n"
"Read Options e.g. -as\n"
" s Consider single bonds only\n";
};
virtual const char* SpecificationURL()
{return "http://pros.orange.fr/carine.crystallography/books/31/carine_31_us.pdf";};
virtual const char* GetMIMEType() { return "chemical/x-acr"; };
virtual unsigned int Flags()
{
return READONEONLY | NOTWRITABLE;
};
virtual int SkipObjects(int n, OBConversion* pConv)
{
return 0;
};
virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
//virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
private:
};
ACRFormat theACRFormat;
bool ACRFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
{
OBMol* pmol = pOb->CastAndClear<OBMol>();
if (pmol == nullptr)
return false;
istream& ifs = *pConv->GetInStream();
pmol->BeginModify();
/** Parse the input stream and use the OpenBabel API to populate the OBMol **/
char buf[BUFF_SIZE];
unsigned int atoms, bonds, tmp;
float scale, dtmp;
bool atom_input = false, bond_input = false;
string type;
//int from, to;
double X,Y,Z;
vector<string> vs;
// read in one at a time
/* WARNING: Atom id starts from zero in Carine; not so in openbabel.
* Solution: Let Open Babel to set them. */
while (true) {
ifs.getline(buf, BUFF_SIZE);
if (ifs.eof()) {
break;
}
if (sscanf(buf, "General Scale=%f\n", &dtmp)) {
scale = dtmp;
continue;
} else if (sscanf(buf, "Number of Atoms in Crystal=%d\n", &tmp)) {
atoms = tmp;
atom_input = true;
// read table column names
ifs.getline(buf, BUFF_SIZE);
continue;
} else if (sscanf(buf, "Number of Links in Crystal=%d\n", &tmp)) {
atom_input = false;
bond_input = true;
bonds = tmp;
// read table column names
ifs.getline(buf, BUFF_SIZE);
continue;
} else if ( '#' == buf[0] || '\r' == buf[0] || '\n' == buf[0] ) {
// between sections, in both windows and unix.
continue;
}
tokenize(vs, buf, " \t\r\n");
if (atom_input) {
if (vs.size() < 9) return false; // timvdm 18/06/2008
type = vs[1];
X = atof((char*)vs[6].c_str())/scale;
Y = atof((char*)vs[7].c_str())/scale;
Z = atof((char*)vs[8].c_str())/scale;
OBAtom* a = pmol->NewAtom();
if (*(type.c_str()) != '*')
a->SetAtomicNum(OBElements::GetAtomicNum(type.c_str()));
a->SetVector(X,Y,Z);
} else if (bond_input) {
if (vs.size() < 2) return false; // timvdm 18/06/2008
// add to pmol
if (!pmol->AddBond(atoi((char*)vs[0].c_str()) + 1, atoi((char*)vs[1].c_str()) + 1,
1 /* bond order not specified in Carine, use PerceiveBondOrder later */))
{
obErrorLog.ThrowError(__FUNCTION__, "addition of bond between " + vs[0] + " and " + vs[1] + " failed", obError);
return false;
}
}
}
/* got sanity? */
if ( pmol->NumBonds() != bonds ) {
// then we read a different number of bonds than those promised.
obErrorLog.ThrowError(__FUNCTION__, "Number of bonds read does not match the number promised", obError);
return false;
} else if ( pmol->NumAtoms() != atoms ) {
obErrorLog.ThrowError(__FUNCTION__, "Number of atoms read does not match the number promised", obError);
return false;
}
pmol->PerceiveBondOrders();
pmol->EndModify();
return true;
}
} //namespace OpenBabel