dozmorovlab / HiCcompare

Joint normalization of two Hi-C matrices, visualization and detection of differential chromatin interactions. See multiHiCcompare for the analysis of multiple Hi-C matrices
https://dozmorovlab.github.io/HiCcompare/
Other
18 stars 3 forks source link

cooler2sparse function code seems error #28

Closed junjunlab closed 1 year ago

junjunlab commented 1 year ago

Hi, I use cooler2sparse function to transform data into up-triangle format. But the data contains only one chromosome, and cooler2sparse doesn't work and get error "no temp found. I check the source code and found the if loop for dealing with only one chromosome seems not right, the following is the code:

cooler2sparse <- function(cooler) {
  # convert to data.table
  cooler <- as.data.table(cooler)
  colnames(cooler) <- c("chr1", "start1", "end1", "chr2", "start2", "end2",
                        "IF")
  # subset to only include cis interactions since HiCloess doesn't work
  # with trans interaction if data for more than 1 chromosome entered
  if (is.factor(cooler$chr1)) {
    chroms <- levels(cooler$chr1)
  }
  if (is.character(cooler$chr1)) {
    chroms <- unique(cooler$chr1)
  }
  if (length(chroms) > 1) {
    cooler <- subset(cooler, chr1 == chr2)
    # split up data into list of sparse matrices for each chromosome
    sparse.list <- vector("list", length(chroms))
    for (i in seq_along(chroms)) {
      temp <- subset(cooler, chr1 == chroms[i])
      temp <- temp[, c("start1", "start2", "IF"), with = FALSE]
      colnames(temp) <- c("region1", "region2", "IF")
      temp <- subset(temp, IF != 0)
      sparse.list[[i]] <- temp
    }
    names(sparse.list) <- chroms
    return(sparse.list)
  } else {
    temp <- temp[, c("start1", "start2", "IF"), with = FALSE]
    colnames(temp) <- c("region1", "region2", "IF")
    temp <- subset(temp, IF != 0)
    return(temp)
  }
}

the error occurs on which temp object not been defined:

 else {
    temp <- temp[, c("start1", "start2", "IF"), with = FALSE]
    colnames(temp) <- c("region1", "region2", "IF")
    temp <- subset(temp, IF != 0)
    return(temp)
  }

Thanks for you checking it! Best wished!

agata-sm commented 1 year ago

Found the same issue, should this section rather read:

cooler <- as.data.table(cooler)
  temp <- cooler[, c("start1", "start2", "IF"), with = FALSE]
    colnames(temp) <- c("region1", "region2", "IF")
    temp <- subset(temp, IF != 0)

?

mdozmorov commented 1 year ago

@agata-sm, thanks for reporting, we'll look into it.

mdozmorov commented 1 year ago

Thanks, @agata-sm, this is indeed the solution. Fixed in https://github.com/dozmorovlab/HiCcompare/commit/f5eff087029dac9f9222dcc584a8249d283258f3, and pushed to Bioconductor.

@junjunlab, sorry for the late reply, I overlooked your initial post. You likely figured, now it'll work by default. Thanks for reporting!

mdozmorov commented 1 year ago

Closing as changes are propagated on Bioconductor, https://bioconductor.org/checkResults/release/bioc-LATEST/HiCcompare/, http://bioconductor.org/checkResults/devel/bioc-LATEST/HiCcompare/