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 調(diào)用blastn钥屈,blastn輸出結(jié)果不保存到文件里窃祝,而是輸出到屏幕,輸出到屏幕的內(nèi)容需要用io.StringIO轉(zhuǎn)化一下才能被NCBIXML解析
https://janakiev.com/blog/python-shell-commands/
這個(gè)鏈接主要介紹的是python subprocess 調(diào)用blastn晶渠,blastn輸出結(jié)果不保存到文件里吼旧,而是輸出到屏幕 畜眨,然后如何將輸出到屏幕的內(nèi)容保存到一個(gè)python 對(duì)象里
https://stackabuse.com/the-python-tempfile-module/
這個(gè)鏈接主要介紹了如何生成臨時(shí)文件(用于存儲(chǔ)用戶(hù)上傳的fasta文件)
https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file
臨時(shí)文件寫(xiě)入內(nèi)容的時(shí)候不知道為啥總是提示沒(méi)有權(quán)限乘陪,這個(gè)鏈接里稍微有點(diǎn)介紹
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
完整代碼
(還很不完善,只是勉強(qiáng)可以運(yùn)行)
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)
運(yùn)行代碼
streamlit run main.py
如何部署呢奄容?
在查資料吧
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1冰更、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2昂勒、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)蜀细、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記戈盈;3奠衔、生物信息學(xué)入門(mén)學(xué)習(xí)資料及自己的學(xué)習(xí)筆記谆刨!