2015-09-11 124 views
0

我有这个.vcf格式的文件,我想在R中读取这个文件。但是,这个文件包含了一些我想跳过的冗余行。我想在结果中找到类似#CHROM匹配行的行。如何读取R中的vcf文件

这是我曾尝试:

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] 

myfile.vcf

#not wanted line 
    #unnecessary line 
    #junk line 
    #CHROM POS  ID  REF  ALT 
    11  33443 3  A  T 
    12  33445 5  A  G 

结果

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

如何使用测序包?有几个,如果谷歌“阅读vcf R” –

+1

Bioconductor有几个VCF阅读器。 – hrbrmstr

+0

@RichardScriven vcfreader不适合我的情况。我只想跳过这些行并获得制表符分隔表。 – MAPK

回答

1

这也许能为你很好:

# 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") 

# 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 

p.s .:如果你有几个vcf文件,那么你应该使用lapply函数。

最好, 罗伯特