YuLab-SMU / ChIPseeker

:dart: ChIP peak Annotation, Comparison and Visualization
https://onlinelibrary.wiley.com/share/author/GYJGUBYCTRMYJFN2JFZZ?target=10.1002/cpz1.585
219 stars 74 forks source link

Mismatch between rownames of tagMatrix and windows indexes #236

Open lufce opened 3 months ago

lufce commented 3 months ago

Dear Pr Guangchuang Yu,

Thank you for developing a useful library. But, there seems to be a bug in getTagMatrix.internal function in tagMatrix.R.

# --- from Line 521
  windows <- subsetByOverlaps(windows, cov.width,
                              type="within", ignore.strand=FALSE)

  chr.idx <- intersect(names(peak.cov),
                       unique(as.character(seqnames(windows))))

  peakView <- Views(peak.cov[chr.idx], as(windows, "IntegerRangesList")[chr.idx])
  tagMatrixList <- lapply(peakView, function(x) t(viewApply(x, as.vector)))
  tagMatrix <- do.call("rbind", tagMatrixList)

  ## get the index of windows, that are reorganized by as(windows, "IntegerRangesList")
  idx.list <- split(1:length(windows),  as.factor(seqnames(windows)))
  idx <- do.call("c", idx.list)

  rownames(tagMatrix) <- idx
  tagMatrix <- tagMatrix[order(idx),]

I guess it tries to copy the indexes of windows to tagMatrix's rownames. But, it won't work because peakView and idx.list do not have the same order of chromosome numbers.

It'll cause to disconnect between tagMatrix index and window index.

I suggest the fixed code below, but I don't know it's the best way.

# --- from Line 521
  windows <- subsetByOverlaps(windows, cov.width,
                              type="within", ignore.strand=FALSE)

  chr.idx <- intersect(names(peak.cov),
                       unique(as.character(seqnames(windows))))

  peakView <- Views(peak.cov[chr.idx], as(windows, "IntegerRangesList")[chr.idx])
  tagMatrixList <- lapply(peakView, function(x) t(viewApply(x, as.vector)))
  tagMatrix <- do.call("rbind", tagMatrixList)

  ## get the index of windows, that are reorganized by as(windows, "IntegerRangesList")
  idx.list <- split(1:length(windows),  as.factor(seqnames(windows)))
  # ---modification
  # idx <- do.call("c", idx.list)
  idx = c()
  for(i in chr.idx){
    idx = c(idx, idx.list[[i]])
  }
  # --- end modification

  rownames(tagMatrix) <- idx
  tagMatrix <- tagMatrix[order(idx),]