All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.chem.ExtendedMoleculeFunctions Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
/*
 * 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.
 */

/*
* 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.
*
*/

package com.actelion.research.chem;

import com.actelion.research.calc.ArrayUtilsCalc;
import com.actelion.research.calc.statistics.StatisticsOverview;
import com.actelion.research.calc.statistics.median.MedianStatisticFunctions;
import com.actelion.research.chem.descriptor.DescriptorEncoder;
import com.actelion.research.chem.descriptor.DescriptorHandler;
import com.actelion.research.util.BurtleHasher;

import java.util.*;

/**
 *
 *
 * ExtendedMoleculeFunctions
 * @author Modest von Korff
 * @version 1.0
 * 2005 MvK: Start implementation
 */
public class ExtendedMoleculeFunctions {


	public static final int NUMBER_ELEMENTS = 118;

	public static final int COLOR_FOR_CENTER_SELECTION = Molecule.cAtomColorRed;


	public static final int [] arrRGroupsAtomicNo = {142,143,144,129,130,131,132,133,134,135,136,137,138,139,140,141};

	public static final String [] arrRGroupsSymbol = {"R1","R2","R3","R4","R5","R6","R7","R8","R9","R10","R11","R12","R13","R14","R15","R16"};


	public final static Coordinates getCenterGravity(ExtendedMolecule mol) {

		int n = mol.getAllAtoms();

		int [] indices = new int [n];

		for (int i = 0; i < indices.length; i++) {
			indices[i]=i;
		}

		return getCenterGravity(mol, indices);
	}

	public final static Coordinates getCenterGravity(ExtendedMolecule mol, int[] indices) {

		Coordinates c = new Coordinates();
		for (int i = 0; i < indices.length; i++) {
			c.x += mol.getAtomX(indices[i]);
			c.y += mol.getAtomY(indices[i]);
			c.z += mol.getAtomZ(indices[i]);
		}
		c.x /= indices.length;
		c.y /= indices.length;
		c.z /= indices.length;

		return c;
	}


	public static void makeSkeleton(StereoMolecule mol) {
		for (int bond=0; bond liIndexAtom, int cAtomColor){

		StringBuilder sb = new StringBuilder();

		int cc=0;
		for (int indexAtom : liIndexAtom) {

			String sColor = getcAtomColor2String(cAtomColor);

			sColor += ":"+indexAtom;

			sb.append(sColor);

			if(cc1) &&
					(atomicNo != 6) &&
					(atomicNo <= NUMBER_ELEMENTS)) {
				hetero++;
			}
		}

		return hetero;
	}

	public static boolean isHetero(ExtendedMolecule mol, int indexAtom){

		boolean hetero = false;

		int atomicNo = mol.getAtomicNo(indexAtom);

		if((atomicNo>1) &&
				(atomicNo != 6) &&
				(atomicNo <= NUMBER_ELEMENTS)) {
			hetero = true;
		}

		return hetero;
	}

	public static int getNumNitroGroupN(StereoMolecule mol) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(AtomFunctionAnalyzer.isNitroGroupN(mol, i)){
				n++;
			}
		}

		return n;
	}

	public static int getNumAmide(StereoMolecule mol) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(AtomFunctionAnalyzer.isAmide(mol, i)){
				n++;
			}
		}

		return n;
	}

	public static int getNumCarboxy(StereoMolecule mol) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(isCarboxyC(mol, i)){
				n++;
			}
		}

		return n;
	}


	public static int getNumAcidicOxygen(StereoMolecule mol) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(AtomFunctionAnalyzer.isAcidicOxygen(mol, i)){
				n++;
			}
		}

		return n;
	}

	public static int getNumBasicNitrogen(StereoMolecule mol) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(AtomFunctionAnalyzer.isBasicNitrogen(mol, i)){
				n++;
			}
		}

		return n;
	}
	public static int getNumAliphaticRingAtoms(ExtendedMolecule mol, int atomicNoQuery) {
		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {
			int atomicNo = mol.getAtomicNo(i);

			if(mol.isRingAtom(i) && atomicNo==atomicNoQuery){
				n++;
			}
		}

		return n;
	}
	/**
	 *
	 * @param mol
	 * @return number of atoms which are not hydrogen.
	 */
	public static int getNumHeavyAtoms(ExtendedMolecule mol) {
		int heavy = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {
			int atomicNo = mol.getAtomicNo(i);
			if(atomicNo>1 && atomicNo <= NUMBER_ELEMENTS) {
				heavy++;
			}
		}

		return heavy;
	}

	public static int getNumAromaticAtoms(ExtendedMolecule mol) {

		int n=0;
		for (int i = 0; i < mol.getAtoms(); i++) {
			if(mol.isAromaticAtom(i)){
				n++;
			}
		}
		return n;
	}

	public static int getNumArylAmine(StereoMolecule mol) {

		int n=0;
		for (int i = 0; i < mol.getAtoms(); i++) {
			if(AtomFunctionAnalyzer.isArylAmine(mol, i)){
				n++;
			}
		}
		return n;
	}

	public static int getNumHeteroAromaticAtoms(ExtendedMolecule mol) {

		int n=0;
		for (int i = 0; i < mol.getAtoms(); i++) {
			if(mol.isRingAtom(i) && mol.isAromaticAtom(i) && mol.getAtomicNo(i)!= 6){
				n++;
			}
		}
		return n;
	}

	public static int getNumIsopropyl(ExtendedMolecule mol) {

		int n=0;
		for (int i = 0; i < mol.getAtoms(); i++) {
			if(mol.isRingAtom(i) && mol.isAromaticAtom(i) && mol.getAtomicNo(i)!= 6){
				n++;
			}
		}
		return n;
	}

	public static int getNumSubstructure(StereoMolecule mol, String idcodeFragment) {

		IDCodeParser parser = new IDCodeParser();

		StereoMolecule frag = parser.getCompactMolecule(idcodeFragment);

		frag.ensureHelperArrays(Molecule.cHelperRings);

		SSSearcher ssSearcher = new SSSearcher();

		ssSearcher.setMol(frag, mol);

		int nMatches = ssSearcher.findFragmentInMolecule();

		return nMatches;
	}



	public static int getBondNo(ExtendedMolecule mol, int atm1,int atm2) {
		for (int bnd=0; bnd 1) {

			StereoMolecule biggest = mol;
			int maxAtoms = 0;
			for (int ii = 0; ii < frags.length; ii++) {
				if (frags[ii].getAllAtoms() > maxAtoms) {
					maxAtoms = frags[ii].getAllAtoms();
					biggest = frags[ii];
				}
			}
			Canonizer canFrag = new Canonizer(biggest);
			idBiggest = canFrag.getIDCode();
		}

		return idBiggest;
	}

	public static StereoMolecule getBiggestFragment(StereoMolecule mol) {

		StereoMolecule [] frags = mol.getFragments();

		StereoMolecule biggest = mol;

		if(frags.length > 1) {

			int maxAtoms = 0;
			for (int ii = 0; ii < frags.length; ii++) {
				if (frags[ii].getAllAtoms() > maxAtoms) {
					maxAtoms = frags[ii].getAllAtoms();
					biggest = frags[ii];
				}
			}
		}
		return biggest;
	}

	/**
	 * Replaces all hetero-atoms, except hydrogen, with carbon.
	 * @param m
	 * @return
	 */
	public static StereoMolecule getConverted2CarbonSkeleton(StereoMolecule m){

		StereoMolecule skel = new StereoMolecule(m);

		skel.ensureHelperArrays(Molecule.cHelperRings);

		for (int i = 0; i < skel.getAtoms(); i++) {

			if(skel.getAtomicNo(i)>1) {
				skel.setAtomicNo(i, 6);
			}
		}

		skel.ensureHelperArrays(Molecule.cHelperRings);

		return skel;

	}

	public static Comparator getComparatorAtomsBonds(){

		return new Comparator() {

			@Override
			public int compare(StereoMolecule m1, StereoMolecule m2) {

				if(m1.getAllAtoms() > m2.getAllAtoms()){
					return 1;
				}else if(m1.getAllAtoms() < m2.getAllAtoms()){
					return -1;
				} else if(m1.getAllBonds() > m2.getAllBonds()){
					return 1;
				} else if(m1.getAllBonds() < m2.getAllBonds()){
					return -1;
				}

				return 0;
			}
		};

	}

	public static boolean checkBiggestFragmentForUnwanted(StereoMolecule mol, List liAtomicNo) {
		boolean bOk=true;

		ExtendedMolecule [] frags = mol.getFragments();
		int indexBiggestFrag = 0;
		if(frags.length > 1) {
			int maxAtoms = 0;
			for (int ii = 0; ii < frags.length; ii++) {
				if (frags[ii].getAllAtoms() > maxAtoms) {
					indexBiggestFrag = ii;
					maxAtoms = frags[ii].getAllAtoms();
				}
			}
		}

		ExtendedMolecule frag = frags[indexBiggestFrag];

		for (int i = 0; i < frag.getAllAtoms(); i++) {
			if(liAtomicNo.contains(new Integer(frag.getAtomicNo(i)))) {
				bOk=false;
				break;
			}
		}
		return bOk;
	}
	/**
	 *
	 * @param mol
	 * @param hsAtomicNo
	 * @return true if an atomic number from the hash set is found.
	 */
	public static boolean containsAtLeastOneAtomicNumbersFromHashSet(ExtendedMolecule mol, HashSet hsAtomicNo) {
		boolean bOk=false;

		for (int i = 0; i < mol.getAllAtoms(); i++) {
			if(hsAtomicNo.contains(mol.getAtomicNo(i))) {
				bOk=true;
				break;
			}
		}
		return bOk;
	}

	public static boolean containsHeteroAtom(ExtendedMolecule mol, int [] arrIndexAt) {
		boolean hetero=false;

		for (int indexAt : arrIndexAt) {
			if(mol.getAtomicNo(indexAt)!=6 && mol.getAtomicNo(indexAt)!=1){
				hetero=true;
				break;
			}
		}
		return hetero;
	}

	/**
	 *
	 * @param mol
	 * @param hsAtomicNo
	 * @return true if the molecule contains an atomic number that is not in the hash set.
	 */
	public static boolean containsSolelyAtomicNumbersFromHashSet(ExtendedMolecule mol, HashSet hsAtomicNo) {

		boolean allAtomicNosInHashset=true;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(!hsAtomicNo.contains(mol.getAtomicNo(i))) {

				allAtomicNosInHashset=false;

				break;
			}
		}

		return allAtomicNosInHashset;
	}

	/**
	 *
	 * @param molecule
	 * @param at index to specify atom in molecule
	 * @param fragment one atom must have the color: COLOR_FOR_CENTER_SELECTION.
	 * @return true if the specified atom in molecule equals the colored atom in fragment.
	 */
	public final static boolean atomAtomSubStrucMatch(StereoMolecule molecule, int at, StereoMolecule fragment) {
		boolean bMatch = false;

		SSSearcher sss = new SSSearcher();
		sss.setMol(fragment, molecule);
		int numFrags = sss.findFragmentInMolecule(SSSearcher.cCountModeOverlapping, SSSearcher.cMatchAromDBondToDelocalized);
		if(numFrags == 0)
			return false;

		List liAtomLists = sss.getMatchList();
		ArrayUtilsCalc.removeDoubletsIntOrderIndepend(liAtomLists);

		int atomMarkedInQuery = -1;
		for (int i = 0; i < fragment.getAllAtoms(); i++) {
			if(fragment.getAtomColor(i)== COLOR_FOR_CENTER_SELECTION){
				atomMarkedInQuery = i;
			}
		}

		for (Iterator iter = liAtomLists.iterator(); iter.hasNext();) {
			int[] arrInd = (int[]) iter.next();
			if(arrInd[atomMarkedInQuery] == at){
				bMatch = true;
				break;
			}
		}

		return bMatch;

	}

	public static double [][] getDistanceArray(ExtendedMolecule mol) {

		double arr[][]= new double[mol.getAllAtoms()][mol.getAllAtoms()];

		for (int i = 0; i < arr.length; i++) {
			for (int j = i+1; j < arr.length; j++) {
				double dx = mol.getAtomX(i) - mol.getAtomX(j);
				double dy = mol.getAtomY(i) - mol.getAtomY(j);
				double dz = mol.getAtomZ(i) - mol.getAtomZ(j);
				double v= Math.sqrt(dx*dx+dy*dy+dz*dz);
				arr[i][j] = v;
				arr[j][i] = v;
			}
		}

		return arr;
	}

	public final static int [] getTopologicalDistances(int [][] topoDistMatrix, int [] at1, int [] at2) {

		int [] d = new int[at1.length*at2.length];
		int cc=0;
		for (int i = 0; i < at1.length; i++) {
			for (int j = 0; j < at2.length; j++) {
				d[cc++]=topoDistMatrix[at1[i]][at2[j]];
			}
		}

		return d;
	}
	public final static int getMedianTopologicalDistance(int [][] topoDistMatrix, int [] at1, int [] at2) {
		int [] d = getTopologicalDistances(topoDistMatrix, at1, at2);
		int medTopoDist = MedianStatisticFunctions.getMedianForInteger(d).median;
		return medTopoDist;
	}

	public final static int getTopologicalDistance(ExtendedMolecule mol, int at1, int at2) {
		int dist = 0;

		if(at1 == at2)
			return 0;

		List liExamine = new ArrayList();
		List liSphere = new ArrayList();
		List liVisited = new ArrayList();

		liExamine.add(new Integer(at1));

		liSphere.add(new Integer(1));

		boolean bFound = false;
		while(!liExamine.isEmpty()) {
			dist = ((Integer)liSphere.remove(0)).intValue();
			int indAtCenter = ((Integer)liExamine.remove(0)).intValue();
			liVisited.add(new Integer(indAtCenter));
			int numJNeighbors = mol.getAllConnAtoms(indAtCenter);
			for (int i = 0; i < numJNeighbors; i++) {
				int indAtNeighb = mol.getConnAtom(indAtCenter, i);
				if(indAtNeighb == at2) {
					bFound = true;
					break;
				}
				if(!liVisited.contains(new Integer(indAtNeighb))) {
					liExamine.add(new Integer(indAtNeighb));
					liSphere.add(new Integer(dist + 1));
				}
			}
			if(bFound)
				break;
		}

		if(!bFound)
			dist = -1;

		return dist;
	}

	public final static int [][] getTopologicalDistanceMatrix(StereoMolecule mol) {
		return getNumberOfBondsBetweenAtoms(mol, mol.getBonds(), null);
	}

	/**
	 * From Joel Freyss developed for the FFMolecule
	 * Computes a matrix of distances between all the atoms in the graph.
	 * Complexity: O(m*n*maxBonds) m = number of bonds, n = number of atoms
	 * @param mol
	 * @param maxBonds
	 * @return an array A[i][j] = nBonds if i and j are connected by less than maxBonds
	 * 						      or -1  otherwise
	 */
	public static int[][] getNumberOfBondsBetweenAtoms(StereoMolecule mol, int maxBonds, int[][] dist) {
		//Initialization of the data
		if(dist==null)  dist = new int[mol.getAtoms()][mol.getAtoms()];
		int N = dist.length;
		for(int i=0; i=N || a2>=N) continue;
				for(int a0=0; a00]
					if(dist[a0][a1]>=0 && (dist[a0][a2]==-1 || dist[a0][a1]+1=0 && (dist[a0][a1]==-1 || dist[a0][a2]+14)?true:false;
			}
		}

		return five;
	}

	/**
	 *
	 * @param mol
	 * @param atom
	 * @return true for cyano and iso-cyano.
	 */
	public static boolean isCyanoN(StereoMolecule mol, int atom) {

		if(mol.getAtomicNo(atom)!= 7){
			return false;
		}


		int nConn = mol.getConnAtoms(atom);

		if(nConn != 1){
			return false;
		}

		int indexConn = mol.getConnAtom(atom, 0);

		int bond = mol.getBond(atom, indexConn);

		if(mol.getBondOrder(bond) != 3){
			return false;
		}

		return true;
	}

	public static boolean isThioEther(StereoMolecule mol, int atom) {

		if(mol.getAtomicNo(atom)!= 16){
			return false;
		}


		int nConn = mol.getConnAtoms(atom);

		if(nConn != 2){
			return false;
		}

		boolean thio = true;
		for (int i = 0; i < nConn; i++) {

			int indexConn = mol.getConnAtom(atom, i);

			if(mol.getAtomicNo(indexConn) != 6){
				thio=false;
				break;
			}

		}

		return thio;
	}

	public static boolean isWildcard(StereoMolecule mol, int atom) {

		if(mol.getAtomicNo(atom) == PeriodicTable.ConnectionPoint){
			return true;
		}

		return false;
	}

	/**
	 *
	 * @param mol
	 * @param atom
	 * @return true if atom is S and at let one attached atom is O.
	 */
	public static boolean isSulfoxyGroup(StereoMolecule mol, int atom) {

		if(mol.getAtomicNo(atom)!= 16){
			return false;
		}

		int nConn = mol.getConnAtoms(atom);

		boolean oxy = false;
		for (int i = 0; i < nConn; i++) {

			int indexConn = mol.getConnAtom(atom, i);

			if(mol.getAtomicNo(indexConn) == 8){
				oxy=true;
				break;
			}

		}

		return oxy;
	}

	/**
	 *
	 * @param mol
	 * @param indexAtCentral
	 * @param arrIndexAt
	 * @return true if no connected carbon atom is in arrIndexAt
	 */
	public static boolean isIsolatedCarbon(StereoMolecule mol, int indexAtCentral, int [] arrIndexAt){

		boolean isolated=true;
		int nConnected = mol.getConnAtoms(indexAtCentral);
		boolean [] arrConnected = new boolean[mol.getAtoms()];

		for (int i = 0; i < nConnected; i++) {
			arrConnected[mol.getConnAtom(indexAtCentral,i)]=true;
		}

		for (int indexAt : arrIndexAt) {
			if(!arrConnected[indexAt]){
				continue;
			}
			if(mol.getAtomicNo(indexAt)==6){
				isolated=false;
				break;
			}
		}

		return isolated;
	}

	public static int [] extractAromaticRing(StereoMolecule mol, int [] arrIndexAt){

		RingCollection rc = mol.getRingSet();

		boolean [] arrRingMemberMarker = new boolean[mol.getAtoms()];

		int [] arrIndexAromaticRing = null;

		for (int i = 0; i < rc.getSize(); i++) {

			if(!rc.isAromatic(i)){
				continue;
			}

			Arrays.fill(arrRingMemberMarker, false);

			int [] arrRingAtoms = rc.getRingAtoms(i);

			for (int indexRingAtom : arrRingAtoms) {
				arrRingMemberMarker[indexRingAtom]=true;
			}

			int sum=0;
			for (int indexAt : arrIndexAt) {
				if(arrRingMemberMarker[indexAt]){
					sum++;
				}
			}

			if(sum==arrRingAtoms.length){
				arrIndexAromaticRing=arrRingAtoms;
				break;
			}
		}

		return arrIndexAromaticRing;
	}

	/**
	 * Counts cyano and iso-cyano
	 * @param mol
	 * @return
	 */
	public static int getNumCyanoGroups(StereoMolecule mol){

		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(isCyanoN(mol, i)){
				n++;
			}
		}

		return n;

	}


	public static int getTotalCharge(StereoMolecule mol){
		int n = 0;
		for (int i = 0; i < mol.getAllAtoms(); i++) {
			n+=mol.getAtomCharge(i);
		}
		return n;
	}
	public static int getNumAtomsCharged(StereoMolecule mol){
		int n = 0;
		for (int i = 0; i < mol.getAllAtoms(); i++) {
			if(mol.getAtomCharge(i)!=0){
				n++;
			}
		}
		return n;
	}

	public static int getNumAlcoholicOxygen(StereoMolecule mol){
		int n = 0;
		for (int i = 0; i < mol.getAllAtoms(); i++) {
			if(isAlcoholicOxygen(mol, i)){
				n++;
			}
		}
		return n;
	}

	public static int getNumThioEther(StereoMolecule mol){

		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(isThioEther(mol, i)){
				n++;
			}
		}

		return n;

	}

	public static int getNumSulfOxyGroups(StereoMolecule mol){

		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(isSulfoxyGroup(mol, i)){
				n++;
			}
		}

		return n;

	}

	public static int getNumWildcards(StereoMolecule mol){

		int n = 0;

		for (int i = 0; i < mol.getAllAtoms(); i++) {

			if(isWildcard(mol, i)){
				n++;
			}
		}

		return n;

	}

	public static boolean isAlcoholicOxygen(StereoMolecule mol, int atom) {

		if (mol.getAtomicNo(atom) != 8 || mol.isAromaticAtom(atom))
			return false;

		int nConnected = mol.getConnAtoms(atom);

		if(nConnected != 1){

			return false;
		}

		int indConn = mol.getConnAtom(atom, 0);

		if(mol.getAtomicNo(indConn) != 6){
			return false;
		}

		if(AtomFunctionAnalyzer.getNegativeNeighbourCount(mol, indConn) > 1){
			return false;
		}

		return true;
	}

	public static boolean isEtherOxygenAtAromatic(StereoMolecule mol, int atom) {

		boolean aromaticEtherO = false;

		if (mol.getAtomicNo(atom) != 8 || mol.isAromaticAtom(atom))
			return false;

		int nConnected = mol.getConnAtoms(atom);

		if(nConnected != 2){

			return false;
		}

		int indConn1 = mol.getConnAtom(atom, 0);

		int indConn2 = mol.getConnAtom(atom, 1);

		if(mol.isAromaticAtom(indConn1)){

			aromaticEtherO = true;

		} else if(mol.isAromaticAtom(indConn2)){

			aromaticEtherO = true;

		}

		return aromaticEtherO;
	}

	public static boolean isCarboxyC(StereoMolecule mol, int atom) {

		if (mol.getAtomicNo(atom) != 6 || mol.getAtomPi(atom) != 0)
			return false;

		boolean carboxyO = false;

		boolean etherO = false;

		for (int i=0; i removeSubStructures(List liInput){
		SSSearcher sss = new SSSearcher();

		LinkedList li = new LinkedList(liInput);

		for (int i = li.size()-1; i >= 0; i--) {

			StereoMolecule mol1 = li.get(i);

			for (int j = 0; j < li.size(); j++) {
				if(i!=j){

					StereoMolecule mol2 = li.get(j);

					if(mol1.getAtoms() liFragment){

		StereoMolecule molReduced = new StereoMolecule(mol);

		molReduced.ensureHelperArrays(Molecule.cHelperRings);

		SSSearcher sss = new SSSearcher();

		HashSet hsIndexMatchingAtoms = new HashSet();

		for (StereoMolecule frag : liFragment) {

			sss.setMol(frag, molReduced);

			if(sss.findFragmentInMolecule()>0){

				List liMatch = sss.getMatchList();

				hsIndexMatchingAtoms.clear();

				for (int[] arrMatch : liMatch) {

					for (int i = 0; i < arrMatch.length; i++) {
						hsIndexMatchingAtoms.add(arrMatch[i]);
					}
				}

				int [] arrIndexMatchingAtomsUnique = ArrayUtilsCalc.toIntArray(hsIndexMatchingAtoms);

				molReduced.deleteAtoms(arrIndexMatchingAtomsUnique);

				molReduced.ensureHelperArrays(Molecule.cHelperRings);

			}
		}


		return molReduced;
	}

	public static StereoMolecule removeSubstructureFromMolecule(StereoMolecule mol, StereoMolecule frag){

		StereoMolecule molReduced = new StereoMolecule(mol);

		molReduced.ensureHelperArrays(Molecule.cHelperRings);

		SSSearcher sss = new SSSearcher();



		sss.setMol(frag, molReduced);

		if(sss.findFragmentInMolecule()>0){

			List liMatch = sss.getMatchList();


			if(liMatch.size() > 1){
				throw new RuntimeException("Fragment found more than once!");

			} if(liMatch.size() == 1){

				int [] arrMatchIndex = liMatch.get(0);

				molReduced.deleteAtoms(arrMatchIndex);

				molReduced.ensureHelperArrays(Molecule.cHelperRings);

//				boolean [] arrMatchingAtom = new boolean [molReduced.getAtoms()];
//
//
//
//				for (int i = 0; i < arrMatchIndex.length; i++) {
//					arrMatchingAtom[arrMatchIndex[i]]=true;
//				}
//
//				int indexRGroup = 1;
//
//				for (int i = 0; i < arrMatchIndex.length; i++) {
//
//					int atMatch = arrMatchIndex[i];
//
//					int nConnected = molReduced.getConnAtoms(atMatch);
//
//					for (int j = 0; j < nConnected; j++) {
//
//						int atConnected = molReduced.getConnAtom(atMatch, j);
//
//						if(arrMatchingAtom[atConnected]){
//							continue;
//						}
//
//						int bond = molReduced.getBond(atMatch, nConnected);
//
//						molReduced.deleteBond(bond);
//
//						int atNoRGroup = getAtomicNoRGroup(indexRGroup++);
//
//						int indexAtRGroup = molReduced.addAtom(atNoRGroup);
//
//						molReduced.addBond(atConnected, indexAtRGroup, Molecule.cBondTypeSingle);
//
//						molReduced.ensureHelperArrays(Molecule.cHelperNeighbours);
//
//					}
//
//				}
//


			}



		}



		return molReduced;
	}

	public static StereoMolecule removeWildcards(StereoMolecule mol){

		StereoMolecule molReduced = new StereoMolecule(mol);

		molReduced.ensureHelperArrays(Molecule.cHelperRings);

		for (int i = molReduced.getAtoms()-1; i >= 0; i--) {

			if(molReduced.getAtomicNo(i)==0){
				molReduced.deleteAtom(i);
			}

		}

		molReduced.ensureHelperArrays(Molecule.cHelperRings);

		return molReduced;
	}


	public static void setColorMCS2Molecule(StereoMolecule mol, StereoMolecule molMCS){

		for (int i = 0; i < mol.getAtoms(); i++) {
			mol.setAtomColor(i, Molecule.cAtomColorNone);
		}

		SSSearcher sss = new SSSearcher();

		sss.setMol(molMCS, mol);

		if(sss.findFragmentInMolecule()>0){
			List liMatch = sss.getMatchList();

			int [] arrIndexMatch = liMatch.get(0);

			for (int i = 0; i < arrIndexMatch.length; i++) {
				mol.setAtomColor(arrIndexMatch[i], Molecule.cAtomColorBlue);
			}

		}

	}

	public static void setColorMolecule(StereoMolecule mol, int [] arrIndexMatch){

		for (int i = 0; i < mol.getAtoms(); i++) {
			mol.setAtomColor(i, Molecule.cAtomColorNone);
		}

		for (int i = 0; i < arrIndexMatch.length; i++) {
			mol.setAtomColor(arrIndexMatch[i], Molecule.cAtomColorBlue);
		}
	}

	/**
	 *
	 * @param mol
	 * @param arrIndexBonds
	 * @param color i.e. Molecule.cAtomColorBlue
	 */
	public static void setColorMoleculeFromBondIndex(StereoMolecule mol, int [] arrIndexBonds, int color){

		for (int i = 0; i < arrIndexBonds.length; i++) {

			final int indexAtom1 = mol.getBondAtom(0, arrIndexBonds[i]);

			final int indexAtom2 = mol.getBondAtom(1, arrIndexBonds[i]);

			mol.setAtomColor(indexAtom1, color);
			mol.setAtomColor(indexAtom2, color);

		}
	}

	public static void setCoordinatesNull(StereoMolecule mol){
		for (int i = 0; i < mol.getAtoms(); i++) {
			mol.setAtomX(i,0);
			mol.setAtomY(i,0);
			mol.setAtomZ(i,0);
		}
	}

	/**
	 * Returns the colors vector for the substructure in mol.
	 * @param mol
	 * @param frag
	 * @param atomColor Molecule.cAtomColor
	 * @return
	 */
	public static String getColorVectorSubstructure(StereoMolecule mol, StereoMolecule frag, int atomColor){

		SSSearcher sss = new SSSearcher();

		sss.setMol(frag, mol);

		HashSet hsAtomIndex = new HashSet();

		if(sss.findFragmentInMolecule()>0){
			List liMatch = sss.getMatchList();


			int[] arr = liMatch.get(0);
			for (int i = 0; i < arr.length; i++) {
				hsAtomIndex.add(arr[i]);
			}

//			for (int[] arr : liMatch) {
//				for (int i = 0; i < arr.length; i++) {
//					hsAtomIndex.add(arr[i]);
//				}
//			}
		}

		String sAtomColor = ExtendedMoleculeFunctions.getColorRecord(mol, hsAtomIndex, atomColor);

		return sAtomColor;

	}

	public static float getSimilarity(StereoMolecule m1, StereoMolecule m2, DescriptorHandler dh){

		Object d1 = dh.createDescriptor(m1);
		Object d2 = dh.createDescriptor(m2);

		return dh.getSimilarity(d1, d2);
	}

	/**
	 * Taken from Thomas Sander SkeletonSpheres descriptor
	 * @param mol
	 * @param rootAtom atom index to start
	 * @param depth so many spheres are taken
	 * @return Fragment containing the spheres started at rootAtom
	 */
	public static StereoMolecule getSphere(StereoMolecule mol, int rootAtom, int depth){

		mol.ensureHelperArrays(Molecule.cHelperRings);
		StereoMolecule fragment = new StereoMolecule(mol.getAtoms(), mol.getBonds());

		int[] atomList = new int[mol.getAtoms()];
		boolean[] atomMask = new boolean[mol.getAtoms()];
		if (rootAtom != 0)
			Arrays.fill(atomMask, false);

		int min = 0;
		int max = 0;

		for (int sphere=0; sphere arrRGroupsAtomicNo.length){
			throw new RuntimeException("");
		}

		return arrRGroupsAtomicNo[r-1];

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy