Page 1 of 1

modifying qbox_xyz.sh to read incomplete runs

Posted: Thu Jun 14, 2012 10:35 pm
by gpanchap
Dear Francois,

Is there a simple way to modify your qbox_xyz.sh tool so it might also read incomplete MD output files? Currently it quits complaining about premature end of data. Thanks for your help.

Ganesh

Re: modifying qbox_xyz.sh to read incomplete runs

Posted: Thu Jun 14, 2012 10:47 pm
by gpanchap
Ah..I got it to work. The problem was xst was looking for a style and it wasn't complete due to the crash of the MD run. Closing the different styles properly seem to work. Please ignore my question.

Re: modifying qbox_xyz.sh to read incomplete runs

Posted: Thu Jun 14, 2012 11:34 pm
by fgygi
Dear Ganesh,

A number of these post-processing scripts do not behave well when the document is not complete (i.e. is not a well-formed XML document). Things may be improved using python scripts and progressive parsing. The following python script extracts xyz data from a run and (in the cases I tested) does not complain if the file is incomplete.

Best
Francois

Code: Select all

#!/usr/bin/python
# qbox_xyz.py
# extract positions from Qbox output
# use: qbox_xyz.py file.r > file.xyz
import xml.sax
import sys

# Qbox output handler to extract and process <atomset>
class QboxOutputHandler(xml.sax.handler.ContentHandler):
  def __init__(self):
    self.niter = 0
    self.readData = 0
    self.buffer = ""
    self.outbuf = ""

  def startElement(self, name, attributes):
    if name == "atomset":
      self.nat = 0
      self.niter += 1
      self.outbuf = ""
    elif name == "atom": 
      self.atom_name = attributes["name"]
      self.nat += 1
    elif name == "position":
      self.readData = 1 
      self.buffer = ""

  def characters(self, data):
    if self.readData:
      self.buffer += data

  def endElement(self, name):
    if name == "atomset":
      print self.nat
      print self.niter
      print self.outbuf, 
    elif name == "position":
      self.readData = 0
    elif name == "atom":
      self.pos = self.buffer.split()
      self.outbuf += self.atom_name+" "
      self.outbuf += str(float(self.pos[0])*0.529177)+" "
      self.outbuf += str(float(self.pos[1])*0.529177)+" "
      self.outbuf += str(float(self.pos[2])*0.529177)+" "
      self.outbuf += "\n"

parser = xml.sax.make_parser()
handler = QboxOutputHandler()
parser.setContentHandler(handler)

file = open(sys.argv[1])
s = file.read(8192)
while s !="": 
  parser.feed(s)
  s = file.read(8192)

parser.reset()
#print "parsing done"