Two functions to extract INFO and GENOTYPE data from a VCF file

September 23, 2015 ยท View on GitHub

get_info=function(info,field,num=T) { get_single_info=function(single_info,field) { grep_res=grep(paste("^",field,"=",sep=""),unlist(strsplit(single_info,";")),value=T) if (length(grep_res)>0) strsplit(grep_res,"=")[[1]][2] else NA } res=unlist(lapply(info,get_single_info,field)) if (num) as.numeric(res) else res }

get_genotype=function(genotype,format,field,num=T) { get_single_genotype=function(single_genotype,format,field) { single_res=unlist(strsplit(single_genotype,":"))[which(unlist(strsplit(format,":"))==field)] if (length(single_res)>0) single_res else NA } res=unlist(lapply(genotype,get_single_genotype,format,field)) if (num) as.numeric(res) else res }