PennLINC / qsiprep

Preprocessing of diffusion MRI
http://qsiprep.readthedocs.io
BSD 3-Clause "New" or "Revised" License
138 stars 55 forks source link

How to transform lesion_mask to MNI space? #532

Open 276562578 opened 1 year ago

276562578 commented 1 year ago

Hi, according to the documentation, the sub-001_T1w_label-lesion_roi.nii.gz has been put in the bids folder then run the qsiprerp. But I wonder how to get the lesion mask of MNI space? The output *xfm.h5 looks like a transform matrix file from *prepro_T1w to *space_MNI_prepro_T1w. It's very appreciated if anyone can tell me the proccess how to transfor a native space lesion_mask file to MNI space!

hfxcarl commented 1 year ago

Something like this should work (bash-code example):

#!/bin/bash
export ANTSPATH=/opt/ants-2.4.2/bin
export PATH="$ANTSPATH:$PATH"
PROJECT_DIR=/project/BIDS_WORK
S="sub-001"
les_img=$PROJECT_DIR/BIDS_defaced/$S/anat/${S}_T1w_label-lesion_roi.nii.gz
QSI_DIR=$PROJECT_DIR/derivs/qsiprep/$S/anat
ref_img=$QSI_DIR/${S}_space-MNI152NLin2009cAsym_desc-preproc_T1w.nii.gz
xfm_file=$QSI_DIR/${S}_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5
out_img=$QSI_DIR/${S}_space-MNI152NLin2009cAsym_label-lesion_roi.nii.gz
${ANTSPATH}/antsApplyTransforms \
    --dimensionality 3 \
    --interpolation NearestNeighbor \
    --reference-image ${ref_img} \
    --input ${les_img} \
    --output ${out_img} \
    --transform ${xfm_file} \
    --verbose 1
mattcieslak commented 1 year ago

Thank you @hfxcarl this is correct in most cases. If your original T1w had an oblique acquisition then the obliquity is removed before any processing happens (this avoids a bug in ITK). I'm not sure how best to implement this step in the derivatives though

276562578 commented 1 year ago

I have tried the scripte @hfxcarl mentioned above, the final lesion file not fit the space-MNI152NLin2009cAsym_desc-preproc_T1w.nii.gz.

Actually,

  1. the original lesion file match the original T1w file before qsiprep process
  2. the original lesion file does NOT match the desc-preproc_T1w.nii.gz after qsiprep process

I believe the qsiprep maybe do somethings in processing from original T1w to desc-preproc_T1w.nii.gz, which makes the transform failed

mattcieslak commented 1 year ago

Do you still have the working directory from when you ran qsiprep? The T1w is transformed to T1wACPC space through a rigid transform. It is a soon-to-be-fixed bug that the T1wNative to T1wACPC transform isn't saved in the derivatives.

If you don't have the derivatives you can rigid register your native T1w to the T1wACPC, apply that transform to your lesion mask. Then you can use the h5 transform to mni to get your lesion in mni space

hfxcarl commented 1 year ago

@mattcieslak Interesting! I tested my script on a couple of datasets and you are indeed correct, the registrations are missing the T1wNative-to-T1wACPC rigid-body transform. I had originally only tested it on a single dataset that was, unfortunately, very closely aligned to acpc-centered space so I didn't even notice the incorrect aligned outputs. Here is a more complete script for @276562578 .

#!/bin/bash

## setup ANTs software
export ANTSPATH=/opt/ants-2.4.2/bin
export PATH="$ANTSPATH:$PATH"
export ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=4   ## multi-threading across 4-cores

## define project variables
PROJECT_DIR=/project/BIDS_WORK
S="sub-CB001A"
raw_t1=$PROJECT_DIR/BIDS_defaced/$S/anat/${S}_T1w.nii.gz
raw_mask=$PROJECT_DIR/BIDS_defaced/$S/anat/${S}_T1w_label-lesion_roi.nii.gz
QSI_DIR=$PROJECT_DIR/derivatives/qsiprep/$S/anat
acpc_ref_img=$QSI_DIR/${S}_desc-preproc_T1w.nii.gz
acpc_out_prefix=$QSI_DIR/${S}_desc-preproc_T1wNativeToACPC  ## prefix for both out-img and out-xfm
mni_ref_img=$QSI_DIR/${S}_space-MNI152NLin2009cAsym_desc-preproc_T1w.nii.gz
mni_xfm=$QSI_DIR/${S}_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5
mni_out_t1=$QSI_DIR/${S}_space-MNI152NLin2009cAsym_desc-T1wNative.nii.gz
mni_out_mask=$QSI_DIR/${S}_space-MNI152NLin2009cAsym_label-lesion_roi.nii.gz

## 1) rigid-body-registration of T1wNative to T1w-ACPC
${ANTSPATH}/antsRegistrationSyNQuick.sh -d 3 -t r -f ${acpc_ref_img} -m ${raw_t1} -o ${acpc_out_prefix}

## 2) apply rigid-body + h5(affine+warp) to register T1wNative image to MNI152-space.
${ANTSPATH}/antsApplyTransforms \
    --dimensionality 3 \
    --interpolation Linear \
    --reference-image ${mni_ref_img} \
    --input ${raw_t1} \
    --output ${mni_out_t1} \
    --transform ${mni_xfm} \
    --transform "${acpc_out_prefix}0GenericAffine.mat" \
    --verbose 1

## 3) apply rigid-body + h5(affine+warp) to register mask from T1wNative-space to MNI152-space.
${ANTSPATH}/antsApplyTransforms \
    --dimensionality 3 \
    --interpolation NearestNeighbor \
    --reference-image ${mni_ref_img} \
    --input ${raw_mask} \
    --output ${mni_out_mask} \
    --transform ${mni_xfm} \
    --transform "${acpc_out_prefix}0GenericAffine.mat" \
    --verbose 1
276562578 commented 1 year ago

@hfxcarl It works! Very appreciated! Of course, a more natural way to do this might be to use the intermediate files generated by qsiprep 4F027302

276562578 commented 1 year ago

Do you still have the working directory from when you ran qsiprep? The T1w is transformed to T1wACPC space through a rigid transform. It is a soon-to-be-fixed bug that the T1wNative to T1wACPC transform isn't saved in the derivatives.

If you don't have the derivatives you can rigid register your native T1w to the T1wACPC, apply that transform to your lesion mask. Then you can use the h5 transform to mni to get your lesion in mni space

@mattcieslak I still have the intermediate files of qsiprep, but I tried some of these deformation matrices without success

file structure:

workdir/
├── 20221211-090831_6f38d904-43d3-4895-b729-7bad7c1f782c
│   └── bids_db
│       └── layout_index.sqlite
├── qsiprep_wf
│   ├── d3.js
│   ├── graph1.json
│   ├── graph.json
│   ├── index.html
│   └── single_subject_L001_wf
│       ├── about
│       ├── anat_preproc_wf
│       ├── bids_info
│       ├── bidssrc
│       ├── ds_report_about
│       ├── ds_report_summary
│       ├── dwi_finalize_ses_1_wf
│       ├── dwi_finalize_ses_2_wf
│       ├── dwi_finalize_ses_3_wf
│       ├── dwi_preproc_ses_1_wf
│       ├── dwi_preproc_ses_2_wf
│       ├── dwi_preproc_ses_3_wf
│       └── summary
└── reportlets
    └── qsiprep
        └── sub-L001