-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdb_bfac.py
executable file
·88 lines (66 loc) · 2.4 KB
/
pdb_bfac.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
#!/usr/bin/python
# Takes a directory containing .pdb files as argument, and computes the average
# b-factor of all heavy atoms with the ATOM prefixes in these .pdb files
#
# https://github.com/thomas-coudrat/toolbx_pdb
# Thomas Coudrat <thomas.coudrat@gmail.com>
import argparse
import sys
from glob import glob
def main():
"""
Run the bfac script
"""
# Get the first argument of the executed script
pdbDir = parsing()
# Print the Bfactors from the X-ray structure files in this directory
print_bfactors(pdbDir)
def print_bfactors(pdbDir):
"""
Read each PDB file, collect B-factor values and atom count.
Calculate average B-factor.
"""
print("\nAnalysing B-factor from X-ray structures" +
"in directory: {}\n".format(pdbDir))
# Make a list of all .pdb files in the directory given as argument
pdbFilePaths = glob(pdbDir + "/*.pdb")
# Loop over all those .pdb files to extract the information
for pdbPath in pdbFilePaths:
# Read the pdb
pdbFile = open(pdbPath, "r")
pdbLines = pdbFile.readlines()
pdbFile.close()
# Get the b-factor list for all atoms in that list
bFactors = []
for line in pdbLines:
ll = line.split()
# if the line represents an ATOM, and if this atom is
# not a hydrogen
if ll[0] == 'ATOM' and ll[2][0] != 'H':
# b-factors are written in the pdb files between
# columns 60 and 67
bFac = line[60:67].strip()
bFac = float(bFac)
bFactors.append(bFac)
# Loop over the b-factors, and get the total and average
bFacTotal = 0
numAtoms = len(bFactors)
for line in bFactors:
bFacTotal = bFacTotal + line
print("B-factor on:\t {}".format(pdbPath))
print("total bFac:\t {}".format(bFacTotal))
print("atom count:\t {}".format(numAtoms))
print("bFac avg:\t {}\n".format(bFacTotal / numAtoms))
def parsing():
"""
Parse arguments
"""
descr = "Display the B-factor of X-ray structure PDB files"
descr_pdbDir = "Directory containing X-ray structure files"
parser = argparse.ArgumentParser(description=descr)
parser.add_argument("pdbDir", help=descr_pdbDir)
args = parser.parse_args()
pdbDir = args.pdbDir
return pdbDir
if __name__ == "__main__":
main()