Skip to content

Adjusting Coordinates in Cell bin JSON Files

Guang-Wei Zhang edited this page Aug 23, 2023 · 2 revisions

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