jianhuupenn / SpaGCN

SpaGCN: Integrating gene expression, spatial location and histology to identify spatial domains and spatially variable genes by graph convolutional network

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run SpaGCN with histology as .png instead of .tif

uthsavc opened this issue · comments

If it helps anyone, here is my code for when you do not have the .tif histology image and only have the png histology image, eg tissue_hires_image.png from SpaceRanger, and want to run SpaGCN. This issue was made in #7 and #31 but I had to write this code myself.

Basically, you need to use the scale factor in scalefactors_json.json to scale down the pixel coordinates. Specifically I created x_pixel and y_pixel using

scale=float( pd.read_json(f'path/to/scalefactors_json.json',orient='index').loc['tissue_hires_scalef'] )
x_pixel=np.array(adata.obs['x_pixel'] * scale).astype(int)
y_pixel=np.array(adata.obs['y_pixel'] * scale).astype(int)

Then when testing whether the coordinates worked, I multiplied the 20 by the scaling factor scale, i.e.

#Set coordinates
x_array=adata.obs["x_array"].tolist()
y_array=adata.obs["y_array"].tolist()
x_pixel=x_pixel_new.tolist()
y_pixel=y_pixel_new.tolist()

#Test coordinates on the image
img_new=img.copy()
for i in range(len(x_pixel)):
    x=x_pixel[i]
    y=y_pixel[i]
    img_new[int(x-20*scale):int(x+20*scale), int(y-20*scale):int(y+20*scale),:]=0

cv2.imwrite('map.jpg', img_new)

To create the adjacency matrix, you need to multiply b by scale:

#Calculate adjacent matrix
s=1
b=49*scale
adj=spg.calculate_adj_matrix(x=x_array,y=y_array, x_pixel=x_pixel, y_pixel=y_pixel, image=img, beta=b, alpha=s, histology=True)
#If histlogy image is not available, SpaGCN can calculate the adjacent matrix using the fnction below
#adj=calculate_adj_matrix(x=x_pixel,y=y_pixel, histology=False)
np.savetxt('spagcn_adj.csv', adj, delimiter=',')

The rest of the code is the same. Hope this helps someone.