Skip to content

ThomasYeoLab/Kong2026_TMSTree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tree-based MS-HBM targeting for TMS

References


Background

Functional connectivity (FC) is used to identify personalized targets for transcranial magnetic stimulation (TMS). However, existing methods often overlook individual differences in whole-cortex network organization and were not explicitly designed to identify TMS targets near the scalp. We use the multi-session hierarchical Bayesian model (MS-HBM) to estimate high-quality individual-specific networks. A tree-based algorithm is then used to select the optimal target location. By essentially having no parameter to tune, our framework might improve generalizability across populations. We compare our approach to existing FC-based algorithms for TMS target selection in terms of scalp proximity, test-retest reliability, and FC to brain regions implicated in depression. The tree-based MS-HBM algorithm provides a robust, generalizable framework to estimate near-scalp personalized targets across different populations.

TreeFig

License

All code in this repository is governed by the RESEARCH AND DEVELOPMENT SOURCE CODE LICENSE: usage is restricted to research purposes, and redistribution or commercial use is prohibited. For more details, please refer to the LICENSE file in the root directory.

Prepare the environment and input data

export B1N_LIB_DIR=/path/to/TMS_scripts_private
  • Please set an environment variable CBIG_CODE_DIR point to the CBIG repository root directory, e.g.,:
export CBIG_CODE_DIR=/path/to/CBIG
  • This code requires the user preprocessed their fMRI and T1 images before running the scripts. We recommend using the CBIG preprocessing pipeline. However the code should work with other preprocessing pipelines as long as the global signal regression has been performed on the fMRI data. If the user is using the CBIG preprocessing pipeline, make sure you include the global signal regression, projection to fsaverage, and projection to MNI in your CBIG preprocessing config file:
CBIG_preproc_regress -whole_brain -wm -csf -motion12_itamar -detrend_method detrend -per_run -censor -polynomial_fit 1
CBIG_preproc_native2fsaverage -proj fsaverage6 -sm 6 -down fsaverage5
CBIG_preproc_native2mni_ants -final_mask ${FSL_DIR}/data/standard/MNI152_T1_2mm_brain_mask_dil.nii.gz -sm 4
  • This code requires the preprocessed data to be in native volumetric space, as well as the FreeSurfer fsaverage6 surface space (can be done via mri_vol2surf from FreeSurfer). The current code does not support using preprocessed in MNI space to derive the TMS targets, as it dismisses individual differences in cortical geometry which is important for TMS targeting.

  • This code requires the FreeSurfer recon-all to be run on the T1 images.

Code Release

The code utilized in this work is organized into two main directories: preprocessing and targeting.

Preprocessing

This directory contains additional preprocessing scripts for data. This assumes that the data has already been preprocessed. There are two additional preprocessing steps required before running the targeting scripts:

  • B1N_preproc_func2T1.sh: prepare gray matter mask and loose brain mask in individual functional image space.

  • B1N_preproc_BETsurf.sh: run BET to generate scalp mesh. Note that we will only use scalp surface for small DLPFC area so even if BET fails for some other poor SNR areas, it should be fine.

Targeting

  • B1N_tms_parcellation.m: generate individual-specific parcellation using MS-HBM.

  • B1N_tms_generate_FC.m: generate functional connectivity maps for targeting.

  • B1N_tms_FC_target.m: identify TMS targets based on FC maps and scalp proximity.

Usage

Here are examples of how to prepare the paths and output directories for running the scripts:

Prepare the paths

### Prepare the input data paths
# Assume RECON_ALL and PREPROC_REST have been done using CBIG preprocessing pipeline
# RECON_ALL_DIR: the directory where the recon-all output is stored
RECON_ALL_DIR=/Preproc/RECON_ALL/${subname}/${subname}_${t}
# PREPROC_REST_DIR: the directory where the preprocessed resting state data is stored
PREPROC_REST_DIR=/Preproc/PREPROC_REST/${subname}/${subname}_${t}

### Prepare the output directories
# MASK_DIR: the output directory where the gray matter masks and loose masks will be stored 
MASK_DIR=/Preproc/REST_masks/${subname}/${subname}_${t}
# BETsurf_DIR: the output directory where the skull mesh output will be stored
BETsurf_DIR=/Preproc/BETsurf/${subname}/${subname}_${t}
# TMS_out_dir: the output directory where all the TMS targeting results will be stored
TMS_out_dir=/path/TMS_results
# ROI_out_dir: the output directory where the ROIs will be stored
ROI_out_dir=${TMS_out_dir}/${subname}_${t}/T1_data/ROIs
# FC_out_dir: the output directory where the functional connectivity maps will be stored
FC_out_dir=${TMS_out_dir}/${subname}_${t}/T1_data/groupFC
# MS-HBM parcellation output directory
MSHBM_out_dir=${TMS_out_dir}/${subname}_${t}/MSHBM_parcellation
gMSHBM_out_dir="" #please leave it empty

Run additional preprocessing

  • Perform additional smoothing and generate gray matter and loose brain masks
PREPROC_REST_DIR=/path/to/preprocessed/resting/state/data  # path to preprocessed resting state data directory
RECON_ALL_DIR=/path/to/recon/all/directory  # path to FreeSurfer recon-all directory
MASK_DIR=/path/to/brain/mask/directory  # output directory where the masks will be stored
sm=4  # smoothing parameter
nrun=001 # run number, if you have multiple runs, you can loop through this
FD=0.2  # framewise displacement threshold
DVARS=50  # DVARS threshold
B1N_preproc_func2T1.sh -p $PREPROC_REST_DIR -r $RECON_ALL_DIR -m $MASK_DIR -u $nrun -h $sm --reduce-mem -f $FD -d $DVARS"
  • BET skull mesh extraction
RECON_ALL_DIR=/path/to/recon/all/directory  # path to FreeSurfer recon-all directory
BETsurf_DIR=/path/to/output/BET/surf/directory  # output directory where the BET skull mesh will be stored
BETparam=0.5  # BET parameter, you can adjust this value based on the quality of the skull mesh, default is 0.5. If the scalp surface is too tight, decrease this value. If the scalp surface is too loose, increase this value.
B1N_preproc_BETsurf.sh -r $RECON_ALL_DIR -b $BETsurf_DIR -a $BETparam"

Perform TMS targeting

  • Prepare group-level ROIs and SGC FC map in T1 space
# MNI ROIs (This is precomputed)
DLPFC_MNI2mm=${B1N_LIB_DIR}/lib_data/group_data/MNI_ROIs/DLPFCsphere_bin.nii.gz
SGC_MNI2mm=${B1N_LIB_DIR}/lib_data/group_data/MNI_ROIs/SGCsphere_bin.nii.gz

# HCP SGC FC map
SGC_FC_map=${B1N_LIB_DIR}/lib_data/group_data/SGC_FC_group/HCP_SGC_seed_map_group_average_mean.nii.gz

CBIG_TMS_project_ROIs_and_group_SGC_FC_to_T1.sh \
    -recon-all ${RECON_ALL_DIR} \
    -preproc-rest ${PREPROC_REST_DIR} -mask ${MASK_DIR} \
    -roi-out ${ROI_out_dir} -fc-out ${FC_out_dir} \
    -DLPFC-MNI2mm ${DLPFC_MNI2mm} -SGC-MNI2mm ${SGC_MNI2mm} -SGC-FC-map ${SGC_FC_map}"
  • Generate individual-specific parcellation
B1N_tms_parcellation(${PREPROC_REST_DIR}, ${RECON_ALL_DIR}, ${MSHBM_out_dir}, ${gMSHBM_out_dir}, ${TMS_out_dir}, ${FD}, ${DVARS})
  • Generate functional connectivity maps for targeting
% Note that the last argument '1' indicates to use the reduce memory option
B1N_tms_generate_FC(${PREPROC_REST_DIR}, ${ROI_out_dir}, ${FC_out_dir}, ${TMS_out_dir}, ${nrun}, ${FD}, ${DVARS},'1')
  • Identify TMS targets based on FC maps and scalp proximity
% Note that the last argument '1' indicates to use the reduce memory option
B1N_tms_FC_target(${PREPROC_REST_DIR}, ${MASK_DIR}, ${BETsurf_DIR}, ${ROI_out_dir}, ${TMS_out_dir}, '1')

Output

Target results will be saved in ${TMS_out_dir}/target_results/group_weight/MSHBM_scalp_target_results_LITE/MSHBM_target_results.mat. If you load this mat file, the final target should be the Opt.cenRAS(1,:). The rest coordinates Opt.cenRAS(2:end,:) are other candidate targets.

Updates

  • Release v0.0.1 (13/01/2026): Initial release of the Tree-based MS-HBM TMS targeting code.

Bugs and Questions

Please contact Ru(by) Kong at roo.cone@gmail.com.

About

Tree-based MS-HBM TMS targeting algorithm

Resources

License

Stars

Watchers

Forks

Packages

No packages published