python splinter 辅助blast 较多的序列
生信分析过程中,有时需要blast一些序列去看一下一些序列到底是什么东西。。一个一个的复制粘贴有点太麻烦,所以写了一个python的脚本,能够解放双手。以sam文件为例,提供一个思路。
首先,安装splinter库。前一段时间搞12306的脚本接触到的,操作起来对新手挺友好的一个爬虫库。
安装:pip install splinter
关键是再放一个chromedriver(对于谷歌浏览器)到python的路径下(默认为:C:/Users/Chipeyown/Appdata/Local/Programs/Python/Python37),因为这个路径已经加到环境变量里面去了。推荐去这个镜像上下载,速度较快且没有被墙:链接
一定要下载与版本匹配的driver
from splinter import Browser
import re
import time
fl=open('samfile.txt')
b=Browser('chrome')
url='https://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastn&PAGE_TYPE=BlastSearch&LINK_LOC=blasthome'
b.visit(url)
b.find_by_name('NEWWIN').click()#让blast的结果显示在新的窗口
#从文件中找需要blast的序列
lines=fl.readlines()
print('文件中共包含%d条序列'%len(lines))
number=input('请问想要blast多少个序列:')
pa=r'[A,G,C,T]+'
i=0
while i<int(number):
try:
line=lines[i]
res=re.findall(pa,line)
for str in res:
if len(str)>10:
seq=str
break
except:
print('文件中只有%d条序列'%i)
break
b.fill('QUERY',seq)#自动填充要去blast的序列
b.find_by_xpath('//*[@id="b1"]').click()#点击页面上的blast按钮
b.windows[0]#窗口切换回第一张
i+=1
fl.close()
#使用方法:
#在该脚本对应的文件夹下建一个名字为samfile的文本文件,然后将sam文件中内容直接复制进去即可
很简单一个脚本,5秒种之内,帮你blast30条序列。再也不用一个一个的粘贴复制了。
blast的序列别太多,小心电脑受不了卡死。。。