com.actelion.research.chem.coords.InventorFragment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openchemlib Show documentation
Show all versions of openchemlib Show documentation
Open Source Chemistry Library
/*
* Copyright (c) 1997 - 2016
* Actelion Pharmaceuticals Ltd.
* Gewerbestrasse 16
* CH-4123 Allschwil, Switzerland
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Thomas Sander
*/
package com.actelion.research.chem.coords;
import com.actelion.research.chem.StereoMolecule;
import java.util.ArrayList;
import java.util.Arrays;
public class InventorFragment {
private static final double cCollisionLimitBondRotation = 0.8;
private static final double cCollisionLimitAtomMovement = 0.5;
private final int CIRCULAR_BINS = 36;
protected int[] mGlobalAtom;
protected int[] mGlobalBond;
protected int[] mGlobalToLocalAtom;
protected int[] mPriority;
protected double[] mAtomX;
protected double[] mAtomY;
protected boolean mKeepMarkedAtoms;
private StereoMolecule mMol;
private boolean mMinMaxAvail;
private double mMinX;
private double mMinY;
private double mMaxX;
private double mMaxY;
private double mCollisionPanalty;
private int[][] mFlipList;
private int[] mSortedAtom;
protected InventorFragment(StereoMolecule mol, int atoms, boolean keepMarkedAtoms) {
mMol = mol;
mKeepMarkedAtoms = keepMarkedAtoms;
mGlobalAtom = new int[atoms];
mPriority = new int[atoms];
mAtomX = new double[atoms];
mAtomY = new double[atoms];
}
protected InventorFragment(InventorFragment f) {
mMol = f.mMol;
mKeepMarkedAtoms = f.mKeepMarkedAtoms;
mGlobalAtom = new int[f.size()];
mPriority = new int[f.size()];
mAtomX = new double[f.size()];
mAtomY = new double[f.size()];
for (int i=0; ithe bond atom that lies on the larger side of the bond
// [1]->the bond atom on the smaller side of the bond
// [2...n]->all other atoms on the smaller side of the bond.
// These are the ones getting flipped on the mirror
// line defined by the bond.
if (mFlipList == null)
mFlipList = new int[mMol.getAllBonds()][];
if (mFlipList[bond] == null) {
int[] graphAtom = new int[mGlobalAtom.length];
boolean[] isOnSide = new boolean[mMol.getAllAtoms()];
int atom1 = mMol.getBondAtom(0, bond);
int atom2 = mMol.getBondAtom(1, bond);
graphAtom[0] = atom1;
isOnSide[atom1] = true;
int current = 0;
int highest = 0;
while (current <= highest) {
for (int i=0; i mGlobalAtom.length/2);
// if we retain core atoms and the smaller side contains core atoms, then flip the larger side
if (mKeepMarkedAtoms) {
boolean coreOnSide = false;
boolean coreOffSide = false;
for (int i = 0; i< mGlobalAtom.length; i++) {
int atom = mGlobalAtom[i];
if (mMol.isMarkedAtom(atom) && atom != atom1 && atom != atom2) {
if (isOnSide[mGlobalAtom[i]])
coreOnSide = true;
else
coreOffSide = true;
}
}
if (coreOnSide != coreOffSide)
flipOtherSide = coreOnSide;
}
int count = 2;
mFlipList[bond] = new int[flipOtherSide ? mGlobalAtom.length-highest : highest+2];
for (int i = 0; i< mGlobalAtom.length; i++) {
if (mGlobalAtom[i] == atom1)
mFlipList[bond][flipOtherSide ? 0 : 1] = i;
else if (mGlobalAtom[i] == atom2)
mFlipList[bond][flipOtherSide ? 1 : 0] = i;
else if (flipOtherSide ^ isOnSide[mGlobalAtom[i]])
mFlipList[bond][count++] = i;
}
}
double x = mAtomX[mFlipList[bond][0]];
double y = mAtomY[mFlipList[bond][0]];
double mirrorAngle = InventorAngle.getAngle(x, y, mAtomX[mFlipList[bond][1]],
mAtomY[mFlipList[bond][1]]);
for (int i=2; i=2) ? corner-2 : corner+2);
if (maxGain < gain) {
maxGain = gain;
maxCorner = corner;
}
}
double sumHeight = getHeight() + f.getHeight();
double sumWidth = 0.75 * (getWidth() + f.getWidth());
double maxHeight = Math.max(getHeight(), f.getHeight());
double maxWidth = 0.75 * Math.max(getWidth(), f.getWidth());
double bestCornerSize = Math.sqrt((sumHeight - maxGain) * (sumHeight - maxGain)
+ (sumWidth - 0.75 * maxGain) * (sumWidth - 0.75 * maxGain));
double toppedSize = Math.max(maxWidth, sumHeight);
double besideSize = Math.max(maxHeight, sumWidth);
if (bestCornerSize < toppedSize && bestCornerSize < besideSize) {
switch(maxCorner) {
case 0:
f.translate(mMaxX - f.mMinX - maxGain + 1.0, mMinY - f.mMaxY + maxGain - 1.0);
break;
case 1:
f.translate(mMaxX - f.mMinX - maxGain + 1.0, mMaxY - f.mMinY - maxGain + 1.0);
break;
case 2:
f.translate(mMinX - f.mMaxX + maxGain - 1.0, mMaxY - f.mMinY - maxGain + 1.0);
break;
case 3:
f.translate(mMinX - f.mMaxX + maxGain - 1.0, mMinY - f.mMaxY + maxGain - 1.0);
break;
}
}
else if (besideSize < toppedSize) {
f.translate(mMaxX - f.mMinX + 1.0, (mMaxY + mMinY - f.mMaxY - f.mMinY) / 2);
}
else {
f.translate((mMaxX + mMinX - f.mMaxX - f.mMinX) / 2, mMaxY - f.mMinY + 1.0);
}
}
private void calculateMinMax() {
if (mMinMaxAvail)
return;
mMinX = mAtomX[0];
mMaxX = mAtomX[0];
mMinY = mAtomY[0];
mMaxY = mAtomY[0];
for (int i = 0; i< mGlobalAtom.length; i++) {
double surplus = getAtomSurplus(i);
if (mMinX > mAtomX[i] - surplus)
mMinX = mAtomX[i] - surplus;
if (mMaxX < mAtomX[i] + surplus)
mMaxX = mAtomX[i] + surplus;
if (mMinY > mAtomY[i] - surplus)
mMinY = mAtomY[i] - surplus;
if (mMaxY < mAtomY[i] + surplus)
mMaxY = mAtomY[i] + surplus;
}
mMinMaxAvail = true;
}
private double getCornerDistance(int corner) {
double minDistance = 9999.0;
for (int atom = 0; atom< mGlobalAtom.length; atom++) {
double surplus = getAtomSurplus(atom);
double d = 0.0;
switch (corner) {
case 0: // top right
d = mMaxX - 0.5 * (mMaxX + mMinY + mAtomX[atom] - mAtomY[atom]);
break;
case 1: // bottom right
d = mMaxX - 0.5 * (mMaxX - mMaxY + mAtomX[atom] + mAtomY[atom]);
break;
case 2: // bottom left
d = 0.5 * (mMinX + mMaxY + mAtomX[atom] - mAtomY[atom]) - mMinX;
break;
case 3: // top left
d = 0.5 * (mMinX - mMinY + mAtomX[atom] + mAtomY[atom]) - mMinX;
break;
}
if (minDistance > d - surplus)
minDistance = d - surplus;
}
return minDistance;
}
private double getAtomSurplus(int atom) {
return (mMol.getAtomQueryFeatures(mGlobalAtom[atom]) != 0) ? 0.6
: (mMol.getAtomicNo(mGlobalAtom[atom]) != 6) ? 0.25 : 0.0;
}
protected ArrayList getCollisionList() {
mCollisionPanalty = 0.0;
ArrayList collisionList = new ArrayList();
for (int i = 1; i< mGlobalAtom.length; i++) {
for (int j=0; j atom)
fragmentBonds++;
// for (int j=connAtoms; j atom && isMember(mMol.getConnAtom(atom, j)))
// fragmentBonds++;
}
mGlobalBond = new int[fragmentBonds];
mGlobalToLocalAtom = new int[mMol.getAllAtoms()];
fragmentBonds = 0;
for (int i=0; i atom)
mGlobalBond[fragmentBonds++] = mMol.getConnBond(atom, j);
// for (int j=connAtoms; j atom && isMember(mMol.getConnAtom(atom, j)))
// mGlobalBond[fragmentBonds++] = mMol.getConnBond(atom, j);
}
}
protected void optimizeAtomCoordinates(int atom) {
double x = mAtomX[atom];
double y = mAtomY[atom];
InventorAngle[] collisionForce = new InventorAngle[4];
int forces = 0;
for (int i = 0; i< mGlobalBond.length; i++) {
if (forces >= 4)
break;
if (atom == mGlobalToLocalAtom[mMol.getBondAtom(0, mGlobalBond[i])]
|| atom == mGlobalToLocalAtom[mMol.getBondAtom(1, mGlobalBond[i])])
continue;
double x1 = mAtomX[mGlobalToLocalAtom[mMol.getBondAtom(0, mGlobalBond[i])]];
double y1 = mAtomY[mGlobalToLocalAtom[mMol.getBondAtom(0, mGlobalBond[i])]];
double x2 = mAtomX[mGlobalToLocalAtom[mMol.getBondAtom(1, mGlobalBond[i])]];
double y2 = mAtomY[mGlobalToLocalAtom[mMol.getBondAtom(1, mGlobalBond[i])]];
double d1 = Math.sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
double d2 = Math.sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));
double bondLength = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
if (d1 0) {
InventorAngle force = CoordinateInventor.getMeanAngle(collisionForce, forces);
mAtomX[atom] += force.mLength * Math.sin(force.mAngle);
mAtomY[atom] += force.mLength * Math.cos(force.mAngle);
}
}
/**
* @param x
* @param y
* @return angle
*/
protected double calculatePreferredAttachmentAngle(double x, double y, int neighbourAtomCount, double padding) {
if (size() == 1)
return 0;
final double BIN_ANGLE = 2.0 * Math.PI / CIRCULAR_BINS;
double neighbourRadius = padding
+ Math.sqrt(neighbourAtomCount); // assume a little large, because they neighbour exposes its wide side
double[] distance = new double[CIRCULAR_BINS];
for (int i = 0; i< mGlobalAtom.length; i++) {
double angle = InventorAngle.getAngle(x, y, mAtomX[i], mAtomY[i]);
int bin = correctBin((int)Math.round(angle * CIRCULAR_BINS / (2.0*Math.PI)));
double dx = x - mAtomX[i];
double dy = y - mAtomY[i];
double sd = dx*dx + dy*dy;
if (distance[bin] < sd)
distance[bin] = sd;
}
double maxDistance = -1;
int maxBin = -1;
for (int i=0; i= minDistance)
continue;
double localMinDistance = distance[bin];
// check, whether localMinDistance is compatible with adjacent bins and adapt, if needed
for (int i=1; i localMinDistance) {
minDistance = localMinDistance;
minBin = bin;
}
}
return Math.PI * 2 * minBin / CIRCULAR_BINS;
}
private int correctBin(int bin) {
return bin < 0 ? bin + CIRCULAR_BINS : bin >= CIRCULAR_BINS ? bin - CIRCULAR_BINS : bin;
}
}