-
Notifications
You must be signed in to change notification settings - Fork 0
convert cell bin gef file to h5ad file
import pandas as pd
import stereo as st
import warnings
import os
# Load the data frame from Excel
data_file_path = r'XXXXXXXXXXXXX.xlsx' # this excel file contains the section meta data and the cropping coordination
df = pd.read_excel(data_file_path)
# Filter out the warnings
warnings.filterwarnings('ignore')
# Define a list of file paths
file_list = [
'/media/zhang/Elements1/cell_bin_gef/B01204C2.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 = 'XXXXXXXXXXXX'
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
Cleaned-up Code:
import pandas as pd
import stereo as st
import warnings
import os
# Load the data frame from Excel
data_file_path = r'/home/zhang/spinalcord_stereoseq/info_chip_cropping_v2.xlsx'
df = pd.read_excel(data_file_path)
# Filter out the warnings
warnings.filterwarnings('ignore')
# Define a list of file paths
file_list = [
'/media/zhang/Elements1/cell_bin_gef/B01204C2.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 = '/media/zhang/Elements1/spinalcord_gef_files/B01204C2/'
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 processingThis code is designed to process spinal cord stereo sequencing data. It performs the following tasks:
-
Loading Data: The script begins by loading a specified Excel file into a Pandas dataframe. This file contains metadata for processing
.geffiles. -
File List Definition: A list of
.geffile paths is defined. These files are expected to contain the data for stereo sequencing. -
Processing: The code then iterates through each row of the loaded Excel file. For each row:
- It searches for a corresponding
.geffile in the file list. - Upon finding a match, it extracts certain boundary values from the current row.
- Using the Stereo library, the
.gefdata 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
.h5adfile with a defined naming convention.
- It searches for a corresponding
-
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.