~vcs-imports/openbabel/trunk

« back to all changes in this revision

Viewing changes to c3d.cpp

  • Committer: ghutchis
  • Date: 2001-11-27 18:50:36 UTC
  • Revision ID: svn-v4:86f38270-e075-4da6-bf68-7dcd545c500b:openbabel/trunk:46
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
 
3
 
 
4
This program is free software; you can redistribute it and/or modify
 
5
it under the terms of the GNU General Public License as published by
 
6
the Free Software Foundation version 2 of the License.
 
7
 
 
8
This program is distributed in the hope that it will be useful,
 
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
GNU General Public License for more details.
 
12
***********************************************************************/
 
13
 
 
14
#include "mol.h"
 
15
#include "oeutil.h"
 
16
 
 
17
namespace OpenEye
 
18
{
 
19
static bool WriteChem3d(ostream &ofs,OEMol &mol, char *mol_typ);
 
20
 
 
21
bool WriteChem3d2(ostream &ofs,OEMol &mol)
 
22
{
 
23
  return(WriteChem3d(ofs,mol,"C3D"));
 
24
}
 
25
 
 
26
bool WriteChem3d1(ostream &ofs,OEMol &mol)
 
27
{
 
28
  return(WriteChem3d(ofs,mol,"MM2"));
 
29
}
 
30
 
 
31
bool WriteMmads(ostream &ofs,OEMol &mol)
 
32
{
 
33
  return(WriteChem3d(ofs,mol,"MMADS"));
 
34
}
 
35
 
 
36
static bool WriteChem3d(ostream &ofs,OEMol &mol, char *mol_typ)
 
37
 
38
  int atnum;
 
39
  int type_num;
 
40
  char buffer[BUFF_SIZE],type_name[10],ele_type[10];
 
41
 
 
42
  sprintf(buffer,"%d",mol.NumAtoms()); ofs << buffer;
 
43
  if (EQ(mol_typ,"MMADS"))
 
44
  {
 
45
    sprintf(buffer," %s",mol.GetTitle()); ofs << buffer;
 
46
    ttab.SetToType("MM2");
 
47
  }
 
48
  else ttab.SetToType(mol_typ);
 
49
  ofs << endl;
 
50
 
 
51
  ttab.SetFromType("INT"); 
 
52
 
 
53
  OEAtom *atom,*nbr;
 
54
  vector<OEAtom*>::iterator i;
 
55
  vector<OEBond*>::iterator j;
 
56
 
 
57
  for (atom = mol.BeginAtom(i);atom;atom = mol.NextAtom(i))
 
58
  {
 
59
    if (!ttab.Translate(type_name,atom->GetType()))
 
60
    {
 
61
      sprintf(buffer,"Unable to assign %s type to atom %d type = %s\n",
 
62
              mol_typ,atom->GetIdx(),atom->GetType());
 
63
      ThrowError(buffer);
 
64
      atnum = atom->GetAtomicNum();
 
65
      type_num = atnum * 10 + atom->GetValence();
 
66
      sprintf(type_name,"%d",type_num);
 
67
    }
 
68
    strcpy(ele_type,etab.GetSymbol(atom->GetAtomicNum()));
 
69
    sprintf(buffer,"%-3s %-5d %8.4f  %8.4f  %8.4f %5s",
 
70
            ele_type,
 
71
            atom->GetIdx(),
 
72
            atom->x(),
 
73
            atom->y(),
 
74
            atom->z(),
 
75
            type_name); ofs << buffer;
 
76
 
 
77
    for (nbr = atom->BeginNbrAtom(j);nbr;nbr = atom->NextNbrAtom(j))
 
78
      {
 
79
        sprintf(buffer,"%6d",nbr->GetIdx()); 
 
80
        ofs << buffer;
 
81
      }
 
82
    ofs << endl;
 
83
  }
 
84
  return(true);
 
85
}
 
86
 
 
87
static bool ReadChem3d(istream&,OEMol&,bool,char*);
 
88
 
 
89
bool ReadMmads(istream &ifs,OEMol &mol,char *title)
 
90
{
 
91
  return(ReadChem3d(ifs,mol,true,"MM2"));
 
92
}
 
93
 
 
94
bool ReadChem3d1(istream &ifs,OEMol &mol,char *title)
 
95
{
 
96
  return(ReadChem3d(ifs,mol,false,"MM2"));
 
97
}
 
98
 
 
99
int read_chem3d2(istream &ifs,OEMol &mol,char *title)
 
100
{
 
101
  return(ReadChem3d(ifs,mol,false,"C3D"));
 
102
}
 
103
 
 
104
bool ReadChem3d(istream &ifs,OEMol &mol,bool mmads,char *type_key)
 
105
{
 
106
  char buffer[BUFF_SIZE];
 
107
  int natoms,i;
 
108
  char tmp[10],tmp1[10];
 
109
  char atomic_type[10];
 
110
  float exponent = 0.0f;
 
111
  float divisor = 1.0f;
 
112
  float Alpha,Beta,Gamma,A,B,C;
 
113
  bool has_fractional = false, has_divisor = false;
 
114
  Matrix3x3 m;
 
115
  
 
116
  vector<string> vs;
 
117
  ifs.getline(buffer,BUFF_SIZE);
 
118
  tokenize(vs,buffer);
 
119
 
 
120
  if (mmads)
 
121
  {
 
122
    if (vs.empty()) return(false);
 
123
    natoms = atoi((char*)vs[0].c_str());
 
124
    if (vs.size() == 2) mol.SetTitle(vs[1]);
 
125
  }
 
126
  else
 
127
    {
 
128
    switch(vs.size())
 
129
      {
 
130
      case 7 :
 
131
        sscanf(buffer,"%d%f%f%f%f%f%f",
 
132
               &natoms,&Alpha,&Beta,&Gamma,&A,&B,&C);
 
133
        m.FillOrth(Alpha,Beta,Gamma,A,B,C);
 
134
        has_fractional = true;
 
135
        break;
 
136
      case 8 :
 
137
        sscanf(buffer,"%d%f%f%f%f%f%f%f",
 
138
               &natoms,&Alpha,&Beta,&Gamma,&A,&B,&C,&exponent);
 
139
        m.FillOrth(Alpha,Beta,Gamma,A,B,C);
 
140
        has_fractional = true;
 
141
        has_divisor = true;
 
142
        break;   
 
143
      default :
 
144
        sscanf(buffer,"%d",&natoms);
 
145
        break;
 
146
      }
 
147
    }
 
148
 
 
149
  if (!natoms) return(false);
 
150
  divisor = pow(10.0,exponent);
 
151
  mol.ReserveAtoms(natoms);
 
152
 
 
153
  ttab.SetToType("INT");
 
154
  ttab.SetFromType(type_key);
 
155
 
 
156
  OEAtom *atom;
 
157
  float x,y,z;
 
158
  Vector v;
 
159
 
 
160
  unsigned int k;
 
161
  for (i = 1; i <= natoms; i++)
 
162
  {
 
163
    ifs.getline(buffer,BUFF_SIZE);
 
164
    sscanf(buffer,"%s%*d%f%f%f%s",
 
165
           atomic_type,
 
166
           &x,
 
167
           &y,
 
168
           &z,
 
169
           tmp);
 
170
    v.Set(x,y,z);
 
171
    if (has_fractional) v *= m;
 
172
    if (has_divisor) v/= divisor;
 
173
      
 
174
    tokenize(vs,buffer);
 
175
    if (vs.empty()) return(false);
 
176
 
 
177
    atom = mol.NewAtom();
 
178
    ttab.Translate(tmp1,tmp);
 
179
    atom->SetType(tmp1);
 
180
    atom->SetVector(v);
 
181
    atom->SetAtomicNum(etab.GetAtomicNum(atomic_type));
 
182
 
 
183
    for (k = 6;k < vs.size(); k++)
 
184
      mol.AddBond(atom->GetIdx(),atoi((char*)vs[k].c_str()),1);
 
185
  }
 
186
  
 
187
  //assign_bond_order(mol);
 
188
  
 
189
  return(true);
 
190
}
 
191
 
 
192
}