Home > OS >  Create an R function that normalizes data based on input values
Create an R function that normalizes data based on input values

Time:09-10

I don't make to many complicated functions and typically stick with very basic ones. I have a question, how do I create a function that takes a dataset and normalizes based on desired normalization method and boxplots the output? Currently norm_method is different between the norm methods, was wondering if there is a way to call this in the start of function to pull through the correct method? Below is the code I created, but am stuck how to proceed.

library(reshape2)  # for melt
library(cowplot)   

demoData;

# target_deoData will need to be changed at some point
TestFunc <- function(demoData) {
  
  # Q3 norm (75th percentile)
  target_demoData <- normalize(demoData ,
                             norm_method = "quant", 
                             desiredQuantile = .75,
                             toElt = "q_norm")

  # Background normalization without spike
  target_demoData <- normalize(demoData ,
                             norm_method = "neg", 
                             fromElt = "exprs",
                             toElt = "neg_norm")
        
  boxplot(assayDataElement(demoData[,1:10], elt = "q_norm"),
        col = "red", main = "Q3",
        log = "y", names = 1:10, xlab = "Segment",
        ylab = "Counts, Q3 Normalized")
        
  boxplot(assayDataElement(demoData[,1:10], elt = "neg_norm"),
        col = "blue", main = "Neg",
        log = "y", names = 1:10, xlab = "Segment",
        ylab = "Counts, Neg. Normalized")
}

CodePudding user response:

You might want to consider designing your normalize() and assayDataElement() functions to take ..., which provides more flexibility.

In lieu of that, given the examples above, you could make a simple configuration list, and elements of that configuration are passed to your normalize() and assayDataElement() functions, like this:

TestFunc <- function(demoData, method=c("quant", "neg")) {
  
  method = match.arg(method)
  method_config = list(
    "quant" = list("norm_args" = list("norm_method" = "quant", desired_quantile = 0.75, "toElt" = "q_norm"),
                   "plot_args" = list("col"="red", main="Q3", ylab = "Counts, Q3 Normalized")),
    "neg" = list("norm_args" = list("fromElt" = "exprs", "toElt" = "neg_norm"),
                 "plot_args" = list("col"="blue", main="Neg", ylab = "Counts, Neg Normalized"))
  )
  
  mcn = method_config[[method]][["norm_args"]]
  mcp = method_config[[method]][["plot_args"]]
  
  # normalize the data
  target_demoData = do.call(normalize, c(list(data = demoData[1:10]), mcn))
  
  # get the plot
  boxplot(assayDataElement(
    demoData[1:10], elt=mcp[["toElt"]],col = mcp[["col"],main = mcp[["main"]],
    log = "y", names = 1:10, xlab = "Segment",ylab = mcp[["ylab"]]
  )
}

Again, using this approach is not as flexible as ... (and consider splitting into two functions.. one that returns normalized data, and a second function that generates the plot..

  • Related