File: MolChemicalFeatureFactory.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 (121 lines) | stat: -rw-r--r-- 4,147 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
// $Id: MolChemicalFeatureFactory.cpp 1528 2010-09-26 17:04:37Z glandrum $
//
//  Copyright (C) 2004-2006 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 "MolChemicalFeature.h"
#include "MolChemicalFeatureDef.h"
#include "MolChemicalFeatureFactory.h"
#include "FeatureParser.h"
#include <RDGeneral/Invariant.h>
#include <GraphMol/ROMol.h>

#include <GraphMol/Substruct/SubstructMatch.h>
#include <vector>
#include <sstream>
#include <set>
#include <algorithm>


namespace RDKit {

  FeatSPtrList MolChemicalFeatureFactory::getFeaturesForMol(const ROMol &mol,
                                                            const char* includeOnly) const {
    PRECONDITION(includeOnly,"bad limits");
    std::string limits(includeOnly);
    
#ifdef USE_VFLIB
    AR_MOLGRAPH *molG=getMolGraph(mol);
#endif
    FeatSPtrList res;
    typedef std::vector< std::pair< std::string,std::set<int> > > MatchSetCollection;
    MatchSetCollection matchSets;
    for(MolChemicalFeatureDef::CollectionType::const_iterator featDefIt=beginFeatureDefs();
        featDefIt!=endFeatureDefs();featDefIt++){
      MolChemicalFeatureDef::CollectionType::value_type featDef=*featDefIt;
      if(limits=="" || limits==featDef->getFamily()){
        std::vector< MatchVectType > matches;
#ifdef USE_VFLIB
        unsigned int numMatches=SubstructMatch(molG,*featDef->getPattern(),matches);
#else
        unsigned int numMatches=SubstructMatch(mol,*featDef->getPattern(),matches);
#endif
        for(unsigned int i=0;i<numMatches;i++){
          const MatchVectType &match=matches[i];
          std::set<int> matchSet;
          for(MatchVectType::const_iterator mIt=match.begin();
              mIt!=match.end();
              ++mIt){
            matchSet.insert(mIt->second);
          }
          
          // loop over the matches we've already found and see if this one
          // is unique:
          bool unique=true;
          for(MatchSetCollection::const_iterator vsiCI=matchSets.begin();
              vsiCI!=matchSets.end();
              ++vsiCI){
            if(vsiCI->first==featDef->getFamily() &&
               std::includes(vsiCI->second.begin(),vsiCI->second.end(),
                             matchSet.begin(),matchSet.end())){
              unique=false;
              break;
            }
          }
          if(unique){
            matchSets.push_back(std::make_pair(featDef->getFamily(),matchSet));
    
            // Set up the feature:
            MolChemicalFeature *newFeat=new MolChemicalFeature(&mol,this,featDef.get());
            MolChemicalFeature::AtomPtrContainer &atoms=newFeat->d_atoms;
            atoms.resize(match.size());
    
            // set up the atoms:
            for(MatchVectType::const_iterator matchIt=match.begin();
                        matchIt!=match.end();matchIt++){
              int atomIdx=matchIt->second;
              int queryIdx=matchIt->first;
              atoms[queryIdx]=mol.getAtomWithIdx(atomIdx);
            }
    
            // finally, add this to our result:
            res.push_back(FeatSPtrList::value_type(newFeat));
          }
        }
      }
    }
#ifdef USE_VFLIB
#ifndef CACHE_ARMOLGRAPHS
    delete molG;
#endif
#endif
    return res;
  }
  
  MolChemicalFeatureFactory *buildFeatureFactory(const std::string &featureData){
    std::stringstream ss(featureData);
    return buildFeatureFactory(ss);
  }

  MolChemicalFeatureFactory *buildFeatureFactory(std::istream &inStream){
    MolChemicalFeatureFactory *res=0;
    MolChemicalFeatureDef::CollectionType featDefs;

    if(parseFeatureData(inStream,featDefs)==0){
      // everything parsed ok
      res = new MolChemicalFeatureFactory();
      //std::copy(featDefs.begin(),featDefs.end(),res->beginFeatureDefs());
      for(MolChemicalFeatureDef::CollectionType::const_iterator ci=featDefs.begin();
          ci!=featDefs.end();ci++){
        res->addFeatureDef(*ci);
      }
    }

    return res;
  }
}