-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdb_downloader.py
53 lines (41 loc) · 1.36 KB
/
pdb_downloader.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
import requests
import os
class PDBDownloader:
r"""
Class name: PDBDownloader
Description: given a PDB ID, downloads the PDB file from Protein Data Bank
Variables:
self._host_url: the url of Protein Data Bank
self._pdb_id: PDB ID
"""
def __init__(
self,
host_url: str = "https://files.rcsb.org/view/",
):
r"""
Object constructor.
:param host_url: the url of Protein Data Bank
"""
self._host_url = host_url
self._pdb_id = ""
def get_user_input(self) -> str:
r"""
Gets user input for PDB ID and verify.
:return: A valid PDB ID
"""
pdbid = input("Please give your PDB ID: ")
url = f"{self._host_url}{pdbid}.pdb"
while not requests.get(url).text.startswith("HEADER"):
pdbid = input("PDB ID invalid. Please try again: ")
url = f"{self._host_url}{pdbid}.pdb"
self._pdb_id = pdbid
return pdbid
def download_pdb(self) -> str:
r"""
Downloads the PDB file.
:return: The path to which the PDB file is stored
"""
current_dir = os.getcwd()
with open(self._pdb_id + ".pdb", "w") as out:
out.write(requests.get(f"{self._host_url}{self._pdb_id}.pdb").text)
return os.path.join(current_dir, f"{self._pdb_id}.pdb")