kaizhang / anndata-rs

http://kzhang.org/anndata-rs/
29 stars 5 forks source link

Basic usage in rust? #11

Open nleroy917 opened 7 months ago

nleroy917 commented 7 months ago

Hello! I've had my eye on this crate for a bit...

Do you have any documentation for basic opening/closing of AnnData objects in rust? I see the python docs. Moreover, I see the readme says this:

We do not provide installation instructions here. Right now this package is bundled with the SnapATAC2 package. Please install SnapATAC2 to get these features.

But I am really interested in manipulation of AnnData objects from rust and not python. I see this issue about getting a CsrMatrix, but I couldn't even get that working.

Would love some basic tutorials if possible!

Nathan

nleroy917 commented 7 months ago

More specifically, I'm just trying to get this to run:

fn get_obs_cols(filename: &str) {
    let path = Path::new(filename);
    let adata = AnnData::<H5>::new(filename);
    let obs = adata.obs;
}

But I get:

the function or associated item `new` exists for struct `AnnData<H5>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`anndata_hdf5::H5: anndata::Backend`

which is odd, because I see here it does implement the Backend trait

nleroy917 commented 7 months ago

Not sure if it matters, but here is how I am specifying dependencies:

[dependencies]
anndata = { git = "https://github.com/kaizhang/anndata-rs"}
anndata-hdf5 = { git = "https://github.com/kaizhang/anndata-rs"}
nleroy917 commented 7 months ago

Interestingly, specifying a commit hash from last December has worked: 8f821fead0f01baffdf35eeb670ad8b62ad12511

kaizhang commented 7 months ago

Please install the packages from crates.io not from github

Liripo commented 2 months ago

I'm having problems trying to use: image

use std::path::Path;
use anndata::{AnnData, backend::Backend};
use anndata_hdf5::H5;

fn read_obs(filename: &str) -> DataFrame {
    let path = Path::new(filename);
    let adata = AnnData::<H5>::open(path)?;
    println!("{adata}");
    let obs = adata.obs;
    obs
}
kaizhang commented 2 months ago

I'm having problems trying to use: image

use std::path::Path;
use anndata::{AnnData, backend::Backend};
use anndata_hdf5::H5;

fn read_obs(filename: &str) -> DataFrame {
    let path = Path::new(filename);
    let adata = AnnData::<H5>::open(path)?;
    println!("{adata}");
    let obs = adata.obs;
    obs
}

You may have multiple versions installed. Please install the packages from crates.io not from github

Liripo commented 2 months ago

I switched to GitHub and it worked, but I encountered an error when reading the file:

Error: Cannot read shape information from type 'Scalar(f32)'

my code:

use anndata_hdf5::H5;
use anndata::{AnnData, AnnDataOp, Backend}; // 导入 AnnData 和 Backend
use anyhow::Result; // 导入 Result
use std::path::Path;

fn read_obs(filename: &str) -> Result<()> {
    let path = H5::open(Path::new(filename))?;
    let adata = AnnData::<H5>::open(path)?; // 如果 new 方法返回 Result,需要处理错误
    let obs = adata.read_obs()?;
    println!("obs {:?}",obs);
    Ok(())
}

fn main() -> Result<()> {
    read_obs(r"pbmc3k.h5ad")?;
    Ok(())
}

pbmc3k.h5ad can download from https://github.com/chanzuckerberg/cellxgene/raw/main/example-dataset/pbmc3k.h5ad