R/normalize_to_Pair.R

Defines functions normalize_to_Pair

Documented in normalize_to_Pair

#' For data with pairs, normalize them to the sample with flag eaqul to -1.
#'
#' @param data_frame a data frame as input.
#' @param experiment_design_file a data frame for design of experiment.
#'
#' @return A data frame after normalization.
#' @export
#' @examples
#' ## The process needs to load data from PhosMap datasets stored into FTP server and perform large computation.
#' ## It may take a few minutes.
#' if(FALSE){
#' ftp_url <- "https://github.com/ecnuzdd/PhosMap_datasets/function_demo_data/normalize_to_Pair.RData"
#' load_data <- load_data_with_ftp(ftp_url, 'Rdata')
#' writeBin(load_data, "normalize_to_Pair.RData")
#' load("normalize_to_Pair.RData")
#'
#' phospho_data_normalize_by_column <- normalize_to_Pair(
#'   phospho_data_normalized,
#'   pairing_phosphorylation_experiment_design_file
#' )
#' head(phospho_data_normalize_by_column)
#'
#' }


normalize_to_Pair <- function(data_frame, experiment_design_file){
  data_frame_colnames <- colnames(data_frame)
  ID <- as.vector(data_frame[,1])
  Value_raw <- data_frame[,-1]
  Value_raw_colnames <- colnames(Value_raw)
  groups_labels <- names(table(experiment_design_file$Group))
  groups_labels_len <- length(groups_labels)
  correction_df <- NULL
  correction_df_colnames <- NULL
  for(i in seq_len(groups_labels_len)){
    group_label <- groups_labels[i]
    case_index <- which(experiment_design_file$Group == group_label & experiment_design_file$Pair == 1)
    case_v <- as.vector(unlist(Value_raw[,case_index]))
    control_index <- which(experiment_design_file$Group == group_label & experiment_design_file$Pair == (-1))
    control_v <- as.vector(unlist(Value_raw[,control_index]))
    correction_v <- (case_v + 1) / (control_v + 1)
    correction_df <- cbind(correction_df, correction_v)
    correction_df_colname <- paste(group_label, Value_raw_colnames[case_index], Value_raw_colnames[control_index], sep = '_')
    correction_df_colnames <- c(correction_df_colnames, correction_df_colname)
  }

  data_frame_normalization <- data.frame(ID, correction_df)
  colnames(data_frame_normalization) <- c(data_frame_colnames[1], correction_df_colnames)
  return(data_frame_normalization)
}
ecnuzdd/PhosMap documentation built on Dec. 7, 2022, 4:09 a.m.