-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdb_to_fasta.py
57 lines (47 loc) · 1.38 KB
/
pdb_to_fasta.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
47
48
49
50
51
52
53
54
55
56
57
import os
import sys
threetoone = {
"CYS": "C",
"ASP": "D",
"SER": "S",
"GLN": "Q",
"LYS": "K",
"ILE": "I",
"PRO": "P",
"THR": "T",
"PHE": "F",
"ASN": "N",
"GLY": "G",
"HIS": "H",
"LEU": "L",
"ARG": "R",
"TRP": "W",
"ALA": "A",
"VAL": "V",
"GLU": "E",
"TYR": "Y",
"MET": "M",
}
def pdb_to_sequence(pdb_filename: str) -> str:
"""Extract sequence from PDB file and return it as a string."""
sequence = ""
with open(pdb_filename, "r") as file:
for line in file.readlines():
if line.startswith("ATOM") and line[12:16].strip() == "CA":
sequence += threetoone[line[17:20].strip()]
return sequence
def bulk_pdb_to_fasta(pdb_dir: str) -> str:
"""Extract sequences from all PDB files in a directory and return them as a FASTA string"""
fasta = ""
for filename in os.listdir(pdb_dir):
if filename.endswith(".pdb"):
fasta += ">" + filename[:-4] + "\n"
fasta += pdb_to_sequence(os.path.join(pdb_dir, filename)) + "\n"
return fasta
def run(pdb_dir: str, output: str) -> None:
"""Run the bulk_pdb_to_fasta function on all PDB files in a directory and write the result to a file."""
fasta = bulk_pdb_to_fasta(pdb_dir)
with open(output, "w") as file:
file.write(fasta)
if __name__ == "__main__":
run(*sys.argv[1:])