Chemistry in XML

An Example Application of XML in Mathematica

With Mathematica 4.2, it is possible to import chemical information that is presented in an XML format and use Mathematica to visualize that information.  The specific form of XML used here is MoDL (pronounced Model), or Molecular Dynamics Language.  Although MoDL was not created by Wolfram Research, Inc., Mathematica is able to read the data and use it.  This is a powerful example of why XML is useful.  For more information about the specifics of this form of XML, including the DTD, see the hyperlink in the Reference section.

Initialization

<< Graphics`Shapes`

ModLToGraphics3D[modl_] := 
      Block[{defs = 
            Cases[modl, XMLElement["DEFINE", __], 
              Infinity], 
body = 
            First[Cases[modl, XMLElement["body", __], 
                Infinity]], 
gr, moldef, themols, 
          theatoms}, 
ProcessDefinition /@ 
          defs; 
themols = 
          Flatten@Cases[body, 
              XMLElement["molecule", {"type" -> 
                      t_, ___}, __] :> moldef[t], 
              Infinity]; 
theatoms = 
          Cases[body, 
            XMLElement["atom", {___, "type" -> 
                    t_, "position" -> p_, ___}, __] :> 
              Append[moldef[t], MolPosToList[p]], 
            Infinity]; 
Graphics3D[
          MolToGraphics /@ Join[themols, theatoms], 
          Boxed -> False]
]

ProcessDefinition[
          XMLElement["DEFINE", {"name" -> 
                name_String}, {XMLElement["molecule", {}, 
                subdef___]}]] := (moldef[name] = 
            ProcessSubdef /@ subdef);

Off[General::spell1]

\(\(ProcessDefinition[
          XMLElement["DEFINE", {"name" -> 
                name_String}, {XMLElement["atom", {a__}, {}]}]] := \((moldef[name] = Atom[GetRad[a], GetColor[a]])\);\)\)

ProcessSubdef[
          XMLElement["atom", {"type" -> t_, "id" -> 
                id_, pos___}, {}]] := (moldef[id] = 
            Append[moldef[t], GetPos[pos]]);

ProcessSubdef[
        XMLElement["bond", {"atom1" -> a1_, "atom2" -> 
              a2_}, {}]] := 
      Bond[moldef[a1][[3]], moldef[a2][[3]]]

MolStringListToList[molstringlist_String] := 
        Module[{stream = StringToStream[molstringlist], thelist}, 
          thelist = Read[stream, {Number, Number, Number}]; Close[stream]; 
          thelist];

MolColorToRGBColor[molcolor_String] := 
        RGBColor @@ MolStringListToList[molcolor];

MolPosToList[molpos_String] := 
        MolStringListToList[molpos];

GetPos[___, "position" -> p_, ___] := 
        MolPosToList[p];

GetRad[___, "radius" -> r_, ___] := 
        ToExpression[r];

GetColor[___, "color" -> c_, ___] := 
        MolColorToRGBColor[c];

GetColor[___] := GrayLevel[1];

MolToGraphics[Bond[pts__]] := {Thickness[0.01], Line[{pts}]}

MolToGraphics[Atom[r_, col_, pos_]] := {SurfaceColor[col], 
          EdgeForm[], TranslateShape[Sphere[r, 10, 10], pos]};

Chemistry in Mathematica

First, let's examine the methane molecule.

methane = Import["methane.xml"];

b = 
        Table[Show[ModLToGraphics3D[methane],  
            ViewPoint -> {2.490,  i,  i}],  {i, -16,  
            16,   .66}];

Export["methane.gif",  b,  "GIF",  
        ConversionOptions -> {"Transparency" -> 
              GrayLevel[1]}];

Next, let's examine the (Subscript[CaF, 2]) structure.

caf2 = Import["caf2.xml"];

<< Default3D`

Show[ModLToGraphics3D[caf2]];

Finally, let's visualize a complex carbon structure.  The portion that follows runs better with a fair amount of memory and a fast processor due to the number of objects that will be created and rendered.

ful = Import["ful.xml"];

Show[ModLToGraphics3D[ful]];

We can use RealTime3D[ ] to interactively visualize the structure.

<< RealTime3D`

Show[ModLToGraphics3D[ful]];

Reference

http://violet.csa.iisc.ernet.in/~modl/