Hi,
I wrote some codes. Explanations are below:
# Loading required packages
library("VariantAnnotation")
library("TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")
# args will take different values (VCF files) while running the script from command line
args <- commandArgs(trailingOnly = TRUE)
file1<- table(seqnames(readVcf(args[1],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
file2<- table(seqnames(readVcf(args[2],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
file3<- table(seqnames(readVcf(args[3],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
file4<- table(seqnames(readVcf(args[4],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
file5<- table(seqnames(readVcf(args[5],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
file6<- table(seqnames(readVcf(args[6],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
all<- rbind(file1, file2, file3, file4, file5, file6)
all<- as.matrix(all)
pdf("Barplot_of_mutaton.pdf")
barplot(all, beside=TRUE, col= rainbow(nrow(all)),
legend=rownames(all), cex.names=0.7, args.legend = list(x = "topright",bty = "n",
inset=c(-0.07, -0.14)))
dev.off()
This script was for 6 inputs. This worked properly.
Now, I want to make a function, that will take input, while I run the script through command line. Input could be 1 to n. But it should plot the barplot.
For example: if I input 2 files, than also it should plot a barplot and if I provide input of 10 files, than also script should work. I am not good at writing functions, but tried the following commands:
args <- commandArgs(trailingOnly = TRUE)
args<- NULL
for (i in args) {
library("VariantAnnotation")
library("TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")
args[i]<- table(seqnames(readVcf(args[i],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
all<- rbind(args[i])
pdf("Barplot_of_mutaton.pdf")
barplot(all, beside=TRUE, col= rainbow(nrow(all)),
legend=rownames(all), cex.names=0.7, args.legend = list(x = "topright",bty = "n",
inset=c(-0.07, -0.14)))
dev.off()
}
Regards,
Deepak
To make this clear, you want to make barplot for every file and don't know how to make such looping through files, yes?
PS.: I am asking this, because post title is about barplots, but it seems that your problem is looping.
Probably,
sapply
function is to be used. But, how to use it and where to use it.How can I learn about making functions and loops?
​Suggestions are welcome.