I would like to query a pdb file (in XML format) and store the relevant information in another xml file. I've been around some tutorials and managed to do it in simplier files.
The XML file looks like this: http://www.rcsb.org/pdb/files/1L2Y.xml
I want to store the atoms coordinates (and some other information) based in a simple condition the attribute value PDBx:auth_atom_id is equal to "N", "CA", or "C".
XDocument xmlDocument = XDocument.Load (@"...\ProjectC#\Molecule_00\PDBLibary_00\Data\1L2Y.xml");
XDocument result = new XDocument (
new XElement ("Molecule",
new XElement ("Atom",
from s in xmlDocument.Descendants ("PDBx:atom_site")
where s.Attribute ("PDBx:auth_atom_id").Value == "N" // s.Attribute ("PDBx:auth_atom_id").Value == "CA" || s.Attribute ("PDBx:auth_atom_id").Value == "C"
select new XElement ("Atom",
new XElement ("AtMolType", s.Element ("PDBx:auth_atom_id").Value),
new XElement ("CoordX", s.Element ("PDBx:Cartn_x").Value),
new XElement ("CoordY", s.Element ("PDBx:Cartn_y").Value),
new XElement ("CoordZ", s.Element ("PDBx:Cartn_z").Value)))));
result.Save(@"...\PDBLibary_00\Data\_1L2Y.xml");
When I run the script it returns a System.XML.XMLException (the character ":" can't be used. However it's how the XML file is constructed. I have tried to change the character in the XML file but didn't succeed.
You've misunderstood the meaning of an element that looks like this:
<PDBx:atom_siteCategory>
That's an element with a local name of atom_siteCategory
in the namespace with the URI of "http://pdbml.pdb.org/schema/pdbx-v40.xsd"
as previously specified here:
xmlns:PDBx="http://pdbml.pdb.org/schema/pdbx-v40.xsd"
LINQ to XML has great namespace support, so you can just use:
XNamespace pdbx = "http://pdbml.pdb.org/schema/pdbx-v40.xsd";
then in your query use:
new XElement("AtMolType", s.Element(pdbx + "auth_atom_id").Value),
new XElement("CoordX", s.Element(pdbx + "Cartn_x").Value),
new XElement("CoordY", s.Element(pdbx + "Cartn_y").Value),
new XElement("CoordZ", s.Element(pdbx + "Cartn_z").Value)))));
(likewise for Descendants
etc).
See more on this question at Stackoverflow