Please don't look at these unless you are really struggling!
This version uses the 'obvious' approach and .item(0) to obtain the first item from a set
#!/usr/bin/env python3
# https://docs.python.org/3.4/library/xml.dom.minidom.html
from xml.dom import minidom
import sys
progname = sys.argv.pop(0)
file = sys.argv.pop(0)
doc = minidom.parse(file)
for mutant_group in doc.getElementsByTagName('mutant_group'):
native_structure_list = mutant_group.getElementsByTagName('native_structure')
ns = native_structure_list.item(0)
resolution_list = ns.getElementsByTagName('resolution')
res = resolution_list.item(0).firstChild.data
if(float(res) <= 2.0):
domid = mutant_group.getAttribute('native')
print (domid, " ", res)
This version uses my preferred nested loops approach.
#!/usr/bin/env python3
# https://docs.python.org/3.4/library/xml.dom.minidom.html
from xml.dom import minidom
import sys
progname = sys.argv.pop(0)
file = sys.argv.pop(0)
doc = minidom.parse(file)
for mutant_group in doc.getElementsByTagName('mutant_group'):
for native_structure in mutant_group.getElementsByTagName('native_structure'):
for resolution in native_structure.getElementsByTagName('resolution'):
res = resolution.firstChild.data
if(float(res) <= 2.0):
domid = mutant_group.getAttribute('native')
print (domid, " ", res)
This version uses the 'obvious' approach again, this time using [0] rather than .item(0) and splits the steps differently.
#!/usr/bin/env python3
# https://docs.python.org/3.4/library/xml.dom.minidom.html
from xml.dom import minidom
import sys
progname = sys.argv.pop(0)
file = sys.argv.pop(0)
doc = minidom.parse(file)
for mutant_group in doc.getElementsByTagName('mutant_group'):
nativeStructure = mutant_group.getElementsByTagName('native_structure')[0]
resolution = nativeStructure.getElementsByTagName('resolution')[0]
res = resolution.firstChild.data
if(float(res) <= 2.0):
domid = mutant_group.getAttribute('native')
print (domid, " ", res)
This version uses the 'obvious' approach and .item(0) to obtain the first item from a set
#!/usr/bin/env python3
from xml.dom import minidom
import sys
progname = sys.argv.pop(0)
file = sys.argv.pop(0)
doc = minidom.parse(file)
for mutant_group in doc.getElementsByTagName('mutant_group'):
native = mutant_group.getAttribute('native')
if(native == '1eugA0'):
for mutant in mutant_group.getElementsByTagName('mutant'):
domid = mutant.getAttribute('domid')
structure = mutant.getElementsByTagName('structure').item(0)
resolution = structure.getElementsByTagName('resolution').item(0)
res = resolution.firstChild.data
mutation = mutant.getElementsByTagName('mutation').item(0)
resnum = mutation.getAttribute('resnum')
wildtype = mutation.getAttribute('wildtype')
substitution = mutation.getAttribute('substitution')
print ("%6s %-4s %3s %3s %.2f" % (domid, resnum, wildtype, substitution, float(res)))
This version uses my preferred nested loops approach.
#!/usr/bin/env python3
from xml.dom import minidom
import sys
progname = sys.argv.pop(0)
file = sys.argv.pop(0)
doc = minidom.parse(file)
for mutant_group in doc.getElementsByTagName('mutant_group'):
native = mutant_group.getAttribute('native')
if(native == '1eugA0'):
for mutant in mutant_group.getElementsByTagName('mutant'):
domid = mutant.getAttribute('domid')
for structure in mutant.getElementsByTagName('structure'):
for resolution in structure.getElementsByTagName('resolution'):
res = resolution.firstChild.data
for mutation in mutant.getElementsByTagName('mutation'):
resnum = mutation.getAttribute('resnum')
wildtype = mutation.getAttribute('wildtype')
substitution = mutation.getAttribute('substitution')
print ("%6s %-4s %3s %3s %.2f" % (domid, resnum, wildtype, substitution, float(res)))