Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in anndata$write(filename = filename, compression = compression, : attempt to apply non-function #8

Closed
akhst7 opened this issue Mar 31, 2021 · 13 comments

Comments

@akhst7
Copy link

akhst7 commented Mar 31, 2021

HI,

I am tiring to save an ad file as follows;

write_h5ad(pbmc10k.ad, "~/Desktop/pbmc10k.new.h5ad")

but I get this error ;

Error in anndata$write(filename = filename, compression = compression,  : 
  attempt to apply non-function

Here is my ann data;

> pbmc10k.ad
AnnData object with n_obs × n_vars = 10194 × 36601
    obs: 'nCount_RNA', 'nFeature_RNA', 'nCount_SCT', 'nFeature_SCT', 'SCT_snn_res.0.8', 'seurat_clusters', 'nCount_unspliced', 'nFeature_unspliced'
    var: 'name'
    obsm: 'X_pca', 'X_umap'
    layers: 'spl', 'unspl'

Any help will be appreciated.

@rcannood
Copy link
Member

Hey @akhst7 !

Could you provide a preproducible example script and sample dataset? Which version of anndata for R are you using?

Kind regards,
Robrecht

@akhst7
Copy link
Author

akhst7 commented Mar 31, 2021

@rcannood,

Thanks for helping. I am using

> packageVersion("anndata")
[1] ‘0.7.5.2’

So the file in question was generated by sceasy package with a following command;

convertFormat(pbmc10k.su, from = "seurat", to="anndata", outFile = "pbmc10k.h5ad")

abd imported in R by andante package as follows;

pbmc10k.ad<-read_h5ad(filename = "/Volumes/Documents/R/pbmc10k.h5ad")

In the previous post (now in closed), I added two layers, split and unsell to pbmc10k.ad, by

pbmc10k.ad$layers[["spl"]]<-spl.t
pbmc10k.ad$layers[["unspl"]]<-unspl

Both spl.t and unspl are trasnposed matrix derived from space matrix files in Suerat obj (pbmc10k).

I uploaded the original "pbmc10k.h5ad" in my AWS S3 and a link is ;

https://myanndata.s3.us-east-2.amazonaws.com/pbmc10k.h5ad

Thanks for help !!

@rcannood
Copy link
Member

rcannood commented Apr 1, 2021

Am I correct in understanding that your script looks something like this:

library(tidyverse)
library(anndata)

# downloaded from: https://myanndata.s3.us-east-2.amazonaws.com/pbmc10k.h5ad
pbmc10k.ad <- read_h5ad("pbmc10k.h5ad")

spl.t <- ...?
unspl <- ...?

pbmc10k.ad$layers[["spl"]]<-spl.t
pbmc10k.ad$layers[["unspl"]]<-unspl

pbmc10k.ad$write_h5ad("pbmc10k.new.h5ad")

Could you fill in how you define spl.t and unspl?

@akhst7
Copy link
Author

akhst7 commented Apr 2, 2021

@rcannood
yeah. Two the missing ones are as follows;

spliced<-GetAssayData(pbmc10k, assay = "spliced")
spl.t<-as.matrix(spliced) %>% t()

unspliced<-GetAssayData(pbmc10k, assay = "unspliced")
unspl<-as.matrix(unspliced) %>% t()

Structure of both of these are;

> str(spl.t)
 num [1:10194, 1:36601] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:10194] "AAAGAACAGAGATTCA-1" "AAAGAACTCCTCAGGG-1" "AACAAAGAGCGCGTTC-1" "AAAGGATCAAGCAGGT-1" ...
  ..$ : chr [1:36601] "BX004987.1" "AC145212.1" "MAFIP" "AC011043.1" ...

> str(unspl)
 num [1:10194, 1:36601] 0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:10194] "AAACCCACATAACTCG-1" "AAACCCACATGTAACC-1" "AAACCCAGTGAGTCAG-1" "AAACCCAGTGCTTATG-1" ...
  ..$ : chr [1:36601] "BX004987.1" "AC145212.1" "MAFIP" "AC011043.1" ...

I also uploaded both spl.t and unspl in AWS S3

https://myanndata.s3.us-east-2.amazonaws.com/splt.RDS

https://myanndata.s3.us-east-2.amazonaws.com/unspl.RDS

Thanks.

@rcannood
Copy link
Member

rcannood commented Apr 2, 2021 via email

@akhst7
Copy link
Author

akhst7 commented Apr 2, 2021

@rcannood Sorry about that. I fixed the permission of those files and you should be able to download them. You can use the same link to download. They are little too big for mail attachment since my school mail server has this idiotically small limit for attachment.

Thanks.

@rcannood
Copy link
Member

rcannood commented Apr 2, 2021

Can you send me a reproducible script that I can run in order to debug your problem?

@akhst7
Copy link
Author

akhst7 commented Apr 2, 2021

I can't even write the ist imported ann obj.

> pbmc10k.ad<-read_h5ad(filename = "/Volumes/Documents/R/pbmc10k.h5ad")
> write_h5ad(pbmc10k.ad, "~/Desktop/pbmc10k.h5ad")
Error in anndata$write(filename = filename, compression = compression,  : 
  attempt to apply non-function

If this does not occur in your R, it must be specific to my R setup but I am not sure what's causing it.

@rcannood
Copy link
Member

rcannood commented Apr 2, 2021

Hmm.. I see.

This does work for me:

> pbmc10k.ad <- read_h5ad("pbmc10k.h5ad")
> pbmc10k.ad$write_h5ad("pbmc10k_2.h5ad")

I've noticed Python anndata doesn't expand the '~' by default, though this is not the issue that you're encountering.

Could you try running:

devtools::install_github("dynverse/anndata")
anndata::install_anndata()

Could you also show me the output of the following command?

reticulate::py_config()

@akhst7
Copy link
Author

akhst7 commented Apr 2, 2021

So I run

remotes::install_github("dynverse/anndata")

anndata::install_anndata()
Error: could not find a Python environment for /usr/local/bin/python3

Install_anndata() imports python anndata from conda ? I have anndata installed under pip3;

~ pip3 list
Package             Version
------------------- ---------
anndata             0.7.5
anndata2ri          1.0.6
anyio               2.2.0
appdirs             1.4.4

here is py_config output

Sys.setenv(RETICULATE_PYTHON = "/usr/local/bin/python3")
> reticulate::py_config()
python:         /usr/local/bin/python3
libpython:      /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/config-3.9-darwin/libpython3.9.dylib
pythonhome:     /usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9:/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9
version:        3.9.2 (default, Mar 26 2021, 23:27:12)  [Clang 12.0.0 (clang-1200.0.32.29)]
numpy:          /usr/local/lib/python3.9/site-packages/numpy
numpy_version:  1.20.2

Thanks.

@akhst7
Copy link
Author

akhst7 commented Apr 13, 2021

I found a workaround for this.

I created the Anndata obj, pbmc10K.ad including unspl and spl layers in R as described above. Then I did the following;

Sys.setenv(RETICULATE_PYTHON = "/usr/local/bin/python3")
library(reticulate)
an <-import("anndata")
an$AnnData$write(pbmc10k.ad, filename = "pbmc.h5ad")

So basically, I called Python from R and using AnnData to save the pbmc10k.ad to pbmc.h5ad.

I check to see if I can load the pbmc.h5ad into scvelo in the Jupyter Notebook as follows;

adata = scv.read("/Volumes/Documents/R/pbmc.h5ad", cache=True)
adata
AnnData object with n_obs × n_vars = 10194 × 36601
    obs: 'nCount_RNA', 'nFeature_RNA', 'nCount_SCT', 'nFeature_SCT', 'SCT_snn_res.0.8', 'seurat_clusters', 'nCount_unspliced', 'nFeature_unspliced'
    var: 'name'
    obsm: 'X_pca', 'X_umap'
    layers: 'spl', 'unspl'

Isn't anndata essentially a wrapper for python AnnData package ? If so how come read_h5ad works but write_h5ad does not ?

@rcannood
Copy link
Member

My apologies for the delay, I was out of office during the past week.

I finally found the problem. It seemed that ad$write_h5ad("foo.h5ad") was working, but write_h5ad(ad, "foo.h5ad") was not. My bad. I added a simple unit test to check for this issue, should it ever arise again.

I pushed a fix to the devel branch of this repository. I'll push the bug fix to CRAN asap.

Can you confirm that installing the devel branch solves your issue?

@akhst7
Copy link
Author

akhst7 commented Apr 13, 2021

@rcannood,

It works now. Thanks for fixing. I am gonna close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants