script for saving pdb chains in single file

This commit is contained in:
Tanushree Tunstall 2020-05-15 13:44:57 +01:00
parent 01a7cbf26e
commit f7e371a585
4 changed files with 103 additions and 17 deletions

18
scripts/chain_extract.py Normal file → Executable file
View file

@ -10,21 +10,15 @@
from Bio.PDB import PDBParser, PDBIO, Select
io = PDBIO()
pdb = PDBParser().get_structure("3byw", "3byw.pdb")
# Select() Method to return True for every chain in 'chains'
class ChainSelect(Select):
class ChainExtract(Select):
def __init__(self, chain):
self.chain = chain
def accept_chain(self, chain):
#print dir(chain)
if chain.id in chains:
#print(dir(chain))
if chain.id in self.chain:
return 1
else:
return 0
if __name__ == '__main__':
chains = ['G', 'H'] # specify selected chains
io.set_structure(pdb)
io.save(pdb.get_id() + "_crop.pdb", ChainSelect())

74
scripts/pdb_chain_extract.py Executable file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env python3
# Copyright 2020, Tanushree Tunstall
# This program is distributed under General Public License v. 3. See the file
# COPYING for a copy of the license.
__description__ = \
"""
chain_extract.py
extract chain/s from pdb and saves each chain as a separate file
"""
__author__ = "Tanushree Tunstall"
__date__ = ""
#=======================================================================
import os, shutil, sys
#from pdbtools.helper import cmdline
from chain_extract import ChainExtract
from Bio.PDB import PDBParser, PDBIO, Select
#from Bio.PDB.PDBParser import PDBParser
#io = PDBIO()
import argparse
#=======================================================================
def main():
"""
Function to call if run from command line.
Example use:
pdb_chain_extract.py -f <your_pdb_file> -c <chainid1><chainid2>
Extracts chain 'A' by default.
FIXME: extract all chains from the given pdb and write them out individually
"""
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-i', '--pdb_file', help='provide pdb file', default = 'None')
arg_parser.add_argument('-c', '--chain', help='chain/s to extract without spaces.', nargs = '+', default = 'A', type = list)
arg_parser.add_argument('-p', '--out_path', help='specify output path', default = '.', type = str)
arg_parser.add_argument('-o', '--out_file', help='specify output filename. Will be used as a prefix to append chain id and pdb file extension', default = 'pdbfile', type = str)
args = arg_parser.parse_args()
# Extract chains and write each chain as a separate file
pdb_file = args.pdb_file
print('input pdb file:', pdb_file)
# type = list, makes it a list of lists. Hence extracting the list of chains.
chains = args.chain[0]
#chains = ['A','B','C']
print ('user supplied chain:', chains)
# output filename and path
outpath = args.out_path
outfile = args.out_file
# get structure
p = PDBParser(PERMISSIVE=1)
structure = p.get_structure(pdb_file, pdb_file)
print('input pdb filename:', structure.get_id())
my_chains = chains
#my_chains = ['G', 'H']
c_names = ''.join(my_chains)
print('Extracting chains:', my_chains)
pdb_chains_file = outpath + '/' + outfile + '_' + c_names + '.pdb'
io = PDBIO()
io.set_structure(structure)
#io.save(structure.get_id() + "_crop.structure", ChainExtract())
#io.save("_crop.pdb", ChainExtract(my_chains))
#io.save('{}'.format(pdb_chains_file), ChainExtract(my_chains))
io.save(pdb_chains_file, ChainExtract(my_chains))
if __name__ == "__main__":
main()

View file

@ -40,7 +40,7 @@ def main():
# Extract chains and write each chain as a separate file
pdb_file = args.pdb_file
print('inpput pdb file:', pdb_file)
print('input pdb file:', pdb_file)
# type = list, makes it a list of lists. Hence extracting the list of chains.
chains = args.chain[0]

View file

@ -23,3 +23,21 @@ home/tanu/git/LSHTM_analysis/scripts/pdbtools/scripts/pdb_residue_renumber /home
/home/tanu/git/LSHTM_analysis/scripts/pdbtools/scripts/pdb_ligand /home/tanu/git/Data/ethambutol/input/3byw.pdb
/home/tanu/git/LSHTM_analysis/scripts/pdbtools/scripts/pdb_ligand /home/tanu/git/Data/cycloserine/input/alr_complex_model.pdb
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# my pdb tools
#======================================================
# save specifed chains as individual pdbs
#======================================================
./pdb_chain_splitter.py -i /home/tanu/git/Data/ethambutol/input/3byw.pdb -c DF -p /home/tanu/git/Data/ethambutol/input -o 3byw
#======================================================
# save specifed chains as one pdb
#======================================================
./pdb_chain_extract.py -i /home/tanu/git/Data/ethambutol/input/3byw.pdb -c DF -p /home/tanu/git/Data/ethambutol/input -o 3byw^C