File: MolDescriptors.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (130 lines) | stat: -rw-r--r-- 3,839 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
// $Id: MolDescriptors.cpp 1719 2011-04-28 04:56:48Z glandrum $
//
//  Copyright (C) 2005-2008 Greg Landrum and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <RDGeneral/Invariant.h>
#include <GraphMol/RDKitBase.h>
#include "MolDescriptors.h"
#include <map>
#include <list>
#include <algorithm>
#include <sstream>

namespace RDKit{
  namespace Descriptors {

    static std::string _amwVersion="1.0.0";
    double calcAMW(const ROMol &mol,bool onlyHeavy){
      double res=0.0;
      for(ROMol::ConstAtomIterator atomIt=mol.beginAtoms();
	  atomIt != mol.endAtoms();++atomIt){
	int atNum=(*atomIt)->getAtomicNum();
	if(atNum!=1 || !onlyHeavy){
	  res += (*atomIt)->getMass();
	}

	// add our implicit Hs if we need to:
	if(!onlyHeavy){
          const PeriodicTable *table=PeriodicTable::getTable();
	  res += (*atomIt)->getTotalNumHs()*table->getAtomicWeight(1);
	}
      }
      return res;
    }

    static std::string _exactmwVersion="1.0.0";
    double calcExactMW(const ROMol &mol,bool onlyHeavy){
      double res=0.0;
      int nHsToCount=0;
      const PeriodicTable *table=PeriodicTable::getTable();
      for(ROMol::ConstAtomIterator atomIt=mol.beginAtoms();
	  atomIt != mol.endAtoms();++atomIt){
	int atNum=(*atomIt)->getAtomicNum();
	if(atNum!=1 || !onlyHeavy){
          if(atNum==1) --nHsToCount;
          if(fabs((*atomIt)->getMass()-table->getAtomicWeight(atNum))<.001){
            res += table->getMostCommonIsotopeMass(atNum);
          } else {
            res += (*atomIt)->getMass();
          }
	}

	// add our implicit Hs if we need to:
	if(!onlyHeavy){
          nHsToCount += (*atomIt)->getTotalNumHs(true);
	}

      }
      if(!onlyHeavy){
        res += nHsToCount*table->getMostCommonIsotopeMass(1);
      }
      return res;
    }

    static std::string _molFormulaVersion="1.1.0";
    std::string calcMolFormula(const ROMol &mol){
      std::ostringstream res;
      std::map<std::string,unsigned int> counts;
      int charge=0;
      unsigned int nHs=0;
      const PeriodicTable *table=PeriodicTable::getTable();
      for(ROMol::ConstAtomIterator atomIt=mol.beginAtoms();
	  atomIt != mol.endAtoms();++atomIt){
	int atNum=(*atomIt)->getAtomicNum();
        std::string symb=table->getElementSymbol(atNum);
        if(counts.find(symb)!=counts.end()){
          counts[symb]+=1;
        } else {
          counts[symb]=1;
        }
        nHs += (*atomIt)->getTotalNumHs();
        charge += (*atomIt)->getFormalCharge();
      }

      if(nHs){
        if(counts.find(std::string("H"))!=counts.end()){
          counts["H"]+=nHs;
        } else {
          counts["H"]=nHs;
        }
      }
      std::list<std::string> ks;
      for(std::map<std::string,unsigned int>::const_iterator countIt=counts.begin();
          countIt!=counts.end();++countIt){
        ks.push_back(countIt->first);
      }
      ks.sort();
      // put in Hill order:
      if(counts.find(std::string("C"))!=counts.end()){
        ks.remove(std::string("C"));
        if(counts.find(std::string("H"))!=counts.end()){
          ks.remove(std::string("H"));
          ks.push_front(std::string("H"));
        }
        ks.push_front(std::string("C"));
      }

      for(std::list<std::string>::const_iterator kIter=ks.begin();
          kIter!=ks.end();++kIter){
        res << *kIter;
        if(counts[*kIter]>1) res<<counts[*kIter];
      }
      if(charge>0){
        res<<"+";
        if(charge>1) res<<charge;
      } else if(charge<0){
        res<<"-";
        if(charge<-1) res<<-1*charge;
      }
      return res.str();
    }

    
  } // end of namespace Descriptors
} // end of namespace RDKit