Project: s41467-024-47581-1 | Function: check (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:** Implements hypergeometric test for statistical enrichment analysis across 4 molecular subtypes (CS1-CS4). Performs multiple hypergeometric tests using phyper() to assess enrichment of specific features or mutations in each subtype compared to others, which is core to the subtype characterization described in the paper.

**KEY TERMS:** {
  "k1,k2,k3,k4": "counts in each subtype",
  "m1,m2,m3,m4": "total counts per subtype",
  "n": "total sample size",
  "p1,p2,p3,p4": "enrichment p-values for 4 subtypes"
}

**CONTEXT CODE:**                            meta = c(union(marker.up.meta$templates$probe,marker.d.meta$templates$probe))),
             show.rownames = c(F,F), # specify for each omics data
             show.colnames = FALSE, # show no sample names
             color         = col.list,
             annCol        = clin.display[,c('RecurRisk','Recur','TERT_promoter','BRAF','MUC16','CCDC6_RET','NCOA4_RET')], # no annotation for samples
             annColors     = list(RecurRisk = c(
               "Median" = 'cyan',
               "Low" = 'purple',
               "High" = 'red'
             ),
             Recur=c("TRUE"='black',"FALSE"='lightgrey'),
             TERT_promoter = c('TERT C228T' = 'black', "No" = 'lightgrey'),
             CCDC6_RET = c("TRUE" = 'black', "FALSE" = 'lightgrey'),
             NCOA4_RET = c("TRUE" = 'black', "FALSE" = 'lightgrey'),
             BRAF = c("TRUE" = 'black', "FALSE" = 'lightgrey'),
             MUC16 = c("TRUE" = 'black', "FALSE" = 'lightgrey')), # no annotation color
             width         = 10, # width of each subheatmap
             height        = 5, # height of each subheatmap
             fig.name      = "COMPREHENSIVE HEATMAP")

#####Compare TDS BRAF RAS scores###########
setwd('H:/project/multiOmics')
clin.display = read.csv(file = './variables/clin.display_TDSBSRS.csv',row.names = 1)
clin.display$Cluster = paste0('CS',cmoic$clust.res[rownames(clin.display),'clust'])
c.data = clin.display[,c('Age','TumorSize','LNM.No','Cluster','TDS','RAS.Score','BRAF.Score')]
library(reshape2)
c.data$Cluster2 = ifelse(c.data$Cluster == 'CS2','CS2','Others')

c.data = melt(c.data,measure.vars = c('Age','TumorSize','LNM.No','TDS','RAS.Score','BRAF.Score'))

ggplot(c.data,aes(x=Cluster,y=value,fill=Cluster))+
  geom_boxplot()+
  facet_wrap(~variable,scales = 'free')+
  theme_cowplot()+scale_fill_manual(values = pal_npg('nrc')(4))

ggplot(c.data,aes(x=Cluster,y=value,fill=Cluster))+geom_boxplot()+facet_grid(~variable,scales = 'free',space = 'free_y')+
  stat_compare_means(comparisons = list(c('CS1','CS2'),c('CS1','CS3'),c('CS2','CS3'),
                                        c('CS1','CS4'),c('CS4','CS3'),c('CS2','CS4')),
                     method = 't.test')+theme_bw()+scale_fill_manual(values = pal_npg('nrc')(4))

ggplot(c.data.plot,aes(x=Cluster2,y=value,fill=Cluster2))+
  geom_boxplot(scale='width')+facet_grid(~variable,scales = 'free')+
  stat_compare_means(method = 't.test')+theme_bw()+
  scale_fill_manual(values = pal_npg('nrc')(6)[c(1,6)])



####Compare clinical and mutation ####
}  # End of disabled global code block

check = function(feature, subtype) {
    stop("Function implementation has been masked")
}

check2 = function(a,b){
  cc = table(a,b)
  #�ܹ���N����Ʒ������M����Ʒ�����ڴ��г�ȡn������飬�鵽k����Ʒ�ĸ��ʷֲ����ӳ����ηֲ�
  k1=cc[2,1]#k
  n1=sum(cc[2,])#n
  m1=sum(cc[,1])#M
  n=sum(cc)#N
  # q = the number of white balls drawn from the urn (without replacement)
  # q��Ӧ���������⣬Ϊk
  # 
  # m = the number of white balls in the urn
  # m��Ӧ���������⣬ΪM
  # 
  # n = the number of black balls in the urn
  # n��Ӧ���������⣬ΪN-M
  # 
  # k = the number of balls drawn from the urn (sample size)
  # k��Ӧ���������⣬Ϊn
  # ��������������������������������phyper(q-1, m, n, k, lower.tail=F)
  p1=dhyper(k1, m1, n-m1, n1)
  k2=cc[2,2]#k
  n2=sum(cc[2,])#n
  m2=sum(cc[,2])#M
  
  k3=cc[2,3]#k
  n3=sum(cc[2,])#n
  m3=sum(cc[,3])#M
  p3=dhyper(k3, m3, n-m3, n3)
  
  
  p2=dhyper(k2, m2, n-m2, n2)
  
  k4=cc[2,4]#k
  n4=sum(cc[2,])#n
  m4=sum(cc[,4])#M
  p4=dhyper(k4, m4, n-m4, n4)
  
  return(c(p1,p2,p3,p4))
}

# Wrap all remaining global code to prevent execution during testing
if (FALSE) {  # This code block is disabled
for(feature in c('Gender','RecurRisk','Recur','exTInvasion','LNM','exNInvasion','LNM.3cm','T.stage',
                 'N.stage','M.stage','TNM.stage','TERT_promoter','CCDC6_RET','NCOA4_RET',
                 'BRAF','MUC16')){
  print(c(feature,chisq.test(clin.display$Cluster,clin.display[,feature])$p.value))
  
}

check(clin.display$TERT_promoter,clin.display$Cluster)

#[1] 0.352678124 0.714572810 0.842829199 0.003487506
check(clin.display$BRAF,clin.display$Cluster)
#0.1233816302 0.7131736013 0.9915548449 0.0004093522
check(clin.display$Recur,clin.display$Cluster)
check(clin.display$CCDC6_RET,clin.display$Cluster)

check(clin.display$NCOA4_RET,clin.display$Cluster)

check(clin.display$MUC16,clin.display$Cluster)
chisq.test(table(clin.display$TERT_promoter,clin.display$Cluster=='CS4'))
chisq.test(table(clin.display$BRAF,clin.display$Cluster=='CS4'))

# All check2 and subsequent code now in main disabled block
check2(clin.display$BRAF,clin.display$Cluster)
check2(clin.display$TERT_promoter,clin.display$Cluster)

for(feature in c('Gender','RecurRisk','Recur','exTInvasion','LNM','exNInvasion','LNM.3cm','T.stage',
                 'N.stage','M.stage','TNM.stage','TERT_promoter','CCDC6_RET','NCOA4_RET',
                 'BRAF','MUC16')){
  print(c(feature,chisq.test(clin.display$Cluster %in% c('CS2'),clin.display[,feature])$p.value))
  
}

# Continue with other global code
clin.display$RecurRisk = factor(clin.display$RecurRisk,levels = c('High','Median','Low'))
plot.data = melt(clin.display,measure.vars = c('RecurRisk','exNInvasion','N.stage','T.stage','BRAF','TERT_promoter','MUC16'))
plot.data$value = factor(plot.data$value,levels = c('High','Median','Low','TRUE','FALSE',
                                                    "N0","N1a","N1b","T1" ,"T2","T3" ,"T4",'TERT C228T','No'))
plot.data$variable = factor(plot.data$variable,levels = c('RecurRisk','T.stage','N.stage','BRAF','exNInvasion','TERT_promoter','MUC16'))

####Figure 5b
ggplot(plot.data,aes(x=Cluster,fill=value))+geom_bar(width = 0.75,position = 'fill')+
  facet_wrap(~variable,scales = 'free',nrow = 2)+
  theme_cowplot()+
  theme(axis.text = element_text(size=8),
        legend.text = element_text(size = 8),
        legend.title = element_text(size = 8),
        axis.title = element_text(size=8),
        strip.background = element_blank(),
        strip.text.x = element_text(size = 8),
        axis.text.x = element_text(angle = 90))+
  scale_fill_manual(values = c('red','cyan','purple','black','grey',pal_npg()(10)[c(-1,-2)],'lightgrey'))
#"Median"='cyan',"Low"='purple',"High"='red'




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