-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdb_extract_chains.py
executable file
·46 lines (35 loc) · 1.42 KB
/
pdb_extract_chains.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
#/Users/maghoi/opt/anaconda3/envs/py310/bin/python
import sys
import numpy as np
import biotite.structure.io as strucio
import biotite.structure.io.pdb as pdb
def get_n_residues_chains(A):
n_residues = (A.atom_name == 'CA').sum()
chains = list(np.unique(A.chain_id))
return n_residues, chains
def extract_and_save_chains(input_pdb_path, output_pdb_path, output_chains):
# Load atom array
pdb_file = pdb.PDBFile.read(input_pdb_path)
A_in = pdb_file.get_structure(model=1, extra_fields=["b_factor"])
n_residues, chains = get_n_residues_chains(A_in)
print(f"Read {n_residues} residues, chains {chains} from {input_pdb_path}")
# Extract the chains
mask = np.full(len(A_in), False)
for c in output_chains:
m = A_in.chain_id == c
mask += m
# Only extract selected chains
A_out = A_in[mask >= 1]
# Save the new structure to a PDB file
n_residues, chains = get_n_residues_chains(A_out)
strucio.save_structure(output_pdb_path, A_out)
print(f"Saving {n_residues} residues, chains {chains} to {output_pdb_path}")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage: python script.py input.pdb output.pdb chain1 chain2 ...")
sys.exit(1)
input_pdb_path = sys.argv[1]
output_pdb_path = sys.argv[2]
chains = sys.argv[3:]
assert output_pdb_path[-4:] == ".pdb"
extract_and_save_chains(input_pdb_path, output_pdb_path, chains)