ANTsR Notes: 7/28/15: Ants R for segmentation/cortical thickness

General comments:You will want to install and load ANTsR: library(ANTsR)
You will also want to do looping and error checking on your script.
I removed that from here for instructional purposes (i.e., makes this page more pithy).

Comments

Command

set name

sub<-"VNS_01"

load T1

t1<-antsImageRead(paste(sub,".nii.gz",sep=""),3)

field correction

t1c<-abpN4(t1)

Comments

#For optimal registration it is important to take out inhomogeneities in your brain
#See http://www.ncbi.nlm.nih.gov/pubmed/20378467 for more details about N4 correction

Save results

antsImageWrite(t1c,paste(sub,"_c.nii.gz",sep=""))

get template brain with skull

tem<-antsImageRead("/mnt/nfs2/users/alans/niiTemplates/NKI/TT_template.nii.gz")

Comments

#This is an important step/choice. Last week we went over how you make your own templates.
#But currently ANTsR does not do template construction (I’d love to build this — time allowing).
#You create your own or use existing templates https://figshare.com/articles/ANTs_ANTsR_Brain_Templates/915436
#Here we give an example of NKI (IXI and Kirby are nice choices).
#You can use the MNI template — this has the advantage of pulling you straight to MNI space and the cost of not being #as pretty as a ANTs based brain.

get template brain without skull

temmask<-antsImageRead("/mnt/nfs2/users/alans/niiTemplates/NKI/TT_template_BrainCerebellumMask.nii.gz")

 Comments

#This is mask to handle brain extraction. The idea that you can cookie cutter the brain once you have it aligned.
#If you are using your own/sample created one you will need to create a mask
#(can 3dSkullStrip->3dAutomask then hand trim).

creates clean brain and segmented brain

bm<-abpBrainExtraction(t1c,tem,temmask)

Comments

#A whole lot of stuff is done here – you get transformations (needed to skull strip) and skull stripping
#you also get a segmentation of the brain for free

Transform

bmbrain <- antsApplyTransforms(fixed=tem,moving=bm$brain,transformlist=bm$fwdtransforms )

 Comments

#This is the ANTs bit. This uses your transformations (created in the prior step) and you can apply them to your brain.
#You need to attend to the order of these transformations to not make bad happen.
#You can use the transforms in the bm structure to move between spaces.

Save results

antsImageWrite(bmbrain,paste(sub,"_t1.nii.gz",sep=""))

Save results

file.copy(bm$fwdtransforms[1],paste(sub,"_ft.nii.gz",sep=""))

Save results

file.copy(bm$fwdtransforms[2],paste(sub,"_ft.mat",sep=""))

Comments

#You may want this information later (you can apply it to your functional data etc.

Transform

bmseg <- antsApplyTransforms(fixed=tem,moving=bm$kmeansseg,transformlist=bm$fwdtransforms )

 Comments

#Here we can save the simple kmeans segmentation for simple analysis but if we want priors we will forge on.

Save results

antsImageWrite(bmseg,paste(sub,"_kseg.nii.gz",sep=""))

get nicer segmented brain info

kseg<-kmeansSegmentation(bm$brain,3,bm$bmask)

 Comments

#To get probability images for cortical thickness data.
#For details http://www.ncbi.nlm.nih.gov/pubmed/21373993
#Particularly Figure 2

Transform

bmseg <- antsApplyTransforms(fixed=tem,moving=kseg$segmentation,transformlist=bm$fwdtransforms )

Save results

antsImageWrite(bmseg,paste(sub,"_Kseg.nii.gz",sep=""))

Combine images for rf

fseg<-lappend(t1c,kseg$probabilityimages)

Clear some memory

rm("t1","t1c","temmask")

Do random forest

rfsegs<-rfSegmentation(kseg$segmentation, fseg, verbose=TRUE,ntrees=50)

 

#Random forest can be used to refine the classification this is most probably overkill
#And was developed in part to better identify tumors (using a k=X segmenting).
#For details: http://www.ncbi.nlm.nih.gov/pubmed/25433513
#Particularly Figure 5
#Note: the default is ntrees=100 but my server couldn’t handle that so I scaled back.

Apply random forest

rfsegsP<-rfSegmentationPredict( rfsegs$rfModel , fseg , bm$bmask )

 

#This is where the random forest is applied

Save results

rfseg <- antsApplyTransforms(fixed=tem,moving=rfsegs$segmentation,transformlist=bm$fwdtransforms )

Save results

antsImageWrite(rfsegsP,paste(sub,"_rfseg.nii.gz",sep=""))

get cortical thickness

rfkthk<-kellyKapowski(s=rfsegs$segmentation,g=rfsegs$probabilityimages[[2]],w=rfsegs$probabilityimages[[3]],its=45,r=0.5,m=1)

Comments

#This is the cortical thickness calculator.
#For details
http://www.ncbi.nlm.nih.gov/pubmed/19150502
#Seems comparable to freesurfer: http://www.ncbi.nlm.nih.gov/pubmed/24879923
#Note: if you you don’t random forest just drop the rf suffix

Transform

bmrfkthk <- antsApplyTransforms(fixed=tem,moving=rfkthk,transformlist=bm$fwdtransforms )

Save results

antsImageWrite(bmkthk,paste(sub,"_thk.nii.gz",sep=""))