2222import pandas as pd
2323from pathlib import Path
2424import plotly .express as px
25+ import plotly .graph_objs as go
2526import streamlit as st
2627from streamlit_option_menu import option_menu
2728import yaml
@@ -43,6 +44,14 @@ def get_filter_metadata_file():
4344 filter_marker_data = json .load (fh )
4445
4546
47+ def get_strlist_file ():
48+ return importlib .resources .files ("lusSTR" ) / "data/str_lists.json"
49+
50+
51+ with open (get_strlist_file (), "r" ) as fh :
52+ str_lists = json .load (fh )
53+
54+
4655# ------------ Function to Generate config.yaml File ---------- #
4756
4857
@@ -197,14 +206,33 @@ def interactive_plots_allmarkers(sample_df, flagged_df):
197206 max_yvalue = (int (math .ceil (max_reads / n )) * n ) + n
198207 increase_value = int (math .ceil ((max_yvalue / 5 ) / n )) * n
199208 n = 0
200- for marker in sample_df ["Locus" ].unique ():
209+ all_loci = (
210+ str_lists ["forenseq_strs" ]
211+ if st .session_state .kit == "forenseq"
212+ else str_lists ["powerseq_strs" ]
213+ )
214+ missing_loci = [x for x in all_loci if x not in sample_df ["Locus" ].unique ()]
215+ for marker in all_loci :
201216 col = cols [n ]
202217 container = col .container (border = True )
203218 sample_locus = sample_df ["SampleID" ].unique () + "_" + marker
204- marker_df = sample_df [sample_df ["Locus" ] == marker ].sort_values (by = "CE_Allele" )
219+ sample_df = np .where (
220+ sample_df ["Locus" ] == "AMELOGENIN" ,
221+ np .where (sample_df ["CE_Allele" ] == "X" , 0 , 1 ),
222+ sample_df ["CE_Allele" ],
223+ )
224+ sample_df ["CE_Allele" ] = pd .to_numeric (sample_df ["CE_Allele" ])
225+ marker_df = sample_df [sample_df ["Locus" ] == marker ].sort_values (
226+ by = ["CE_Allele" , "allele_type" ], ascending = [False , True ]
227+ )
205228 if sample_locus in flagged_df ["key" ].values :
206229 marker = f"⚠️{ marker } ⚠️"
207- plot = interactive_plots (marker_df , marker , max_yvalue , increase_value , all = True )
230+ if marker in missing_loci :
231+ marker = f"⚠️{ marker } ⚠️"
232+ plot = go .Figure ()
233+ plot .update_layout (title = marker )
234+ else :
235+ plot = interactive_plots (marker_df , marker , max_yvalue , increase_value , all = True )
208236 container .plotly_chart (plot , use_container_width = True )
209237 if n == 3 :
210238 n = 0
@@ -240,9 +268,14 @@ def interactive_plots(df, locus, ymax, increase, all=False):
240268 )
241269 plot .add_hline (y = at , line_width = 3 , line_dash = "dot" , line_color = "gray" )
242270 plot .add_annotation (text = f"AT" , x = min_x + 0.1 , y = at , showarrow = False , yshift = 10 )
243- plot .update_layout (
244- xaxis = dict (range = [min_x , max_x ], tickmode = "array" , tickvals = np .arange (min_x , max_x , 1 ))
245- )
271+ if locus == "AMELOGENIN" :
272+ plot .update_layout (
273+ xaxis = dict (tickvals = np .arange (- 1 , 2 , 1 ), tickmode = "array" , ticktext = ["" , "X" , "Y" , "" ])
274+ )
275+ else :
276+ plot .update_layout (
277+ xaxis = dict (range = [min_x , max_x ], tickmode = "array" , tickvals = np .arange (min_x , max_x , 1 ))
278+ )
246279 if all :
247280 plot .update_layout (
248281 yaxis = dict (range = [0 , ymax ], tickmode = "array" , tickvals = np .arange (0 , ymax , increase ))
@@ -307,11 +340,16 @@ def interactive_setup(df1, file):
307340 )
308341 interactive_plots_allmarkers (sample_df , flags )
309342 else :
343+ plot_df = sample_df
344+ sample_df = np .where (
345+ sample_df ["Locus" ] == "AMELOGENIN" ,
346+ np .where (sample_df ["CE_Allele" ] == "X" , 0 , 1 ),
347+ sample_df ["CE_Allele" ],
348+ )
349+ plot_df ["CE_Allele" ] = pd .to_numeric (plot_df ["CE_Allele" ])
310350 locus_key = f"{ sample } _{ locus } "
311351 if locus_key not in st .session_state :
312- st .session_state [locus_key ] = sample_df [sample_df ["Locus" ] == locus ].reset_index (
313- drop = True
314- )
352+ st .session_state [locus_key ] = plot_df [plot_df ["Locus" ] == locus ].reset_index (drop = True )
315353 Type = [
316354 "Deleted" ,
317355 "Typed" ,
0 commit comments