[docs]defparsing(adata:AnnData,coordinates_file:int|str|bytes|PathLike[str]|PathLike[bytes],copy:bool=True,)->AnnData|None:"""\ Parsing the old spaital transcriptomics data Parameters ---------- adata Annotated data matrix. coordinates_file Coordinate file generated by st_spot_detector. copy Return a copy instead of writing to adata. Returns ------- Depending on `copy`, returns or updates `adata` with the following fields. **imagecol** and **imagerow** : `adata.obs` field Spatial information of the tissue image. """# Get a map of the new coordinatesnew_coordinates=dict()withopen(coordinates_file)asfilehandler:forlineinfilehandler.readlines():tokens=line.split()assertlen(tokens)>=6orlen(tokens)==4iftokens[0]!="x":old_x=int(tokens[0])old_y=int(tokens[1])new_x=round(float(tokens[2]),2)new_y=round(float(tokens[3]),2)iflen(tokens)>=6:pixel_x=float(tokens[4])pixel_y=float(tokens[5])new_coordinates[(old_x,old_y)]=(pixel_x,pixel_y)else:raiseValueError("Error, output format is pixel coordinates but\n ""the coordinates file only contains 4 columns\n")adata=adata.copy()ifcopyelseadatacounts_table=adata.to_df()new_index_values=list()imgcol=[]imgrow=[]forindexincounts_table.index:tokens=index.split("x")x=int(tokens[0])y=int(tokens[1])try:new_x,new_y=new_coordinates[(x,y)]imgcol.append(new_x)imgrow.append(new_y)new_index_values.append(f"{new_x}x{new_y}")exceptKeyError:counts_table.drop(index,inplace=True)# Assign the new indexes# counts_table.index = new_index_values# Remove genes that have now a total count of zerocounts_table=counts_table.transpose()[counts_table.sum(axis=0)>0].transpose()adata=AnnData(counts_table)adata.obs["imagecol"]=imgcoladata.obs["imagerow"]=imgrowadata.obsm["spatial"]=np.c_[[imgcol,imgrow]].reshape(-1,2)returnadataifcopyelseNone