From f27a73ad68b91828208decfe0c03a0043737221a Mon Sep 17 00:00:00 2001 From: sjplimp Date: Wed, 10 Nov 2010 14:58:49 +0000 Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@5231 f3b2605a-c512-4ea7-a41b-209d697bcdaa --- python/README | 58 +++++-- python/examples/pizza/pdbfile.py | 289 +++++++++++++++++++++++++++++++ 2 files changed, 335 insertions(+), 12 deletions(-) create mode 100644 python/examples/pizza/pdbfile.py diff --git a/python/README b/python/README index 742d5e6f62..a2cea54fbf 100644 --- a/python/README +++ b/python/README @@ -22,26 +22,60 @@ pages. Once you have successfully built and tested the wrappers, you can run the Python scripts in the examples sub-directory: -trivial.py read/run a LAMMPS input script thru Python -demo.py invoke various LAMMPS library interface routines -simple.py mimic operation of couple/simple/simple.cpp in Python -gui.py GUI go/stop/temperature-slider to control LAMMPS -plot.py real-time temeperature plot with GnuPlot via Pizza.py -viz.py real-time viz from GL tool in Pizza.py -vizplotgui.py combination of viz.py and plot.py and gui.py +trivial.py read/run a LAMMPS input script thru Python +demo.py invoke various LAMMPS library interface routines +simple.py mimic operation of couple/simple/simple.cpp in Python +gui.py GUI go/stop/temperature-slider to control LAMMPS +plot.py real-time temeperature plot with GnuPlot via Pizza.py +viz_tool.py real-time viz from GL tool in Pizza.py +vizplotgui_tool.py combination of viz.py and plot.py and gui.py -Run them with the following input scripts and arguments: +For the viz_tool.py and vizplotgui_tool.py commands, replace "tool" +with "gl" or "atomeye" or "pymol", depending on what visualization +package you have installed. We hope to add a VMD option soon. + +Note that for GL, you need to be able to run the Pizza.py GL tool, +which is included in the pizza sub-directory. See the Pizza.py doc +pages for more info: + +http://www.sandia.gov/~sjplimp/pizza.html + +Note that for AtomEye, you need version 3, and their is a line in the +scripts that specifies the path and name of the executable. See +the AtomEye WWW pages for more details: + +http://mt.seas.upenn.edu/Archive/Graphics/A/ +http://mt.seas.upenn.edu/Archive/Graphics/A3/A3.html + +Note that for PyMol, you need to have built and installed the +open-source version of PyMol in your Python, so that you can import it +from a Python script. See the AtomEye WWW pages for more details: + +http://www.pymol.org/ +http://sourceforge.net/scm/?type=svn&group_id=4546 + +The latter link is to the open-source version. + +You can run a particular script in either of the following ways: + +% trivial.py in.trivial +% python -i trivial.py in.trivial + +The former assumes that you have changed the first line of the script +to point to the Python installed on your box. + +Run the Python scripts with the following LAMMPS input scripts and arguments: trivial.py in.trivial demo.py simple.py in.simple gui.py in.gui 100 plot.py in.plot 10 1000 thermo_temp -viz.py in.viz 100 5000 -vizplotgui.py in.viz 100 thermo_temp +viz_tool.py in.viz 100 5000 +vizplotgui_tool.py in.viz 100 thermo_temp You can un-comment the Pypar calls if you want to run these in parallel. -Each script has more documentation at the top of the file that -explains how to use it. +Each script has more documentation in the file that explains how to +use it and what it is doing. diff --git a/python/examples/pizza/pdbfile.py b/python/examples/pizza/pdbfile.py new file mode 100644 index 0000000000..1713ada043 --- /dev/null +++ b/python/examples/pizza/pdbfile.py @@ -0,0 +1,289 @@ +# Pizza.py toolkit, www.cs.sandia.gov/~sjplimp/pizza.html +# Steve Plimpton, sjplimp@sandia.gov, Sandia National Laboratories +# +# Copyright (2005) Sandia Corporation. Under the terms of Contract +# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains +# certain rights in this software. This software is distributed under +# the GNU General Public License. + +# pdb tool + +oneline = "Read, write PDB files in combo with LAMMPS snapshots" + +docstr = """ +p = pdbfile("3CRO") create pdb object from PDB file or WWW +p = pdbfile("pep1 pep2") read in multiple PDB files +p = pdbfile("pep*") can use wildcards +p = pdbfile(d) read in snapshot data with no PDB file +p = pdbfile("3CRO",d) read in single PDB file with snapshot data + + string arg contains one or more PDB files + don't need .pdb suffix except wildcard must expand to file.pdb + if only one 4-char file specified and it is not found, + it will be downloaded from http://www.rcsb.org as 3CRO.pdb + d arg is object with atom coordinates (dump, data) + +p.one() write all output as one big PDB file to tmp.pdb +p.one("mine") write to mine.pdb +p.many() write one PDB file per snapshot: tmp0000.pdb, ... +p.many("mine") write as mine0000.pdb, mine0001.pdb, ... +p.single(N) write timestamp N as tmp.pdb +p.single(N,"new") write as new.pdb + + how new PDB files are created depends on constructor inputs: + if no d: one new PDB file for each file in string arg (just a copy) + if only d specified: one new PDB file per snapshot in generic format + if one file in str arg and d: one new PDB file per snapshot + using input PDB file as template + multiple input PDB files with a d is not allowed + +index,time,flag = p.iterator(0) +index,time,flag = p.iterator(1) + + iterator = loop over number of PDB files + call first time with arg = 0, thereafter with arg = 1 + N = length = # of snapshots or # of input PDB files + index = index of snapshot or input PDB file (0 to N-1) + time = timestep value (time stamp for snapshot, index for multiple PDB) + flag = -1 when iteration is done, 1 otherwise + typically call p.single(time) in iterated loop to write out one PDB file +""" + +# History +# 8/05, Steve Plimpton (SNL): original version + +# ToDo list +# for generic PDB file (no template) from a LJ unit system, +# the atoms in PDB file are too close together + +# Variables +# files = list of input PDB files +# data = data object (ccell,data,dump) to read snapshots from +# atomlines = dict of ATOM lines in original PDB file +# key = atom id, value = tuple of (beginning,end) of line + +# Imports and external programs + +import sys, types, glob, urllib + +# Class definition + +class pdbfile: + + # -------------------------------------------------------------------- + + def __init__(self,*args): + if len(args) == 1: + if type(args[0]) is types.StringType: + filestr = args[0] + self.data = None + else: + filestr = None + self.data = args[0] + elif len(args) == 2: + filestr = args[0] + self.data = args[1] + else: raise StandardError, "invalid args for pdb()" + + # flist = full list of all PDB input file names + # append .pdb if needed + + if filestr: + list = filestr.split() + flist = [] + for file in list: + if '*' in file: flist += glob.glob(file) + else: flist.append(file) + for i in xrange(len(flist)): + if flist[i][-4:] != ".pdb": flist[i] += ".pdb" + if len(flist) == 0: + raise StandardError,"no PDB file specified" + self.files = flist + else: self.files = [] + + if len(self.files) > 1 and self.data: + raise StandardError, "cannot use multiple PDB files with data object" + if len(self.files) == 0 and not self.data: + raise StandardError, "no input PDB file(s)" + + # grab PDB file from http://rcsb.org if not a local file + + if len(self.files) == 1 and len(self.files[0]) == 8: + try: + open(self.files[0],'r').close() + except: + print "downloading %s from http://rcsb.org" % self.files[0] + fetchstr = "http://www.rcsb.org/pdb/cgi/export.cgi/%s?format=PDB&pdbId=2cpk&compression=None" % self.files[0] + urllib.urlretrieve(fetchstr,self.files[0]) + + if self.data and len(self.files): self.read_template(self.files[0]) + + # -------------------------------------------------------------------- + # write a single large PDB file for concatenating all input data or files + # if data exists: + # only selected atoms returned by extract + # atoms written in order they appear in snapshot + # atom only written if its tag is in PDB template file + # if no data: + # concatenate all input files to one output file + + def one(self,*args): + if len(args) == 0: file = "tmp.pdb" + elif args[0][-4:] == ".pdb": file = args[0] + else: file = args[0] + ".pdb" + + f = open(file,'w') + + # use template PDB file with each snapshot + + if self.data: + n = flag = 0 + while 1: + which,time,flag = self.data.iterator(flag) + if flag == -1: break + self.convert(f,which) + print >>f,"END" + print time, + sys.stdout.flush() + n += 1 + + else: + for file in self.files: + f.write(open(file,'r').read()) + print >>f,"END" + print file, + sys.stdout.flush() + + f.close() + print "\nwrote %d datasets to %s in PDB format" % (n,file) + + # -------------------------------------------------------------------- + # write series of numbered PDB files + # if data exists: + # only selected atoms returned by extract + # atoms written in order they appear in snapshot + # atom only written if its tag is in PDB template file + # if no data: + # just copy all input files to output files + + def many(self,*args): + if len(args) == 0: root = "tmp" + else: root = args[0] + + if self.data: + n = flag = 0 + while 1: + which,time,flag = self.data.iterator(flag) + if flag == -1: break + + if n < 10: + file = root + "000" + str(n) + elif n < 100: + file = root + "00" + str(n) + elif n < 1000: + file = root + "0" + str(n) + else: + file = root + str(n) + file += ".pdb" + + f = open(file,'w') + self.convert(f,which) + f.close() + + print time, + sys.stdout.flush() + n += 1 + + else: + n = 0 + for infile in self.files: + if n < 10: + file = root + "000" + str(n) + elif n < 100: + file = root + "00" + str(n) + elif n < 1000: + file = root + "0" + str(n) + else: + file = root + str(n) + file += ".pdb" + + f = open(file,'w') + f.write(open(infile,'r').read()) + f.close() + print file, + sys.stdout.flush() + + n += 1 + + print "\nwrote %d datasets to %s*.pdb in PDB format" % (n,root) + + # -------------------------------------------------------------------- + # write a single PDB file + # if data exists: + # time is timestamp in snapshot + # only selected atoms returned by extract + # atoms written in order they appear in snapshot + # atom only written if its tag is in PDB template file + # if no data: + # time is index into list of input PDB files + # just copy one input file to output file + + def single(self,time,*args): + if len(args) == 0: file = "tmp.pdb" + elif args[0][-4:] == ".pdb": file = args[0] + else: file = args[0] + ".pdb" + f = open(file,'w') + + if self.data: + which = self.data.findtime(time) + self.convert(f,which) + else: + f.write(open(self.files[time],'r').read()) + + f.close() + + # -------------------------------------------------------------------- + # iterate over list of input files or selected snapshots + # latter is done via data objects iterator + + def iterator(self,flag): + if not self.data: + if not flag: self.iterate = 0 + else: + self.iterate += 1 + if self.iterate > len(self.files): return 0,0,-1 + return self.iterate,self.iterate,1 + + return self.data.iterator(flag) + + # -------------------------------------------------------------------- + # read a PDB file and store ATOM lines + + def read_template(self,file): + lines = open(file,'r').readlines() + self.atomlines = {} + for line in lines: + if line.find("ATOM") == 0: + tag = int(line[4:11]) + begin = line[:30] + end = line[54:] + self.atomlines[tag] = (begin,end) + + # -------------------------------------------------------------------- + # convert one set of atoms to PDB format and write to f + + def convert(self,f,which): + time,box,atoms,bonds,tris,lines = self.data.viz(which) + if len(self.files): + for atom in atoms: + id = atom[0] + if self.atomlines.has_key(id): + (begin,end) = self.atomlines[id] + line = "%s%8.3f%8.3f%8.3f%s" % (begin,atom[2],atom[3],atom[4],end) + print >>f,line, + else: + for atom in atoms: + begin = "ATOM %6d %2d R00 1 " % (atom[0],atom[1]) + middle = "%8.3f%8.3f%8.3f" % (atom[2],atom[3],atom[4]) + end = " 1.00 0.00 NONE" + print >>f,begin+middle+end