marcnol / pyHiM

Multiplexed DNA-FISH data analysis pipeline
GNU Lesser General Public License v3.0
4 stars 2 forks source link

Bug (`trace_filter.py`) - dist_max is an input argument but it's never used #245

Open XDevos opened 6 months ago

XDevos commented 6 months ago

It should be used to set a maximum distance threshold between the two most distant barcodes

MarionBardou commented 6 months ago

Solution found to remove barcodes that give rise to values higher than the expected threshold :

import numpy as np 

path = "/mnt/grey/DATA/AnalyzedData_2023/Experiment_16_Sandrine_Marion_pupa_WT_Mutant/Pooled_WT_Mutant_Exp16_2023_Exp58_2022/WT/Merged_3bc_min/All_labeled/Reduced_1dot5um_threshold/"
matrix = np.load(path+"Trace_combined_3D_method:mask1_label:M_action:all_3bc_RT_removed_47_30_11_12_13_under_1-5um_post_filt_min_3bc_Matrix_PWDscMatrix.npy")

# Assuming 'matrix' is a 3D NumPy array with shape (20, 20, 51786)

# Create empty lists to store matrices
threshold = 1.5
above_1_5_preserved = []
above_1_5_replaced_by_nan = []
under_1_5 =[]
matrix_filt =[]

# Iterate over each matrix
for i in range(matrix.shape[2]):  # Loop over the third dimension
    sub_matrix = matrix[:, :, i]  # Get the 20x20 matrix at index i

    # Check if any value in the sub-matrix exceeds 1.5
    if np.any(sub_matrix > threshold):
        # Store a copy of the original sub-matrix
        original_sub_matrix = np.copy(sub_matrix)

        # Replace values above 1.5 with NaNs
        original_sub_matrix[sub_matrix > threshold] = np.nan

        # Append a copy of the original sub-matrix to the list
        above_1_5_preserved.append(np.copy(sub_matrix))

        # Append the modified sub-matrix (with NaNs) to the list
        above_1_5_replaced_by_nan.append(original_sub_matrix)
        matrix_filt.append(original_sub_matrix)
    else:
        # If no value exceeds 1.5, append a copy of the sub-matrix to the list without modifications
        under_1_5.append(np.copy(sub_matrix))
        matrix_filt.append(sub_matrix)

# Convert the lists to numpy arrays

above_1_5_preserved = np.array(above_1_5_preserved)
above_1_5_preserved = np.transpose(above_1_5_preserved, (1, 2, 0))
above_1_5_replaced_by_nan = np.array(above_1_5_replaced_by_nan)
above_1_5_replaced_by_nan = np.transpose(above_1_5_replaced_by_nan, (1, 2, 0))
under_1_5 = np.array(under_1_5)
under_1_5 = np.transpose(under_1_5, (1, 2, 0))
matrix_filt = np.array(matrix_filt)
matrix_filt = np.transpose(matrix_filt, (1, 2, 0))