astirn commited on
Commit
7c87ab2
·
1 Parent(s): da98162
Files changed (1) hide show
  1. app.py +48 -61
app.py CHANGED
@@ -9,89 +9,76 @@ def convert_df(df):
9
  # IMPORTANT: Cache the conversion to prevent computation on every rerun
10
  return df.to_csv().encode('utf-8')
11
 
12
- # title and instructions
 
13
  st.title('TIGER Cas13 Efficacy Prediction')
14
- st.session_state["fasta_seq"] = ""
15
- st.session_state["text_seq"] = ""
16
- status_bar, status_text = None, None
17
 
 
18
  with st.form(key='calc_options'):
19
- c1_names = ['On Target Only', 'On and Off Target']
20
- option = st.radio('Calculation Options', c1_names, index=0)
21
- submitButton = st.form_submit_button(label='Choose Options')
22
 
23
- # UserInput Form from text input
24
- text_form = st.form("text")
25
- text_input = text_form.text_input(
26
  label='Enter a target transcript:',
27
  #value='ATGCAGGACGCGGAGAACGTGGCGGTGCCCGAGGCGGCCGAGGAGCGCGC',
28
  placeholder='Upper or lower case')
29
- if text_input:
30
- # input is too short
31
- if len(text_input) < TARGET_LEN:
32
- transcript_len = len(text_input)
33
- text_form.write('Transcript length ({:d}) must be at least {:d} bases.'.format(transcript_len, TARGET_LEN))
34
  else:
35
- st.session_state["text_seq"] = text_input
36
- text_calc = text_form.form_submit_button(label="calculate")
37
- #status bar
38
- status_text_textform = text_form.empty()
39
- status_bar_textform = text_form.progress(0)
40
-
41
 
42
- # UserInput Form from file
43
- fasta_form = st.form("fasta")
44
- fasta = fasta_form.file_uploader(label="upload fasta file")
45
- if fasta:
46
- if os.path.exists("temp"):
47
- shutil.rmtree("temp")
48
- os.makedirs("temp")
49
- fname = fasta.name
50
- st.write(fname)
51
- fpath = os.path.join("temp", fname)
52
- with open(fpath, "w") as f:
53
- f.write(fasta.getvalue().decode("utf-8"))
54
  transcript_tbl = load_transcripts([fpath])
55
- fasta_form.text("fasta file contents")
56
  fasta_form.write(transcript_tbl)
57
  seq = transcript_tbl['seq'][0]
58
- st.session_state["fasta_seq"] = seq
59
- fasta_calc = fasta_form.form_submit_button(label="calculate")
60
- status_text_fastaform = fasta_form.empty()
61
- status_bar_fastaform = fasta_form.progress(0)
62
- #st.write(text_calc)
63
- #st.write(fasta_calc)
64
 
65
- #Calculation
66
- if text_calc:
67
- src_seq = st.session_state["text_seq"]
68
- status_text = status_text_textform
69
- status_bar= status_bar_textform
70
- elif fasta_calc:
71
- src_seq = st.session_state["fasta_seq"]
72
- status_text = status_text_fastaform
73
- status_bar= status_bar_fastaform
74
  else:
75
- src_seq = ""
76
- #st.write(src_seq)
77
 
78
  # valid input
79
  if src_seq and all([True if nt.upper() in NUCLEOTIDE_TOKENS.keys() else False for nt in src_seq]):
80
  on_target, off_target = tiger_exhibit(pd.DataFrame(dict(id=['ManualEntry'], seq=[src_seq])),
81
- status_bar, status_text, check_off_targets=option == 'On and Off Target')
82
- on_target.rename(columns={"Guide":"23 nt guide sequence"}, inplace=True)
83
- if len(on_target)>0:
84
- if on_target.iloc[0]["On-target ID"] == 0:
85
- on_target.drop(["On-target ID"], axis=1, inplace=True)
86
  st.write('On-target predictions: ', on_target)
87
  st.download_button(label='Download', data=convert_df(on_target), file_name='on_target.csv', mime='text/csv')
88
- if len(off_target) > 0:
89
- off_target.rename(columns={"Guide":"23 nt guide sequence"}, inplace=True)
90
  st.write('Off-target predictions: ', off_target)
91
  st.download_button(label='Download', data=convert_df(off_target), file_name='off_target.csv', mime='text/csv')
92
- else:
93
  st.write('We did not find any off-target effects!')
94
 
95
  # invalid input
96
- #else:
97
- # st.write('Nucleotides other than ACGT detected!')
 
9
  # IMPORTANT: Cache the conversion to prevent computation on every rerun
10
  return df.to_csv().encode('utf-8')
11
 
12
+
13
+ # title and initialization
14
  st.title('TIGER Cas13 Efficacy Prediction')
15
+ st.session_state['manual_seq'] = ''
16
+ st.session_state['fasta_seq'] = ''
 
17
 
18
+ # run mode selection
19
  with st.form(key='calc_options'):
20
+ c1_names = ['On-target', 'On- and off-target']
21
+ option = st.radio('Select mode:', c1_names, index=0)
22
+ submitButton = st.form_submit_button(label='Choose options')
23
 
24
+ # text input
25
+ manual_entry = st.form('text')
26
+ manual_input = manual_entry.manual_input(
27
  label='Enter a target transcript:',
28
  #value='ATGCAGGACGCGGAGAACGTGGCGGTGCCCGAGGCGGCCGAGGAGCGCGC',
29
  placeholder='Upper or lower case')
30
+ if manual_input:
31
+ if len(manual_input) < TARGET_LEN:
32
+ manual_entry.write('Transcript must be at least {:d} bases.'.format(TARGET_LEN))
 
 
33
  else:
34
+ st.session_state['manual_seq'] = manual_input
 
 
 
 
 
35
 
36
+ # fasta input
37
+ fasta_form = st.form('fasta')
38
+ fasta_input = fasta_form.file_uploader(label='Upload a fasta file:')
39
+ if fasta_input:
40
+ if os.path.exists('temp'):
41
+ shutil.rmtree('temp')
42
+ os.makedirs('temp')
43
+ st.write(fasta_input.name)
44
+ fpath = os.path.join('temp', fasta_input.name)
45
+ with open(fpath, 'w') as f:
46
+ f.write(fasta_input.getvalue().decode('utf-8'))
 
47
  transcript_tbl = load_transcripts([fpath])
48
+ fasta_form.text('fasta file contents')
49
  fasta_form.write(transcript_tbl)
50
  seq = transcript_tbl['seq'][0]
51
+ st.session_state['fasta_seq'] = seq
 
 
 
 
 
52
 
53
+ # input-specific configuration
54
+ if manual_entry.form_submit_button(label='Calculate'):
55
+ src_seq = st.session_state['manual_seq']
56
+ status_text = manual_entry.empty()
57
+ status_bar = manual_entry.progress(0)
58
+ elif fasta_form.form_submit_button(label='Calculate'):
59
+ src_seq = st.session_state['fasta_seq']
60
+ status_text = fasta_form.empty()
61
+ status_bar = fasta_form.progress(0)
62
  else:
63
+ src_seq = status_bar = status_text = None
 
64
 
65
  # valid input
66
  if src_seq and all([True if nt.upper() in NUCLEOTIDE_TOKENS.keys() else False for nt in src_seq]):
67
  on_target, off_target = tiger_exhibit(pd.DataFrame(dict(id=['ManualEntry'], seq=[src_seq])),
68
+ status_bar, status_text, check_off_targets=option == 'On and Off Target')
69
+ on_target.rename(columns={'Guide': '23 nt guide sequence'}, inplace=True)
70
+ if len(on_target) > 0:
71
+ if on_target.iloc[0]['On-target ID'] == 0:
72
+ on_target.drop(['On-target ID'], axis=1, inplace=True)
73
  st.write('On-target predictions: ', on_target)
74
  st.download_button(label='Download', data=convert_df(on_target), file_name='on_target.csv', mime='text/csv')
75
+ if option == 'On and Off Target' and len(off_target) > 0:
76
+ off_target.rename(columns={'Guide': '23 nt guide sequence'}, inplace=True)
77
  st.write('Off-target predictions: ', off_target)
78
  st.download_button(label='Download', data=convert_df(off_target), file_name='off_target.csv', mime='text/csv')
79
+ elif option == 'On and Off Target' and len(off_target) == 0:
80
  st.write('We did not find any off-target effects!')
81
 
82
  # invalid input
83
+ else:
84
+ st.write('Invalid input!')