Skip to content

Instantly share code, notes, and snippets.

@mfoll
Created September 23, 2015 08:42
Show Gist options
  • Save mfoll/a4dfbb92068dc559f130 to your computer and use it in GitHub Desktop.
Save mfoll/a4dfbb92068dc559f130 to your computer and use it in GitHub Desktop.
Two functions to extract INFO and GENOTYPE data from a VCF file
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment