Repository: scverse/scanpy

Issue:
read_10x_h5() `genome` argument appears recently broken for 10x v2 format
- [x] I have checked that this issue has not already been reported.
- [x] I have confirmed this bug exists on the latest version of scanpy.
- [x] (optional) I have confirmed this bug exists on the master branch of scanpy.

---

To reproduce this issue:
1. download the public 10x dataset here (https://cf.10xgenomics.com/samples/cell-exp/2.1.0/hgmm_12k/hgmm_12k_raw_gene_bc_matrices_h5.h5) 
2. run the following

```python
import scanpy as sc

adata_human = sc.read_10x_h5('hgmm_12k_raw_gene_bc_matrices_h5.h5', genome='hg19')
adata_mouse = sc.read_10x_h5('hgmm_12k_raw_gene_bc_matrices_h5.h5', genome='mm10')

assert (adata_human.X != adata_mouse.X).sum() > 0, 'these count matrices are equal'
```

which produces the assertion error.  We see that the loaded data is the same regardless of `'genome'` argument.  A look at the file itself shows this is not the case (notice the number of gene names, which are different for hg19 and mm10):

![image](https://user-images.githubusercontent.com/10214815/165848884-0ef5c172-83f9-4ead-9687-0acadb496e87.png)


#### Versions

Also I think I can say confidently that this was working fine as of scanpy 1.8.1

<details>

-----
anndata     0.8.0
scanpy      1.9.1
-----
PIL                 8.1.0
appnope             0.1.2
backcall            0.2.0
cached_property     1.5.2
cellbender          NA
cffi                1.14.5
colorcet            3.0.0
cycler              0.10.0
cython_runtime      NA
dateutil            2.8.1
decorator           5.0.9
fontTools           4.33.3
h5py                3.2.0
igraph              0.9.10
ipykernel           5.5.5
ipython_genutils    0.2.0
ipywidgets          7.6.3
jedi                0.18.0
joblib              1.0.1
kiwisolver          1.3.1
leidenalg           0.8.10
llvmlite            0.38.0
lxml                4.8.0
matplotlib          3.5.1
matplotlib_inline   NA
mkl                 2.3.0
mpl_toolkits        NA
natsort             7.1.1
numba               0.55.1
numexpr             2.7.3
numpy               1.19.2
packaging           20.9
pandas              1.2.3
param               1.12.1
parso               0.8.2
pexpect             4.8.0
pickleshare         0.7.5
pkg_resources       NA
prompt_toolkit      3.0.18
psutil              5.8.0
ptyprocess          0.7.0
pycparser           2.20
pygments            2.8.0
pynndescent         0.5.6
pyparsing           2.4.7
pytz                2021.1
scipy               1.6.1
seaborn             0.11.2
session_info        1.0.0
six                 1.15.0
sklearn             0.24.1
skmisc              0.1.4
sphinxcontrib       NA
statsmodels         0.12.2
storemagic          NA
tables              3.6.1
texttable           1.6.4
tornado             6.1
tqdm                4.55.1
traitlets           5.0.5
typing_extensions   NA
umap                0.5.3
wcwidth             0.2.5
yaml                6.0
zipp                NA
zmq                 22.0.3
-----
IPython             7.23.1
jupyter_client      6.1.12
jupyter_core        4.7.1
notebook            6.4.0
-----
Python 3.7.9 (default, Aug 31 2020, 07:22:35) [Clang 10.0.0 ]
Darwin-20.6.0-x86_64-i386-64bit
-----

</details>



Discussion:
It seems like `_read_legacy_10x_h5()` invokes ` _collect_datasets()` without taking `genome` into account?  Perhaps this was lost during a refactor?
https://github.com/scverse/scanpy/blob/bd06cc3d1e0bd990f6994e54414512fa0b25fea0/scanpy/readwrite.py#L222
Yes, I think line 222 should be 

```python
    _collect_datasets(dsets, f[genome])
```

I will make a PR, and I will try to add a couple of tests to highlight this issue.

Resolving patch:
diff --git a/scanpy/readwrite.py b/scanpy/readwrite.py
--- a/scanpy/readwrite.py
+++ b/scanpy/readwrite.py
@@ -219,7 +219,7 @@ def _read_legacy_10x_h5(filename, *, genome=None, start=None):
                 )
 
             dsets = {}
-            _collect_datasets(dsets, f)
+            _collect_datasets(dsets, f[genome])
 
             # AnnData works with csr matrices
             # 10x stores the transposed data, so we do the transposition right away


Test patch:
diff --git a/scanpy/tests/_data/10x_data/1.2.0/multiple_genomes.h5 b/scanpy/tests/_data/10x_data/1.2.0/multiple_genomes.h5
new file mode 100644
Binary files /dev/null and b/scanpy/tests/_data/10x_data/1.2.0/multiple_genomes.h5 differ
diff --git a/scanpy/tests/test_read_10x.py b/scanpy/tests/test_read_10x.py
--- a/scanpy/tests/test_read_10x.py
+++ b/scanpy/tests/test_read_10x.py
@@ -73,6 +73,23 @@ def test_read_10x_h5_v1():
     assert_anndata_equal(spec_genome_v1, nospec_genome_v1)
 
 
+def test_read_10x_h5_v2_multiple_genomes():
+    genome1_v1 = sc.read_10x_h5(
+        ROOT / '1.2.0' / 'multiple_genomes.h5',
+        genome='hg19_chr21',
+    )
+    genome2_v1 = sc.read_10x_h5(
+        ROOT / '1.2.0' / 'multiple_genomes.h5',
+        genome='another_genome',
+    )
+    # the test data are such that X is the same shape for both "genomes",
+    # but the values are different
+    assert (genome1_v1.X != genome2_v1.X).sum() > 0, (
+        'loading data from two different genomes in 10x v2 format. '
+        'should be different, but is the same. '
+    )
+
+
 def test_read_10x_h5():
     spec_genome_v3 = sc.read_10x_h5(
         ROOT / '3.0.0' / 'filtered_feature_bc_matrix.h5',