use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class SmilesGenerator method parseAtom.
/**
* Generates the SMILES string for the atom
*
*@param a the atom to generate the SMILES for.
*@param buffer the string buffer that the atom is to be
* apended to.
*@param container the AtomContainer to analyze.
*@param chiral is a chiral smiles wished?
*@param parent the atom we came from.
*@param atomsInOrderOfSmiles a vector containing the atoms in the order
* they are in the smiles.
*@param currentChain The chain we currently deal with.
*@param useAromaticity true=aromaticity or sp2 will trigger lower case letters, wrong=only sp2
*/
private void parseAtom(IAtom a, StringBuffer buffer, IAtomContainer container, boolean chiral, boolean[] doubleBondConfiguration, IAtom parent, List<IAtom> atomsInOrderOfSmiles, List<Object> currentChain, boolean useAromaticity) {
if (!"H".equals(a.getSymbol()))
this.order.add(a);
String symbol = a.getSymbol();
if (a instanceof PseudoAtom)
symbol = "*";
boolean stereo = false;
if (chiral)
stereo = BondTools.isStereo(container, a);
boolean brackets = symbol.equals("B") || symbol.equals("C") || symbol.equals("N") || symbol.equals("O") || symbol.equals("P") || symbol.equals("S") || symbol.equals("F") || symbol.equals("Br") || symbol.equals("I") || symbol.equals("Cl");
brackets = !brackets;
// Deal with the start of a double bond configuration
if (chiral && isStartOfDoubleBond(container, a, parent, doubleBondConfiguration)) {
buffer.append('/');
}
String mass = generateMassString(a);
brackets = brackets | !mass.equals("");
String charge = generateChargeString(a);
brackets = brackets | !charge.equals("");
// we put in a special check for N.planar3 cases such
// as for indole and pyrrole, which require an explicit
// H on the nitrogen. However this only makes sense when
// the connectivity is not 3 - so for a case such as n1ncn(c1)CC
// the PLANAR3 N already has 3 bonds, so don't add a H for this case
boolean isSpecialNitrogen = a.getSymbol().equals("N") && a.getHybridization() == IAtomType.Hybridization.PLANAR3 && container.getConnectedAtomsList(a).size() != 3 && (a.getFormalCharge() == null || a.getFormalCharge() == 0);
brackets = brackets | isSpecialNitrogen;
if (chiral && stereo && (BondTools.isTrigonalBipyramidalOrOctahedral(container, a) != 0 || BondTools.isSquarePlanar(container, a) || BondTools.isTetrahedral(container, a, false) != 0 || BondTools.isSquarePlanar(container, a))) {
brackets = true;
}
if (brackets) {
buffer.append('[');
}
buffer.append(mass);
if ((useAromaticity && a.getFlag(CDKConstants.ISAROMATIC))) {
// the PLANAR3 N already has 3 bonds, so don't add a H for this case
if (isSpecialNitrogen) {
buffer.append(a.getSymbol().toLowerCase()).append("H");
} else
buffer.append(a.getSymbol().toLowerCase());
} else {
buffer.append(symbol);
if (symbol.equals("*") && a.getImplicitHydrogenCount() != null && a.getImplicitHydrogenCount() > 0)
buffer.append("H").append(a.getImplicitHydrogenCount());
}
if (a.getProperty(RING_CONFIG) != null && a.getProperty(RING_CONFIG).equals(UP)) {
buffer.append('/');
}
if (a.getProperty(RING_CONFIG) != null && a.getProperty(RING_CONFIG).equals(DOWN)) {
buffer.append('\\');
}
if (chiral && stereo && (BondTools.isTrigonalBipyramidalOrOctahedral(container, a) != 0 || BondTools.isSquarePlanar(container, a) || BondTools.isTetrahedral(container, a, false) != 0)) {
buffer.append('@');
}
if (chiral && stereo && BondTools.isSquarePlanar(container, a)) {
buffer.append("SP1");
}
// chiral
// hcount
buffer.append(charge);
if (brackets) {
buffer.append(']');
}
// Deal with the end of a double bond configuration
if (chiral && isEndOfDoubleBond(container, a, parent, doubleBondConfiguration)) {
IAtom viewFrom = null;
for (int i = 0; i < currentChain.size(); i++) {
if (currentChain.get(i) == parent) {
int k = i - 1;
while (k > -1) {
if (currentChain.get(k) instanceof IAtom) {
viewFrom = (IAtom) currentChain.get(k);
break;
}
k--;
}
}
}
if (viewFrom == null) {
for (int i = 0; i < atomsInOrderOfSmiles.size(); i++) {
if (atomsInOrderOfSmiles.get(i) == parent) {
viewFrom = (IAtom) atomsInOrderOfSmiles.get(i - 1);
}
}
}
boolean afterThisAtom = false;
IAtom viewTo = null;
for (int i = 0; i < currentChain.size(); i++) {
if (afterThisAtom && currentChain.get(i) instanceof IAtom) {
viewTo = (IAtom) currentChain.get(i);
break;
}
if (afterThisAtom && currentChain.get(i) instanceof List) {
viewTo = (IAtom) ((List<?>) currentChain.get(i)).get(0);
break;
}
if (a == currentChain.get(i)) {
afterThisAtom = true;
}
}
try {
if (BondTools.isCisTrans(viewFrom, a, parent, viewTo, container)) {
buffer.append('\\');
} else {
buffer.append('/');
}
} catch (CDKException ex) {
// If the user wants a double bond configuration, where there is none, we ignore this.
}
}
List<IAtom> v = new Vector<IAtom>();
Iterator<Integer> it = getRingOpenings(a, v).iterator();
Iterator<IAtom> it2 = v.iterator();
// logger.debug("in parseAtom() after checking for Ring openings");
while (it.hasNext()) {
Integer integer = (Integer) it.next();
IAtom a2 = (IAtom) it2.next();
IBond b = container.getBond(a2, a);
IBond.Order type = b.getOrder();
if (!(useAromaticity && a.getFlag(CDKConstants.ISAROMATIC) && a2.getFlag(CDKConstants.ISAROMATIC))) {
if (type == IBond.Order.DOUBLE) {
buffer.append("=");
} else if (type == IBond.Order.TRIPLE) {
buffer.append("#");
}
}
if (integer >= 10)
buffer.append("%" + integer);
else
buffer.append(integer);
}
atomsInOrderOfSmiles.add(a);
// logger.debug("End of parseAtom()");
}
use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class MappedChain method getNeighborsBonds.
/**
* Get neighbors bonds idxs
* @param idxs Blocs bonds idxs
* @param res Search in res
* @return neighbors bonds
*/
public List<Integer> getNeighborsBonds(IMolecule mol) {
List<Integer> idxs = null;
if (this.bondsMapping.size() == 0) {
idxs = new ArrayList<>();
for (IBond b : mol.bonds()) idxs.add(mol.getBondNumber(b));
return idxs;
}
idxs = this.getBondsMapping();
ArrayList<Integer> neighbors = new ArrayList<>();
for (Integer idx : idxs) {
IBond bond = mol.getBond(idx);
List<IBond> nBonds = new ArrayList<>();
nBonds.addAll(mol.getConnectedBondsList(bond.getAtom(0)));
nBonds.addAll(mol.getConnectedBondsList(bond.getAtom(1)));
for (IBond nb : nBonds) {
int nIdx = mol.getBondNumber(nb);
if (!idxs.contains(nIdx) && !neighbors.contains(nIdx))
neighbors.add(nIdx);
}
}
return neighbors;
}
use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class DeepenMatcher method mask.
/**
* Create a new molecule without covered atoms
*/
public static Molecule mask(Coverage coverage) {
Molecule covMol = coverage.getChemicalObject().getMolecule();
Molecule mol = new Molecule();
List<Integer> coveredIdxs = DeepenMatcher.calculateCoveredAtoms(coverage);
for (IAtom atom : covMol.atoms()) if (!coveredIdxs.contains(covMol.getAtomNumber(atom)))
mol.addAtom(atom);
HashSet<IBond> bonds = new HashSet<>();
for (IAtom atom : mol.atoms()) {
for (IBond bond : covMol.getConnectedBondsList(atom)) if (mol.contains(bond.getAtom(0)) && mol.contains(bond.getAtom(1))) {
if (!bonds.contains(bond)) {
mol.addBond(bond);
bonds.add(bond);
}
}
}
return mol;
}
use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class Chain method getMolecule.
/**
* construct recursively a molecule corresponding to the bloc.
* Attention, this function is not very efficient because of multiple molecules creation.
* @return Bloc molecule. Can return null if there are problems of compatibility between sub-bloc and extension positions.
*/
public IMolecule getMolecule() {
if (this.mol != null)
return this.mol;
IMolecule mol = null;
this.atoms = new ArrayList<>();
// Sub-bloc molecule
if (this.getSize() > 1) {
IMolecule subMol = this.subBlc.getMolecule();
mol = new Molecule();
for (IAtom a : subMol.atoms()) mol.addAtom(a);
for (IBond b : subMol.bonds()) mol.addBond(b);
for (IAtom a : this.subBlc.atoms) {
IAtom cloneA = mol.getAtom(subMol.getAtomNumber(a));
this.atoms.add(cloneA);
}
} else {
mol = new Molecule();
}
// Extension of mol
IAtom a1 = this.ext.getBond().getAtom(0);
if (this.position1 == -1) {
try {
a1 = (IAtom) a1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
mol.addAtom(a1);
this.atoms.add(a1);
// Hydrogens
int hydrogens = this.ext.getBond().getAtom(0).getImplicitHydrogenCount();
a1.setImplicitHydrogenCount(hydrogens);
// Aromatic
if (this.ext.getBond().getAtom(0).getFlag(CDKConstants.ISAROMATIC))
a1.setFlag(CDKConstants.ISAROMATIC, true);
else
a1.setFlag(CDKConstants.ISAROMATIC, false);
} else {
if (a1.getAtomicNumber() == this.atoms.get(this.position1).getAtomicNumber())
a1 = this.atoms.get(this.position1);
else
return null;
}
IAtom a2 = this.ext.getBond().getAtom(1);
if (this.position2 == -1) {
try {
a2 = (IAtom) a2.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
mol.addAtom(a2);
this.atoms.add(a2);
// Hydrogen
int hydrogens = this.ext.getBond().getAtom(1).getImplicitHydrogenCount();
a2.setImplicitHydrogenCount(hydrogens);
// Aromatic
if (this.ext.getBond().getAtom(1).getFlag(CDKConstants.ISAROMATIC))
a2.setFlag(CDKConstants.ISAROMATIC, true);
else
a2.setFlag(CDKConstants.ISAROMATIC, false);
} else if (a2.getAtomicNumber() == this.atoms.get(this.position2).getAtomicNumber())
a2 = this.atoms.get(this.position2);
else
return null;
mol.addBond(new Bond(a1, a2, this.ext.getBond().getOrder()));
this.mol = mol;
return mol;
}
use of org.openscience.cdk.interfaces.IBond in project Smiles2Monomers by yoann-dufresne.
the class ChainLearning method initLearn.
// -------------------------------- Initialization ----------------------------------
private void initLearn(List<Residue> roots, ResidueMappings mappings) {
for (Residue res : roots) {
List<MappedChain> resMappings = new ArrayList<>();
for (IBond bond : res.getMolecule().bonds()) {
Extension ext = new Extension(bond);
Chain bloc = new Chain(ext);
String smiles = bloc.getMySmiles();
this.chains.put(smiles, bloc);
this.frequence.put(smiles, 0);
// Creation of residue matchings (sequences of size 1)
for (BondMapping bm : ext.match(bond, MatchingType.EXACT)) {
MappedChain resMap = this.createMappingFromMatch(res, bond, bloc, bm);
resMappings.add(resMap);
}
}
mappings.put(res, resMappings);
}
this.frequencesInit();
}
Aggregations