Esempio n. 1
0
        /// <summary>
        /// Serializes the IChemObject to CML and redirects it to the output Writer.
        /// </summary>
        /// <param name="obj">A Molecule of AtomContaineSet object</param>
        public override void Write(IChemObject obj)
        {
            if (!(obj is IAtomContainer) && !(obj is IEnumerableChemObject <IAtomContainer>) &&
                !(obj is IReaction) && !(obj is IReactionSet) &&
                !(obj is IChemSequence) && !(obj is IChemModel) &&
                !(obj is IChemFile) && !(obj is ICrystal) && !(obj is IAtom) &&
                !(obj is IBond))
            {
                throw new CDKException("Cannot write this unsupported IChemObject: " + obj.GetType().Name);
            }

            Debug.WriteLine($"Writing obj in CML of type: {obj.GetType().Name}");

            CustomizeJob();

            Convertor convertor = new Convertor(cmlIds.IsSet,
                                                (namespacePrefix.Setting.Length > 0) ? namespacePrefix.Setting : null);

            // adding the customizer
            if (customizers != null)
            {
                foreach (var customizer in customizers)
                {
                    convertor.RegisterCustomizer(customizer);
                }
            }

            // now convert the obj
            XElement root = null;

            if (obj is IPDBPolymer)
            {
                root = convertor.CDKPDBPolymerToCMLMolecule((IPDBPolymer)obj);
            }
            else if (obj is ICrystal)
            {
                root = convertor.CDKCrystalToCMLMolecule((ICrystal)obj);
            }
            else if (obj is IAtom)
            {
                root = convertor.CDKAtomToCMLAtom(null, (IAtom)obj);
            }
            else if (obj is IBond)
            {
                root = convertor.CDKJBondToCMLBond((IBond)obj);
            }
            else if (obj is IReaction)
            {
                root = convertor.CDKReactionToCMLReaction((IReaction)obj);
            }
            else if (obj is IReactionScheme)
            {
                root = convertor.CDKReactionSchemeToCMLReactionSchemeAndMoleculeList((IReactionScheme)obj);
            }
            else if (obj is IReactionSet)
            {
                root = convertor.CDKReactionSetToCMLReactionList((IReactionSet)obj);
            }
            else if (obj is IEnumerableChemObject <IAtomContainer> )
            {
                root = convertor.CDKAtomContainerSetToCMLList((IEnumerableChemObject <IAtomContainer>)obj);
            }
            else if (obj is IChemSequence)
            {
                root = convertor.CDKChemSequenceToCMLList((IChemSequence)obj);
            }
            else if (obj is IChemModel)
            {
                root = convertor.CDKChemModelToCMLList((IChemModel)obj);
            }
            else if (obj is IAtomContainer)
            {
                root = convertor.CDKAtomContainerToCMLMolecule((IAtomContainer)obj);
            }
            else if (obj is IChemFile)
            {
                root = convertor.CDKChemFileToCMLList((IChemFile)obj);
            }

            var encoding = "ISO-8859-1";
            var se       = new XmlWriterSettings
            {
                Encoding = Encoding.GetEncoding(encoding)
            };

            if (indent.IsSet)
            {
                Trace.TraceInformation("Indenting XML output");
                se.Indent      = true;
                se.IndentChars = new string(' ', 2);
            }

            XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

            if (schemaInstanceOutput.IsSet)
            {
                root.SetAttributeValue(XNamespace.Xmlns + "xsi", xsi.NamespaceName);
                root.SetAttributeValue(xsi + "schemaLocation", "http://www.xml-cml.org/schema/cml2/core " + instanceLocation.Setting);
            }

            var       de  = new XDeclaration(null, encoding, null);
            XDocument doc = new XDocument(de, root);

            doc.Save(output);
        }
Esempio n. 2
0
 /// <summary>
 /// Writes a <see cref="IChemObject"/> to the MDL molfile formated output.
 /// </summary>
 /// <param name="obj">Best choice is a set of molecules</param>
 /// <see cref="IChemObjectWriter.Write(IChemObject)"/>
 public override void Write(IChemObject obj)
 {
     try
     {
         XProcessingInstruction pi = new XProcessingInstruction("xml-stylesheet", "href=\"http://www.w3.org/2000/08/w3c-synd/style.css\" type=\"text/css\"");
         var doc = new XDocument();
         doc.Add(pi);
         var rdfElement = new XElement(NS_RSS10 + "RDF");
         rdfElement.SetAttributeValue(XNamespace.Xmlns + "rdf", NS_RSS10);
         rdfElement.SetAttributeValue(XNamespace.Xmlns + "mn", "http://usefulinc.com/rss/manifest/");
         rdfElement.SetAttributeValue(XNamespace.Xmlns + "dc", NS_DCELEMENTS);
         rdfElement.SetAttributeValue(XNamespace.Xmlns + "cml", Convertor.NamespaceCML);
         doc.Add(rdfElement);
         var channelElement = new XElement(NS_RSS10 + "channel");
         var titleElement   = new XElement(NS_RSS10 + "title")
         {
             Value = this.Title
         };
         channelElement.Add(titleElement);
         var linkElement = new XElement(NS_RSS10 + "link")
         {
             Value = Link
         };
         channelElement.Add(linkElement);
         var descriptionElement = new XElement(NS_RSS10 + "description")
         {
             Value = Description
         };
         channelElement.Add(descriptionElement);
         var publisherElement = new XElement(NS_DCELEMENTS + "publisher")
         {
             Value = Publisher
         };
         channelElement.Add(publisherElement);
         var creatorElement = new XElement(NS_DCELEMENTS + "creator")
         {
             Value = Creator
         };
         channelElement.Add(creatorElement);
         var imageElement = new XElement(NS_RSS10 + "image");
         imageElement.SetAttributeValue(NS_RDF + "resource", ImageLink);
         channelElement.Add(imageElement);
         var itemsElement = new XElement(NS_RSS10 + "items");
         var seqElement   = new XElement(NS_RDF + "seq");
         itemsElement.Add(seqElement);
         channelElement.Add(itemsElement);
         channelElement.SetAttributeValue(NS_RDF + "about", About);
         rdfElement.Add(channelElement);
         var list = new List <IChemObject>();
         if (obj is IChemObjectSet <IAtomContainer> )
         {
             for (int i = 0; i < ((IChemObjectSet <IAtomContainer>)obj).Count; i++)
             {
                 list.Add(((IChemObjectSet <IAtomContainer>)obj)[i]);
             }
         }
         else
         {
             list.Add(obj);
         }
         for (int i = 0; i < list.Count; i++)
         {
             IChemObject chemObject  = (IChemObject)list[i];
             var         itemElement = new XElement(NS_RSS10 + "item");
             string      easylink    = LinkMap[chemObject];
             if (easylink != null)
             {
                 itemElement.SetAttributeValue(NS_RDF + "about", easylink);
             }
             var link2Element = new XElement(NS_RSS10 + "link")
             {
                 Value = easylink
             };
             itemElement.Add(link2Element);
             string title = chemObject.GetProperty <string>(CDKPropertyName.Title);
             if (TitleMap[chemObject] != null)
             {
                 var title2Element = new XElement(NS_RSS10 + "title")
                 {
                     Value = TitleMap[chemObject]
                 };
                 itemElement.Add(title2Element);
             }
             if (title != null)
             {
                 var description2Element = new XElement(NS_RSS10 + "description")
                 {
                     Value = title
                 };
                 itemElement.Add(description2Element);
                 var subjectElement = new XElement(NS_DCELEMENTS + "subject")
                 {
                     Value = title
                 };
                 itemElement.Add(subjectElement);
             }
             if (DateMap[chemObject] != null)
             {
                 var dateElement = new XElement(NS_DCELEMENTS + "date")
                 {
                     Value = DateMap[chemObject].ToString("yyyy-MM-dd'T'HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + TimeZone
                 };
                 itemElement.Add(dateElement);
             }
             var creator2Element = new XElement(NS_DCELEMENTS + "creator")
             {
                 Value = CreatorMap[chemObject]
             };
             itemElement.Add(creator2Element);
             // add the InChI to the CMLRSS feed
             if (InChIMap[chemObject] != null)
             {
                 var inchiElement = new XElement(NS_CML + "identifier")
                 {
                     Value = InChIMap[chemObject]
                 };
                 itemElement.Add(inchiElement);
             }
             CMLElement root      = null;
             var        convertor = new Convertor(true, null);
             obj = list[i];
             if (obj is ICrystal)
             {
                 root = convertor.CDKCrystalToCMLMolecule((ICrystal)obj);
             }
             else if (obj is IAtomContainer)
             {
                 root = convertor.CDKAtomContainerToCMLMolecule((IAtomContainer)obj);
             }
             else if (obj is IAtom)
             {
                 root = convertor.CDKAtomToCMLAtom(null, (IAtom)obj);
             }
             else if (obj is IBond)
             {
                 root = convertor.CDKJBondToCMLBond((IBond)obj);
             }
             else if (obj is IReaction)
             {
                 root = convertor.CDKReactionToCMLReaction((IReaction)obj);
             }
             else if (obj is IReactionSet)
             {
                 root = convertor.CDKReactionSetToCMLReactionList((IReactionSet)obj);
             }
             else if (obj is IChemObjectSet <IAtomContainer> )
             {
                 root = convertor.CDKAtomContainerSetToCMLList((IChemObjectSet <IAtomContainer>)obj);
             }
             else if (obj is IChemSequence)
             {
                 root = convertor.CDKChemSequenceToCMLList((IChemSequence)obj);
             }
             else if (obj is IChemModel)
             {
                 root = convertor.CDKChemModelToCMLList((IChemModel)obj);
             }
             else if (obj is IChemFile)
             {
                 root = convertor.CDKChemFileToCMLList((IChemFile)obj);
             }
             else
             {
                 throw new CDKException($"Unsupported chemObject: {obj.GetType().Name}");
             }
             itemElement.Add(root);
             if (MultiMap[chemObject] != null)
             {
                 var coll = MultiMap[chemObject];
                 foreach (var e in coll)
                 {
                     itemElement.Add(e);
                 }
             }
             rdfElement.Add(itemElement);
             var imageElement2 = new XElement(NS_RDF + "li");
             imageElement2.SetAttributeValue(NS_RDF + "resource", LinkMap[chemObject]);
             seqElement.Add(imageElement2);
         }
         writer.Write(doc.ToString());
         writer.Flush();
     }
     catch (IOException ex)
     {
         throw new CDKException(ex.Message, ex);
     }
 }