Hello everyone,
I am doing KEGG enrichment analysis in R using the following codes. However I need to do this with multiple files. The filenames are as
file01_accessory_KOfam.txt
fileA_accessory_KOfam.txt
fileB_accessory_KOfam.txt
and so on...
I was able to run my codes for enrichment analysis using the following codes , however I am not familiar with for loops. Hope you guys can help me out. My codes are as below:
file01 <- read.delim("file01_accessory_KOfam.txt", header = FALSE, sep="\t")
colnames(file01) <- c("ProteinID", "KO")
file01<- (file$KO)
kegg_file01 <- enrichKEGG( file01, organism = "ko", keyType = "kegg", pvalueCutoff = 0.05,
pAdjustMethod = "none", qvalueCutoff = 0.3)
png("KEGG_file01.png",res=90, w=1000, h=800)
p<- dotplot(kegg_22470, showCategory=10) + ggtitle("Enriched Pathways file01")
p + theme(
plot.title = element_text(color="black", size=14, face="bold", hjust=0.5))
dev.off()
Thanks!!
2 answers
Per comments from iraun and @Shred online search will give you what you want. I believe you almost there, just need to put your code in for loop and define some variable inside that. Something like the following :
Put your files in a directory (d), then save the file names in a variable (fn) ;
fn = list.files(d)
# define a list to save enrichment result from for loop
enrich_list = list()
# for loop
for (f in 1:length(fn)){
file <- read.delim(fn[f], header = FALSE, sep="\t")
colnames(file) <- c("ProteinID", "KO")
file<- (file$KO)
kegg_file <- enrichKEGG(file, organism = "ko", keyType = "kegg", pvalueCutoff = 0.05, pAdjustMethod = "none", qvalueCutoff = 0.3)
enrich_list[f] = kegg_file
names(enrich_list)[f] = fn[f]
png(paste0(fn[f], ".png"),res=90, w=1000, h=800)
p<- dotplot(kegg_22470, showCategory=10) + ggtitle(paste0("Enriched Pathways ", fn[f]))
p + theme( plot.title = element_text(color="black", size=14, face="bold" hjust=0.5))
dev.off()
}
You may need to slightly modify the code to fit your exact use case.
# Get a list of files.
files <- list.files(".", "\\.txt$", full.names=TRUE)
names(files) <- tools::file_path_sans_ext(basename(files))
# Load the data in and run KEGG enrichment.
kegg_results <- lapply(files, \(x) {
x <- read.delim(x, header=FALSE, sep="\t")[, 2, drop=TRUE]
result <- enrichKEGG(
x, organism="ko", keyType="kegg", pvalueCutoff=0.05,
pAdjustMethod="none", qvalueCutoff=0.3)
return(result)
})
# Plot the KEGG enrichment results.
for (x in names(kegg_results)) {
png(paste0(x, ".png"), res=90, w=1000, h=800)
p <- dotplot(kegg_results[[x]], showCategory=10) +
ggtitle(paste("Enriched Pathways", x)) +
theme(plot.title=element_text(
color="black", size=14, face="bold", hjust=0.5))
print(p); dev.off()
}
Log in to answer this question.
Isn't a good practice asking here someone to write code for you. Besides that, be more clear, explaining against what you want to loop (which is the filename structure?) and read something online about basic for looping in R before asking basic question.
@Shred I edited my question, hopefully its more clear. I don't know how for loops work, however its not that I am asking everything. I am able to do this with my individual files. I just need some idea on how to run this on multiple files with different output names with file name.
I recommend you to do some "googling" on your own before asking as @Shred recommended. Have you seen this post for example? I hope it helps you to code the solution. If you try and you get stuck, you can always come back here with your attempt and the error you are getting :).
Since you have come this far, convert your code to function and use lapply.