#include <openbabel/babelconfig.h>
#include <openbabel/obmolecformat.h>
#include <openbabel/mol.h>
#include <openbabel/atom.h>
#include <openbabel/elements.h>
#include <openbabel/internalcoord.h>
#include <cstdlib>
using namespace std;
namespace OpenBabel
{
class AmberPrepFormat : public OBMoleculeFormat
{
public:
AmberPrepFormat()
{
OBConversion::RegisterFormat("prep",this);
}
virtual const char* Description() {
return
"Amber Prep format\n"
" Read Options e.g. -as\n"
" s Output single bonds only\n"
" b Disable bonding entirely\n\n";
};
virtual const char* SpecificationURL()
{return "http://amber.scripps.edu/doc/prep.html";};
virtual unsigned int Flags()
{
return NOTWRITABLE;
};
virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
};
AmberPrepFormat theAmberPrepFormat;
bool AmberPrepFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
{
OBMol* pmol = pOb->CastAndClear<OBMol>();
if (pmol == nullptr)
return false;
istream &ifs = *pConv->GetInStream();
OBMol &mol = *pmol;
const char* title = pConv->GetTitle();
char buffer[BUFF_SIZE];
string str,str1;
OBAtom *atom;
OBInternalCoord *coord;
vector<string> vs;
vector<OBInternalCoord*> internals;
mol.BeginModify();
while (ifs.getline(buffer,BUFF_SIZE))
{
tokenize(vs,buffer);
if (vs.size() == 10)
{
atom = mol.NewAtom();
coord = new OBInternalCoord();
if (mol.NumAtoms() > 1)
coord->_a = mol.GetAtom(atoi(vs[4].c_str()));
if (mol.NumAtoms() > 2)
coord->_b = mol.GetAtom(atoi(vs[5].c_str()));
if (mol.NumAtoms() > 3)
coord->_c = mol.GetAtom(atoi(vs[6].c_str()));
coord->_dst = atof(vs[7].c_str());
coord->_ang = atof(vs[8].c_str());
coord->_tor = atof(vs[9].c_str());
internals.push_back(coord);
atom->SetAtomicNum(OBElements::GetAtomicNum(vs[1].c_str()));
if (!ifs.getline(buffer,BUFF_SIZE))
break;
tokenize(vs,buffer);
}
}
if (internals.size() > 0)
InternalToCartesian(internals,mol);
if (!pConv->IsOption("b",OBConversion::INOPTIONS))
mol.ConnectTheDots();
if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
mol.PerceiveBondOrders();
mol.EndModify();
mol.SetTitle(title);
return(true);
}
}