livnatje / DIALOGUE

DIALOGUE is a dimensionality reduction method that uses cross-cell-type associations to identify multicellular programs (MCPs) and map the cell transcriptome as a function of its environment.
Other
106 stars 16 forks source link

average.mat.rows: Error in laply(ids.u, function(x) { : could not find function "laply" #35

Closed bmill3r closed 1 year ago

bmill3r commented 1 year ago

Hi @livnatje,

Just wanted to let you know that I cam across a bug in the code.

When trying to build a cell.type object with DIALOGUE::make.cell.type(), ran into an error, which points to a misspelling in:

average.mat.rows<-function(m,ids,f = colMeans){
  ids.u<-sort(get.abundant(ids))
  m1<-laply(ids.u,function(x){return(f(m[is.element(ids,x),]))})
  rownames(m1)<-ids.u
  colnames(m1)<-colnames(m)

Thanks, Brendan

livnatje commented 1 year ago

Hi @bmill3r,

What was the error you got? What was the input you provided to DIALOGUE::make.cell.type() ? Did you include the sample vector?

Thanks, Livnat

bmill3r commented 1 year ago

Hi @livnatje,

The full error is:

Error in laply(ids.u, function(x) { : could not find function "laply"

9. average.mat.rows(t(tpm), samples)
8. t(average.mat.rows(t(tpm), samples))
7. initialize(value, ...)
6. initialize(value, ...)
5. new(structure("cell.type", package = "DIALOGUE"), ...)
4.  cell.type(name = gsub("_", "", name), cells = colnames(tpm),
    genes = rownames(tpm), cellQ = cellQ, tpm = tpm, tpmAv = tpmAv,
    qcAv = aggregate(x = cellQ, by = list(samples), FUN = mean),
    X = X, samples = samples, metadata = cbind.data.frame(cellQ = cellQ, ...
3.  DIALOGUE::make.cell.type(name = ct, tpm = mat[, idx], samples = merfish_meta[idx,
    ]$fov, X = pca[idx, ], metadata = merfish_meta[idx, "volume"],
    cellQ = rep(1, length(idx)))
2. FUN(X[[i]], ...)
1.  lapply(unique(merfish_clusters$cell_cluster), function(ct) {
    idx <- rownames(merfish_clusters[merfish_clusters$cell_cluster ==
    ct, ])
    CT <- DIALOGUE::make.cell.type(name = ct, tpm = mat[, idx], ...

If I look at average.mat.rows() in DIALOGUE.util.R:

average.mat.rows<-function(m,ids,f = colMeans){
  ids.u<-sort(get.abundant(ids))
  m1<-laply(ids.u,function(x){return(f(m[is.element(ids,x),]))})
  rownames(m1)<-ids.u
  colnames(m1)<-colnames(m)

  ids.u1<-setdiff(unique(ids),ids.u)
  if(length(ids.u1)==0){return(m1)}
  b<-is.element(ids,ids.u1)
  m0<-m[b,]
  if(sum(b)==1){m0<-t(as.matrix(m0))}
  rownames(m0)<-ids[b]

  m2<-rbind(m1,m0)
  m2<-m2[sort(rownames(m2)),]
  return(m2)
}

I can see that in line 3 there is a laply. Perhaps this is suppose to be lapply? Or perhaps it is the laply function within plyr? If the latter, then maybe plyr is not being called properly within the code?

I am using the code in the following way:

cell.type.object <- lapply(unique(merfish_clusters$cell_cluster), function(ct){

  idx <- rownames(merfish_clusters[merfish_clusters$cell_cluster == ct,])

  CT <- DIALOGUE::make.cell.type(name = ct,
                                tpm = mat[,idx], ## m x n, so genes by cells?
                                samples = merfish_meta[idx,]$fov, ## n x 1, so n = cells
                                X = pcs[idx,], ## n x k, so cells by PCs
                                metadata = merfish_meta[idx,"volume"], ## n x r, so cells by meta features
                                cellQ = rep(1, length(idx)) ## n x 1, so cells by number of reads/genes ?
                                )
  CT

})

with the goal of making a list of cell.type objects.

My inputs look like this:

## using previously annotated cell clusters as the "cell types". 
head(merfish_clusters$cell_cluster)
[1] 4 4 4 3 5 4

## matrix row ids corresponding to cell indices, for cell type 1, for example:
idx <- rownames(merfish_clusters[merfish_clusters$cell_cluster == 1,])
head(idx)
[1] "3.04192638552574e+38"
[2] "5.73177306666544e+36"
[3] "9.33359979711486e+36"
[4] "2.11188246001083e+38"
[5] "1.1510773019566e+38" 
[6] "1.81608062266214e+38"

## log-transformed, CPM normalized gene counts
mat[,idx][1:10,1:10]
10 x 10 Matrix of class "dgeMatrix"
      3.04192638552574e+38 5.73177306666544e+36 9.33359979711486e+36 2.11188246001083e+38 1.1510773019566e+38
Oxgr1             0.000000             0.000000             0.000000             0.000000                   0
Htr1a             3.305610             0.000000             0.000000             0.000000                   0
Htr1b             0.000000             0.000000             0.000000             0.000000                   0
Htr1d             0.000000             0.000000             0.000000             0.000000                   0
Htr1f             3.305610             0.000000             0.000000             3.263044                   0
Htr2a             0.000000             0.000000             0.000000             3.263044                   0
Htr2b             0.000000             0.000000             0.000000             0.000000                   0
Htr2c             0.000000             0.000000             0.000000             0.000000                   0
Htr4              3.606532             3.730568             3.250508             0.000000                   0
Htr5a             3.305610             3.730568             3.250508             0.000000                   0
      1.81608062266214e+38 1.98878799510489e+38 2.47188870534919e+38 3.29952401623605e+38 2.86549731334929e+38
Oxgr1              0.00000             0.000000             0.000000                    0             0.000000
Htr1a              0.00000             0.000000             0.000000                    0             0.000000
Htr1b              0.00000             0.000000             0.000000                    0             0.000000
Htr1d              0.00000             0.000000             0.000000                    0             3.095633
Htr1f              0.00000             0.000000             0.000000                    0             0.000000
Htr2a              0.00000             3.452379             0.000000                    0             0.000000
Htr2b              0.00000             0.000000             0.000000                    0             0.000000
Htr2c              0.00000             0.000000             3.142378                    0             0.000000
Htr4               3.20928             4.151226             3.142378                    0             3.095633
Htr5a              3.20928             0.000000             0.000000                    0             3.095633

## using the fovs as "samples" cell type belong to:
head(merfish_meta[idx,]$fov)
[1] 230 266 266 268 291 291

## first 30 PCs
head(pcs[idx,])
                          [,1]     [,2]       [,3]       [,4]       [,5]       [,6]      [,7]      [,8]       [,9]
3.04192638552574e+38 -20.85855 3.468723 -11.323820 -10.223033  2.1418359 -0.3122051 -1.941103 -4.243623 -0.3348506
5.73177306666544e+36 -13.73657 7.978402 -17.346178 -10.552101 -2.2806244  3.7437456 -4.307433 -4.644298  3.5534842
9.33359979711486e+36 -16.53564 4.678668 -19.150370 -11.438859 -0.8255688  1.1928382 -7.538735 -7.589243  2.5456689
2.11188246001083e+38 -17.82277 1.250319 -16.067496 -13.871564  3.0650848  1.4021338 -1.423498 -7.942001  3.1665006
1.1510773019566e+38  -16.06846 2.492803  -6.282591  -6.993334  2.7607547  0.3678520 -1.085087 -4.427023  1.5912212
1.81608062266214e+38 -21.34210 5.743750 -14.706683 -14.128554 -0.3030275  3.6286189 -5.944685 -4.009360  0.9786209
                          [,10]     [,11]      [,12]       [,13]     [,14]      [,15]     [,16]      [,17]
3.04192638552574e+38 -0.8229630 -5.872544 -2.3225408  0.71327509 -3.934477 -4.8980537 -2.277902  0.1500283
5.73177306666544e+36  3.5181884 -5.264993 -1.1761705  4.54402315 -1.620066 -0.2407639 -1.474252  2.3471659
9.33359979711486e+36 -0.2534522 -4.029777  0.2917511  1.36847611 -5.447910 -2.6525716  0.530893  1.6570428
2.11188246001083e+38 -1.0625918 -2.358724 -0.1573568 -0.07790937 -5.146467 -0.4410305  1.780614  1.4763691
1.1510773019566e+38  -0.4799603 -4.958052 -0.5737951  2.04419127 -3.107525 -3.0026306 -0.793805 -0.1014854
1.81608062266214e+38  1.0750092 -6.896024 -0.3064331  0.98888937 -2.052382 -1.6755815  1.255150  2.3955008
                         [,18]      [,19]      [,20]      [,21]      [,22]     [,23]       [,24]      [,25]
3.04192638552574e+38 0.6044275 -1.9509338 -0.3114941  1.2246288 -1.6079532 3.8281491 -5.04621662  3.2354167
5.73177306666544e+36 4.6605758 -1.1239838 -0.1401343  0.8615767 -2.4887615 4.9548243 -2.13709686  0.5166874
9.33359979711486e+36 3.5944748 -2.8681809 -2.1664462 -0.3378814 -1.2052485 0.6418253 -0.20533394  0.3698811
2.11188246001083e+38 2.8593153 -0.8929788 -0.2296621  1.5797288 -2.0024594 4.9543458 -1.04734673  1.2440791
1.1510773019566e+38  4.0125676  0.4165494  1.3987808  1.5414359 -0.5608966 0.8457129  0.14739521 -1.8045328
1.81608062266214e+38 3.9961504 -0.6490009 -3.2686653 -0.7461935 -3.3101564 0.7493479  0.03949428 -0.2359749
                          [,26]      [,27]     [,28]      [,29]      [,30]
3.04192638552574e+38  0.5332552  0.3297274  0.116293  1.9208043 -1.0869468
5.73177306666544e+36  0.4807519 -0.6449110 -0.700884  4.8612859 -1.9899049
9.33359979711486e+36 -2.0620298 -0.2221826 -1.349927  2.3738295 -0.9757050
2.11188246001083e+38  0.1641520 -1.7714064 -1.537605 -0.5689467 -0.1314146
1.1510773019566e+38  -2.2601806 -1.7004340 -2.561850  2.4746467 -1.8238884
1.81608062266214e+38 -2.4479545 -0.8920225 -2.117581  0.2176016  2.3792538

## including cell volume as metadata:
head(merfish_meta[idx,"volume"])
[1]  941.4514 1179.0478 1818.7359 1415.7208
[5]  314.3615 1010.0882

## for cellQ, using 1 read/gene
head(rep(1, length(idx)))
[1] 1 1 1 1 1 1

Please let me know if this helps solve the error, or if you need additional information. Also if there are flaws in my logic for constructing the list of cell.type objects, please let me know.

Thank you so much for your time, Brendan

christineyyeh commented 1 year ago

Hello @bmill3r ,

I'm Christine, a graduate student in the Jerby Lab. I have encountered this error before and it tends to happen if the popular package dplyr is loaded after DIALOGUE. the "laply" function is under the namespace of plyr; note the lack of the preceding "d". I'd try making sure that the plyr is loaded correctly and not conflicting with dplyr and see if that fixes things.

Cheers, Christine

bmill3r commented 1 year ago

Hi @christineyyeh,

Thanks! Yup, loading the plyr library did the trick. Perhaps in the code adding plyr::laply or roxygenizing with: #' @importFrom plyr laply could help protect against future environment conflicts.

Thanks again for your help, Brendan