Ejemplo n.º 1
0
        public void TestNewPseudoAtom_String_Point3d()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom("Foo", new Vector3(1, 2, 3));

            Assert.IsNotNull(atom);
        }
Ejemplo n.º 2
0
        public bool AreRootAtomsDefined()
        {
            foreach (var rgpNum in RGroupDefinitions.Keys)
            {
                bool represented = false;
                foreach (var rootAtom in this.RootStructure.Atoms)
                {
                    if (rootAtom is IPseudoAtom && rootAtom.Symbol.StartsWithChar('R'))
                    {
                        IPseudoAtom pseudo = (IPseudoAtom)rootAtom;
                        if (pseudo.Label.Length > 1)
                        {
                            int rootAtomRgrpNumber = int.Parse(pseudo.Label.Substring(1), NumberFormatInfo.InvariantInfo);
                            if (rootAtomRgrpNumber == rgpNum)
                            {
                                represented = true;
                                goto Break_RootLoop;
                            }
                        }
                    }
                }
Break_RootLoop:
                ;
                if (!represented)
                {
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        public void TestRGPLine()
        {
            StringWriter   writer   = new StringWriter();
            IAtomContainer molecule = builder.NewAtomContainer();
            IPseudoAtom    atom1    = builder.NewPseudoAtom();

            atom1.Symbol = "R";
            atom1.Label  = "R12";

            IAtom atom2 = builder.NewAtom("C");
            IBond bond  = builder.NewBond(atom1, atom2);

            IPseudoAtom atom3 = builder.NewPseudoAtom();

            atom3.Symbol = "A";
            atom3.Label  = "A";
            IBond bond2 = builder.NewBond(atom3, atom2);

            molecule.Atoms.Add(atom1);
            molecule.Atoms.Add(atom2);
            molecule.Atoms.Add(atom3);
            molecule.Bonds.Add(bond);
            molecule.Bonds.Add(bond2);

            MDLV2000Writer mdlWriter = new MDLV2000Writer(writer);

            mdlWriter.Write(molecule);
            mdlWriter.Close();
            string output = writer.ToString();

            Assert.IsTrue(-1 != output.IndexOf("R#"), "Test for R#");
            Assert.IsTrue(-1 != output.IndexOf("M  RGP  1   1  12"), "Test for RGP line");
        }
Ejemplo n.º 4
0
        public void TestNewPseudoAtom_String()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom("Foo");

            Assert.IsNotNull(atom);
        }
Ejemplo n.º 5
0
        public void TestNewPseudoAtom_IAtom()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom(builder.NewAtom());

            Assert.IsNotNull(atom);
        }
Ejemplo n.º 6
0
        public void TestNewPseudoAtom_IElement()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            IPseudoAtom        atom    = builder.NewPseudoAtom(ChemicalElement.R);

            Assert.IsNotNull(atom);
        }
Ejemplo n.º 7
0
 private void SerializePseudoAtomFields(INode rdfAtom, IPseudoAtom atom)
 {
     SerializeAtomFields(rdfAtom, atom);
     if (atom.Label != null)
     {
         g.Assert(new Triple(rdfAtom, P_HASLABEL, g.CreateLiteralNode(atom.Label)));
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Safely access the label of a pseudo atom. If the label is null or empty, the default label is
        /// returned.
        /// </summary>
        /// <param name="atom">the pseudo</param>
        /// <returns>pseudo label</returns>
        public static string AccessPseudoLabel(IPseudoAtom atom, string defaultLabel)
        {
            var label = atom.Label;

            if (!string.IsNullOrEmpty(label))
            {
                return(label);
            }
            return(defaultLabel);
        }
Ejemplo n.º 9
0
 public void TestPseudoAtomLabels()
 {
     using (var ins = ResourceLoader.GetAsStream("NCDK.Data.MDL.pseudoatomsv3000.mol"))
         using (MDLV3000Reader reader = new MDLV3000Reader(ins))
         {
             IAtomContainer molecule = builder.NewAtomContainer();
             molecule = reader.Read(molecule);
             reader.Close();
             Assert.IsTrue(molecule.Atoms[9] is IPseudoAtom);
             Assert.AreEqual("R", molecule.Atoms[9].Symbol);
             IPseudoAtom pa = (IPseudoAtom)molecule.Atoms[9];
             Assert.AreEqual("Leu", pa.Label);
         }
 }
Ejemplo n.º 10
0
        public void TestWritePseudoAtoms_nullLabel()
        {
            IAtomContainer container = builder.NewAtomContainer();

            IAtom       c1       = builder.NewAtom("C");
            IPseudoAtom nullAtom = builder.NewPseudoAtom("");

            nullAtom.Label = null;

            container.Atoms.Add(c1);
            container.Atoms.Add(nullAtom);

            StringWriter   sw     = new StringWriter();
            MDLV2000Writer writer = new MDLV2000Writer(sw);

            writer.Write(container);
            writer.Close();

            string output = sw.ToString();

            Assert.IsTrue(output.Contains("R"));
        }
Ejemplo n.º 11
0
        public override void TestSetExactMass_Double()
        {
            IPseudoAtom atom = (IPseudoAtom)NewChemObject();

            atom.ExactMass = 12.001;
        }
Ejemplo n.º 12
0
        /// <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);
        }
Ejemplo n.º 13
0
        // Creates a CxSmilesState from a molecule with atom labels, repeat units, multi-center bonds etc
        private static CxSmilesState GetCxSmilesState(SmiFlavors flavour, IAtomContainer mol)
        {
            CxSmilesState state = new CxSmilesState
            {
                atomCoords = new List <double[]>(),
                coordFlag  = false
            };

            // set the atom labels, values, and coordinates,
            // and build the atom->idx map required by other parts
            var atomidx = new Dictionary <IAtom, int>();

            for (int idx = 0; idx < mol.Atoms.Count; idx++)
            {
                IAtom atom = mol.Atoms[idx];
                if (atom is IPseudoAtom)
                {
                    if (state.atomLabels == null)
                    {
                        state.atomLabels = new SortedDictionary <int, string>();
                    }

                    IPseudoAtom pseudo = (IPseudoAtom)atom;
                    if (pseudo.AttachPointNum > 0)
                    {
                        state.atomLabels[idx] = "_AP" + pseudo.AttachPointNum;
                    }
                    else
                    {
                        if (!"*".Equals(pseudo.Label, StringComparison.Ordinal))
                        {
                            state.atomLabels[idx] = pseudo.Label;
                        }
                    }
                }
                object comment = atom.GetProperty <object>(CDKPropertyName.Comment);
                if (comment != null)
                {
                    if (state.atomValues == null)
                    {
                        state.atomValues = new SortedDictionary <int, string>();
                    }
                    state.atomValues[idx] = comment.ToString();
                }
                atomidx[atom] = idx;

                var p2 = atom.Point2D;
                var p3 = atom.Point3D;

                if (SmiFlavorTool.IsSet(flavour, SmiFlavors.Cx2dCoordinates) && p2 != null)
                {
                    state.atomCoords.Add(new double[] { p2.Value.X, p2.Value.Y, 0 });
                    state.coordFlag = true;
                }
                else if (SmiFlavorTool.IsSet(flavour, SmiFlavors.Cx3dCoordinates) && p3 != null)
                {
                    state.atomCoords.Add(new double[] { p3.Value.X, p3.Value.Y, p3.Value.Z });
                    state.coordFlag = true;
                }
                else if (SmiFlavorTool.IsSet(flavour, SmiFlavors.CxCoordinates))
                {
                    state.atomCoords.Add(new double[3]);
                }
            }

            if (!state.coordFlag)
            {
                state.atomCoords = null;
            }

            // radicals
            if (mol.SingleElectrons.Count > 0)
            {
                state.atomRads = new SortedDictionary <int, CxSmilesState.Radical>();
                foreach (ISingleElectron radical in mol.SingleElectrons)
                {
                    // 0->1, 1->2, 2->3
                    if (!state.atomRads.TryGetValue(EnsureNotNull(atomidx[radical.Atom]), out CxSmilesState.Radical val))
                    {
                        val = CxSmilesState.Radical.Monovalent;
                    }
                    else if (val == CxSmilesState.Radical.Monovalent)
                    {
                        val = CxSmilesState.Radical.Divalent;
                    }
                    else if (val == CxSmilesState.Radical.Divalent)
                    {
                        val = CxSmilesState.Radical.Trivalent;
                    }
                    else if (val == CxSmilesState.Radical.Trivalent)
                    {
                        throw new ArgumentException("Invalid radical state, can not be more than trivalent");
                    }

                    state.atomRads[atomidx[radical.Atom]] = val;
                }
            }

            var sgroups = mol.GetCtabSgroups();

            if (sgroups != null)
            {
                state.sgroups     = new List <CxSmilesState.PolymerSgroup>();
                state.positionVar = new SortedDictionary <int, IList <int> >();
                foreach (Sgroup sgroup in sgroups)
                {
                    switch (sgroup.Type)
                    {
                    // polymer SRU
                    case SgroupType.CtabStructureRepeatUnit:
                    case SgroupType.CtabMonomer:
                    case SgroupType.CtabMer:
                    case SgroupType.CtabCopolymer:
                    case SgroupType.CtabCrossLink:
                    case SgroupType.CtabModified:
                    case SgroupType.CtabMixture:
                    case SgroupType.CtabFormulation:
                    case SgroupType.CtabAnyPolymer:
                    case SgroupType.CtabGeneric:
                    case SgroupType.CtabComponent:
                    case SgroupType.CtabGraft:
                        string supscript = (string)sgroup.GetValue(SgroupKey.CtabConnectivity);
                        state.sgroups.Add(new CxSmilesState.PolymerSgroup(GetSgroupPolymerKey(sgroup),
                                                                          ToAtomIdxs(sgroup.Atoms, atomidx),
                                                                          sgroup.Subscript,
                                                                          supscript));
                        break;

                    case SgroupType.ExtMulticenter:
                        IAtom        beg   = null;
                        List <IAtom> ends  = new List <IAtom>();
                        ISet <IBond> bonds = sgroup.Bonds;
                        if (bonds.Count != 1)
                        {
                            throw new ArgumentException("Multicenter Sgroup in inconsistent state!");
                        }
                        IBond bond = bonds.First();
                        foreach (IAtom atom in sgroup.Atoms)
                        {
                            if (bond.Contains(atom))
                            {
                                if (beg != null)
                                {
                                    throw new ArgumentException("Multicenter Sgroup in inconsistent state!");
                                }
                                beg = atom;
                            }
                            else
                            {
                                ends.Add(atom);
                            }
                        }
                        state.positionVar[EnsureNotNull(atomidx[beg])] =
                            ToAtomIdxs(ends, atomidx);
                        break;

                    case SgroupType.CtabAbbreviation:
                    case SgroupType.CtabMultipleGroup:
                        // display shortcuts are not output
                        break;

                    case SgroupType.CtabData:
                        // can be generated but currently ignored
                        break;

                    default:
                        throw new NotSupportedException("Unsupported Sgroup Polymer");
                    }
                }
            }

            return(state);
        }