-
Notifications
You must be signed in to change notification settings - Fork 0
/
pspdsd.py
146 lines (111 loc) · 3.84 KB
/
pspdsd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
#
# This is a python script for computing backbone dihedral angles from protein crystal structures.
#
import sys
import copy
from Protein import *
### Print program usage instructions and exit
def usage():
print ""
print "Usage: python pspdsd.py [OPTIONS]"
print " python pspdsd.py [-h | --help]"
print " python pspdsd.py [-i INPUT | --input INPUT]"
print ""
print "pspdsd.py is a script for computing backbone dihedral angles from protein"
print "crystal structures."
print ""
print "OPTIONS:"
print " -h, --help Print this help message and exit"
print " -i, --input Name of protein input file in PDB format"
print " -o, --output Name of output file"
print ""
exit()
### Print an error message, tell user how to get help information, and exit
def die(errorMessage):
print errorMessage
print "Use \'python " + progName + " --help\' for help info."
exit()
### Given a file name, read a pdb file, save it as a Protein() object, and return that object.
### This is expecting that every atom starts with 'ATOM', every chain ends in 'TER', and the
### columns are formatted to standard PDB conventions.
def read_protein(inputFilename):
myProtein = Protein()
myChain = Chain()
myResidue = Residue()
myAtom = Atom()
lastresid = ''
inputFile = open(inputFilename, 'r')
for line in inputFile:
if line.startswith("TER"):
myResidue.resid = lastresid
myChain.chainid = line[21]
myChain.residue.append(copy.deepcopy(myResidue))
myProtein.chain.append(copy.deepcopy(myChain))
myResidue.clean()
myChain.clean()
elif line.startswith("ATOM"):
myAtom.atomname = line[12:16]
myAtom.x = line[30:38]
myAtom.y = line[38:46]
myAtom.z = line[46:54]
thisresname = line[17:20]
thisresid = line[22:26]
# if residue is the same as last time, add to residue
if ( thisresid == lastresid ):
myResidue.atom.append(copy.deepcopy(myAtom))
else:
if ( len(myResidue.atom) > 0 ):
myResidue.resid = lastresid
myChain.residue.append(copy.deepcopy(myResidue))
myResidue.clean()
myResidue.atom.append(copy.deepcopy(myAtom))
lastresid = thisresid
myAtom.clean()
else:
pass
inputFile.close()
myChain.clean()
myResidue.clean()
myAtom.clean()
return myProtein
### Main function
def main():
global progName
progName = sys.argv[0]
sys.argv.pop(0)
inputFilename = ''
outputFilename = ''
if ( len(sys.argv) == 0 ): die ("Error: Missing command line options.")
# Parse arguments
while ( len(sys.argv) > 0 ):
arg = sys.argv.pop(0)
if ( arg == "-h" or arg == "--help" ):
usage()
elif ( arg == "-i" or arg == "--input" ):
if ( len(sys.argv) >= 1 ):
inputFilename=sys.argv[0]
sys.argv.pop(0)
else:
die ("Input file not specified on the command line.")
elif ( arg == "-o" or arg == "--output" ):
if ( len(sys.argv) >= 1 ):
outputFilename=sys.argv[0]
sys.argv.pop(0)
else:
die ("Output file not specified on the command line.")
else:
die ("Command line option " + arg + " unrecognized.")
thisProtein = read_protein(inputFilename)
print "Printing chains: "
thisProtein.print_chains()
print ""
print "Printing residues: "
thisProtein.print_residues()
print ""
print "Printing atoms: "
thisProtein.print_atoms()
print ""
exit()
if __name__ == "__main__":
main()