Skip to content
Guang-Wei Zhang edited this page Sep 7, 2023 · 3 revisions

General working pipeline

#Certainly! Installing Jupyter Notebook and setting up a kernel within a Conda virtual environment is a common task, especially when working on multiple projects with different requirements. Here's how you can do that:

Install Conda If you haven't installed Conda yet, you can get it through Miniconda or Anaconda. Miniconda is a minimal installer for conda, while Anaconda comes with many data science packages preinstalled. Anaconda: Go to the official website and download the appropriate installer.

Installation of stereopy and generate bin1 visulization for tissue cutting

installation of stereopy (now could work with Win OS)

conda create --name st python=3.8 
conda activate st
conda install stereopy -c stereopy -c grst -c numba -c conda-forge -c bioconda

Install Jupyter Notebook in virtual environment

conda install jupyter
conda install ipykernel
python -m ipykernel install --user --name=st

Convert all gef files into scanpy h5ad files for easy manipulation and better compatibility (you will not be restricted by the default stereopy tools)

import stereo as st
import warnings
import pandas as pd
import os

warnings.filterwarnings('ignore')

folder_path = r'S:\spinal_cord_2nd_Batch\sp_2nd_gef'
output_folder = r'S:\spinal_cord_2nd_Batch\sp_2nd_gef/bin1_h5ad_files'

# Automatically generate the list of .gef files in the folder
file_list = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.gef')]

for file_path in file_list:
    data = st.io.read_gef(file_path=file_path, bin_type='bins', bin_size=1)
    # Additional processing code goes here...

    output_file_name = os.path.basename(file_path).replace('.gef', '_bin1.h5ad')  # Extract the filename and change the extension
    output_path = os.path.join(output_folder, output_file_name)  # Combine the folder path with the filename
    data.tl.raw_checkpoint()
    adata = st.io.stereo_to_anndata(data, flavor='scanpy', output=output_path)
    # Optionally, break or continue the loop as needed

Plot the bin1 1-to-1 pixel to bin unit png for easy manipulation

import numpy as np
import scanpy as sc
from matplotlib.colors import Normalize
from matplotlib.cm import get_cmap
from PIL import Image
import os

def plot_raster(adata, filename):
    x_coords = adata.obs['x'].values
    y_coords = adata.obs['y'].values

    x_coords -= int(x_coords.min())
    y_coords -= int(y_coords.min())

    gene_counts = adata.X.sum(axis=1).A1
    x_max = int(x_coords.max())
    y_max = int(y_coords.max())

    image = np.ones((y_max + 1, x_max + 1, 3), dtype=np.uint8) * 255

    norm = Normalize(vmin=gene_counts.min(), vmax=gene_counts.max())
    colormap = get_cmap('viridis')

    # Using vectorized operations
    colors = (colormap(norm(gene_counts))[:, :3] * 255).astype(np.uint8)
    image[y_coords.astype(int), x_coords.astype(int)] = colors

    Image.fromarray(image).save(filename + '.png')

directory = r'S:\spinal_cord_2nd_Batch\sp_2nd_gef/bin1_h5ad_files'

for filename in os.listdir(directory):
    if filename.endswith('.h5ad'):
        full_path = os.path.join(directory, filename)

        try:
            adata = sc.read(full_path)
            plot_raster(adata, full_path)
        except Exception as e:
            print(f"Error processing {filename}: {e}")