Project: s41467-024-52600-2 | Function: Anno_table_maker (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 transcription factor enrichment analysis using Fisher's exact test to identify TFs that bind to promoters of target genes. Calculates odds ratios, confidence intervals, and p-values for TF-gene associations.

**KEY TERMS:** {
  "target.genes": "genes bound by TF in promoter regions",
  "total.genes": "total genes with promoter binding",
  "fishTest": "Fisher's exact test for enrichment",
  "OddsRatio": "enrichment odds ratio"
}

**CONTEXT CODE:**   annotations  %>% filter(symbol != "<NA>") %>% dplyr::select(symbol) %>% filter(!duplicated(symbol)) %>% nrow
  #result:39473
  
  
  ### Filtering TF ###
  samples <- list.files(path = "./sample_bed",pattern="bed", full.names = T)
  files <- file.path(samples)
  names(files) <- str_replace(samples, "./sample_bed/", "") %>% str_replace(".sorted.bed", "") 
  
  peakAnnoNames <- c()
  for (var in names(files)){
    newname <- as.character(str_split(var,"_", simplify = T))[1]
    ifelse(newname %in% TF, peakAnnoNames <- append(peakAnnoNames,var), NA)  
  }
  filtered_files <- files[names(files) %in% peakAnnoNames]
  
  
  ### Peak annotation using ChIPseeker ###
  # Promoter region [-1000,1000]
  peakAnnoList <- lapply(filtered_files, annotatePeak, TxDb=txdb, 
                         tssRegion=c(-1000, 1000), verbose=FALSE)
  
  ### Add annotations for each peaks ###
  AnnoList <- list()
  for (var in peakAnnoNames){
    anno <- data.frame(peakAnnoList[[var]]@anno)
    anno$transcriptId <- sub('\\.[0-9]*$', '', anno$transcriptId)
    newlist <- list(left_join(anno, annotations, by= c("transcriptId"="tx_id")))
    names(newlist) <- var
    AnnoList <- append(AnnoList,newlist)
  }
  
  ### IDR filtering ###
  AnnoList2=list()
  for (var in peakAnnoNames){
    newlist <- AnnoList[[var]] %>% dplyr::filter(V5 >= 830.482) %>% arrange(-V7) %>% list() #%>% dplyr::filter(AnnoList_t[[var]]$width <= summary(AnnoList_t[[var]]$width)[5]) -> newdf
    names(newlist) <- var
    AnnoList2 <- append(AnnoList2,newlist)
  }
  
  ### save RDS files ###
  saveRDS(AnnoList2, file="AnnoList2.RData")
  saveRDS(peakAnnoNames, file="peakAnnoNames.RData")
} # End of global execution block





### ChIP-seq based TF enrichment ###
Anno_table_maker <- function(AnnoList = AnnoList2, genelist = ALgenes, peakAnnoNames = peakAnnoNames) {
  stop("Function implementation has been masked")
}



### main ###
### Read RDS files ###
if (FALSE) {
  annoList_f <- readRDS("AnnoList2.RData")
  peakAnnoNames <- readRDS("peakAnnoNames.RData")
}

### Input gene lists ###
# Wrap global execution code to prevent running during testing
if (FALSE) {
  Lys_bio <- read.delim(file = "Lysosomal_biogenesis_Bordi.txt", sep = "\t", header = F, quote = "")
  DE_Lys <- read.table("Differentially_expressed_lysosomal_genes.txt", sep = "\t", header = F) 
  
  Lys_bio <- Lys_bio %>% pull()
  DE_Lys <- DE_Lys %>% pull()
  
  ### results ###
  Lys_bio_table <- Anno_table_maker(annoList_f,Lys_bio,peakAnnoNames)
  DE_Lys_table <- Anno_table_maker(annoList_f,DE_Lys,peakAnnoNames)
  
  
  
  
  sessionInfo()
} # End of example usage block






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