amc-heme / scExploreR

Shiny app for single cell omics data visualization
https://amc-heme.github.io/scExploreR/
MIT License
2 stars 0 forks source link

Add human readable suffix to DotPlots #299

Closed agillen closed 1 day ago

agillen commented 2 weeks ago

DotPlots are currently the only plot type supported by scExploreR that don't respect the "human readable suffix" option in the config. This causes at least two problems:

  1. Features with the same name from different assays are indistinguishable on the DotPlot (i.e. surface markers where the protein and gene name are identical; CD14, CD34, etc.).
  2. Sorting by feature expression is ambiguous when features in (1) are plotted together (they appear as a single entry in the sort by menu).
agillen commented 2 weeks ago

General Plan:

wish1832 commented 2 weeks ago

The names of the features on the x-axis are controlled by a call to scale_x_discrete() in R/shiny_dot.R.

if (isTruthy(rename_feature_labels)){
        # Construct named vector with rename targets as values, and features
        # entered as names
        names(rename_feature_labels) <- features_entered

        # Rename x-axis tick labels according to named vector
        plot <-
          plot +
          scale_x_discrete(
            labels = rename_feature_labels
          )
        }

The names are defined by a series of reactive expressions in module-plot_module.R. Section 5.1. determines the "default" labels, using the feature names and the scExploreR:::hr_name() function. Section 5.2. processes user inputs for custom feature names if enabled via the "Appearance of Features on x-axis" menu in the dot plot options menu. Section 5.3. will apply the custom labels from 5.2 if custom labels are enabled, and if not, section 5.3. will use the "default" labels from 5.1. The labels will either be left as-is or truncated after 20 characters if the "Appearance of Features on x-axis" menu is set to "truncated", which is the default. Section 5.3. also uses the hr_name() function when truncating the labels.

The cause of this is the hr_name() calls in sections 5.1. and 5.3., where use_suffix is set to FALSE. This will be set to TRUE, in accordance with other plots.

if (plot_type == "dot"){
                   ## 5.1. Define default labels to show in menu ####
                   default_dot_x_labels <-
                     reactive(
                       label = glue("{plot_label}: Define default x-axis labels for menu"),
                       {
                       # Only runs when the plot is enabled
                       req(plot_switch())
                       # Require features to be defined
                       req(features())

                       # Use features, not features_entered (dot plot may have
                       # separate features)
                       feature_names <- features()

                       # Default names: each feature entered, with the assay
                       # key removed
                       for (i in 1:length(feature_names)){
                         feature_names[i] <-
                           hr_name(
                             machine_readable_name = feature_names[i],
                             assay_config = assay_config(),
                             use_suffix = FALSE
                             )
                         }

                       feature_names
                       })

                   ## 5.2. Process user inputs for custom feature labels ####
                   user_dot_x_labels <-
                     multi_text_input_server(
                       id = "rename_dot_x_labels",
                       default_vector = default_dot_x_labels
                       )

                   ## 5.3. Use custom labels, or default ones ####
                   dot_x_labels <-
                     reactive(
                       label = glue("{plot_label}: Custom X-axis labels"),
                       {
                       # Only runs when the plot is enabled
                       req(plot_switch())

                       # Only process if input$dot_x_labels is defined
                       # (otherwise return NULL, which will leave labels unchanged)
                       if (isTruthy(input$dot_x_labels)){
                         # Use default or custom feature labels on x-axis
                         if (input$dot_x_labels == "custom"){
                           # If custom, rename feature labels to the custom order
                           user_dot_x_labels()
                         } else if (input$dot_x_labels == "truncated"){
                           # If default, use the default feature names, but
                           # condense them (if they are more than 20 characters
                           # long, remove any characters beyond the 20th and add
                           # "...")
                           # use features() (dot plot may have separate features)
                           feature_names <- features()
                           truncated_feature_names <- c()

                           for (i in 1:length(feature_names)){
                             truncated_feature_names <-
                               c(truncated_feature_names,
                                 truncate_str(
                                   # Remove assay key prefix from feature name
                                   # before truncating
                                   str =
                                     hr_name(
                                       machine_readable_name = feature_names[i],
                                       assay_config = assay_config(),
                                       use_suffix = FALSE
                                       ),
                                   max_length = 20
                                   )
                                 )
                           }

                           # Return truncated feature names
                           truncated_feature_names
                         } else if (input$dot_x_labels == "full"){
                           # For "full", remove assay keys from labels
                           # without truncating
                           feature_names <- features()

                           for (i in 1:length(feature_names)){
                             feature_names[i] <-
                               hr_name(
                                 machine_readable_name = feature_names[i],
                                 assay_config = assay_config(),
                                 use_suffix = FALSE
                               )
                           }

                           # Return full feature names
                           feature_names
                         }
                       }
                     })
                 }