Skip to content

Adjusting Coordinates in Cell bin JSON Files

Guang-Wei Zhang edited this page Aug 23, 2023 · 2 revisions
import os
import re
import json
import pandas as pd

def extract_pattern(filename):
    """
    Extract the specific pattern from the filename.
    """
    match = re.search(r"(_\w+_[A-Z0-9]+)\.", filename)
    return match.group(1) if match else None

def adjust_coordinates_v3(json_path, csv_data):
    """
    Adjust the coordinates in the JSON file using the corresponding x_min and y_min values from the CSV data.
    This version matches based on the specific substring pattern in the filename.
    """
    # Extract the filename pattern from the path
    filename_pattern = extract_pattern(os.path.basename(json_path))
    
    # Find the matching row in the CSV data based on the extracted pattern
    matching_row = csv_data[csv_data["filename"].str.contains(filename_pattern, na=False)]
    
    if matching_row.empty:
        return f"Pattern {filename_pattern} not found in CSV data. Skipping adjustment."
    
    # Get the x_min and y_min values for the matching row
    x_min = matching_row["x_min"].values[0]
    y_min = matching_row["y_min"].values[0]
    
    # Load the JSON data
    with open(json_path, 'r') as file:
        data = json.load(file)
        
    # Adjust the coordinates in the JSON data
    for shape in data["shapes"]:
        shape["points"] = [[point[0] - x_min, point[1] - y_min] for point in shape["points"]]
        
    # Save the adjusted JSON data to the offset_json subfolder
    offset_path = os.path.join(os.path.dirname(json_path), "offset_json", os.path.basename(json_path))
    with open(offset_path, 'w') as file:
        json.dump(data, file)
    
    return f"Coordinates in {json_path} adjusted successfully based on pattern {filename_pattern}."

def process_folder(csv_path, json_folder):
    """
    Process all JSON files in the specified folder using the offset values from the CSV file.
    """
    # Read the CSV data
    csv_data = pd.read_csv(csv_path)
    
    # Ensure the offset_json subfolder exists
    offset_folder = os.path.join(json_folder, "offset_json")
    if not os.path.exists(offset_folder):
        os.makedirs(offset_folder)
    
    # List all JSON files in the specified folder
    json_files = [f for f in os.listdir(json_folder) if f.endswith('.json') and os.path.isfile(os.path.join(json_folder, f))]
    
    # Adjust coordinates for each JSON file
    for json_file in json_files:
        adjust_coordinates_v3(os.path.join(json_folder, json_file), csv_data)
        print(f"Processed {json_file}")

# Example usage
# csv_path = "path_to_off_set.csv"
# json_folder = "path_to_folder_containing_json_files"
# process_folder(csv_path, json_folder)

Adjusting Coordinates in JSON Files

This script provides a utility to adjust the coordinates in JSON files based on the offsets provided in a CSV file. The coordinates are adjusted by subtracting the corresponding x_min and y_min values from each x and y coordinate in the JSON file, respectively.

Dependencies:

  • pandas
  • json
  • os
  • re

How to Use:

  1. Ensure you have the necessary libraries installed:
pip install pandas
  1. Place the script in your project directory.

  2. Set the path to your off_set.csv file and the folder containing your JSON files in the example usage section at the bottom of the script.

  3. Run the script.

Code Explanation:

  • extract_pattern(filename):

    • This function extracts a specific pattern (e.g., _lr_D01158C2) from the filename using regular expressions.
    • The pattern is used to match filenames in the CSV data.
  • adjust_coordinates_v3(json_path, csv_data):

    • This function adjusts the coordinates in a given JSON file using the offset values provided in the CSV data.
    • It matches files based on the specific substring pattern extracted from the filename.
    • The adjusted JSON file is saved in a subfolder named offset_json within the original folder.
  • process_folder(csv_path, json_folder):

    • This function processes all JSON files in the specified folder.
    • It reads the offset values from the CSV file, adjusts the coordinates in each JSON file, and saves the adjusted files in the offset_json subfolder.

Example Usage:

Replace the placeholders in the example usage section with your specific paths:

csv_path = "path_to_off_set.csv"
json_folder = "path_to_folder_containing_json_files"
process_folder(csv_path, json_folder)

Uncomment these lines and run the script to adjust the coordinates in all JSON files within the specified folder.

Data processing pipeline

preparation of cell bin data

Data Structure of scanpy

Clone this wiki locally