[英]How can I extract sequences from a FASTA file for each of the intervals defined in a BED file using R?
How can I extract sequences from a FASTA file for each of the intervals defined in a BED file using R? 如何使用R从FASTA文件中提取BED文件中定义的每个间隔的序列? The reference genome used is "Gallus gallus" that can be obtained by: 所使用的参考基因组是“鸡鸡”,可通过以下方法获得:
source("http://bioconductor.org/biocLite.R")
biocLite("BSgenome.Ggallus.UCSC.galGal4")
library(BSgenome.Ggallus.UCSC.galGal4)
My data file is a result of gRanges package 我的数据文件是gRanges软件包的结果
library("GenomicRanges")
> olaps
GRanges object with 2141 ranges and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] chr14 [ 1665929, 1673673] *
[2] chr14 [ 2587465, 2595209] *
[3] chr14 [ 8143785, 8151529] *
[4] chr14 [ 9779705, 9787449] *
[5] chr14 [10281129, 10288873] *
... ... ... ...
[2137] chr24 [3280553, 3288297] *
[2138] chr24 [3330889, 3338633] *
[2139] chr24 [3005641, 3015321] *
[2140] chr24 [3319273, 3327017] *
[2141] chr24 [5549545, 5557289] *
-------
seqinfo: 31 sequences from an unspecified genome; no seqlengths
That I can transform in data.table 我可以在data.table中进行转换
olaps<- as.data.table(olaps)
Example to be used: 使用示例:
olaps<-"seqnames start end width strand
chr1 1665929 1673673 7745 *
chr1 2587465 2595209 7745 *
chr1 8143785 8151529 7745 *
chr2 9779705 9787449 7745 *
chr2 10281129 10288873 7745 *"
olaps<-read.table(text=olaps,header=T)
Expected outcome: something like this (fasta format): 预期结果:如下所示(fasta格式):
>SEQUENCE_1
ACTGACTAGCATCGCAT...
>SEQUENCE_2
ACGTAGAGAGGGACATA...
>SEQUENCE_3...
I have tried to use this package unsuccessful until now: 到目前为止,我一直尝试使用此软件包失败:
source("http://bioconductor.org/biocLite.R")
biocLite("rtracklayer")
This, should solve your trick: 这,应该解决您的把戏:
First: 第一:
seq = BSgenome::getSeq(BSgenome.Ggallus.UCSC.galGal4, olaps)
to add names to the sequences: 为序列添加名称:
names(seq) = paste0("SEQUENCE_", seq_along(seq))
To generate a ".fasta" from your sequences: 要从您的序列中生成“ .fasta”:
Biostrings::writeXStringSet(seq, "my.fasta")
More details were provided before: 之前提供了更多详细信息:
https://support.bioconductor.org/p/77913/#77986 https://support.bioconductor.org/p/77913/#77986
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.