根据Knumbers注释爬取KEGG对应KO号和pathwayname
背景
⼿头有KEGG对应各个基因的注释⽂件(注释可以参考KAAS注释流程),但没有对应的KO号,需要得到相应的KO号做富集分析
Python script
⽤selenium+xpath爬取,试了下发现KEGG最⼤接受的输⼊knumbers数量为7000左右,所以分割数据进⾏爬取(这⾥没有⽤线程池爬,要加速可以⽤多线程),注意脚本中chrome driver改成⾃⼰的地址即可。
参数三个,分别为-kn,对应你的⽤换⾏符分割的knumbers⽂件;-k2ko,对应爬取的kn对应ko关系的⽂件保存地址;-t2n,对应爬取的ko号和pathway name描述的⽂件保存地址。
得到⽂件后就可以很⽅便的⽤R整理数据,以及超⼏何检验/现成R包(例如ClusterProfiler)进⾏富集
from selenium import webdriver
# No visual interface
from selenium.webdriver.chrome.options import Options
# Avoid test
from selenium.webdriver import ChromeOptions
from lxml import etree
import os
import argparse
def setoption():
"""
Google Chrome's anti-anti-crawler parameter settings
"""
# No visual interface
chrome_options = Options()
chrome_options.add_experimental_option(
'excludeSwitches', ['enable-logging'])
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# Avoid test
option = ChromeOptions()
option.add_experimental_option("excludeSwitches",
["enable-automation"])
return chrome_options, option
def mapkn2ko(knsearch, k2kopath, term2namepath):
"""
main process to map kn to ko
:param knsearch: kn string
:param k2kopath: path to save k2ko file
:param term2namepath: path to save term2name file
"""
chrome_options, option = setoption()
bro = webdriver.Chrome(executable_path=r"/",
chrome_options=chrome_options,
options=option)
<("jp/kegg/ko.html")
param namesearch_input = bro.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/form/table/tbody/tr[2]/td/textarea')
search_input.send_keys(knsearch)
btn = bro.find_element_by_xpath('//*[@id="content"]/div[2]/div[1]/form/table/tbody/tr[3]/td/input[3]')
btn.click()
bro.switch_to_window(bro.window_handles[1])
page_text = bro.page_source
tree = etree.HTML(page_text)
li_list = tree.xpath('/html/body/div[2]/li')
for li in li_list:
ko = li.xpath('./a[1]/text()')[0]
ko_name = li.xpath('./text()')[0].split(" (")[0]
kn_list = li.xpath('./div/a//text()')
with open(k2kopath, "a") as k2ko:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论