How can each file be commanded separately with multiple files in R??
2
0
Entering edit mode
5 weeks ago

Hi I am new to R.

I have multiple files and I want to command these files separately. (I have about 200 files)

Using for loop or lapply, how can I command these for each file automatically? I want to tag sample names in Barcode and filenames like sample1, sample2, sample3...

sample1 <- read.delim("sample1.hg38_multianno.txt", header = F)
colnames(sample1)[1:10] = c("Chr", "Start", "End", "Ref", "Alt", "Func.refGene", "Gene.refGene", "GeneDetail.refGene", "ExonicFunc.refGene","AAChange.refGene")
sample1 <- sample1[30:nrow(sample1),]
sample1 <- sample1[,-c(11:87)]
sample1$Tumor_Sample_Barcode = "sample1"
write.table(sample1, file="sample1.txt", row.names = FALSE, sep = "\t", quote = FALSE)

sample1.maf = maftools::annovarToMaf(annovar = "sample1.txt", 
                                         Center = NULL,
                                         refBuild = 'hg38', 
                                         tsbCol = 'Tumor_Sample_Barcode',
                                         table = 'refGene')

write.table(sample1.maf, file="sample1.maf", row.names = FALSE, sep = "\t", quote = FALSE)

Thank you in advance.

for command loop multiple R • 151 views
ADD COMMENT
0
Entering edit mode
5 weeks ago
Ram 34k

I'm sure there is a better way to do this, but you could declare a variable outside the loop and increment it inside the loop (or use it in lapply) like so:

lapply(1:200, function(i) {
sample_name <- paste0("sample", i)
sample_file <- read.delim(paste0(sample_name,".hg38_multianno.txt", header = F)
colnames(sample_file)[1:10] = c("Chr", "Start", "End", "Ref", "Alt", "Func.refGene", "Gene.refGene", "GeneDetail.refGene", "ExonicFunc.refGene","AAChange.refGene")
sample_file <- sample_file[30:nrow(sample_file),]
sample_file <- sample_file[,-c(11:87)]
sample_file$Tumor_Sample_Barcode = sample_name
write.table(sample_file, file=paste0(sample_name, ".txt"), row.names = FALSE, sep = "\t", quote = FALSE)

sample_file.maf = maftools::annovarToMaf(annovar = paste0(sample_name, ".txt"), 
                                         Center = NULL,
                                         refBuild = 'hg38', 
                                         tsbCol = 'Tumor_Sample_Barcode',
                                         table = 'refGene')

write.table(sample_file.maf, file=paste0(sample_name, ".maf"), row.names = FALSE, sep = "\t", quote = FALSE)
})
ADD COMMENT
0
Entering edit mode
5 weeks ago
Hamid Ghaedi ★ 1.6k

While it is recommended to use apply function instead of for loop in R (like what Ram did), I understand the fucntion behavior using for loop much more better!! Here is a for loop solution :

files = list.files() # assuming you have only the intended files in the current working directory
n =  #column number of the "Tumor_Sample_Barcode" in your files

for (f in files){
  tmpDf<- read.delim(files[f], header = F)
  tmpDf <- tmpDf[30:nrow(tmpDf),]
  tmpDf <- tmpDf[,-c(11:87)]
  tmdDF[, n] = substr(files[f],1,7)
  write.table(tmpDf, file=paste0(substr(files[f],1,7), "txt"), row.names = FALSE, sep = "\t", quote = FALSE)
  tmp.maf = maftools::annovarToMaf(annovar = paste0(substr(files[f],1,7), "txt"), 
                                   Center = NULL,
                                   refBuild = 'hg38', 
                                   tsbCol = 'Tumor_Sample_Barcode',
                                   table = 'refGene')

  write.table(tmp.maf, file=paste0(substr(files[f],1,7), "maf"), row.names = FALSE, sep = "\t", quote = FALSE)

}
ADD COMMENT

Login before adding your answer.

Traffic: 1575 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6