|
| 1 | +## This script open freeview with MRI images, MELD predictions and surfaces for quality check of segmentation |
| 2 | + |
| 3 | + |
| 4 | +## To run : python new_pt_qc_script.py -id <sub_id> |
| 5 | + |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +import subprocess as sub |
| 11 | +import bids.layout |
| 12 | +import json |
| 13 | +import glob |
| 14 | + |
| 15 | +def return_meld_T1_FLAIR(meld_dir, subject_id): |
| 16 | + subject_data={} |
| 17 | + subject_data['id'] = subject_id |
| 18 | + for modality in ['T1', 'FLAIR']: |
| 19 | + files = glob.glob(os.path.join(meld_dir, subject_id, modality, "*.nii*")) |
| 20 | + if len(files)==1: |
| 21 | + subject_data[f"{modality}_path"] = files[0] |
| 22 | + elif len(files)>1: |
| 23 | + print((f'Find too much volumes for {modality}. Check and remove the additional volumes with same key name', subject_id, 'WARNING')) |
| 24 | + return None |
| 25 | + else: |
| 26 | + subject_data[f"{modality}_path"] = None |
| 27 | + return subject_data |
| 28 | + |
| 29 | +def return_bids_T1_FLAIR(bids_dir, subject_id): |
| 30 | + subject_data={} |
| 31 | + subject_data['id'] = subject_id |
| 32 | + if 'sub-' in subject_id: |
| 33 | + subject_id = subject_id.split('sub-')[-1] |
| 34 | + print(subject_id) |
| 35 | + # get bids structure |
| 36 | + layout = bids.layout.BIDSLayout(bids_dir) |
| 37 | + print(layout) |
| 38 | + # find parameters to extract bids file |
| 39 | + config_file = os.path.join(bids_dir, 'meld_bids_config.json') |
| 40 | + with open(config_file, "r") as json_file: |
| 41 | + dict = json.load(json_file) |
| 42 | + # Create query |
| 43 | + for modality in ['T1', 'FLAIR']: |
| 44 | + query = dict[modality] |
| 45 | + query['subject'] = subject_id |
| 46 | + # Get a list of matching files |
| 47 | + files = layout.get(return_type='file', extension=['nii.gz'], **query) |
| 48 | + if len(files)==1: |
| 49 | + subject_data[f"{modality}_path"] = files[0] |
| 50 | + elif len(files)>1: |
| 51 | + print(f'Find too much volumes for {modality}. Check and remove the additional volumes with same key name', subject_id, 'WARNING') |
| 52 | + return None |
| 53 | + else: |
| 54 | + subject_data[f"{modality}_path"] = None |
| 55 | + return subject_data |
| 56 | + |
| 57 | +def get_anat_files(subject_id, meld_data_path): |
| 58 | + ''' |
| 59 | + return path of T1 and FLAIR if BIDs format or MELD format |
| 60 | + ''' |
| 61 | + input_dir = os.path.join(meld_data_path, "input") |
| 62 | + subject_data_meld = return_meld_T1_FLAIR(input_dir, subject_id) |
| 63 | + if subject_data_meld is None: |
| 64 | + return None |
| 65 | + if subject_data_meld['T1_path'] is None: |
| 66 | + subject_data_bids = return_bids_T1_FLAIR(input_dir, subject_id) |
| 67 | + if subject_data_bids is None: |
| 68 | + return None |
| 69 | + if subject_data_bids['T1_path'] is None: |
| 70 | + print(f'ERROR: Could not find any T1w nifti file. Please ensure your data are in MELD or BIDS format') |
| 71 | + return None |
| 72 | + else: |
| 73 | + subject_data = subject_data_bids |
| 74 | + else: |
| 75 | + subject_data = subject_data_meld |
| 76 | + print(f'INFO: T1 file used : {subject_data[f"T1_path"]} ') |
| 77 | + if subject_data['FLAIR_path'] is None: |
| 78 | + print(f'INFO: No FLAIR found') |
| 79 | + else: |
| 80 | + print(f'ERROR: FLAIR file used : {subject_data[f"FLAIR_path"]}') |
| 81 | + |
| 82 | + return subject_data |
| 83 | + |
| 84 | +def return_file(path, file_name): |
| 85 | + files = glob.glob(path) |
| 86 | + if len(files)>1 : |
| 87 | + print(f'ERROR: Find too much volumes for {file_name}. Check and remove the additional volumes with same key name') |
| 88 | + return None |
| 89 | + elif not files: |
| 90 | + print(f'ERROR: Could not find {file_name} volume. Check if name follow the right nomenclature') |
| 91 | + return None |
| 92 | + else: |
| 93 | + return files[0] |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + |
| 97 | + #parse commandline arguments |
| 98 | + parser = argparse.ArgumentParser(description='perform cortical parcellation using recon-all from freesurfer') |
| 99 | + parser.add_argument('-id','--id_subj', |
| 100 | + help='Subject ID.', |
| 101 | + required=True,) |
| 102 | + parser.add_argument('-meld_data','--meld_data', |
| 103 | + help='MELD data folder.', |
| 104 | + required=True,) |
| 105 | + args = parser.parse_args() |
| 106 | + subject=str(args.id_subj) |
| 107 | + meld_data_path=args.meld_data |
| 108 | + |
| 109 | + # get subject folder and fs folder |
| 110 | + subject_dir = os.path.join(meld_data_path,'input', subject) |
| 111 | + pred_dir = os.path.join(meld_data_path,'output', 'predictions_reports', subject) |
| 112 | + subject_fs_folder = os.path.join(meld_data_path, 'output', 'fs_outputs', subject) |
| 113 | + |
| 114 | + #initialise freesurfer variable environment |
| 115 | + ini_freesurfer = format("$FREESURFER_HOME/SetUpFreeSurfer.sh") |
| 116 | + |
| 117 | + # Find inputs T1 and FLAIR if exists |
| 118 | + if not os.path.isdir(subject_fs_folder): |
| 119 | + print(f'Freesurfer outputs does not exist for this subject. Unable to perform qc') |
| 120 | + else : |
| 121 | + subject_dict = get_anat_files(subject, meld_data_path) |
| 122 | + #select inputs files T1 and FLAIR |
| 123 | + T1_file = subject_dict['T1_path'] |
| 124 | + FLAIR_file = subject_dict['FLAIR_path'] |
| 125 | + #select predictions files |
| 126 | + pred_lh_file = return_file(os.path.join(pred_dir, 'predictions', 'lh.prediction.nii*'), 'lh_prediction') |
| 127 | + pred_rh_file = return_file(os.path.join(pred_dir, 'predictions', 'rh.prediction.nii*'), 'rh_prediction') |
| 128 | + |
| 129 | + #setup cortical segmentation command |
| 130 | + file_text = os.path.join(meld_data_path, 'temp1.txt') |
| 131 | + if T1_file: |
| 132 | + #create txt file with freeview commands |
| 133 | + with open(file_text, 'w') as f: |
| 134 | + f.write(f'-v {T1_file}:colormap=grayscale -layout 2 \n') |
| 135 | + if FLAIR_file: |
| 136 | + f.write(f'-v {FLAIR_file}:colormap=grayscale \n') |
| 137 | + if (pred_lh_file!=None) & (pred_rh_file!=None): |
| 138 | + f.write(f'-v {pred_lh_file}:colormap=lut \n') |
| 139 | + f.write(f'-v {pred_rh_file}:colormap=lut \n') |
| 140 | + f.write(f'-f {subject_fs_folder}/surf/lh.white:edgecolor=yellow {subject_fs_folder}/surf/lh.pial:edgecolor=red {subject_fs_folder}/surf/rh.white:edgecolor=yellow {subject_fs_folder}/surf/rh.pial:edgecolor=red \n') |
| 141 | + #launch freeview |
| 142 | + freeview = format(f"freeview -cmd {file_text}") |
| 143 | + command = ini_freesurfer + ';' + freeview |
| 144 | + print(f"INFO : Open freeview") |
| 145 | + sub.check_call(command, shell=True) |
| 146 | + os.remove(file_text) |
| 147 | + |
| 148 | + else: |
| 149 | + print('Could not find either T1 volume') |
| 150 | + pass |
| 151 | + |
| 152 | + |
| 153 | + |
0 commit comments