icbi-lab / infercnvpy

Infer copy number variation (CNV) from scRNA-seq data. Plays nicely with Scanpy.
https://infercnvpy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
122 stars 27 forks source link

ValueError: Reference must match the number of genes in AnnData. #134

Closed HZT55 closed 1 week ago

HZT55 commented 2 weeks ago

Report

Hi,

I am trying to use infercnvpy directly supply an array of average normal gene expression, but when I run:

cnv.tl.infercnv(
    adata,
    reference = normal_expr,
    window_size=250,
)

I get the error message: Reference must match the number of genes in AnnData.

The normal_expr is generated by the following code:

normal_raw_matrix_sort = normal_raw_matrix.loc[adata.var.index.intersection(normal_raw_matrix.index)]
normal_raw_matrix_sort = normal_raw_matrix_sort.loc[adata.var.index]
normal_expr = normal_raw_matrix_sort.mean(axis=1).values

So the number and order of genes in normal_expr does match the adata.

assert len(normal_expr) == adata.shape[1]
try:
    cnv.tl.infercnv(
        GSE131907_adata,
        reference=normal_expr,
        window_size=250,
    )
except ValueError as e:
    print("Error: ", e)
    print("Shape of normal_expr: ", normal_expr.shape)
    print("Shape of GSE131907_adata: ", GSE131907_adata.shape)`
Error:  Reference must match the number of genes in AnnData. 
Shape of normal_expr:  (18383,)
Shape of GSE131907_adata:  (55320, 18383)

How do I deal with this?

Many thanks!

Version information

anndata 0.10.7 infercnvpy 0.4.5 matplotlib 3.8.4 numpy 1.26.4 pandas 2.2.1 scanpy 1.10.1

grst commented 2 weeks ago

Hi @HZT55,

thanks for reporting the issue! I think I found the problem. The issue was that the the X and Y chromosomes are by default removed from the anndata, and therefore the reference didn't match anymore.

The problem should be solved in #135. Would be great if you could test it by installing this version via

pip install git+https://github.com/icbi-lab/infercnvpy.git@fix-134

Cheers, Gregor

HZT55 commented 1 week ago

Hi Gregor,

Thank you for the quick fix! I can confirm that the issue has been resolved. Everything is working smoothly now.

grst commented 1 week ago

Great, I'll include it in the next release then.