I have R code takes a vcf file returns a parsed .txt output.

I have 21 vcf files in a folder

I named my R code parse_vcf_alt1 function


3 Answers 3


Why don't you try using docopt which can make your R script "understand" command line parameters.

Doing that, you can create a simple bash loop to execute the script for all vcf files, such as:

for files in "*.vcf" ; do script.R -i $files -o {output} ; done

See this example here

Hope it helps, Felipe.


The upper part, where you try to use lapply() over your list of files with your custom function does not quite make sense to me: You declare your files but you do not use this in your lapply() call. I would expect to see something like `lapply(files, your_custom_function).

Moreover, the error you get is results from dplyr::rename() not being able to find #CHROM because readr::read_tsv() could not read the header line properly. In my case the vcf was read as an n x 1 data frame! You could use readr::read_delim() to make sure you are reading the data right.

I would, however, would not try to read a VCF file as an ordinary data frame, there are packages for that, for example [vcfR][1]. Once you create your vcfR object, you can check the different slots:

The vcfR object is an S4 class object with three slots containing the metadata, the fixed data and the genotype data.

  • $\begingroup$ for files in "*.vcf" ; do script.R -i $files -o {output} ; done should be run on the whatever *sh terminal you are using and not on R terminal. $\endgroup$
    – haci
    Jan 17, 2020 at 10:42
  • $\begingroup$ @MahtaMira, please first try to figure out what the error messages are about. Apparently you don't have the permissions to execute your script and Google will immediately explain why you get this error but also how to overcome that. And even within comments, you can format your code using "`". $\endgroup$
    – haci
    Jan 17, 2020 at 11:18

By adding this to the script


file_list <- list.files(path="/path/to/vcf/files/")
for (i in 1:length(file_list))
  fn=paste('test',x,sep = "")

But Also adding

df <- apply(vcf,2,as.character)
  fn=paste("C:/Users/44736/Videos/Captures/New folder/",fn,sep="")

to the main function I could get this

The problem is I don't know how to have a tab delimited file rather than csv also removing quote from the written output


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.