[docs]defwrite(filename:str|Path,adata:AnnData,extension:str|bool=None,compression:Literal["gzip","lzf"]|None="gzip",compression_opts:int|None=None,)->None:"""Write :class:`~anndata.AnnData` objects to file. It is possbile to either write an :class:`~anndata.AnnData` object to a .csv file or a .h5ad file. The .h5ad file can be used as a cache to save the current state of the object and to retrieve it faster once needed. This preserves the object state at the time of writing. It is possible to write both, encoded and unencoded objects. Args: filename: File name or path to write the file to. adata: Annotated data matrix. extension: File extension. One of 'h5ad', 'csv'. Defaults to `None` which infers the extension from the filename. compression: Optional file compression. One of 'gzip', 'lzf'. compression_opts: See http://docs.h5py.org/en/latest/high/dataset.html. Examples: >>> import ehrapy as ep >>> adata = ep.dt.mimic_2(encoded=True) >>> ep.io.write("mimic_2.h5ad", adata) """filename=Path(filename)# allow passing stringsif_get_file_extension(filename):filename=filename_extension=_get_file_extension(filename)ifextensionisNone:extension=_extensionelifextension!=_extension:raiseValueError("It suffices to provide the file type by ""providing a proper extension to the filename."'One of "csv", "h5".')else:key=filenameextension=settings.file_format_dataifextensionisNoneelseextensionfilename=_get_filename_from_key(key,extension)ifextension=="csv":adata.write_csvs(filename)else:# dummy encoding when there is non numerical data in Xifnotnp.issubdtype(adata.X.dtype,np.number)andextension=="h5ad":# flag to indicate an Anndata object has been dummy encoded to write it to .h5ad file# Case of writing an unencoded non numerical AnnData objectencoded_adata=encode(adata,autodetect=True)encoded_adata.uns["ehrapy_dummy_encoding"]=Trueencoded_adata.uns["columns_obs_only"]=list(adata.obs.columns)encoded_adata.write(filename,compression=compression,compression_opts=compression_opts)else:adata.write(filename,compression=compression,compression_opts=compression_opts)
def_get_file_extension(file_path:Path)->str:"""Check whether the argument is a filename. Args: file_path: Path to the file. Returns: File extension of the specified file. """ext=file_path.suffixesiflen(ext)>2:ext=ext[-2:]ifextandext[-1][1:]insupported_extensions:returnext[-1][1:]raiseValueError(f"""\{file_path!r} does not end on a valid extension. Please, provide one of the available extensions.{supported_extensions} """)def_get_filename_from_key(key,extension=None)->Path:"""Gets full file name from a key. Args: key: Key to get file name for. extension: file extension. Returns: Path to the full file. """extension=settings.file_format_dataifextensionisNoneelseextensionextension="csv"ifextensionisNoneelseextensionreturnsettings.datasetdir/f"{key}.{extension}"