(还很不美满,只是勉强可以运行)
import streamlit as stimport tempfileimport osimport iofrom Bio import SeqIOimport subprocessfrom Bio.Blast import NCBIXMLimport pandas as pdst.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 ownblastn = "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)运行代码
streamlit run main.py怎样摆设呢?
在查资料吧
接待各人关注我的公众号 小明的数据分析条记本