Personal tools

Programming Python with MGLTools

Document Actions

Note: This is the print view with all the tutorial pages on one page. The paginated version is available here, if you prefer that.

Miscellaneous Python programming topics.

removeHOH.py

Removes water (HOH) molecules from a molecule.

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"
9
10 def usage():
11 "Print helpful usage statement to stdout."
12 print __file__ +": no input file\n"
13 print "\tUsage: removeHOH.py input molecule supported by MolKit\n"
14
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.

removeAltLoc.py

Removes atoms with alternate locations.

The following is listing for removeAltLoc.py.

 1  #!/usr/bin/env python 
2 "Removes atoms with alternate locations"
3 import sys
4
5 try:
6 import MolKit
7 except ImportError, inst:
8 print inst
9 print "Please install MolKit - http://mgltools.scripps.edu"
10 sys.exit(1)
11 12 def usage():
13 "Print helpful usage statement to stdout."
14 print __file__ +": no input file\n"
15 print "\tUsage: removeAlt.py input molecule supported by MolKit\n"
16
17
18 if len(sys.argv) < 2:
19 usage()
20 sys.exit(1) #help(sys.exit) for more info
21
22 mol = MolKit.Read(sys.argv[1]) #FIXME: check if file sys.argv[1] exists
23 for chain in mol.chains:
24 for residue in chain.residues:
25 altDict = {} #to keep atoms with
26 for atom in residue.atoms:
27 if '@' in atom.name:
28 list = atom.name.split('@') #'CB@B'=>['CB', 'B']
29 if list[-1] in altDict:
30 altDict[list[-1]].append(atom)
31 else:
32 altDict[list[-1]] = [atom]
33 if altDict:
34 keys = altDict.keys()
35 print "\nResidue %s altLoc:"%residue.name,
36 print keys,
37 print ' Removing ',
38 for key in keys[1:]:
39 for atom in altDict[key]:
40 print atom.name,
41 residue.remove(atom)
42
43 from MolKit.pdbWriter import PdbWriter
44 writer = PdbWriter()
45 writer.write('out.pdb',mol)

Run with 1DM2.pdb as input, for instance, to see it in action.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: