/// <summary> Partitions the atoms in an AtomContainer into covalently connected components. /// /// </summary> /// <param name="atomContainer"> The AtomContainer to be partitioned into connected components, i.e. molecules /// </param> /// <returns> A SetOfMolecules. /// /// </returns> /// <cdk.dictref> blue-obelisk:graphPartitioning </cdk.dictref> public static ISetOfMolecules partitionIntoMolecules(IAtomContainer atomContainer) { IAtomContainer ac = atomContainer.Builder.newAtomContainer(); IAtom atom = null; IElectronContainer eContainer = null; IMolecule molecule = null; ISetOfMolecules molecules = atomContainer.Builder.newSetOfMolecules(); System.Collections.ArrayList sphere = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); for (int f = 0; f < atomContainer.AtomCount; f++) { atom = atomContainer.getAtomAt(f); atom.setFlag(CDKConstants.VISITED, false); ac.addAtom(atom); } IElectronContainer[] eContainers = atomContainer.ElectronContainers; for (int f = 0; f < eContainers.Length; f++) { eContainer = eContainers[f]; eContainer.setFlag(CDKConstants.VISITED, false); ac.addElectronContainer(eContainer); } while (ac.AtomCount > 0) { atom = ac.getAtomAt(0); molecule = atomContainer.Builder.newMolecule(); sphere.Clear(); sphere.Add(atom); atom.setFlag(CDKConstants.VISITED, true); PathTools.breadthFirstSearch(ac, sphere, molecule); molecules.addMolecule(molecule); ac.remove(molecule); } return(molecules); }
/// <summary> Method that assign properties to an atom given a particular atomType. /// /// </summary> /// <param name="atom"> Atom to configure /// </param> /// <param name="atomType"> AtomType /// </param> public static void configure(IAtom atom, IAtomType atomType) { atom.AtomTypeName = atomType.AtomTypeName; atom.MaxBondOrder = atomType.MaxBondOrder; atom.BondOrderSum = atomType.BondOrderSum; atom.VanderwaalsRadius = atomType.VanderwaalsRadius; atom.CovalentRadius = atomType.CovalentRadius; atom.Valency = atomType.Valency; atom.setFormalCharge(atomType.getFormalCharge()); atom.Hybridization = atomType.Hybridization; atom.FormalNeighbourCount = atomType.FormalNeighbourCount; atom.setFlag(CDKConstants.IS_HYDROGENBOND_ACCEPTOR, atomType.getFlag(CDKConstants.IS_HYDROGENBOND_ACCEPTOR)); atom.setFlag(CDKConstants.IS_HYDROGENBOND_DONOR, atomType.getFlag(CDKConstants.IS_HYDROGENBOND_DONOR)); System.Object constant = atomType.getProperty(CDKConstants.CHEMICAL_GROUP_CONSTANT); if (constant != null) { atom.setProperty(CDKConstants.CHEMICAL_GROUP_CONSTANT, constant); } atom.setFlag(CDKConstants.ISAROMATIC, atomType.getFlag(CDKConstants.ISAROMATIC)); System.Object color = atomType.getProperty("org.openscience.cdk.renderer.color"); if (color != null) { atom.setProperty("org.openscience.cdk.renderer.color", color); } if (atomType.AtomicNumber != 0) { atom.AtomicNumber = atomType.AtomicNumber; } if (atomType.getExactMass() > 0.0) { atom.setExactMass(atomType.getExactMass()); } }
/// <summary> Check whether a set of atoms in an atomcontainer is connected /// /// </summary> /// <param name="atomContainer"> The AtomContainer to be check for connectedness /// </param> /// <returns> true if the AtomContainer is connected /// </returns> public static bool isConnected(IAtomContainer atomContainer) { IAtomContainer ac = atomContainer.Builder.newAtomContainer(); IAtom atom = null; IMolecule molecule = atomContainer.Builder.newMolecule(); System.Collections.ArrayList sphere = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); for (int f = 0; f < atomContainer.AtomCount; f++) { atom = atomContainer.getAtomAt(f); atomContainer.getAtomAt(f).setFlag(CDKConstants.VISITED, false); ac.addAtom(atomContainer.getAtomAt(f)); } IBond[] bonds = atomContainer.Bonds; for (int f = 0; f < bonds.Length; f++) { bonds[f].setFlag(CDKConstants.VISITED, false); ac.addBond(bonds[f]); } atom = ac.getAtomAt(0); sphere.Add(atom); atom.setFlag(CDKConstants.VISITED, true); PathTools.breadthFirstSearch(ac, sphere, molecule); if (molecule.AtomCount == atomContainer.AtomCount) { return(true); } return(false); }
/// <summary> Recursivly perfoms a depth first search in a molecular graphs contained in /// the AtomContainer molecule, starting at the root atom and returning when it /// hits the target atom. /// CAUTION: This recursive method sets the VISITED flag of each atom /// does not reset it after finishing the search. If you want to do the /// operation on the same collection of atoms more than once, you have /// to set all the VISITED flags to false before each operation /// by looping of the atoms and doing a /// "atom.setFlag((CDKConstants.VISITED, false));" /// /// </summary> /// <param name="molecule">The /// AtomContainer to be searched /// </param> /// <param name="root"> The root atom /// to start the search at /// </param> /// <param name="target"> The target /// </param> /// <param name="path"> An /// AtomContainer to be filled with the path /// </param> /// <returns> true if the /// target atom was found during this function call /// </returns> public static bool depthFirstTargetSearch(IAtomContainer molecule, IAtom root, IAtom target, IAtomContainer path) { IBond[] bonds = molecule.getConnectedBonds(root); IAtom nextAtom; root.setFlag(CDKConstants.VISITED, true); for (int f = 0; f < bonds.Length; f++) { nextAtom = bonds[f].getConnectedAtom(root); if (!nextAtom.getFlag(CDKConstants.VISITED)) { path.addAtom(nextAtom); path.addBond(bonds[f]); if (nextAtom == target) { return(true); } else { if (!depthFirstTargetSearch(molecule, nextAtom, target, path)) { // we did not find the target path.removeAtom(nextAtom); path.removeElectronContainer(bonds[f]); } else { return(true); } } } } return(false); }
/// <summary> Recursively perform a DFS search on the <code>container</code> placing /// atoms and branches in the vector <code>tree</code>. /// /// </summary> /// <param name="a"> the atom being visited. /// </param> /// <param name="tree"> vector holding the tree. /// </param> /// <param name="parent"> the atom we came from. /// </param> /// <param name="container"> the AtomContainer that we are parsing. /// </param> private void createDFSTree(IAtom a, System.Collections.ArrayList tree, IAtom parent, IAtomContainer container) { tree.Add(a); System.Collections.IList neighbours = getCanNeigh(a, container); neighbours.Remove(parent); IAtom next; a.setFlag(CDKConstants.VISITED, true); //System.out.println("Starting with DFSTree and AtomContainer of size " + container.getAtomCount()); //System.out.println("Current Atom has " + neighbours.size() + " neighbours"); System.Collections.IEnumerator iter = neighbours.GetEnumerator(); //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'" while (iter.MoveNext()) { //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'" next = (IAtom)iter.Current; if (!next.getFlag(CDKConstants.VISITED)) { //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'" if (!iter.MoveNext()) { //Last neighbour therefore in this chain createDFSTree(next, tree, a, container); } else { System.Collections.ArrayList branch = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); tree.Add(branch); //System.out.println("adding branch"); createDFSTree(next, branch, a, container); } } else { //Found ring closure between next and a //System.out.println("found ringclosure in DFTTreeCreation"); ringMarker++; BrokenBond bond = new BrokenBond(this, a, next, ringMarker); if (!brokenBonds.Contains(bond)) { brokenBonds.Add(bond); } else { ringMarker--; } } } }
/// <summary> Read a Molecule from a file in MDL sd format /// /// </summary> /// <returns> The Molecule that was read from the MDL file. /// </returns> private IMolecule readMolecule(IMolecule molecule) { //logger.debug("Reading new molecule"); int linecount = 0; int atoms = 0; int bonds = 0; int atom1 = 0; int atom2 = 0; int order = 0; int stereo = 0; int RGroupCounter = 1; int Rnumber = 0; System.String[] rGroup = null; double x = 0.0; double y = 0.0; double z = 0.0; double totalZ = 0.0; //int[][] conMat = new int[0][0]; //String help; IBond bond; IAtom atom; System.String line = ""; try { //logger.info("Reading header"); line = input.ReadLine(); linecount++; if (line == null) { return(null); } //logger.debug("Line " + linecount + ": " + line); if (line.StartsWith("$$$$")) { //logger.debug("File is empty, returning empty molecule"); return(molecule); } if (line.Length > 0) { molecule.setProperty(CDKConstants.TITLE, line); } line = input.ReadLine(); linecount++; //logger.debug("Line " + linecount + ": " + line); line = input.ReadLine(); linecount++; //logger.debug("Line " + linecount + ": " + line); if (line.Length > 0) { molecule.setProperty(CDKConstants.REMARK, line); } //logger.info("Reading rest of file"); line = input.ReadLine(); linecount++; //logger.debug("Line " + linecount + ": " + line); atoms = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim()); //logger.debug("Atomcount: " + atoms); bonds = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim()); //logger.debug("Bondcount: " + bonds); // read ATOM block //logger.info("Reading atom block"); for (int f = 0; f < atoms; f++) { line = input.ReadLine(); linecount++; //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" x = System.Double.Parse(line.Substring(0, (10) - (0)).Trim()); //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" y = System.Double.Parse(line.Substring(10, (20) - (10)).Trim()); //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" z = System.Double.Parse(line.Substring(20, (30) - (20)).Trim()); totalZ += System.Math.Abs(z); // *all* values should be zero, not just the sum //logger.debug("Coordinates: " + x + "; " + y + "; " + z); System.String element = line.Substring(31, (34) - (31)).Trim(); //logger.debug("Atom type: ", element); if (IsotopeFactory.getInstance(molecule.Builder).isElement(element)) { atom = IsotopeFactory.getInstance(molecule.Builder).configure(molecule.Builder.newAtom(element)); } else { //logger.debug("Atom ", element, " is not an regular element. Creating a PseudoAtom."); //check if the element is R rGroup = element.Split(new char[] { '^', 'R' }); // ???? if (rGroup.Length > 1) { try { Rnumber = System.Int32.Parse(rGroup[(rGroup.Length - 1)]); RGroupCounter = Rnumber; } catch (System.Exception ex) { Rnumber = RGroupCounter; RGroupCounter++; } element = "R" + Rnumber; } atom = molecule.Builder.newPseudoAtom(element); } // store as 3D for now, convert to 2D (if totalZ == 0.0) later atom.setPoint3d(new Point3d(x, y, z)); // parse further fields System.String massDiffString = line.Substring(34, (36) - (34)).Trim(); //logger.debug("Mass difference: ", massDiffString); if (!(atom is IPseudoAtom)) { try { int massDiff = System.Int32.Parse(massDiffString); if (massDiff != 0) { IIsotope major = IsotopeFactory.getInstance(molecule.Builder).getMajorIsotope(element); atom.AtomicNumber = major.AtomicNumber + massDiff; } } catch (System.Exception exception) { //logger.error("Could not parse mass difference field"); } } else { //logger.error("Cannot set mass difference for a non-element!"); } System.String chargeCodeString = line.Substring(36, (39) - (36)).Trim(); //logger.debug("Atom charge code: ", chargeCodeString); int chargeCode = System.Int32.Parse(chargeCodeString); if (chargeCode == 0) { // uncharged species } else if (chargeCode == 1) { atom.setFormalCharge(+3); } else if (chargeCode == 2) { atom.setFormalCharge(+2); } else if (chargeCode == 3) { atom.setFormalCharge(+1); } else if (chargeCode == 4) { } else if (chargeCode == 5) { atom.setFormalCharge(-1); } else if (chargeCode == 6) { atom.setFormalCharge(-2); } else if (chargeCode == 7) { atom.setFormalCharge(-3); } try { // read the mmm field as position 61-63 System.String reactionAtomIDString = line.Substring(60, (63) - (60)).Trim(); //logger.debug("Parsing mapping id: ", reactionAtomIDString); try { int reactionAtomID = System.Int32.Parse(reactionAtomIDString); if (reactionAtomID != 0) { atom.ID = reactionAtomIDString; } } catch (System.Exception exception) { //logger.error("Mapping number ", reactionAtomIDString, " is not an integer."); //logger.debug(exception); } } catch (System.Exception exception) { // older mol files don't have all these fields... //logger.warn("A few fields are missing. Older MDL MOL file?"); } molecule.addAtom(atom); } // convert to 2D, if totalZ == 0 if (totalZ == 0.0 && !forceReadAs3DCoords.Set) { //logger.info("Total 3D Z is 0.0, interpreting it as a 2D structure"); IAtom[] atomsToUpdate = molecule.Atoms; for (int f = 0; f < atomsToUpdate.Length; f++) { IAtom atomToUpdate = atomsToUpdate[f]; Point3d p3d = atomToUpdate.getPoint3d(); atomToUpdate.setPoint2d(new Point2d(p3d.x, p3d.y)); atomToUpdate.setPoint3d(null); } } // read BOND block //logger.info("Reading bond block"); for (int f = 0; f < bonds; f++) { line = input.ReadLine(); linecount++; atom1 = System.Int32.Parse(line.Substring(0, (3) - (0)).Trim()); atom2 = System.Int32.Parse(line.Substring(3, (6) - (3)).Trim()); order = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim()); if (line.Length > 12) { stereo = System.Int32.Parse(line.Substring(9, (12) - (9)).Trim()); } else { //logger.warn("Missing expected stereo field at line: " + line); } //if (//logger.DebugEnabled) //{ // //logger.debug("Bond: " + atom1 + " - " + atom2 + "; order " + order); //} if (stereo == 1) { // MDL up bond stereo = CDKConstants.STEREO_BOND_UP; } else if (stereo == 6) { // MDL down bond stereo = CDKConstants.STEREO_BOND_DOWN; } else if (stereo == 4) { //MDL bond undefined stereo = CDKConstants.STEREO_BOND_UNDEFINED; } // interpret CTfile's special bond orders IAtom a1 = molecule.getAtomAt(atom1 - 1); IAtom a2 = molecule.getAtomAt(atom2 - 1); if (order == 4) { // aromatic bond bond = molecule.Builder.newBond(a1, a2, CDKConstants.BONDORDER_AROMATIC, stereo); // mark both atoms and the bond as aromatic bond.setFlag(CDKConstants.ISAROMATIC, true); a1.setFlag(CDKConstants.ISAROMATIC, true); a2.setFlag(CDKConstants.ISAROMATIC, true); molecule.addBond(bond); } else { bond = molecule.Builder.newBond(a1, a2, (double)order, stereo); molecule.addBond(bond); } } // read PROPERTY block //logger.info("Reading property block"); while (true) { line = input.ReadLine(); linecount++; if (line == null) { throw new CDKException("The expected property block is missing!"); } if (line.StartsWith("M END")) { break; } bool lineRead = false; if (line.StartsWith("M CHG")) { // FIXME: if this is encountered for the first time, all // atom charges should be set to zero first! int infoCount = System.Int32.Parse(line.Substring(6, (9) - (6)).Trim()); SupportClass.Tokenizer st = new SupportClass.Tokenizer(line.Substring(9)); for (int i = 1; i <= infoCount; i++) { System.String token = st.NextToken(); int atomNumber = System.Int32.Parse(token.Trim()); token = st.NextToken(); int charge = System.Int32.Parse(token.Trim()); molecule.getAtomAt(atomNumber - 1).setFormalCharge(charge); } } else if (line.StartsWith("M ISO")) { try { System.String countString = line.Substring(6, (9) - (6)).Trim(); int infoCount = System.Int32.Parse(countString); SupportClass.Tokenizer st = new SupportClass.Tokenizer(line.Substring(9)); for (int i = 1; i <= infoCount; i++) { int atomNumber = System.Int32.Parse(st.NextToken().Trim()); int absMass = System.Int32.Parse(st.NextToken().Trim()); if (absMass != 0) { IAtom isotope = molecule.getAtomAt(atomNumber - 1); isotope.MassNumber = absMass; } } } catch (System.FormatException exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String error = "Error (" + exception.Message + ") while parsing line " + linecount + ": " + line + " in property block."; //logger.error(error); throw new CDKException("NumberFormatException in isotope information on line: " + line, exception); } } else if (line.StartsWith("M RAD")) { try { System.String countString = line.Substring(6, (9) - (6)).Trim(); int infoCount = System.Int32.Parse(countString); SupportClass.Tokenizer st = new SupportClass.Tokenizer(line.Substring(9)); for (int i = 1; i <= infoCount; i++) { int atomNumber = System.Int32.Parse(st.NextToken().Trim()); int spinMultiplicity = System.Int32.Parse(st.NextToken().Trim()); if (spinMultiplicity > 1) { IAtom radical = molecule.getAtomAt(atomNumber - 1); for (int j = 2; j <= spinMultiplicity; j++) { // 2 means doublet -> one unpaired electron // 3 means triplet -> two unpaired electron molecule.addElectronContainer(molecule.Builder.newSingleElectron(radical)); } } } } catch (System.FormatException exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String error = "Error (" + exception.Message + ") while parsing line " + linecount + ": " + line + " in property block."; //logger.error(error); throw new CDKException("NumberFormatException in radical information on line: " + line, exception); } } else if (line.StartsWith("G ")) { try { System.String atomNumberString = line.Substring(3, (6) - (3)).Trim(); int atomNumber = System.Int32.Parse(atomNumberString); //String whatIsThisString = line.substring(6,9).trim(); System.String atomName = input.ReadLine(); // convert Atom into a PseudoAtom IAtom prevAtom = molecule.getAtomAt(atomNumber - 1); IPseudoAtom pseudoAtom = molecule.Builder.newPseudoAtom(atomName); if (prevAtom.getPoint2d() != null) { pseudoAtom.setPoint2d(prevAtom.getPoint2d()); } if (prevAtom.getPoint3d() != null) { pseudoAtom.setPoint3d(prevAtom.getPoint3d()); } AtomContainerManipulator.replaceAtomByAtom(molecule, prevAtom, pseudoAtom); } catch (System.FormatException exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String error = "Error (" + exception.ToString() + ") while parsing line " + linecount + ": " + line + " in property block."; //logger.error(error); throw new CDKException("NumberFormatException in group information on line: " + line, exception); } } if (!lineRead) { //logger.warn("Skipping line in property block: ", line); } } } catch (CDKException exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.Message; //logger.error(error); //logger.debug(exception); throw exception; } catch (System.Exception exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.Message; //logger.error(error); //logger.debug(exception); throw new CDKException(error, exception); } return(molecule); }
/// <summary> Recursivly perfoms a depth first search in a molecular graphs contained in /// the AtomContainer molecule, starting at the root atom and returning when it /// hits the target atom. /// CAUTION: This recursive method sets the VISITED flag of each atom /// does not reset it after finishing the search. If you want to do the /// operation on the same collection of atoms more than once, you have /// to set all the VISITED flags to false before each operation /// by looping of the atoms and doing a /// "atom.setFlag((CDKConstants.VISITED, false));" /// /// </summary> /// <param name="molecule">The /// AtomContainer to be searched /// </param> /// <param name="root"> The root atom /// to start the search at /// </param> /// <param name="target"> The target /// </param> /// <param name="path"> An /// AtomContainer to be filled with the path /// </param> /// <returns> true if the /// target atom was found during this function call /// </returns> public static bool depthFirstTargetSearch(IAtomContainer molecule, IAtom root, IAtom target, IAtomContainer path) { IBond[] bonds = molecule.getConnectedBonds(root); IAtom nextAtom; root.setFlag(CDKConstants.VISITED, true); for (int f = 0; f < bonds.Length; f++) { nextAtom = bonds[f].getConnectedAtom(root); if (!nextAtom.getFlag(CDKConstants.VISITED)) { path.addAtom(nextAtom); path.addBond(bonds[f]); if (nextAtom == target) { return true; } else { if (!depthFirstTargetSearch(molecule, nextAtom, target, path)) { // we did not find the target path.removeAtom(nextAtom); path.removeElectronContainer(bonds[f]); } else { return true; } } } } return false; }