0

我正在运行一个自定义函数,它为 data.frame 的每一行生成并保存一些图:

zz <- "chr  start end   name  max
chr11 66184332 66197785 NPAS4_CBP20 90
chr11  62666002 62683613 BC047540_CBP20 100   
chr1 9824542  9828548  MIR3687_CBP20  500
chr6  33239767 33259282 B3GALT4_Pol2   1000
chr20  244996112   245029580   HNRNPU-AS1_Pol2   450
chr20 62487823 62525914 ABHD16B_Pol2   370
chr12 121146198   121179996   ACADS_Pol2  90"

my.genes <- read.table(text=zz, header = TRUE)

该函数有两个步骤,其中一个很慢(约 40 秒),但只需要为每个 chr 运行一个,然后为该 chr 的每一行运行一个。在伪代码中,它将类似于:

for each chr createData
   for each nameInChr do somethingWithData.

我的问题是,如何优化这个?嵌套 d_ply?按 chr 对 data.frame 进行排序,然后对 unique(mygenes$chr) 进行某种形式的应用?

如果这不包含实际功能,我很抱歉,但它很长,我认为这更像是一个很好的实践/理论问题。但是,如果需要,我可以添加适当的代码。

编辑

这是简化的函数(plotGviz)。事实上,我认为 d_ply 不会工作,因为它会为每一行运行 UcscTrack(我正确吗?)。最终目标是为每个独特的染色体运行 UcscTrack,然后使用该信息为每个基因(名称)构建图。这两个步骤(UcscTrack 和绘图)都很耗时,但基本原理是,首先运行代码的非唯一部分(适用于 chr)将节省整体时间 - 该表只是我所拥有的一小部分。

自定义 plotGviz 中的功能不能仅按照我设置它们的方式进行优化。

################
### libraries ##
library(Gviz)
library(GenomicRanges)
library(GenomicFeatures)
library(data.table)
library("RColorBrewer")
#######################


d_ply(my.genes, .(chr, name), plotGviz)

plotGviz <- function(gene) {
   chr <- gene$chr
   mygene.start <- gene$start
   mygene.end <- gene$end
   mygene <- gene$name
   max.cov <- gene$max

#################################################
## this part runs only once for each unique chr
## UcscTrack is what takes most time

   ## Gene annotations ##
   knownGenes <- UcscTrack(genome=gen, chromosome=chr,
   track="knownGene",  trackType="GeneRegionTrack",
   rstarts="exonStarts", rends="exonEnds", gene="name", symbol="name",
   transcript="name", strand="strand", fill="#FF7F00", name="UCSC Genes", geneSymbols = TRUE, showId = TRUE)

   refGenes <- UcscTrack(genome=gen, chromosome=chr,
   track="refGene", trackType="GeneRegionTrack", 
   rstarts="exonStarts", rends="exonEnds", gene="name", symbol="name",
   transcript="name", strand="strand", fill="#FF7F00", name="RefSeq Genes", geneSymbols = TRUE, showId = TRUE)

   ## axis scale ##
   ideoTrack <- IdeogramTrack(genome = gen, chromosome = chr)
   # plotTracks(ideoTrack, from = mygene.start, to = mygene.end)

   axisTrack <- GenomeAxisTrack(scale=0.25)


####################################
## now for each name in unique chr
######################

   ## construct tracks ##


   ## ChIP-seq coverage from BAM ##
   bamPol2 <- "~/bioinfo/srp_chip_seq/data/reads/merged_reads_CBP20line/Pol2.bam"
   bamInput <- "~/bioinfo/srp_chip_seq/data/reads/merged_reads_CBP20line/Input.bam"

  ## histogram
   bTrackPol2 <- DataTrack(range = bamPol2, genome = gen, chromosome = chr,
      name = "Pol2", type = "histogram", col.histogram="#984EA3",
      ylim=c(0,max.cov))

   bTrackInput <- DataTrack(range = bamInput, genome = gen, chromosome = chr,
      name = "Input", type = "histogram", col.histogram="#999999",
      ylim=c(0,max.cov))


   #################
   ## plot tracks ##

   pdf(paste("./plots/", mygene,"_cov.pdf", sep="")) 

   plotTracks(list(ideoTrack, axisTrack, bTrackCBP20, pTrackCBP20, bTrackPol2, pTrackPol2, bTrackInput, refGenes, bTrackRNAcov), from = mygene.start, to = mygene.end, fontfamily="Helvetica", background.title="white", col.title="black", col.axis="black")

   plotTracks(list(ideoTrack, axisTrack, bTrackCBP20, pTrackCBP20, bTrackPol2, pTrackPol2, bTrackInput, knownGenes, bTrackRNAcov), from = mygene.start, to = mygene.end, fontfamily="Helvetica", background.title="white", col.title="black", col.axis="black")
   dev.off()

   # plotTracks(list(axisTrack, refGenes, bTrackCBP20, pTrackCBP20, bTrackPol2, pTrackPol2, bTrackInput, bTrackRNA), from = mygene.start, to = mygene.end, fontfamily="Helvetica", background.title="white", col.title="black", col.axis="black")

   # head(displayPars(bTrackPol2))
}

编辑2

我的临时解决方案为唯一的 chr 使用 for 循环,在为每个 chr 子设置所有名称之前创建 UcscTrack,并为绘图创建 d_pply。基本上在 2 部分中吐出这么大的功能。

for (chrom in unique(my.genes$chr)) {
   print(paste("creating annotation for ", chrom, sep=""))

   ## Gene annotations ##
   knownGenes <- UcscTrack(genome=gen, chromosome=chrom,
   track="knownGene",  trackType="GeneRegionTrack",
   rstarts="exonStarts", rends="exonEnds", gene="name", symbol="name",
   transcript="name", strand="strand", fill="#FF7F00", name="UCSC Genes", geneSymbols = TRUE, showId = TRUE)

   refGenes <- UcscTrack(genome=gen, chromosome=chrom,
   track="refGene", trackType="GeneRegionTrack", 
   rstarts="exonStarts", rends="exonEnds", gene="name", symbol="name",
   transcript="name", strand="strand", fill="#FF7F00", name="RefSeq Genes", geneSymbols = TRUE, showId = TRUE)

   ## axis scale ##
   ideoTrack <- IdeogramTrack(genome = gen, chromosome = chrom)
   # plotTracks(ideoTrack, from = mygene.start, to = mygene.end)

   axisTrack <- GenomeAxisTrack(scale=0.25)

   ## select genes in chromosome
   df.g <- subset(my.genes, chr == chrom)
   d_ply(df.g, .(name), plotGviz)
}
4

1 回答 1

0

试试data.table吧,它会比 更快plyr。这是解决您的问题的通用方法:

library(data.table)
dt = data.table(my.genes)

dt[, .SD[, do_smth(), by = name], by = chr]

这是一个不同 SO 问题中上述方法的示例 -如何使用 data.frame 或 data.table 长格式方法集成在多行上定义的属性

于 2013-08-08T16:40:21.593 回答