~vcs-imports/openbabel/trunk

« back to all changes in this revision

Viewing changes to molvector.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
#include "molvector.h"
 
18
 
 
19
namespace OpenEye {
 
20
 
 
21
//Functions for dealing with groups of molecules.  MolVec will read either all
 
22
//molecules from a file or a set of conformers.  
 
23
 
 
24
OEMolVector::~OEMolVector()
 
25
{
 
26
  for (unsigned int i = 0; i < _molvec.size(); i++)
 
27
    {
 
28
      delete _molvec[i];
 
29
    }
 
30
}
 
31
 
 
32
// Read all molecules from a file into a OEMolVector.  Input and output types
 
33
// default to SDF
 
34
 
 
35
void OEMolVector::Read(ifstream &ifs, const io_type in_type, const io_type out_type, int nToRead)
 
36
{       
 
37
  int nRead= 0;
 
38
  OEFileFormat ff;
 
39
  while (1)
 
40
    {
 
41
      if (nRead == nToRead) break;
 
42
      OEMol *mol;
 
43
      mol = new OEMol;
 
44
      (*mol).SetInputType(in_type);
 
45
      (*mol).SetOutputType(out_type);
 
46
      ff.ReadMolecule(ifs,*mol);
 
47
      nRead++;
 
48
      if (!(*mol).NumAtoms())
 
49
        {
 
50
          delete mol;
 
51
          break;
 
52
        }
 
53
      _molvec.push_back(mol);
 
54
    }
 
55
}
 
56
 
 
57
// Write a OEMolVector to a file.  Output type defaults to SDF
 
58
 
 
59
void OEMolVector::Write(ofstream &ofs)
 
60
{
 
61
  vector<OEMol *>::iterator mol_i;
 
62
  OEFileFormat ff;
 
63
 
 
64
  for (mol_i = _molvec.begin(); mol_i != _molvec.end(); mol_i++)
 
65
    {
 
66
      ff.WriteMolecule(ofs,(**mol_i));
 
67
    }
 
68
}
 
69
 
 
70
// Get a specific molecule from a OEMolVector.  Index starts at zero.
 
71
OEMol *OEMolVector::GetMol(int i)
 
72
{
 
73
  if (i >= 0 && i < (signed)_molvec.size())
 
74
    return(_molvec[i]);
 
75
  else
 
76
    {
 
77
      cerr << "Index " << i << " out of range in OEMolVector::GetMol " << endl;
 
78
      return(NULL);
 
79
    }
 
80
}
 
81
 
 
82
// Read a set of conformers from an input file and put them into a MolVec.
 
83
// This function read the first molecule and sets the current title (held
 
84
// int the variable master) to be the current title.  It continues to read
 
85
// molecules and push them into the vector util it reads a molecule with a
 
86
// different name.  At this point it rewinds the file stream to the beginning
 
87
// of the current molecule and returns
 
88
bool OEMolVector::ReadConfs(ifstream &ifs, const io_type in_type, const io_type out_type)
 
89
{
 
90
  OEMol *mol;
 
91
  OEFileFormat ff;
 
92
  string title,master;
 
93
 
 
94
  _molvec.resize(0);
 
95
  
 
96
  int i = 1;
 
97
  while (1)
 
98
    {
 
99
      mol = new OEMol;
 
100
      (*mol).SetInputType(in_type);
 
101
      (*mol).SetOutputType(out_type);
 
102
      streampos sp = ifs.tellg();
 
103
      ff.ReadMolecule(ifs,*mol);
 
104
      if (mol->NumAtoms() == 0)
 
105
        {
 
106
          delete mol;
 
107
          return(false);
 
108
        }
 
109
      
 
110
      title = mol->GetTitle();
 
111
      if (i == 1)
 
112
        {
 
113
          master = title;
 
114
          _molvec.push_back(mol);
 
115
        }
 
116
      else
 
117
        {
 
118
          if (title == master)
 
119
            _molvec.push_back(mol);
 
120
          else
 
121
            {
 
122
              ifs.seekg(sp);
 
123
              delete mol;
 
124
              break;
 
125
            }
 
126
        }
 
127
      i++;
 
128
    }
 
129
  return(true);
 
130
}
 
131
 
 
132
 
 
133
} // namespace OpenEye
 
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
 
 
140
 
 
141