File: molvector.cpp

package info (click to toggle)
ghemical 1.01-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,984 kB
  • ctags: 19,443
  • sloc: ansic: 69,073; cpp: 60,583; fortran: 35,324; sh: 5,419; makefile: 506; perl: 91
file content (145 lines) | stat: -rw-r--r-- 3,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**********************************************************************
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

#include "mol.h"
#include "obutil.h"

#include "molvector.h"

using namespace std;

namespace OpenBabel {

//Functions for dealing with groups of molecules.  MolVec will read either all
//molecules from a file or a set of conformers.  

OBMolVector::~OBMolVector()
{
  for (unsigned int i = 0; i < _molvec.size(); i++)
    {
      delete _molvec[i];
      _molvec[i] = NULL;
    }
}

// Read all molecules from a file into a OBMolVector.  Input and output types
// default to SDF

void OBMolVector::Read(ifstream &ifs, const io_type in_type, const io_type out_type, int nToRead)
{	
  int nRead= 0;
  OBFileFormat ff;
  while (1)
    {
      if (nRead == nToRead) break;
      OBMol *mol;
      mol = new OBMol;
      (*mol).SetInputType(in_type);
      (*mol).SetOutputType(out_type);
      ff.ReadMolecule(ifs,*mol);
      nRead++;
      if (!(*mol).NumAtoms())
	{
	  delete mol;
	  mol = NULL;
	  break;
	}
      _molvec.push_back(mol);
    }
}

// Write a OBMolVector to a file.  Output type defaults to SDF

void OBMolVector::Write(ofstream &ofs)
{
  vector<OBMol *>::iterator mol_i;
  OBFileFormat ff;

  for (mol_i = _molvec.begin(); mol_i != _molvec.end(); mol_i++)
    {
      ff.WriteMolecule(ofs,(**mol_i));
    }
}

// Get a specific molecule from a OBMolVector.  Index starts at zero.
OBMol *OBMolVector::GetMol(int i)
{
  if (i >= 0 && i < (signed)_molvec.size())
    return(_molvec[i]);
  else
    {
      cerr << "Index " << i << " out of range in OBMolVector::GetMol " << endl;
      return(NULL);
    }
}

// Read a set of conformers from an input file and put them into a MolVec.
// This function read the first molecule and sets the current title (held
// int the variable master) to be the current title.  It continues to read
// molecules and push them into the vector util it reads a molecule with a
// different name.  At this point it rewinds the file stream to the beginning
// of the current molecule and returns
bool OBMolVector::ReadConfs(ifstream &ifs, const io_type in_type, const io_type out_type)
{
  OBMol *mol;
  OBFileFormat ff;
  string title,master;

  _molvec.resize(0);
  
  int i = 1;
  while (1)
    {
      mol = new OBMol;
      (*mol).SetInputType(in_type);
      (*mol).SetOutputType(out_type);
      streampos sp = ifs.tellg();
      ff.ReadMolecule(ifs,*mol);
      if (mol->NumAtoms() == 0)
        {
          delete mol; mol = NULL;
	  return(false);
        }
      
      title = mol->GetTitle();
      if (i == 1)
        {
          master = title;
          _molvec.push_back(mol);
        }
      else
        {
          if (title == master)
	    _molvec.push_back(mol);
          else
            {
              ifs.seekg(sp);
              delete mol; mol = NULL;
              break;
            }
        }
      i++;
    }
  return(true);
}


} // namespace OpenBabel