使用空间转录组文件gem生成相应的灰度图 | Word Count: 312 | Reading Time: 1mins | Post Views:
一般我们在进行圈细胞之前需要将基因在空间上的表达情况绘制出来,同时与拍照的TIF图片进行对应,在我的上篇博客里面用了一位师兄的代码,但是却发现其执行效率过于低下,经常一张芯片要运行四五个小时。故自己写了新的架构,将速度提升了数百倍。代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import pandas as pdfrom PIL import Imageimport matplotlib.pyplot as pltfrom scipy.sparse import csr_matriximport numpy as npimport skimageimport cv2''' log setting ''' import logginglog = logging.getLogger('Console' ) log.setLevel(logging.DEBUG) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) console_handler.setFormatter(formatter) log.addHandler(console_handler) def generate_geneExpression_png ( gem_file_path: str , save_path: str ): ''' 这里的 gem_file_path 指的是你的 gem 文件地址 如 ~/gem/***.gem.gz save_path 指的是保存灰度图的文件地址,如 ~/geneExpression/***.png ''' log.info('start read gem file' ) gem_file = pd.read_csv(gem_file_path, sep='\t' ,comment='#' ) x, y = gem_file["x" ].values, gem_file["y" ].values shape = (x.max () + 1 ,y.max () + 1 ) log.info('matrix generating' ) X = csr_matrix((gem_file["MIDCount" ].values, (x, y)), shape=shape, dtype=np.uint16) mtx = X.todense().astype(np.uint8) mtx_ = skimage.color.gray2rgb(np.array(mtx)) log.info('matrix ploting' ) plt.imsave(save_path, mtx_) return mtx_