#!/bin/tcsh

##  Needs to be run in the ACPC directory
##  This is for T1-only segmentation
##  Usage is:
##  segment.sh T1ImageName
set t1 = $1
set acpcdir = `pwd`
set id = `echo $t1 | sed s/_/" "/g | awk '{print $1}'`

################################################################################
#	Run bootstrap segmentation
################################################################################
      if (! (-d $acpcdir/$USER) ) mkdir $acpcdir/$USER

        echo "set scanId [getScanIdFromACPCdir $acpcdir]" > $acpcdir/$USER/segment1.brains2
        echo "brains2TissueClassify 1 1 1 \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/$t1 \" >> $acpcdir/$USER/segment1.brains2
        echo ".  \" >> $acpcdir/$USER/segment1.brains2
        echo ".  \" >> $acpcdir/$USER/segment1.brains2
        echo "box \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/Talairach.bnd \" >> $acpcdir/$USER/segment1.brains2
        echo "/opt/brains2/bin/talairach/brain_box  \" >> $acpcdir/$USER/segment1.brains2
        echo ". \" >> $acpcdir/$USER/segment1.brains2
        echo "image . \" >> $acpcdir/$USER/segment1.brains2
        echo ". T1 \" >> $acpcdir/$USER/segment1.brains2
        echo "00139 0.85 0.10 4000 4000 2000 200 \" >> $acpcdir/$USER/segment1.brains2
        echo "2.5 10.0 2.0 1 1 1 \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/grey_plugs.mask \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/white_plugs.mask \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/csf_plugs.mask \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/blood_plugs.mask \" >> $acpcdir/$USER/segment1.brains2
        echo "0.0 0.0 1 \" >> $acpcdir/$USER/segment1.brains2
        echo "1 1 1 1 1 1 1 1 1 \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/Tissue_Class.mdl \" >> $acpcdir/$USER/segment1.brains2
        echo ". \" >> $acpcdir/$USER/segment1.brains2
        echo "const . \" >> $acpcdir/$USER/segment1.brains2
        echo ". \" >> $acpcdir/$USER/segment1.brains2
        echo "197 \" >> $acpcdir/$USER/segment1.brains2
        echo "$acpcdir/$id""_segment.hdr 0 0" >> $acpcdir/$USER/segment1.brains2
	echo  "b2 exit" >> $acpcdir/$USER/segment1.brains2
brains2 -c  $acpcdir/$USER/segment1.brains2

env LD_LIBRARY_PATH="/opt/brains2/lib:/opt/brains2/lib/InsightToolkit:${LD_LIBRARY_PATH}" PATH="/opt/brains2/bin:${PATH}" brains2_neuralcut.csh ronald $acpcdir/$id""_segment.hdr /opt/brains2/bin/net/MR5/brain/brain.struct /opt/brains2/bin/net/MR5/brain/brain.net $acpcdir/Talairach.bnd $acpcdir/bootstrap_brain.mask

        echo "brains2TissueClassify 1 1 1 \" > $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/$t1 \" >> $acpcdir/$USER/segment1a.brains2
        echo ".  \" >> $acpcdir/$USER/segment1a.brains2
        echo ".  \" >> $acpcdir/$USER/segment1a.brains2
        echo "mask \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/Talairach.bnd \" >> $acpcdir/$USER/segment1a.brains2
        echo "/opt/brains2/bin/talairach/brain_box  \" >> $acpcdir/$USER/segment1a.brains2
        echo "[convertMaskToErosionCleanMask $acpcdir/bootstrap_brain.mask ] \" >> $acpcdir/$USER/segment1a.brains2
        echo "image . \" >> $acpcdir/$USER/segment1a.brains2
        echo ". T1 \" >> $acpcdir/$USER/segment1a.brains2
        echo "00139 0.85 0.10 4000 4000 2000 200 \" >> $acpcdir/$USER/segment1a.brains2
        echo "2.5 10.0 2.0 1 1 1 \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/grey_plugs.mask \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/white_plugs.mask \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/csf_plugs.mask \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/blood_plugs.mask \" >> $acpcdir/$USER/segment1a.brains2
        echo "0.0 0.0 1 \" >> $acpcdir/$USER/segment1a.brains2
        echo "1 1 1 1 1 1 1 1 1 \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/Tissue_Class.mdl \" >> $acpcdir/$USER/segment1a.brains2
        echo ". \" >> $acpcdir/$USER/segment1a.brains2
        echo "const . \" >> $acpcdir/$USER/segment1a.brains2
        echo ". \" >> $acpcdir/$USER/segment1a.brains2
        echo "197 \" >> $acpcdir/$USER/segment1a.brains2
        echo "$acpcdir/$id""_segment.hdr 0 1" >> $acpcdir/$USER/segment1a.brains2

        echo "exec rm $acpcdir/bootstrap_brain.mask" >> $acpcdir/$USER/segment1a.brains2

	echo  "b2 exit" >> $acpcdir/$USER/segment1a.brains2

brains2 -c  $acpcdir/$USER/segment1a.brains2


################################################################################
#	Find dark csf in brain mask of the T1
################################################################################


set supGoal = 0.5
set midGoal = 0.5
set infGoal = 0.5
set bmask = $acpcdir/bootstrap_brain.mask.erosionCleaned.mask

set supline = `awk ' $1=="IPL_HEADER_END" {print (NR+3)}' Talairach.bnd`
set infline = `awk ' $1=="IPL_HEADER_END" {print (NR+4)}' Talairach.bnd`
set sup = ` head -$supline Talairach.bnd | tail -1 | awk '{print $2}'`
set inf = ` head -$infline Talairach.bnd | tail -1 | awk '{print $2}'`
set t3 = `echo $sup $inf | awk '{print $1-(($1-$2)/3)}'`
set b3 = `echo $sup $inf | awk '{print $2+(($1-$2)/3)}'`


brains2 -c - << EOF
set wdir [pwd]
set t1 [b2 load image $acpcdir/$t1] 
set brain [b2 load mask $bmask  ] 

set supBrain [b2 split mask \$brain axial $t3 +] 
set infBrain [b2 split mask \$brain axial $b3 -] 
set supInfBrain [b2 or masks \$supBrain \$infBrain] 
set midBrain [b2 xor masks \$brain \$supInfBrain] 

set supCSFvol 0.0
set midCSFvol 0.0
set infCSFvol 0.0

set thresh 0
set inc 3
while {\$supCSFvol < $supGoal} { set thresh [expr \$thresh + \$inc] ; set invthreshmask [b2 threshold image \$t1 \$thresh] ; set threshmask [b2 not mask \$invthreshmask] ; set supCSF [b2 AND masks \$supBrain \$threshmask] ; set msr [b2 measure volume mask \$supCSF] ; set supCSFvol [lindex [lindex \$msr 0] 1] ; puts \$supCSFvol }
puts \$thresh
b2 save mask $acpcdir/csf_sup_thresh.mask brains2 \$supCSF 
b2 exit
EOF

brains2 -c - << EOF
set wdir [pwd]
set t1 [b2 load image $acpcdir/$t1] 
set brain [b2 load mask $bmask  ] 

set supBrain [b2 split mask \$brain axial $t3 +] 
set infBrain [b2 split mask \$brain axial $b3 -] 
set supInfBrain [b2 or masks \$supBrain \$infBrain] 
set midBrain [b2 xor masks \$brain \$supInfBrain] 

set supCSFvol 0.0
set midCSFvol 0.0
set infCSFvol 0.0

set thresh 0
set inc 3
while {\$midCSFvol < $midGoal} { set thresh [expr \$thresh + \$inc] ; set invthreshmask [b2 threshold image \$t1 \$thresh] ; set threshmask [b2 not mask \$invthreshmask] ; set midCSF [b2 AND masks \$midBrain \$threshmask] ; set msr [b2 measure volume mask \$midCSF] ; set midCSFvol [lindex [lindex \$msr 0] 1] ; puts \$midCSFvol }
puts \$thresh
b2 save mask $acpcdir/csf_mid_thresh.mask brains2 \$midCSF 
b2 exit
EOF

brains2 -c - << EOF
set wdir [pwd]
set t1 [b2 load image $acpcdir/$t1] 
set brain [b2 load mask $bmask  ] 

set supBrain [b2 split mask \$brain axial $t3 +] 
set infBrain [b2 split mask \$brain axial $b3 -] 
set supInfBrain [b2 or masks \$supBrain \$infBrain] 
set midBrain [b2 xor masks \$brain \$supInfBrain] 

set supCSFvol 0.0
set midCSFvol 0.0
set infCSFvol 0.0

set thresh 0
set inc 3
while {\$infCSFvol < $infGoal} { set thresh [expr \$thresh + \$inc] ; set invthreshmask [b2 threshold image \$t1 \$thresh] ; set threshmask [b2 not mask \$invthreshmask] ; set infCSF [b2 AND masks \$infBrain \$threshmask] ; set msr [b2 measure volume mask \$infCSF] ; set infCSFvol [lindex [lindex \$msr 0] 1] ; puts \$infCSFvol }
puts \$thresh
b2 save mask $acpcdir/csf_inf_thresh.mask brains2 \$infCSF 
b2 exit
EOF


brains2 -c - << EOF
set wdir [pwd]
set supCSF [b2 load mask $acpcdir/csf_sup_thresh.mask]
set midCSF [b2 load mask $acpcdir/csf_mid_thresh.mask ]
set infCSF [b2 load mask $acpcdir/csf_inf_thresh.mask]
set csf [b2 or masks \$supCSF \$midCSF \$infCSF]
b2 save mask $acpcdir/csf_thresh.mask brains2 \$csf 
b2 exit
EOF


################################################################################
#	Rerun segmentation with csf_thresh.mask
################################################################################

        echo "set scanId [getScanIdFromACPCdir $acpcdir]" > $acpcdir/$USER/segment2.brains2
 
        echo "brains2TissueClassify 0 1 1 \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/$t1 \" >> $acpcdir/$USER/segment2.brains2
        echo ".  \" >> $acpcdir/$USER/segment2.brains2
        echo ".  \" >> $acpcdir/$USER/segment2.brains2
        echo "box \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/Talairach.bnd \" >> $acpcdir/$USER/segment2.brains2
        echo "/opt/brains2/bin/talairach/brain_box  \" >> $acpcdir/$USER/segment2.brains2
        echo ". \" >> $acpcdir/$USER/segment2.brains2
        echo "traces . \" >> $acpcdir/$USER/segment2.brains2
        echo ". T1 \" >> $acpcdir/$USER/segment2.brains2
        echo "00139 0.85 0.10 4000 4000 2000 200 \" >> $acpcdir/$USER/segment2.brains2
        echo "2.5 10.0 2.0 1 1 1 \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/grey_plugs.mask \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/white_plugs.mask \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/csf_thresh.mask \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/blood_plugs.mask \" >> $acpcdir/$USER/segment2.brains2
        echo "0.0 0.0 1 \" >> $acpcdir/$USER/segment2.brains2
        echo "1 1 1 1 1 1 1 1 1 \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/00139_10_Tissue_Class.mdl \" >> $acpcdir/$USER/segment2.brains2
        echo ". \" >> $acpcdir/$USER/segment2.brains2
        echo "const . \" >> $acpcdir/$USER/segment2.brains2
        echo ". \" >> $acpcdir/$USER/segment2.brains2
        echo "197 \" >> $acpcdir/$USER/segment2.brains2
        echo "$acpcdir/$id""_segment.hdr 0 1" >> $acpcdir/$USER/segment2.brains2

        echo "b2 exit" >> $acpcdir/$USER/segment2.brains2
	
brains2 -c $acpcdir/$USER/segment2.brains2