Skip to content

Let Scanpy read from Cell Ranger 3.0 outputs - #1

Merged
qianggong11 merged 29 commits into
masterfrom
read_CR3.0
Oct 29, 2018
Merged

qianggong11 merged 29 commits into
masterfrom
read_CR3.0

Conversation

@qianggong11

Copy link
Copy Markdown

Updated read_10x_h5:

  • Renamed the original read_10x_h5 as read_legacy_10x_h5;
  • Added read_v3_10x_h5 to read the new Cell Ranger output format;
  • The new read_10x_h5 determines the version of HDF5 input by the presence of the matrix key, and wraps the above two functions. In addition, it takes a gex_only argument which filters out feature barcoding counts from the outcome object when it is True (default). Otherwise, the full matrix will be retained.
  • For CR-v3, feature_types and genome were added into the outcome object as new attributes.

Updated read_10x_mtx:

  • Renamed the original read_10x_mtx as read_legacy_10x_mtx;
  • Added read_v3_10x_mtx to read the new Cell Ranger output format;
  • The new read_10x_mtx determines the version of matrix input by the presence of the genes.tsv file under the input directory, and wraps the above two functions. In addition, it takes a gex_only argument which filters out feature barcoding counts from the outcome object when it is True (default). Otherwise, the full matrix will be retained.
  • For CR-v3, feature_types was added into the outcome object as a new attribute.

Note for the genome argument:

  • There is a genome argument in Scanpy's read_10x_h5 function but not in read_10x_mtx as the genome was already specified by the path of input directory. The outcome object of the two functions should be the same which always take one genome at a time.
  • In this PR, when there are multiple genomes (e.g. Barnyard), read_10x_mtx always read them all, whereas read_10x_h5 always need to specify one of them (Scanpy makes mm10 by default).

The following datasets have been tested for both HDF5 and MEX reading. Downstream analytic functions (PCA, umap, clustering, DEG, plotting, etc) have been test for 1 v2 dataset and 2 v3 datasets. Related notebooks are under the custom analysis repo CA00063.

Lena ID Description Type CR-version
77767 PBMC_5000_v2, User A, Verification v2 4009.20.2
77775 PBMC_5000_v3, User A, Verification_GEX pbmc 4009.30.0
89585 Wk4_Barnyard_1000_Mainline_Rep 2 barnyard 4009.30.0
80818 PAX35:899_500 cells_73% viable_SC3v3_GEX + CAP crispr 4009.30.0
88938 3p_Mainline_4_1K_PBMCs_userA_stain1_rep1_meta antibody 4009.30.0
89758 VDJ-FB_1K PBMC + TotalSeqC 20/3 panel_r1_Meta vdj-fb 4009.30.0
86611 5'Val2_37-38_D223_CMV+ PBMC_sorted_combined_meta dextramer 4009.29.0

@qianggong11 qianggong11 self-assigned this Oct 18, 2018

@pmarks pmarks left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This looks great, thanks Qiang. Once Nigel has a quick look, let's send this on to scanpy.

@ambarrio

Copy link
Copy Markdown

Great PR @qianggong11 Good job! Keep it rolling in scanpy 👏

@evolvedmicrobe evolvedmicrobe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Looks great overall, nice job!

I made a few comments to address throughout. Another request I would make is that you add tests for this functionality. This repo uses a CI testing framework, and by adding tests you make the code a lot easier to review, as people can see exactly how the new function works, and also let's people verify that the testing you've clearly already done, are good tests. Perhaps most importantly, you also guard against future changes people make, as there are a lot of ways to break stuff in python by having name conflicts or just small changes that have unintended consequences. Finally, at some point later, a refactoring may be called for, and this is a lot easier todo if there are some basic tests around.

You don't have to move all the testing you did over, but some small ones would be good. I made some small test files for equivalent Seurat pull that you can grab here if it helps:

https://github.com/evolvedmicrobe/seurat/pull/1/files

Comment thread scanpy/readwrite.py Outdated
Filename.
genome : :class:`str`, optional (default: ``'mm10'``)
Genome group in hdf5 file.
gex_only : :class:`bool`, optional (default: ``True``)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not deeply familiar with the documentation system, but noticed other doc strings in the file are formatted like so:

make_unique : `bool`, optional (default: `True`)

Where :class: doesn't seemed to be used for primitive types like int or str, the default value True only has one set of strings around it. Just verifying, have you checked if the documentation this produces looks okay?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to one set of strings. As there the authors use :class: to elsewhere, I'm keeping it there as we do not intend to change the style.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you see them use :class:? I count 123 instances in this repo where the primitive type bool is not decorated with :class:, 0 where it is.

grep -rnw \`bool\` .
./scanpy/tools/dpt.py:67:    allow_kendall_tau_shift : `bool`, optional (default: `True`)
./scanpy/tools/dpt.py:71:    copy : `bool`, optional (default: `False`)
./scanpy/tools/sim.py:44:    params_file : `bool`, (default: `True`)
./scanpy/tools/sim.py:48:    branching : `bool`, optional (default: `None`)
./scanpy/tools/phate.py:83:    verbose : `bool`, `int` or `None`, optional (default: `sc.settings.verbosity`)
./scanpy/tools/phate.py:86:    copy : `bool` (default: `False`)
./scanpy/tools/paga.py:44:    use_rna_velocity : `bool` (default: `False`)
./scanpy/tools/paga.py:51:    copy : `bool`, optional (default: `False`)
./scanpy/tools/rank_genes_groups.py:35:    use_raw : `bool`, optional (default: `True`)
./scanpy/tools/rank_genes_groups.py:53:    rankby_abs : `bool`, optional (default: `False`)
./scanpy/tools/louvain.py:51:    use_weights : `bool`, optional (default: `False`)
./scanpy/tools/louvain.py:59:    copy : `bool` (default: `False`)
./scanpy/tools/umap.py:84:    copy : `bool` (default: `False`)
./scanpy/tools/draw_graph.py:56:    proceed : `bool`, optional (default: `None`)
./scanpy/tools/draw_graph.py:60:    copy : `bool` (default: `False`)
./scanpy/tools/score_genes.py:47:    copy : `bool`, optional (default: `False`)
./scanpy/tools/score_genes.py:49:    use_raw : `bool`, optional (default: `False`)
./scanpy/tools/score_genes.py:147:    copy : `bool`, optional (default: `False`)
./scanpy/tools/diffmap.py:25:    copy : `bool` (default: `False`)
./scanpy/tools/tsne.py:58:    use_fast_tsne : `bool`, optional (default: `True`)
./scanpy/tools/tsne.py:62:    copy : `bool` (default: `False`)
./scanpy/readwrite.py:53:    cache : `bool`, optional (default: `False`)
./scanpy/readwrite.py:62:    first_column_names : `bool`, optional (default: `False`)
./scanpy/readwrite.py:150:    make_unique : `bool`, optional (default: `True`)
./scanpy/readwrite.py:153:    cache : `bool`, optional (default: `False`)
./scanpy/plotting/tools/__init__.py:34:    use_raw : `bool`, optional (default: `True`)
./scanpy/plotting/tools/__init__.py:39:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/tools/__init__.py:66:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/tools/__init__.py:84:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/tools/__init__.py:171:    sharey: `bool`, optional (default: True)
./scanpy/plotting/tools/__init__.py:468:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/tools/__init__.py:471:    split : `bool`, optional (default: `True`)
./scanpy/plotting/tools/__init__.py:475:    strip : `bool`, optional (default: `True`)
./scanpy/plotting/tools/__init__.py:477:    jitter : `int`, `float`, `bool`, optional (default: `True`)
./scanpy/plotting/tools/__init__.py:552:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:215:    single_component : `bool`, optional (default: `False`)
./scanpy/plotting/tools/paga.py:240:    export_to_gexf : `bool`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:243:    normalize_to_color : `bool`, optional (default: `False`)
./scanpy/plotting/tools/paga.py:254:    add_pos : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:258:    frameon : `bool`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:260:    show : `bool`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:262:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:812:    use_raw : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:830:    as_heatmap : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:833:    show_node_names : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:835:    show_colorbar : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:837:    show_yticks : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:839:    normalize_to_zero_one : `bool`, optional (default: `True`)
./scanpy/plotting/tools/paga.py:841:    return_data : `bool`, optional (default: `False`)
./scanpy/plotting/tools/paga.py:843:    show : `bool`, optional (default: `None`)
./scanpy/plotting/tools/paga.py:845:    save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/anndata.py:71:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:578:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:580:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:582:    multi_panel : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:584:    stripplot : `bool` optional (default: `True`)
./scanpy/plotting/anndata.py:587:    jitter : `float` or `bool`, optional (default: `True`)
./scanpy/plotting/anndata.py:706:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:779:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:781:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:787:    stripplot : `bool` optional (default: `True`)
./scanpy/plotting/anndata.py:790:    jitter : `float` or `bool`, optional (default: `True`)
./scanpy/plotting/anndata.py:815:    swap_axes: `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:993:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:995:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:1141:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:1143:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:1349:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:1351:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/anndata.py:1460:    log : `bool`, optional (default: `False`)
./scanpy/plotting/anndata.py:1462:    use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/docs.py:11:use_raw : `bool`, optional (default: `None`)
./scanpy/plotting/docs.py:18:edges : `bool`, optional (default: `False`)
./scanpy/plotting/docs.py:24:arrows : `bool`, optional (default: `False`)
./scanpy/plotting/docs.py:30:sort_order : `bool`, optional (default: `True`)
./scanpy/plotting/docs.py:53:frameon : `bool`, optional (default: `True`)
./scanpy/plotting/docs.py:71:show : `bool`, optional (default: `None`)
./scanpy/plotting/docs.py:73:save : `bool` or `str`, optional (default: `None`)
./scanpy/plotting/preprocessing.py:20:    log : `bool`
./scanpy/plotting/preprocessing.py:24:    save : `bool` or `str`, optional (default: `None`)
./scanpy/settings.py:91:    scanpy : `bool`, optional (default: `True`)
./scanpy/settings.py:98:    frameon : `bool`, optional (default: `True`)
./scanpy/settings.py:100:    vector_friendly : `bool`, optional (default: `True`)
./scanpy/settings.py:106:    transparent : `bool`, optional (default: `True`)
./scanpy/preprocessing/magic.py:64:    verbose : `bool`, `int` or `None`, optional (default: `sc.settings.verbosity`)
./scanpy/preprocessing/magic.py:67:    copy : `bool` or `None`, optional. Default: `None`.
./scanpy/preprocessing/dca.py:49:    normalize_per_cell : `bool`, optional. Default: `True`.
./scanpy/preprocessing/dca.py:55:    scale : `bool`, optional. Default: `True`.
./scanpy/preprocessing/dca.py:59:    log1p : `bool`, optional. Default: `True`.
./scanpy/preprocessing/dca.py:67:    batchnorm : `bool`, optional. Default: `True`.
./scanpy/preprocessing/dca.py:89:    verbose : `bool`, optional. Default: `False`.
./scanpy/preprocessing/dca.py:93:    return_model : `bool`, optional. Default: `False`.
./scanpy/preprocessing/dca.py:95:    return_info : `bool`, optional. Default: `False`.
./scanpy/preprocessing/dca.py:100:    copy : `bool`, optional. Default: `False`.
./scanpy/preprocessing/recipes.py:90:    log : `bool`, optional (default: `True`)
./scanpy/preprocessing/recipes.py:92:    plot : `bool`, optional (default: `True`)
./scanpy/preprocessing/recipes.py:94:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/mnn_correct.py:50:    cos_norm_in : `bool`, optional (default: True)
./scanpy/preprocessing/mnn_correct.py:53:    cos_norm_out : `bool`, optional (default: True)
./scanpy/preprocessing/mnn_correct.py:59:    var_adj : `bool`, optional (default: True)
./scanpy/preprocessing/mnn_correct.py:62:    compute_angle : `bool`, optional (default: False)
./scanpy/preprocessing/mnn_correct.py:73:    do_concatenate : `bool`, optional (default: True)
./scanpy/preprocessing/mnn_correct.py:75:    save_raw : `bool`, optional (default: False)
./scanpy/preprocessing/simple.py:70:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:179:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:287:    log : `bool`, optional (default: `True`)
./scanpy/preprocessing/simple.py:289:    subset : `bool`, optional (default: `True`)
./scanpy/preprocessing/simple.py:292:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:452:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:493:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:531:    zero_center : `bool` or `None`, optional (default: `True`)
./scanpy/preprocessing/simple.py:542:    return_info : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:545:    use_highly_variable : `bool`, optional (default: `None`)
./scanpy/preprocessing/simple.py:549:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:552:    chunked : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:695:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:833:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:951:    zero_center : `bool`, optional (default: `True`)
./scanpy/preprocessing/simple.py:956:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:1007:    copy : `bool`, optional (default: `False`)
./scanpy/preprocessing/simple.py:1054:    copy : `bool`, optional (default: `False`)
./scanpy/neighbors/__init__.py:738:        density_normalize : `bool`
./scanpy/neighbors/__init__.py:778:        sym : `bool`

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. It seems this function (read_10x_h5) is the only place of such use. Although it was from the authors, I will correct it. Thank you.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see how we both had the impressions we did now! Thanks for clarifying.

Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
Comment thread scanpy/readwrite.py Resolved
- Move four functions (read_v3_* and read_legacy_*) as internal
- Use os.path.join for potential Windows users.
- Some stylish editing.
@qianggong11 qianggong11 reopened this Oct 27, 2018
@qianggong11

Copy link
Copy Markdown
Author

Thank @evolvedmicrobe's comments which are all very helpful. Testing datasets (v2 and v3 matrix and HDF5 files) and the testing code (read_10x.py) have been added. All the other comments have been addressed. Reading across the above mentioned internal datasets have been re-done.

@evolvedmicrobe

evolvedmicrobe commented Oct 27, 2018 •

Copy link
Copy Markdown
Collaborator

Excellent! This is really cool! LGTM and good to merge.

One final comment, at some 10X repositories we have the "squash and merge" option enabled on GitHub, which combines a series of commits in one pull request into a single commit to be merged into the repo. Prior to this feature, to keep commit logs simple and make things look cleaner, it was common for people to squash several commits into one before committing. In practice, everyone stumbles around a bit and runs into problems that lead to multiple commits, but it looks cleaner if it appears as though it was one single commit and rapid action that led to the new feature.

I looked at one PR for this repo here, scverse#289, and it appears they merge without squashing so you'll have this entire commit history as part of the repo going forward. If you felt like it, it might make for a cleaner commit to squash it first, but this isn't at all necessary as the code will work just as well either way.

@qianggong11

Copy link
Copy Markdown
Author

👍 Good point. Will do.

@pmarks

pmarks commented Oct 27, 2018 via email

Copy link
Copy Markdown

@qianggong11
qianggong11 merged commit 4cdb7f0 into master Oct 29, 2018
@qianggong11

Copy link
Copy Markdown
Author

FYI, this PR has been merged to theislab/scanpy:master. Thanks.

@pmarks

pmarks commented Oct 29, 2018 via email

Copy link
Copy Markdown

govinda-kamath pushed a commit that referenced this pull request Mar 30, 2023
update to match upstream
@govinda-kamath
govinda-kamath deleted the read_CR3.0 branch April 10, 2023 17:25
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

Successfully merging this pull request may close these issues.

4 participants