welch-lab / liger

R package for integrating and analyzing multiple single-cell datasets
GNU General Public License v3.0
380 stars 78 forks source link

invalid type "list" in R_matrix_as_sparse error #309

Closed Obtusah closed 4 months ago

Obtusah commented 4 months ago

As I'm running a code from https://github.com/Roy-lab/scMTNI/blob/master/Scripts/Integration/LIGER_scRNAseq_scATAC.R, I confronted this issue that seems to be related with Liger.

image

Input type of the createLigerDataset() is dataframe, which is considered as list, and the error saids that "list" is invalid type for internally called function .m2sparse.

I'm assuming that at the line 225 of import.R in liger, something went wrong when calling methods::as(rawData, "CsparseMatrix") ?

image

Can anyone please help me with this error?

theAeon commented 4 months ago

this sounds like a matrix ABI issue-can you get a sessionInfo()?

mvfki commented 4 months ago

@Obtusah Package rliger has been massively updated. During the development cycle, it is just not expected that single-cell data would be presented in "data.frame" form due to the inefficiency in memory use. Single-cell data, especially RNAseq data, tends to be sparse, and is always recommended to be presented with "dgCMatrix" class, which is supported by package Matrix. A simple fix for you would be something like below:

for (i in seq_along(samples))
{
  print(paste0("Reading sample", i))
  fname <- fnames[i]
  myCount <- fread(fname, header = TRUE)
  # No need to do as.data.frame
  # myCount = as.data.frame(myCount)
  # genenames can still be extracted with this syntax
  genenames <- myCount$Gene
  rownames(myCount) <- genenames
  print("Added gene names")
  suff <- samples[[i]]
  myCount <- myCount[, -1]
  colnames(myCount) <- paste(suff, colnames(myCount), sep = "_")
  print("Added colnames")
  # Coerce the data.table into a matrix
  # which can be then converted into "dgCMatrix" with `as(., "CsparseMatrix")`
  mypanel4[[suff]] <- as(as.matrix(myCount), "CsparseMatrix")
  print(myCount[1:5,1:5])
}

You might need to library(Matrix) if R says it has issue understanding what "CsparseMatrix" is.

Given such a case, support for "data.frame" would be added back in the next release. Though we still strongly suggest considering using the memory efficient form of representation through out the workflow.

Meanwhile, from your source code I noticed that you defined quite a lot plotting functions where I can see a lot of data access using @ symbol. @ access to clusters, tsne.coords and etc. are no longer available now due to structural changes to the liger object. In the new version of rliger we provided accessor functions (getter and setter) for accessing all data you need. It is now generally not suggested to use @ symbol for accessing data because scripts can easily get broken if developers again need to do structural updates like we did. Please see resources below for ways to update them:

  1. Access cell metadata
  2. Access dimensionality reduction
  3. Other access can be seen on the same web page.

Please feel free to reach out if you need more help.

Yichen

Obtusah commented 4 months ago

Thank you for detailed answer! The error was solved with the code you gave. I am changing other deprecated methods too.