@@ -67,6 +67,12 @@ add_zscores_ratios_to_df <- function(outlist, metabolites_ratios_df, all_sample_
6767# '
6868# ' @returns zscore_ratios_df: dataframe containing Z-scores for all ratios for all samples
6969calculate_zscore_ratios <- function (metabolites_ratios_df , intensities_zscores_df , intensity_col_names ) {
70+ # remove Z-score columns from intensity_col_names
71+ if (any(grepl(" _Zscore" , intensity_col_names ))) {
72+ intensity_col_names <- intensity_col_names [- grep(" _Zscore" , intensity_col_names )]
73+ }
74+
75+ # create empty data frame for results
7076 zscore_ratios_df <- data.frame (matrix (
7177 ncol = ncol(intensities_zscores_df ),
7278 nrow = nrow(metabolites_ratios_df )
@@ -665,14 +671,20 @@ create_pdf_violin_plots <- function(pdf_dir, patient_id, metab_perpage, top_meta
665671 for (metab_class in names(metab_perpage )) {
666672 # extract list of metabolites to plot on a page
667673 metab_zscores_df <- metab_perpage [[metab_class ]]
668- # extract original data for patient of interest (pt_name) before cut-offs
669- patient_zscore_df <- metab_zscores_df %> % filter(Sample == patient_id )
670-
671- # Remove patient column and change Z-score. If under -5 to -5 and if above 20 to 20.
674+ # copy Z-scores to Z_score_original for displaying values
675+ metab_zscores_df $ Z_score_original <- metab_zscores_df $ Z_score
676+ # Cap Z-scores under -5 to -5 and above 20 to 20
672677 metab_zscores_df <- metab_zscores_df %> %
673- filter(Sample != patient_id ) %> %
674678 mutate(Z_score = pmin(pmax(Z_score , - 5 ), 20 ))
675679
680+ # extract original data for patient of interest (pt_name)
681+ patient_zscore_df <- metab_zscores_df %> %
682+ filter(Sample == patient_id )
683+
684+ # Remove patient of interest and retain only other patient data
685+ metab_zscores_df <- metab_zscores_df %> %
686+ filter(Sample != patient_id )
687+
676688 # subtitle per page
677689 sub_perpage <- gsub(" _" , " " , metab_class )
678690 # for IEM plots, put subtitle on two lines
@@ -733,7 +745,7 @@ create_violin_plot <- function(metab_zscores_df, patient_zscore_df, sub_perpage,
733745 # Add the Z-score at the right side of the plot
734746 geom_text(
735747 data = patient_zscore_df ,
736- aes(16 , label = paste0(" Z=" , round(Z_score , 2 ))),
748+ aes(16 , label = paste0(" Z=" , round(Z_score_original , 2 ))),
737749 hjust = " left" , vjust = + 0.2 , size = 3 , na.rm = TRUE
738750 ) +
739751 # Set colour for the Z-score of the selected patient
0 commit comments