Commit 80654cf3 authored by Alberto Lazari's avatar Alberto Lazari
Browse files

fixed corrupt files

parent 6b2f5ec2
#!/bin/sh
#
# Moises Hernandez-Fernandez - FMRIB Image Analysis Group
#
# Copyright (C) 2004 University of Oxford
#
# SHCOPYRIGHT
Usage() {
echo ""
echo "Usage: NODDI_Watson_finish <subject_directory>"
echo ""
echo "expects to find all the estimatedParameters in subject directory"
echo ""
exit 1
}
[ "$1" = "" ] && Usage
directory=$1
cd ${directory}
mv $directory/Param_0_samples.nii.gz $directory/fiso_samples.nii.gz
mv $directory/Param_1_samples.nii.gz $directory/fintra_samples.nii.gz
mv $directory/Param_2_samples.nii.gz $directory/kappa_samples.nii.gz
# Change range???
mv $directory/Param_3_samples.nii.gz $directory/th_samples.nii.gz
mv $directory/Param_4_samples.nii.gz $directory/ph_samples.nii.gz
Two_div_pi=0.636619772367581
$FSLDIR/bin/fslmaths $directory/fiso_samples.nii.gz -Tmean $directory/mean_fiso
$FSLDIR/bin/fslmaths $directory/fintra_samples.nii.gz -Tmean $directory/mean_fintra
$FSLDIR/bin/fslmaths $directory/kappa_samples.nii.gz -Tmean $directory/mean_kappa
$FSLDIR/bin/make_dyadic_vectors $directory/th_samples $directory/ph_samples $directory/nodif_brain_mask.nii.gz dyads1
${FSLDIR}/bin/fslmaths $directory/mean_kappa -recip -atan -mul $Two_div_pi $directory/OD
# For postmortem model have a 5th parameter (non-attenuating dot compartment)
if [ -f $directory/Param_5_samples.nii.gz ]; then
mv $directory/Param_5_samples.nii.gz $directory/irFrac_samples.nii.gz
$FSLDIR/bin/fslmaths $directory/irFrac_samples.nii.gz -Tmean $directory/mean_irFrac.nii.gz
fi
#!/bin/sh
#
# Moises Hernandez-Fernandez - FMRIB Image Analysis Group
#
# Copyright (C) 2004 University of Oxford
#
# SHCOPYRIGHT
#
# Pipeline for fitting NODDI-Watson exvivo
# Now with user specified dax and diso - Amy Howard Apr 2020
if [ "x$CUDIMOT" == "x" ]; then
echo ""
echo "Please, set enviroment variable CUDIMOT with the path where cuDIMOT is installed"
echo "The path must contain a bin directory with binary files, i.e. \$CUDIMOT/bin"
echo "For instance: export CUDIMOT=/home/moises/CUDIMOT"
echo ""
exit 1
fi
bindir=${CUDIMOT}/bin
make_absolute(){
dir=$1;
if [ -d ${dir} ]; then
OLDWD=`pwd`
cd ${dir}
dir_all=`pwd`
cd $OLDWD
else
dir_all=${dir}
fi
echo ${dir_all}
}
Usage() {
echo ""
echo "Usage: Pipeline_NODDI_Watson_diff_exvivo.sh <subject_directory> [options]"
echo ""
echo "expects to find data and nodif_brain_mask in subject directory"
echo ""
echo "<options>:"
echo "--dax (axial diffusivity in m^2/s e.g. 0.0017)"
echo "--diso (isotropic diffusivity in m^2/s e.g. 0.003)"
echo "-Q (name of the GPU(s) queue, default cuda.q (defined in environment variable: FSLGECUDAQ)"
echo "-NJOBS (number of jobs to queue, the data is divided in NJOBS parts, usefull for a GPU cluster, default 4)"
echo "--runMCMC (if you want to run MCMC)"
echo "--rician (if you want to run rician noise modelling)"
echo "-b (burnin period, default 5000)"
echo "-j (number of jumps, default 1250)"
echo "-s (sample every, default 25)"
echo "--BIC_AIC (calculate BIC & AIC)"
echo ""
exit 1
}
[ "$1" = "" ] && Usage
modelname=NODDI_Watson_diff_exvivo
step1=GridSeach
step2=FitFractions
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${FSLDIR}/lib
subjdir=`make_absolute $1`
subjdir=`echo $subjdir | sed 's/\/$/$/g'`
echo "---------------------------------------------------------------------------------"
echo "------------------------------------ CUDIMOT ------------------------------------"
echo "----------------------------- MODEL: $modelname -----------------------------"
echo "---------------------------------------------------------------------------------"
echo subjectdir is $subjdir
start=`date +%s`
#parse option arguments
njobs=1
burnin=1000
njumps=1250
sampleevery=25
other=""
queue=""
lastStepModelOpts=""
dax=0.0006
diso=0.002
shift
while [ ! -z "$1" ]
do
case "$1" in
--dax) dax=$2;shift;;
--diso) diso=$2;shift;;
-Q) queue="-q $2";shift;;
-NJOBS) njobs=$2;shift;;
-b) burnin=$2;shift;;
-j) njumps=$2;shift;;
-s) sampleevery=$2;shift;;
--runMCMC) lastStepModelOpts=$lastStepModelOpts" --runMCMC";;
--BIC_AIC) lastStepModelOpts=$lastStepModelOpts" --BIC_AIC";;
--rician) lastStepModelOpts=$lastStepModelOpts" --rician";;
#*) other=$other" "$1;;
esac
shift
done
#Set options
opts="--bi=$burnin --nj=$njumps --se=$sampleevery"
opts="$opts $other"
if [ "x$SGE_ROOT" != "x" ]; then
queue="-q $FSLGECUDAQ"
fi
#check that all required files exist
if [ ! -d $subjdir ]; then
echo "subject directory $1 not found"
exit 1
fi
if [ `${FSLDIR}/bin/imtest ${subjdir}/data` -eq 0 ]; then
echo "${subjdir}/data not found"
exit 1
fi
if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif_brain_mask` -eq 0 ]; then
echo "${subjdir}/nodif_brain_mask not found"
exit 1
fi
if [ -e ${subjdir}.${modelname}/xfms/eye.mat ]; then
echo "${subjdir} has already been processed: ${subjdir}.${modelname}."
echo "Delete or rename ${subjdir}.${modelname} before repeating the process."
exit 1
fi
echo Making output directory structure
mkdir -p ${subjdir}.${modelname}/
mkdir -p ${subjdir}.${modelname}/diff_parts
mkdir -p ${subjdir}.${modelname}/logs
mkdir -p ${subjdir}.${modelname}/Dtifit
mkdir -p ${subjdir}.${modelname}/${step1}
mkdir -p ${subjdir}.${modelname}/${step1}/diff_parts
mkdir -p ${subjdir}.${modelname}/${step1}/logs
mkdir -p ${subjdir}.${modelname}/${step2}
mkdir -p ${subjdir}.${modelname}/${step2}/diff_parts
mkdir -p ${subjdir}.${modelname}/${step2}/logs
part=0
echo Copying files to output directory
${FSLDIR}/bin/imcp ${subjdir}/nodif_brain_mask ${subjdir}.${modelname}
if [ `${FSLDIR}/bin/imtest ${subjdir}/nodif` = 1 ] ; then
${FSLDIR}/bin/fslmaths ${subjdir}/nodif -mas ${subjdir}/nodif_brain_mask ${subjdir}.${modelname}/nodif_brain
fi
# Specify Common Fixed Parameters
CFP_file=$subjdir.${modelname}/CFP
cp ${subjdir}/bvecs $subjdir.${modelname}
cp ${subjdir}/bvals $subjdir.${modelname}
echo $subjdir.${modelname}/bvecs > $CFP_file
echo $subjdir.${modelname}/bvals >> $CFP_file
N=`wc -w ${subjdir}/bvals | awk '{print $1;}'`
for dparam in dax diso; do
for file in $subjdir.${modelname}/$dparam $subjdir.${modelname}/ddtmp; do
if [ -f $file ]; then rm $file; fi
done
if [ "$dparam" == "dax" ]; then d=$dax; fi
if [ "$dparam" == "diso" ]; then d=$diso; fi
for ((i=1;i<=$N;i++)); do echo $d >> $subjdir.${modelname}/ddtmp; done
echo `cat $subjdir.${modelname}/ddtmp` > $subjdir.${modelname}/$dparam
echo $subjdir.${modelname}/$dparam >> $CFP_file
done
rm $subjdir.${modelname}/ddtmp
#Set more options
opts=$opts" --data=${subjdir}/data --maskfile=$subjdir.${modelname}/nodif_brain_mask --forcedir --CFP=$CFP_file"
# Calculate S0 with the mean of the volumes with bval<50
bvals=`cat ${subjdir}/bvals`
mkdir -p ${subjdir}.${modelname}/temporal
pos=0
for i in $bvals; do
# if [ $i -le 50 ]; then
if (( $(echo "$i < 50" |bc -l) )); then
fslroi ${subjdir}/data ${subjdir}.${modelname}/temporal/volume_$pos $pos 1
fi
pos=$(($pos + 1))
done
fslmerge -t ${subjdir}.${modelname}/temporal/S0s ${subjdir}.${modelname}/temporal/volume*
fslmaths ${subjdir}.${modelname}/temporal/S0s -Tmean ${subjdir}.${modelname}/S0
rm -rf ${subjdir}.${modelname}/temporal
# Specify Fixed parameters: S0
FixPFile=${subjdir}.${modelname}/FixP
echo ${subjdir}.${modelname}/S0 >> $FixPFile
##############################################################################
################################ First Dtifit ###############################
##############################################################################
echo "Queue Dtifit"
PathDTI=${subjdir}.${modelname}/Dtifit
dtifit_command="${bindir}/Run_dtifit.sh ${subjdir} ${subjdir}.${modelname} ${bindir}"
#SGE
dtifitProcess=`${FSLDIR}/bin/fsl_sub $queue -l $PathDTI/logs -N dtifit $dtifit_command`
##### Model Parameters: fiso, fintra, kappa, th, ph ######
#############################################################
##################### Grid Search Step ######################
#############################################################
echo "Queue GridSearch process"
PathStep1=$subjdir.${modelname}/${step1}
# Create file to specify initialisation parameters (2 parameters: th,ph)
InitializationFile=$PathStep1/InitializationParameters
echo "" > $InitializationFile #fiso
echo "" >> $InitializationFile #fintra
echo "" >> $InitializationFile #kappa
echo ${PathDTI}/dtifit_V1_th.nii.gz >> $InitializationFile #th
echo ${PathDTI}/dtifit_V1_ph.nii.gz >> $InitializationFile #ph
echo "" >> $InitializationFile #irFrac
# Do GridSearch (fiso,fintra,kappa)
GridFile=$PathStep1/GridSearch
echo "search[0]=(0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)" > $GridFile #fiso
echo "search[1]=(0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)" >> $GridFile #fintra
echo "search[2]=(1,2,3,4,5,6,7,8)" >> $GridFile #kappa
echo "search[5]=(0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0)" >> $GridFile #irFrac
partsdir=$PathStep1/diff_parts
outputdir=$PathStep1
Step1Opts=$opts" --outputdir=$outputdir --partsdir=$partsdir --FixP=$FixPFile --gridSearch=$GridFile --no_LevMar --init_params=$InitializationFile --fixed=3,4"
postproc=`${bindir}/jobs_wrapper.sh $PathStep1 $dtifitProcess $modelname GS $njobs $Step1Opts`
###############################################################
##################### Fit only Fractions ######################
###############################################################
echo "Queue Fitting Fractions process"
PathStep2=$subjdir.${modelname}/${step2}
# Create file to specify initialisation parameters (2 parameters: fiso,fintra)
InitializationFile=$PathStep2/InitializationParameters
echo $PathStep1/Param_0_samples > $InitializationFile #fiso
echo $PathStep1/Param_1_samples >> $InitializationFile #fintra
echo $PathStep1/Param_2_samples >> $InitializationFile #kappa
echo ${PathDTI}/dtifit_V1_th.nii.gz >> $InitializationFile #th
echo ${PathDTI}/dtifit_V1_ph.nii.gz >> $InitializationFile #ph
echo $PathStep1/Param_5_samples >> $InitializationFile #irFrac
partsdir=$PathStep2/diff_parts
outputdir=$PathStep2
Step2Opts=$opts" --outputdir=$outputdir --partsdir=$partsdir --FixP=$FixPFile --init_params=$InitializationFile --fixed=2,3,4"
postproc=`${bindir}/jobs_wrapper.sh $PathStep2 $postproc $modelname FitFractions $njobs $Step2Opts`
######################################################################################
######################### Fit all the parameters of the Model ########################
######################################################################################
echo "Queue Fitting process"
# Create file to specify initialization parameters (5 parameters: fiso,fintra,kappa,th,ph)
InitializationFile=$subjdir.${modelname}/InitializationParameters
echo $PathStep2/Param_0_samples > $InitializationFile #fiso
echo $PathStep2/Param_1_samples >> $InitializationFile #fintra
echo ${PathStep1}/Param_2_samples >> $InitializationFile #kappa
echo ${PathDTI}/dtifit_V1_th.nii.gz >> $InitializationFile #th
echo ${PathDTI}/dtifit_V1_ph.nii.gz >> $InitializationFile #ph
echo $PathStep2/Param_5_samples >> $InitializationFile #irFrac
partsdir=${subjdir}.${modelname}/diff_parts
outputdir=${subjdir}.${modelname}
ModelOpts=$opts" --outputdir=$outputdir --partsdir=$partsdir --FixP=$FixPFile --init_params=$InitializationFile $lastStepModelOpts"
postproc=`${bindir}/jobs_wrapper.sh ${subjdir}.${modelname} $postproc $modelname FitProcess $njobs $ModelOpts`
#########################################
### Calculate Dispersion Index & dyads ###
##########################################
finish_command="${bindir}/${modelname}_finish.sh ${subjdir}.${modelname}"
#SGE
finishProcess=`${FSLDIR}/bin/fsl_sub $queue -l ${subjdir}.${modelname}/logs -N ${modelname}_finish -j $postproc $finish_command`
endt=`date +%s`
runtime=$((endt-start))
#echo Runtime $runtime
echo Everything Queued
#!/bin/sh
# Wrapper function to run NODDI
# Can use specify both dax and diso on either
# AH Apr 2020
make_absolute(){
dir=$1;
if [ -d ${dir} ]; then
OLDWD=`pwd`
cd ${dir}
dir_all=`pwd`
cd $OLDWD
else
dir_all=${dir}
fi
echo ${dir_all}
}
Usage() {
echo ""
echo "Usage: run_NODDI.sh <subject_directory> [options]"
echo ""
echo "expects to find data and nodif_brain_mask in subject directory"
echo ""
echo "<options>:"
echo "-m (model = invivo/exvivo)"
echo "--dax (axial diffusivity) "
echo "--diso (isotropic diffusivity) "
echo "--runMCMC (if you want to run MCMC)"
echo "--rician (if you want to run rician noise modelling)"
echo "-b (burnin period, default 5000)"
echo "-j (number of jumps, default 1250)"
echo "-s (sample every, default 25)"
echo "--BIC_AIC (calculate BIC & AIC)"
echo ""
exit 1
}
[ "$1" = "" ] && Usage
subjdir=`make_absolute $1`
module add cuda/8.0
. /opt/fmrib/fsl/etc/fslconf/fsl.sh
export FSLCONFDIR=/opt/fmrib/fsl/config
export FSLMACHTYPE=linux_64-gcc4.8
export CUDA=/opt/cuda-8.0
export FSLDEVDIR=/home/fs0/amyh/cudimot/fsl-dev
export CUDIMOT=$FSLDEVDIR
#parse option arguments
njobs=4
burnin=1000
njumps=1250
sampleevery=25
other=""
queue=""
lastStepModelOpts=""
model=invivo
shift
while [ ! -z "$1" ]
do
case "$1" in
-m) model=$2;shift;;
--dax) dax=$2;shift;;
--diso) diso=$2;shift;;
-b) burnin=$2;shift;;
-j) njumps=$2;shift;;
-s) sampleevery=$2;shift;;
--runMCMC) lastStepModelOpts=$lastStepModelOpts" --runMCMC";;
--rician) lastStepModelOpts=$lastStepModelOpts" --rician";;
--BIC_AIC) lastStepModelOpts=$lastStepModelOpts" --BIC_AIC";;
*) other=$other" "$1;;
esac
shift
done
if [ -z "$model" ]; then
echo "Must specify model name as either 'invivo' or 'exvivo'"
exit 1
elif [ "$model" == "invivo" ]; then
# Default params
if [ -z "$dax" ]; then dax=0.0017; fi
if [ -z "$diso" ]; then diso=0.003; fi
export modelname=NODDI_Watson_diff
elif [ "$model" == "exvivo" ]; then
# Default params
if [ -z "$dax" ]; then dax=0.0006; fi # No idea where these values come from
if [ -z "$diso" ]; then diso=0.002; fi
export modelname=NODDI_Watson_diff_exvivo
else
echo "Must specify model name as either 'invivo' or 'exvivo'"
exit 1
fi
opts="--dax $dax --diso $diso -b $burnin -j $njumps -s $sampleevery"
/home/fs0/amyh/cudimot/mymodels/$modelname/Pipeline_${modelname}.sh $subjdir $opts $lastStepModelOpts
#!/bin/sh
# Wrapper function to run NODDI
# AH Apr 2020
make_absolute(){
dir=$1;
if [ -d ${dir} ]; then
OLDWD=`pwd`
cd ${dir}
dir_all=`pwd`
cd $OLDWD
else
dir_all=${dir}
fi
echo ${dir_all}
}
Usage() {
echo ""
echo "Usage: run_NODDI.sh <subject_directory>"
echo ""
echo "expects to find estimated parameters in subject directory"
echo ""
echo "<options>:"
echo "-m (model = invivo/exvivo)"
exit 1
}
[ "$1" = "" ] && Usage
subjdir=`make_absolute $1`
model=invivo # default
shift
while [ ! -z "$1" ]
do
case "$1" in
-m) model=$2;shift;;
esac
shift
done
if [ -z "$model" ]; then
echo "Must specify model name as either 'invivo' or 'exvivo'"
exit 1
elif [ "$model" == "invivo" ]; then
export modelname=NODDI_Watson_diff
elif [ "$model" == "exvivo" ]; then
export modelname=NODDI_Watson_diff_exvivo
else
echo "Must specify model name as either 'invivo' or 'exvivo'"
exit 1
fi
outDir=${subjdir}.${modelname}
echo 'Converting cudimot output to NODDI variables'
/home/fs0/amyh/cudimot/mymodels/NODDI_Watson_diff/NODDI_Watson_finish.sh $outDir
#!/usr/bin/env bash
# definitions
workDir=/vols/Scratch/myelin/Mohamed/Merima_data_firstbatch_imported
scriptDir=/vols/Scratch/alazari/preclinicalMPM_scripts
subjList="MS81_10_20200904
MS82_3_20200914
MS86_10_20200924
MS81_1_20200901
MS82_N_20200911
MS86_1_20200921
MS81_3_20200903
MS84_10_20200918
MS86_3_20200923
MS81_N_20200907
MS84_1_20200916
MS86_N_20200925
MS82_10_20200915
MS84_3_20200917
MS82_1_20200910
MS84_N_20200909"
# "MS81_10_20200904
# MS82_3_20200914
# MS86_10_20200924
# MS81_1_20200901
# MS82_N_20200911
# MS86_1_20200921
# MS81_3_20200903
# MS84_10_20200918
# MS86_3_20200923
# MS81_N_20200907
# MS84_1_20200916
# MS86_N_20200925
# MS82_10_20200915
# MS84_3_20200917
# MS82_1_20200910
# MS84_N_20200909"
for subj in $subjList; do
echo "preprocessing diffusion for $subj"
# merge all scans
rm $workDir/"$subj"/DWI_filenames
for i in {1..32}; do
find "$workDir"/"$subj"/DWI2/*_1_${i}.nii >> $workDir/"$subj"/DWI_filenames
done
for i in {1..32}; do
find "$workDir"/"$subj"/DWI3/*_1_${i}.nii >> $workDir/"$subj"/DWI_filenames
done
filelist=`cat $workDir/"$subj"/DWI_filenames`
mkdir -p $workDir/"$subj"/DWI_all
fslmerge -t $workDir/"$subj"/DWI_all/data_merged_original $filelist
# extract bvals and bvecs
$scriptDir/extract_bvecs_bvals.sh $workDir/"$subj"/bvals_bvecs
echo "orientation correction and mask generation for $subj"
cp $workDir/"$subj"/DWI_all/data_merged_original.nii.gz $workDir/"$subj"/DWI_all/data.nii.gz
fslorient -deleteorient $workDir/"$subj"/DWI_all/data.nii.gz
fslswapdim $workDir/"$subj"/DWI_all/data.nii.gz z -y x $workDir/"$subj"/DWI_all/data.nii.gz
fslorient -setsform 0.1 0 0 0 0 0.1 0 0 0 0 0.1 0 0 0 0 1 $workDir/"$subj"/DWI_all/data.nii.gz
fslorient -copysform2qform $workDir/"$subj"/DWI_all/data.nii.gz
fslorient -setsformcode 1 $workDir/"$subj"/DWI_all/data.nii.gz
fslorient -setqformcode 1 $workDir/"$subj"/DWI_all/data.nii.gz
mkdir $workDir/"$subj"/DWI_all/data_split
fslsplit $workDir/"$subj"/DWI_all/data.nii.gz $workDir/"$subj"/DWI_all/data_split/data -t
fslmerge -t $workDir/"$subj"/DWI_all/b0s_data $workDir/"$subj"/DWI_all/data_split/data0000.nii.gz $workDir/"$subj"/DWI_all/data_split/data0016.nii.gz $workDir/"$subj"/DWI_all/data_split/data0032.nii.gz $workDir/"$subj"/DWI_all/data_split/data0048.nii.gz
#mask generation
fslmaths $workDir/"$subj"/DWI_all/b0s_data -Tmean $workDir/"$subj"/DWI_all/b0_mean
fslmaths $workDir/"$subj"/DWI_all/b0_mean -thr 600 -dilM -ero -dilM -dilM -dilM -dilM -ero -ero -ero -bin $workDir/"$subj"/DWI_all/b0_mean_mask.nii.gz
echo "dtifits for $subj"
mkdir $workDir/"$subj"/dtifit
dtifit -k $workDir/"$subj"/DWI_all/data.nii.gz -m $workDir/"$subj"/DWI_all/b0_mean_mask.nii.gz -b $workDir/"$subj"/bvals_bvecs/bvals -r $workDir/"$subj"/bvals_bvecs/bvecs -o $workDir/"$subj"/dtifit/"$subj"
echo "done"
done
# Mini-Guide
STEP 1. Run Cristiana's wrapper (diffpostproc_pipeline_full.sh)
STEP 2. Extract average b0 + run Amy's run_NODDI.sh
STEP 3. Run Amy's run_NODDI_cleanup.sh - which extracts NODDI parameters
STEP 4. Run Cristiana's full clean-up with diff_cleanup.sh
STEP 5. Run wrapper to copy files and convert to mnc (copying ODI and fintra/NDI, but not fiso)
STEP 6. Offline QC and import to Graham for registration of average b0s
#!/usr/bin/env bash
# definitions
scriptDir=/vols/Scratch/alazari/preclinicalMPM_scripts/
subjList="MS80@20210312_192904_MS80_Merima_Doldcr_T2w_Diffusion_1_3"
# "20210301_160217_MS74_MS74_MCAO_T2w_Diffusion_QSM_round2_1_3
# 20210302_195833_MS75_MS74_MCAO_T2w_Diffusion_QSM_1_2