---
# System prepended metadata

title: Tractify New Freesurfer Version (2023)

---


# Tractify New Freesurfer Version (2023)

The new tractify registration workflow was adapted from https://surfer.nmr.mgh.harvard.edu/fswiki/FsAnat-to-NativeAnat.

The main difference is we now use the brain.mgz from freesurfer as our anatomical image instead of just the T1 from before. The only change you should need to do for your commands are to add --fs-brain to point to the brain.mgz for the specific participant's freesurfer brain.mgz file (found in the `mri` folder in freesurfer.

Here's an example of a run I did that you can adapt for each participant (I copied this participant's data to my local folders for ease of use).
```
tractify \
/scratch/smansour/UKB_2023/sub-1004160/T1_unbiased_brain.nii.gz \
/scratch/smansour/UKB_2023/sub-1004160/data_ud.nii.gz \
/scratch/smansour/UKB_2023/sub-1004160/data.eddy_rotated_bvecs \
/scratch/smansour/UKB_2023/sub-1004160/bvals \
/scratch/smansour/UKB_2023/template/MNI152_T1_2mm_brain.nii.gz \
/scratch/smansour/UKB_2023/Schaefer2018_200Parcels_7Networks_order_FSLMNI152_2mm.nii.gz \
/scratch/smansour/UKB_2023/tractify_output_align_22/ \
--participant-label=1004160 
--session-label=01 \
--gen5tt-algo=freesurfer \
--fs-file=/scratch/smansour/UKB/sub-1004160_2/mri/aseg.mgz  \
--fs-brain=/scratch/smansour/UKB/sub-1004160_2/mri/brain.mgz \
--num-tracts=50000
```

## Old Tractify Commands

https://hackmd.io/@slimnsour/HJRGubCFq

## Old Tractify Images
https://docs.google.com/document/d/1zNJrpEf_nS4XO4m95AdMDUTAjAv0OEgm2rMs8uepUqY/edit#

## New Tractify Bash Script Commands (Updated March 2023)
```
Atlas: Schaefer2018_200Parcels_7Networks_order_FSLMNI152_2mm_flirt.nii.gz
Template: MNI152_T1_2mm_brain.nii.gz
```

## Extracting SSE from b1000s
#### Extract b0s and b1000s from dwi
`dwiextract --export_grad_fsl dwi_b1000.new_bvecs dwi_b1000.new_bval -fslgrad <bvecs> <bval> -shell 0.0,1000.0 <dwi.nii.gz> dwi_b1000.nii.gz`

#### Skullstrip the b1000s using bet
`bet dwi_b1000.nii.gz dwi_b1000_brain.nii.gz -f 0.50 -m -R`

#### Switch over to bet images

#### Get sse from eddy b1000s using dtifit
`dtifit -k dwi_b1000.nii.gz -o dtifit_ -m dwi_b1000_brain.nii.gz -r dwi_b1000.new_bvecs -b dwi_b1000.new_bval --save_tensor --sse`


## B0s
#### Extract the b0s from the dwi
`dwiextract -bzero --export_grad_fsl dwi_b0.new_bvecs dwi_b0.new_bval -fslgrad <bvecs> <bval> <dwi.nii.gz> dwi_b0.nii.gz`

#### Average out the b0s
`mrmath -axis 3 dwi_b0.nii.gz mean dwi_b0_avg.nii.gz`

#### Skullstrip the average b0s using bet
`bet dwi_b0_avg.nii.gz dwi_b0_avg_brain.nii.gz -f 0.50 -m -R`



## Registering atlas to diffusion space
### Convert the freesurfer brain.mgz to nii.gz
`mrconvert <brain.mgz> brain.nii.gz`

#### Register the T1 to diffusion space (t1_flirt)
`flirt -in brain.nii.gz -ref dwi_b0_avg_brain.nii.gz -out T1_to_dwi.nii.gz -omat T1_to_dwi.mat -dof 6`

#### Register the T1 to the template (pre_atlas_flirt)
`flirt -in brain.nii.gz -ref <template> -out T1_to_template.nii.gz -omat T1_to_template.mat`
#### Suggestion: should be non linear to adjust for different head shapes (fnirt/fmriprep or sdcflows workflow)
#### And use applyxfm instead to apply a combination of linear and nonlinear transforms

#### Invert the matrix to get template->T1 matrix
`convert_xfm -omat template_to_T1.mat -inverse T1_to_template.mat`

#### Concatenate the template->T1 and T1->diffusion to get template->diffusion matrix
`convert_xfm -omat template_to_dwi.mat -concat T1_to_dwi.mat template_to_T1.mat`

#### Use the MNI->diffusion matrix to register the atlas to diffusion space (atlas flirt)
`flirt -in <atlas.nii.gz> -ref T1_to_dwi.nii.gz -out atlas_to_dwi.nii.gz -omat atlas_to_dwi.mat -applyxfm -init template_to_dwi.mat -interp nearestneighbour`
#### Use non linear transform (applyxfm not flirt)

## Gray white matter interface

#### Convert the aseg to nii.gz using mrconvert
`mrconvert aseg.mgz aseg.nii.gz`

#### Register the aseg to the diffusion data before extracting the gray white matter interface using nearestneighbors
`flirt -in aseg.nii.gz -ref T1_to_dwi.nii.gz -out aseg_to_diff.nii.gz -omat aseg_to_diff.mat -applyxfm -init T1_to_dwi.mat -interp nearestneighbour`



#### Generate the five-tissue-type image (5TT) using registered aseg in diffusion space
`5ttgen freesurfer -nocrop aseg_to_diff.nii.gz 5TT.nii.gz`

#### Convert the 5TT to gray white matter interface to seed streamlines from
`5tt2gmwmi 5TT.nii.gz gmwmi.mif`


## Getting tracts and filtering
#### Generate response function using the multishell 5tt algorithm
`dwi2response msmt_5tt -fslgrad <bvecs> <bval> -mask dwi_b0_avg_brain_mask.nii.gz dwi.nii.gz 5TT.nii.gz wm.txt gm.txt csf.txt`

#### Estimate the fibre orientation distributions (FOD) from the dwi
`dwi2fod msmt_csd dwi.nii.gz wm.txt -fslgrad <bvecs> <bval> -mask dwi_b0_avg_brain_mask.nii.gz FOD.mif gm.txt gm.mif csf.txt csf.mif`

#### Generate tracts from gray white matter interface
`tckgen -act 5TT_to_diff.mif -algorithm iFOD2 -samples 4 -output_seeds out_seeds.nii.gz -seed_gmwmi gmwmi.mif -select 1000000 FOD.mif tracked.tck`

#### Filter the tracts to match the FOD lobe integrals
`tcksift2 tracked.tck FOD.mif prob_weights.txt`


## Generating connectivity matrices and outputs
#### Generate the connectivity matrix scaled by length
`tck2connectome -tck_weights_in prob_weights.txt -keep_unassigned -scale_invnodevol -scale_length -assignment_radial_search 4.000000 -symmetric -zero_diagonal tracked.tck atlas_to_dwi.nii.gz conmat_length_invnodevol.csv`


## Other outputs
#### Convert the 5TT for output
`mrconvert 5TT.mif 5TT.nii.gz`

#### Convert the gray white matter interface for output
`mrconvert gmwmi.mif gmwmi.nii.gz`