Skip to content

Commit

Permalink
getposition, setposition, pdb2fasta
Browse files Browse the repository at this point in the history
  • Loading branch information
zwang123 committed Oct 10, 2018
1 parent 160ebd8 commit 9a4700f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
42 changes: 42 additions & 0 deletions parse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict
from .pdblist import PDBList
import textwrap

def parse_pdb_line(l):
"""
Expand Down Expand Up @@ -64,6 +65,47 @@ def write_pdb_file(pdbdata, filename):
with open(filename, "w") as f:
f.write(write_pdb_lines(pdbdata))

def pdb2fasta(pdbdata, pdbname='', linewidth=80, resmap={}):
""" convert parsed pdb data to fasta sequence """

# TODO python 3.5
resmap = {'ALA':'A','ARG':'R','ASN':'N',
'ASP':'D','CYS':'C','GLU':'E',
'GLN':'Q','GLY':'G','HIS':'H',
'HSD':'H','HSE':'H','HSP':'H',
'ILE':'I','LEU':'L','LYS':'K',
'MET':'M','PHE':'F','PRO':'P',
'SER':'S','THR':'T','TRP':'W',
'TYR':'Y','VAL':'V', **resmap}

delim = '\n'
for entry in pdbdata:
if delim in entry['chainID'] + entry['segment']:
raise ValueError(r"chainID or segment contain \n")

fasta = OrderedDict()
for entry in pdbdata:
symbol = resmap.get(entry['resName'], '-')
key = entry['chainID']+delim+entry['segment']
res = entry['resSeq']
if key in fasta:
if res in fasta[key]:
if fasta[key][res] != symbol:
raise ValueError("Multiple resName in the same residue")
else:
fasta[key][res] = symbol
else:
fasta[key] = {res: symbol}

fasta_str = ''
for key, item in fasta.items():
chain, segment = key.split(delim)
fasta_str += '>{}:{}|{}\n'.format(pdbname, chain, segment)
seq = ''.join(item[k] for k in sorted(item))
fasta_str += textwrap.fill(seq, width=linewidth) + '\n'

return fasta_str

if __name__ == "__main__":
#parse_pdb_line("ATOM 1914 SOD SOD S1127 0.016 -3.389 -0.040 1.00 58.57 S NA")
source = "/home/zhiwang/my_proj/charmm-gui_e86p_A51_drude/step2_drude.pdb"
Expand Down
11 changes: 11 additions & 0 deletions pdblist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from numpy import array

class PDBList(list):
"""
store as list of dict, but can function as a dict of list
Expand All @@ -22,3 +24,12 @@ def find_serial(self, serial):
for i, x in enumerate(self):
if x["serial"] == serial:
return i

def getposition(self, index):
entry = self[index]
return array((entry["x"], entry["y"], entry["z"]))

def setposition(self, index, value):
self[index]["x"] = value[0]
self[index]["y"] = value[1]
self[index]["z"] = value[2]

0 comments on commit 9a4700f

Please sign in to comment.