Created
November 27, 2020 13:57
-
-
Save jcupitt/9ed59241ab8b78d41b5e481cae614712 to your computer and use it in GitHub Desktop.
final stage of struct pipeline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() { | |
cmd="$*" | |
$cmd | |
if [ $? != 0 ]; then | |
echo ===================== | |
echo $cmd | |
echo failed | |
exit 1 | |
fi | |
} | |
usage() | |
{ | |
base=$(basename "$0") | |
echo "usage: $base entries.csv results_dir release_dir | |
This script uploads all the results. | |
Arguments: | |
entries.csv Entries for the subjects (subjectID-sessionID) used for the release | |
release_dir The directory used for the release. | |
data_dir The directory used to output the files. | |
" | |
exit; | |
} | |
[ $# -ge 5 ] || { usage; } | |
subjectID=$1 | |
sessionID=$2 | |
subj=$3 | |
age=$4 | |
releasedir=$5 | |
datadir=`pwd` | |
if [ $# -ge 6 ];then | |
datadir=$6; | |
fi | |
minimal=1 | |
if [ $# -ge 7 ];then | |
minimal=$7 | |
fi | |
action=cp | |
Hemi=('left' 'right'); | |
Cortex=('CORTEX_LEFT' 'CORTEX_RIGHT'); | |
subjdir=sub-$subjectID | |
sessiondir=ses-$sessionID | |
prefix=${subjdir}_${sessiondir} | |
anat=$subjdir/$sessiondir/anat | |
outputRawDir=$releasedir/sourcedata/$anat | |
outputDerivedDir=$releasedir/derivatives/$anat | |
outputSurfDir=$outputDerivedDir/Native | |
outputWarpDir=$outputDerivedDir/xfms | |
tmp=tmp | |
code_dir=~/GIT/dhcp-structural-pipeline | |
MNI_T1=$code_dir/atlases/MNI/MNI152_T1_1mm.nii.gz | |
if [ -f $outputDerivedDir/Native/${prefix}_wb.spec ]; then | |
exit | |
fi | |
run mkdir -p $outputSurfDir $outputWarpDir $outputRawDir $tmp | |
# raw images | |
run $action \ | |
$datadir/restore/T2/${subj}_defaced.nii.gz \ | |
$outputRawDir/${prefix}_T2w.nii.gz | |
if [ -f $datadir/T1/$subj.nii.gz ]; then | |
run mirtk transform-image \ | |
$datadir/masks/${subj}_mask_defaced.nii.gz \ | |
$tmp/${subj}_mask_defaced_T1.nii.gz \ | |
-target $datadir/T1/$subj.nii.gz \ | |
-dofin $datadir/dofs/$subj-T2-T1-r.dof.gz \ | |
-invert | |
run fslmaths \ | |
$datadir/T1/${subj}.nii.gz \ | |
-thr 0 \ | |
-mul $tmp/${subj}_mask_defaced_T1.nii.gz \ | |
$outputRawDir/${prefix}_T1w.nii.gz | |
rm $tmp/${subj}_mask_defaced_T1.nii.gz | |
fi | |
# derived images | |
ms=T2 | |
if [ -f $datadir/T1/$subj.nii.gz ]; then | |
ms="T1 T2" | |
fi | |
for m in ${ms}; do | |
for restore in "_defaced" "_restore_defaced" "_restore_brain" "_bias"; do | |
nrestore=`echo $restore |sed -e 's:_defaced::g' |sed -e 's:_bias:_biasfield:g'` | |
run $action \ | |
$datadir/restore/$m/${subj}${restore}.nii.gz \ | |
$outputDerivedDir/${prefix}_${m}w${nrestore}.nii.gz | |
done | |
done | |
# masks | |
run $action \ | |
$datadir/masks/$subj.nii.gz \ | |
$outputDerivedDir/${prefix}_brainmask_drawem.nii.gz | |
run $action \ | |
$datadir/masks/$subj-bet.nii.gz \ | |
$outputDerivedDir/${prefix}_brainmask_bet.nii.gz | |
# segmentations | |
for seg in all_labels tissue_labels;do | |
run $action \ | |
$datadir/segmentations/${subj}_${seg}.nii.gz \ | |
$outputDerivedDir/${prefix}_drawem_${seg}.nii.gz | |
done | |
# warps | |
ages="$age" | |
if [ $age != 40 ]; then | |
ages="$ages 40" | |
fi | |
for cage in ${ages}; do | |
run mirtk convert-dof \ | |
$datadir/dofs/template-$cage-$subj-n.dof.gz \ | |
$outputWarpDir/${prefix}_anat2std${cage}w.nii.gz \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-target $code_dir/atlases/non-rigid-v2/T2/template-$cage.nii.gz \ | |
-source $outputRawDir/${prefix}_T2w.nii.gz | |
run mirtk convert-dof \ | |
$datadir/dofs/$subj-template-$cage-n.dof.gz \ | |
$outputWarpDir/${prefix}_std${cage}w2anat.nii.gz \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-source $code_dir/atlases/non-rigid-v2/T2/template-$cage.nii.gz \ | |
-target $outputRawDir/${prefix}_T2w.nii.gz | |
done | |
# surfaces, which might be missing ... look for MyelinMap, since that will | |
# only be made at the end of surface extraction | |
surfdir=$datadir/surfaces/$subj/workbench | |
if [ -f $surfdir/$subj.L.SmoothedMyelinMap.native.func.gii ]; then | |
for f in corrThickness curvature drawem inflated pial roi sphere sulc thickness white MyelinMap; do | |
sfiles=`ls $surfdir/$subj.*$f*` | |
for sf in ${sfiles}; do | |
so=`echo $sf | sed -e "s:$surfdir.::g"|sed -e "s:$subj.:${prefix}_:g"` | |
# bids: | |
so=`echo $so | sed -e 's:corrThickness:corr_thickness:g' | sed -e 's:SmoothedMyelinMap:smoothed_myelin_map:g' | sed -e 's:MyelinMap:myelin_map:g'` | |
so=`echo $so | sed -e 's:.native::g' | sed -e 's:_L.:_left_:g'| sed -e 's:_R.:_right_:g'` | |
run $action $sf $outputDerivedDir/Native/$so | |
done | |
done | |
wbvols="T2w_restore" | |
wbmetrics="sulc thickness curvature corr_thickness" | |
wbsurfs="white pial midthickness inflated very_inflated sphere" | |
if [ -f $datadir/T1/$subj.nii.gz ]; then | |
wbvols="$wbvols T1w_restore T1wdividedbyT2w T1wdividedbyT2w_ribbon" | |
wbmetrics="$wbmetrics myelin_map smoothed_myelin_map" | |
# myelin images etc. | |
run $action \ | |
$surfdir/$subj.ribbon.nii.gz \ | |
$outputDerivedDir/${prefix}_ribbon.nii.gz | |
run $action \ | |
$surfdir/$subj.T1wDividedByT2w_defaced.nii.gz \ | |
$outputDerivedDir/${prefix}_T1wdividedbyT2w.nii.gz | |
run $action \ | |
$surfdir/$subj.T1wDividedByT2w_ribbon.nii.gz \ | |
$outputDerivedDir/${prefix}_T1wdividedbyT2w_ribbon.nii.gz | |
fi | |
fi | |
if [ ! $minimal -eq 1 ]; then | |
# segmentations | |
seg=labels | |
run $action \ | |
$datadir/segmentations/${subj}_${seg}.nii.gz \ | |
$outputDerivedDir/${prefix}_drawem_${seg}.nii.gz | |
# warps | |
for cage in ${ages}; do | |
run mirtk convert-dof \ | |
$datadir/dofs/template-$cage-$subj-n.dof.gz \ | |
$tmp/template-$cage-$subj-r.dof.gz \ | |
-input-format mirtk \ | |
-output-format rigid | |
run mirtk convert-dof \ | |
$datadir/dofs/$subj-template-$cage-n.dof.gz \ | |
$tmp/$subj-template-$cage-r.dof.gz \ | |
-input-format mirtk \ | |
-output-format rigid | |
run mirtk convert-dof \ | |
$tmp/template-$cage-$subj-r.dof.gz \ | |
$outputWarpDir/${prefix}_anat2std${cage}w.mat \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-target $code_dir/atlases/non-rigid-v2/T2/template-$cage.nii.gz \ | |
-source $outputRawDir/${prefix}_T2w.nii.gz | |
run mirtk convert-dof \ | |
$tmp/$subj-template-$cage-r.dof.gz \ | |
$outputWarpDir/${prefix}_std${cage}w2anat.mat \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-source $code_dir/atlases/non-rigid-v2/T2/template-$cage.nii.gz \ | |
-target $outputRawDir/${prefix}_T2w.nii.gz | |
run $action \ | |
$tmp/template-$cage-$subj-r.dof.gz \ | |
$outputWarpDir/${prefix}_anat2std${cage}w-r.dof.gz | |
run $action \ | |
$tmp/$subj-template-$cage-r.dof.gz \ | |
$outputWarpDir/${prefix}_std${cage}w2anat-r.dof.gz | |
run $action \ | |
$datadir/dofs/template-$cage-$subj-n.dof.gz \ | |
$outputWarpDir/${prefix}_anat2std${cage}w-n.dof.gz | |
run $action \ | |
$datadir/dofs/$subj-template-$cage-n.dof.gz \ | |
$outputWarpDir/${prefix}_std${cage}w2anat-n.dof.gz | |
done | |
if [ -f $datadir/dofs/$subj-MNI-n.dof.gz ]; then | |
run mirtk convert-dof \ | |
$datadir/dofs/$subj-MNI-n.dof.gz \ | |
$tmp/$subj-MNI-r.dof.gz \ | |
-input-format mirtk \ | |
-output-format rigid | |
run mirtk convert-dof \ | |
$datadir/dofs/$subj-MNI-n.dof.gz \ | |
$outputWarpDir/${prefix}_MNI2anat.nii.gz \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-target $outputRawDir/${prefix}_T2w.nii.gz \ | |
-source $MNI_T1 | |
run mirtk convert-dof \ | |
$datadir/dofs/$subj-MNI-r.dof.gz \ | |
$outputWarpDir/${prefix}_MNI2anat.mat \ | |
-input-format mirtk \ | |
-output-format fsl \ | |
-target $outputRawDir/${prefix}_T2w.nii.gz \ | |
-source $MNI_T1 | |
run $action \ | |
$datadir/dofs/$subj-MNI-n.dof.gz \ | |
$outputWarpDir/${prefix}_MNI2anat-n.dof.gz | |
run $action \ | |
$tmp/$subj-MNI-r.dof.gz \ | |
$outputWarpDir/${prefix}_MNI2anat-r.dof.gz | |
fi | |
if [ -f $datadir/T1/$subj.nii.gz ]; then | |
run $action \ | |
$datadir/dofs/$subj-T2-T1-r.mat \ | |
$outputWarpDir/${prefix}_T1w2anat.mat | |
run $action \ | |
$datadir/dofs/$subj-T2-T1-r.dof.gz \ | |
$outputWarpDir/${prefix}_T1w2anat-r.dof.gz | |
fi | |
# copy posteriors | |
outputPostDir=$outputDerivedDir/posteriors | |
mkdir -p $outputPostDir | |
structs=`ls $datadir/posteriors` | |
for str in ${structs}; do | |
run $action \ | |
$datadir/posteriors/$str/$subj.nii.gz \ | |
$outputPostDir/${prefix}_drawem_${str}.nii.gz | |
done | |
fi | |
# don't make the spec files -- too annoying | |
exit | |
# create spec file | |
cd $outputDerivedDir/Native | |
spec=${prefix}_wb.spec | |
rm -f $spec | |
for hi in {0..1}; do | |
h=${Hemi[$hi]} | |
C=${Cortex[$hi]} | |
for surf in ${wbsurfs};do | |
run wb_command -add-to-spec-file $spec $C ${prefix}_${h}_$surf.surf.gii | |
done | |
done | |
C=INVALID | |
for metric in ${wbmetrics};do | |
run wb_command -add-to-spec-file $spec $C ${prefix}_$metric.dscalar.nii | |
done | |
run wb_command -add-to-spec-file $spec $C ${prefix}_drawem.dlabel.nii | |
for file in ${wbvols};do | |
run wb_command -add-to-spec-file $spec $C ../${prefix}_$file.nii.gz | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment