Skip to content

convert cell bin gef file to h5ad file

Guang-Wei Zhang edited this page Aug 22, 2023 · 3 revisions
import pandas as pd
import stereo as st
import warnings
import os

# Load the data frame from Excel
data_file_path = r'XXXXXXXXXXXXXXXX.xlsx'
df = pd.read_excel(data_file_path)

# Filter out the warnings
warnings.filterwarnings('ignore')

# Define a list of file paths
file_list = [
    'XXXXXXXXXXXXX.cellbin.gef'
]

# Iterate over rows in the data frame
for idx, row in df.iterrows():
    # Check for a matching file in the file list
    for file_path in file_list:
        if row['id_chip'] in file_path:
            print(f"Processing: {file_path}")
            
            # Extract boundaries from the data frame row
            x_min, x_max, y_min, y_max = row['x_min'], row['x_max'], row['y_min'], row['y_max']

            # Read and process the .gef data
            data = st.io.read_gef(file_path=file_path, bin_type='cell_bins', region=[x_min, x_max, y_min, y_max])
            data.tl.cal_qc()
            data.plt.spatial_scatter(show_ticks=True)

            # Define the output file path and save data
            output_folder = 'XXXXXXXXXXXXXXXXXXXX'
            output_file_name = f"{row['segment']}_{row['location_chip']}_{row['id_chip']}.cellbin.h5ad"
            output_path = os.path.join(output_folder, output_file_name)
            
            data.tl.raw_checkpoint()
            adata = st.io.stereo_to_anndata(data, flavor='scanpy', output=output_path)
            
            break  # Break out of the inner loop after processing

Code Description

This code is designed to process spinal cord stereo sequencing data. It performs the following tasks:

  1. Loading Data: The script begins by loading a specified Excel file into a Pandas dataframe. This file contains metadata for processing .gef files.

  2. File List Definition: A list of .gef file paths is defined. These files are expected to contain the data for stereo sequencing.

  3. Processing: The code then iterates through each row of the loaded Excel file. For each row:

    • It searches for a corresponding .gef file in the file list.
    • Upon finding a match, it extracts certain boundary values from the current row.
    • Using the Stereo library, the .gef data within these boundaries is read and processed.
    • Quality control is applied to the data and a spatial scatter plot is generated.
    • The processed data is then saved as a .h5ad file with a defined naming convention.
  4. Error Handling and Logging: Warnings generated during the processing are suppressed for clarity.

This script is particularly useful for users working with stereo sequencing data, especially those interested in visualizing or further analyzing the extracted and processed segments from .gef files.

Data processing pipeline

preparation of cell bin data

Data Structure of scanpy

Clone this wiki locally