Exemplo n.º 1
0
        // private functions

        /// <summary>
        /// Reads a ChemFile object from input.
        /// </summary>
        /// <returns>ChemFile with the content read from the input</returns>
        private IChemFile ReadChemFile(IChemFile cf)
        {
            // have to do stuff here
            try
            {
                string line = null;
                while ((line = input.ReadLine()) != null)
                {
                    if (line.StartsWith("INChI=", StringComparison.Ordinal) || line.StartsWith("InChI=", StringComparison.Ordinal))
                    {
                        // ok, the fun starts
                        cf = cf.Builder.NewChemFile();
                        // ok, we need to parse things like:
                        // INChI=1.12Beta/C6H6/c1-2-4-6-5-3-1/h1-6H
                        string INChI = line.Substring(6);
                        var    tok   = Strings.Tokenize(INChI, '/');
                        // ok, we expect 4 tokens
                        // tok[0]; // 1.12Beta not stored since never used
                        string formula     = tok[1]; // C6H6
                        string connections = null;
                        if (tok.Count > 2)
                        {
                            connections = tok[2].Substring(1); // 1-2-4-6-5-3-1
                        }
                        //final string hydrogens = tokenizer.NextToken().Substring(1); // 1-6H

                        IAtomContainer parsedContent = InChIContentProcessorTool.ProcessFormula(
                            cf.Builder.NewAtomContainer(), formula);
                        if (connections != null)
                        {
                            InChIContentProcessorTool.ProcessConnections(connections, parsedContent, -1);
                        }

                        var moleculeSet = cf.Builder.NewAtomContainerSet();
                        moleculeSet.Add(cf.Builder.NewAtomContainer(parsedContent));
                        var model = cf.Builder.NewChemModel();
                        model.MoleculeSet = moleculeSet;
                        var sequence = cf.Builder.NewChemSequence();
                        sequence.Add(model);
                        cf.Add(sequence);
                    }
                }
            }
            catch (Exception exception)
            {
                if (exception is IOException || exception is ArgumentException)
                {
                    Console.Error.WriteLine(exception.StackTrace);
                    throw new CDKException($"Error while reading INChI file: {exception.Message}", exception);
                }
                else
                {
                    throw;
                }
            }
            return(cf);
        }
Exemplo n.º 2
0
 public override void EndElement(XElement element)
 {
     Debug.WriteLine($"end element: {element.ToString()}");
     if (string.Equals("identifier", element.Name.LocalName, StringComparison.Ordinal))
     {
         if (tautomer != null)
         {
             // ok, add tautomer
             setOfMolecules.Add(tautomer);
             chemModel.MoleculeSet = setOfMolecules;
             chemSequence.Add(chemModel);
         }
     }
     else if (string.Equals("formula", element.Name.LocalName, StringComparison.Ordinal))
     {
         if (tautomer != null)
         {
             Trace.TraceInformation("Parsing <formula> chars: ", element.Value);
             tautomer = builder.NewAtomContainer(
                 InChIContentProcessorTool.ProcessFormula(setOfMolecules.Builder.NewAtomContainer(), element.Value));
         }
         else
         {
             Trace.TraceWarning("Cannot set atom info for empty tautomer");
         }
     }
     else if (string.Equals("connections", element.Name.LocalName, StringComparison.Ordinal))
     {
         if (tautomer != null)
         {
             Trace.TraceInformation("Parsing <connections> chars: ", element.Value);
             InChIContentProcessorTool.ProcessConnections(element.Value, tautomer, -1);
         }
         else
         {
             Trace.TraceWarning("Cannot set dbond info for empty tautomer");
         }
     }
     else
     {
         // skip all other elements
     }
 }