Difference between revisions of "Useful Python Scripts"

From CUC3
Jump to navigation Jump to search
import>Ho246
import>Jss43
Line 3: Line 3:
 
== Converting .pdb files from vmd to xleap readable format ==
 
== Converting .pdb files from vmd to xleap readable format ==
   
This script takes a filename as command line argument reads the respective .pdb file and converts it to an xleap
+
This [[Media:pdb2xleap.txt|script]] takes a filename as command line argument reads the respective .pdb file and converts it to an xleap
 
readable format. The initial file is overwritten, but the original file is retained in filename.orig.
 
readable format. The initial file is overwritten, but the original file is retained in filename.orig.
  +
 
<pre>
 
<pre>
 
#!/usr/bin/env python
 
#!/usr/bin/env python
Line 60: Line 61:
   
 
</pre>
 
</pre>
  +
  +
== Fortran module dependencies ==
  +
  +
It is useful to be able to view the dependencies on modules in a fortran code graphically. This is especially helpful when trying to reorder code for a cleaner flow, trying to remove issues leading to circular dependencies, or for becoming familiar with the structure of a new code.
  +
  +
[[Media:get_dependencies.txt|get_dependencies.py]] is a script which analyses source code and outputs (in the [http://www.graphviz.org dot language]) the dependencies on modules.
  +
The output contains the following information:
  +
* modules depending on other modules.
  +
* non-modules files depending on modules.
  +
These are further split up into dependencies involving utilty or data modules (which must be specified inside get_dependencies.py). Graphs of the dependencies can be produced using a suitable parser.
  +
  +
Usage:
  +
get_dependencies.py [source files]
  +
  +
The user needs to do modify a few things before first use:
  +
# Give the names of your data and utility modules and the output directory in the appropriate place in get_dependencies.py.
  +
# Create the output directory.
  +
# Run get_dependencies on your source code. I do:
  +
~/src $ dependencies/get_dependencies.py *.F{,90}
  +
as all our source files are either .F or .F90 files.
  +
  +
Producing graphs from the output requires [http://www.graphviz.org graphviz]. This is not installed in the sector, but is easy to [[Useful_Makefiles#Graphviz|compile]].
  +
  +
Run the desired parser (e.g. one of those provided by graphviz) on the resultant dot files. I find dot works best. I run:
  +
~/src/dependencies $ for f in *.dot; do dot -Tps $f -o `basename $f .dot`.ps; done
  +
to produce all the graphs in one shot. (Note ~/local/bin is on my path.)

Revision as of 21:56, 29 September 2008

On this script you will find (and can add!) Python scripts, that may be useful in any work-related way.

Converting .pdb files from vmd to xleap readable format

This script takes a filename as command line argument reads the respective .pdb file and converts it to an xleap readable format. The initial file is overwritten, but the original file is retained in filename.orig.

#!/usr/bin/env python
# -*- coding: latin1 -*-

import sys

def main():
 	fname = sys.argv[1]
 
 	fin = open( fname, "r")
 	lines = fin.readlines()
 	fin.close()

	lin2=[]

	foumod=open( fname+".orig","w")
	fomod=open( fname,"w")


	counter=1
	first=True
	oldnum=""
	for l in lines:
		if l.find( "CRYST")==-1:
			l2=l.replace( " X ", "   ")
			if len( l2)>16:
				if l2[13].isdigit():
					subs=l2[12:16]
					subs2=l2[13:17]
					l2=l2.replace( subs, subs2)

			if (not first) and len( l2)>25:
				if oldnum!=l2[25]:
					oldnum=l2[25]
					print oldnum
					lin2.append( "TER\n")
					counter+=1
			first=False
			lin2.append( l2)
		else:
			counter-=1
	lin2.insert(-1,"TER\n")

	for i in range( counter):
		lines.append( "")

	for l,l2 in zip( lines, lin2):
		foumod.write( l)
		fomod.write( l2)


if __name__ == "__main__":
	main()

Fortran module dependencies

It is useful to be able to view the dependencies on modules in a fortran code graphically. This is especially helpful when trying to reorder code for a cleaner flow, trying to remove issues leading to circular dependencies, or for becoming familiar with the structure of a new code.

get_dependencies.py is a script which analyses source code and outputs (in the dot language) the dependencies on modules. The output contains the following information:

  • modules depending on other modules.
  • non-modules files depending on modules.

These are further split up into dependencies involving utilty or data modules (which must be specified inside get_dependencies.py). Graphs of the dependencies can be produced using a suitable parser.

Usage:

get_dependencies.py [source files]

The user needs to do modify a few things before first use:

  1. Give the names of your data and utility modules and the output directory in the appropriate place in get_dependencies.py.
  2. Create the output directory.
  3. Run get_dependencies on your source code. I do:
~/src $ dependencies/get_dependencies.py *.F{,90}

as all our source files are either .F or .F90 files.

Producing graphs from the output requires graphviz. This is not installed in the sector, but is easy to compile.

Run the desired parser (e.g. one of those provided by graphviz) on the resultant dot files. I find dot works best. I run:

~/src/dependencies $ for f in *.dot; do dot -Tps $f -o `basename $f .dot`.ps; done

to produce all the graphs in one shot. (Note ~/local/bin is on my path.)