📄 deducebondsystemtool.java
字号:
java.util.ArrayList al5 = new java.util.ArrayList();
al1.add(num[0] + "-" + num[1]);
al1.add(num[2] + "-" + num[3]);
al1.add(num[4] + "-" + num[5]);
al2.add(num[0] + "-" + num[1]);
al2.add(num[2] + "-" + num[3]);
al2.add(num[5] + "-" + num[6]);
al3.add(num[1] + "-" + num[2]);
al3.add(num[3] + "-" + num[4]);
al3.add(num[5] + "-" + num[6]);
al4.add(num[1] + "-" + num[2]);
al4.add(num[3] + "-" + num[4]);
al4.add(num[6] + "-" + num[0]);
al5.add(num[2] + "-" + num[3]);
al5.add(num[4] + "-" + num[5]);
al5.add(num[6] + "-" + num[0]);
ArrayList mal = new ArrayList();
mal.add(al1);
mal.add(al2);
mal.add(al3);
mal.add(al4);
mal.add(al5);
MasterList.add(mal);
}
private int getBadCount(IMolecule molecule, IRingSet ringSet) {
// finds count of nitrogens in the rings that have 4 bonds
// to non hydrogen atoms and one to hydrogen
// or nitrogens with 2 double bonds to atoms in the ringset
// or have S atom with more than 2 bonds
// these arent necessarily bad- just unlikely
int count = 0;
for (int j = 0; j <= molecule.getAtomCount() - 1; j++) {
IAtom atom = molecule.getAtom(j);
//logger.debug(mol.getBondOrderSum(a));
if (inRingSet(atom, ringSet)) {
//logger.debug("in ring set");
if (atom.getSymbol().equals("N")) {
if (atom.getFormalCharge() == 0) {
// logger.debug(mol.getBondOrderSum(a));
if (molecule.getBondOrderSum(atom) == 4) {
count++; //
} else if (molecule.getBondOrderSum(atom) == 5) {
// check if have 2 double bonds to atom in ring
int doublebondcount = 0;
java.util.List ca = molecule.getConnectedAtomsList(atom);
for (int k = 0; k <= ca.size() - 1; k++) {
if (molecule.getBond(atom, (IAtom)ca.get(k)).getOrder() == 2) {
if (inRingSet((IAtom)ca.get(k), ringSet)) {
doublebondcount++;
}
}
}
if (doublebondcount == 2) {
count++;
}
}
} else if (atom.getFormalCharge() == 1) {
if (molecule.getBondOrderSum(atom) == 5) {
count++;
}
}
} else if (atom.getSymbol().equals("S")) {
if (molecule.getBondOrderSum(atom) > 2) {
count++;
}
}
}
}
//logger.debug("here bad count = " + count);
return count;
}
private boolean inRingSet(IAtom atom, IRingSet ringSet) {
for (int i = 0; i < ringSet.getAtomContainerCount(); i++) {
Ring ring = (Ring) ringSet.getAtomContainer(i);
if (ring.contains(atom)) return true;
}
return false;
}
private IMolecule loop(long starttime, IMolecule molecule, int index,
ArrayList MasterList, int [] choices, IMoleculeSet som) throws CDKException {
//logger.debug(System.currentTimeMillis());
long time = System.currentTimeMillis();
long diff = time - starttime;
if (diff > 100000) {
//time out after 100 seconds
throw new CDKException("Timed out after 100 seconds.");
} else if (this.interrupted) {
throw new CDKException("Process was interrupted.");
}
ArrayList ringlist = (ArrayList) MasterList.get(index);
IMolecule mnew2 = null;
for (int i = 0; i <= ringlist.size() - 1; i++) {
choices[index] = i;
if (index == MasterList.size() - 1) {
//logger.debug(choices[0]+"\t"+choices[1]);
IMolecule mnew = null;
try {
mnew = (Molecule) molecule.clone();
} catch (Exception e) {
logger.error("Failed to clone molecule: ", e.getMessage());
logger.debug(e);
}
for (int j = 0; j <= MasterList.size() - 1; j++) {
ArrayList ringlist2 = (ArrayList) MasterList.get(j);
ArrayList bondlist = (ArrayList) ringlist2.get(choices[j]);
// logger.debug(j+"\t"+choices[j]);
applyBonds(mnew, bondlist);
}
// logger.debug("");
counter++;
try {
hAdder.addImplicitHydrogensToSatisfyValency(mnew);
} catch (Exception e) {
logger.error("Failed to add hydrogens: ", e.getMessage());
logger.debug(e);
}
if (isStructureOK(mnew)) {
IRingSet rs = this.removeExtraRings(mnew); // need to redo this since created new molecule (mnew)
if (rs != null) {
int count = this.getBadCount(mnew, rs);
// logger.debug("bad count="+count);
if (count == 0) {
// logger.debug("found match after "+counter+"
// iterations");
return mnew; // dont worry about adding to set
// just finish
} else {
som.addMolecule(mnew);
}
}
}
}
if (index + 1 <= MasterList.size() - 1) {
// logger.debug("here3="+counter);
mnew2 = loop(starttime, molecule, index + 1, MasterList, choices, som); //recursive def
}
if (mnew2 instanceof IMolecule) {
return mnew2;
}
}
return null;
}
private boolean isStructureOK(IMolecule molecule) {
for (int i = 0; i <= molecule.getAtomCount() - 1; i++) {
//logger.debug(mj.getBondOrderSum(mj.getAtomAt(i)));
try {
// Note: valencyHybridChecker.couldMatchAtomType shouldnt check Hybridization to get it to work for non carbon atoms
valencyChecker.isSaturated(molecule.getAtom(i), molecule);
//valencyChecker.allSaturated didnt seem to work so did it this way
} catch (Exception e) {
logger.debug(i + "\t" + "atom " + (i + 1) + " is not saturated");
logger.debug(e.toString());
return false;
}
}
try {
IRingSet ringSet = recoverRingSystem(molecule);
for (int i = 0; i <= molecule.getAtomCount() - 1; i++) {
molecule.getAtom(i).setFlag(CDKConstants.ISAROMATIC, false);
}
for (int i = 0; i <= ringSet.getAtomContainerCount() - 1; i++) {
Ring r = (Ring) ringSet.getAtomContainer(i);
r.setFlag(CDKConstants.ISAROMATIC, false);
}
//logger.debug("Rs size= "+rs.size());
// do it multiple times to catch all the aromatic rings
// this problem is that the aromaticity detector relies on
// the aromaticity of the individual atoms in the ring
// these wont be picked up until you do it multiple times
// for example pyrene 129-00-0
for (int i = 0; i <= ringSet.getAtomContainerCount() - 1; i++) {
HueckelAromaticityDetector.detectAromaticity(molecule, ringSet, false);
}
// Figure out which rings we want to make sure are aromatic:
boolean [] Check = this.findRingsToCheck(ringSet);
// for (int i=0;i<=Check.length-1;i++) {
// logger.debug(i+"\t"+rs.getAtomContainer(i).getAtomCount()+"\t"+Check[i]);
// }
for (int i = 0; i <= ringSet.getAtomContainerCount() - 1; i++) {
Ring ring = (Ring) ringSet.getAtomContainer(i);
//logger.debug(k+"\t"+r.getAtomCount()+"\t"+r.getFlag(CDKConstants.ISAROMATIC));
if (Check[i]) {
for (int j = 0; j <= ring.getAtomCount() - 1; j++) {
if (ring.getAtom(j).getHydrogenCount()<0) {
return false;
}
}
if (!ring.getFlag(CDKConstants.ISAROMATIC)) {
// logger.debug(counter+"\t"+"ring not aromatic"+"\t"+r.getAtomCount());
return false;
}
}
}
return true;
} catch (Exception e) {
logger.debug(e.toString());
return false;
}
}
/**
* Remove rings.
* <p/>
* Removes rings which do not have all sp2 aromatic atoms and also gets rid of rings that have more than
* 7 or less than 5 atoms in them.
*
* @param m The molecule from which we want to remove rings
* @return The set of reduced rings
*/
private IRingSet removeExtraRings(IMolecule m) {
try {
SSSRFinder arf = new SSSRFinder(m);
IRingSet rs = arf.findSSSR();
//remove rings which dont have all aromatic atoms (according to hybridization set by lower case symbols in smiles):
//logger.debug("numrings="+rs.size());
iloop:
for (int i = 0; i <= rs.getAtomContainerCount() - 1; i++) {
boolean AllAromatic = true;
IRing r = (Ring) rs.getAtomContainer(i);
if (r.getAtomCount() > 7 || r.getAtomCount() < 5) {
rs.removeAtomContainer(i);
i--; // go back to first one
continue iloop;
}
//int NonSP2Count = 0;
for (int j = 0; j <= r.getAtomCount() - 1; j++) {
//logger.debug(j+"\t"+r.getAtomAt(j).getSymbol()+"\t"+r.getAtomAt(j).getHybridization());
if (r.getAtom(j).getHybridization() == CDKConstants.UNSET || r.getAtom(j).getHybridization() != CDKConstants.HYBRIDIZATION_SP2) {
rs.removeAtomContainer(i);
i--; // go back
continue iloop;
// NonSP2Count++;
// if (r.getAtom(j).getSymbol().equals("C")) {
// rs.removeAtomContainer(i);
// i--; // go back
// continue iloop;
// }
}
}
// if (NonSP2Count > 1) {
// rs.removeAtomContainer(i);
// i--; // go back
// continue iloop;
// }
}
return rs;
} catch (Exception e) {
return new RingSet();
}
}
private boolean[] findRingsToCheck(IRingSet rs) {
boolean[] Check = new boolean[rs.getAtomContainerCount()];
for (int i = 0; i <= Check.length - 1; i++) {
Check[i] = true;
}
iloop:
for (int i = 0; i <= rs.getAtomContainerCount() - 1; i++) {
boolean AllAromatic = true;
IRing r = (Ring) rs.getAtomContainer(i);
if (r.getAtomCount() > 7) {
Check[i] = false;
continue iloop;
}
int NonSP2Count = 0;
for (int j = 0; j <= r.getAtomCount() - 1; j++) {
// logger.debug(j+"\t"+r.getAtomAt(j).getSymbol()+"\t"+r.getAtomAt(j).getHybridization());
if (r.getAtom(j).getHybridization() == CDKConstants.UNSET || r.getAtom(j).getHybridization() != 2) {
NonSP2Count++;
if (r.getAtom(j).getSymbol().equals("C")) {
Check[i] = false;
continue iloop;
}
}
}
if (NonSP2Count > 1) {
Check[i] = false;
continue iloop;
}
}
return Check;
}
/**
* Stores an IRingSet corresponding to a molecule using the bond numbers.
*
* @param mol The IMolecule for which to store the IRingSet.
* @param ringSet The IRingSet to store
* @see recoverRingSystem
*/
private void storeRingSystem(IMolecule mol, IRingSet ringSet) {
listOfRings = new ArrayList(); // this is a list of int arrays
for (int r = 0; r < ringSet.getAtomContainerCount(); ++r) {
IRing ring = (IRing)ringSet.getAtomContainer(r);
int[] bondNumbers = new int[ring.getBondCount()];
for (int i = 0; i < ring.getBondCount(); ++i)
bondNumbers[i] = mol.getBondNumber(ring.getBond(i));
listOfRings.add(bondNumbers);
}
}
/**
* Recovers a RingSet corresponding to a molecule that has been
* stored by storeRingSystem().
*
* @param mol The IMolecule for which to recover the IRingSet.
* @see storeRingSystem
*/
private IRingSet recoverRingSystem(IMolecule mol) {
IRingSet ringSet = mol.getBuilder().newRingSet();
for (int r = 0; r < listOfRings.size(); ++r) {
int[] bondNumbers = (int[])listOfRings.get(r);
IRing ring = mol.getBuilder().newRing(bondNumbers.length);
for (int i = 0; i < bondNumbers.length; ++i) {
IBond bond = mol.getBond(bondNumbers[i]);
ring.addBond(bond);
if (!ring.contains(bond.getAtom(0))) ring.addAtom(bond.getAtom(0));
if (!ring.contains(bond.getAtom(1))) ring.addAtom(bond.getAtom(1));
}
ringSet.addAtomContainer(ring);
}
return ringSet;
}
public void setInterrupted(boolean interrupted) {
this.interrupted = interrupted;
}
public boolean isInterrupted() {
return this.interrupted;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -