~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to src/mmod.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**********************************************************************
2
 
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
3
 
Some portions Copyright (c) 2001-2003 by Geoffrey R. Hutchison
4
 
 
5
 
This program is free software; you can redistribute it and/or modify
6
 
it under the terms of the GNU General Public License as published by
7
 
the Free Software Foundation version 2 of the License.
8
 
 
9
 
This program is distributed in the hope that it will be useful,
10
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
GNU General Public License for more details.
13
 
***********************************************************************/
14
 
 
15
 
#include "mol.h"
16
 
#include "obutil.h"
17
 
#include "typer.h"
18
 
 
19
 
using namespace std;
20
 
 
21
 
namespace OpenBabel
22
 
{
23
 
 
24
 
bool ReadMacroModel(istream &ifs, OBMol &mol,const char *defaultTitle)
25
 
26
 
  // Get Title
27
 
  char buffer[BUFF_SIZE];
28
 
  int natoms;
29
 
  vector<vector<pair<int,int> > > connections;
30
 
  
31
 
  if (ifs.getline(buffer,BUFF_SIZE))
32
 
    {
33
 
      vector<string> vs;
34
 
      tokenize(vs,buffer," \n");
35
 
      
36
 
      if ( !vs.empty() && vs.size() > 0)
37
 
        sscanf(buffer,"%i%*s",&natoms);
38
 
      
39
 
      if ( !vs.empty() && vs.size() > 1)
40
 
        mol.SetTitle(vs[1]);
41
 
      else
42
 
        {
43
 
          string s = defaultTitle;
44
 
          mol.SetTitle(defaultTitle);
45
 
        }
46
 
    }
47
 
  else return(false);
48
 
 
49
 
  mol.BeginModify();
50
 
  mol.ReserveAtoms(natoms);
51
 
  connections.resize(natoms+1);
52
 
 
53
 
  /***********************************************************************/
54
 
  
55
 
  // Get Type Bonds, BondOrder, X, Y, Z
56
 
  
57
 
  double x,y,z;
58
 
  vector3 v;
59
 
  char temp_type[10];
60
 
  int i,j;
61
 
  double charge;
62
 
  OBAtom atom;
63
 
 
64
 
  ttab.SetFromType("MMD");  
65
 
  for (i = 1; i <= natoms; i++)
66
 
    {
67
 
      if (!ifs.getline(buffer,BUFF_SIZE)) break;
68
 
      
69
 
      int end[6], order[6]; 
70
 
 
71
 
      sscanf(buffer,"%s%d%d%d%d%d%d%d%d%d%d%d%d%lf%lf%lf", 
72
 
             temp_type,&end[0],&order[0],&end[1],&order[1],&end[2],&order[2],
73
 
             &end[3], &order[3], &end[4], &order[4], &end[5], &order[5],
74
 
             &x, &y, &z);
75
 
 
76
 
      pair<int,int> tmp;
77
 
      for ( j = 0 ; j <=5 ; j++ )
78
 
        {
79
 
          if ( end[j] > 0  && end[j] > i)
80
 
            {
81
 
              tmp.first = end[j];
82
 
              tmp.second = order[j];
83
 
              connections[i].push_back(tmp);
84
 
            }
85
 
        }
86
 
      
87
 
      v.SetX(x); v.SetY(y); v.SetZ(z);
88
 
      atom.SetVector(v);
89
 
      
90
 
      string str = temp_type,str1;
91
 
      ttab.SetToType("ATN"); ttab.Translate(str1,str);
92
 
      atom.SetAtomicNum(atoi(str1.c_str()));
93
 
      ttab.SetToType("INT"); ttab.Translate(str1,str);
94
 
      atom.SetType(str1);
95
 
      
96
 
      // stuff for optional fields      
97
 
      
98
 
      buffer[109]='\0';
99
 
      sscanf(&buffer[101],"%lf", &charge);  
100
 
      atom.SetPartialCharge(charge);
101
 
      mol.AddAtom(atom);
102
 
    }
103
 
  
104
 
  for (i = 1; i <= natoms; i++)
105
 
    for (j = 0; j < (signed)connections[i].size(); j++)
106
 
      mol.AddBond(i, connections[i][j].first, connections[i][j].second);
107
 
 
108
 
  mol.EndModify();
109
 
 
110
 
  OBBond *bond;
111
 
  vector<OBEdgeBase*>::iterator bi;
112
 
  for (bond = mol.BeginBond(bi);bond;bond = mol.NextBond(bi))
113
 
    if (bond->GetBO() == 5 && !bond->IsInRing())
114
 
      bond->SetBO(1);
115
 
  
116
 
  if ( natoms != (signed)mol.NumAtoms() ) return(false);
117
 
 
118
 
  return(true);
119
 
}
120
 
 
121
 
bool WriteMacroModel(ostream &ofs,OBMol &mol)
122
 
{
123
 
  char buffer[BUFF_SIZE];
124
 
  sprintf(buffer," %5d %6s      E = %7.3f KJ/mol",
125
 
          mol.NumAtoms(),mol.GetTitle(),4.184*mol.GetEnergy());
126
 
  ofs << buffer << endl;
127
 
 
128
 
  int type,k;
129
 
  OBAtom *atom,*nbr;
130
 
  string from,to;
131
 
  vector<OBNodeBase*>::iterator i;
132
 
  vector<OBEdgeBase*>::iterator j;
133
 
  ttab.SetFromType("INT");
134
 
  ttab.SetToType("MMD");
135
 
 
136
 
  for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
137
 
    {
138
 
      if (atom->IsHydrogen())
139
 
        {
140
 
          type = 41;
141
 
          if ((nbr = atom->BeginNbrAtom(j)))
142
 
            if (nbr->IsOxygen()) type = 42;
143
 
            else if (nbr->IsNitrogen()) type = 43;
144
 
        }
145
 
      else
146
 
        {
147
 
          from = atom->GetType();
148
 
          ttab.Translate(to,from);
149
 
          type = atoi((char*)to.c_str());
150
 
        }
151
 
      sprintf(buffer,"%4d",type);
152
 
      ofs << buffer;
153
 
      for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
154
 
        {
155
 
          sprintf(buffer," %5d %1d",nbr->GetIdx(),(*j)->GetBO());
156
 
          ofs << buffer;
157
 
        }      
158
 
      for (k=atom->GetValence();k < 6;k++)
159
 
        {
160
 
          sprintf(buffer," %5d %1d",0,0);
161
 
          ofs << buffer;
162
 
        }      
163
 
 
164
 
      sprintf(buffer," %11.6f %11.6f %11.6f %5d %5d %8.5f \n",
165
 
              atom->x(), atom->y(),atom->z(),0,0,
166
 
              atom->GetPartialCharge());
167
 
      ofs << buffer;
168
 
    }
169
 
 
170
 
  return(true);
171
 
}
172
 
 
173
 
}