#!/bin/bash
FSL_DIR=~/ptmp/bin/fsl

#default mask  added 2018
maskim=${bpdir}/nodif_brain_mask.nii.gz

if [ $# -lt "1" ]; then show_help=1; fi
while getopts "hbs:w:e:f:x:p:t:m:F" Option; do
 case $Option in
  h)
   show_help=1
   ;;
  f)
   # first roi
   mask1="$OPTARG";
   m1name=${mask1##*/};
   m1name=${m1name%%.*};
   ;;
  s)
   # second roi
   mask2="$OPTARG";
   m2name=${mask2##*/};
   m2name=${m2name%%.*};
   m2string="-and-${m2name}";
   HowManyRois="2";
   ;;
  w)
   # waypoint
   wp="$OPTARG"
   wpname=${wp##*/};
   wpname=${wpname%%.*};
   wystring="-Thru${wpname}";
   waypoint="1"
   ;;
  x)
   # second waypoint
   xp="$OPTARG"
   xpname=${xp##*/};
   xpname=${xpname%%.*};
   wystring=${wystring}"and${xpname}";
   xaypoint="1"
   ;;
  p)
   # prefix
   prefix="${OPTARG}_"
   ;;
  e)
   # avoid/exclusion
   avoidm="$OPTARG"
   avoidname=${avoidm##*/};
   avoidname=${avoidname%%.*};
   avstring="-Avoid${avoidname}";
   avoid="1" 
   ;;
  t)
   # termination
   termim="$OPTARG"
   terminame=${termim##*/};
   terminame=${terminame%%.*};
   termstring="-Term${terminame}";
   termi="1" 
   ;;
   m)
   # mask   new in ver2018
   maskim="$OPTARG"
   maskname=${maskim##*/};
   maskname=${maskname%%.*};
   maskstring="-Mask${maskname}";
   maski="1" 
   ;;
  b)
   # first time?
   first="1"
   ;;
  F)
   # first time?
   FMRIB="1"
   first="1"
  esac
 done

shift $((OPTIND-1))


if [[ $show_help == "1" ]]; then
echo ""
echo "USAGE: ${0} [OPTION]... <InputDirs>
Options:
  -p  <prefix to the output>
  -f  <first ROI>
  -s  <second ROI>
  -w  <waypoint>
  -x  <second waypoint>
  -e  <exclusion/avoid>
  -t  <termination/stop>
  -m  <mask>
  -b  bet (otherwise no bet)
  -F  input ROIs are in FMRIB brain and register outputs to FMRIB brain";
echo ""
echo "Example: one ROI (LeftOrbito), take LeftUncinate waypoint and avoid AntiLVFAT mask, do it for any directory whose first character is 4. 
${0} -f LeftOrbitoFrontalWM.nii.gz -w LeftUncinate.nii.gz -a AntiLVFAT.nii.gz /analysis/Tests/TwoRoiProbTrack_Tossi/4*"
echo ""
exit;
fi

whichone=${1}    #Subjects
#echo "it will do Probtrack for" ${whichone}
echo "prefix=" ${prefix}
echo "roi1=" ${mask1}
echo "roi2=" ${mask2}
echo "waypoint=" ${wp} 
echo "second waypoint=" ${xp}
echo "exclusion=" ${avoidm}
echo "termination=" ${termim}


#echo "argument count: $#"
#echo "next argument: $1"

while [[ "${1}" != "" ]]; do
ses="${1}"
sesname=${ses##*_};
echo "${ses}"
pushd ${ses};
echo "/** I'm here at ${sesname}";
ec=$(ls EC_*.nii.gz);
out="${prefix}${m1name}${m2string}${wystring}${avstring}${termstring}"
echo "/** ${out} is the output"
outdir="${out}_in${sesname}";
mkdir ${outdir}
echo "/** All outputs are to be made in ${outdir}"
outnative="${out}_probtrack_in${sesname}"
outmni="${out}_probtrack_of${sesname}_inMNI"
outfmrib="${out}_probtrack_of${sesname}_inFMRIB"



if [ "${first}" = "1" ];
  then echo "/** bet now";
  bet2 ${ec} NoSkull_${ec} -f 0.2;
  #cp ${ec} NoSkull_${ec} bpx_*.bedpostX/;
  if [ "${FMRIB}" = "1" ];
    then echo "/** First flirt to FMRIB";
    ${FSL_DIR}/bin/flirt -interp nearestneighbour -in NoSkull_${ec} -ref ${FSL_DIR}/data/standard/FMRIB58_FA_1mm -omat diff2standard_${sesname}.mat;
    #/usr/local/fsl/bin/flirt -in NoSkull_${ec} -ref /usr/local/fsl/data/standard/FMRIB58_FA_1mm -omat diff2standard_${sesname}.mat;
    else echo "/** First flirt to MNI";
    ${FSL_DIR}/bin/flirt -interp nearestneighbour -in NoSkull_${ec} -ref ${FSL_DIR}/data/standard/MNI152_T1_1mm_brain -omat diff2standard_${sesname}.mat;
    #/usr/local/fsl/bin/flirt -in NoSkull_${ec} -ref /usr/local/fsl/data/standard/MNI152_T1_1mm_brain -omat diff2standard_${sesname}.mat;
  echo "/** inverse matrix";
  ${FSL_DIR}/bin/convert_xfm -omat standard2diff_${sesname}.mat -inverse diff2standard_${sesname}.mat;
  fi
fi
echo "/** second flirt to make a diffusion space ROIs/Masks";
flirt -interp nearestneighbour -in ${mask1} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${m1name}_in${sesname};
roioption="-x ${outdir}/${m1name}_in${sesname}.nii.gz"
bpdir=$(ls -d bpx_*.bedpostX);
if [ "${HowManyRois}" = "2" ];
  then flirt -interp nearestneighbour -in ${mask2} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${m2name}_in${sesname};
  echo -e "${PWD}/${outdir}/${m1name}_in${sesname}.nii.gz\n${PWD}/${outdir}/${m2name}_in${sesname}.nii.gz" > ${outdir}/ROIs.txt
  # echo ${roioption}
  roioption="-x ${outdir}/ROIs.txt";
fi
if [ "${avoid}" = "1" ];
  then flirt -interp nearestneighbour -in ${avoidm} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${avoidname}_in${sesname};
  fslmaths ${outdir}/${avoidname}_in${sesname} -thr 0.5 -bin ${outdir}/${avoidname}_in${sesname}_bin
  avoption="--avoid=${outdir}/${avoidname}_in${sesname}_bin"
fi
if [ "${termi}" = "1" ];
  then flirt -interp nearestneighbour -in ${termim} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${terminame}_in${sesname};
  termioption="--stop=${outdir}/${terminame}_in${sesname}"
fi
if [ "${waypoint}" = "1" ];
  then flirt -interp nearestneighbour -in ${wp} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${wpname}_in${sesname};
  wyoption="--waypoints=${outdir}/${wpname}_in${sesname}";
fi
if [ "${xaypoint}" = "1" ];
  then flirt -interp nearestneighbour -in ${xp} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${xpname}_in${sesname};
  echo -e "${PWD}/${outdir}/${wpname}_in${sesname}\n${PWD}/${outdir}/${xpname}_in${sesname}" > ${outdir}/Waypoints.txt
  wyoption="--waypoints=${outdir}/Waypoints.txt";
fi
if [ "${maski}" = "1" ];
  then flirt -interp nearestneighbour -in ${maskim} -ref ${ec} -applyxfm -init standard2diff_${sesname}.mat -out ${outdir}/${maskname}_in${sesname};
  maskoption="--mask=${outdir}/${maskname}_in${sesname}"
  else maskoption="-m ${bpdir}/nodif_brain_mask.nii.gz"
fi
echo "/** mask applied";

#${FSL_DIR}/bin/probtrackx --network --mode=seedmask ${roioption} -l -c 0.2 -S 2000 --steplength=0.5 -P 5000 --forcedir --opd -s ${bpdir}/merged -m ${bpdir}/nodif_brain_mask.nii.gz --dir=${outdir} ${wyoption} ${avoption} ${termioption};
${FSL_DIR}/bin/probtrackx --network --mode=seedmask ${roioption} -l -c 0.2 -S 2000 --steplength=0.5 -P 5000 --forcedir --opd -s ${bpdir}/merged ${maskoption} --dir=${outdir} ${wyoption} ${avoption} ${termioption};
# /usr/local/fsl/bin/probtrackx 
# --network 
# --mode=seedmask 
# -x /analysis/Tests/TrackTest_Ctrl10/HCfromMSIT/400_01_21117/LeftFrontalWM-and-RightFrontalWM-ThruROI_Xmin90Xmax90_Ymin0Ymax217_Zmin0Zmax181_GUI_NoXFM/masks.txt  
# -l -c 0.2 -S 2000 
# --steplength=0.5 
# -P 5000 
# --forcedir 
# --opd 
# -s /analysis/Tests/TrackTest_Ctrl10/HCfromMSIT/400_01_21117/bpx_20110121_1634.bedpostX/merged 
# -m /analysis/Tests/TrackTest_Ctrl10/HCfromMSIT/400_01_21117/bpx_20110121_1634.bedpostX/nodif_brain_mask  
# --dir=/analysis/Tests/TrackTest_Ctrl10/HCfromMSIT/400_01_21117/LeftFrontalWM-and-RightFrontalWM-ThruROI_Xmin90Xmax90_Ymin0Ymax217_Zmin0Zmax181_GUI_NoXFM 
# --waypoints=/analysis/Tests/TrackTest_Ctrl10/HCfromMSIT/400_01_21117/LeftFrontalWM-and-RightFrontalWM-ThruROI_Xmin90Xmax90_Ymin0Ymax217_Zmin0Zmax181_GUI_NoXFM/waypoints.txt

mv ${outdir}/fdt_paths.nii.gz ${outdir}/${outnative}.nii.gz
#wt=$(cat ${outdir}/waytotal);
#wt=$(python /ptmp/r1770/bin/WayTotalReader.py ${outdir}/waytotal)
wt=$(python ~/ptmp/bin/WayTotalReader.py ${outdir}/waytotal)
fslmaths ${outdir}/${outnative} -div ${wt} ${outdir}/${outnative}_Norm
if [ "${FMRIB}" = "1" ];
  then flirt -in ${outdir}/${outnative} -ref ${FSL_DIR}/data/standard/FMRIB58_FA_1mm -applyxfm -init diff2standard_${sesname}.mat -out ${outdir}/${outfmrib};
  flirt -in ${outdir}/${outnative}_Norm -ref ${FSL_DIR}/data/standard/FMRIB58_FA_1mm -applyxfm -init diff2standard_${sesname}.mat -out ${outdir}/${outfmrib}_Norm;
  # below two lines create MNI to/from DTI matrix
  flirt -in NoSkull_${ec} -ref ${FSL_DIR}/data/standard/MNI152_T1_1mm_brain -omat diff2standard_${sesname}.mat;
  convert_xfm -omat standard2diff_${sesname}.mat -inverse diff2standard_${sesname}.mat;
  else flirt -in ${outdir}/${outnative} -ref ${FSL_DIR}/data/standard/MNI152_T1_1mm_brain -applyxfm -init diff2standard_${sesname}.mat -out ${outdir}/${outmni};
  flirt -in ${outdir}/${outnative}_Norm -ref ${FSL_DIR}/data/standard/MNI152_T1_1mm_brain -applyxfm -init diff2standard_${sesname}.mat -out ${outdir}/${outmni}_Norm;
fi
#fslmaths ${outdir}/fdt_paths.nii.gz -div ${wt} ${outmni}_Norm
#flirt -in ${out}_Norm -ref /usr/local/fsl/data/standard/MNI152_T1_1mm_brain -applyxfm -init diff2standard_${sesname}.mat -out ${outmni}_Norm_inMNI;
popd;
echo ${roioption}; 
echo ${wyoption};
echo ${avoption};
echo ${termioption};
echo ${maskoption};

#STUFF HERE

shift
done

exit

# for ses in $(ls -d ${whichone});
# 
# done
