Browse Source

PDB Parse basics

Thomas Forest 5 years ago
parent
commit
9aab335c9d
4 changed files with 88 additions and 0 deletions
  1. BIN
      doc/pdb_format_33_rot90.png
  2. BIN
      src/__pycache__/atom.cpython-36.pyc
  3. 20 0
      src/atom.py
  4. 68 0
      src/pdb.py

BIN
doc/pdb_format_33_rot90.png View File


BIN
src/__pycache__/atom.cpython-36.pyc View File


+ 20 - 0
src/atom.py View File

@@ -0,0 +1,20 @@
1
+class Atom:
2
+
3
+    def __init__(self, ATOM_ID, ATOM_NAME, ALT_LOCAT, RES_NAME, CHAIN_ID,
4
+                 RES_SEQ_NB, RES_INSER_CODE, COORD_X, COORD_Y, COORD_Z,
5
+                 OCCUPANCY, TEMP_FACT, ELEM_SYMBOL, ATOM_CHARGE):
6
+        
7
+        self.ATOM_ID = ATOM_ID
8
+        self.ATOM_NAME = ATOM_NAME
9
+        self.ALT_LOCAT = ALT_LOCAT 
10
+        self.RES_NAME = RES_NAME
11
+        self.CHAIN_ID = CHAIN_ID
12
+        self.RES_SEQ_NB = RES_SEQ_NB
13
+        self.RES_INSER_CODE = RES_INSER_CODE
14
+        self.COORD_X = COORD_X
15
+        self.COORD_Y = COORD_Y
16
+        self.COORD_Z = COORD_Z
17
+        self.OCCUPANCY = OCCUPANCY
18
+        self.TEMP_FACT = TEMP_FACT
19
+        self.ELEM_SYMBOL = ELEM_SYMBOL
20
+        self.ATOM_CHARGE = ATOM_CHARGE

+ 68 - 0
src/pdb.py View File

@@ -0,0 +1,68 @@
1
+import sys
2
+#import collections
3
+
4
+
5
+# custom imports
6
+from atom import *
7
+
8
+class PDBFile:
9
+
10
+    def getContent(self, filename):
11
+        with open(filename) as f:
12
+            return(f.readlines())
13
+
14
+    def getHeader(self):
15
+        #Metadata = collections.namedtuple("Metadata", ["header", "compound", "source", "author"])
16
+        Metadata = {}
17
+        for line in self.rawLines:
18
+            # no need to continue if meta are complete
19
+            if(len(Metadata) <4):
20
+                if(line[0:10] == "HEADER    "):
21
+                    Metadata['header']=line
22
+                elif(line[0:10] == "COMPND   2"):
23
+                    Metadata['compound']=line
24
+                elif(line[0:10] == "SOURCE   2"):
25
+                    Metadata['source']=line
26
+                elif(line[0:10] == "AUTHOR    "):
27
+                    Metadata['author']=line
28
+            else:
29
+                # if meta are complete, stop parsing
30
+                break     
31
+        return(Metadata)
32
+
33
+    def getAtoms(self):
34
+        self.ATOMS = []
35
+        
36
+        for line in self.rawLines:
37
+            if line.startswith("ATOM" or "HETATM"):
38
+                atom = Atom(ATOM_ID = int(line[6:11].strip()),
39
+                            ATOM_NAME = line[12:16].strip(),
40
+                            ALT_LOCAT = line[16:17].strip(),
41
+                            RES_NAME = line[17:20].strip(),
42
+                            CHAIN_ID = line[21:22].strip(),
43
+                            RES_SEQ_NB = int(line[22:26].strip()),
44
+                            RES_INSER_CODE = line[26:27].strip(),
45
+                            COORD_X = float(line[30:38].strip()),
46
+                            COORD_Y = float(line[38:46].strip()),
47
+                            COORD_Z = float(line[46:54].strip()),
48
+                            OCCUPANCY = float(line[54:60].strip()),
49
+                            TEMP_FACT = float(line[60:66].strip()),
50
+                            ELEM_SYMBOL = line[76:78].strip(),
51
+                            ATOM_CHARGE = line[78:80].strip())
52
+                self.ATOMS.append(atom)
53
+                      
54
+    
55
+    def __init__(self, filename):
56
+        self.rawLines = self.getContent(filename)
57
+        self.Metadata = self.getHeader()
58
+        self.getAtoms()
59
+        for elem in self.Metadata :    
60
+            print(self.Metadata[elem], end="")
61
+
62
+
63
+if __name__ == "__main__":
64
+    if(len(sys.argv)<2):
65
+        print("Not enough arguments! Run with --help to learn more about proper"
66
+              "call structure and parameters.")
67
+    else:
68
+        pdbFile = PDBFile(sys.argv[1])