diff --git a/flip_timelapse_SUMO.py b/flip_timelapse_SUMO.py new file mode 100644 index 0000000000000000000000000000000000000000..4a876deb2f15fb757fcacd233510f7825f33584a --- /dev/null +++ b/flip_timelapse_SUMO.py @@ -0,0 +1,92 @@ +import tifffile as tf +import numpy as np +import pandas as pd +import os +import logging +from tqdm import tqdm + +logging.basicConfig(level="DEBUG") +logger = logging.getLogger(__name__) + + +path = "/home/aaristov/Multicell/Sebastien/mESC_aggregates/SUMO/SOX1-T_Movie/{well}/POINT 00001/aligned.tif" + +coords = { + "H10": [34, 35, 36, 37, 38, 39, 40], + "I12": [15, 18, 20], + "J08": [37], + "J12": [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40], + "J15": range(13, 40), + "L13": [14, 17, 36, 37, 38, 39, 40], + "N06": [17], +} + +def flip_gfp_rfp_values(csv_file_path, frames_to_flip): + logger.info(csv_file_path) + new_path = csv_file_path.replace(".csv", "-realigned.csv") + assert new_path != csv_file_path + + try: + assert not os.path.exists(new_path) + except AssertionError: + logger.warning("csv exists") + # Load the CSV file + df = pd.read_csv(csv_file_path, index_col=0) + print(df.head) + # Flip the values for specified frames + try: + for frame in frames_to_flip: + df.loc[frame, ['GFP', 'RFP']] = df.loc[frame, ['RFP', 'GFP']].values + except KeyError: + df = df.rename(columns={"SOX1": "GFP", "BRA": "RFP"}) + for frame in frames_to_flip: + df.loc[frame, ['GFP', 'RFP']] = df.loc[frame, ['RFP', 'GFP']].values + # Save the modified DataFrame back to the same CSV file + df.to_csv(new_path) + logger.info(f"saved new table to {new_path}") + + +def flip_frames(file_path, frames_to_flip): + logger.debug(file_path) + # Convert frames_to_flip from 1-based to 0-based indexing + frames_to_flip = [frame - 1 for frame in frames_to_flip] + + new_file_path = file_path.replace('aligned.tif', 'realigned.tif') + assert new_file_path != file_path + + flip_gfp_rfp_values(file_path.replace("aligned.tif", "peaks.csv"), frames_to_flip) + + try: + assert not os.path.exists(new_file_path) + except AssertionError: + logger.warning("File exists") + return new_file_path + # Load the tiff file + with tf.TiffFile(file_path) as tif: + data = tif.asarray() + logger.debug(data.shape) + + + # Check if data is TCYX and flip required frames + if data.ndim == 4: + for t in frames_to_flip: + # Flip the frame vertically and horizontally (180 degree rotation) + data[t] = np.rot90(data[t], k=2, axes=(1,2)) + else: + raise ValueError("Data does not have TCYX shape.") + + # Save the modified stack + tf.imwrite(new_file_path, np.expand_dims(data, 1), imagej=True) + logger.debug(new_file_path) + return new_file_path + + +def process(path=path, coords=coords): + out = [ + flip_frames(path.format(well=well), frames_to_flip=coords[well]) + for well in coords + ] + return out + +if __name__ == "__main__": + process() \ No newline at end of file