1 |
/********************************************************************** |
2 |
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
3 |
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison |
4 |
Some portions Copyright (C) 2004 by Chris Morley |
5 |
|
6 |
This program is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation version 2 of the License. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
***********************************************************************/ |
15 |
|
16 |
/* contributed by Walter Scott (wscott@igc.phys.chem.ethz.ch) |
17 |
|
18 |
(Actually the routine was copied from write_xyz and and write_pdb and |
19 |
then modified...) |
20 |
|
21 |
This is a small routine to write a GROMOS96 formatted |
22 |
"position coordinate block" (POSITION) or a |
23 |
"reduced position coordinate block" (POSITIONRED) |
24 |
The former has name information (atom and residue names) while |
25 |
the latter only has coordinates. |
26 |
This version does not support the writing of binary |
27 |
GROMOS files. |
28 |
|
29 |
NOTE 1: the actual formats used in writing out the coordinates |
30 |
do not matter, as GROMOS96 uses free formatted reads. |
31 |
Each line may not be longer than 80 characters. |
32 |
|
33 |
(Note, however, in the POSITION block, the first 24 (twenty four) |
34 |
character on each line are ignored when the line is read in by GROMOS) |
35 |
Comments lines, beginning with hash (#) may occur within a block and are |
36 |
used as delimiters for easier reading. |
37 |
|
38 |
NOTE 2: Many programs specify the units of the coordinates (e.g. Angstrom). |
39 |
GROMOS96 does NOT, as all physical constants, from K_B to EPS are |
40 |
NOT hardwired into the code, but specified by the user. |
41 |
This allows some (mostly Americans) to use GROMOS96 in KCal and |
42 |
Angstrom and the rest of us to use kJoule and nm. |
43 |
It also makes it easy to use reduced units. |
44 |
|
45 |
We get around this by supplying a routine, wr_sco_gr96, which |
46 |
will scale the coordinates by a factor before writing. |
47 |
This routine is then called with the factor set to 1.0 in |
48 |
write_gr96A, or to 0.1 in write_gr96N depending on the users choice. |
49 |
Thus, we always assume that we have read coordinates in Angstrom. |
50 |
*** But now handled by a command line option in new framework. |
51 |
*/ |
52 |
|
53 |
#include "mol.hpp" |
54 |
#include "obconversion.hpp" |
55 |
|
56 |
using namespace std; |
57 |
namespace OpenBabel |
58 |
{ |
59 |
|
60 |
class GROMOS96Format : public OBFormat |
61 |
{ |
62 |
public: |
63 |
//Register this format type ID |
64 |
GROMOS96Format() |
65 |
{ |
66 |
OBConversion::RegisterFormat("gr96",this); |
67 |
} |
68 |
|
69 |
virtual const char* Description() //required |
70 |
{ |
71 |
return |
72 |
"GROMOS96 format\n \ |
73 |
Write Options e.g. -xn\n\ |
74 |
n output nm (not Angstroms)\n"; |
75 |
}; |
76 |
|
77 |
virtual const char* SpecificationURL() |
78 |
{ |
79 |
return ""; |
80 |
}; //optional |
81 |
|
82 |
//Flags() can return be any the following combined by | or be omitted if none apply |
83 |
// NOTREADABLE READONEONLY NOTWRITABLE WRITEONEONLY |
84 |
virtual unsigned int Flags() |
85 |
{ |
86 |
return NOTREADABLE | WRITEONEONLY; |
87 |
}; |
88 |
|
89 |
//////////////////////////////////////////////////// |
90 |
/// The "API" interface functions |
91 |
virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv); |
92 |
|
93 |
//////////////////////////////////////////////////// |
94 |
/// The "Convert" interface functions |
95 |
virtual bool WriteChemObject(OBConversion* pConv) |
96 |
{ |
97 |
//Retrieve the target OBMol |
98 |
OBBase* pOb = pConv->GetChemObject(); |
99 |
OBMol* pmol = dynamic_cast<OBMol*> (pOb); |
100 |
bool ret=false; |
101 |
if(pmol) |
102 |
ret=WriteMolecule(pmol,pConv); |
103 |
|
104 |
std::string auditMsg = "OpenBabel::Write molecule "; |
105 |
std::string description(Description()); |
106 |
auditMsg += description.substr( 0, description.find('\n') ); |
107 |
obErrorLog.ThrowError(__func__, |
108 |
auditMsg, |
109 |
obAuditMsg); |
110 |
|
111 |
delete pOb; |
112 |
return ret; |
113 |
}; |
114 |
}; |
115 |
|
116 |
//Make an instance of the format class |
117 |
GROMOS96Format theGROMOS96Format; |
118 |
|
119 |
//////////////////////////////////////////////////////////////// |
120 |
|
121 |
bool GROMOS96Format::WriteMolecule(OBBase* pOb, OBConversion* pConv) |
122 |
{ |
123 |
OBMol* pmol = dynamic_cast<OBMol*>(pOb); |
124 |
if(pmol==NULL) |
125 |
return false; |
126 |
|
127 |
//Define some references so we can use the old parameter names |
128 |
ostream &ofs = *pConv->GetOutStream(); |
129 |
OBMol &mol = *pmol; |
130 |
double fac = pConv->IsOption("n") ? 0.1 : 1.0; //new framework |
131 |
|
132 |
char type_name[16]; |
133 |
char res_name[16]; |
134 |
char buffer[BUFF_SIZE]; |
135 |
int res_num; |
136 |
|
137 |
snprintf(buffer, BUFF_SIZE, "#GENERATED BY OPEN BABEL %s\n",BABEL_VERSION); |
138 |
ofs << buffer; |
139 |
|
140 |
/* GROMOS wants a TITLE block, so let's write one*/ |
141 |
ofs << "TITLE\n" << mol.GetTitle() << "\nEND\n"; |
142 |
ofs << "POSITION\n"; |
143 |
|
144 |
OBAtom *atom; |
145 |
OBResidue *res; |
146 |
vector<OBNodeBase*>::iterator i; |
147 |
|
148 |
for(atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i)) |
149 |
{ |
150 |
if ( (res = atom->GetResidue()) ) |
151 |
{ |
152 |
// 16 = sizeof(res_name) and sizeof(type_name) |
153 |
strncpy(res_name,(char*)res->GetName().c_str(), 16); |
154 |
res_name[15] = '\0'; |
155 |
strncpy(type_name,(char*)res->GetAtomID(atom).c_str(), 16); |
156 |
type_name[15] = '\0'; |
157 |
res_num = res->GetNum(); |
158 |
} |
159 |
else |
160 |
{ |
161 |
strncpy(type_name,etab.GetSymbol(atom->GetAtomicNum()), 16); |
162 |
strcpy(res_name,"UNK"); |
163 |
res_num = 1; |
164 |
} |
165 |
|
166 |
snprintf(buffer, BUFF_SIZE, "%5d %5s %5s %6d %15.5f %15.5f %15.5f\n", |
167 |
res_num,res_name,type_name,atom->GetIdx(), |
168 |
atom->x()*fac,atom->y()*fac,atom->z()*fac); |
169 |
ofs << buffer; |
170 |
|
171 |
if (!(atom->GetIdx()%10)) |
172 |
{ |
173 |
sprintf(buffer,"# %d\n",atom->GetIdx()); |
174 |
ofs << buffer; |
175 |
} |
176 |
} |
177 |
|
178 |
ofs << "END\n"; |
179 |
|
180 |
return(true); |
181 |
} |
182 |
|
183 |
} //namespace OpenBabel |