&v) : _n(v.size()), _pVector(_n) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = v[i];
+}
+
+Vector::Vector(const Vector &v) : _n(v._n), _pVector(_n) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = v._pVector[i];
+}
+
+Vector::~Vector() { _pVector.clear(); }
+
+void Vector::clear() {
+ _pVector.clear();
+ _n = 0;
+}
+
+void Vector::reset(unsigned int n) {
+ if (_n != n) // only reset the vector itself if the new size is larger
+ _pVector.resize(n);
+ _n = n;
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = 0;
+}
+
+void Vector::resize(unsigned int n) {
+ if (_n != n)
+ _pVector.resize(n);
+ _n = n;
+}
+
+double Vector::getValueAt(const unsigned int i) { return _pVector[i]; }
+
+double Vector::getValueAt(const unsigned int i) const { return _pVector[i]; }
+
+double Vector::max() const {
+ double d = _pVector[0];
+ for (unsigned int i = 1; i < _n; ++i) {
+ if (_pVector[i] > d) {
+ d = _pVector[i];
+ }
+ }
+ return d;
+}
+
+double Vector::max(unsigned int &index) const {
+ double d = _pVector[0];
+ for (unsigned int i = 1; i < _n; ++i) {
+ if (_pVector[i] > d) {
+ d = _pVector[i];
+ index = i;
+ }
+ }
+ return d;
+}
+
+double Vector::min() const {
+ double d = _pVector[0];
+ for (unsigned int i = 1; i < _n; ++i) {
+ if (_pVector[i] < d) {
+ d = _pVector[i];
+ }
+ }
+ return d;
+}
+
+double Vector::min(unsigned int &index) const {
+ double d = _pVector[0];
+ for (unsigned int i = 1; i < _n; ++i) {
+ if (_pVector[i] > d) {
+ d = _pVector[i];
+ index = i;
+ }
+ }
+ return d;
+}
+
+double Vector::sum() const {
+ double m(0.0);
+ for (unsigned int i = 0; i < _n; ++i)
+ m += _pVector[i];
+ return m;
+}
+
+double Vector::mean() const {
+ double m(0.0);
+ for (unsigned int i = 0; i < _n; ++i)
+ m += _pVector[i];
+ return m / _n;
+}
+
+double Vector::stDev() const {
+ double m(0.0);
+ for (unsigned int i = 0; i < _n; ++i)
+ m += _pVector[i];
+ double s(0.0);
+ for (unsigned int i = 0; i < _n; ++i)
+ s += (m - _pVector[i]) * (m - _pVector[i]);
+ return sqrt(s / (_n - 1));
+}
+
+double Vector::stDev(double m) const {
+ double s(0.0);
+ for (unsigned int i = 0; i < _n; ++i)
+ s += (m - _pVector[i]) * (m - _pVector[i]);
+ return sqrt(s / (_n - 1));
+}
+
+Vector &Vector::operator=(const Vector &src) {
+ if (_n != src._n) {
+ _n = src._n;
+ _pVector.resize(_n);
+ }
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = src._pVector[i];
+ return *this;
+}
+
+Vector &Vector::operator=(const double &v) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = v;
+ return *this;
+}
+
+Vector &Vector::operator+=(const double &v) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] += v;
+ return *this;
+}
+
+Vector &Vector::operator+=(const Vector &V) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] += V._pVector[i];
+ return *this;
+}
+
+Vector &Vector::operator-=(const double &v) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] -= v;
+ return *this;
+}
+
+Vector &Vector::operator-=(const Vector &V) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] -= V._pVector[i];
+ return *this;
+}
+
+Vector &Vector::operator*=(const double &v) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] *= v;
+ return *this;
+}
+
+Vector &Vector::operator*=(const Vector &V) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] *= V._pVector[i];
+ return *this;
+}
+
+Vector &Vector::operator/=(const double &v) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] /= v;
+ return *this;
+}
+
+Vector &Vector::operator/=(const Vector &V) {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] /= V._pVector[i];
+ return *this;
+}
+
+Vector &Vector::operator-() {
+ for (unsigned int i = 0; i < _n; ++i)
+ _pVector[i] = -_pVector[i];
+ return *this;
+}
+
+Vector Vector::operator+(const Vector &V) const {
+ Vector r(_n);
+ for (unsigned int i = 0; i < _n; ++i)
+ r[i] = _pVector[i] + V._pVector[i];
+ return r;
+}
+
+Vector Vector::operator-(const Vector &V) const {
+ Vector r(_n);
+ for (unsigned int i = 0; i < _n; ++i)
+ r[i] = _pVector[i] - V._pVector[i];
+ return r;
+}
+
+Vector Vector::operator*(const Vector &V) const {
+ Vector r(_n);
+ for (unsigned int i = 0; i < _n; ++i)
+ r[i] = _pVector[i] * V._pVector[i];
+ return r;
+}
+
+Vector Vector::operator/(const Vector &V) const {
+ Vector r(_n);
+ for (unsigned int i = 0; i < _n; ++i)
+ r[i] = _pVector[i] / V._pVector[i];
+ return r;
+}
+
+bool Vector::operator==(const Vector &V) const {
+ for (unsigned int i = 0; i < _n; ++i) {
+ if (_pVector[i] != V._pVector[i])
+ return false;
+ }
+ return true;
+}
+
+bool Vector::operator!=(const Vector &V) const {
+ for (unsigned int i = 0; i < _n; ++i) {
+ if (_pVector[i] != V._pVector[i])
+ return true;
+ }
+ return false;
+}
+
+double Vector::dotProd(const Vector &v) {
+ double d(0.0);
+ for (unsigned int i = 0; i < _n; ++i) {
+ d += _pVector[i] * v[i];
+ }
+ return d;
+}
+
+void Vector::swap(const unsigned int i, const unsigned int j) {
+ double dummy = _pVector[i];
+ _pVector[i] = _pVector[j];
+ _pVector[j] = dummy;
+ return;
+}
+
+Matrix::Matrix(const unsigned int n, const unsigned int m)
+ : _nRows(n), _nCols(m), _pMatrix(nullptr) {
+ if (n && m) {
+ auto *dummy = new double[n * m]; // data
+ _pMatrix = new double *[n]; // row pointers
+ for (unsigned int i = 0; i < n; ++i) {
+ _pMatrix[i] = dummy;
+ dummy += m;
+ }
+ }
+}
+
+Matrix::Matrix(const unsigned int n, const unsigned int m, const double &v)
+ : _nRows(n), _nCols(m), _pMatrix(nullptr) {
+ if (n && m) {
+ auto *dummy = new double[n * m];
+ _pMatrix = new double *[n];
+ for (unsigned int i = 0; i < n; ++i) {
+ _pMatrix[i] = dummy;
+ dummy += m;
+ }
+ for (unsigned int i = 0; i < n; ++i)
+ for (unsigned int j = 0; j < m; ++j)
+ _pMatrix[i][j] = v;
+ }
+}
+
+Matrix::Matrix(const unsigned int n, const unsigned int m, const Vector &vec)
+ : _nRows(n), _nCols(m), _pMatrix(nullptr) {
+ auto *dummy(new double[n * m]);
+ _pMatrix = new double *[n];
+ for (unsigned int i = 0; i < n; ++i) {
+ _pMatrix[i] = dummy;
+ dummy += m;
+ }
+ for (unsigned int i = 0; i < n; ++i) {
+ for (unsigned int j = 0; j < m; ++j) {
+ _pMatrix[i][j] = vec[i * m + j];
+ }
+ }
+}
+
+Matrix::Matrix(const Matrix &src)
+ : _nRows(src._nRows), _nCols(src._nCols), _pMatrix(nullptr) {
+ if (_nRows && _nCols) {
+ auto *dummy(new double[_nRows * _nCols]);
+ _pMatrix = new double *[_nRows];
+ for (unsigned int i = 0; i < _nRows; ++i) {
+ _pMatrix[i] = dummy;
+ dummy += _nCols;
+ }
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = src[i][j];
+ }
+}
+
+Matrix::~Matrix() {
+ if (_pMatrix != nullptr) {
+ if (_pMatrix[0] != nullptr)
+ delete[](_pMatrix[0]);
+ delete[](_pMatrix);
+ }
+ _pMatrix = nullptr;
+}
+
+double Matrix::getValueAt(const unsigned int i, const unsigned int j) {
+ return _pMatrix[i][j];
+}
+
+const double Matrix::getValueAt(const unsigned int i,
+ const unsigned int j) const {
+ return _pMatrix[i][j];
+}
+
+Vector Matrix::getRow(const unsigned int i) const {
+ Vector v(_nCols);
+ for (unsigned int j = 0; j < _nCols; ++j)
+ v[j] = _pMatrix[i][j];
+ return v;
+}
+
+Vector Matrix::getColumn(const unsigned int i) const {
+ Vector v(_nRows);
+ for (unsigned int j = 0; j < _nRows; ++j)
+ v[j] = _pMatrix[j][i];
+
+ return v;
+}
+
+inline void Matrix::setValueAt(const unsigned int i, const unsigned int j,
+ double v) {
+ _pMatrix[i][j] = v;
+}
+
+void Matrix::setRow(const unsigned int i, Vector &src) {
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = src[j];
+}
+
+void Matrix::setColumn(const unsigned int i, Vector &src) {
+ for (unsigned int j = 0; j < _nRows; ++j)
+ _pMatrix[j][i] = src[j];
+}
+
+Matrix &Matrix::operator=(const Matrix &M) {
+ // check dimensions
+ if (_nRows != M.nbrRows() || _nCols != M.nbrColumns()) {
+ if (_nRows && _pMatrix != nullptr) {
+ // delete old matrix
+ if (_nCols && _pMatrix[0] != nullptr)
+ delete[] _pMatrix[0];
+ delete[] _pMatrix;
+ }
+ _pMatrix = nullptr;
+ // create a new matrix
+ _nRows = M.nbrRows();
+ _nCols = M.nbrColumns();
+ _pMatrix = new double *[_nRows];
+ _pMatrix[0] = new double[_nRows * _nCols];
+ for (unsigned int i = 1; i < _nRows; ++i)
+ _pMatrix[i] = _pMatrix[i - 1] + _nCols;
+ }
+ // fill in all new values
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = M[i][j];
+ return *this;
+}
+
+Matrix &Matrix::operator=(const double &v) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = v;
+ return *this;
+}
+
+Matrix &Matrix::operator+=(const double &v) {
+ for (int i = 0; i < _nRows; i++)
+ for (int j = 0; j < _nCols; j++)
+ _pMatrix[i][j] += v;
+ return *this;
+}
+
+Matrix &Matrix::operator+=(const Matrix &M) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] += M[i][j];
+ return *this;
+}
+
+Matrix &Matrix::operator-=(const double &v) {
+ for (int i = 0; i < _nRows; i++)
+ for (int j = 0; j < _nCols; j++)
+ _pMatrix[i][j] -= v;
+ return *this;
+}
+
+Matrix &Matrix::operator-=(const Matrix &M) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] -= M[i][j];
+ return *this;
+}
+
+Matrix &Matrix::operator*=(const double &v) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] *= v;
+ return *this;
+}
+
+Matrix &Matrix::operator*=(const Matrix &M) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] *= M[i][j];
+ return *this;
+}
+
+Matrix &Matrix::operator/=(const double &v) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] /= v;
+ return *this;
+}
+
+Matrix &Matrix::operator/=(const Matrix &M) {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] /= M[i][j];
+ return *this;
+}
+
+Matrix &Matrix::operator-() {
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = -_pMatrix[i][j];
+
+ return *this;
+}
+
+Matrix Matrix::operator+(const Matrix &M) const {
+ Matrix B(M);
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ B[i][j] = _pMatrix[i][j] + M[i][j];
+ return B;
+}
+
+Matrix Matrix::operator-(const Matrix &M) const {
+ Matrix B(M);
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ B[i][j] = _pMatrix[i][j] - M[i][j];
+ return B;
+}
+
+Matrix Matrix::operator*(const Matrix &M) const {
+ Matrix B(M);
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ B[i][j] = _pMatrix[i][j] * M[i][j];
+ return B;
+}
+
+Matrix Matrix::operator/(const Matrix &M) const {
+ Matrix B(M);
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ B[i][j] = _pMatrix[i][j] / M[i][j];
+ return B;
+}
+
+void Matrix::swapRows(unsigned int i, unsigned int j) {
+ double dummy;
+ for (unsigned int k = 0; k < _nCols; ++k) // loop over all columns
+ {
+ dummy = _pMatrix[i][k]; // store original element at [i,k]
+ _pMatrix[i][k] = _pMatrix[j][k]; // replace [i,k] with [j,k]
+ _pMatrix[j][k] = dummy; // replace [j,k] with element originally at [i,k]
+ }
+ return;
+}
+
+void Matrix::swapColumns(unsigned int i, unsigned int j) {
+ double dummy;
+ for (unsigned int k = 0; k < _nRows; ++k) // loop over all rows
+ {
+ dummy = _pMatrix[k][i]; // store original element at [k,i]
+ _pMatrix[k][i] = _pMatrix[k][j]; // replace [k,i] with [k,j]
+ _pMatrix[k][j] = dummy; // replace [k,j] with element orignally at [k,i]
+ }
+ return;
+}
+
+void Matrix::reset(const unsigned int r, const unsigned int c) {
+ // check dimensions
+ if (_nRows != r || _nCols != c) {
+ if (_nRows != 0 && _nCols != 0 && _pMatrix != nullptr) {
+ // delete old matrix
+ if (_pMatrix[0] != nullptr)
+ delete[] _pMatrix[0];
+ delete[] _pMatrix;
+ }
+ // create a new matrix
+ _nRows = r;
+ _nCols = c;
+ if (_nRows == 0 || _nCols == 0) {
+ _pMatrix = nullptr;
+ return;
+ }
+ _pMatrix = new double *[_nRows];
+ _pMatrix[0] = new double[_nRows * _nCols];
+ for (unsigned int i = 1; i < _nRows; ++i)
+ _pMatrix[i] = _pMatrix[i - 1] + _nCols;
+ }
+ // fill in all new values
+ for (unsigned int i = 0; i < _nRows; ++i)
+ for (unsigned int j = 0; j < _nCols; ++j)
+ _pMatrix[i][j] = 0;
+}
+
+void Matrix::clear() {
+ // delete old matrix
+ if (_pMatrix != nullptr) {
+ if (_pMatrix[0] != nullptr)
+ delete[] _pMatrix[0];
+ delete[] _pMatrix;
+ }
+ _pMatrix = nullptr;
+ _nRows = 0;
+ _nCols = 0;
+}
+
+Matrix Matrix::transpose() {
+ Matrix T(_nCols, _nRows);
+ for (unsigned int i(0); i < _nRows; ++i) {
+ for (unsigned int j(0); j < _nCols; ++j) {
+ T[j][i] = _pMatrix[i][j];
+ }
+ }
+ return T;
+}
+
+SiMath::Vector SiMath::rowProduct(const SiMath::Matrix &A,
+ const SiMath::Vector &U) {
+ Vector v(A.nbrRows(), 0.0);
+
+ for (unsigned int i = 0; i < A.nbrRows(); ++i) {
+ double s(0.0);
+ for (unsigned int j = 0; j < A.nbrColumns(); ++j) {
+ s += A[i][j] * U[j];
+ }
+ v[i] = s;
+ }
+ return v;
+}
+
+SiMath::Vector SiMath::colProduct(const SiMath::Vector &U,
+ const SiMath::Matrix &A) {
+ Vector v(A.nbrColumns(), 0.0);
+ for (unsigned int i = 0; i < A.nbrColumns(); ++i) {
+ double s(0.0);
+ for (unsigned int j = 0; j < A.nbrRows(); ++j) {
+ s += U[j] * A[j][i];
+ }
+ v[i] = s;
+ }
+ return v;
+}
+
+SVD::SVD(const Matrix &Aorig, bool bU, bool bV)
+ : _m(Aorig.nbrRows()), _n(Aorig.nbrColumns()), _U(), _V(), _S(0),
+ _computeV(bV), _computeU(bU) {
+ // dimensionality of the problem
+ int nu = min(_m, _n);
+ int nct = min(_m - 1, _n);
+ int nrt = max(0, std::min(_n - 2, _m));
+
+ // define the dimensions of the internal matrices and vetors
+ _S.reset(min(_m + 1, _n));
+
+ if (_computeU)
+ _U.reset(_m, nu);
+
+ if (_computeV)
+ _V.reset(_n, _n);
+
+ // local working vectors
+ Vector e(_n);
+ Vector work(_m);
+
+ // make a copy of A to do the computations on
+ Matrix Acopy(Aorig);
+
+ // loop indices
+ int i = 0, j = 0, k = 0;
+
+ // Reduce A to bidiagonal form, storing the diagonal elements
+ // in _S and the super-diagonal elements in e.
+
+ for (k = 0; k < max(nct, nrt); k++) {
+ if (k < nct) {
+ // Compute the transformation for the k-th column and place the k-th
+ // diagonal in _S[k].
+ _S[k] = 0;
+ for (i = k; i < _m; i++) {
+ _S[k] = triangle(_S[k], Acopy[i][k]);
+ }
+ if (_S[k] != 0.0) {
+ if (Acopy[k][k] < 0.0) {
+ _S[k] = -_S[k];
+ }
+ for (i = k; i < _m; i++) {
+ Acopy[i][k] /= _S[k];
+ }
+ Acopy[k][k] += 1.0;
+ }
+ _S[k] = -_S[k];
+ }
+ for (j = k + 1; j < _n; j++) {
+ if ((k < nct) && (_S[k] != 0.0)) {
+ // Apply the transformation to Acopy
+ double t = 0;
+ for (i = k; i < _m; i++) {
+ t += Acopy[i][k] * Acopy[i][j];
+ }
+ t = -t / Acopy[k][k];
+ for (i = k; i < _m; i++) {
+ Acopy[i][j] += t * Acopy[i][k];
+ }
+ }
+
+ // Place the k-th row of A into e for the subsequent calculation of the
+ // row transformation.
+ e[j] = Acopy[k][j];
+ }
+
+ // Place the transformation in _U for subsequent back multiplication.
+ if (_computeU & (k < nct)) {
+ for (i = k; i < _m; i++) {
+ _U[i][k] = Acopy[i][k];
+ }
+ }
+
+ if (k < nrt) {
+ // Compute the k-th row transformation and place the k-th super-diagonal
+ // in e[k]. Compute 2-norm without under/overflow.
+ e[k] = 0.0;
+ for (i = k + 1; i < _n; i++) {
+ e[k] = triangle(e[k], e[i]);
+ }
+ if (e[k] != 0.0) {
+ if (e[k + 1] < 0.0) { // switch sign
+ e[k] = -e[k];
+ }
+ for (i = k + 1; i < _n; i++) { // scale
+ e[i] /= e[k];
+ }
+ e[k + 1] += 1.0;
+ }
+ e[k] = -e[k];
+ if ((k + 1 < _m) & (e[k] != 0.0)) {
+ // Apply the transformation.
+
+ for (i = k + 1; i < _m; i++) {
+ work[i] = 0.0;
+ }
+ for (j = k + 1; j < _n; j++) {
+ for (i = k + 1; i < _m; i++) {
+ work[i] += e[j] * Acopy[i][j];
+ }
+ }
+ for (j = k + 1; j < _n; j++) {
+ double t = -e[j] / e[k + 1];
+ for (i = k + 1; i < _m; i++) {
+ Acopy[i][j] += t * work[i];
+ }
+ }
+ }
+
+ // Place the transformation in _V for subsequent back multiplication.
+ if (_computeV) {
+ for (i = k + 1; i < _n; i++) {
+ _V[i][k] = e[i];
+ }
+ }
+ }
+ }
+
+ // Set up the final bidiagonal matrix of order p.
+ int p = min(_n, _m + 1);
+ if (nct < _n) {
+ _S[nct] = Acopy[nct][nct];
+ }
+ if (_m < p) {
+ _S[p - 1] = 0.0;
+ }
+ if (nrt + 1 < p) {
+ e[nrt] = Acopy[nrt][p - 1];
+ }
+ e[p - 1] = 0.0;
+
+ // If required, generate U.
+ if (_computeU) {
+ for (j = nct; j < nu; j++) {
+ for (i = 0; i < _m; i++) {
+ _U[i][j] = 0.0;
+ }
+ _U[j][j] = 1.0;
+ }
+ for (k = nct - 1; k >= 0; k--) {
+ if (_S[k] != 0.0) {
+ for (j = k + 1; j < nu; j++) {
+ double t = 0;
+ for (i = k; i < _m; i++) {
+ t += _U[i][k] * _U[i][j];
+ }
+ t = -t / _U[k][k];
+ for (i = k; i < _m; i++) {
+ _U[i][j] += t * _U[i][k];
+ }
+ }
+ for (i = k; i < _m; i++) {
+ _U[i][k] = -_U[i][k];
+ }
+ _U[k][k] = 1.0 + _U[k][k];
+ for (i = 0; i < k - 1; i++) {
+ _U[i][k] = 0.0;
+ }
+ } else {
+ for (i = 0; i < _m; i++) {
+ _U[i][k] = 0.0;
+ }
+ _U[k][k] = 1.0;
+ }
+ }
+ }
+
+ // If required, generate _V.
+ if (_computeV) {
+ for (k = _n - 1; k >= 0; k--) {
+ if ((k < nrt) & (e[k] != 0.0)) {
+ for (j = k + 1; j < nu; j++) {
+ double t = 0;
+ for (i = k + 1; i < _n; i++) {
+ t += _V[i][k] * _V[i][j];
+ }
+ t = -t / _V[k + 1][k];
+ for (i = k + 1; i < _n; i++) {
+ _V[i][j] += t * _V[i][k];
+ }
+ }
+ }
+ for (i = 0; i < _n; i++) {
+ _V[i][k] = 0.0;
+ }
+ _V[k][k] = 1.0;
+ }
+ }
+
+ // Main iteration loop for the singular values.
+ int pp = p - 1;
+ int iter = 0;
+ double eps = pow(2.0, -52.0);
+ while (p > 0) {
+ k = 0;
+ unsigned int mode = 0;
+
+ // Here is where a test for too many iterations would go.
+ // This section of the program inspects for negligible elements in the s and
+ // e arrays. On completion the variables mode and k are set as follows.
+
+ // mode = 1 if s(p) and e[k-1] are negligible and k= -1; k--) {
+ if (k == -1) {
+ break;
+ }
+ if (fabs(e[k]) <= eps * (fabs(_S[k]) + fabs(_S[k + 1]))) {
+ e[k] = 0.0;
+ break;
+ }
+ }
+ if (k == p - 2) {
+ mode = 4;
+ } else {
+ int ks(p - 1); // start from ks == p-1
+ for (; ks >= k; ks--) {
+ if (ks == k) {
+ break;
+ }
+ double t = ((ks != p) ? fabs(e[ks]) : 0.0) +
+ ((ks != k + 1) ? fabs(e[ks - 1]) : 0.0);
+ if (fabs(_S[ks]) <= eps * t) {
+ _S[ks] = 0.0;
+ break;
+ }
+ }
+ if (ks == k) {
+ mode = 3;
+ } else if (ks == p - 1) {
+ mode = 1;
+ } else {
+ mode = 2;
+ k = ks;
+ }
+ }
+ k++;
+
+ // Perform the task indicated by the selected mode.
+ switch (mode) {
+
+ case 1: { // Deflate negligible _S[p]
+ double f = e[p - 2];
+ e[p - 2] = 0.0;
+ for (j = p - 2; j >= k; j--) {
+ double t = SiMath::triangle(_S[j], f);
+ double cs = _S[j] / t;
+ double sn = f / t;
+ _S[j] = t;
+ if (j != k) {
+ f = -sn * e[j - 1];
+ e[j - 1] = cs * e[j - 1];
+ }
+
+ // update V
+ if (_computeV) {
+ for (i = 0; i < _n; i++) {
+ t = cs * _V[i][j] + sn * _V[i][p - 1];
+ _V[i][p - 1] = -sn * _V[i][j] + cs * _V[i][p - 1];
+ _V[i][j] = t;
+ }
+ }
+ }
+ } break; // end case 1
+
+ case 2: { // Split at negligible _S[k]
+ double f = e[k - 1];
+ e[k - 1] = 0.0;
+ for (j = k; j < p; j++) {
+ double t = triangle(_S[j], f);
+ double cs = _S[j] / t;
+ double sn = f / t;
+ _S[j] = t;
+ f = -sn * e[j];
+ e[j] = cs * e[j];
+
+ if (_computeU) {
+ for (i = 0; i < _m; i++) {
+ t = cs * _U[i][j] + sn * _U[i][k - 1];
+ _U[i][k - 1] = -sn * _U[i][j] + cs * _U[i][k - 1];
+ _U[i][j] = t;
+ }
+ }
+ }
+ } break; // end case 2
+
+ case 3: { // Perform one qr step.
+
+ // Calculate the shift.
+ double scale =
+ max(max(max(max(fabs(_S[p - 1]), fabs(_S[p - 2])), fabs(e[p - 2])),
+ fabs(_S[k])),
+ fabs(e[k]));
+ double sp = _S[p - 1] / scale;
+ double spm1 = _S[p - 2] / scale;
+ double epm1 = e[p - 2] / scale;
+ double sk = _S[k] / scale;
+ double ek = e[k] / scale;
+ double b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0;
+ double c = (sp * epm1) * (sp * epm1);
+ double shift = 0.0;
+ if ((b != 0.0) || (c != 0.0)) {
+ shift = sqrt(b * b + c);
+ if (b < 0.0) {
+ shift = -shift;
+ }
+ shift = c / (b + shift);
+ }
+ double f = (sk + sp) * (sk - sp) + shift;
+ double g = sk * ek;
+
+ // Chase zeros.
+
+ for (j = k; j < p - 1; j++) {
+ double t = SiMath::triangle(f, g);
+ double cs = f / t;
+ double sn = g / t;
+ if (j != k) {
+ e[j - 1] = t;
+ }
+ f = cs * _S[j] + sn * e[j];
+ e[j] = cs * e[j] - sn * _S[j];
+ g = sn * _S[j + 1];
+ _S[j + 1] = cs * _S[j + 1];
+
+ if (_computeV) {
+ for (i = 0; i < _n; i++) {
+ t = cs * _V[i][j] + sn * _V[i][j + 1];
+ _V[i][j + 1] = -sn * _V[i][j] + cs * _V[i][j + 1];
+ _V[i][j] = t;
+ }
+ }
+ t = SiMath::triangle(f, g);
+ cs = f / t;
+ sn = g / t;
+ _S[j] = t;
+ f = cs * e[j] + sn * _S[j + 1];
+ _S[j + 1] = -sn * e[j] + cs * _S[j + 1];
+ g = sn * e[j + 1];
+ e[j + 1] = cs * e[j + 1];
+
+ if (_computeU && (j < _m - 1)) {
+ for (i = 0; i < _m; i++) {
+ t = cs * _U[i][j] + sn * _U[i][j + 1];
+ _U[i][j + 1] = -sn * _U[i][j] + cs * _U[i][j + 1];
+ _U[i][j] = t;
+ }
+ }
+ }
+ e[p - 2] = f;
+ iter++;
+ } break; // end case 3
+
+ // convergence step
+ case 4: {
+
+ // Make the singular values positive.
+ if (_S[k] <= 0.0) {
+ _S[k] = (_S[k] < 0.0) ? -_S[k] : 0.0;
+
+ if (_computeV) {
+ for (i = 0; i <= pp; i++) {
+ _V[i][k] = -_V[i][k];
+ }
+ }
+ }
+
+ // Order the singular values.
+ while (k < pp) {
+ if (_S[k] >= _S[k + 1])
+ break;
+
+ // swap values and columns if necessary
+ _S.swap(k, k + 1);
+
+ if (_computeV && (k < _n - 1))
+ _V.swapColumns(k, k + 1);
+
+ if (_computeU && (k < _m - 1))
+ _U.swapColumns(k, k + 1);
+
+ k++;
+ }
+ iter = 0;
+ p--;
+ } break; // end case 4
+ }
+ }
+}
+
+Matrix SVD::getSingularMatrix() {
+ unsigned int n = _S.size();
+ Matrix A(n, n, 0.0);
+ // set diagonal elements
+ for (int i = 0; i < n; i++) {
+ A[i][i] = _S[i];
+ }
+
+ return A;
+}
+
+int SVD::rank() {
+ double eps = pow(2.0, -52.0);
+ double tol = max(_m, _n) * _S[0] * eps;
+ int r = 0;
+ for (int i = 0; i < _S.size(); i++) {
+ if (_S[i] > tol) {
+ r++;
+ }
+ }
+ return r;
+}
+
+double SiMath::randD(double a, double b) {
+ double d(a);
+ d += (b - a) * ((double)rand() / RAND_MAX);
+ return d;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/parseCommandLine.cpp",".cpp","5585","162","/*******************************************************************************
+parseCommandLine.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+Options parseCommandLine(int argc, char *argv[]) {
+ static struct option Arguments[] = {
+ {""version"", no_argument, nullptr, 'v'},
+ {""reference"", required_argument, nullptr, 'r'},
+ {""dbase"", required_argument, nullptr, 'd'},
+ {""scores"", required_argument, nullptr, 's'},
+ {""out"", required_argument, nullptr, 'o'},
+ {""format"", required_argument, nullptr, 'f'},
+ {""scoreOnly"", no_argument, nullptr, 1},
+ {""rankBy"", required_argument, nullptr, 2},
+ {""best"", required_argument, nullptr, 4},
+ {""addIterations"", required_argument, nullptr, 5},
+ {""cutoff"", required_argument, nullptr, 6},
+ {""noRef"", no_argument, nullptr, 11},
+ {""help"", no_argument, nullptr, 'h'}};
+
+ Options o;
+
+ int choice;
+ opterr = 0;
+ int optionIndex = 0;
+ std::string s;
+
+ while ((choice = getopt_long(argc, argv, ""vhpr:d:s:o:f:"", Arguments,
+ &optionIndex)) != -1) {
+ switch (choice) {
+ case 'v': //....................................................version
+ o.version = true;
+ break;
+
+ case 'r': //..................................................reference
+ o.refInpFile = optarg;
+ o.refInpStream = new std::ifstream(optarg);
+ if (!o.refInpStream->good()) {
+ mainErr(""Error opening input file for reference (-r)"");
+ }
+ break;
+
+ case 'd': //......................................................dbase
+ o.dbInpFile = optarg;
+ o.dbInpStream = new std::ifstream(optarg);
+ if (!o.dbInpStream->good()) {
+ mainErr(""Error opening input file for database (-d)"");
+ }
+ break;
+
+ case 's': //.....................................................scores
+ o.scoreOutFile = optarg;
+ o.scoreOutStream = new std::ofstream(optarg);
+ if (!o.scoreOutStream->good()) {
+ mainErr(""Error opening output file for scores (-s)"");
+ }
+ break;
+
+ case 'o': //........................................................out
+ o.molOutFile = optarg;
+ o.molOutStream = new std::ofstream(optarg);
+ if (!o.molOutStream->good()) {
+ mainErr(""Error opening output file for molecules (-o)"");
+ }
+ break;
+
+ case 'f': //.....................................................format
+ o.format = optarg;
+#ifdef USE_RDKIT
+ if (!o.format.empty() && o.format != ""SDF"") {
+ mainErr(""RDKit implementation currently only supports SDF (-f)"");
+ }
+#endif
+ break;
+
+ case 1: //....................................................scoreOnly
+ o.scoreOnly = true;
+ break;
+
+ case 2: //.......................................................rankBy
+ s = optarg;
+ transform(s.begin(), s.end(), s.begin(), toupper);
+ if (s == ""TANIMOTO"") {
+ o.whichScore = tanimoto;
+ } else if (s == ""TVERSKY_DB"") {
+ o.whichScore = tversky_db;
+ } else if (s == ""TVERSKY_REF"") {
+ o.whichScore = tversky_ref;
+ }
+ break;
+
+ case 4: //.........................................................best
+ o.bestHits = strtol(optarg, nullptr, 10);
+ break;
+
+ case 5: //................................................addIterations
+ o.maxIter = strtol(optarg, nullptr, 10);
+ break;
+
+ case 6: //.......................................................cutoff
+ o.cutOff = strtod(optarg, nullptr);
+ if (o.cutOff > 1) {
+ o.cutOff = 1.0;
+ } else if (o.cutOff < 0) {
+ o.cutOff = 0.0;
+ }
+ break;
+
+ case 11: //.......................................................noRef
+ o.showRef = false;
+ break;
+
+ case 'h': //.......................................................help
+ o.help = true;
+ break;
+
+ default:
+ mainErr(""Unknown command line option"");
+ }
+ }
+
+ // If no options are given print the help
+ if (optind == 1) {
+ o.help = true;
+ }
+
+ argc -= optind;
+ argv += optind;
+ return o;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/bestResults.cpp",".cpp","3248","115","/*******************************************************************************
+bestResults.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+BestResults::BestResults(unsigned int n) {
+ _bestList.clear();
+ _size = n;
+ _lowest = 0.0;
+ _filled = 0;
+}
+
+BestResults::~BestResults() {
+ std::vector::iterator it;
+ for (it = _bestList.begin(); it != _bestList.end(); ++it) {
+ if (*it != NULL) {
+ delete *it;
+ *it = NULL;
+ }
+ }
+}
+
+bool BestResults::add(SolutionInfo &res) {
+ std::vector::reverse_iterator it;
+ if (_filled < _size) {
+ auto *i = new SolutionInfo(res);
+ _bestList.push_back(i);
+ ++_filled;
+ } else if (res.score < _lowest) {
+ return false;
+ } else {
+ // delete last element
+ it = _bestList.rbegin();
+ if (*it != NULL) {
+ delete *it;
+ *it = NULL;
+ }
+
+ // make new info element in the list
+ *it = new SolutionInfo(res);
+ }
+
+ std::sort(_bestList.begin(), _bestList.end(), BestResults::_compInfo());
+ it = _bestList.rbegin();
+ _lowest = (*it)->score;
+
+ return true;
+}
+
+#if 0
+void
+BestResults::writeMolecules(Options* uo)
+{
+ if (uo->molOutWriter == NULL)
+ {
+ return;
+ }
+
+ std::vector::iterator it;
+ for (it = _bestList.begin(); it != _bestList.end(); ++it)
+ {
+ if (*it != NULL)
+ {
+ uo->molOutWriter->Write(&((*it)->dbMol), uo->molOutStream);
+ }
+ }
+ return;
+}
+#endif
+
+void BestResults::writeScores(Options *uo) {
+ if (uo->scoreOutStream == nullptr) {
+ return;
+ }
+
+ std::vector score(3);
+ std::vector::iterator it;
+ for (it = _bestList.begin(); it != _bestList.end(); ++it) {
+ if (*it != NULL) {
+ (*it)->printScores(*uo);
+ }
+ }
+ return;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/moleculeRotation.cpp",".cpp","7206","175","/*******************************************************************************
+moleculeRotation.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+#ifndef USE_RDKIT
+// OpenBabel
+#include
+#include
+
+void positionMolecule(OpenBabel::OBMol &m, const Coordinate ¢roid,
+ const SiMath::Matrix &rotation) {
+ std::vector::iterator i;
+ for (OpenBabel::OBAtom *a = m.BeginAtom(i); a; a = m.NextAtom(i)) {
+ // Translate point
+ double x = a->GetX() - centroid.x;
+ double y = a->GetY() - centroid.y;
+ double z = a->GetZ() - centroid.z;
+
+ // Rotate according to eigenvectors SVD
+ a->SetVector(rotation[0][0] * x + rotation[1][0] * y + rotation[2][0] * z,
+ rotation[0][1] * x + rotation[1][1] * y + rotation[2][1] * z,
+ rotation[0][2] * x + rotation[1][2] * y + rotation[2][2] * z);
+ }
+ return;
+}
+
+void repositionMolecule(OpenBabel::OBMol &m, const SiMath::Matrix &rotation,
+ const Coordinate ¢roid) {
+ std::vector::iterator i;
+ for (OpenBabel::OBAtom *a = m.BeginAtom(i); a; a = m.NextAtom(i)) {
+ // Get coordinates
+ double x = a->GetX();
+ double y = a->GetY();
+ double z = a->GetZ();
+
+ // Rotate according to eigenvectors SVD
+ double xx = rotation[0][0] * x + rotation[0][1] * y + rotation[0][2] * z;
+ double yy = rotation[1][0] * x + rotation[1][1] * y + rotation[1][2] * z;
+ double zz = rotation[2][0] * x + rotation[2][1] * y + rotation[2][2] * z;
+
+ a->SetVector(xx + centroid.x, yy + centroid.y, zz + centroid.z);
+ }
+ return;
+}
+
+void rotateMolecule(OpenBabel::OBMol &m, const SiMath::Vector &rotor) {
+ // Build rotation matrix
+ SiMath::Matrix rot(3, 3, 0.0);
+ double r1 = rotor[1] * rotor[1];
+ double r2 = rotor[2] * rotor[2];
+ double r3 = rotor[3] * rotor[3];
+
+ rot[0][0] = 1.0 - 2.0 * r2 - 2.0 * r3;
+ rot[0][1] = 2.0 * (rotor[1] * rotor[2] - rotor[0] * rotor[3]);
+ rot[0][2] = 2.0 * (rotor[1] * rotor[3] + rotor[0] * rotor[2]);
+ rot[1][0] = 2.0 * (rotor[1] * rotor[2] + rotor[0] * rotor[3]);
+ rot[1][1] = 1.0 - 2 * r3 - 2 * r1;
+ rot[1][2] = 2.0 * (rotor[2] * rotor[3] - rotor[0] * rotor[1]);
+ rot[2][0] = 2.0 * (rotor[1] * rotor[3] - rotor[0] * rotor[2]);
+ rot[2][1] = 2.0 * (rotor[2] * rotor[3] + rotor[0] * rotor[1]);
+ rot[2][2] = 1.0 - 2 * r2 - 2 * r1;
+
+ std::vector::iterator i;
+ for (OpenBabel::OBAtom *a = m.BeginAtom(i); a; a = m.NextAtom(i)) {
+ // Translate point
+ double x = a->GetX();
+ double y = a->GetY();
+ double z = a->GetZ();
+
+ // rotate according to eigenvectors SVD
+ a->SetVector(rot[0][0] * x + rot[0][1] * y + rot[0][2] * z,
+ rot[1][0] * x + rot[1][1] * y + rot[1][2] * z,
+ rot[2][0] * x + rot[2][1] * y + rot[2][2] * z);
+ }
+ return;
+}
+#else
+#include
+#include
+#include
+
+void positionMolecule(RDKit::ROMol &m, const Coordinate ¢roid,
+ const SiMath::Matrix &rotation) {
+ RDKit::Conformer &conf = m.getConformer();
+ RDGeom::Point3D rdcentroid(centroid.x, centroid.y, centroid.z);
+ for (unsigned int i = 0; i < m.getNumAtoms(); ++i) {
+ RDGeom::Point3D tp = conf.getAtomPos(i) - rdcentroid;
+ conf.setAtomPos(
+ i, RDGeom::Point3D(rotation[0][0] * tp.x + rotation[1][0] * tp.y +
+ rotation[2][0] * tp.z,
+ rotation[0][1] * tp.x + rotation[1][1] * tp.y +
+ rotation[2][1] * tp.z,
+ rotation[0][2] * tp.x + rotation[1][2] * tp.y +
+ rotation[2][2] * tp.z));
+ }
+}
+
+void repositionMolecule(RDKit::ROMol &m, const SiMath::Matrix &rotation,
+ const Coordinate ¢roid) {
+ RDKit::Conformer &conf = m.getConformer();
+ RDGeom::Point3D rdcentroid(centroid.x, centroid.y, centroid.z);
+ for (unsigned int i = 0; i < m.getNumAtoms(); ++i) {
+ RDGeom::Point3D tp = conf.getAtomPos(i);
+ conf.setAtomPos(
+ i, RDGeom::Point3D(rotation[0][0] * tp.x + rotation[0][1] * tp.y +
+ rotation[0][2] * tp.z,
+ rotation[1][0] * tp.x + rotation[1][1] * tp.y +
+ rotation[1][2] * tp.z,
+ rotation[2][0] * tp.x + rotation[2][1] * tp.y +
+ rotation[2][2] * tp.z));
+ conf.getAtomPos(i) += rdcentroid;
+ }
+ return;
+}
+
+void rotateMolecule(RDKit::ROMol &m, const SiMath::Vector &rotor) {
+ RDKit::Conformer &conf = m.getConformer();
+ // Build rotation matrix
+ SiMath::Matrix rot(3, 3, 0.0);
+ double r1 = rotor[1] * rotor[1];
+ double r2 = rotor[2] * rotor[2];
+ double r3 = rotor[3] * rotor[3];
+
+ rot[0][0] = 1.0 - 2.0 * r2 - 2.0 * r3;
+ rot[0][1] = 2.0 * (rotor[1] * rotor[2] - rotor[0] * rotor[3]);
+ rot[0][2] = 2.0 * (rotor[1] * rotor[3] + rotor[0] * rotor[2]);
+ rot[1][0] = 2.0 * (rotor[1] * rotor[2] + rotor[0] * rotor[3]);
+ rot[1][1] = 1.0 - 2 * r3 - 2 * r1;
+ rot[1][2] = 2.0 * (rotor[2] * rotor[3] - rotor[0] * rotor[1]);
+ rot[2][0] = 2.0 * (rotor[1] * rotor[3] - rotor[0] * rotor[2]);
+ rot[2][1] = 2.0 * (rotor[2] * rotor[3] + rotor[0] * rotor[1]);
+ rot[2][2] = 1.0 - 2 * r2 - 2 * r1;
+
+ for (unsigned int i = 0; i < m.getNumAtoms(); ++i) {
+ RDGeom::Point3D tp = conf.getAtomPos(i);
+ conf.setAtomPos(
+ i, RDGeom::Point3D(
+ rot[0][0] * tp.x + rot[0][1] * tp.y + rot[0][2] * tp.z,
+ rot[1][0] * tp.x + rot[1][1] * tp.y + rot[1][2] * tp.z,
+ rot[2][0] * tp.x + rot[2][1] * tp.y + rot[2][2] * tp.z));
+ }
+}
+
+#endif","C++"
+"In Silico","rdkit/shape-it","src/catch_main.cpp",".cpp","1547","31","//
+// Copyright (C) 2021 Greg Landrum and the Shape-it contributors
+//
+/*
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+*/
+#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
+ // this in one cpp file
+#include ""catch2/catch_all.hpp""
+","C++"
+"In Silico","rdkit/shape-it","src/main.cpp",".cpp","10944","370","/*******************************************************************************
+main.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+// General
+#include
+#include
+#include
+#include
+#include
+
+#ifndef USE_RDKIT
+// OpenBabel
+#include
+#include
+#else
+#include
+#include
+#include
+#endif
+
+// Pharao
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+//*--------------------------------------------------------------------------*//
+//* MAIN MAIN
+//*//
+//*--------------------------------------------------------------------------*//
+int main(int argc, char *argv[]) {
+ // Initialise random number generator
+ srandom(time(nullptr));
+ clock_t t0 = clock();
+
+ // Print header
+ printHeader();
+
+ // Read options
+ Options uo = parseCommandLine(argc, argv);
+ if (uo.version) {
+ printHeader();
+ exit(0);
+ }
+
+ if (uo.help) {
+ printUsage();
+ exit(0);
+ }
+ std::cerr << uo.print();
+
+ // Files
+ if (uo.dbInpFile.empty()) {
+ mainErr(""Missing database file. This is a required option (-d)."");
+ }
+ if (uo.refInpFile.empty()) {
+ mainErr(""Missing ref file. This is a required option (-r)."");
+ }
+
+ if (uo.molOutFile.empty() && uo.scoreOutFile.empty()) {
+ mainErr(""At least one of the -o or -s option should be used."");
+ }
+
+ // Create a list to store the best results
+ BestResults *bestHits = nullptr;
+ if (uo.bestHits != 0) {
+ bestHits = new BestResults(uo.bestHits);
+ }
+
+ // Print header line to score output file
+ if (!uo.scoreOutFile.empty()) {
+ *(uo.scoreOutStream) << ""dbName""
+ << ""\t""
+ << ""refName""
+ << ""\t"" << tanimoto << ""\t"" << tversky_ref << ""\t""
+ << tversky_db << ""\t""
+ << ""overlap""
+ << ""\t""
+ << ""refVolume""
+ << ""\t""
+ << ""dbVolume"" << std::endl;
+ }
+
+ // Create reference molecule
+ std::string refName;
+#ifndef USE_RDKIT
+ OpenBabel::OBMol refMol;
+ OpenBabel::OBConversion refInpReader;
+ if (uo.format.empty()) {
+ refInpReader.SetInFormat(refInpReader.FormatFromExt(uo.refInpFile));
+ } else {
+ refInpReader.SetInFormat(refInpReader.FindFormat(uo.format));
+ }
+ refInpReader.Read(&refMol, uo.refInpStream);
+ refName = refMol.GetTitle();
+#else
+ bool takeOwnership = false;
+ bool sanitize = true;
+
+ bool removeHs = false;
+ RDKit::ForwardSDMolSupplier refsuppl(uo.refInpStream, takeOwnership, sanitize,
+ removeHs);
+ std::unique_ptr refmptr(refsuppl.next());
+ if (!refmptr) {
+ mainErr(""Could not parse reference molecule"");
+ }
+ RDKit::ROMol &refMol = *refmptr;
+ refMol.getPropIfPresent(""_Name"", refName);
+#endif
+ if (refName == """") {
+ refName = ""Unnamed_ref"";
+ }
+
+ // Create the refence set of Gaussians
+ GaussianVolume refVolume;
+
+ // List all Gaussians and their respective intersections
+ listAtomVolumes(refMol, refVolume);
+
+ // Move the Gaussian towards its center of geometry and align with principal
+ // axes
+ if (!uo.scoreOnly) {
+ initOrientation(refVolume);
+ }
+
+ // Write reference molecule to output
+#ifndef USE_RDKIT
+ std::unique_ptr dbOutWriter(
+ new OpenBabel::OBConversion());
+ if (uo.format.empty()) {
+ dbOutWriter->SetOutFormat(dbOutWriter->FormatFromExt(uo.molOutFile));
+ } else {
+ dbOutWriter->SetOutFormat(dbOutWriter->FindFormat(uo.format));
+ }
+ if (uo.showRef && !uo.molOutFile.empty()) {
+ dbOutWriter->Write(&refMol, uo.molOutStream);
+ }
+#else
+ std::unique_ptr dbOutWriter;
+ if (uo.molOutStream != nullptr) {
+ dbOutWriter.reset(new RDKit::SDWriter(uo.molOutStream, false));
+ dbOutWriter->write(refMol);
+ }
+#endif
+
+ // Open database stream
+ unsigned molCount(0);
+ std::ostringstream ss;
+#ifndef USE_RDKIT
+ OpenBabel::OBMol dbMol;
+ OpenBabel::OBConversion dbInpReader;
+ if (uo.format.empty()) {
+ dbInpReader.SetInFormat(dbInpReader.FormatFromExt(uo.dbInpFile));
+ } else {
+ dbInpReader.SetInFormat(dbInpReader.FindFormat(uo.format));
+ }
+ dbInpReader.SetInStream(uo.dbInpStream);
+
+ while (dbInpReader.Read(&dbMol)) {
+#else
+ RDKit::ForwardSDMolSupplier dbsuppl(uo.dbInpStream, takeOwnership, sanitize,
+ removeHs);
+ while (!dbsuppl.atEnd()) {
+ std::unique_ptr dbmptr(dbsuppl.next());
+ if (!dbmptr) {
+ continue;
+ }
+ RDKit::ROMol &dbMol = *dbmptr;
+#endif
+ ++molCount;
+
+ // Keep track of the number of molecules processed so far
+ if ((molCount % 10) == 0) {
+ std::cerr << ""."";
+ if ((molCount % 500) == 0) {
+ std::cerr << "" "" << molCount << "" molecules"" << std::endl;
+ }
+ }
+
+ std::string dbName;
+#ifndef USE_RDKIT
+ dbName = dbMol.GetTitle();
+#else
+ dbMol.getPropIfPresent(""_Name"", dbName);
+#endif
+ if (dbName == """") {
+ ss.str("""");
+ ss << ""MOL_"" << molCount;
+ dbName = ss.str();
+#ifndef USE_RDKIT
+ dbMol.SetTitle(dbName);
+#else
+ dbMol.setProp(""_Name"", dbName);
+#endif
+ }
+
+ // Create the set of Gaussians of database molecule
+ GaussianVolume dbVolume;
+ listAtomVolumes(dbMol, dbVolume);
+
+ // Overlap with reference
+ AlignmentInfo res;
+ double bestScore(0.0);
+
+ SolutionInfo bestSolution;
+ if (uo.scoreOnly) {
+ res.overlap = atomOverlap(refVolume, dbVolume);
+ res.rotor[0] = 1.0;
+ bestScore = getScore(uo.whichScore, res.overlap, refVolume.overlap,
+ dbVolume.overlap);
+ bestSolution.refAtomVolume = refVolume.overlap;
+ updateSolutionInfo(bestSolution, res, bestScore, dbVolume);
+ } else {
+ initOrientation(dbVolume);
+
+ bestSolution = std::move(shapeit::alignVolumes(
+ refVolume, dbVolume, uo.whichScore, uo.maxIter));
+ }
+
+#ifndef USE_RDKIT
+ bestSolution.dbMol = dbMol;
+#else
+ bestSolution.dbMol = dbMol;
+#endif
+ bestSolution.refName = refName;
+ bestSolution.dbName = dbName;
+
+ // Cleanup local pointers to atom-gaussians
+ dbVolume.gaussians.clear();
+ dbVolume.levels.clear();
+ for (auto &childOverlap : dbVolume.childOverlaps) {
+ delete childOverlap;
+ childOverlap = nullptr;
+ }
+ dbVolume.childOverlaps.clear();
+
+ // At this point the information of the solution is stored in bestSolution
+ // Check if the result is better than the cutoff
+ if (bestSolution.score < uo.cutOff) {
+ continue;
+ }
+
+ // Post-process molecules
+ if (uo.bestHits || !uo.molOutFile.empty()) {
+ // Add the score properties
+ setAllScores(bestSolution);
+
+ if (!uo.scoreOnly) {
+ // Translate and rotate the molecule towards its centroid and inertia
+ // axes
+ positionMolecule(bestSolution.dbMol, bestSolution.dbCenter,
+ bestSolution.dbRotation);
+
+ // Rotate molecule with the optimal
+ rotateMolecule(bestSolution.dbMol, bestSolution.rotor);
+
+ // Rotate and translate the molecule with the inverse rotation and
+ // translation of the reference molecule
+ repositionMolecule(bestSolution.dbMol, refVolume.rotation,
+ refVolume.centroid);
+ }
+
+ if (uo.bestHits) {
+ bestHits->add(bestSolution);
+ } else if (!uo.molOutFile.empty() && dbOutWriter != nullptr) {
+#ifndef USE_RDKIT
+ dbOutWriter->Write(&(bestSolution.dbMol), uo.molOutStream);
+#else
+ dbOutWriter->write(bestSolution.dbMol);
+#endif
+ }
+ }
+
+ if ((uo.bestHits == 0) && !uo.scoreOutFile.empty()) {
+ bestSolution.printScores(uo);
+ }
+
+#ifndef USE_RDKIT
+ // Clear current molecule
+ dbMol.Clear();
+#endif
+ }
+
+ if (uo.bestHits) {
+ if (!uo.molOutFile.empty()) {
+ for (const auto molptr : bestHits->getBestList()) {
+ if (molptr != nullptr && dbOutWriter != nullptr) {
+#ifndef USE_RDKIT
+ dbOutWriter->Write(&(molptr->dbMol), uo.molOutStream);
+#else
+ dbOutWriter->write(molptr->dbMol);
+#endif
+ }
+ }
+ delete uo.molOutStream;
+ uo.molOutStream = nullptr;
+ }
+ if (!uo.scoreOutFile.empty()) {
+ bestHits->writeScores(&uo);
+ delete uo.scoreOutStream;
+ uo.scoreOutStream = nullptr;
+ }
+ }
+
+ // Clear current streams
+ if (uo.dbInpStream != nullptr) {
+ delete uo.dbInpStream;
+ uo.dbInpStream = nullptr;
+ }
+ if (uo.refInpStream != nullptr) {
+ delete uo.refInpStream;
+ uo.refInpStream = nullptr;
+ }
+
+ // Done processing database
+ std::cerr << std::endl;
+ std::cerr << ""Processed "" << molCount << "" molecules"" << std::endl;
+ double tt = (double)(clock() - t0) / CLOCKS_PER_SEC;
+ std::cerr << molCount << "" molecules in "" << tt << "" seconds ("";
+ std::cerr << molCount / tt << "" molecules per second)"" << std::endl;
+
+ // Cleanup local db volume
+ refVolume.gaussians.clear();
+ for (auto &childOverlap : refVolume.childOverlaps) {
+ delete childOverlap;
+ childOverlap = nullptr;
+ }
+
+ exit(0);
+}
+","C++"
+"In Silico","rdkit/shape-it","src/basic_tests.cpp",".cpp","5691","157","//
+// Copyright (C) 2021 Greg Landrum and the Shape-it contributors
+/*
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+*/
+
+#include ""catch2/catch_all.hpp""
+
+#include
+#include
+#include
+
+using namespace RDKit;
+
+TEST_CASE(""align mols"", ""[library][molecules]"") {
+ std::string ref = R""CTAB(3l5u_lig_ZEC
+ 3D
+ Structure written by MMmdl.
+ 20 21 0 0 1 0 999 V2000
+ 15.0500 -34.9220 -18.1430 O 0 0 0 0 0 0
+ 14.9110 -34.7040 -19.4790 C 0 0 0 0 0 0
+ 14.7350 -35.7750 -20.3500 C 0 0 0 0 0 0
+ 14.6060 -35.5430 -21.7160 C 0 0 0 0 0 0
+ 14.3620 -36.6080 -23.0370 S 0 0 0 0 0 0
+ 14.3210 -35.3400 -24.1850 C 0 0 0 0 0 0
+ 14.0030 -35.5290 -25.8940 S 0 0 0 0 0 0
+ 15.1750 -34.7990 -26.7570 N 0 0 0 0 0 0
+ 12.6760 -34.9070 -26.2170 O 0 0 0 0 0 0
+ 13.9630 -36.9930 -26.2180 O 0 0 0 0 0 0
+ 14.4970 -34.1790 -23.5590 N 0 0 0 0 0 0
+ 14.6510 -34.2470 -22.2350 C 0 0 0 0 0 0
+ 14.8270 -33.1870 -21.3480 C 0 0 0 0 0 0
+ 14.9560 -33.4070 -19.9800 C 0 0 0 0 0 0
+ 15.1610 -34.0820 -17.6920 H 0 0 0 0 0 0
+ 14.6990 -36.7830 -19.9650 H 0 0 0 0 0 0
+ 15.1440 -34.8130 -27.7660 H 0 0 0 0 0 0
+ 15.9340 -34.3310 -26.2820 H 0 0 0 0 0 0
+ 14.8640 -32.1730 -21.7190 H 0 0 0 0 0 0
+ 15.0910 -32.5720 -19.3090 H 0 0 0 0 0 0
+ 1 2 1 0 0 0
+ 1 15 1 0 0 0
+ 2 3 2 0 0 0
+ 2 14 1 0 0 0
+ 3 4 1 0 0 0
+ 3 16 1 0 0 0
+ 4 5 1 0 0 0
+ 4 12 2 0 0 0
+ 5 6 1 0 0 0
+ 6 7 1 0 0 0
+ 6 11 2 0 0 0
+ 7 8 1 0 0 0
+ 7 9 2 0 0 0
+ 7 10 2 0 0 0
+ 8 17 1 0 0 0
+ 8 18 1 0 0 0
+ 11 12 1 0 0 0
+ 12 13 1 0 0 0
+ 13 14 2 0 0 0
+ 13 19 1 0 0 0
+ 14 20 1 0 0 0
+M END)CTAB"";
+ std::string probe = R""CTAB(3hof_lig_DHC
+ 3D
+ Structure written by MMmdl.
+ 20 20 0 0 1 0 999 V2000
+ 14.6290 -34.5170 -18.4190 C 0 0 0 0 0 0
+ 15.6070 -34.6620 -17.5400 O 0 0 0 0 0 0
+ 14.9220 -34.5200 -19.8370 C 0 0 0 0 0 0
+ 14.7370 -35.7220 -20.3520 C 0 0 0 0 0 0
+ 14.9680 -35.9740 -21.7740 C 0 0 0 0 0 0
+ 14.8780 -34.9380 -22.6930 C 0 0 0 0 0 0
+ 15.1020 -35.2380 -24.0360 C 0 0 0 0 0 0
+ 15.4390 -36.6310 -24.4550 C 0 0 0 0 0 0
+ 15.5160 -37.6070 -23.4830 C 0 0 0 0 0 0
+ 15.2760 -37.2740 -22.1560 C 0 0 0 0 0 0
+ 15.6830 -36.9520 -25.7670 O 0 0 0 0 0 0
+ 15.0160 -34.2570 -24.9550 O 0 0 0 0 0 0
+ 13.4860 -34.4200 -18.0300 O 0 5 0 0 0 0
+ 15.2430 -33.6100 -20.3240 H 0 0 0 0 0 0
+ 14.4110 -36.5360 -19.7210 H 0 0 0 0 0 0
+ 14.6410 -33.9380 -22.3590 H 0 0 0 0 0 0
+ 15.7620 -38.6220 -23.7550 H 0 0 0 0 0 0
+ 15.3330 -38.0570 -21.4140 H 0 0 0 0 0 0
+ 15.1950 -34.6170 -25.8260 H 0 0 0 0 0 0
+ 15.8806 -37.8889 -25.8363 H 0 0 0 0 0 0
+ 1 2 2 0 0 0
+ 1 3 1 0 0 0
+ 1 13 1 0 0 0
+ 3 4 2 0 0 0
+ 3 14 1 0 0 0
+ 4 5 1 0 0 0
+ 4 15 1 0 0 0
+ 5 6 2 0 0 0
+ 5 10 1 0 0 0
+ 6 7 1 0 0 0
+ 6 16 1 0 0 0
+ 7 8 2 0 0 0
+ 7 12 1 0 0 0
+ 8 9 1 0 0 0
+ 8 11 1 0 0 0
+ 9 10 2 0 0 0
+ 9 17 1 0 0 0
+ 10 18 1 0 0 0
+ 11 20 1 0 0 0
+ 12 19 1 0 0 0
+M CHG 1 13 -1
+M END)CTAB"";
+
+ bool sanitize = true;
+ bool removeHs = false;
+ std::unique_ptr refMol{MolBlockToMol(ref, sanitize, removeHs)};
+ REQUIRE(refMol);
+ std::unique_ptr prbMol{MolBlockToMol(probe, sanitize, removeHs)};
+ SECTION(""basics"") {
+ auto solution = shapeit::alignMols(*refMol, *prbMol);
+ CHECK(solution.score == Catch::Approx(0.647).margin(0.01));
+ }
+ SECTION(""self"") {
+ ROMol m2(*refMol);
+ auto solution = shapeit::alignMols(*refMol, m2);
+ CHECK(solution.score == Catch::Approx(1.00).margin(0.01));
+ }
+ SECTION(""align to volume"") {
+ GaussianVolume refVolume;
+ listAtomVolumes(*refMol, refVolume);
+ initOrientation(refVolume);
+ auto solution = shapeit::alignMolToVolume(refVolume, *prbMol);
+
+ refVolume.gaussians.clear();
+ for (auto &childOverlap : refVolume.childOverlaps) {
+ delete childOverlap;
+ childOverlap = nullptr;
+ }
+ refVolume.childOverlaps.clear();
+ }
+}","C++"
+"In Silico","rdkit/shape-it","src/alignLib.cpp",".cpp","6035","181","/*******************************************************************************
+alignLib.cpp - Shape-it
+
+Copyright 2021 by Greg Landrum and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+namespace shapeit {
+
+SolutionInfo
+alignMols(const Molecule &refMol, const Molecule &dbMol,
+ const std::string &whichScore, double maxIter, double cutoff,
+ BestResults *bestHits) { // Create the refence set of Gaussians
+ GaussianVolume refVolume;
+
+ // List all Gaussians and their respective intersections
+ listAtomVolumes(refMol, refVolume);
+
+ // Move the Gaussian towards its center of geometry and align with principal
+ // axes
+ initOrientation(refVolume);
+ auto res =
+ alignMolToVolume(refVolume, dbMol, whichScore, maxIter, cutoff, bestHits);
+
+#ifndef USE_RDKIT
+ res.refName = refMol.GetTitle();
+#else
+ refMol.getProp(""_Name"", res.refName);
+#endif
+
+ // Cleanup local pointers to atom-gaussians
+ refVolume.gaussians.clear();
+ refVolume.levels.clear();
+ for (const auto ci : refVolume.childOverlaps) {
+ delete ci;
+ }
+ refVolume.childOverlaps.clear();
+
+ return res;
+}
+
+SolutionInfo alignMolToVolume(const GaussianVolume &refVolume,
+ const Molecule &dbMol,
+ const std::string &whichScore, double maxIter,
+ double cutoff, BestResults *bestHits) {
+ // Create the set of Gaussians of database molecule
+ GaussianVolume dbVolume;
+ listAtomVolumes(dbMol, dbVolume);
+ initOrientation(dbVolume);
+
+ // Overlap with reference
+ AlignmentInfo res;
+ double bestScore(0.0);
+
+ SolutionInfo bestSolution =
+ std::move(alignVolumes(refVolume, dbVolume, whichScore, maxIter));
+ bestSolution.refName = """";
+#ifndef USE_RDKIT
+ bestSolution.dbMol = dbMol;
+ bestSolution.dbName = dbMol.GetTitle();
+#else
+ bestSolution.dbMol = dbMol;
+ dbMol.getProp(""_Name"", bestSolution.dbName);
+#endif
+
+ // Cleanup local pointers to atom-gaussians
+ dbVolume.gaussians.clear();
+ dbVolume.levels.clear();
+ for (auto &childOverlap : dbVolume.childOverlaps) {
+ delete childOverlap;
+ childOverlap = nullptr;
+ }
+ dbVolume.childOverlaps.clear();
+
+ if (bestSolution.score < cutoff) {
+ return bestSolution;
+ }
+
+ // Add the score properties
+ setAllScores(bestSolution);
+
+ // Translate and rotate the molecule towards its centroid and inertia
+ // axes
+ positionMolecule(bestSolution.dbMol, bestSolution.dbCenter,
+ bestSolution.dbRotation);
+
+ // Rotate molecule with the optimal
+ rotateMolecule(bestSolution.dbMol, bestSolution.rotor);
+
+ // Rotate and translate the molecule with the inverse rotation and
+ // translation of the reference molecule
+ repositionMolecule(bestSolution.dbMol, refVolume.rotation,
+ refVolume.centroid);
+
+ if (bestHits) {
+ bestHits->add(bestSolution);
+ }
+ return bestSolution;
+}
+
+SolutionInfo alignVolumes(const GaussianVolume &refVolume,
+ const GaussianVolume &dbVolume,
+ const std::string &whichScore, double maxIter) {
+ SolutionInfo res;
+ res.refAtomVolume = refVolume.overlap;
+ res.refCenter = refVolume.centroid;
+ res.refRotation = refVolume.rotation;
+
+ ShapeAlignment aligner(refVolume, dbVolume);
+ aligner.setMaxIterations(maxIter);
+
+ AlignmentInfo alignment;
+ double bestScore = 0;
+ for (unsigned int l = 0; l < 4; ++l) {
+ SiMath::Vector quat(4, 0.0);
+ quat[l] = 1.0;
+ AlignmentInfo nextAlignment = aligner.gradientAscent(quat);
+ checkVolumes(refVolume, dbVolume, nextAlignment);
+ double ss = getScore(whichScore, nextAlignment.overlap, refVolume.overlap,
+ dbVolume.overlap);
+ if (ss > bestScore) {
+ alignment = nextAlignment;
+ bestScore = ss;
+ }
+
+ if (bestScore > 0.98) {
+ break;
+ }
+ }
+
+ // Check if additional simulated annealing steps are requested and start
+ // from the current best solution
+ if (maxIter > 0) {
+ AlignmentInfo nextRes = aligner.simulatedAnnealing(alignment.rotor);
+ checkVolumes(refVolume, dbVolume, nextRes);
+ double ss = getScore(whichScore, nextRes.overlap, refVolume.overlap,
+ dbVolume.overlap);
+ if (ss > bestScore) {
+ bestScore = ss;
+ alignment = nextRes;
+ }
+ }
+ // Optimal alignment information is stored in res and bestScore
+ // => result reporting and post-processing
+ updateSolutionInfo(res, alignment, bestScore, dbVolume);
+
+ return std::move(res);
+}
+} // namespace shapeit","C++"
+"In Silico","rdkit/shape-it","src/atomGaussian.cpp",".cpp","2606","68","/*******************************************************************************
+atomGaussian.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+AtomGaussian::AtomGaussian()
+ : center(0.0, 0.0, 0.0), alpha(0.0), volume(0.0), C(0.0), nbr(0) {}
+
+AtomGaussian::~AtomGaussian() = default;
+
+AtomGaussian atomIntersection(AtomGaussian &a, AtomGaussian &b) {
+ AtomGaussian c;
+
+ // new alpha
+ c.alpha = a.alpha + b.alpha;
+
+ // new center
+ c.center.x = (a.alpha * a.center.x + b.alpha * b.center.x) / c.alpha;
+ c.center.y = (a.alpha * a.center.y + b.alpha * b.center.y) / c.alpha;
+ c.center.z = (a.alpha * a.center.z + b.alpha * b.center.z) / c.alpha;
+
+ // self-volume
+ double d = (a.center.x - b.center.x) * (a.center.x - b.center.x) +
+ (a.center.y - b.center.y) * (a.center.y - b.center.y) +
+ (a.center.z - b.center.z) * (a.center.z - b.center.z);
+
+ c.C = a.C * b.C * exp(-a.alpha * b.alpha / c.alpha * d);
+
+ double scale = PI / (c.alpha);
+
+ c.volume = c.C * scale * sqrt(scale);
+
+ // set the number of gaussians
+ c.nbr = a.nbr + b.nbr;
+
+ return c;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/shapeAlignment.cpp",".cpp","20140","633","/*******************************************************************************
+shapeAlignment.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+ShapeAlignment::ShapeAlignment(const GaussianVolume &gRef,
+ const GaussianVolume &gDb)
+ : _gRef(&gRef), _gDb(&gDb), _rAtoms(0), _rGauss(0), _dAtoms(0), _dGauss(0),
+ _maxSize(0), _maxIter(50), _matrixMap() {
+ // Loop over the single atom volumes of both molecules and make the
+ // combinations
+ _rAtoms = gRef.levels[0];
+ _dAtoms = gDb.levels[0];
+ _rGauss = gRef.gaussians.size();
+ _dGauss = gDb.gaussians.size();
+ _maxSize = _rGauss * _dGauss + 1;
+}
+
+ShapeAlignment::~ShapeAlignment() {
+ _gRef = nullptr;
+ _gDb = nullptr;
+
+ // Clear the matrix map
+ for (auto &mi : _matrixMap) {
+ delete[] mi.second;
+ mi.second = nullptr;
+ }
+}
+
+AlignmentInfo ShapeAlignment::gradientAscent(SiMath::Vector rotor) {
+ // Create a queue to hold the pairs to process
+ std::queue> processQueue;
+
+ // Helper variables
+ // Overlap matrix is stored as a double error
+ double *Aij;
+ double Aq[4];
+
+ double Vij(0.0);
+ double qAq(0.0);
+
+ std::vector *d1(nullptr);
+ std::vector *d2(nullptr);
+ std::vector::iterator it1;
+
+ // Gradient information
+ SiMath::Vector overGrad(4, 0.0);
+
+ // Hessian
+ SiMath::Matrix overHessian(4, 4, 0.0);
+
+ // Overlap volume
+ double atomOverlap(0.0);
+ double pharmOverlap(0.0);
+
+ // Solution info
+ AlignmentInfo res;
+
+ double oldVolume(0.0);
+ unsigned int iterations(0);
+ unsigned int mapIndex(0);
+
+ // unsigned int DBSize = _gDb->pharmacophores.size();
+
+ while (iterations < 20) {
+ // reset volume
+ atomOverlap = 0.0;
+ pharmOverlap = 0.0;
+ iterations++;
+
+ // reset gradient
+ overGrad = 0.0;
+
+ // reset hessian
+ overHessian = 0.0;
+
+ double lambda(0.0);
+
+ // iterator over the matrix map
+ MatIter matIter;
+
+ // create atom-atom overlaps
+ for (unsigned int i(0); i < _rAtoms; ++i) {
+ for (unsigned int j(0); j < _dAtoms; ++j) {
+ mapIndex = (i * _dGauss) + j;
+
+ if ((matIter = _matrixMap.find(mapIndex)) == _matrixMap.end()) {
+ Aij = _updateMatrixMap(_gRef->gaussians[i], _gDb->gaussians[j]);
+
+ // add to map
+ _matrixMap[mapIndex] = Aij;
+ } else {
+ Aij = matIter->second;
+ }
+
+ // rotor product
+ Aq[0] = Aij[0] * rotor[0] + Aij[1] * rotor[1] + Aij[2] * rotor[2] +
+ Aij[3] * rotor[3];
+ Aq[1] = Aij[4] * rotor[0] + Aij[5] * rotor[1] + Aij[6] * rotor[2] +
+ Aij[7] * rotor[3];
+ Aq[2] = Aij[8] * rotor[0] + Aij[9] * rotor[1] + Aij[10] * rotor[2] +
+ Aij[11] * rotor[3];
+ Aq[3] = Aij[12] * rotor[0] + Aij[13] * rotor[1] + Aij[14] * rotor[2] +
+ Aij[15] * rotor[3];
+
+ qAq = rotor[0] * Aq[0] + rotor[1] * Aq[1] + rotor[2] * Aq[2] +
+ rotor[3] * Aq[3];
+
+ // compute overlap volume
+ Vij = Aij[16] * exp(-qAq);
+
+ // check if overlap is sufficient enough, should be more than 0.1 for
+ // atom - atom overlap
+ if (Vij /
+ (_gRef->gaussians[i].volume + _gDb->gaussians[j].volume - Vij) <
+ EPS) {
+ continue;
+ }
+
+ // add to overlap volume
+ atomOverlap += Vij;
+
+ // update gradient -2Vij (Aijq);
+ double v2 = 2.0 * Vij;
+
+ lambda -= v2 * qAq;
+
+ overGrad[0] -= v2 * Aq[0];
+ overGrad[1] -= v2 * Aq[1];
+ overGrad[2] -= v2 * Aq[2];
+ overGrad[3] -= v2 * Aq[3];
+
+ // overHessian += 2*Vij(2*Aijq'qAij-Aij); (only upper triangular part)
+ overHessian[0][0] += v2 * (2.0 * Aq[0] * Aq[0] - Aij[0]);
+ overHessian[0][1] += v2 * (2.0 * Aq[0] * Aq[1] - Aij[1]);
+ overHessian[0][2] += v2 * (2.0 * Aq[0] * Aq[2] - Aij[2]);
+ overHessian[0][3] += v2 * (2.0 * Aq[0] * Aq[3] - Aij[3]);
+ overHessian[1][1] += v2 * (2.0 * Aq[1] * Aq[1] - Aij[5]);
+ overHessian[1][2] += v2 * (2.0 * Aq[1] * Aq[2] - Aij[6]);
+ overHessian[1][3] += v2 * (2.0 * Aq[1] * Aq[3] - Aij[7]);
+ overHessian[2][2] += v2 * (2.0 * Aq[2] * Aq[2] - Aij[10]);
+ overHessian[2][3] += v2 * (2.0 * Aq[2] * Aq[3] - Aij[11]);
+ overHessian[3][3] += v2 * (2.0 * Aq[3] * Aq[3] - Aij[15]);
+
+ // loop over child nodes and add to queue
+ d1 = _gRef->childOverlaps[i];
+ d2 = _gDb->childOverlaps[j];
+
+ // first add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+
+ // second add (child(i),j)
+ if (d1 != nullptr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ while (!processQueue.empty()) {
+ // get next element from queue
+ std::pair nextPair = processQueue.front();
+ processQueue.pop();
+
+ unsigned int i = nextPair.first;
+ unsigned int j = nextPair.second;
+
+ // check cache
+ mapIndex = (i * _dGauss) + j;
+ if ((matIter = _matrixMap.find(mapIndex)) == _matrixMap.end()) {
+ Aij = _updateMatrixMap(_gRef->gaussians[i], _gDb->gaussians[j]);
+ _matrixMap[mapIndex] = Aij;
+ } else {
+ Aij = matIter->second;
+ }
+
+ // rotor product
+ Aq[0] = Aij[0] * rotor[0] + Aij[1] * rotor[1] + Aij[2] * rotor[2] +
+ Aij[3] * rotor[3];
+ Aq[1] = Aij[4] * rotor[0] + Aij[5] * rotor[1] + Aij[6] * rotor[2] +
+ Aij[7] * rotor[3];
+ Aq[2] = Aij[8] * rotor[0] + Aij[9] * rotor[1] + Aij[10] * rotor[2] +
+ Aij[11] * rotor[3];
+ Aq[3] = Aij[12] * rotor[0] + Aij[13] * rotor[1] + Aij[14] * rotor[2] +
+ Aij[15] * rotor[3];
+
+ qAq = rotor[0] * Aq[0] + rotor[1] * Aq[1] + rotor[2] * Aq[2] +
+ rotor[3] * Aq[3];
+
+ // compute overlap volume
+ Vij = Aij[16] * exp(-qAq);
+
+ // check if overlap is sufficient enough
+ if (fabs(Vij) / (_gRef->gaussians[i].volume + _gDb->gaussians[j].volume -
+ fabs(Vij)) <
+ EPS) {
+ continue;
+ }
+
+ atomOverlap += Vij;
+
+ double v2 = 2.0 * Vij;
+
+ lambda -= v2 * qAq;
+
+ // update gradient -2Vij (Cij*Aij*q);
+ overGrad[0] -= v2 * Aq[0];
+ overGrad[1] -= v2 * Aq[1];
+ overGrad[2] -= v2 * Aq[2];
+ overGrad[3] -= v2 * Aq[3];
+
+ // hessian 2*Vij(2*AijqqAij-Aij); (only upper triangular part)
+ overHessian[0][0] += v2 * (2.0 * Aq[0] * Aq[0] - Aij[0]);
+ overHessian[0][1] += v2 * (2.0 * Aq[0] * Aq[1] - Aij[1]);
+ overHessian[0][2] += v2 * (2.0 * Aq[0] * Aq[2] - Aij[2]);
+ overHessian[0][3] += v2 * (2.0 * Aq[0] * Aq[3] - Aij[3]);
+ overHessian[1][1] += v2 * (2.0 * Aq[1] * Aq[1] - Aij[5]);
+ overHessian[1][2] += v2 * (2.0 * Aq[1] * Aq[2] - Aij[6]);
+ overHessian[1][3] += v2 * (2.0 * Aq[1] * Aq[3] - Aij[7]);
+ overHessian[2][2] += v2 * (2.0 * Aq[2] * Aq[2] - Aij[10]);
+ overHessian[2][3] += v2 * (2.0 * Aq[2] * Aq[3] - Aij[11]);
+ overHessian[3][3] += v2 * (2.0 * Aq[3] * Aq[3] - Aij[15]);
+
+ // loop over child nodes and add to queue
+ d1 = _gRef->childOverlaps[i];
+ d2 = _gDb->childOverlaps[j];
+ if (d1 != nullptr && _gRef->gaussians[i].nbr > _gDb->gaussians[j].nbr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ } else {
+ // first add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+ if (d1 != nullptr &&
+ _gDb->gaussians[j].nbr - _gRef->gaussians[i].nbr < 2) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ // check if the new volume is better than the previously found one
+ // if not quit the loop
+ if (iterations > 6 && atomOverlap < oldVolume + 0.0001) {
+ break;
+ }
+
+ // store latest volume found
+ oldVolume = atomOverlap;
+
+ // no measurable overlap between two volumes
+ if (std::isnan(lambda) || std::isnan(oldVolume) || oldVolume == 0) {
+ break;
+ }
+
+ // update solution
+ if (oldVolume > res.overlap) {
+ res.overlap = atomOverlap;
+ res.rotor = rotor;
+ if (res.overlap / (_gRef->overlap + _gDb->overlap - res.overlap) > 0.99) {
+ break;
+ }
+ }
+
+ // update the gradient and hessian
+ overHessian -= lambda;
+
+ // fill lower triangular of the hessian matrix
+ overHessian[1][0] = overHessian[0][1];
+ overHessian[2][0] = overHessian[0][2];
+ overHessian[2][1] = overHessian[1][2];
+ overHessian[3][0] = overHessian[0][3];
+ overHessian[3][1] = overHessian[1][3];
+ overHessian[3][2] = overHessian[2][3];
+
+ // update gradient to make h
+ overGrad[0] -= lambda * rotor[0];
+ overGrad[1] -= lambda * rotor[1];
+ overGrad[2] -= lambda * rotor[2];
+ overGrad[3] -= lambda * rotor[3];
+
+ // update gradient based on inverse hessian
+ SiMath::Vector tmp = rowProduct(overHessian, overGrad);
+ double h = overGrad[0] * tmp[0] + overGrad[1] * tmp[1] +
+ overGrad[2] * tmp[2] + overGrad[3] * tmp[3];
+
+ // small scaling of the gradient
+ h = 1 / h *
+ (overGrad[0] * overGrad[0] + overGrad[1] * overGrad[1] +
+ overGrad[2] * overGrad[2] + overGrad[3] * overGrad[3]);
+ overGrad *= h;
+
+ // update rotor based on gradient information
+ rotor -= overGrad;
+
+ // normalise rotor such that it has unit norm
+ double nr = sqrt(rotor[0] * rotor[0] + rotor[1] * rotor[1] +
+ rotor[2] * rotor[2] + rotor[3] * rotor[3]);
+
+ rotor[0] /= nr;
+ rotor[1] /= nr;
+ rotor[2] /= nr;
+ rotor[3] /= nr;
+
+ } // end of endless while loop
+ return res;
+}
+
+AlignmentInfo ShapeAlignment::simulatedAnnealing(SiMath::Vector rotor) {
+ // create a queue to hold the pairs to process
+ std::queue> processQueue;
+
+ // helper variables
+ // overlap matrix is stored as a double error
+ double *Aij;
+ double Aq[4];
+
+ // map store store already computed matrices
+ MatIter matIter;
+
+ double Vij(0.0), qAq(0.0);
+
+ std::vector *d1(nullptr);
+ std::vector *d2(nullptr);
+ std::vector::iterator it1;
+
+ // overlap volume
+ double atomOverlap(0.0);
+ double pharmOverlap(0.0);
+
+ double dTemperature = 1.1;
+
+ // solution info
+ AlignmentInfo res;
+
+ SiMath::Vector oldRotor(rotor);
+ SiMath::Vector bestRotor(rotor);
+
+ double oldVolume(0.0);
+ double bestVolume(0.0);
+ unsigned int iterations(0);
+ unsigned int sameCount(0);
+ unsigned int mapIndex(0);
+ // unsigned int DBSize = _gDb->pharmacophores.size();
+
+ while (iterations < _maxIter) {
+ // reset volume
+ atomOverlap = 0.0;
+ pharmOverlap = 0.0;
+
+ ++iterations;
+
+ // temperature of the simulated annealing step
+ double T = sqrt((1.0 + iterations) / dTemperature);
+
+ // create atom-atom overlaps
+ for (unsigned int i = 0; i < _rAtoms; ++i) {
+ for (unsigned int j = 0; j < _dAtoms; ++j) {
+ mapIndex = (i * _dGauss) + j;
+
+ if ((matIter = _matrixMap.find(mapIndex)) == _matrixMap.end()) {
+ Aij = _updateMatrixMap(_gRef->gaussians[i], _gDb->gaussians[j]);
+ _matrixMap[mapIndex] = Aij;
+ } else {
+ Aij = matIter->second;
+ }
+
+ // rotor product
+ Aq[0] = Aij[0] * rotor[0] + Aij[1] * rotor[1] + Aij[2] * rotor[2] +
+ Aij[3] * rotor[3];
+ Aq[1] = Aij[4] * rotor[0] + Aij[5] * rotor[1] + Aij[6] * rotor[2] +
+ Aij[7] * rotor[3];
+ Aq[2] = Aij[8] * rotor[0] + Aij[9] * rotor[1] + Aij[10] * rotor[2] +
+ Aij[11] * rotor[3];
+ Aq[3] = Aij[12] * rotor[0] + Aij[13] * rotor[1] + Aij[14] * rotor[2] +
+ Aij[15] * rotor[3];
+
+ qAq = rotor[0] * Aq[0] + rotor[1] * Aq[1] + rotor[2] * Aq[2] +
+ rotor[3] * Aq[3];
+
+ // compute overlap volume
+ Vij = Aij[16] * exp(-qAq);
+
+ // check if overlap is sufficient enough, should be more than 0.1 for
+ // atom - atom overlap
+ if (Vij /
+ (_gRef->gaussians[i].volume + _gDb->gaussians[j].volume - Vij) <
+ EPS) {
+ continue;
+ }
+
+ // add to overlap volume
+ atomOverlap += Vij;
+
+ // loop over child nodes and add to queue
+ d1 = _gRef->childOverlaps[i];
+ d2 = _gDb->childOverlaps[j];
+
+ // first add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+ // second add (child(i),j)
+ if (d1 != nullptr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ while (!processQueue.empty()) {
+ // get next element from queue
+ std::pair nextPair = processQueue.front();
+ processQueue.pop();
+
+ unsigned int i = nextPair.first;
+ unsigned int j = nextPair.second;
+
+ // check cache
+ mapIndex = (i * _dGauss) + j;
+ if ((matIter = _matrixMap.find(mapIndex)) == _matrixMap.end()) {
+ Aij = _updateMatrixMap(_gRef->gaussians[i], _gDb->gaussians[j]);
+ _matrixMap[mapIndex] = Aij;
+ } else {
+ Aij = matIter->second;
+ }
+
+ // rotor product
+ Aq[0] = Aij[0] * rotor[0] + Aij[1] * rotor[1] + Aij[2] * rotor[2] +
+ Aij[3] * rotor[3];
+ Aq[1] = Aij[4] * rotor[0] + Aij[5] * rotor[1] + Aij[6] * rotor[2] +
+ Aij[7] * rotor[3];
+ Aq[2] = Aij[8] * rotor[0] + Aij[9] * rotor[1] + Aij[10] * rotor[2] +
+ Aij[11] * rotor[3];
+ Aq[3] = Aij[12] * rotor[0] + Aij[13] * rotor[1] + Aij[14] * rotor[2] +
+ Aij[15] * rotor[3];
+
+ qAq = rotor[0] * Aq[0] + rotor[1] * Aq[1] + rotor[2] * Aq[2] +
+ rotor[3] * Aq[3];
+
+ // compute overlap volume
+ Vij = Aij[16] * exp(-qAq);
+
+ // check if overlap is sufficient enough
+ if (fabs(Vij) / (_gRef->gaussians[i].volume + _gDb->gaussians[j].volume -
+ fabs(Vij)) <
+ EPS) {
+ continue;
+ }
+
+ // even number of overlap atoms => addition to volume
+ atomOverlap += Vij;
+
+ // loop over child nodes and add to queue
+ d1 = _gRef->childOverlaps[i];
+ d2 = _gDb->childOverlaps[j];
+ if (d1 != nullptr && _gRef->gaussians[i].nbr > _gDb->gaussians[j].nbr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ } else {
+ // first add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+ if (d1 != nullptr &&
+ _gDb->gaussians[j].nbr - _gRef->gaussians[i].nbr < 2) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ // check if the new volume is better than the previously found one
+ double overlapVol = atomOverlap;
+ if (overlapVol < oldVolume) {
+ double D = exp(-sqrt(oldVolume - overlapVol)) / T;
+ if (SiMath::randD(0, 1) < D) {
+ oldRotor = rotor;
+ oldVolume = overlapVol;
+ sameCount = 0;
+ } else {
+ ++sameCount;
+ if (sameCount == 30) {
+ iterations = _maxIter;
+ }
+ }
+ } else {
+ // store latest volume found
+ oldVolume = overlapVol;
+ oldRotor = rotor;
+
+ // update best found so far
+ bestRotor = rotor;
+ bestVolume = overlapVol;
+ sameCount = 0;
+
+ // check if it is better than the best solution found so far
+ if (overlapVol > res.overlap) {
+ res.overlap = atomOverlap;
+ res.rotor = rotor;
+ if ((res.overlap / (_gRef->overlap + _gDb->overlap - res.overlap)) >
+ 0.99) {
+ break;
+ }
+ }
+ }
+
+ // make random permutation
+ // double range = 0.05;
+ double range = 0.1 / T;
+ rotor[0] = oldRotor[0] + SiMath::randD(-range, range);
+ rotor[1] = oldRotor[1] + SiMath::randD(-range, range);
+ rotor[2] = oldRotor[2] + SiMath::randD(-range, range);
+ rotor[3] = oldRotor[3] + SiMath::randD(-range, range);
+
+ // normalise rotor such that it has unit norm
+ double nr = sqrt(rotor[0] * rotor[0] + rotor[1] * rotor[1] +
+ rotor[2] * rotor[2] + rotor[3] * rotor[3]);
+ rotor[0] /= nr;
+ rotor[1] /= nr;
+ rotor[2] /= nr;
+ rotor[3] /= nr;
+
+ } // end of endless while loop
+
+ return res;
+}
+
+void ShapeAlignment::setMaxIterations(unsigned int i) {
+ _maxIter = i;
+ return;
+}
+
+double *ShapeAlignment::_updateMatrixMap(const AtomGaussian &a,
+ const AtomGaussian &b) {
+ auto *A = new double[17];
+
+ // variables to store sum and difference of components
+ double dx = (a.center.x - b.center.x);
+ double dx2 = dx * dx;
+ double dy = (a.center.y - b.center.y);
+ double dy2 = dy * dy;
+ double dz = (a.center.z - b.center.z);
+ double dz2 = dz * dz;
+ double sx = (a.center.x + b.center.x);
+ double sx2 = sx * sx;
+ double sy = (a.center.y + b.center.y);
+ double sy2 = sy * sy;
+ double sz = (a.center.z + b.center.z);
+ double sz2 = sz * sz;
+
+ // update overlap matrix
+ double C = a.alpha * b.alpha / (a.alpha + b.alpha);
+ A[0] = C * (dx2 + dy2 + dz2);
+ A[1] = C * (dy * sz - dz * sy);
+ A[2] = C * (dz * sx - dx * sz);
+ A[3] = C * (dx * sy - dy * sx);
+ A[4] = A[1];
+ A[5] = C * (dx2 + sy2 + sz2);
+ A[6] = C * (dx * dy - sx * sy);
+ A[7] = C * (dx * dz - sx * sz);
+ A[8] = A[2];
+ A[9] = A[6];
+ A[10] = C * (sx2 + dy2 + sz2);
+ A[11] = C * (dy * dz - sy * sz);
+ A[12] = A[3];
+ A[13] = A[7];
+ A[14] = A[11];
+ A[15] = C * (sx2 + sy2 + dz2);
+
+ // last elements holds overlap scaling constant
+ // even number of overlap atoms => addition to volume
+ // odd number => substraction
+ if ((a.nbr + b.nbr) % 2 == 0) {
+ A[16] = a.C * b.C * pow(PI / (a.alpha + b.alpha), 1.5);
+ } else {
+ A[16] = -a.C * b.C * pow(PI / (a.alpha + b.alpha), 1.5);
+ }
+
+ return A;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/alignmentInfo.cpp",".cpp","1794","41","/*******************************************************************************
+alignmentInfo.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+AlignmentInfo::AlignmentInfo() : overlap(0.0), rotor(4, 0.0) {
+ rotor[0] = 1.0;
+}
+
+AlignmentInfo::~AlignmentInfo() = default;
+","C++"
+"In Silico","rdkit/shape-it","src/printHeader.cpp",".cpp","3615","89","/*******************************************************************************
+printHeader.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+#ifndef USE_RDKIT
+// OpenBabel
+#include
+#else
+#include
+#endif
+
+void printHeader() {
+ std::cerr << ""+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++""
+ ""++++++++++++""
+ << std::endl;
+ std::cerr << "" Shape-it v"" << SHAPEIT_VERSION << ""."" << SHAPEIT_RELEASE
+ << ""."" << SHAPEIT_SUBRELEASE << "" | "";
+ std::cerr << __DATE__ "" "" << __TIME__ << std::endl;
+ std::cerr << std::endl;
+ std::cerr << "" -> GCC: "" << __VERSION__ << std::endl;
+#ifndef USE_RDKIT
+ std::cerr << "" -> OpenBabel: "" << BABEL_VERSION << std::endl;
+#else
+ std::cerr << "" -> RDKit: "" << RDKit::rdkitVersion << std::endl;
+#endif
+ std::cerr << std::endl;
+ std::cerr
+ << R""DOC( Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+ and the Shape-it contributors
+
+ Shape-it is free software: you can redistribute it and/or modify
+ it under the terms of the MIT license.
+
+ Shape-it 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
+ MIT License for more details.
+)DOC"" << std::endl;
+#ifndef USE_RDKIT
+ std::cerr << "" Shape-it is linked against OpenBabel version 2."" << std::endl;
+ std::cerr
+ << "" OpenBabel is free software; you can redistribute it and/or modify""
+ << std::endl;
+ std::cerr << "" it under the terms of the GNU General Public License as ""
+ ""published by""
+ << std::endl;
+ std::cerr << "" the Free Software Foundation version 2 of the License.""
+ << std::endl;
+#else
+ std::cerr << "" Shape-it is linked against the RDKit (https://www.rdkit.org).""
+ << std::endl;
+#endif
+ std::cerr << ""+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++""
+ ""++++++++++++""
+ << std::endl;
+ std::cerr << std::endl;
+ return;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/mainErr.cpp",".cpp","1761","40","/*******************************************************************************
+mainErr.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+void mainErr(const std::string &msg) {
+ std::cerr << ""**MainError** "" << msg << std::endl;
+ exit(1);
+}
+","C++"
+"In Silico","rdkit/shape-it","src/gaussianVolume.cpp",".cpp","19217","629","/*******************************************************************************
+gaussianVolume.cpp - Shape-it
+
+Copyright 2012-2021 by Silicos-it, a division of Imacosi BVBA, Hans De Winter,
+and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Shape-it can be linked against either OpenBabel version 3 or the RDKit.
+
+ OpenBabel 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.
+
+***********************************************************************/
+
+#include
+
+#ifndef USE_RDKIT
+// OpenBabel
+#include
+#include
+#include
+#include
+#else
+#include
+#include
+#endif
+
+// OpenBabel
+
+GaussianVolume::GaussianVolume()
+ : volume(0.0), overlap(0.0), centroid(0.0, 0.0, 0.0), rotation(3, 3, 0.0),
+ gaussians(), childOverlaps(), levels() {}
+
+GaussianVolume::~GaussianVolume() = default;
+
+double GAlpha(unsigned int an) {
+ switch (an) {
+ case 1: ///< H
+ return 1.679158285;
+ break;
+ case 3: ///< Li
+ return 0.729980658;
+ break;
+ case 5: ///< B
+ return 0.604496983;
+ break;
+ case 6: ///< C
+ return 0.836674025;
+ break;
+ case 7: ///< N
+ return 1.006446589;
+ break;
+ case 8: ///< O
+ return 1.046566798;
+ break;
+ case 9: ///< F
+ return 1.118972618;
+ break;
+ case 11: ///< Na
+ return 0.469247983;
+ break;
+ case 12: ///< Mg
+ return 0.807908026;
+ break;
+ case 14: ///< Si
+ return 0.548296583;
+ break;
+ case 15: ///< P
+ return 0.746292571;
+ break;
+ case 16: ///< S
+ return 0.746292571;
+ break;
+ case 17: ///< Cl
+ return 0.789547080;
+ break;
+ case 19: ///< K
+ return 0.319733941;
+ break;
+ case 20: ///< Ca
+ return 0.604496983;
+ break;
+ case 26: ///< Fe
+ return 1.998337133;
+ break;
+ case 29: ///< Cu
+ return 1.233667312;
+ break;
+ case 30: ///< Zn
+ return 1.251481772;
+ break;
+ case 35: ///< Br
+ return 0.706497569;
+ break;
+ case 53: ///< I
+ return 0.616770720;
+ break;
+ default: ///< *
+ return 1.074661303;
+ }
+ return 1.074661303;
+};
+
+namespace {
+#ifndef USE_RDKIT
+
+unsigned int initFromMol(const Molecule &mol, GaussianVolume &gv) {
+ // Prepare the vector to store the atom and overlap volumes;
+ unsigned int N = 0;
+ for (unsigned int i = 1; i <= mol.NumAtoms(); ++i) {
+ const OpenBabel::OBAtom *a = mol.GetAtom(i);
+ if (a->GetAtomicNum() == 1) {
+ continue;
+ } else {
+ ++N;
+ }
+ }
+ gv.gaussians.resize(N);
+ gv.childOverlaps.resize(N);
+ gv.levels.push_back(N); // first level
+ gv.volume = 0.0;
+ gv.centroid.x = 0;
+ gv.centroid.y = 0;
+ gv.centroid.z = 0;
+ int atomIndex = 0; // keeps track of the atoms processed so far
+ int vecIndex = N; // keeps track of the last element added to the vectors
+ for (unsigned int i = 1; i <= mol.NumAtoms(); ++i) {
+ const OpenBabel::OBAtom *a = mol.GetAtom(i);
+ // Skip hydrogens
+ if (a->GetAtomicNum() == 1) {
+ continue;
+ }
+
+ // First atom append self to the list
+ // Store it at [index]
+ gv.gaussians[atomIndex].center.x = a->GetX();
+ gv.gaussians[atomIndex].center.y = a->GetY();
+ gv.gaussians[atomIndex].center.z = a->GetZ();
+ gv.gaussians[atomIndex].alpha = GAlpha(a->GetAtomicNum());
+ gv.gaussians[atomIndex].C = GCI;
+ double radius = OpenBabel::OBElements::GetVdwRad(a->GetAtomicNum());
+ gv.gaussians[atomIndex].volume =
+ (4.0 * PI / 3.0) * radius * radius * radius;
+ ++atomIndex;
+ }
+ return N;
+}
+#else
+unsigned int initFromMol(const Molecule &mol, GaussianVolume &gv) {
+ // Prepare the vector to store the atom and overlap volumes;
+ unsigned int N = 0;
+ for (const auto a : mol.atoms()) {
+ if (a->getAtomicNum() != 1) {
+ ++N;
+ }
+ }
+ gv.gaussians.resize(N);
+ gv.childOverlaps.resize(N);
+ gv.levels.push_back(N); // first level
+ gv.volume = 0.0;
+ gv.centroid.x = 0;
+ gv.centroid.y = 0;
+ gv.centroid.z = 0;
+ int atomIndex = 0; // keeps track of the atoms processed so far
+ int vecIndex = N; // keeps track of the last element added to the vectors
+ const auto &conf = mol.getConformer();
+ for (const auto a : mol.atoms()) {
+ // Skip hydrogens
+ if (a->getAtomicNum() == 1) {
+ continue;
+ }
+
+ // First atom append self to the list
+ // Store it at [index]
+ const auto &p = conf.getAtomPos(a->getIdx());
+ gv.gaussians[atomIndex].center.x = p.x;
+ gv.gaussians[atomIndex].center.y = p.y;
+ gv.gaussians[atomIndex].center.z = p.z;
+ gv.gaussians[atomIndex].alpha = GAlpha(a->getAtomicNum());
+ gv.gaussians[atomIndex].C = GCI;
+ // double radius = et.GetVdwRad(a->GetAtomicNum());
+ double radius =
+ RDKit::PeriodicTable::getTable()->getRvdw(a->getAtomicNum());
+ gv.gaussians[atomIndex].volume =
+ (4.0 * PI / 3.0) * radius * radius * radius;
+ ++atomIndex;
+ }
+ return N;
+}
+
+#endif
+
+} // namespace
+
+void listAtomVolumes(const Molecule &mol, GaussianVolume &gv) {
+ // Prepare the vector to store the atom and overlap volumes;
+ unsigned int N = initFromMol(mol, gv);
+
+ // Vector to keep track of parents of an overlap
+ std::vector> parents(N);
+
+ // Create a vector to keep track of the overlaps
+ // Overlaps are stored as sets
+ std::vector *> overlaps(N);
+
+ // Start by iterating over the single atoms and build map of overlaps
+ int vecIndex = N; // keeps track of the last element added to the vectors
+ for (unsigned int atomIndex = 0; atomIndex < N; ++atomIndex) {
+ // Add empty child overlaps
+ auto *vec = new std::vector();
+ gv.childOverlaps[atomIndex] = vec;
+
+ // Update volume and centroid
+ gv.volume += gv.gaussians[atomIndex].volume;
+ gv.centroid.x +=
+ gv.gaussians[atomIndex].volume * gv.gaussians[atomIndex].center.x;
+ gv.centroid.y +=
+ gv.gaussians[atomIndex].volume * gv.gaussians[atomIndex].center.y;
+ gv.centroid.z +=
+ gv.gaussians[atomIndex].volume * gv.gaussians[atomIndex].center.z;
+
+ // Add new empty set of possible overlaps
+ auto *tmp = new std::set();
+ overlaps[atomIndex] = tmp;
+
+ // Loop over the current list of processed atoms and add overlaps
+ for (int i = 0; i < atomIndex; ++i) {
+ // Create overlap gaussian
+ AtomGaussian ga =
+ atomIntersection(gv.gaussians[i], gv.gaussians[atomIndex]);
+
+ // Check if the atom-atom overlap volume is large enough
+ if (ga.volume / (gv.gaussians[i].volume + gv.gaussians[atomIndex].volume -
+ ga.volume) <
+ EPS) {
+ continue;
+ }
+
+ // Add gaussian volume, and empty overlap set
+ gv.gaussians.push_back(ga);
+ auto *vec = new std::vector();
+ gv.childOverlaps.push_back(vec);
+
+ // Update local variables of parents and possible overlaps
+ parents.emplace_back(i, atomIndex);
+ auto *dummy = new std::set();
+ overlaps.push_back(dummy);
+
+ // Update volume and centroid (negative contribution of atom-atom overlap)
+ gv.volume -= ga.volume;
+ gv.centroid.x -= ga.volume * ga.center.x;
+ gv.centroid.y -= ga.volume * ga.center.y;
+ gv.centroid.z -= ga.volume * ga.center.z;
+
+ // Update overlap information of the parent atom
+ overlaps[i]->insert(atomIndex);
+ gv.childOverlaps[i]->push_back(vecIndex);
+
+ // Move to next index in vector
+ ++vecIndex;
+ }
+ }
+
+ // Position in list of gaussians where atom gaussians end
+ unsigned int startLevel = N;
+ unsigned int nextLevel = gv.gaussians.size();
+
+ // Update level information
+ gv.levels.push_back(nextLevel);
+
+ // Loop overall possible levels of overlaps from 2 to 6
+ for (unsigned int l = 2; l < LEVEL; ++l) {
+ // List of atom-atom overlaps is made => gv.gaussians[startLevel ..
+ // nextLevel-1]; Now update the overlap lists for each overlap in this level
+ // Create the next overlap Gaussian
+ // And add it to the vector of overlaps
+ for (unsigned int i = startLevel; i < nextLevel; ++i) {
+ // Parent indices
+ unsigned int a1 = parents[i].first;
+ unsigned int a2 = parents[i].second;
+
+ // Append volume to end of overlap vector
+ // Add new empty set
+ std::set *tmp = overlaps[i];
+ std::set_intersection(
+ overlaps[a1]->begin(), overlaps[a1]->end(), overlaps[a2]->begin(),
+ overlaps[a2]->end(),
+ std::insert_iterator>(*tmp, tmp->begin()));
+
+ // Check if the overlap list is empty
+ if (overlaps[i]->empty()) {
+ continue;
+ }
+
+ // Get the possible overlaps from the parent gaussians
+ // and create the new overlap volume
+ for (auto overlapIdx : *overlaps[i]) {
+ if (overlapIdx <= a2) {
+ continue;
+ }
+
+ // Create a new overlap gaussian
+ AtomGaussian ga =
+ atomIntersection(gv.gaussians[i], gv.gaussians[overlapIdx]);
+
+ // Check if the volume is large enough
+ if (ga.volume / (gv.gaussians[i].volume +
+ gv.gaussians[overlapIdx].volume - ga.volume) <
+ EPS) {
+ continue;
+ }
+
+ gv.gaussians.push_back(ga);
+ auto *vec = new std::vector();
+ gv.childOverlaps.push_back(vec);
+
+ // Update local variables
+ parents.emplace_back(i, overlapIdx);
+ auto *tmp = new std::set();
+ overlaps.push_back(tmp);
+
+ // Update volume, centroid and moments
+ // Overlaps consisting of an even number of atoms have a negative
+ // contribution
+ if ((ga.nbr % 2) == 0) {
+ // Update volume and centroid
+ gv.volume -= ga.volume;
+ gv.centroid.x -= ga.volume * ga.center.x;
+ gv.centroid.y -= ga.volume * ga.center.y;
+ gv.centroid.z -= ga.volume * ga.center.z;
+ } else {
+ // Update volume and centroid
+ gv.volume += ga.volume;
+ gv.centroid.x += ga.volume * ga.center.x;
+ gv.centroid.y += ga.volume * ga.center.y;
+ gv.centroid.z += ga.volume * ga.center.z;
+ }
+
+ // Update child list of the first
+ gv.childOverlaps[i]->push_back(vecIndex);
+
+ // Move to next index in vector
+ ++vecIndex;
+ }
+ }
+
+ // Update levels
+ startLevel = nextLevel;
+ nextLevel = gv.gaussians.size();
+
+ // Update level information
+ gv.levels.push_back(nextLevel);
+ }
+
+ // cleanup current set of computed overlaps
+ for (auto &overlap : overlaps) {
+ delete overlap;
+ overlap = nullptr;
+ }
+
+ parents.clear();
+
+ // Update self-overlap
+ gv.overlap = atomOverlap(gv, gv);
+
+ return;
+}
+
+void initOrientation(GaussianVolume &gv) {
+ double x(0.0), y(0.0), z(0.0);
+
+ // Scale centroid and moments with self volume
+ gv.centroid.x /= gv.volume;
+ gv.centroid.y /= gv.volume;
+ gv.centroid.z /= gv.volume;
+
+ // Compute moments of inertia from mass matrix
+ SiMath::Matrix mass(3, 3, 0.0);
+
+ // Loop over all gaussians
+ for (auto &g : gv.gaussians) {
+ // Translate to center
+ g.center.x -= gv.centroid.x;
+ g.center.y -= gv.centroid.y;
+ g.center.z -= gv.centroid.z;
+
+ x = g.center.x;
+ y = g.center.y;
+ z = g.center.z;
+
+ if ((g.nbr % 2) == 0) {
+ // Update upper triangle
+ mass[0][0] -= g.volume * x * x;
+ mass[0][1] -= g.volume * x * y;
+ mass[0][2] -= g.volume * x * z;
+ mass[1][1] -= g.volume * y * y;
+ mass[1][2] -= g.volume * y * z;
+ mass[2][2] -= g.volume * z * z;
+ } else {
+ // Update upper triangle
+ mass[0][0] += g.volume * x * x;
+ mass[0][1] += g.volume * x * y;
+ mass[0][2] += g.volume * x * z;
+ mass[1][1] += g.volume * y * y;
+ mass[1][2] += g.volume * y * z;
+ mass[2][2] += g.volume * z * z;
+ }
+ }
+
+ // Set lower triangle
+ mass[1][0] = mass[0][1];
+ mass[2][0] = mass[0][2];
+ mass[2][1] = mass[1][2];
+
+ // Normalize mass matrix
+ mass /= gv.volume;
+
+ // Compute SVD of the mass matrix
+ SiMath::SVD svd(mass, true, true);
+ gv.rotation = svd.getU();
+
+ double det = gv.rotation[0][0] * gv.rotation[1][1] * gv.rotation[2][2] +
+ gv.rotation[2][1] * gv.rotation[1][0] * gv.rotation[0][2] +
+ gv.rotation[0][1] * gv.rotation[1][2] * gv.rotation[2][0] -
+ gv.rotation[0][0] * gv.rotation[1][2] * gv.rotation[2][1] -
+ gv.rotation[1][1] * gv.rotation[2][0] * gv.rotation[0][2] -
+ gv.rotation[2][2] * gv.rotation[0][1] * gv.rotation[1][0];
+
+ // Check if it is a rotation matrix and not a mirroring
+ if (det < 0) {
+ // Switch sign of third column
+ gv.rotation[0][2] = -gv.rotation[0][2];
+ gv.rotation[1][2] = -gv.rotation[1][2];
+ gv.rotation[2][2] = -gv.rotation[2][2];
+ }
+
+ // Rotate all gaussians
+ for (auto &g : gv.gaussians) {
+ x = g.center.x;
+ y = g.center.y;
+ z = g.center.z;
+ g.center.x =
+ gv.rotation[0][0] * x + gv.rotation[1][0] * y + gv.rotation[2][0] * z;
+ g.center.y =
+ gv.rotation[0][1] * x + gv.rotation[1][1] * y + gv.rotation[2][1] * z;
+ g.center.z =
+ gv.rotation[0][2] * x + gv.rotation[1][2] * y + gv.rotation[2][2] * z;
+ }
+
+ return;
+}
+
+double atomOverlap(const GaussianVolume &gRef, const GaussianVolume &gDb) {
+ // Create a queue to hold the pairs to process
+ std::queue> processQueue;
+
+ // loop over the single atom volumes of both molecules and make the
+ // combinations
+ unsigned int N1(gRef.levels[0]);
+ unsigned int N2(gDb.levels[0]);
+
+ double Cij(0.0), Vij(0.0);
+
+ double dx(0.0), dy(0.0), dz(0.0);
+
+ std::vector *d1(nullptr), *d2(nullptr);
+ std::vector::iterator it1;
+
+ // Overlap volume
+ double overlapVol(0.0);
+
+ // First compute atom-atom overlaps
+ for (unsigned int i(0); i < N1; ++i) {
+ for (unsigned int j(0); j < N2; ++j) {
+ // Scaling constant
+ Cij = gRef.gaussians[i].alpha * gDb.gaussians[j].alpha /
+ (gRef.gaussians[i].alpha + gDb.gaussians[j].alpha);
+
+ // Variables to store sum and difference of components
+ dx = (gRef.gaussians[i].center.x - gDb.gaussians[j].center.x);
+ dx *= dx;
+ dy = (gRef.gaussians[i].center.y - gDb.gaussians[j].center.y);
+ dy *= dy;
+ dz = (gRef.gaussians[i].center.z - gDb.gaussians[j].center.z);
+ dz *= dz;
+
+ // Compute overlap volume
+ Vij = gRef.gaussians[i].C * gDb.gaussians[j].C *
+ pow(PI / (gRef.gaussians[i].alpha + gDb.gaussians[j].alpha), 1.5) *
+ exp(-Cij * (dx + dy + dz));
+
+ // Check if overlap is sufficient enough
+ if (Vij / (gRef.gaussians[i].volume + gDb.gaussians[j].volume - Vij) <
+ EPS) {
+ continue;
+ }
+
+ // Add to overlap volume
+ overlapVol += Vij;
+
+ // Loop over child nodes and add to queue
+ d1 = gRef.childOverlaps[i];
+ d2 = gDb.childOverlaps[j];
+
+ // First add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+ // Second add (child(i,j))
+ if (d1 != nullptr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ while (!processQueue.empty()) {
+ // Get next element from queue
+ std::pair nextPair = processQueue.front();
+ processQueue.pop();
+
+ unsigned int i = nextPair.first;
+ unsigned int j = nextPair.second;
+
+ // Scaling constant
+ Cij = gRef.gaussians[i].alpha * gDb.gaussians[j].alpha /
+ (gRef.gaussians[i].alpha + gDb.gaussians[j].alpha);
+
+ // Variables to store sum and difference of components
+ dx = (gRef.gaussians[i].center.x - gDb.gaussians[j].center.x);
+ dx *= dx;
+ dy = (gRef.gaussians[i].center.y - gDb.gaussians[j].center.y);
+ dy *= dy;
+ dz = (gRef.gaussians[i].center.z - gDb.gaussians[j].center.z);
+ dz *= dz;
+
+ // Compute overlap volume
+ Vij = gRef.gaussians[i].C * gDb.gaussians[j].C *
+ pow(PI / (gRef.gaussians[i].alpha + gDb.gaussians[j].alpha), 1.5) *
+ exp(-Cij * (dx + dy + dz));
+
+ // Check if overlap is sufficient enough
+ if (Vij / (gRef.gaussians[i].volume + gDb.gaussians[j].volume - Vij) <
+ EPS) {
+ continue;
+ }
+
+ // Even number of overlap atoms => addition to volume
+ // Odd number => substraction
+ if ((gRef.gaussians[i].nbr + gDb.gaussians[j].nbr) % 2 == 0) {
+ overlapVol += Vij;
+ } else {
+ overlapVol -= Vij;
+ }
+
+ // Loop over child nodes and add to queue
+ d1 = gRef.childOverlaps[i];
+ d2 = gDb.childOverlaps[j];
+ if (d1 != nullptr && gRef.gaussians[i].nbr > gDb.gaussians[j].nbr) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // Add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ } else {
+ // First add (i,child(j))
+ if (d2 != nullptr) {
+ for (it1 = d2->begin(); it1 != d2->end(); ++it1) {
+ processQueue.push(std::make_pair(i, *it1));
+ }
+ }
+ if (d1 != nullptr && gDb.gaussians[j].nbr - gRef.gaussians[i].nbr < 2) {
+ for (it1 = d1->begin(); it1 != d1->end(); ++it1) {
+ // add (child(i),j)
+ processQueue.push(std::make_pair(*it1, j));
+ }
+ }
+ }
+ }
+
+ return overlapVol;
+}
+
+double getScore(const std::string &id, double Voa, double Vra, double Vda) {
+ // set the score by which molecules are being compared
+ if (id == tanimoto) {
+ return Voa / (Vra + Vda - Voa);
+ } else if (id == tversky_ref) {
+ return Voa / (0.95 * Vra + 0.05 * Vda);
+ } else if (id == tversky_db) {
+ return Voa / (0.05 * Vra + 0.95 * Vda);
+ }
+
+ return 0.0;
+}
+
+void checkVolumes(const GaussianVolume &gRef, const GaussianVolume &gDb,
+ AlignmentInfo &res) {
+ if (res.overlap > gRef.overlap) {
+ res.overlap = gRef.overlap;
+ }
+ if (res.overlap > gDb.overlap) {
+ res.overlap = gDb.overlap;
+ }
+ return;
+}
+","C++"
+"In Silico","rdkit/shape-it","src/Wrap/cpyshapeit.cpp",".cpp","2143","54","/*******************************************************************************
+
+Copyright 2021 by Greg Landrum and the Shape-it contributors
+
+This file is part of Shape-it.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the ""Software""), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+***********************************************************************/
+
+#include
+
+#include ""alignLib.h""
+#include ""options.h""
+
+namespace python = boost::python;
+using namespace RDKit;
+
+namespace {
+double alignMol(const ROMol &ref, ROMol &probe, const std::string &whichScore,
+ double maxIter, double cutoff) {
+ auto sinfo = shapeit::alignMols(ref, probe, whichScore, maxIter, cutoff);
+ const Conformer &conf = sinfo.dbMol.getConformer();
+ probe.clearConformers();
+ probe.addConformer(new Conformer(conf));
+ return sinfo.score;
+}
+} // namespace
+
+void wrap_pyshapeit() {
+ python::def(""AlignMol"", &alignMol,
+ (python::arg(""ref""), python::arg(""probe""),
+ python::arg(""whichScore"") = tanimoto,
+ python::arg(""maxIter"") = 0., python::arg(""cutoff"") = 0.),
+ ""aligns probe to ref, probe is modified"");
+}
+
+BOOST_PYTHON_MODULE(cpyshapeit) { wrap_pyshapeit(); }
+","C++"
+"In Silico","rdkit/shape-it","pyshapeit/__init__.py",".py","61","4","# Copyright (C) 2021 Greg Landrum
+
+from .cpyshapeit import *
+","Python"
+"In Silico","rdkit/shape-it","pyshapeit/basic_test.py",".py","6504","176","#
+# Copyright 2021 by Greg Landrum and the Shape-it contributors
+#
+# This file is part of Shape-it.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the ""Software""), to deal in
+# the Software without restriction, including without limitation the rights to
+# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+# the Software, and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+from rdkit import Chem
+from rdkit.Chem import rdMolAlign
+import cpyshapeit
+
+import unittest
+
+
+class TestCase(unittest.TestCase):
+ def testMols(self):
+ ref = Chem.MolFromMolBlock('''3l5u_lig_ZEC
+ 3D
+ Structure written by MMmdl.
+ 20 21 0 0 1 0 999 V2000
+ 15.0500 -34.9220 -18.1430 O 0 0 0 0 0 0
+ 14.9110 -34.7040 -19.4790 C 0 0 0 0 0 0
+ 14.7350 -35.7750 -20.3500 C 0 0 0 0 0 0
+ 14.6060 -35.5430 -21.7160 C 0 0 0 0 0 0
+ 14.3620 -36.6080 -23.0370 S 0 0 0 0 0 0
+ 14.3210 -35.3400 -24.1850 C 0 0 0 0 0 0
+ 14.0030 -35.5290 -25.8940 S 0 0 0 0 0 0
+ 15.1750 -34.7990 -26.7570 N 0 0 0 0 0 0
+ 12.6760 -34.9070 -26.2170 O 0 0 0 0 0 0
+ 13.9630 -36.9930 -26.2180 O 0 0 0 0 0 0
+ 14.4970 -34.1790 -23.5590 N 0 0 0 0 0 0
+ 14.6510 -34.2470 -22.2350 C 0 0 0 0 0 0
+ 14.8270 -33.1870 -21.3480 C 0 0 0 0 0 0
+ 14.9560 -33.4070 -19.9800 C 0 0 0 0 0 0
+ 15.1610 -34.0820 -17.6920 H 0 0 0 0 0 0
+ 14.6990 -36.7830 -19.9650 H 0 0 0 0 0 0
+ 15.1440 -34.8130 -27.7660 H 0 0 0 0 0 0
+ 15.9340 -34.3310 -26.2820 H 0 0 0 0 0 0
+ 14.8640 -32.1730 -21.7190 H 0 0 0 0 0 0
+ 15.0910 -32.5720 -19.3090 H 0 0 0 0 0 0
+ 1 2 1 0 0 0
+ 1 15 1 0 0 0
+ 2 3 2 0 0 0
+ 2 14 1 0 0 0
+ 3 4 1 0 0 0
+ 3 16 1 0 0 0
+ 4 5 1 0 0 0
+ 4 12 2 0 0 0
+ 5 6 1 0 0 0
+ 6 7 1 0 0 0
+ 6 11 2 0 0 0
+ 7 8 1 0 0 0
+ 7 9 2 0 0 0
+ 7 10 2 0 0 0
+ 8 17 1 0 0 0
+ 8 18 1 0 0 0
+ 11 12 1 0 0 0
+ 12 13 1 0 0 0
+ 13 14 2 0 0 0
+ 13 19 1 0 0 0
+ 14 20 1 0 0 0
+M END''')
+ probe = Chem.MolFromMolBlock('''3hof_lig_DHC
+ 3D
+ Structure written by MMmdl.
+ 20 20 0 0 1 0 999 V2000
+ 14.6290 -34.5170 -18.4190 C 0 0 0 0 0 0
+ 15.6070 -34.6620 -17.5400 O 0 0 0 0 0 0
+ 14.9220 -34.5200 -19.8370 C 0 0 0 0 0 0
+ 14.7370 -35.7220 -20.3520 C 0 0 0 0 0 0
+ 14.9680 -35.9740 -21.7740 C 0 0 0 0 0 0
+ 14.8780 -34.9380 -22.6930 C 0 0 0 0 0 0
+ 15.1020 -35.2380 -24.0360 C 0 0 0 0 0 0
+ 15.4390 -36.6310 -24.4550 C 0 0 0 0 0 0
+ 15.5160 -37.6070 -23.4830 C 0 0 0 0 0 0
+ 15.2760 -37.2740 -22.1560 C 0 0 0 0 0 0
+ 15.6830 -36.9520 -25.7670 O 0 0 0 0 0 0
+ 15.0160 -34.2570 -24.9550 O 0 0 0 0 0 0
+ 13.4860 -34.4200 -18.0300 O 0 5 0 0 0 0
+ 15.2430 -33.6100 -20.3240 H 0 0 0 0 0 0
+ 14.4110 -36.5360 -19.7210 H 0 0 0 0 0 0
+ 14.6410 -33.9380 -22.3590 H 0 0 0 0 0 0
+ 15.7620 -38.6220 -23.7550 H 0 0 0 0 0 0
+ 15.3330 -38.0570 -21.4140 H 0 0 0 0 0 0
+ 15.1950 -34.6170 -25.8260 H 0 0 0 0 0 0
+ 15.8806 -37.8889 -25.8363 H 0 0 0 0 0 0
+ 1 2 2 0 0 0
+ 1 3 1 0 0 0
+ 1 13 1 0 0 0
+ 3 4 2 0 0 0
+ 3 14 1 0 0 0
+ 4 5 1 0 0 0
+ 4 15 1 0 0 0
+ 5 6 2 0 0 0
+ 5 10 1 0 0 0
+ 6 7 1 0 0 0
+ 6 16 1 0 0 0
+ 7 8 2 0 0 0
+ 7 12 1 0 0 0
+ 8 9 1 0 0 0
+ 8 11 1 0 0 0
+ 9 10 2 0 0 0
+ 9 17 1 0 0 0
+ 10 18 1 0 0 0
+ 11 20 1 0 0 0
+ 12 19 1 0 0 0
+M CHG 1 13 -1
+M END''')
+ tmp = Chem.Mol(probe)
+ score = cpyshapeit.AlignMol(ref, tmp)
+ self.assertAlmostEqual(score, 0.647, 3)
+ expected = Chem.MolFromMolBlock('''3hof_lig_DHC
+ RDKit 3D
+
+ 13 13 0 0 1 0 0 0 0 0999 V2000
+ 13.8351 -36.1391 -27.1202 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 12.7314 -36.7492 -27.5199 O 0 0 0 0 0 0 0 0 0 0 0 0
+ 13.8607 -35.4455 -25.8495 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.3613 -36.2184 -24.9028 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.4913 -35.7352 -23.5285 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.5939 -34.3755 -23.2705 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.7220 -33.9730 -21.9418 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.7341 -34.9830 -20.8422 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.6219 -36.3168 -21.1765 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.5069 -36.6821 -22.5117 C 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.8399 -34.6151 -19.5241 O 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.8305 -32.6610 -21.6569 O 0 0 0 0 0 0 0 0 0 0 0 0
+ 14.8316 -36.1976 -27.8063 O 0 0 0 0 0 0 0 0 0 0 0 0
+ 1 2 2 0
+ 1 3 1 0
+ 1 13 1 0
+ 3 4 2 0
+ 4 5 1 0
+ 5 6 2 0
+ 5 10 1 0
+ 6 7 1 0
+ 7 8 2 0
+ 7 12 1 0
+ 8 9 1 0
+ 8 11 1 0
+ 9 10 2 0
+M CHG 1 13 -1
+M END
+''')
+ ssd = 0.0
+ probeConf = probe.GetConformer()
+ expectedConf = expected.GetConformer()
+ for i in range(probeConf.GetNumAtoms()):
+ delt = probeConf.GetAtomPosition(i) - expectedConf.GetAtomPosition(
+ i)
+ ssd += delt.LengthSq()
+ self.assertGreater(ssd, 100)
+ ssd = 0.0
+ probeConf = tmp.GetConformer()
+ expectedConf = expected.GetConformer()
+ for i in range(probeConf.GetNumAtoms()):
+ delt = probeConf.GetAtomPosition(i) - expectedConf.GetAtomPosition(
+ i)
+ ssd += delt.LengthSq()
+ self.assertAlmostEqual(ssd, 0, 3)
+","Python"
+"In Silico","gear-genomics/silica","server/server.py",".py","9156","194","#! /usr/bin/env python
+
+import os
+import errno
+import uuid
+import re
+import subprocess
+import argparse
+import json
+import gzip
+from subprocess import call
+from flask import Flask, send_file, flash, send_from_directory, request, redirect, url_for, jsonify
+from flask_cors import CORS
+from werkzeug.utils import secure_filename
+
+app = Flask(__name__)
+CORS(app)
+SILICAWS = os.path.dirname(os.path.abspath(__file__))
+
+app.config['SILICA'] = os.path.join(SILICAWS, "".."")
+app.config['UPLOAD_FOLDER'] = os.path.join(app.config['SILICA'], ""data"")
+app.config['MAX_CONTENT_LENGTH'] = 8 * 1024 * 1024 #maximum of 8MB
+
+
+def allowed_file(filename):
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in set(['fasta', 'fa', 'json', 'csv', 'txt'])
+
+
+uuid_re = re.compile(r'(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})-{0,1}([ap]{0,1})([cj]{0,1})$')
+def is_valid_uuid(s):
+ return uuid_re.match(s) is not None
+
+
+@app.route('/api/v1/upload', methods=['POST'])
+def generate():
+ uuidstr = str(uuid.uuid4())
+
+ # Get subfolder
+ sf = os.path.join(app.config['UPLOAD_FOLDER'], uuidstr[0:2])
+ if not os.path.exists(sf):
+ os.makedirs(sf)
+
+ # Fasta file
+ primerData = request.form['fastaText']
+ primerData = primerData.replace('\r\n','\n')
+ if primerData == '':
+ return jsonify(errors = [{""title"": ""Please provide a set of primers!""}]), 400
+ ffaname = os.path.join(sf, ""silica_"" + uuidstr + ""_fasta.fa"")
+ with open(ffaname, ""w"") as primFile:
+ primFile.write(primerData)
+
+ # Genome
+ val = request.form.get(""submit"", ""None provided"")
+ genome = request.form['genome']
+ if genome == '':
+ return jsonify(errors = [{""title"": ""Please select a genome!""}]), 400
+ genome = os.path.join(app.config['SILICA'], ""fm"", genome)
+
+ # Run silica
+ outfile = os.path.join(sf, ""silica_"" + uuidstr + "".json.gz"")
+ paramfile = os.path.join(sf, ""silica_"" + uuidstr + ""_parameter.txt"")
+ logfile = os.path.join(sf, ""silica_"" + uuidstr + "".log"")
+ errfile = os.path.join(sf, ""silica_"" + uuidstr + "".err"")
+ with open(logfile, ""w"") as log:
+ with open(errfile, ""w"") as err:
+ with open(paramfile, ""w"") as param:
+ param.write(""genome="" + genome + '\n')
+ setAmpSize = onlyInt(request.form['setAmpSize'])
+ param.write(""maxProdSize="" + setAmpSize + '\n')
+ setTmCutoff = onlyFloat(request.form['setTmCutoff'])
+ param.write(""cutTemp="" + setTmCutoff + '\n')
+ if float(setTmCutoff) < 30.0:
+ return jsonify(errors = [{""title"": ""Mimnimal Primer Tm must be >= 30°C""}]), 400
+ setKmer = onlyInt(request.form['setKmer'])
+ param.write(""kmer="" + setKmer + '\n')
+ if int(setKmer) < 15:
+ return jsonify(errors = [{""title"": ""Number of bp Used to Seach for Matches must be > 14 bp""}]), 400
+ setEDis = onlyInt(request.form['setDist'])
+ param.write(""distance="" + setEDis + '\n')
+ if int(setEDis) != 0 and int(setEDis) != 1:
+ return jsonify(errors = [{""title"": ""Maximal Allowed Number of Mutations must be 0 or 1""}]), 400
+ setCutoffPen = onlyFloat(request.form['setCutoffPen'])
+ param.write(""cutoffPenalty="" + setCutoffPen + '\n')
+ if float(setCutoffPen) < 0.0 and int(setCutoffPen) != -1:
+ return jsonify(errors = [{""title"": ""Keep Only PCR Products with Penalty Below must be > 0.0 or -1""}]), 400
+ setPenTmDiff = onlyFloat(request.form['setPenTmDiff'])
+ param.write(""penaltyTmDiff="" + setPenTmDiff + '\n')
+ if float(setPenTmDiff) < 0.0:
+ return jsonify(errors = [{""title"": ""Penalty Factor for Single Primer Tm Mismatch must be >= 0.0""}]), 400
+ setPenTmMismatch = onlyFloat(request.form['setPenTmMismatch'])
+ param.write(""penaltyTmMismatch="" + setPenTmMismatch + '\n')
+ if float(setPenTmMismatch) < 0.0:
+ return jsonify(errors = [{""title"": ""Penalty Factor for Tm Mismatch of Primers in a Pair must be >= 0.0""}]), 400
+ setPenLength = onlyFloat(request.form['setPenLength'])
+ param.write(""penaltyLength="" + setPenLength + '\n')
+ if float(setPenLength) < 0.0:
+ return jsonify(errors = [{""title"": ""Penalty Factor for PCR Product Length must be >= 0.0""}]), 400
+ setCtmMv = onlyFloat(request.form['setCtmMv'])
+ param.write(""monovalent="" + setCtmMv + '\n')
+ if float(setCtmMv) < 0.0:
+ return jsonify(errors = [{""title"": ""Concentration of Monovalent Ions must be >= 0.0 mMol""}]), 400
+ setCtmDv = onlyFloat(request.form['setCtmDv'])
+ param.write(""divalent="" + setCtmDv + '\n')
+ if float(setCtmDv) < 0.0:
+ return jsonify(errors = [{""title"": ""Concentration of Divalent Ions must be >= 0.0 mMol""}]), 400
+ setCtmDNA = onlyFloat(request.form['setCtmDNA'])
+ param.write(""dna="" + setCtmDNA + '\n')
+ if float(setCtmDNA) < 0.1:
+ return jsonify(errors = [{""title"": ""Concentration of Annealing(!) Oligos must be >= 0.1 nMol""}]), 400
+ setCtmDNTP = onlyFloat(request.form['setCtmDNTP'])
+ param.write(""dntp="" + setCtmDNTP + '\n')
+ if float(setCtmDNTP) < 0.0:
+ return jsonify(errors = [{""title"": ""Concentration of the Sum of All dNTPs must be >= 0.0 mMol""}]), 400
+
+ try:
+ return_code = call(['dicey', 'search', '-g', genome, '-o', outfile, '-i', os.path.join(SILICAWS, ""../primer3_config/""),
+ '--maxProdSize', setAmpSize, '--cutTemp', setTmCutoff,
+ '--kmer', setKmer, '--distance', setEDis,
+ '--cutoffPenalty', setCutoffPen, '--penaltyTmDiff', setPenTmDiff,
+ '--penaltyTmMismatch', setPenTmMismatch, '--penaltyLength', setPenLength,
+ '--monovalent', setCtmMv, '--divalent', setCtmDv,
+ '--dna', setCtmDNA, '--dntp', setCtmDNTP,
+ ffaname], stdout=log, stderr=err)
+ except OSError as e:
+ if e.errno == errno.ENOENT:
+ return jsonify(errors = [{""title"": ""Binary dicey not found!""}]), 400
+ else:
+ return jsonify(errors = [{""title"": ""OSError "" + str(e.errno) + "" running binary dicey!""}]), 400
+ result = gzip.open(outfile).read()
+ if result is None:
+ datajs = []
+ datajs[""errors""] = []
+ else:
+ datajs = json.loads(result)
+ datajs['uuid'] = uuidstr
+ with open(errfile, ""r"") as err:
+ errInfo = "": "" + err.read()
+ if len(errInfo) > 3 or return_code != 0:
+ if len(errInfo) > 3:
+ datajs[""errors""] = [{""title"": ""Error in running silica"" + errInfo}] + datajs[""errors""]
+ if return_code != 0:
+ datajs[""errors""] = [{""title"": ""Run Error - Dicey did not return 0""}] + datajs[""errors""]
+ return jsonify(datajs), 400
+ return jsonify(datajs), 200
+
+
+@app.route('/api/v1/results/', methods = ['GET', 'POST'])
+def results(uuid):
+ if is_valid_uuid(uuid):
+ sf = os.path.join(app.config['UPLOAD_FOLDER'], uuid[0:2])
+ if os.path.exists(sf):
+ sjsfilename = ""silica_"" + uuid + "".json.gz""
+ if os.path.isfile(os.path.join(sf, sjsfilename)):
+ result = gzip.open(os.path.join(sf, sjsfilename)).read()
+ if result is None:
+ datajs = []
+ datajs[""errors""] = []
+ else:
+ datajs = json.loads(result)
+ datajs['uuid'] = uuid
+ with open(os.path.join(sf, ""silica_"" + uuid + "".err""), ""r"") as err:
+ errInfo = "": "" + err.read()
+ if len(errInfo) > 3:
+ datajs[""errors""] = [{""title"": ""Error in running silica"" + errInfo}] + datajs[""errors""]
+ return jsonify(datajs), 400
+ return jsonify(datajs), 200
+ return jsonify(errors = [{""title"": ""Link outdated or invalid!""}]), 400
+
+
+@app.route('/api/v1/genomeindex', methods=['POST'])
+def genomeind():
+ return send_from_directory(os.path.join(SILICAWS, ""../fm""),""genomeindexindex.json""), 200
+
+
+@app.route('/api/v1/health', methods=['GET'])
+def health():
+ return jsonify(status=""OK"")
+
+
+def onlyFloat(txt):
+ onlyNumbDC = re.compile(r'[^0-9,.\-]')
+ txt = onlyNumbDC.sub( '', txt)
+ txt = txt.replace(',', '.')
+ return txt
+
+
+def onlyInt(txt):
+ onlyNumb = re.compile(r'[^0-9\-]')
+ return onlyNumb.sub( '', txt)
+
+
+if __name__ == '__main__':
+ app.run(host = '0.0.0.0', port=3300, debug = True, threaded=True)
+","Python"
+"In Silico","baoilleach/pharao","include/mainWar.h",".h","962","42","/*******************************************************************************
+mainWar.h - Pharao
+
+Copyright (C) 2005-2010 by Silicos NV
+
+This file is part of the Open Babel project.
+For more information, see
+
+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.
+*******************************************************************************/
+
+
+
+#ifndef __SILICOS_PHARAO_MAINWAR_H__
+#define __SILICOS_PHARAO_MAINWAR_H__
+
+
+
+
+// General
+#include
+#include
+
+// OpenBabel
+
+// Pharao
+
+
+
+void mainWar(const std::string&);
+
+
+
+#endif
+","Unknown"
+"In Silico","baoilleach/pharao","include/pharMerger.h",".h","1158","55","/*******************************************************************************
+pharMerger.h - Pharao
+
+Copyright (C) 2005-2010 by Silicos NV
+
+This file is part of the Open Babel project.
+For more information, see
+
+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.
+*******************************************************************************/
+
+
+
+#ifndef __SILICOS_PHARAO_PHARMERGER_H__
+#define __SILICOS_PHARAO_PHARMERGER_H__
+
+
+
+// General
+#include
+#include
+
+// OpenBabel
+
+// Pharao
+#include ""pharmacophore.h""
+#include ""utilities.h""
+
+
+
+class PharMerger
+{
+ private:
+
+ double _deltaSigma;
+ double _threshold;
+
+ public:
+
+ PharMerger();
+
+ void merge(Pharmacophore& phar);
+ };
+
+
+
+#endif
+","Unknown"
+"In Silico","baoilleach/pharao","include/chargeFuncCalc.h",".h","1020","41","/*******************************************************************************
+chargeFuncCalc.h - Pharao
+
+Copyright (C) 2005-2010 by Silicos NV
+
+This file is part of the Open Babel project.
+For more information, see
+
+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.
+*******************************************************************************/
+
+
+
+#ifndef __SILICOS_PHARAO_CHARGEFUNCCALC_H__
+#define __SILICOS_PHARAO_CHARGEFUNCCALC_H__
+
+
+
+// General
+
+// OpenBabel
+#include
+
+// Pharao
+#include ""pharmacophore.h""
+
+
+
+void chargeFuncCalc(OpenBabel::OBMol*, Pharmacophore*);
+
+
+
+#endif
+","Unknown"
+"In Silico","baoilleach/pharao","include/pharmacophore.h",".h","3712","171","/*******************************************************************************
+pharmacophore.h - Pharao
+
+Copyright (C) 2005-2010 by Silicos NV
+
+This file is part of the Open Babel project.
+For more information, see
+
+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.
+*******************************************************************************/
+
+
+
+#ifndef __SILICOS_PHARAO_PHARMACOPHORE_H__
+#define __SILICOS_PHARAO_PHARMACOPHORE_H__
+
+
+
+// General
+#include
+#include
+#include
+#include
+#include
+#include
+#include