-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_CCDS.py
294 lines (269 loc) · 10.7 KB
/
find_CCDS.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import requests
import re
import queue
import threading
from bs4 import BeautifulSoup
lock_list = list()
num = None
class GeneCrawler:
def __init__(self, name, species="Homo sapiens", accession=None):
self.name = name
self.species = species
self.accession = accession
self.CCDS_url = None
self.current_accession = None
self.ID = None
def find_gene_id(self):
"""
This function is used to find the ID of the gene, callable
"""
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
}
url_gene = "https://www.ncbi.nlm.nih.gov/gene/?term=" + self.name
s = requests.Session()
req = requests.Request('GET', url=url_gene, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped, timeout=20)
if r.status_code == requests.codes.ok:
html_gene = r.text
id_pattern = re.compile(r'<span class="gene-id">ID: (\d+).*?<em>(.*?)</em>', re.S|re.I)
gene_id = re.findall(id_pattern, html_gene)
for match in gene_id:
if match[1] == self.species:
self.ID = match[0]
return match[0]
else:
pass
else:
print(self.name + ": Can not find the correct gene ID")
else:
print('status_code = {}'.format(r.status_code))
pass
def _find_gene_id(self, html_gene):
"""
This function is used to find the ID of the gene
"""
id_pattern = re.compile(r'<span class="gene-id">ID: (\d+).*?<em>(.*?)</em>', re.S|re.I)
gene_id = re.findall(id_pattern, html_gene)
for match in gene_id:
if match[1] == self.species:
self.ID = match[0]
return match[0]
else:
pass
else:
print(self.name + ": Can not find the correct gene ID")
def find_gene_url(self):
"""
This function returns the url of target gene in the gene database
"""
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
}
url_gene = "https://www.ncbi.nlm.nih.gov/gene/?term=" + self.name
s = requests.Session()
req = requests.Request('GET', url=url_gene, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped, timeout=20)
if r.status_code == requests.codes.ok:
# print(r.status_code)
r.encoding = r.apparent_encoding
html_gene = r.text
gene_id = self._find_gene_id(html_gene)
new_url = "https://www.ncbi.nlm.nih.gov/gene/" + gene_id
return new_url
else:
print('status_code = {}'.format(r.status_code))
pass
def _parse_gene_html(self, id_html):
"""
This function returns the url of CCDS
"""
CCDS_base_url = "https://www.ncbi.nlm.nih.gov/CCDS/CcdsBrowse.cgi?REQUEST=CCDS&GO=MainBrowse&DATA="
pattern = re.compile(r'CCDS(.*?)', re.S)
accession_num = self.get_accession()
soup = BeautifulSoup(id_html, "html.parser")
new_accession = soup.find_all(text=re.compile(accession_num))[0]
# print(soup.find_all(text=re.compile(accession_num)))
self.current_accession = new_accession
for item in soup.find_all(name="ol", attrs={"class": ""}):
if new_accession in item.text:
for li in item.find_all(name="li"):
if new_accession in li.text:
if not li.find(text=pattern):
print(self.name + ": Can not find CCDS in this accession")
else:
self.CCDS_url = CCDS_base_url + li.find(text=pattern)
return self.CCDS_url
else:
pass
else:
pass
print("An error has occurred in function '_parse_gene_html'")
return None
def find_mRNA_url(self, url):
"""
This function inputs gene url and returns CCDS url
"""
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
}
s = requests.Session()
req = requests.Request('GET', url=url, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped, timeout=20)
r.encoding = r.apparent_encoding
id_html = r.text
if not id_html:
print("Can not get url of the specific gene page")
else:
pass
m_url = self._parse_gene_html(id_html)
return m_url
def get_sequence(self, url):
"""
This function returns the CCDS list
"""
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36",
}
s = requests.Session()
req = requests.Request('GET', url=url, headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped, timeout=20)
r.encoding = r.apparent_encoding
html = r.text
nt_list = self.parse_html(html)
return nt_list
def parse_html(self, html):
"""
This function parses the CCDS page and finds the CCDS contents in the CCDS page
"""
nt_pattern = re.compile(r'<font color=([a-z]*?|#0089cc)>([A-Z]*?|[A-Z]*?<br>[A-Z]*?)</font>', re.S)
codon = re.findall(nt_pattern, html)
nt_list = self.decode_gene(codon)
return nt_list
@ staticmethod
def decode_gene(codon):
"""
This function decodes the CCDS contents
"""
nt_list = list()
new_list = list()
for item in range(len(codon)):
if codon[item][0] == 'black':
nt_list.append(codon[item][1])
elif codon[item][0] == '#0089cc':
nt_list.append(str.lower(codon[item][1]))
else:
pass
for string in nt_list:
new_list.append(string.replace('<br>', ''))
return new_list
def write_gene(self, nt_list):
"""
This function writes decoded gene sequence into 'panel.txt'
"""
with open('panel.txt', mode='a', encoding='utf-8') as f:
f.write('>' + self.name + '|' + self.current_accession + '\n')
for nucleotide in nt_list:
f.write(nucleotide)
f.write('\n\n')
return None
def get_accession(self):
"""
This function gets the accession number, from the first number to "."
It omits the version of the sequence
"""
str_list = list(self.accession)
index = str_list.index(".")
acce = str_list[0: index]
return "".join(acce)
class MultiThread(threading.Thread):
def __init__(self, thread_id, working_queue):
super(MultiThread, self).__init__()
self.queue = working_queue
self.ID = thread_id
def run(self):
# print("开启线程", self.ID)
if self.ID < num:
thread_process(self.ID, self.queue, lock_list[self.ID-1], lock_list[self.ID])
else:
thread_process(self.ID, self.queue, lock_list[self.ID-1], lock_list[0])
# print("结束线程", self.ID)
def thread_process(thread_id, gene_queue, lock1, lock2):
while True:
if not gene_queue.empty():
gene = gene_queue.get()
gene_crawler = GeneCrawler(gene["name"], accession=gene["accession"])
try:
gene_url = gene_crawler.find_gene_url()
mRNA_url = gene_crawler.find_mRNA_url(gene_url)
seq_list = gene_crawler.get_sequence(mRNA_url)
lock1.acquire()
gene_crawler.write_gene(seq_list)
print("thread %s is processing %s" % (thread_id, gene_crawler.name))
lock2.release()
except Exception:
print(gene_crawler.name + ": ERROR")
else:
break
def easy_find_seq(gene_to_find, threads_num=5):
global num
num = threads_num
# create work queue
work_queue = queue.Queue(len(gene_to_find))
for gene_dict in gene_to_find:
work_queue.put(gene_dict)
# create locks
global lock_list
number = 0
while number < threads_num:
lock_list.append(threading.Lock())
lock_list[number].acquire()
number += 1
lock_list[0].release()
# create threads
threads = list()
thread_ID = 1
for no_threads in range(threads_num):
thread = MultiThread(thread_ID, work_queue)
thread.start()
threads.append(thread)
thread_ID += 1
# wait for the threads to finish
for t in threads:
t.join()
print("\nJob done!")
pass
if __name__ == "__main__":
# generate gene list
gene_list = [
{"name": "AIM2", "accession": "NM_004833.1"},
{"name": "ANXA5", "accession": "NM_001154.3"},
{"name": "APOBEC3G", "accession": "NM_021822.3"},
{"name": "ARL4C", "accession": "NM_005737.3"},
{"name": "AURKB", "accession": "NM_004217.3"},
{"name": "B3GAT1", "accession": "NM_018644.3"},
{"name": "BAX", "accession": "NM_001291428.1"},
{"name": "BCL11B", "accession": "NM_022898.2"},
{"name": "BCL2", "accession": "NM_000633.2"},
{"name": "BCL6", "accession": "NM_001706.4"},
{"name": "BIN2", "accession": "NM_016293.3"},
{"name": "BTG1", "accession": "NM_001731.2"},
{"name": "BTLA", "accession": "NM_181780.3"},
{"name": "C10orf54", "accession": "NM_022153.1"},
{"name": "CASP3", "accession": "NM_004346.3"},
{"name": "CBLB", "accession": "NM_170662.4"},
{"name": "CCL1", "accession": "NM_002981.2"},
{"name": "CCL2", "accession": "NM_002982.3"},
{"name": "CCL20", "accession": "NM_004591.2"},
{"name": "CCL3", "accession": "NM_002983.2"},
{"name": "CTLA4", "accession": "NM_005214.4"}
]
easy_find_seq(gene_list)