最近在做KEGG富集時遇到一個問題:phytozome上的數(shù)據(jù)采用的注釋為Locus tag,但是kobas3.0并不支持這一類型,如此需要進行轉(zhuǎn)換。如果是常見的模式物種還好,在bioconductor中很多包和一些網(wǎng)站上都提供了轉(zhuǎn)化,這里不贅述,需要的自行查找,或者直接看下文代碼。
于是,就想到編寫一個爬蟲對數(shù)據(jù)進行轉(zhuǎn)換,需要環(huán)境為python,并安裝requests庫,沒有該庫的用pip進行安裝。首先需要將Locus tag轉(zhuǎn)換為對應(yīng)的NCBI地址,格式為https://www.ncbi.nlm.nih.gov/gene/?term=Locus tag,如赤桉的ICE1的Locus tag為EUGRSUZ_G01938,那么對應(yīng)的地址就是https://www.ncbi.nlm.nih.gov/gene/?term=EUGRSUZ_G01938。這個用Excel就可以實現(xiàn),公式為=“https://www.ncbi.nlm.nih.gov/gene/?term=”&A2,其中A2為存儲Locus tag欄。
在爬蟲運行目錄提供用于存儲地址的gene_url.txt,運行如下Python爬蟲代碼:
import requests
from requests.adapters import HTTPAdapter
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))
f = open("./gene_url.txt")
t = open("./gene_id.txt", 'w')
ids = ""
line = f.readline()
while line:
? ? temp1 = line.strip('\n')
? ? try:
? ? ? ? response = requests.get(url=temp1)
? ? ? ? start = response.text.find("Gene ID: ")#替換此處查找的文本可以實現(xiàn)Gene symbol、Gene type等轉(zhuǎn)換
? ? ? ? end = response.text.find(", updated on")
? ? ? ? if start != -1 & end != -1:
? ? ? ? ? ? gid = response.text[start + 9:end]
? ? ? ? ? ? print(gid)
? ? ? ? ? ? ids += gid + '\n'
? ? except requests.exceptions.RequestException as e:
? ? ? ? print(e)
? ? line = f.readline()
t.write(ids)
f.close()
t.close()
最后轉(zhuǎn)換好的gene id就會在當前目錄下的gene_id.txt找到。
xizzy版權(quán)所有,轉(zhuǎn)載請注明出處!
最后:如果想了解更多和生信或者精品咖啡有關(guān)的內(nèi)容歡迎關(guān)注我的微信公眾號:**生信咖啡**,更多精彩等你發(fā)現(xiàn)!