removeHOH.py
Removes water (HOH) molecules from a molecule.
sargis
Miscellaneous Python programming topics.
Page
1
of
2.
The following is listing for removeHOH.py.
1 #!/usr/bin/env python
2 "Removes water (HOH) molecules from a molecule"
3 import sys
4 try:
5 import MolKit
6 except ImportError, inst:
7 print inst
8 print "Please install MolKit - http://mgltools.scripps.edu"
910 def usage():11 "Print helpful usage statement to stdout."14
12 print __file__ +": no input file\n"
13 print "\tUsage: removeHOH.py input molecule supported by MolKit\n"
15
16 if len(sys.argv) < 2:
17 usage()
18 sys.exit(1) #help(sys.exit) for more info
19
20 mol = MolKit.Read(sys.argv[1]) #FIXME: check if file sys.argv[1] exists
21 chainsToRemove = [] #this is needed to remove chains with no residues.
22 for chain in mol.chains:
23 residuesHOH = [] #water residues
24 for residue in chain.residues:
25 if "HOH" in residue.name:
26 #Can't do chain.residues.remove(residue). See for instance http://effbot.org/zone/python-list.htm
27 residuesHOH.append(residue)
28 for HOH in residuesHOH:
29 chain.residues.remove(HOH)
30 if not chain.residues:
31 chainsToRemove.append(chain)
32
33 for chain in chainsToRemove:
34 mol.chains.remove(chain)
35
36 from MolKit.pdbWriter import PdbWriter
37 writer = PdbWriter()
38 writer.write('out.pdb',mol, records=['ATOM', 'CONECT', 'HETATM'])
39
40 #TODO: Add an option for choosing output file name
Run with 1DM2.pdb as input, for instance, to see it in action.
