importzipfileaszfimportscanpyasscfromanndataimportAnnDatafrom.._settingsimportsettings# TODO - Add scanpy and covert this over.
[docs]defvisium_sge(sample_id="V1_Breast_Cancer_Block_A_Section_1",*,include_hires_tiff:bool=False,)->AnnData:"""Processed Visium Spatial Gene Expression data from 10x Genomics’ database. The database_ can be browsed online to find the ``sample_id`` you want. .. _database: https://support.10xgenomics.com/spatial-gene-expression/datasets Parameters ---------- sample_id The ID of the data sample in 10x’s spatial database. include_hires_tiff Download and include the high-resolution tissue image (tiff) in `adata.uns["spatial"][sample_id]["metadata"]["source_image_path"]`. Returns ------- Annotated data matrix. """sc.settings.datasetdir=settings.datasetdirreturnsc.datasets.visium_sge(sample_id,include_hires_tiff=include_hires_tiff)
[docs]defxenium_sge(base_url="https://cf.10xgenomics.com/samples/xenium/1.0.1",image_filename="he_image.ome.tif",alignment_filename="he_imagealignment.csv",zip_filename="outs.zip",library_id="Xenium_FFPE_Human_Breast_Cancer_Rep1",include_hires_tiff:bool=False,):""" Download and extract Xenium SGE data files. Unlike scanpy this current does not load the data. Data is located in `settings.datasetdir` / `library_id`. Args: base_url: Base URL for downloads image_filename: Name of the image file to download alignment_filename: Name of the affine transformation file to download zip_filename: Name of the zip file to download library_id: Identifier for the library include_hires_tiff: Whether to download the high-res TIFF image """sc.settings.datasetdir=settings.datasetdirlibrary_dir=settings.datasetdir/library_idlibrary_dir.mkdir(parents=True,exist_ok=True)files_to_extract=["cell_feature_matrix.h5","cells.csv.gz","experiment.xenium"]all_sge_files_exist=all((library_dir/sge_file).exists()forsge_fileinfiles_to_extract)download_filenames=[]ifnotall_sge_files_exist:download_filenames.append(zip_filename)ifinclude_hires_tiffand(not(library_dir/alignment_filename).exists()ornot(library_dir/image_filename).exists()):download_filenames+=[alignment_filename,image_filename]forfile_nameindownload_filenames:file_path=library_dir/file_nameurl=f"{base_url}/{library_id}/{library_id}_{file_name}"ifnotfile_path.is_file():sc.readwrite._download(url=url,path=file_path)ifnotall_sge_files_exist:try:zip_file_path=library_dir/zip_filenamewithzf.ZipFile(zip_file_path,"r")aszip_ref:forzip_filenameinfiles_to_extract:withopen(library_dir/zip_filename,"wb")asfile_name:file_name.write(zip_ref.read(f"outs/{zip_filename}"))exceptzf.BadZipFile:raiseValueError(f"Invalid zip file: {library_dir/zip_filename}")