-
Notifications
You must be signed in to change notification settings - Fork 0
/
bioai.py
191 lines (177 loc) · 6.87 KB
/
bioai.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
from typing import Optional
from Bio import SwissProt
import requests
import settings
import sys
import pickle
import random
class Feature:
def __init__(self):
self.type=None
self.location=None
self.qualifiers={}
pass
def create_by_feature(self,feature:SwissProt.FeatureTable):
self.type=feature.type
self.location=feature.location
for key in feature.qualifiers:
self.qualifiers[key]=feature.qualifiers[key]
pass
def __str__(self):
return f'{self.type} {self.location} {self.qualifiers}'
def chebi_download(self,path:str) -> Optional[str]:
chebiid:str=self.qualifiers.get('ligand_id')
chebistart='ChEBI:CHEBI:'
if chebiid.startswith(chebistart):
chebiid=chebiid[len(chebistart):]
print(f'Chebi id: {chebiid}')
return download_chebi_molecule(chebiid,os.path.join(path,f'{chebiid}.sdf'))
else:
return None
class Protein:
def __init__(self):
self.id=None
self.sequence=None
self.go_terms:list[int]=[]
self.features:list[Feature]=[]
self.alpha_folds=[]
self.pdb_structures=[]
pass
def create_by_entry(self,record:SwissProt.Record):
self.id = record.entry_name
self.sequence = record.sequence
for xref in record.cross_references:
if xref[0] == 'GO':
go_id = xref[1]
if not go_id.startswith('GO:'):
sys.exit(f"Invalid GO ID: {go_id}")
go_int = int(go_id[3:])
self.go_terms.append(go_int)
if xref[0]=='PDB':
self.pdb_structures.append(str(xref[1]))
if xref[0]=='AlphaFoldDB':
self.alpha_folds.append(str(xref[1]))
for feature in record.features:
protein_feature = Feature()
protein_feature.create_by_feature(feature)
self.features.append(protein_feature)
def __str__(self):
string=f'{self.id}\n{self.sequence}\nGO terms:\n'
for go in self.go_terms:
string+=f'{go}\n'
string+='Features:\n'
for feature in self.features:
string+=str(feature)+'\n'
string+='PDB structures:\n'
for pdb in self.pdb_structures:
string+=pdb+'\n'
string+='AlphaFold structures:\n'
for af in self.alpha_folds:
string+=af+'\n'
return string
def find_features_by_ligand(self,ligand:str) -> list[Feature]:
features=[]
for feature in self.features:
if feature.qualifiers.get('ligand') == ligand:
features.append(feature)
return features
def download_alpha_fold(self,directory:str) ->Optional[str]:
for af in self.alpha_folds:
return copy_alphafold_structure(af,directory)
return None
class ProteinContainer:
def __init__(self):
self.proteins:list[Protein] = []
def add_protein(self,protein:Protein):
self.proteins.append(protein)
def generate(self,skip=0,limit:int=None):
with open(settings.abs_uni_prot_db, "r") as f:
counter=0
for i in range(skip):
SwissProt.parse(f)
for record in SwissProt.parse(f):
protein = Protein()
protein.create_by_entry(record)
self.proteins.append(protein)
if limit is not None:
counter+=1
if counter > limit:
break
def generate_shuffled(self,count:int)->'ProteinContainer':
proteincontainer=ProteinContainer()
for i in range(count):
random_protein=self.select_ramdom()
if proteincontainer.find_by_id(random_protein.id) is None:
proteincontainer.add_protein(random_protein)
return proteincontainer
def select_ramdom(self)->Protein:
return random.choice(self.proteins)
def find_by_id(self,id:str) -> Protein:
for protein in self.proteins:
if protein.id == id:
return protein
return None
def get_ids(self) -> list[str]:
return [protein.id for protein in self.proteins]
def save(self,path:str=None):
if path is None:
path = settings.abs_protein_container
with open(path, "wb") as f:
pickle.dump(self,f)
def load(path:str=None) -> 'ProteinContainer':
if path is None:
path = settings.abs_protein_container
with open(path, "rb") as f:
return pickle.load(f)
def extract_ligands(self) -> dict[str:Feature]:
ligands:dict[str:Feature] = {}
ligand='ligand'
ligandid='ligand_id'
feature_type='BINDING'
for protein in self.proteins:
for feature in protein.features:
if feature.type == feature_type and feature.qualifiers.get(ligandid) is not None:
ligand_id=feature.qualifiers[ligandid]
ligand_name=feature.qualifiers.get(ligand)
extracted=ligands.get(ligand_name)
if extracted is None:
extracted=ligands[ligand_name]=feature
if extracted.qualifiers.get(ligandid) != ligand_id:
print(f'Warning: Ligand {ligand_name} has multiple ids')
return ligands
def find_by_ligand(self,ligand:str) -> list[str]:
proteins=[]
for protein in self.proteins:
for feature in protein.features:
if feature.qualifiers.get('ligand') == ligand:
proteins.append(protein.id)
break
return proteins
def get_alphafold_structure(uniprot_id) -> Optional[str]:
base_url = f"https://alphafold.ebi.ac.uk/files/AF-{uniprot_id}-F1-model_v4.pdb"
response = requests.get(base_url)
if response.status_code == 200:
pdb_data = response.text
return pdb_data
else:
return None
def copy_alphafold_structure(uniprot_id,path)->Optional[str]:
base_url = f"https://alphafold.ebi.ac.uk/files/AF-{uniprot_id}-F1-model_v4.pdb"
response = requests.get(base_url)
if response.status_code == 200:
storepath=os.path.join(path,f'{uniprot_id}.pdb')
with open(storepath, "wb") as f:
f.write(response.content)
return storepath
return None
def download_chebi_molecule(chebi_id, save_path):
#url = f'https://www.ebi.ac.uk/chebi/saveStructure.do?defaultImage=true&chebiId={chebi_id}&imageId=0'
url = f'https://www.ebi.ac.uk/chebi/saveStructure.do?sdf=true&chebiId={chebi_id}&imageId=0'
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as file:
file.write(response.content)
return save_path
else:
return None