Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fatty acid analysis plots. #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: soda
Title: Simple Omics Data Analysis
Version: 0.8.55
Version: 0.8.55.9000
Authors@R:
c(person(given = "Yassene",
family = "Mohammed",
Expand Down
122 changes: 119 additions & 3 deletions R/class_lips_exp.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ Lips_exp = R6::R6Class(
pca_scores_table = NULL,
pca_loadings_table = NULL,
dbplot_table = NULL,
fa_analysis_table = NULL,

# GSEA & over representation
gsea_prot_list = NULL,
Expand All @@ -292,6 +293,7 @@ Lips_exp = R6::R6Class(
heatmap = NULL,
pca_plot = NULL,
double_bond_plot = NULL,
fa_analysis_plot = NULL,

# Functional analysis plots
dotplot = NULL,
Expand Down Expand Up @@ -486,6 +488,14 @@ Lips_exp = R6::R6Class(
self$params$or_emap_plot$enable_physics = enable_physics
},

param_fa_analysis_plot = function(data_table, feature_meta, sample_meta, group_column, pathway, img_format) {
self$params$fa_analysis_plot$data_table = data_table
self$params$fa_analysis_plot$feature_meta = feature_meta
self$params$fa_analysis_plot$sample_meta = sample_meta
self$params$fa_analysis_plot$group_col = group_column
self$params$fa_analysis_plot$pathway = pathway
self$params$fa_analysis_plot$img_format = img_format
},

#-------------------------------------------------------- Table methods ----

Expand Down Expand Up @@ -859,9 +869,12 @@ Lips_exp = R6::R6Class(
self$param_ridge_plot(showCategory = 30,
img_format = "png")




self$param_fa_analysis_plot(data_table = self$tables$raw_data,
feature_meta = self$tables$feature_table,
sample_meta = self$tables$raw_meta,
group_column = self$indices$group_col,
pathway = NULL,
img_format = "png")



Expand Down Expand Up @@ -2246,7 +2259,110 @@ Lips_exp = R6::R6Class(
layout(xaxis = list(title = 'Count')
)
self$plots$or_barplot = fig
},


plot_fa_analysis = function(data_table = self$tables$raw_data,
feature_table = self$tables$feature_table,
sample_meta = self$tables$raw_meta,
group_col = self$indices$group_col,
pathway = self$params$fa_analysis_plot$pathway,
colour_list,
width = NULL,
height = NULL) {

## At the moment this function is using the raw data table
# do the calculations
res <- fa_analysis_calc(data_table = data_table,
feature_table = feature_table,
sample_meta = sample_meta)


# Produce the class x group table
# add ID's, group's and make long
res$ID <- rownames(res)
res$group <- sample_meta[res$ID, group_col]
res_long <- res |>
tidyr::pivot_longer(cols = -c(ID, group),
names_to = "fa_chain",
values_to = "value")

# calculate mean and stdev per group
plot_table <- tapply(as.data.frame(res_long), list(res_long$group, res_long$fa_chain), function(x) {
avg <- mean(x[, "value"], na.rm = TRUE)
stdev <- sd(x[, "value"], na.rm = TRUE)

return(list(avg = avg,
stdev = stdev,
fa_chain = x[1, "fa_chain"],
group = x[1, "group"]))
# print(x)
})

plot_table <- do.call(rbind.data.frame, plot_table)

# filter plot_table based on pathway selection
pathway_fa <- c(
paste(seq(16, 26, 2), 0, sep = ":"),
paste(seq(16, 24, 2), 1, sep = ":"),
c("18:2", "18:3", "20:2", "20:3", "20:4",
"22:4", "22:5","24:4", "24:5"),
c("18:3", "18:4", "20:3", "20:4", "20:5",
"22:5", "22:6", "24:5", "24:6")
)
names(pathway_fa) <- c(rep("SFA", 6),
rep("MUFA", 5),
rep("PUFA6", 9),
rep("PUFA3", 9))

if(!is.null(pathway)) {
selected_pathway_fa <- unique(pathway_fa[names(pathway_fa) %in% pathway])
plot_table <- plot_table[plot_table$fa_chain %in% selected_pathway_fa, ]
}

# Store the plot_table
self$tables$fa_analysis_table <- plot_table

# plotting
i <- 1
fig <- plotly::plot_ly(colors = colour_list, width = width, height = height)
for (grp in unique(plot_table$group)) {
fig <- fig |>
plotly::add_trace(data = plot_table[plot_table$group == grp, ],
x = ~fa_chain,
y = ~avg,
color = colour_list[i],
type = "bar",
name = grp,
error_y = ~ list(array = stdev,
color = "#000000"))
fig <- fig |>
plotly::layout(legend = list(orientation = 'h',
xanchor = "center",
x = 0.5),
xaxis = list(title = "Fatty acid chain"),
yaxis = list(title = "Concentration"))
i <- i + 1
}
fig <- fig |>
plotly::layout(annotations =
list(x = 1,
y = -0.175,
text = "NOTE: error bars are standard deviation",
showarrow = FALSE,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "auto",
xshift = 0,
yshift = 0,
font = list(size = 10))
)
fig

self$plots$fa_analysis_plot <- fig
}

#------------------------------------------------------------------ END ----
)
)
137 changes: 136 additions & 1 deletion R/plotboxes_lipidomics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1476,4 +1476,139 @@ db_plot_events = function(r6, dimensions_obj, color_palette, input, output, sess
))
}
})
}
}


#-----------------------------------------------------------FA analysis index ----
fa_analysis_generate = function(r6, colour_list, dimensions_obj, input) {
print_tm(r6$name, "Fatty acid analysis index plot: generating plot.")

if (input$fa_analysis_plotbox$maximized){
width = dimensions_obj$xpx_total * dimensions_obj$x_plot_full
height = dimensions_obj$ypx_total * dimensions_obj$y_plot_full
} else {
width = dimensions_obj$xpx * dimensions_obj$x_plot
height = dimensions_obj$ypx * dimensions_obj$y_plot
}

r6$plot_fa_analysis(data_table = r6$tables$raw_data, #table_switch(input$class_comparison_dataset, r6),
group_col = input$fa_analysis_metacol,
colour_list = colour_list,
width = width,
height = height)
}

fa_analysis_spawn = function(r6, format, output) {
print_tm(r6$name, "Fatty acid analysis index: spawning plot.")

output$fa_analysis_plot = plotly::renderPlotly({
r6$plots$fa_analysis_plot
plotly::config(r6$plots$fa_analysis_plot, toImageButtonOptions = list(format = format,
filename = timestamped_name('fa_analysis'),
height = NULL,
width = NULL,
scale = 1))
})
}

fa_analysis_ui = function(dimensions_obj, session) {
# add function to show bs4dash with plotting function
get_plotly_box(id = "fa_analysis",
label = "Fatty acid analysis",
dimensions_obj = dimensions_obj,
session = session)
}

fa_analysis_server = function(r6, output, session) {
ns = session$ns
print_tm(r6$name, "Fatty acid analysis index: START.")

# set some UI
output$fa_analysis_sidebar_ui = shiny::renderUI({
shiny::tagList(
shiny::selectInput(
inputId = ns("fa_analysis_metacol"),
label = "Select group column",
choices = colnames(r6$tables$raw_meta),
selected = r6$params$fa_analysis_plot$group_col
),
shiny::selectInput(
inputId = ns("fa_analysis_pathway"),
label = "Select pathway",
choices = c("SFA" = "SFA",
"MUFA" = "MUFA",
"PUFA(n-6)" = "PUFA6",
"PUFA(n-3)" = "PUFA3"),
selected = "",
multiple = TRUE,
width = "100%"),
shiny::hr(style = "border-top: 1px solid #7d7d7d;"),
shiny::selectInput(
inputId = ns("fa_analysis_img_format"),
label = "Image format",
choices = c("png", "svg", "jpeg", "webp"),
selected = r6$params$fa_analysis_plot$img_format,
width = "100%"),
shiny::downloadButton(
outputId = ns("download_fa_analysis_table"),
label = "Download associated table",
style = "width:100%;"
)
)
})
}

fa_analysis_events = function(r6, dimensions_obj, color_palette, input, output, session) {
# Generate the plot
shiny::observeEvent(c(input$fa_analysis_metacol,
input$fa_analysis_pathway,
input$fa_analysis_img_format), {
print_tm(r6$name, "Fatty acid analysis: Updating params...")

r6$param_fa_analysis_plot(data_table = r6$tables$raw_data,
feature_meta = r6$tables$feature_table,
sample_meta = r6$tables$raw_meta,
group_col = input$fa_analysis_metacol,
pathway = input$fa_analysis_pathway,
img_format = input$fa_analysis_img_format)

base::tryCatch({
fa_analysis_generate(r6, color_palette, dimensions_obj, input)
fa_analysis_spawn(r6, input$fa_analysis_img_format, output)
},
error = function(e) {
print_tm(r6$name, 'Fatty acid analysis error, missing data.')
print(e)
},
finally = {}
)
})

# Download associated table
output$download_fa_analysis_table = shiny::downloadHandler(
filename = function(){timestamped_name("fa_analysis_table.csv")},
content = function(file_name){
write.csv(r6$tables$fa_analysis_table, file_name)
}
)

# Expanded boxes
fa_analysis_proxy = plotly::plotlyProxy(outputId = "fa_analysis_plot",
session = session)

shiny::observeEvent(input$fa_analysis_plotbox,{
if (input$fa_analysis_plotbox$maximized) {
plotly::plotlyProxyInvoke(p = fa_analysis_proxy,
method = "relayout",
list(width = dimensions_obj$xpx_total * dimensions_obj$x_plot_full,
height = dimensions_obj$ypx_total * dimensions_obj$y_plot_full
))
} else {
plotly::plotlyProxyInvoke(p = fa_analysis_proxy,
method = "relayout",
list(width = dimensions_obj$xpx * dimensions_obj$x_plot,
height = dimensions_obj$ypx * dimensions_obj$y_plot
))
}
})
}
8 changes: 6 additions & 2 deletions R/serv_lipidomics.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ plotbox_switch_ui_lips = function(selection_list){
"select_volcano_plot" = volcano_plot_ui,
"select_heatmap" = heatmap_ui,
"select_pca" = pca_ui,
"select_double_bond_plot" = double_bonds_ui
"select_double_bond_plot" = double_bonds_ui,
"select_fa_analysis_plot" = fa_analysis_ui
)
)
}
Expand All @@ -25,7 +26,8 @@ plotbox_switch_server_lips = function(selection_list){
"select_volcano_plot" = volcano_plot_server,
"select_heatmap" = heatmap_server,
"select_pca" = pca_server,
"select_double_bond_plot" = double_bonds_server
"select_double_bond_plot" = double_bonds_server,
"select_fa_analysis_plot" = fa_analysis_server
)
)
}
Expand Down Expand Up @@ -2124,6 +2126,8 @@ lipidomics_server = function(id, ns, input, output, session, module_controler) {
heatmap_events(r6, dimensions_obj, color_palette, input, output, session)
pca_events(r6, dimensions_obj, color_palette, input, output, session)
db_plot_events(r6, dimensions_obj, color_palette, input, output, session)
fa_analysis_events(r6, dimensions_obj, color_palette, input, output, session)

session$userData[[id]]$showPlots = shiny::observeEvent(input$showPlots,{

# Update x dimensions in px and bs, and y in px
Expand Down
Loading