Skip to content

Commit f677561

Browse files
author
James Parkhurst
committed
Changed to use enums rather than strings
1 parent b1b43a6 commit f677561

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

profet/alphafold.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from requests_html import HTMLSession
1010
import requests
1111
from bs4 import BeautifulSoup
12+
from .enums import FileType
1213

1314

1415
class Alphafold_DB:
@@ -40,11 +41,13 @@ def check_structure(self, uniprot_id: str) -> bool:
4041
4142
"""
4243
uniprot_id = uniprot_id.upper()
43-
url = self.make_url(uniprot_id, "pdb")
44+
url = self.make_url(uniprot_id, FileType.pdb)
4445
r = requests.get(url)
4546
return r.status_code != 404
4647

47-
def get_file_url(self, uniprot_id: str, filetype: str = "cif") -> str:
48+
def get_file_url(
49+
self, uniprot_id: str, filetype: FileType = FileType.cif
50+
) -> str:
4851
"""
4952
Get file url relative to an id from the the Alphafold entry page
5053
@@ -56,6 +59,7 @@ def get_file_url(self, uniprot_id: str, filetype: str = "cif") -> str:
5659
The URL of the file to download
5760
5861
"""
62+
filetype = FileType(filetype)
5963

6064
# Do we recognise the filetpye, otherwise raise an exception.
6165
if filetype in ["pdb", "cif"]:
@@ -77,7 +81,9 @@ def get_file_url(self, uniprot_id: str, filetype: str = "cif") -> str:
7781
# Return the URL
7882
return url["href"]
7983

80-
def make_url(self, uniprot_id: str, filetype: str = "cif") -> str:
84+
def make_url(
85+
self, uniprot_id: str, filetype: FileType = FileType.cif
86+
) -> str:
8187
"""
8288
Make the URL for the protein
8389
@@ -89,6 +95,7 @@ def make_url(self, uniprot_id: str, filetype: str = "cif") -> str:
8995
The URL of the file to download
9096
9197
"""
98+
filetype = FileType(filetype)
9299

93100
uniprot_id = uniprot_id.upper()
94101
af_id = "AF-" + uniprot_id + "-F1"
@@ -109,7 +116,7 @@ def make_url(self, uniprot_id: str, filetype: str = "cif") -> str:
109116
def get_pdb(
110117
self,
111118
uniprot_id: str,
112-
filetype: str = "cif",
119+
filetype: FileType = FileType.cif,
113120
) -> tuple:
114121
"""
115122
Returns pdb/cif as strings, saves to file if requested.
@@ -123,6 +130,7 @@ def get_pdb(
123130
Tuple containing the filename and file from the database
124131
125132
"""
133+
filetype = FileType(filetype)
126134
# af_id = self.df.loc[self.df['Uniprot_ID'] == uniprot_id.upper()]["AF_ID"].to_numpy()[0]
127135
# version = self.df.loc[self.df['Uniprot_ID'] == uniprot_id.upper()]["version"].to_numpy()[0]
128136
# af_id = "AF-"+uniprot_id+"-F1"

profet/cache.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
from .enums import FileType
34

45

56
class PDBFileCache(object):
@@ -35,7 +36,7 @@ def __init__(self, directory: str = None):
3536
# The manifest filename
3637
self.manifest = os.path.join(self.directory, "manifest.txt")
3738

38-
def path(self, uniprot_id: str, filetype: str = "cif") -> str:
39+
def path(self, uniprot_id: str, filetype: FileType = FileType.cif) -> str:
3940
"""
4041
Get the proposed path
4142
@@ -47,6 +48,7 @@ def path(self, uniprot_id: str, filetype: str = "cif") -> str:
4748
The absolute path
4849
4950
"""
51+
filetype = FileType(filetype)
5052
assert filetype in ["pdb", "cif"]
5153
return os.path.join(self.directory, uniprot_id.lower()) + "." + filetype
5254

@@ -64,7 +66,7 @@ def find(self, uniprot_id: str) -> list:
6466
return [
6567
filename
6668
for filename in [
67-
self.path(uniprot_id, filetype) for filetype in ["pdb", "cif"]
69+
self.path(uniprot_id, filetype) for filetype in FileType
6870
]
6971
if os.path.exists(filename)
7072
]
@@ -137,7 +139,11 @@ def items(self):
137139
yield uniprot_id, self.path(uniprot_id, filetype[1:])
138140

139141
def _update_manifest(
140-
self, uniprot_id: str, fileorigin: str, filetype: str, filename: str
142+
self,
143+
uniprot_id: str,
144+
fileorigin: str,
145+
filetype: FileType,
146+
filename: str,
141147
):
142148
"""
143149
Update the manifest file
@@ -149,6 +155,7 @@ def _update_manifest(
149155
filename: The filename
150156
151157
"""
158+
filetype = FileType(filetype)
152159

153160
# Read the current manifest
154161
if os.path.exists(self.manifest):

profet/enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from enum import Enum
2+
3+
4+
class FileType(str, Enum):
5+
pdb = "pdb"
6+
cif = "cif"

profet/pdb.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from urllib.request import urlretrieve
22
from rcsbapi.search import TextQuery
3+
from .enums import FileType
34

45

56
class PDB_DB:
@@ -50,7 +51,9 @@ def check_structure(self, uniprot_id: str) -> bool:
5051
return True
5152
return False
5253

53-
def make_url(self, uniprot_id: str, filetype: str = "pdb") -> str:
54+
def make_url(
55+
self, uniprot_id: str, filetype: FileType = FileType.pdb
56+
) -> str:
5457
"""
5558
Make the URL for the protein
5659
@@ -63,14 +66,15 @@ def make_url(self, uniprot_id: str, filetype: str = "pdb") -> str:
6366
6467
"""
6568

69+
filetype = FileType(filetype)
6670
uniprot_id = uniprot_id.upper()
6771
url = f"https://files.rcsb.org/download/{uniprot_id}.{filetype}"
6872
return url
6973

7074
def get_pdb(
7175
self,
7276
uniprot_id: str,
73-
filetype: str = "cif",
77+
filetype: FileType = FileType.cif,
7478
) -> tuple:
7579
"""
7680
Returns pdb/cif as strings, saves to file if requested
@@ -84,6 +88,7 @@ def get_pdb(
8488
8589
"""
8690

91+
filetype = FileType(filetype)
8792
if not self.results:
8893
self.results = [self.uniprot_id_to_pdb_id(uniprot_id)]
8994

@@ -97,9 +102,9 @@ def get_pdb(
97102
filedata = file.read()
98103
except Exception:
99104
if filetype == "pdb":
100-
filetype = "cif"
105+
filetype = FileType.cif
101106
else:
102-
filetype = "pdb"
107+
filetype = FileType.pdb
103108
url = self.make_url(pdb_id, filetype)
104109
filename, result = urlretrieve(url)
105110
with open(filename) as file:

profet/profet.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .pdb import PDB_DB
33
from .cache import PDBFileCache
44
from .cleaver import Cleaver
5+
from .enums import FileType
56
import os
67

78

@@ -54,7 +55,7 @@ def cache(self) -> PDBFileCache:
5455
def file_from_db(
5556
self,
5657
prot_id: str,
57-
filetype: str = "cif",
58+
filetype: FileType = FileType.cif,
5859
db: str = "pdb",
5960
) -> tuple:
6061
"""
@@ -69,14 +70,15 @@ def file_from_db(
6970
Tuple containing the filename and file from the database
7071
7172
"""
73+
filetype = FileType(filetype)
7274
return {"pdb": self.pdb.get_pdb, "alphafold": self.alpha.get_pdb}[db](
7375
prot_id, filetype=filetype
7476
)
7577

7678
def get_file(
7779
self,
7880
uniprot_id: str,
79-
filetype: str = "cif",
81+
filetype: FileType = FileType.cif,
8082
filesave: bool = False,
8183
db: str = "pdb",
8284
) -> tuple:
@@ -96,6 +98,7 @@ def get_file(
9698
2. File from the database, or None if it is not available in any database.
9799
98100
"""
101+
filetype = FileType(filetype)
99102

100103
# Get the PDB cache
101104
cache = PDBFileCache(directory=self.save_directory)
@@ -211,7 +214,7 @@ def remove(
211214
"""
212215
# Get the PDB cache
213216
cache = PDBFileCache(directory=self.save_directory)
214-
identifier, _, _ = self.pdb.get_pdb(uniprot_id, filetype="cif")
217+
identifier, _, _ = self.pdb.get_pdb(uniprot_id, filetype=FileType.cif)
215218
if uniprot_id in cache:
216219
filename = cache[uniprot_id]
217220
signal_list = self.Cleaver.signal_residuenumbers_requester(

0 commit comments

Comments
 (0)