tiger / app.py
Andrew Stirn
ability to run locally
a2591b6
raw
history blame
3.59 kB
import pandas as pd
import streamlit as st
import os, shutil
from tiger import tiger_exhibit, load_transcripts, TARGET_LEN, NUCLEOTIDE_TOKENS
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
# title and instructions
st.title('TIGER Cas13 Efficacy Prediction')
st.session_state["fasta_seq"] = ""
st.session_state["text_seq"] = ""
status_bar, status_text = None, None
with st.form(key='calc_options'):
c1_names = ['On Target Only', 'On and Off Target']
option = st.radio('Calculation Options', c1_names, index=0)
submitButton = st.form_submit_button(label='Choose Options')
# UserInput Form from text input
text_form = st.form("text")
text_input = text_form.text_input(
label='Enter a target transcript:',
#value='ATGCAGGACGCGGAGAACGTGGCGGTGCCCGAGGCGGCCGAGGAGCGCGC',
placeholder='Upper or lower case')
if text_input:
# input is too short
if len(text_input) < TARGET_LEN:
transcript_len = len(text_input)
text_form.write('Transcript length ({:d}) must be at least {:d} bases.'.format(transcript_len, TARGET_LEN))
else:
st.session_state["text_seq"] = text_input
text_calc = text_form.form_submit_button(label="calculate")
#status bar
status_text_textform = text_form.empty()
status_bar_textform = text_form.progress(0)
# UserInput Form from file
fasta_form = st.form("fasta")
fasta = fasta_form.file_uploader(label="upload fasta file")
if fasta:
if os.path.exists("temp"):
shutil.rmtree("temp")
os.makedirs("temp")
fname = fasta.name
st.write(fname)
fpath = os.path.join("temp", fname)
with open(fpath, "w") as f:
f.write(fasta.getvalue().decode("utf-8"))
transcript_tbl = load_transcripts([fpath])
fasta_form.text("fasta file contents")
fasta_form.write(transcript_tbl)
seq = transcript_tbl['seq'][0]
st.session_state["fasta_seq"] = seq
fasta_calc = fasta_form.form_submit_button(label="calculate")
status_text_fastaform = fasta_form.empty()
status_bar_fastaform = fasta_form.progress(0)
#st.write(text_calc)
#st.write(fasta_calc)
#Calculation
if text_calc:
src_seq = st.session_state["text_seq"]
status_text = status_text_textform
status_bar= status_bar_textform
elif fasta_calc:
src_seq = st.session_state["fasta_seq"]
status_text = status_text_fastaform
status_bar= status_bar_fastaform
else:
src_seq = ""
#st.write(src_seq)
# valid input
if src_seq and all([True if nt.upper() in NUCLEOTIDE_TOKENS.keys() else False for nt in src_seq]):
on_target, off_target = tiger_exhibit(pd.DataFrame(dict(id=['ManualEntry'], seq=[src_seq])),
status_bar, status_text, check_off_targets=option == 'On and Off Target')
on_target.rename(columns={"Guide":"23 nt guide sequence"}, inplace=True)
if len(on_target)>0:
if on_target.iloc[0]["On-target ID"] == 0:
on_target.drop(["On-target ID"], axis=1, inplace=True)
st.write('On-target predictions: ', on_target)
st.download_button(label='Download', data=convert_df(on_target), file_name='on_target.csv', mime='text/csv')
if len(off_target) > 0:
off_target.rename(columns={"Guide":"23 nt guide sequence"}, inplace=True)
st.write('Off-target predictions: ', off_target)
st.download_button(label='Download', data=convert_df(off_target), file_name='off_target.csv', mime='text/csv')
else:
st.write('We did not find any off-target effects!')
# invalid input
#else:
# st.write('Nucleotides other than ACGT detected!')