如何在R中读取vcf文件

如何在R中读取vcf文件,r,bioinformatics,genetics,vcf-variant-call-format,R,Bioinformatics,Genetics,Vcf Variant Call Format,我有这个,我想在R中读取这个文件。但是,这个文件包含一些我想跳过的冗余行。我想在结果中得到类似的结果,其中行以行匹配#CHROM开始 这就是我尝试过的: chromo1<-try(scan(myfile.vcf,what=character(),n=5000,sep="\n",skip=0,fill=TRUE,na.strings="",quote="\"")) ## find the start of the vcf file skip.lines<-grep("^#CHROM",

我有这个,我想在R中读取这个文件。但是,这个文件包含一些我想跳过的冗余行。我想在结果中得到类似的结果,其中行以行匹配
#CHROM
开始

这就是我尝试过的:

chromo1<-try(scan(myfile.vcf,what=character(),n=5000,sep="\n",skip=0,fill=TRUE,na.strings="",quote="\"")) ## find the start of the vcf file
skip.lines<-grep("^#CHROM",chromo1)


column.labels<-read.delim(myfile.vcf,header=F,nrows=1,skip=(skip.lines-1),sep="\t",fill=TRUE,stringsAsFactors=FALSE,na.strings="",quote="\"")
num.vars<-dim(column.labels)[2]
结果

    #CHROM  POS     ID      REF     ALT
    11      33443   3        A       T
    12      33445   5        A       G

也许这对你有好处:

# read two times the vcf file, first for the columns names, second for the data
tmp_vcf<-readLines("test.vcf")
tmp_vcf_data<-read.table("test.vcf", stringsAsFactors = FALSE)

# filter for the columns names
tmp_vcf<-tmp_vcf[-(grep("#CHROM",tmp_vcf)+1):-(length(tmp_vcf))]
vcf_names<-unlist(strsplit(tmp_vcf[length(tmp_vcf)],"\t"))
names(tmp_vcf_data)<-vcf_names
#读取vcf文件两次,第一次读取列名,第二次读取数据
tmp_vcf数据。表::fread按预期读取,参见示例:

library(data.table)

#try this example vcf from GitHub
vcf <- fread("https://raw.githubusercontent.com/vcflib/vcflib/master/samples/sample.vcf")

#or if the file is local:
vcf <- fread("path/to/my/vcf/sample.vcf")
库(data.table)
#尝试GitHub中的这个示例vcf

vcf使用测序软件包怎么样?如果你在谷歌上搜索“read vcf R”,有几个。Bioconductor有几个vcf阅读器。@RichardScriven那个vcf阅读器不适合我的情况。我只想跳过这些行,得到一个以制表符分隔的表。这个答案很可能是重复的,但是你总是在变量名中使用点吗?我发现它让人困惑(特别是如果你也懂python的话),我更喜欢下划线。不过,我想这是一个品味问题,干杯。@RicardoGuerreiro圆点在R中是惯用的变量名称。广泛使用,完全可以接受。
library(data.table)

#try this example vcf from GitHub
vcf <- fread("https://raw.githubusercontent.com/vcflib/vcflib/master/samples/sample.vcf")

#or if the file is local:
vcf <- fread("path/to/my/vcf/sample.vcf")