1 |
|
/********************************************************************** |
2 |
|
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
3 |
< |
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison |
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 |
50 |
|
*** But now handled by a command line option in new framework. |
51 |
|
*/ |
52 |
|
|
53 |
< |
#include "gromos96format.hpp" |
53 |
> |
#include "mol.hpp" |
54 |
> |
#include "obconversion.hpp" |
55 |
|
|
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); |
129 |
|
OBMol &mol = *pmol; |
130 |
|
double fac = pConv->IsOption("n") ? 0.1 : 1.0; //new framework |
131 |
|
|
132 |
< |
char type_name[10]; |
133 |
< |
char res_name[10],padded_name[10]; |
132 |
> |
char type_name[16]; |
133 |
> |
char res_name[16]; |
134 |
|
char buffer[BUFF_SIZE]; |
135 |
|
int res_num; |
136 |
|
|
137 |
< |
sprintf(buffer,"#GENERATED BY OPEN BABEL %s",BABEL_VERSION); |
138 |
< |
ofs << buffer << endl; |
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 |
< |
sprintf(buffer,"TITLE\n%s\nEND",mol.GetTitle()); |
142 |
< |
ofs << buffer << endl; |
81 |
< |
ofs << "POSITION" << endl; |
141 |
> |
ofs << "TITLE\n" << mol.GetTitle() << "\nEND\n"; |
142 |
> |
ofs << "POSITION\n"; |
143 |
|
|
144 |
|
OBAtom *atom; |
145 |
|
OBResidue *res; |
149 |
|
{ |
150 |
|
if ( (res = atom->GetResidue()) ) |
151 |
|
{ |
152 |
< |
strcpy(res_name,(char*)res->GetName().c_str()); |
153 |
< |
strcpy(type_name,(char*)res->GetAtomID(atom).c_str()); |
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 |
< |
strcpy(type_name,etab.GetSymbol(atom->GetAtomicNum())); |
161 |
> |
strncpy(type_name,etab.GetSymbol(atom->GetAtomicNum()), 16); |
162 |
|
strcpy(res_name,"UNK"); |
99 |
– |
sprintf(padded_name,"%2s",type_name); |
100 |
– |
strcpy(type_name,padded_name); |
163 |
|
res_num = 1; |
164 |
|
} |
165 |
|
|
166 |
< |
sprintf(buffer,"%5d %5s %5s %6d %15.5f %15.5f %15.5f", |
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 << endl; |
169 |
> |
ofs << buffer; |
170 |
|
|
171 |
|
if (!(atom->GetIdx()%10)) |
172 |
|
{ |
173 |
< |
sprintf(buffer,"# %d",atom->GetIdx()); |
174 |
< |
ofs << buffer << endl; |
173 |
> |
sprintf(buffer,"# %d\n",atom->GetIdx()); |
174 |
> |
ofs << buffer; |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
< |
ofs << "END" << endl; |
178 |
> |
ofs << "END\n"; |
179 |
|
|
180 |
|
return(true); |
181 |
|
} |