如何在R中读取VCF文件 [英] How to read vcf file in R

查看:11
本文介绍了如何在R中读取VCF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个VCF format file,我想在R中读取这个文件。但是,这个文件包含一些我想跳过的多余行。我希望得到类似于结果中的内容,其中行以匹配的行#CHROM开始。

这是我尝试过的:

chromo1<-try(scan(myfile.vcf,what=character(),n=5000,sep="
",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="	",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

推荐答案

也许这对您有好处:

# 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)],"	"))
names(tmp_vcf_data)<-vcf_names

附注:如果您有多个VCF文件,则应使用lApply函数。

最佳 罗伯特

这篇关于如何在R中读取VCF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆