Function for barplot
2
0
Entering edit mode
dktarathym • 0
@dktarathym-6880
Last seen 7.3 years ago
United States

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

R • 1.1k views
ADD COMMENT
0
Entering edit mode
@levi-waldron-3429
Last seen 5 weeks ago
CUNY Graduate School of Public Health a…

I am guessing that the problem you're having is that only the last file appears in your PDF?  Try putting your pdf() before the loop and dev.off() after the loop to get a multi-page PDF file rather than over-writing it with each loop.

ADD COMMENT
0
Entering edit mode

My idea is that, I want to make a script, that would work to plot the mutation for vcf file(s). For example: if I have 1 vcf file, and I want to see the no. of mutations in it (for each chromosome), this script should print the barplot. In case, I have multiple files, say 5, it should plot mutations for each chromosome in a single barplot (for bar plot, command is:
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))).

)

 

If I understood properly, you want me to do the following:
 

library("VariantAnnotation")
library("TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")

args <- commandArgs(trailingOnly = TRUE)

pdf("Barplot_of_mutaton.pdf")
for (i in 1:length(args)) {
  args[i]<- table(seqnames(readVcf(args[i],"TxDb.Scerevisiae.UCSC.sacCer3.sgdGene")))
  all<- rbind(args[i])
  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()

 when I passed the files through command line, there was following error message:
 

Error in -0.01 * height : non-numeric argument to binary operator

Calls: barplot -> barplot.default

In addition: Warning message:

In args[i] <- table(seqnames(readVcf(args[i], "TxDb.Scerevisiae.UCSC.sacCer3.sgdGene"))) :

  number of items to replace is not a multiple of replacement length

Execution halted

ADD REPLY
0
Entering edit mode
dktarathym • 0
@dktarathym-6880
Last seen 7.3 years ago
United States

   

ADD COMMENT

Login before adding your answer.

Traffic: 262 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