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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// Copyright (c) 2016, Novartis Institutes for BioMedical Research Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Novartis Institutes for BioMedical Research Inc.
// nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include <GraphMol/RDKitBase.h>
#include <GraphMol/QueryOps.h>
#include <GraphMol/RDKitQueries.h>
#include <GraphMol/MonomerInfo.h>
#include <RDGeneral/types.h>
#include "RDFreeSASA.h"
#include "boost/format.hpp"
extern "C" {
#include "freesasa.h"
}
namespace RDKit {
namespace common_properties {
namespace Atom {
const std::string SASA = "SASA";
; // Solvent Accessible Surface Area for atom- double
const std::string SASAClass = "SASAClass"; // Class type, 0,1,2... etc
const std::string SASAClassName =
"SASAClassName"; // Class name, Polar, APolar etc...
} // namespace Atom
namespace Molecule {
const std::string SASA =
"SASA"; // Total Solvent Accessible Surface area for molecule;
}
} // namespace common_properties
} // namespace RDKit
namespace FreeSASA {
using namespace RDKit;
bool classifyAtoms(ROMol &mol, std::vector<double> &radii,
const SASAOpts &opts) {
radii.clear();
const freesasa_classifier *classifier = nullptr;
switch (opts.classifier) {
case SASAOpts::Protor:
classifier = &freesasa_protor_classifier;
break;
case SASAOpts::NACCESS:
classifier = &freesasa_naccess_classifier;
break;
case SASAOpts::OONS:
classifier = &freesasa_oons_classifier;
break;
default:
throw ValueErrorException("unknown FreeSASA classifier specified");
return false;
}
bool success = true;
for (ROMol::AtomIterator at = mol.beginAtoms(); at != mol.endAtoms(); ++at) {
Atom *atom = *at;
freesasa_atom_class cls = FREESASA_ATOM_UNKNOWN;
std::string classification = "Unclassified";
double radius = 0.0;
const AtomMonomerInfo *info = atom->getMonomerInfo();
if (info) {
const char *atom_name = info->getName().c_str();
const char *res_name = nullptr;
if (info->getMonomerType() == AtomMonomerInfo::PDBRESIDUE) {
res_name = ((AtomPDBResidueInfo *)info)->getResidueName().c_str();
radius = freesasa_classifier_radius(classifier, res_name, atom_name);
if (radius == 0.0) {
BOOST_LOG(rdWarningLog)
<< "Atom " << atom->getIdx() << " has zero radius" << std::endl;
}
cls = freesasa_classifier_class(classifier, res_name, atom_name);
if (cls == FREESASA_ATOM_UNKNOWN) {
BOOST_LOG(rdWarningLog) << "Atom " << atom->getIdx()
<< " could not be classified" << std::endl;
success = false;
} else {
classification = freesasa_classifier_class2str(cls);
}
}
}
radii.push_back(radius);
atom->setProp<int>(common_properties::Atom::SASAClass, (int)cls);
atom->setProp(common_properties::Atom::SASAClassName, classification);
}
return success;
}
namespace {
double internalCalcSASA(const ROMol &mol, const std::vector<double> &radii,
int confIdx, const SASAOpts &opts) {
PRECONDITION(mol.getNumConformers(), "No conformers in molecule");
PRECONDITION(mol.getNumAtoms(), "Empty molecule");
freesasa_parameters params = freesasa_default_parameters;
params.n_threads = 1;
switch (opts.algorithm) {
case SASAOpts::LeeRichards:
params.alg = FREESASA_LEE_RICHARDS;
break;
case SASAOpts::ShrakeRupley:
params.alg = FREESASA_SHRAKE_RUPLEY;
break;
default:
throw ValueErrorException("Unknown freesasa algorithm");
}
// sneaky, but legal :)
std::vector<double> coords(mol.getNumAtoms() * 3);
const RDGeom::POINT3D_VECT &vec = mol.getConformer(confIdx).getPositions();
for (size_t i = 0; i < mol.getNumAtoms(); ++i) {
coords[i * 3] = vec[i].x;
coords[i * 3 + 1] = vec[i].y;
coords[i * 3 + 2] = vec[i].z;
}
freesasa_result *res =
freesasa_calc_coord(&coords[0], &radii[0], mol.getNumAtoms(), ¶ms);
if (!res) return 0.0;
CHECK_INVARIANT(res->n_atoms == rdcast<int>(mol.getNumAtoms()),
"freesasa didn't return the correct number of atoms");
double sasa = res->total;
mol.setProp(common_properties::Molecule::SASA, sasa);
size_t i = 0;
for (ROMol::ConstAtomIterator at = mol.beginAtoms(); at != mol.endAtoms();
++at, ++i) {
(*at)->setProp(common_properties::Atom::SASA, res->sasa[i]);
}
freesasa_result_free(res);
return sasa;
}
} // namespace
double calcSASA(const RDKit::ROMol &mol, const std::vector<double> &radii,
int confIdx, const RDKit::QueryAtom *query,
const SASAOpts &opts) {
double result = internalCalcSASA(mol, radii, confIdx, opts);
if (query) {
result = 0.0f;
for (ROMol::ConstQueryAtomIterator at = mol.beginQueryAtoms(query);
at != mol.endQueryAtoms(); ++at) {
const Atom *atom = *at;
result += atom->getProp<double>("SASA");
}
}
return result;
}
const RDKit::QueryAtom *makeFreeSasaAPolarAtomQuery() {
auto *qa = new QueryAtom;
qa->setQuery(makePropQuery<Atom, std::string>("SASAClassName", "Apolar"));
return qa;
}
const RDKit::QueryAtom *makeFreeSasaPolarAtomQuery() {
auto *qa = new QueryAtom;
qa->setQuery(makePropQuery<Atom, std::string>("SASAClassName", "Polar"));
return qa;
}
} // namespace FreeSASA
|