~ubuntu-branches/ubuntu/vivid/openbabel/vivid-proposed

« back to all changes in this revision

Viewing changes to src/patty.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2002-02-01 01:19:44 UTC
  • Revision ID: james.westby@ubuntu.com-20020201011944-4a9guzcsnpezzawx
Tags: upstream-1.99
ImportĀ upstreamĀ versionĀ 1.99

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 "obutil.h"
 
16
#include "parsmart.h"
 
17
#include "patty.h"
 
18
 
 
19
// Simple programmable atom typer
 
20
// WPW - 070199
 
21
// Usage is in sample main below
 
22
 
 
23
namespace OpenBabel {
 
24
 
 
25
void patty::read_rules(const string &infile)
 
26
{
 
27
  ifstream ifs, ifs1, *ifsP;
 
28
  vector<string> vs;
 
29
  char buffer[BUFF_SIZE];
 
30
  char tmp_str[BUFF_SIZE];
 
31
  char patty_dir[BUFF_SIZE];
 
32
 
 
33
  _sp.resize(1000);
 
34
  
 
35
  ifs.open(infile.c_str());
 
36
  ifsP= &ifs;
 
37
  if (!ifs)
 
38
    {
 
39
      if (getenv("BABEL_DATADIR") == NULL)
 
40
      {
 
41
        cerr << "The BABEL_DATADIR environment variable is not defined" << endl;
 
42
        cerr << "Please define it so the program can find " << infile << endl;
 
43
        exit(0);
 
44
      }
 
45
      else
 
46
        strcpy(patty_dir,getenv("BABEL_DATADIR"));
 
47
      strcat(patty_dir,FILE_SEP_CHAR);  
 
48
      strcat(patty_dir,infile.c_str());  
 
49
      ifs1.open(patty_dir);  
 
50
      ifsP= &ifs1;
 
51
 //     if (!ifs1)
 
52
  //    {
 
53
   //     cerr << "Could not open " << patty_dir << endl;
 
54
    //    exit(0);
 
55
     // }
 
56
    }
 
57
 
 
58
  int i = 0;
 
59
  if (!ifsP){
 
60
        cerr << "Could not open " << patty_dir << endl;
 
61
        exit(0);
 
62
  }
 
63
  while (ifsP->getline(buffer,BUFF_SIZE))
 
64
    {
 
65
      if (buffer[0] != '#')
 
66
    {
 
67
      tokenize(vs,buffer," \t\n");
 
68
      if (vs.size() >= 2)
 
69
        {
 
70
          strcpy(tmp_str,vs[0].c_str());
 
71
          _sp[i]->Init(tmp_str);
 
72
          smarts.push_back(vs[0]);
 
73
          typ.push_back(vs[1]);
 
74
          i++;
 
75
        }
 
76
    }
 
77
    }
 
78
  _sp.resize(i);
 
79
}
 
80
 
 
81
void patty::assign_rules(vector<string> &rules)
 
82
{
 
83
        vector<string> vs;
 
84
        char buffer[BUFF_SIZE];
 
85
        char tmp_str[BUFF_SIZE];
 
86
        unsigned int i;
 
87
 
 
88
        _sp.resize(1000);
 
89
        for ( i = 0 ; i < rules.size() ; i++ )
 
90
        {
 
91
                strncpy(buffer, rules[i].c_str(), BUFF_SIZE);
 
92
                if (buffer[0] != '#')
 
93
                {
 
94
                        tokenize(vs,buffer," \t\n");
 
95
                        if (vs.size() >= 2)
 
96
                        {
 
97
                                strcpy(tmp_str,vs[0].c_str());
 
98
                                _sp[i]->Init(tmp_str);
 
99
                                smarts.push_back(vs[0]);
 
100
                                typ.push_back(vs[1]);
 
101
                        }
 
102
                        else
 
103
                                i--;
 
104
                }
 
105
                else
 
106
                        i--;
 
107
        }
 
108
        _sp.resize(i);  
 
109
}
 
110
 
 
111
void patty::assign_types(OBMol &mol,vector<string> &atm_typ)
 
112
{
 
113
  atm_typ.resize(mol.NumAtoms()+1);
 
114
 
 
115
  for (unsigned int i = 0; i < _sp.size(); i++)
 
116
    {
 
117
      _sp[i]->Match(mol);
 
118
      vector<vector<int> > match = _sp[i]->GetMapList();
 
119
      //vector<vector<int> >& match = _sp[i]->GetMapList();
 
120
      if (match.size())
 
121
        {
 
122
          if (debug)
 
123
            cout << typ[i] << " " << smarts[i] << " matched " ;
 
124
      
 
125
          for (unsigned int j = 0; j < match.size(); j++)
 
126
            {
 
127
              if (debug)
 
128
                cout << match[j][0] << " ";
 
129
              atm_typ[match[j][0]] = typ[i];
 
130
            }
 
131
          if (debug)
 
132
            cout << endl;
 
133
        }
 
134
    }
 
135
}
 
136
 
 
137
void patty::assign_types(OBMol &mol,vector<int> &atm_typ)
 
138
{
 
139
  atm_typ.resize(mol.NumAtoms()+1);
 
140
 
 
141
  for (unsigned int i = 0; i < _sp.size(); i++)
 
142
    {
 
143
      _sp[i]->Match(mol);
 
144
      vector<vector<int> > match = _sp[i]->GetMapList();
 
145
      //vector<vector<int> >& match = _sp[i]->GetMapList();
 
146
      if (match.size())
 
147
        {
 
148
          if (debug)
 
149
            cout << typ[i] << " " << smarts[i] << " matched " ;
 
150
      
 
151
          for (unsigned int j = 0; j < match.size(); j++)
 
152
            {
 
153
              if (debug)
 
154
                cout << match[j][0] << " ";
 
155
              atm_typ[match[j][0]] = type_to_int(typ[i]);
 
156
            }
 
157
          if (debug)
 
158
            cout << endl;
 
159
        }
 
160
    }
 
161
}
 
162
 
 
163
 
 
164
int patty::type_to_int(const string &type, bool failOnUndefined)
 
165
{
 
166
 
 
167
  int result;
 
168
 
 
169
 
 
170
    switch(toupper(type.c_str()[0]))
 
171
      {
 
172
      case 'C' : // CAT - CATION
 
173
         result = PT_CATION;
 
174
         break;
 
175
      case 'A' :
 
176
         if (toupper(type.c_str()[1]) == 'N') // ANI - ANION
 
177
            result = PT_ANION;
 
178
         else
 
179
            result = PT_ACCEPTOR;
 
180
         break;
 
181
      case 'P' : // POL - POLAR
 
182
         result = PT_POLAR;
 
183
         break;
 
184
      case 'D' : // DON - DONOR
 
185
         result = PT_DONOR;
 
186
         break;
 
187
      case 'H' : // HYD - HYDROPHOBIC
 
188
         result = PT_HYDROPHOBIC;
 
189
         break;
 
190
                        case 'M' : // Metal
 
191
                                 result = PT_METAL;
 
192
                                 break;
 
193
      case 'O' : // OTH - OTHER
 
194
         result = PT_OTHER;
 
195
         break;
 
196
      default :
 
197
          // This was added by Brian,
 
198
          // Behavior will fail if type is undefined
 
199
          if (failOnUndefined){
 
200
              cerr << "Unable to find type of feature passed in " << endl;
 
201
              cerr << "Feature passed in is " << type << endl;
 
202
              exit(-1);
 
203
          }else{
 
204
              result = 7;
 
205
          }
 
206
      }
 
207
      return(result);
 
208
}
 
209
 
 
210
}
 
211
 
 
212
#ifdef I_EVER_BUY_A_MICHAEL_BOLTON_ALBUM
 
213
 
 
214
int main(int argc, char *argv[])
 
215
{
 
216
  OBMol mol(SDF,SDF);
 
217
  vector<string> types;
 
218
 
 
219
  ifstream ifs(argv[1]);
 
220
  if (!ifs)
 
221
    {
 
222
      cerr << "Could not open argv[1] " << endl;
 
223
      exit(0);
 
224
    }
 
225
 
 
226
  patty p("simple.txt");
 
227
  for (;;)
 
228
    {
 
229
      ifs >> mol;
 
230
      if (!mol.NumAtoms()) break;
 
231
      p.assign_types(mol,types);
 
232
      mol.Clear();
 
233
    }
 
234
 
 
235
  for (int i = 1; i < types.size(); i++)
 
236
    {
 
237
      cout << i << " " << types[i] << endl;
 
238
    }
 
239
}
 
240
 
 
241
#endif
 
242
 
 
243
 
 
244
 
 
245
 
 
246
 
 
247
 
 
248
 
 
249
 
 
250
 
 
251
 
 
252
 
 
253