jeudi 21 avril 2016

Conditionally process (bgzip tabix) files using loop and if else statement

I have some .vcf files. I have selected those files from my directory and want to convert them to two other formats.

I am a bit confused using if and else if here. I want to do it like this: if there isn't .bgz file for [i]th .vcf file, I want to convert it to .bgz file keeping the original file.

If there is already .bgz file, but not .bgz.tbi file for [i] th .bgz file, then I want to convert .bgz file to .bgz.tbi file keeping the original .bgz that I get from .vcf file.

Can someone please help me finish this loop? It works for if condition, but don't know how to proceed from there.

  path.file<-"/mypath/for/files/" 
  all.files <- list.files("/mypath/for/files")
  all.files <- all.files[grepl(".vcf$",all.files)]

for (i in 1:length(all.files)){
 if(!exists(paste0(all.files[i],".bgz"))){
  bgzip(paste0(path.file,all.files[i]), overwrite=FALSE)
  }else{(!exists(paste0(all.files[i],".bgz",".tbi"))){
  #if(!exists(paste0(all.files[i],".bgz",".tbi"))){
  indexTabix(paste0(paste0(path.file,all.files[i]),".bgz"), format="vcf")
  }
 }

Aucun commentaire:

Enregistrer un commentaire