1 /**********************************************************************
2 Copyright (C) 2000 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 #include <openbabel/babelconfig.h>
16 
17 #include <openbabel/obmolecformat.h>
18 #include <openbabel/mol.h>
19 #include <openbabel/atom.h>
20 #include <openbabel/elements.h>
21 #include <openbabel/internalcoord.h>
22 
23 #include <cstdlib>
24 
25 using namespace std;
26 namespace OpenBabel
27 {
28 
29 class AmberPrepFormat : public OBMoleculeFormat
30 {
31 public:
32     //Register this format type ID
AmberPrepFormat()33     AmberPrepFormat()
34     {
35         OBConversion::RegisterFormat("prep",this);
36     }
37 
Description()38   virtual const char* Description() //required
39   {
40     return
41       "Amber Prep format\n"
42       " Read Options e.g. -as\n"
43       " s  Output single bonds only\n"
44       " b  Disable bonding entirely\n\n";
45   };
46 
SpecificationURL()47   virtual const char* SpecificationURL()
48   {return "http://amber.scripps.edu/doc/prep.html";};
49 
50     //Flags() can return be any the following combined by | or be omitted if none apply
51     // NOTREADABLE  READONEONLY  NOTWRITABLE  WRITEONEONLY
Flags()52     virtual unsigned int Flags()
53     {
54         return NOTWRITABLE;
55     };
56 
57     ////////////////////////////////////////////////////
58     /// The "API" interface functions
59     virtual bool ReadMolecule(OBBase* pOb, OBConversion* pConv);
60 };
61 
62 //Make an instance of the format class
63 AmberPrepFormat theAmberPrepFormat;
64 
65 /////////////////////////////////////////////////////////////////
ReadMolecule(OBBase * pOb,OBConversion * pConv)66 bool AmberPrepFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
67 {
68 
69     OBMol* pmol = pOb->CastAndClear<OBMol>();
70     if (pmol == nullptr)
71         return false;
72 
73     //Define some references so we can use the old parameter names
74     istream &ifs = *pConv->GetInStream();
75     OBMol &mol = *pmol;
76     const char* title = pConv->GetTitle();
77 
78     char buffer[BUFF_SIZE];
79     string str,str1;
80     OBAtom *atom;
81     OBInternalCoord *coord;
82     vector<string> vs;
83     vector<OBInternalCoord*> internals;
84 
85     mol.BeginModify();
86 
87     while	(ifs.getline(buffer,BUFF_SIZE))
88     {
89         tokenize(vs,buffer);
90         if (vs.size() == 10)
91         {
92             atom = mol.NewAtom();
93             coord = new OBInternalCoord();
94             if (mol.NumAtoms() > 1)
95                 coord->_a = mol.GetAtom(atoi(vs[4].c_str()));
96             if (mol.NumAtoms() > 2)
97                 coord->_b = mol.GetAtom(atoi(vs[5].c_str()));
98             if (mol.NumAtoms() > 3)
99                 coord->_c = mol.GetAtom(atoi(vs[6].c_str()));
100             coord->_dst = atof(vs[7].c_str());
101             coord->_ang = atof(vs[8].c_str());
102             coord->_tor = atof(vs[9].c_str());
103             internals.push_back(coord);
104 
105             atom->SetAtomicNum(OBElements::GetAtomicNum(vs[1].c_str()));
106 
107             if (!ifs.getline(buffer,BUFF_SIZE))
108                 break;
109             tokenize(vs,buffer);
110         }
111     }
112 
113     if (internals.size() > 0)
114       InternalToCartesian(internals,mol);
115 
116     if (!pConv->IsOption("b",OBConversion::INOPTIONS))
117       mol.ConnectTheDots();
118     if (!pConv->IsOption("s",OBConversion::INOPTIONS) && !pConv->IsOption("b",OBConversion::INOPTIONS))
119       mol.PerceiveBondOrders();
120 
121     mol.EndModify();
122     mol.SetTitle(title);
123     return(true);
124 }
125 
126 } //namespace OpenBabel
127