From 3772dfd101c909578796f0f247aae1fd9c712cc8 Mon Sep 17 00:00:00 2001 From: Victor Perez Meza Date: Thu, 21 Mar 2024 11:52:36 +0100 Subject: [PATCH] move updated macsima2mc from main to dev --- macsima2mc_v1.py | 59 +++++++-- tests.py | 337 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 384 insertions(+), 12 deletions(-) create mode 100644 tests.py diff --git a/macsima2mc_v1.py b/macsima2mc_v1.py index 4743ed5..98ecd31 100644 --- a/macsima2mc_v1.py +++ b/macsima2mc_v1.py @@ -1,4 +1,5 @@ #!/usr/bin/python +import json import ome_types import numpy as np import pandas as pd @@ -61,7 +62,7 @@ parser.add_argument('-he', '--hi_exposure_only', action='store_true', - help='Activate this flag to extract only the set of images with the highest exposure time.' + help='Activate this flag to extract only the set o-of images with the highest exposure time.' ) parser.add_argument('-nb', @@ -109,7 +110,13 @@ def antigen_cycle_info(antigen_cycle_no=antigen_cycle_number,cycles_path=cycles_ antigen_cycle=f'{antigen_cycle_no:03d}' cycle_folder='_'.join([antigen_cycle,'AntigenCycle']) workdir=os.path.join(cycles_path,cycle_folder) + #filter out non tif files images=list(filter(lambda x: x.endswith('.tif'),os.listdir(workdir))) + #filter out files with tags None or Empty in the name + #images=list(filter(lambda x: "Empty" not in x,images)) + #filter out files with tags None or Empty in the name + #images=list(filter(lambda x: "None" not in x,images)) + cycle_info={'img_full_path':[], 'image':images, 'marker':[], @@ -121,6 +128,8 @@ def antigen_cycle_info(antigen_cycle_no=antigen_cycle_number,cycles_path=cycles_ 'exposure':[] } + + for im in images: marker_info=im.split('AntigenCycle')[-1].split('__') @@ -133,6 +142,11 @@ def antigen_cycle_info(antigen_cycle_no=antigen_cycle_number,cycles_path=cycles_ if ref_marker in m: cycle_info['marker'].append(ref_marker) cycle_info['filter'].append(ref_marker) + + elif "Empty" in m: + cycle_info['marker'].append(m.split('_')[0]) + cycle_info['filter'].append(m.split("bit")[0].split("_")[-2]) + else: cycle_info['marker'].append(m) #cycle_info['filter'].append(marker_info[-1].split('_')[2]) @@ -183,6 +197,9 @@ def bleach_cycle_info(antigen_cycle_no=antigen_cycle_number,cycles_path=cycles_d cycle_folder='_'.join([bleach_cycle,'BleachCycle']) workdir=os.path.join(cycles_path,cycle_folder) images=list(filter(lambda x: x.endswith('.tif'),os.listdir(workdir))) + #filter out files with tags None or Empty in the name + #images=list(filter(lambda x: "Empty" not in x,images)) + #images=list(filter(lambda x: "None" not in x,images)) cycle_info={'img_full_path':[], 'image':images, 'marker':[], @@ -297,12 +314,13 @@ def create_ome(img_info,xy_tile_positions_units,img_path): ) #--Generate channels block--# chann_block=[] - for ch in range(0,no_of_channels): + #for ch in range(0,no_of_channels): + for ch,chann_name in enumerate(markers): chann_block.append(Channel(id=ome_types.model.simple_types.ChannelID('Channel:{x}'.format(x=ch)), - color=ome_types.model.simple_types.Color((255,255,255)), - emission_wavelength=1,#place holder - excitation_wavelength=1,#place holder - + name= chann_name, + color=ome_types.model.simple_types.Color((255,255,255)), + emission_wavelength=1,#place holder + excitation_wavelength=1,#place holder ) ) #--Generate pixels block--# @@ -316,7 +334,8 @@ def create_ome(img_info,xy_tile_positions_units,img_path): template_plane_block[ch].position_x=xy_tile_positions_units[t][0] template_plane_block[ch].position_y=xy_tile_positions_units[t][1] template_plane_block[ch].exposure_time=exposures[ch] - template_chann_block[ch].id='Channel:{y}:{x}:{marker_name}'.format(x=ch,y=100+t,marker_name=mark) + template_chann_block[ch].id='Channel:{y}:{x}'.format(x=ch,y=100+t) + template_chann_block[ch].name=mark template_tiffdata_block[ch].ifd=ifd_counter ifd_counter+=1 pix_block.append(Pixels(id=ome_types.model.simple_types.PixelsID('Pixels:{x}'.format(x=t)), @@ -451,7 +470,8 @@ def create_stack(info,exp_level=1, groupA=info.loc[(info['rack']==r) & (info['well']==w)] rois=groupA['roi'].unique() - + print("remember to delete the line below line 467") + rois=[1] for roi in rois: roi_no='roi_{n}'.format(n=f'{roi:02d}') exp_level_no='exp_level_{n}'.format(n=f'{exp_level:02d}') @@ -459,8 +479,11 @@ def create_stack(info,exp_level=1, #create_dir(roi_path) #exposure_path=roi_path / 'exp_level_{n}'.format(n=f'{exp_level:02d}') #create_dir(exposure_path) - output_levels.append(roi_no) - output_levels.append(exp_level_no) + root_name=copy.deepcopy(output_levels) + root_name.append(roi_no) + root_name.append(exp_level_no) + #output_levels.append(roi_no) + #output_levels.append(exp_level_no) counter=0 @@ -484,12 +507,15 @@ def create_stack(info,exp_level=1, X=[] Y=[] + stack=np.zeros((stack_size_z,height,width),dtype=dtype_ref) exposure_per_marker=[] exp=groupB.loc[(groupB['marker']==ref_marker) & (groupB['fov']==1) & (groupB['exposure_level']=='ref'),'exposure'].tolist()[0] exposure_per_marker.append(exp) for s in markers_subset: + #print(s) + #print("test",groupB.loc[(groupB['marker']==s) & (groupB['fov']==1) & (groupB['exposure_level']==exp_level),'exposure'].tolist()) exp=groupB.loc[(groupB['marker']==s) & (groupB['fov']==1) & (groupB['exposure_level']==exp_level),'exposure'].tolist()[0] exposure_per_marker.append(exp) @@ -518,9 +544,9 @@ def create_stack(info,exp_level=1, counter+=1 if args.output_folders_list: - output_folders_path=stack_path / '--'.join(output_levels) / 'raw' + output_folders_path=stack_path / '--'.join(root_name) / 'raw' else: - output_folders_path=stack_path / Path('/'.join(output_levels)) / 'raw' + output_folders_path=stack_path / Path('/'.join(root_name)) / 'raw' if os.path.exists(output_folders_path): @@ -579,6 +605,10 @@ def main(): antigen_info=antigen_cycle_info(antigen_cycle_no=i) exp=antigen_info['exposure_level'].unique() exp=exp[exp!='ref'] + + if len(exp)==0: + exp=[1] + exp.sort() if args.hi_exposure_only: @@ -625,6 +655,11 @@ def main(): else: out_ant['background'].extend(antigen_stack_info['no_channels']*['']) + #with open(Path("D:/out_ant.json"), "w") as fp: + # json.dump(out_ant , fp) + + #with open(Path("D:/out_ble.json"), "w") as fp: + # json.dump(out_ble , fp) for e in exp: if extract_bleach: diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..c9750ee --- /dev/null +++ b/tests.py @@ -0,0 +1,337 @@ +import os +from pathlib import Path +from operator import itemgetter + +cycles_dir=Path('C:/Users/vp232003/repos/test_data/macsima_v2') + +def pull_all_cycles( cycles_path=cycles_dir): + folders=list(filter(lambda x: 'Cycle' in x ,os.listdir( cycles_path ))) + aux_list={ int(f.split('Cycle_')[-1]):f for f in folders } + folders=dict( sorted(aux_list.items(),reverse=False) ) + #aux_list=sorted(aux_list,key=itemgetter(1),reverse=False) + return folders + +cycles_list=pull_all_cycles(cycles_dir) + +def cycle_info(cycle_no=1,cycles_path=cycles_dir ,cycles=cycles_list,ref_marker='DAPI'): + + workdir=cycles_path / cycles[cycle_no] + images=list(filter(lambda x: x.endswith('.tif'),os.listdir(workdir))) + signal_images=list( filter(lambda x: '_S_' in x ,images) ) + bleach_images=list( filter(lambda x: '_B_' in x ,images) ) + print(signal_images) + print(bleach_images) + + + cycle_info={'img_full_path':[], + 'image':images, + 'marker':[], + 'filter':[], + 'rack':[], + 'well':[], + 'roi':[], + 'fov':[], + 'exposure':[] + } + +#cycle_info() + +name='C-001_S-000_S_DAPI_R-01_W-A01_ROI-01_A-DAPI.tif' +full_name=name.replace('_S_','_B_') +print(name.replace('_S_','_B_')) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +