R-在数据连续时将一些行合并为一行



我有一个床文件,该文件被加载为数据框中的R。基因组坐标,看起来像这样的东西:

chrom   start  end
chrX    400    600
chrX    800    1000
chrX    1000   1200
chrX    1200   1400
chrX    1600   1800
chrX    2000   2200
chrX    2200   2400

无需保留所有的行,将其压缩到这样的东西会更好:

chrom   start  end
chrX    400    600
chrX    800    1400
chrX    1600   1800
chrX    2000   2400

我该怎么做?

我试图考虑使用dplyr的事情,但没有成功。group_by无法使用,因为我不知道如何使用第一行的启动坐标将连续行的块修改为一个,而从最后一行则结束坐标,因为有许多这些块。

使用BioConductor的Genomicranges软件包,专为床文件等建造:

library(GenomicRanges)
# Example data
gr <- GRanges(
  seqnames = Rle("chr1", 6),
  ranges = IRanges(start = c(400 ,800, 1200, 1400, 1800, 2000),
                   end = c(600, 1000, 1400, 1600, 2000, 2200)))
gr
# GRanges object with 6 ranges and 0 metadata columns:
#       seqnames       ranges strand
#          <Rle>    <IRanges>  <Rle>
#   [1]     chr1 [ 400,  600]      *
#   [2]     chr1 [ 800, 1000]      *
#   [3]     chr1 [1200, 1400]      *
#   [4]     chr1 [1400, 1600]      *
#   [5]     chr1 [1800, 2000]      *
#   [6]     chr1 [2000, 2200]      *
#   -------
#   seqinfo: 1 sequence from an unspecified genome; no seqlengths
# merge contiouse ranges into one using reduce:
reduce(gr)
# GRanges object with 4 ranges and 0 metadata columns:
#       seqnames       ranges strand
#          <Rle>    <IRanges>  <Rle>
#   [1]     chr1 [ 400,  600]      *
#   [2]     chr1 [ 800, 1000]      *
#   [3]     chr1 [1200, 1600]      *
#   [4]     chr1 [1800, 2200]      *
#   -------
#   seqinfo: 1 sequence from an unspecified genome; no seqlength
# EDIT: if the bed file is a data.frame we can convert it to ranges object:
gr <- GRanges(seqnames(Rle(df$chrom),
                       ranges = IRanges(start = df$start,
                                        end = df$end)))

相关内容

  • 没有找到相关文章

最新更新