Ejemplo n.º 1
0
        public static IAtomContainer GetLargestMoleculeFragment(
            IAtomContainer mol,
            out int acc)
        {
            AtomContainerSet acs;
            IAtomContainer   mol2, molMain = null;

            acs = null;

            if (ConnectivityChecker.isConnected(mol))
            {
                acc = 1;
                return(mol);
            }

            acs = (AtomContainerSet)ConnectivityChecker.partitionIntoMolecules(mol);
            int largestAc = -1;

            acc = acs.getAtomContainerCount();
            for (int aci = 0; aci < acc; aci++)
            {
                mol2 = acs.getAtomContainer(aci);
                int ac2 = mol2.getAtomCount();
                if (ac2 > largestAc)
                {
                    largestAc = mol2.getAtomCount();
                    molMain   = mol2;
                }
            }

            return(molMain);
        }
Ejemplo n.º 2
0
        /// <summary>  Description of the Method
        ///
        /// </summary>
        /// <param name="smiles">                     Description of the Parameter
        /// </param>
        /// <returns>                             Description of the Return Value
        /// </returns>
        /// <exception cref="InvalidSmilesException"> Description of the Exception
        /// </exception>
        public virtual Reaction parseReactionSmiles(System.String smiles)
        {
            SupportClass.Tokenizer tokenizer      = new SupportClass.Tokenizer(smiles, ">");
            System.String          reactantSmiles = tokenizer.NextToken();
            System.String          agentSmiles    = "";
            System.String          productSmiles  = tokenizer.NextToken();
            if (tokenizer.HasMoreTokens())
            {
                agentSmiles   = productSmiles;
                productSmiles = tokenizer.NextToken();
            }

            Reaction reaction = new Reaction();

            // add reactants
            IMolecule       reactantContainer = parseSmiles(reactantSmiles);
            ISetOfMolecules reactantSet       = ConnectivityChecker.partitionIntoMolecules(reactantContainer);

            IMolecule[] reactants = reactantSet.Molecules;
            for (int i = 0; i < reactants.Length; i++)
            {
                reaction.addReactant(reactants[i]);
            }

            // add reactants
            if (agentSmiles.Length > 0)
            {
                IMolecule       agentContainer = parseSmiles(agentSmiles);
                ISetOfMolecules agentSet       = ConnectivityChecker.partitionIntoMolecules(agentContainer);
                IMolecule[]     agents         = agentSet.Molecules;
                for (int i = 0; i < agents.Length; i++)
                {
                    reaction.addAgent(agents[i]);
                }
            }

            // add products
            IMolecule       productContainer = parseSmiles(productSmiles);
            ISetOfMolecules productSet       = ConnectivityChecker.partitionIntoMolecules(productContainer);

            IMolecule[] products = productSet.Molecules;
            for (int i = 0; i < products.Length; i++)
            {
                reaction.addProduct(products[i]);
            }

            return(reaction);
        }
Ejemplo n.º 3
0
        /// <summary> Method that saturates a molecule by adding implicit hydrogens.
        ///
        /// </summary>
        /// <param name="container"> Molecule to saturate
        /// </param>
        /// <cdk.keyword>           hydrogen, adding </cdk.keyword>
        /// <cdk.keyword>           implicit hydrogen </cdk.keyword>
        //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
        public virtual System.Collections.Hashtable addImplicitHydrogensToSatisfyValency(IAtomContainer container)
        {
            ISetOfMolecules moleculeSet = ConnectivityChecker.partitionIntoMolecules(container);

            IMolecule[] molecules = moleculeSet.Molecules;
            //UPGRADE_TODO: Class 'java.util.HashMap' was converted to 'System.Collections.Hashtable' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilHashMap'"
            System.Collections.Hashtable hydrogenAtomMap = new System.Collections.Hashtable();
            for (int k = 0; k < molecules.Length; k++)
            {
                IMolecule molPart = molecules[k];
                IAtom[]   atoms   = molPart.Atoms;
                for (int f = 0; f < atoms.Length; f++)
                {
                    int[] hydrogens = addImplicitHydrogensToSatisfyValency(molPart, atoms[f]);
                    hydrogenAtomMap[atoms[f]] = hydrogens;
                }
            }
            return(hydrogenAtomMap);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Fragment a molecule
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>

        public static List <KeyValuePair <string, IAtomContainer> > FragmentMoleculeAndCanonicalizeSmiles(
            IAtomContainer mol,
            bool filterOutCommonCounterIons)
        {
            int aci, fi, i1;

            KeyValuePair <string, IAtomContainer>         kvp;
            List <KeyValuePair <string, IAtomContainer> > frags = new List <KeyValuePair <string, IAtomContainer> >();

            AtomContainerSet acs = (AtomContainerSet)ConnectivityChecker.partitionIntoMolecules(mol);

            int acc = acs.getAtomContainerCount();

            for (aci = 0; aci < acc; aci++)
            {
                IAtomContainer fragMol    = acs.getAtomContainer(aci);
                string         fragSmiles = AtomContainerToSmiles(fragMol);
                if (filterOutCommonCounterIons)
                {
                    if (CommonSmallFragments.Contains(fragSmiles) ||
                        GetHeavyAtomCount(fragMol) <= 6)
                    {
                        continue;
                    }
                }

                kvp = new KeyValuePair <string, IAtomContainer>(fragSmiles, fragMol);

                int ac = fragMol.getAtomCount();

                for (fi = frags.Count - 1; fi >= 0; fi--)                 // insert into list so that fragments are ordered largest to smallest
                {
                    if (frags[fi].Value.getAtomCount() >= ac)
                    {
                        break;
                    }
                }
                frags.Insert(fi + 1, kvp);
            }

            return(frags);
        }
 public static void removeElectronContainer(ISetOfAtomContainers set_Renamed, IElectronContainer electrons)
 {
     IAtomContainer[] acs = set_Renamed.AtomContainers;
     for (int i = 0; i < acs.Length; i++)
     {
         IAtomContainer container = acs[i];
         if (container.contains(electrons))
         {
             container.removeElectronContainer(electrons);
             IMolecule[] molecules = ConnectivityChecker.partitionIntoMolecules(container).Molecules;
             if (molecules.Length > 1)
             {
                 set_Renamed.removeAtomContainer(container);
                 for (int k = 0; k < molecules.Length; k++)
                 {
                     set_Renamed.addAtomContainer(molecules[k]);
                 }
             }
             return;
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary> Method that saturates a molecule by adding explicit hydrogens.
        /// In order to get coordinates for these Hydrogens, you need to
        /// remember the average bondlength of you molecule (coordinates for
        /// all atoms should be available) by using
        /// double bondLength = GeometryTools.getBondLengthAverage(atomContainer);
        /// and then use this method here and then use
        /// org.openscience.cdk.HydrogenPlacer(atomContainer, bondLength);
        ///
        /// </summary>
        /// <param name="molecule"> Molecule to saturate
        /// </param>
        /// <cdk.keyword>           hydrogen, adding </cdk.keyword>
        /// <cdk.keyword>           explicit hydrogen </cdk.keyword>
        public virtual IAtomContainer addExplicitHydrogensToSatisfyValency(IMolecule molecule)
        {
            //logger.debug("Start of addExplicitHydrogensToSatisfyValency");
            ISetOfMolecules moleculeSet = ConnectivityChecker.partitionIntoMolecules(molecule);

            IMolecule[]    molecules             = moleculeSet.Molecules;
            IAtomContainer changedAtomsAndBonds  = molecule.Builder.newAtomContainer();
            IAtomContainer intermediateContainer = null;

            for (int k = 0; k < molecules.Length; k++)
            {
                IMolecule molPart = molecules[k];
                IAtom[]   atoms   = molPart.Atoms;
                for (int i = 0; i < atoms.Length; i++)
                {
                    intermediateContainer = addHydrogensToSatisfyValency(molPart, atoms[i], molecule);
                    changedAtomsAndBonds.add(intermediateContainer);
                }
            }
            //logger.debug("End of addExplicitHydrogensToSatisfyValency");
            return(changedAtomsAndBonds);
        }
Ejemplo n.º 7
0
        /// <summary>  Parses a SMILES string and returns a Molecule object.
        ///
        /// </summary>
        /// <param name="smiles">                     A SMILES string
        /// </param>
        /// <returns>                             A Molecule representing the constitution
        /// given in the SMILES string
        /// </returns>
        /// <exception cref="InvalidSmilesException"> Exception thrown when the SMILES string
        /// is invalid
        /// </exception>
        public virtual Molecule parseSmiles(System.String smiles)
        {
            //logger.debug("parseSmiles()...");
            Bond bond = null;

            nodeCounter    = 0;
            bondStatus     = 0;
            bondIsAromatic = false;
            bool bondExists = true;

            thisRing      = -1;
            currentSymbol = null;
            molecule      = new Molecule();
            position      = 0;
            // we don't want more than 1024 rings
            rings     = new Atom[1024];
            ringbonds = new double[1024];
            for (int f = 0; f < 1024; f++)
            {
                rings[f]     = null;
                ringbonds[f] = -1;
            }

            char mychar = 'X';

            char[] chars    = new char[1];
            Atom   lastNode = null;

            System.Collections.ArrayList atomStack = new System.Collections.ArrayList();
            System.Collections.ArrayList bondStack = new System.Collections.ArrayList();
            Atom atom = null;

            do
            {
                try
                {
                    mychar = smiles[position];
                    //logger.debug("");
                    //logger.debug("Processing: " + mychar);
                    if (lastNode != null)
                    {
                        //logger.debug("Lastnode: ", lastNode.GetHashCode());
                    }
                    if ((mychar >= 'A' && mychar <= 'Z') || (mychar >= 'a' && mychar <= 'z') || (mychar == '*'))
                    {
                        status = 1;
                        //logger.debug("Found a must-be 'organic subset' element");
                        // only 'organic subset' elements allowed
                        atom = null;
                        if (mychar == '*')
                        {
                            currentSymbol = "*";
                            atom          = new PseudoAtom("*");
                        }
                        else
                        {
                            currentSymbol = getSymbolForOrganicSubsetElement(smiles, position);
                            if (currentSymbol != null)
                            {
                                if (currentSymbol.Length == 1)
                                {
                                    if (!(currentSymbol.ToUpper()).Equals(currentSymbol))
                                    {
                                        currentSymbol      = currentSymbol.ToUpper();
                                        atom               = new Atom(currentSymbol);
                                        atom.Hybridization = CDKConstants.HYBRIDIZATION_SP2;
                                    }
                                    else
                                    {
                                        atom = new Atom(currentSymbol);
                                    }
                                }
                                else
                                {
                                    atom = new Atom(currentSymbol);
                                }
                                //logger.debug("Made atom: ", atom);
                            }
                            else
                            {
                                throw new InvalidSmilesException("Found element which is not a 'organic subset' element. You must " + "use [" + mychar + "].");
                            }
                        }

                        molecule.addAtom(atom);
                        //logger.debug("Adding atom ", atom.GetHashCode());
                        if ((lastNode != null) && bondExists)
                        {
                            //logger.debug("Creating bond between ", atom.Symbol, " and ", lastNode.Symbol);
                            bond = new Bond(atom, lastNode, bondStatus);
                            if (bondIsAromatic)
                            {
                                bond.setFlag(CDKConstants.ISAROMATIC, true);
                            }
                            molecule.addBond(bond);
                        }
                        bondStatus = CDKConstants.BONDORDER_SINGLE;
                        lastNode   = atom;
                        nodeCounter++;
                        position       = position + currentSymbol.Length;
                        bondExists     = true;
                        bondIsAromatic = false;
                    }
                    else if (mychar == '=')
                    {
                        position++;
                        if (status == 2 || smiles.Length == position + 1 || !(smiles[position] >= '0' && smiles[position] <= '9'))
                        {
                            bondStatus = CDKConstants.BONDORDER_DOUBLE;
                        }
                        else
                        {
                            bondStatusForRingClosure = CDKConstants.BONDORDER_DOUBLE;
                        }
                    }
                    else if (mychar == '#')
                    {
                        position++;
                        if (status == 2 || smiles.Length == position + 1 || !(smiles[position] >= '0' && smiles[position] <= '9'))
                        {
                            bondStatus = CDKConstants.BONDORDER_TRIPLE;
                        }
                        else
                        {
                            bondStatusForRingClosure = CDKConstants.BONDORDER_TRIPLE;
                        }
                    }
                    else if (mychar == '(')
                    {
                        atomStack.Add(lastNode);
                        //logger.debug("Stack:");
                        System.Collections.IEnumerator ses = atomStack.GetEnumerator();
                        //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' 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_javautilEnumerationhasMoreElements'"
                        while (ses.MoveNext())
                        {
                            //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' 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_javautilEnumerationnextElement'"
                            Atom a = (Atom)ses.Current;
                            //logger.debug("", a.GetHashCode());
                        }
                        //logger.debug("------");
                        bondStack.Add((double)bondStatus);
                        position++;
                    }
                    else if (mychar == ')')
                    {
                        lastNode = (Atom)SupportClass.StackSupport.Pop(atomStack);
                        //logger.debug("Stack:");
                        System.Collections.IEnumerator ses = atomStack.GetEnumerator();
                        //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' 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_javautilEnumerationhasMoreElements'"
                        while (ses.MoveNext())
                        {
                            //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' 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_javautilEnumerationnextElement'"
                            Atom a = (Atom)ses.Current;
                            //logger.debug("", a.GetHashCode());
                        }
                        //logger.debug("------");
                        bondStatus = ((System.Double)SupportClass.StackSupport.Pop(bondStack));
                        position++;
                    }
                    else if (mychar >= '0' && mychar <= '9')
                    {
                        status        = 2;
                        chars[0]      = mychar;
                        currentSymbol = new System.String(chars);
                        thisRing      = (System.Int32.Parse(currentSymbol));
                        handleRing(lastNode);
                        position++;
                    }
                    else if (mychar == '%')
                    {
                        currentSymbol = getRingNumber(smiles, position);
                        thisRing      = (System.Int32.Parse(currentSymbol));
                        handleRing(lastNode);
                        position += currentSymbol.Length + 1;
                    }
                    else if (mychar == '[')
                    {
                        currentSymbol = getAtomString(smiles, position);
                        atom          = assembleAtom(currentSymbol);
                        molecule.addAtom(atom);
                        //logger.debug("Added atom: ", atom);
                        if (lastNode != null && bondExists)
                        {
                            bond = new Bond(atom, lastNode, bondStatus);
                            if (bondIsAromatic)
                            {
                                bond.setFlag(CDKConstants.ISAROMATIC, true);
                            }
                            molecule.addBond(bond);
                            //logger.debug("Added bond: ", bond);
                        }
                        bondStatus     = CDKConstants.BONDORDER_SINGLE;
                        bondIsAromatic = false;
                        lastNode       = atom;
                        nodeCounter++;
                        position = position + currentSymbol.Length + 2;
                        // plus two for [ and ]
                        bondExists = true;
                    }
                    else if (mychar == '.')
                    {
                        bondExists = false;
                        position++;
                    }
                    else if (mychar == '-')
                    {
                        bondExists = true;
                        // a simple single bond
                        position++;
                    }
                    else if (mychar == ':')
                    {
                        bondExists     = true;
                        bondIsAromatic = true;
                        position++;
                    }
                    else if (mychar == '/' || mychar == '\\')
                    {
                        //logger.warn("Ignoring stereo information for double bond");
                        position++;
                    }
                    else if (mychar == '@')
                    {
                        if (position < smiles.Length - 1 && smiles[position + 1] == '@')
                        {
                            position++;
                        }
                        //logger.warn("Ignoring stereo information for atom");
                        position++;
                    }
                    else
                    {
                        throw new InvalidSmilesException("Unexpected character found: " + mychar);
                    }
                }
                catch (InvalidSmilesException exc)
                {
                    //logger.error("InvalidSmilesException while parsing char (in parseSmiles()): " + mychar);
                    //logger.debug(exc);
                    throw exc;
                }
                catch (System.Exception exception)
                {
                    //logger.error("Error while parsing char: " + mychar);
                    //logger.debug(exception);
                    throw new InvalidSmilesException("Error while parsing char: " + mychar);
                }
                //logger.debug("Parsing next char");
            }while (position < smiles.Length);

            // add implicit hydrogens
            try
            {
                //logger.debug("before H-adding: ", molecule);
                hAdder.addImplicitHydrogensToSatisfyValency(molecule);
                //logger.debug("after H-adding: ", molecule);
            }
            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'"
                //logger.error("Error while calculation Hcount for SMILES atom: ", exception.Message);
            }

            // setup missing bond orders
            try
            {
                valencyChecker.saturate(molecule);
                //logger.debug("after adding missing bond orders: ", molecule);
            }
            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'"
                //logger.error("Error while calculation Hcount for SMILES atom: ", exception.Message);
            }

            // conceive aromatic perception
            IMolecule[] moleculeSet = ConnectivityChecker.partitionIntoMolecules(molecule).Molecules;
            //logger.debug("#mols ", moleculeSet.Length);
            for (int i = 0; i < moleculeSet.Length; i++)
            {
                //logger.debug("mol: ", moleculeSet[i]);
                try
                {
                    valencyChecker.saturate(moleculeSet[i]);
                    //logger.debug(" after saturation: ", moleculeSet[i]);
                    if (HueckelAromaticityDetector.detectAromaticity(moleculeSet[i]))
                    {
                        //logger.debug("Structure is aromatic...");
                    }
                }
                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'"
                    //logger.error("Could not perceive aromaticity: ", exception.Message);
                    //logger.debug(exception);
                }
            }

            return(molecule);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// BuildFingerprints
        /// </summary>
        public static void BuildFingerprints(
            UniChemData icd)
        {
            IAtomContainer mol, mol2;
            InChIGenerator ig = null;
            int            acc, aci, ci;

            icd.Children.Clear();

            string parentFIKHB = icd.GetFIKHB();

            DateTime t0 = DateTime.Now;

            mol = CdkMol.InChIToAtomContainer(icd.InChIString);

            icd.CanonSmiles = CdkMol.AtomContainerToSmiles(mol);

            InChIToAtomContainerTime += TimeOfDay.Delta(ref t0);

            BitSetFingerprint fp =             // generate a fingerprint
                                   CdkFingerprint.BuildBitSetFingerprint(mol, FingerprintType.MACCS, -1, -1);

            BuildFinterprintTime1 += TimeOfDay.Delta(ref t0);

            icd.Fingerprint = fp;

            if (ConnectivityChecker.isConnected(mol))
            {
                return;                                                   // single fragment
            }
            InChIGeneratorFactory igf = InChIGeneratorFactory.getInstance();

            AtomContainerSet acs = (AtomContainerSet)ConnectivityChecker.partitionIntoMolecules(mol);

            PartitionIntoMoleculesTime += TimeOfDay.Delta(ref t0);

            acc = acs.getAtomContainerCount();

            for (aci = 0; aci < acc; aci++)
            {
                mol2 = acs.getAtomContainer(aci);
                GetAtomContainerTime += TimeOfDay.Delta(ref t0);

                ig = igf.getInChIGenerator(mol2);
                if (!IsAcceptableInchiStatus(ig))
                {
                    continue;
                }

                string childKey   = ig.getInchiKey();
                string childFIKHB = UniChemUtil.GetFIKHB(childKey);
                InChIGeneratorTime += TimeOfDay.Delta(ref t0);

                fp =                 // generate a fingerprint for the fragment
                     CdkFingerprint.BuildBitSetFingerprint(mol2, FingerprintType.MACCS, -1, -1);

                BuildFinterprintTime2 += TimeOfDay.Delta(ref t0);

                for (ci = 0; ci < icd.Children.Count; ci++)                 // see if a dup child
                {
                    if (icd.Children[ci].ChildFIKHB == childFIKHB)
                    {
                        break;
                    }
                }
                if (ci < icd.Children.Count)
                {
                    continue;                                          // skip if dup
                }
                UniChemFIKHBHierarchy fikhbHier = new UniChemFIKHBHierarchy();
                fikhbHier.ParentFIKHB = parentFIKHB;
                fikhbHier.ChildFIKHB  = childFIKHB;
                fikhbHier.CanonSmiles = CdkMol.AtomContainerToSmiles(mol2);
                fikhbHier.Fingerprint = fp;

                icd.Children.Add(fikhbHier);
            }

            return;
        }