다음 BioPython 코드는 사용자의 요구에 적합해야합니다.
PDB.Select
을 사용하여 원하는 체인 (사용자의 경우 하나의 체인)과 PDBIO()
만 선택하여 체인 만 포함하는 구조를 만듭니다.
import os
from Bio import PDB
class ChainSplitter:
def __init__(self, out_dir=None):
""" Create parsing and writing objects, specify output directory. """
self.parser = PDB.PDBParser()
self.writer = PDB.PDBIO()
if out_dir is None:
out_dir = os.path.join(os.getcwd(), "chain_PDBs")
self.out_dir = out_dir
def make_pdb(self, pdb_path, chain_letters, overwrite=False, struct=None):
""" Create a new PDB file containing only the specified chains.
Returns the path to the created file.
:param pdb_path: full path to the crystal structure
:param chain_letters: iterable of chain characters (case insensitive)
:param overwrite: write over the output file if it exists
"""
chain_letters = [chain.upper() for chain in chain_letters]
# Input/output files
(pdb_dir, pdb_fn) = os.path.split(pdb_path)
pdb_id = pdb_fn[3:7]
out_name = "pdb%s_%s.ent" % (pdb_id, "".join(chain_letters))
out_path = os.path.join(self.out_dir, out_name)
print "OUT PATH:",out_path
plural = "s" if (len(chain_letters) > 1) else "" # for printing
# Skip PDB generation if the file already exists
if (not overwrite) and (os.path.isfile(out_path)):
print("Chain%s %s of '%s' already extracted to '%s'." %
(plural, ", ".join(chain_letters), pdb_id, out_name))
return out_path
print("Extracting chain%s %s from %s..." % (plural,
", ".join(chain_letters), pdb_fn))
# Get structure, write new file with only given chains
if struct is None:
struct = self.parser.get_structure(pdb_id, pdb_path)
self.writer.set_structure(struct)
self.writer.save(out_path, select=SelectChains(chain_letters))
return out_path
class SelectChains(PDB.Select):
""" Only accept the specified chains when saving. """
def __init__(self, chain_letters):
self.chain_letters = chain_letters
def accept_chain(self, chain):
return (chain.get_id() in self.chain_letters)
if __name__ == "__main__":
""" Parses PDB id's desired chains, and creates new PDB structures. """
import sys
if not len(sys.argv) == 2:
print "Usage: $ python %s 'pdb.txt'" % __file__
sys.exit()
pdb_textfn = sys.argv[1]
pdbList = PDB.PDBList()
splitter = ChainSplitter("/home/steve/chain_pdbs") # Change me.
with open(pdb_textfn) as pdb_textfile:
for line in pdb_textfile:
pdb_id = line[:4].lower()
chain = line[4]
pdb_fn = pdbList.retrieve_pdb_file(pdb_id)
splitter.make_pdb(pdb_fn, chain)
마지막으로 참고 : 는 PDB 파일에 자체 파서을 쓰지 않는다. 형식 사양은 못생긴 (정말 못생긴)이며, 결함이있는 PDB 파일의 양은 엄청납니다. 당신을 위해 파싱을 처리 할 BioPython과 같은 도구를 사용하십시오!
wget
을 사용하는 대신 PDB 데이터베이스와 상호 작용하는 도구를 사용해야합니다. FTP 연결 제한 사항, PDB 데이터베이스의 변화하는 특성 등을 고려합니다. 데이터베이스의 변경 사항을 처리하려면 updated Bio.PDBList
을 알아야합니다. =)
코드 및 설명 주셔서 감사합니다. 하지만 코드를 실행하는 방법을 모르겠습니다. 당신이 나를 도울 수? – user1545114
예. 나는 당신의 목표에 특별히 맞는 방법을 추가했습니다. 파일을 이동할 디렉터리로 본체의 줄을 변경하십시오. [Python 설치] (http://www.python.org/getit/) (아마도 이미 $ python으로 실행 해보십시오), [BioPython] (http://biopython.org/wiki/Download #Installation_Instructions). 위의 파일을'.py' 확장자 (예 :'extract.py')로 저장 한 다음'$ python extract.py pdb.txt'를 실행하십시오. 그게 다야! –
더 많은 생물 정보학 관련 작업을 수행하고 있다면 파이썬을 배우는 것이 좋습니다.그것은 현장에서 매우 유명합니다 ([BioPython] (http://biopython.org)와 [PyMOL] (http://pymol.org), 두 가지 좋은 예입니다), 그리고 그것은 좋은 일반 언어입니다. [Python docs] (http://docs.python.org)와 [Think Python] (http://www.greenteapress.com/thinkpython/) 모두 좋은 출발점입니다. –