Skip to content

Commit

Permalink
add LayoutManager to FLASHTagger
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mdavid-m committed May 23, 2024
1 parent f382ca9 commit f0c66ce
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 97 deletions.
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def flashtagPages():
Page("app.py", "FLASHViewer", "🏠"),
Page("pages/FLASHTaggerWorkflow.py", "Workflow", "⚙️"),
Page("pages/FileUploadTagger.py", "File Upload", "📁"),
Page("pages/LayoutManagerTagger.py", "Layout Manager", "📝️"),
Page("pages/FLASHTaggerViewer.py", "Viewer", "👀"),
])

Expand Down
110 changes: 13 additions & 97 deletions pages/FLASHTaggerViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@
# Annotated Spectrum will be replaced with new component including tags


def sendDataToJS(selected_data, layout_info_per_exp):
st.session_state.input_sequence = 'TENWMFIGQALRLTHNYRRPNQDLECVKGYRGDSLEAIVLWERRHSFPCI'

# getting data
def sendDataToJS(selected_data, layout_info_per_exp, grid_key='flash_viewer_grid'):

selected_anno_file = selected_data.iloc[0]['Annotated Files']
selected_deconv_file = selected_data.iloc[0]['Deconvolved Files']
selected_tag_file = selected_data.iloc[0]['Tag Files']
selected_db_file = selected_data.iloc[0]['DB Files']
selected_db_file = selected_data.iloc[0]['Protein Files']

# getting data from mzML files
spec_df = st.session_state['deconv_dfs_tagger'][selected_deconv_file]
anno_df = st.session_state['anno_dfs_tagger'][selected_anno_file]
#spec_df.to_excel('Deconv.xlsx')
#anno_df.to_excel('Anno.xlsx')
tag_df = st.session_state['tag_dfs_tagger'][selected_tag_file]

# Process tag df into a linear data format
Expand Down Expand Up @@ -100,87 +96,6 @@ def sendDataToJS(selected_data, layout_info_per_exp):
str(sequence), p_cov, np.max(coverage)
)






# # Stores sequence information {id : {sequence, coverage}}
# sequence_data = {}

# tag_df = tag_df[~pd.isna(tag_df['ProteinIndex'])]
# tag_df['Scan'] = 0
# for i, row in tag_df.iterrows():
# if isinstance(row['ProteinIndex'], str) and (';' in row['ProteinIndex']):
# tag_df.loc[i, 'ProteinIndex'] = row['ProteinIndex'].split(';')[0]


# protein_df = tag_df.loc[:,['ProteinIndex', 'ProteinAccession', 'ProteinDescription']].drop_duplicates()
# for i, row in protein_df.iterrows():
# if not pd.isna(row['ProteinDescription']):
# acc = f"{row['ProteinAccession']} {row['ProteinDescription']}"
# else:
# acc = row['ProteinAccession']
# if pd.isna(acc):
# continue
# if ';' in acc:
# acc = acc.split(';')[0]
# protein_df.loc[i, 'ProteinAccession'] = acc
# protein_df.loc[i,'length'] = len(protein_db[acc])
# protein_df.loc[i,'sequence'] = str(protein_db[acc])
# sequence_data[row['ProteinIndex']] = {'sequence' : protein_db[acc]}

# protein_df = protein_df.rename(
# columns={
# 'ProteinIndex' : 'index',
# 'ProteinAccession' : 'accession',
# 'ProteinDescription' : 'description'
# }
# )
# protein_df.loc[:,'index'] = protein_df['index'].astype('int')

# # Augment tags with sequence position
# for i, row in tag_df.iterrows():
# pid = row['ProteinIndex']
# if pd.isna(pid):
# continue
# sequence = sequence_data[pid]['sequence']
# tag_sequence = row['TagSequence']
# for j in range(len(sequence)):
# if str(sequence[j:j+len(tag_sequence)]) == str(tag_sequence):
# tag_df.loc[i,'StartPos'] = j
# tag_df.loc[i,'EndPos'] = j+len(tag_sequence)-1
# break

# tag_df = tag_df[~pd.isna(tag_df['StartPos']) | ~pd.isna(tag_df['EndPos'])]

# # Compute coverage
# for pid, data in sequence_data.items():
# sequence = data['sequence']
# coverage = np.zeros(len(sequence), dtype='float')
# for i in range(len(sequence)):
# coverage[i] = np.sum(
# (tag_df['ProteinIndex'] == pid) &
# (tag_df['StartPos'] <= i) &
# (tag_df['EndPos'] >= i)
# )
# p_cov = np.zeros(len(coverage))
# if np.max(coverage) > 0:
# p_cov = coverage/np.max(coverage)
# sequence_data[pid] = getFragmentDataFromSeq(
# str(sequence), p_cov, np.max(coverage)
# )

# tag_df.loc[:,'ProteinIndex'] = tag_df['ProteinIndex'].astype('int')
# tag_df.loc[:,'TagIndex'] = tag_df['TagIndex'].astype('int')
# if 'Score' not in tag_df.columns:
# tag_df.loc[:,'Score'] = tag_df['DeNovoScore']

# print(protein_df)
# print(protein_df.columns)
# print(tag_df)
# print(tag_df.columns)

components = []
data_to_send = {}
per_scan_contents = {'mass_table': False, 'anno_spec': False, 'deconv_spec': False, '3d': False}
Expand Down Expand Up @@ -211,10 +126,10 @@ def sendDataToJS(selected_data, layout_info_per_exp):
component_arguments = Tabulator('MassTable')
elif comp_name == 'protein_table':
data_to_send['protein_table'] = protein_df
data_to_send['per_scan_data'] = getSpectraTableDF(spec_df)
component_arguments = Tabulator('ProteinTable')
elif comp_name == 'tag_table':
data_to_send['tag_table'] = tag_df
data_to_send['per_scan_data'] = getSpectraTableDF(spec_df)
component_arguments = Tabulator('TagTable')
elif comp_name == '3D_SN_plot':
per_scan_contents['3d'] = True
Expand Down Expand Up @@ -260,7 +175,7 @@ def sendDataToJS(selected_data, layout_info_per_exp):
# Set sequence data
data_to_send['sequence_data'] = sequence_data

flash_viewer_grid_component(components=components, data=data_to_send)
flash_viewer_grid_component(components=components, data=data_to_send, component_key=grid_key)


def setSequenceViewInDefaultView():
Expand Down Expand Up @@ -292,15 +207,15 @@ def content():
st.selectbox("choose experiment", experiment_df['Experiment Name'], key="selected_experiment0")
selected_exp0 = experiment_df[experiment_df['Experiment Name'] == st.session_state.selected_experiment0]
layout_info = DEFAULT_LAYOUT
if "saved_layout_setting" in st.session_state: # when layout manager was used
layout_info = st.session_state["saved_layout_setting"][0]
if "saved_layout_setting_tagger" in st.session_state: # when layout manager was used
layout_info = st.session_state["saved_layout_setting_tagger"][0]
with st.spinner('Loading component...'):
sendDataToJS(selected_exp0, layout_info)

### for multiple experiments on one view
if "saved_layout_setting" in st.session_state and len(st.session_state["saved_layout_setting"]) > 1:
if "saved_layout_setting_tagger" in st.session_state and len(st.session_state["saved_layout_setting_tagger"]) > 1:

for exp_index, exp_layout in enumerate(st.session_state["saved_layout_setting"]):
for exp_index, exp_layout in enumerate(st.session_state["saved_layout_setting_tagger"]):
if exp_index == 0: continue # skip the first experiment

st.divider() # horizontal line
Expand All @@ -311,13 +226,14 @@ def content():

selected_exp = experiment_df[
experiment_df['Experiment Name'] == st.session_state["selected_experiment%d"%exp_index]]
layout_info = st.session_state["saved_layout_setting"][exp_index]
layout_info = st.session_state["saved_layout_setting_tagger"][exp_index]
print(layout_info)
with st.spinner('Loading component...'):
sendDataToJS(selected_exp, layout_info)
sendDataToJS(selected_exp, layout_info, 'flash_viewer_grid_%d' % exp_index)


selected_tags = selected_exp0.iloc[0]['Tag Files']
selected_proteins = selected_exp0.iloc[0]['DB Files']
selected_proteins = selected_exp0.iloc[0]['Protein Files']
tag_df = st.session_state['tag_dfs_tagger'][selected_tags]
protein_df = st.session_state['protein_dfs_tagger'][selected_proteins]

Expand Down
Loading

0 comments on commit f0c66ce

Please sign in to comment.