前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用python的streamlit模块搭建一个简易的网页版blast

使用python的streamlit模块搭建一个简易的网页版blast

作者头像
用户7010445
发布2023-01-06 20:36:42
1K0
发布2023-01-06 20:36:42
举报

streamlit的参考资料

https://docs.streamlit.io/library/get-started/create-an-app

st.button https://docs.streamlit.io/library/api-reference/widgets/st.button

st.text_area https://docs.streamlit.io/library/api-reference/widgets/st.text_area

python io https://docs.python.org/3/library/io.html

io.StringIO 主要作用 python subprocess 调用blastn,blastn输出结果不保存到文件里,而是输出到屏幕,输出到屏幕的内容需要用io.StringIO转化一下才能被NCBIXML解析

https://janakiev.com/blog/python-shell-commands/

这个链接主要介绍的是python subprocess 调用blastn,blastn输出结果不保存到文件里,而是输出到屏幕 ,然后如何将输出到屏幕的内容保存到一个python 对象里

https://stackabuse.com/the-python-tempfile-module/

这个链接主要介绍了如何生成临时文件(用于存储用户上传的fasta文件)

https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file

临时文件写入内容的时候不知道为啥总是提示没有权限,这个链接里稍微有点介绍

st.datatable https://docs.streamlit.io/1.3.0/library/api-reference/data/st.dataframe

https://www.metagenomics.wiki/tools/blast/blastn-output-format-6

blastn output format 6 的表头

st.file_uploader https://docs.streamlit.io/library/api-reference/widgets/st.file_uploader

完整代码

(还很不完善,只是勉强可以运行)

代码语言:javascript
复制
import streamlit as st
import tempfile
import os
import io
from Bio import SeqIO
import subprocess
from Bio.Blast import NCBIXML
import pandas as pd

st.title("Learn how to build web blast app using streamlit")

# abc = st.text_area(label="paste your fasta here",
#              value=">seq1\nATCGA",
#              height=200)

# #print(abc)
# if st.button('save'):
#     # for line in abc:
#     #     st.write(line)
#     with open('abc.txt','w') as fw:
#         fw.write(abc)
        
#     st.write("OK")
# result = st.button("Click Here")
# 
# # st.write(result)
# print(os.getcwd())
# if result:
#     with tempfile.TemporaryFile() as fp:
#         tmp = tempfile.NamedTemporaryFile(suffix=".fasta",delete=False)
#         st.write(tmp.name)
#         tmp.write(bytes(abc,'utf-8'))
#         tmp.seek(0)
#         with open(tmp.name,'r') as fr:
#             for line in fr:
#                 st.write(line)
#         #os.write(new_file,b'abcde')
#         #st.write("OK")
#         #os.close(new_file)
#         # with open(tmp.name,'w') as fw:
#         #     fw.write(abc)
#     st.write(":smile:")
    

# you need to change this path to you own
blastn = "D:/Biotools/blast/ncbiblast/bin/blastn"
db = 'D:/Bioinformatics_Intro/streamlit/uploadfiles/blastdb/cpvirus'
tempfile.tempdir = "D:/Bioinformatics_Intro/streamlit/uploadfiles/temp"

fasta = st.text_area(label="you can paste your fasta here",
             value=">seq1\nATCGA",
             height=400)

runblastn = st.button("run blastn")

names = "qseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore".split()

if runblastn:
    tmp = tempfile.NamedTemporaryFile(suffix=".fasta",delete=False)
    st.write(tmp.name)
    
    tmp.write(bytes(fasta,'utf-8'))
    tmp.seek(0)
    for rec in SeqIO.parse(tmp.name,'fasta'):
        st.write(rec.id)
        
    cmd = [blastn,'-db',db,'-query',tmp.name,'-evalue','0.0001','-outfmt','6']
    process = subprocess.Popen(cmd,stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               universal_newlines=True)
    stdout,stderr = process.communicate()
    # for record in NCBIXML.parse(io.StringIO(stdout)):
    #     st.write(record.query)
    
    df = pd.read_csv(io.StringIO(stdout),sep="\t",header=None,names=names)
    st.dataframe(df)
    tmp.close()
    os.unlink(tmp.name)
    
    
uploaded_file = st.file_uploader("or upload your fasta file here")

if uploaded_file is not None:
    bytes_data = uploaded_file.getvalue()
    #print(type(bytes_data))
    #st.write(bytes_data)
    tmp = tempfile.NamedTemporaryFile(suffix=".fasta",delete=False)
    st.write(tmp.name)
    try:
        tmp.write(bytes_data)
        tmp.seek(0)
        with open(tmp.name,'r') as fr:
            for line in fr:
                if line.startswith(">"):
                    st.write("input seq id is: %s"%(line.strip().replace(">","")))
                
        cmd = [blastn,'-db',db,'-query',tmp.name,'-evalue','0.0001','-outfmt','6']
        process = subprocess.Popen(cmd,stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                universal_newlines=True)
        stdout,stderr = process.communicate()
        # for record in NCBIXML.parse(io.StringIO(stdout)):
        #     st.write(record.query)
        
        df = pd.read_csv(io.StringIO(stdout),sep="\t",header=None,names=names)
        st.dataframe(df)
    finally:
        tmp.close()
        os.unlink(tmp.name)

运行代码

代码语言:javascript
复制
streamlit run main.py

如何部署呢?

在查资料吧

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • streamlit的参考资料
  • 完整代码
  • 运行代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档