Project: s41467-024-47581-1 | Function: runDiff (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Performs differential analysis between recurrence risk groups using Wilcoxon rank-sum test and fold change calculations. Core statistical method for identifying molecular features that distinguish different recurrence risk levels, directly implementing the comparative analysis described in the methods.

**KEY TERMS:** {
  "omics": "multi-omics data matrix",
  "cols.1": "samples in group 1",
  "cols.2": "samples in group 2",
  "FC": "fold change",
  "FDR": "false discovery rate correction"
}

**CONTEXT CODE:** # Conditionally load packages to avoid errors when running tests
suppressWarnings({
  if (!requireNamespace("dplyr", quietly = TRUE)) {
    # Package not available
  } else {
    library(dplyr)
  }
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
    # Package not available
  } else {
    library(ggplot2)
  }
  if (!requireNamespace("ggrepel", quietly = TRUE)) {
    # Package not available
  } else {
    library(ggrepel)
  }
  if (!requireNamespace("cowplot", quietly = TRUE)) {
    # Package not available
  } else {
    library(cowplot)
  }
  if (!requireNamespace("ggsci", quietly = TRUE)) {
    # Package not available
  } else {
    library(ggsci)
  }
  if (!requireNamespace("MOVICS", quietly = TRUE)) {
    # Package not available #R4.0.4 only
  } else {
    library(MOVICS)
  }
  if (!requireNamespace("survival", quietly = TRUE)) {
    # Package not available
  } else {
    library(survival)
  }
  if (!requireNamespace("survminer", quietly = TRUE)) {
    # Package not available
  } else {
    library(survminer)
  }
})

# Disable global code to prevent execution during testing
if (FALSE) {  # This code block is disabled
  # Global code would go here if needed for manual execution
}  # End of disabled global code

####Differential across RecurRisk
runDiff = function(omics,cols.1,cols.2){
    stop("Function implementation has been masked")
}

findDiffItems = function(omics,clin.both){
  
  clin.both = clin.both[rownames(clin.both) %in% colnames(omics),]
  high.cols = clin.both[clin.both$RecurRisk=='High','ID']
  m.cols = clin.both[clin.both$RecurRisk=='Median','ID']
  l.cols = clin.both[clin.both$RecurRisk=='Low','ID']
  
  res.high = runDiff(omics,high.cols,c(m.cols,l.cols))
  res.m = runDiff(omics,m.cols,c(high.cols,l.cols))
  res.l = runDiff(omics,l.cols,c(m.cols,high.cols))
  
  res.high$Risk = 'H'
  res.m$Risk='M'
  res.l$Risk='L'
  
  res = rbind(res.high,res.m,res.l)
  
  return(res)
  
}

runGSEA.i = function(path2Gene,res){
  res = arrange(res,desc(FC),p.value)
  FC.gsea = res$FC
  names(FC.gsea)=as.character(res$id)
  
  diff.pathway = GSEA(FC.gsea,minGSSize = 2,maxGSSize = 500,pvalueCutoff = 1.2,
                      TERM2GENE = path2Gene[,c(1,2)],pAdjustMethod = 'fdr',
                      seed=1234)
  #minGSSize=5 for omics other than metabolomics
  return(diff.pathway@result)
}

findDiffPathway = function(diff.res,path2Gene){
  res.high = diff.res[diff.res$Risk == 'H',]
  res.m = diff.res[diff.res$Risk == 'M',]
  res.l = diff.res[diff.res$Risk == 'L',]
  
  path.high =runGSEA.i(path2Gene,res.high)
  path.m = runGSEA.i(path2Gene,res.m)
  path.l = runGSEA.i(path2Gene,res.l)
  path.high$Risk = 'H'
  path.m$Risk='M'
  path.l$Risk='L'
  
  res = rbind(path.high,path.m,path.l)
  
  return(res)
  
}
getKEGGIDAndLogFC = function(res.r.nona){
  kegg.ids = c()
  log.fcs = c()
  froms = unique(res.r.nona$From)
  froms.new = c()
  risks = c('H','M','L')
  risks.new = c()
  for(fr in froms){
    for(ri in risks){
 
  kegg2logfc = filter(res.r.nona,From == fr & Risk == ri & kegg %in% c('','-') == F) %>% group_by(kegg) %>% mutate(mLog2FC = ifelse(mean(FC)>0,max(FC),min(FC)))
  
  for( i in 1:nrow(kegg2logfc)){
    kegg.id = kegg2logfc$kegg[i]
    logfc = kegg2logfc$mLog2FC[i]
    if(grepl(',',kegg.id)){
      ids = strsplit(kegg.id,',')[[1]]
      for(id in ids){
        kegg.ids = append(kegg.ids,id)
        log.fcs = append(log.fcs,logfc)
        froms.new = append(froms.new,fr)
        risks.new = append(risks.new,ri)
      }
    }else{
      kegg.ids = append(kegg.ids,kegg.id)
      log.fcs = append(log.fcs,logfc)
      froms.new = append(froms.new,fr)
      risks.new = append(risks.new,ri)
    }
   
  }
  
  }}
  
  rr = data.frame(id = kegg.ids,FC = log.fcs,From=froms.new,Risk =risks.new)
  return(rr)
}

# Wrap global code to prevent execution during testing
if (FALSE) {  # This code block is disabled
data.meta = readRDS(file = './variables/normalizedMetabolism.Rds')##The names were changed into the same as mrna-count
data.mrna = readRDS(file = './variables/deseqNorm_count.Rds')
data.mrna = log2(data.mrna+1)
sample.type = read.csv(file='H:/project/multiOmics/data/rnaseq/sampleInfo.csv',row.names = 1,check.names = F)
samples.tp = rownames(sample.type[sample.type$Type == 'P',])



Complete the function implementation replacing "stop("Not implemented")".