56. Functional MRI‐guided TMS - fahsuanlin/labmanual GitHub Wiki

Procedures of using resting-state fMRI to guide TMS.

Tested with s012 of the EEG-MRI data (eegmri_memory/s012) on July 7 2025.

Prepare functional connectivity analysis

Structural MRI with FreeSurfer reconstruction

Consult the procedure here

Functional MRI preprocessing

  • Procedures to unpack dicom, build analysis template, motion correction, slice timing correction, and spatial smoothing

  • Procedures to register between fMRI and structural MRI.

  • Procedures to morph EPI to cortical surface in native and MNI (fsaverage) space.

Perform seed-based functional connectivity analysis

Define seed region with FreeSurfer annotations

  • Get seed time series from a segmented brain region. This script takes the hippocampus as the example. The hippocampus is automatically segmented by FreeSurfer in .aseg file.
The following part should be updated correctly:
root_path='/Users/fhlin/workspace/eegmri_memory';

file_hippo_aseg={
    'aseg.mgz';
    }; %freesurfer default; no change if you are using freesurfer .aseg files

setenv('SUBJECTS_DIR','/Users/fhlin/workspace/eegmri_memory/subjects');

%fMRI file stem for seed time series.
fstem={
    'fmcprstc';
    };

%fMRI runs; need to be updated based on your data.
fmri_vol_data={
    '005';
    };

%output Matlab file for the seed time series
output_fstem={
    'native_hippo_regressors_rest';
    }

%subject; need to be updated based on your data.
subject={
    's012';
    };
This part defines the brain region based on FreeSurfer segmentation codes.
                    region_index={
                        [17]; %left hippo
                        [53]; %right hippo
                        %[203 233 235 237 239 241 243 245 211]; %head
                        %[234 236 238 240 242 244 246 212]; %body
                        %[226]; %tail
                        };

                    region_stem={
                        'hippo_left';
                        'hippo_right';
                        %'hippo_head';
                        %'hippo_body';
                        %'hippo_tail';
                        };
  • Get seed time series from a cortical brain region. This script takes the entorhinal cortex as the example. The entorhinal cortex is automatically parcellated by FreeSurfer in .label file.

Prepare confounds

Here we take the time course of the average signals at the ventricles and white matter as confounds. We also take the time series of 6 parameters in motion correction as confounds.

  • Motion confounds: This script collect the motion regressors.
This part should be updated.
bold_dir='/Users/fhlin/workspace/eegmri_memory/s012/resting_data/unpack';


dirs={
'bold/005';
};

  • Ventricle and white matter confounds: This script collects the time series of ventricles and the white matter average.
This part should be updated.
target_subject='s012';
file_register_source={
    '../resting_data/unpack/bold/005/fmcprstc.nii.gz';
    };

file_regression_source={
    '../resting_data/unpack/bold/005/fmcprstc.nii.gz';
    };

file_register={
    './register.dat';
    };

file_aseg={
        'aparc+aseg_fmcprstc_005.nii';
};

file_output={
    'regressor_wm_ventrical_005.mat';
    };

Calculate the fMRI seed-based connectivity.

This script calculates the functional connectivity to a cortical region. We use the entorhinal cortex as the example.

This part should be updated.
data_path={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/unpack/bold/005';
    };

reg_file={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/regressor_wm_ventrical_005.mat';
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/regressor_wm_ventrical_005.mat';
    };

mc_file={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_analysis/mc_regressor_005.mat';
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_analysis/mc_regressor_005.mat';
    };

fstem={
    'sfmcprstc';
    };

file_register='./register.dat';

file_aseg={
    'native_ent_regressors_rest.mat';
    };

var_aseg={
    'regressor_entorhinal';
    };

output_aseg_stem={
    'entorhinal';
    };


output_stem={
    'entorhinal_fconn_native_vol_aseg_060525';
    };

TR=2.0; %second

n_dummy=5;
flag_gavg=1;

confound_polynomial_order=1;
confound_period=[];


subject={
    's012';
    };

This script calculates the functional connectivity to a brain region. We use the hippocampus as the example.

This part should be updated.
data_path={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/unpack/bold/005';
    };

reg_file={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/regressor_wm_ventrical_005.mat';
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_data/regressor_wm_ventrical_005.mat';
    };

mc_file={
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_analysis/mc_regressor_005.mat';
    '/Users/fhlin/workspace/eegmri_memory/s012/resting_analysis/mc_regressor_005.mat';
    };

fstem={
    'sfmcprstc';
    };

file_register='./register.dat';

file_aseg={
    'native_hippo_regressors_rest_hippo_left.mat';
    'native_hippo_regressors_rest_hippo_right.mat';
    };

var_aseg={
    'regressor_hippo';
    'regressor_hippo';
    };

output_aseg_stem={
    'hippo_left';
    'hippo_right';
    };


output_stem={
    'hippo_fconn_native_vol_aseg_060525';
    };

TR=2.0; %second

n_dummy=5;
flag_gavg=1;

confound_polynomial_order=1;
confound_period=[];


subject={
    's012';
    };

Sometimes we would like to estimate the dynamic functional connectivity, which are connectivity estimated over a moving temporal window. Here are scripts of dynamic functional connectivity to a cortical seed and to a brain region. We can use these dynamic functional connectivity estimates to calculate the statistically significant differences between them using this script.

Incorporate fMRI connectivity for TMS

Results of functional connectivity analysis in .nii format in the native space can be imported to guide TMS. Here is the snapshot of this process using Brainsight.

Estimate the TMS-induced electric field

The TMS-induced electric field around a pre-defined target can be calculated to estimate the outcome of neuromodulation.

Select TMS target coordinates

Select a TMS target coordinate in MNI coordinate.

Calculate TMS-induced electric fields

Using BEM-FMM, the induced electric field with the TMS coil position and orientation targeting a pre-defined coordinate can be calculated.

  • This script calculate the e-field induced by a TMS coil (MRi-B91) targeting the specified coordinate. Enable the flag_nav flag to 1 if you want to show the GUI of e-field calculation. Setting flag_nav=0 will not show any GUI.

This script also calculates e-field by rotating the TMS coil in 15-degree steps (clockwise).

NOTE: Here we used the following codes to rotate TMS coil. This keeps the origin and orientation as that before the rotation.

    if(flag_nav)
        [tms_coil_xfm_moved_tuned]=etc_tms_target_xfm_tune(target_coord, bem_obj(head_surf_idx), tms_coil_xfm_moved(1:3,4).*1e3, -tms_coil_xfm_moved(1:3,3), tms_coil_xfm_moved(1:3,2), tms_coil_xfm_moved, 4, rotation_deg(rot_idx),'app',etc_render_fsbrain.app_tms_nav);
    else
        [tms_coil_xfm_moved_tuned]=etc_tms_target_xfm_tune(target_coord, bem_obj(head_surf_idx), tms_coil_xfm_moved(1:3,4).*1e3, -tms_coil_xfm_moved(1:3,3), tms_coil_xfm_moved(1:3,2), tms_coil_xfm_moved, 4, rotation_deg(rot_idx));
    end;
These parts should be updated.

Basic setup includes subject ID and subjects directory. Also update the output file (Matlab MAT) file stem.

The target coordinate targeg_coord_mni should be in MNI coordinate system*

flag_nav is optional to enable/disable GUI.


subjects_dir='/space_lin2/fhlin/eegmri_memory/subjects';

root_dir='/space_lin2/fhlin/eegmri_memory/s012/tms_e_field';

subject={'s012'};

output_stem='mri_b91_mpfc_fc1_bh_proj_efield';

targeg_coord_mni=[-15 58 38]; %fc target

flag_nav=1; %open navigation window

To calculate the e-field of different rotations, edit the following at the later part of the code. Here we calculate the rotation from 0 to 360 degree in steps of 15 degree. An additional efield_rot including the electric fields for different orientations will be appended in the result MAT file.

rotation_deg=[0:15:360-1];

NOTE The target coordinate is in the MNI coordinate system

Results will be two .nii images: one is the e-field of targeting the specified coordinate in the TMS coil orientation withe coil handle upward to the vertex. The other file include 24 e-field estimates, each of which has the TMS coil rotating 15 degree clockwise.

Export electric fields to guide TMS

Results of the electric field calculation can be exported in .nii format in the native space using this script. This can be imported to Brainsight to guide the TMS coil position and orientation.

This part should be updated.
subject='s012'; %subject ID
hemi={'lh','rh'};

file_efield={
    'mri_b91_mpfc_fc1_bh_proj_efield.mat';
    }; % a list of mat files for the calculated e-field
⚠️ **GitHub.com Fallback** ⚠️