Pre-processing TIGER R01 data

From Wikipedia, the free encyclopedia

This article is about the writing implement. For other uses, see Pencil (disambiguation).

Overview

General subject folder structure: ~/project-tiffany/projects/TIGER-R01/raw/sub-XXX

‘Raw’ Subject Data Sub-Folders

  • EJA_SVS_SLASER_0005: Contains DICOM files for a spectroscopy scan, possibly using the Siemens SVS/SLASER sequence.
  • EP2D_BOLD_1_6MM_ISO_P2_SMS5_HCP_PA_CMRR_0014: Contains DICOM files for a BOLD (blood oxygen level dependent) fMRI scan using the Siemens HCP (Human Connectome Project) sequence.
  • EP2D_BOLD_1_6MM_ISO_P2_SMS5_HCP_PA_CMRR_SBREF_0013: Contains DICOM files for a BOLD fMRI scan using the Siemens HCP sequence, for a single-shot gradient-echo echo-planar imaging (GE-EPI) acquisition.
  • EP2D_BOLD_1_6MM_ISO_P2_SMS5_HCP_PA_CMRR_SBREF_0015: Contains DICOM files for a BOLD fMRI scan using the Siemens HCP sequence, for a single-shot GE-EPI acquisition, for the reference scan.
  • LOCALIZER_0007: Contains DICOM files for a localizer scan, which is typically a low-resolution image used for positioning subsequent scans.
  • SPINECHOFIELDMAP_AP_CMRR_0010: Contains DICOM files for a field map scan in the anterior-posterior (AP) direction, possibly for use in distortion correction of subsequent scans.
  • SPINECHOFIELDMAP_AP_CMRR_SBREF_0009: Contains DICOM files for a field map reference scan in the anterior-posterior (AP) direction.
  • SPINECHOFIELDMAP_PA_CMRR_0012: Contains DICOM files for a field map scan in the posterior-anterior (PA) direction, possibly for use in distortion correction of subsequent scans.
  • SPINECHOFIELDMAP_PA_CMRR_SBREF_0011: Contains DICOM files for a field map reference scan in the posterior-anterior (PA) direction.
  • SUB_S4-S3_1_0102: Contains DICOM files for a T1-weighted anatomical scan.
  • SVS_SLASER_DKD_RACC_WS_64AV_0006: Contains DICOM files for a spectroscopy scan, possibly using the Siemens SVS/SLASER sequence.
  • T1_MP2RAGE_SAG_P3_1MM_INV2_0003: Contains DICOM files for a T1-weighted anatomical scan, possibly using the Siemens MP2RAGE sequence.
  • _MPR_RANGE[1]__0101: Contains DICOM files for an anatomical scan in the axial plane, possibly using the Siemens MPRAGE sequence.
  • _MPR_RANGE__0100: Contains DICOM files for an anatomical scan in the axial plane, possibly using the Siemens MPRAGE sequence.

Batch DICOM to NIFTI Conversion

To begin the conversion process, follow these steps:

  1. Navigate to the directory:
    cd ~/project-tiffany/projects/TIGER_R01/
  2. Run the script:
    python batch_dcm2niix.py
  3. That’s it!

Python Script: batch_dcm2niix.py


                    import os
                    import subprocess
                    
                    # Set the root directory for the study
                    root_dir = os.getcwd()
                    
                    # Find all subject directories in the raw data folder
                    subj_dirs = [d for d in os.listdir(os.path.join(root_dir, 'raw')) if d.startswith('sub-')]
                    
                    # Loop through each subject directory
                    for subj_dir in subj_dirs:
                        subj_id = subj_dir.split('-')[1]
                        print(f"Processing subject {subj_id}...")
                        
                        func_input_dir = os.path.join(root_dir, 'raw', subj_dir)
                        func_output_dir = os.path.join(root_dir, 'bids', subj_dir, 'func')
                    
                        func_dirs = [d for d in os.listdir(func_input_dir) if 'EP2D_BOLD' in d and not d.endswith('SBREF')]
                        if not func_dirs:
                            print(f"No functional image directory found for subject {subj_id}!")
                            continue
                    
                        func_dir = os.path.join(func_input_dir, func_dirs[0])
                        struct_input_dir = os.path.join(func_input_dir, [d for d in os.listdir(func_input_dir) if 'SUB' in d][0])
                        struct_output_dir = os.path.join(root_dir, 'bids', subj_dir, 'anat')
                    
                        cmd = f"dcm2niix -b y -ba y -z y -o {func_output_dir} -f sub-{subj_id}_task-rest_bold {func_dir}"
                        subprocess.call(cmd, shell=True)
                    
                        if not os.path.exists(struct_input_dir):
                            print(f"No structural image directory found for subject {subj_id}!")
                            continue
                    
                        struct_output_file = f"sub-{subj_id}_T1w"
                        if not os.path.exists(struct_output_dir):
                            os.makedirs(struct_output_dir)
                        cmd = f"dcm2niix -b y -ba y -z y -o {struct_output_dir} -f {struct_output_file} {struct_input_dir}"
                        subprocess.call(cmd, shell=True)
                        print(f"Data conversion complete for subject {subj_id}!")
                    

Automating Script Execution with Crontab

Use crontab to schedule the batch conversion script to run daily at 4 AM PST:

0 4 * * * cd ~/project-tiffany/projects/TIGER_R01 && python batch_dcm2niix.py >> batch_dcm2niix.log 2>&1

Applying Transformations


                    import os
                    
                    # Set the root directory for the study
                    root_dir = os.getcwd()
                    
                    # Loop through each subject directory in the BIDS format
                    for subj_dir in os.listdir(os.path.join(root_dir, 'bids')):
                        subj_id = subj_dir.split('-')[1]
                        print(f"Processing subject {subj_id}...")
                    
                        anat_input_dir = os.path.join(root_dir, 'bids', subj_dir, 'anat')
                        anat_output_dir = os.path.join(root_dir, 'bids', subj_dir, 'derivatives', 'anat')
                    
                        if not os.path.exists(anat_output_dir):
                            os.makedirs(anat_output_dir)
                    
                        struct_input_file = os.path.join(anat_input_dir, f"sub-{subj_id}_T1w.nii.gz")
                        struct_output_file = os.path.join(anat_output_dir, f"sub-{subj_id}_T1w_bet.nii.gz")
                    
                        # Skull-stripping
                        cmd = f"/usr/local/fsl/bin/bet {struct_input_file} {struct_output_file}"
                        os.system(cmd)
                    
                        # Binary Mask
                        mask_input_file = struct_output_file
                        mask_output_file = os.path.join(anat_output_dir, f"sub-{subj_id}_T1w_mask.nii.gz")
                        cmd = f"/usr/local/fsl/bin/fslmaths {mask_input_file} -bin {mask_output_file}"
                        os.system(cmd)
                    
                        # Normalization
                        norm_input_file = mask_output_file
                        norm_output_file = os.path.join(anat_output_dir, f"sub-{subj_id}_T1w_normalized.nii.gz")
                        cmd = f"/usr/local/fsl/bin/fslmaths {norm_input_file} -inm 10000 {norm_output_file}"
                        os.system(cmd)
                    
                        # Segmentation
                        seg_input_file = norm_output_file
                        seg_output_file = os.path.join(anat_output_dir, f"sub-{subj_id}_T1w_seg.nii.gz")
                        cmd = f"/usr/local/fsl/bin/fast -t 1 -n 3 -H 0.1 -I 4 -l 20.0 -o {seg_output_file} {seg_input_file}"
                        os.system(cmd)
                    
                        # Binary Mask Transformation
                        mult_input_file = norm_output_file
                        mult_output_file = os.path.join(anat_output_dir, f"sub-{subj_id}_T1w_normalized_and_masked.nii.gz")
                        cmd = f"/usr/local/fsl/bin/fslmaths {norm_input_file} -mul {mask_output_file} {mult_output_file}"
                        os.system(cmd)
                    
                        print(f"Transformations complete for subject {subj_id}!")