def read_visium_hd( path, genome = None, *, count_file = "filtered_feature_bc_matrix.h5", library_id = None, load_images = True, source_image_path = None): path = Path(path) adata = sc.read_10x_h5(path / count_file, genome=genome) adata.uns["spatial"] = dict() with File(path / count_file, mode="r") as f: attrs = dict(f.attrs) if library_id is None: library_id = attrs.pop("library_ids")[0].decode() adata.uns["spatial"][library_id] = dict() if load_images: files = dict( tissue_positions_file=path / "spatial/tissue_positions.parquet", scalefactors_json_file=path / "spatial/scalefactors_json.json", hires_image=path / "spatial/tissue_hires_image.png", lowres_image=path / "spatial/tissue_lowres_image.png", ) # check if files exists, continue if images are missing for f in files.values(): if not f.exists(): if any(x in str(f) for x in ["hires_image", "lowres_image"]): warnings.warn( f"You seem to be missing an image file.\n" f"Could not find '{f}'." ) else: raise OSError(f"Could not find '{f}'") adata.uns["spatial"][library_id]["images"] = dict() for res in ["hires", "lowres"]: try: adata.uns["spatial"][library_id]["images"][res] = imread( str(files[f"{res}_image"]) ) except Exception: raise OSError(f"Could not find '{res}_image'") # read json scalefactors adata.uns["spatial"][library_id]["scalefactors"] = json.loads( files["scalefactors_json_file"].read_bytes() ) adata.uns["spatial"][library_id]["metadata"] = { k: (str(attrs[k], "utf-8") if isinstance(attrs[k], bytes) else attrs[k]) for k in ("chemistry_description", "software_version") if k in attrs } # read coordinates positions = pd.read_parquet(files["tissue_positions_file"]) positions.set_index('barcode', inplace=True) positions.columns = ['in_tissue', 'array_row', 'array_col', 'pxl_col_in_fullres', 'pxl_row_in_fullres'] adata.obs = adata.obs.join(positions, how="left") adata.obsm["spatial"] = adata.obs[ ["pxl_row_in_fullres", "pxl_col_in_fullres"] ].to_numpy() adata.obs.drop( columns=["pxl_row_in_fullres", "pxl_col_in_fullres"], inplace=True, ) # put image path in uns if source_image_path is not None: # get an absolute path source_image_path = str(Path(source_image_path).resolve()) adata.uns["spatial"][library_id]["metadata"]["source_image_path"] = str( source_image_path ) return adata
from itertools import product samples = ['Colon_Cancer', 'Small_Intestine']resolutions = ['square_002um', 'square_008um', 'square_016um']combinations = product(samples, resolutions) for combin in combinations: sample, resolution = combin adata = read_visium_hd(f'./{sample}/{resolution}') adata.var_names_make_unique() adata.write(f'./{sample}_{resolution}.h5ad')
~/miniconda3/envs/official_Test/lib/python3.9/site-packages/anndata/_core/anndata.py:1830: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`. utils.warn_names_duplicates("var")