|
1 | 1 | #! /bin/bash |
2 | | -# This script is meant to evoke the algorithm without requiring any input arguments |
3 | 2 | # |
4 | | -LOG_FILE=/flywheel/v0/output/mri-deface.log |
5 | | - |
6 | | -( |
7 | | - # Define directory names and containers |
8 | | - FLYWHEEL_BASE=/flywheel/v0 |
9 | | - INPUT_DIR=$FLYWHEEL_BASE/input/anatomical |
10 | | - OUTPUT_DIR=$FLYWHEEL_BASE/output |
11 | | - CONFIG_FILE=$FLYWHEEL_BASE/config.json |
12 | | - CONTAINER='[flywheel/mri-deface]' |
13 | | - |
14 | | - # Define brain and face templates |
15 | | - brain_template=$FLYWHEEL_BASE/talairach_mixed_with_skull.gca |
16 | | - face_template=$FLYWHEEL_BASE/face.gca |
17 | | - |
18 | | - # Check if the input directory is not empty |
19 | | - if [[ "$(ls -A $INPUT_DIR)" ]] ; then |
20 | | - echo "$CONTAINER Starting..." |
21 | | - else |
22 | | - echo "Input directory is empty: $INPUT_DIR" |
23 | | - exit 1 |
24 | | - fi |
| 3 | +# Run script for flywheel/mri-deface Gear. |
| 4 | +# |
| 5 | +# Authorship: Jennifer Reiter, Michael Perry |
| 6 | +# |
| 7 | + |
| 8 | +############################################################################## |
| 9 | +# Define directory names and containers |
| 10 | + |
| 11 | +FLYWHEEL_BASE=/flywheel/v0 |
| 12 | +INPUT_DIR=$FLYWHEEL_BASE/input/anatomical |
| 13 | +OUTPUT_DIR=$FLYWHEEL_BASE/output |
| 14 | +CONFIG_FILE=$FLYWHEEL_BASE/config.json |
| 15 | +CONTAINER='[flywheel/mri-deface]' |
| 16 | + |
| 17 | + |
| 18 | +############################################################################## |
| 19 | +# Parse configuration options |
| 20 | + |
| 21 | +# If config.json exists, then we parse config file and cast vals to ENV Vars |
| 22 | +# (Flywheel gear run). Otherwise we parse manifest.json and cast the values to |
| 23 | +# ENV Vars from manifest (Docker run) Note value.default is used to grab the |
| 24 | +# configured defaults. |
| 25 | + |
| 26 | +if [[ -f $CONFIG_FILE ]]; then |
| 27 | + eval $(jq -r '.config | to_entries[] | "config_\(.key)=\(.value)"' $CONFIG_FILE) |
| 28 | +else |
| 29 | + CONFIG_FILE=$FLYWHEEL_BASE/manifest.json |
| 30 | + eval $(jq -r '.config | to_entries[] | "config_\(.key)=\(.value.default)"' $CONFIG_FILE) |
| 31 | +fi |
| 32 | + |
| 33 | + |
| 34 | +############################################################################## |
| 35 | +# Define brain and face templates |
| 36 | + |
| 37 | +brain_template=$FLYWHEEL_BASE/talairach_mixed_with_skull.gca |
| 38 | +face_template=$FLYWHEEL_BASE/face.gca |
| 39 | + |
25 | 40 |
|
26 | | - # Find input file in input directory with the extension .nii, .nii.gz, .mgz or .mgh.gz or a .zip dicom file |
27 | | - input_file=`find $INPUT_DIR -iname '*.nii' -o -iname '*.nii.gz' -o -iname '*.mgz' -o -iname '*.mgh.gz' -o -iname '*.zip'` |
28 | | - |
29 | | - # Check if input file exists |
30 | | - if [[ -e $input_file ]] |
31 | | - then |
32 | | - ## Define extension of the input file |
33 | | - bni=`basename "$input_file"` |
34 | | - filename="${bni%%.*}" |
35 | | - inextension="${bni#*.}" |
36 | | - # If input extension is .zip, then unzip the files (assuming they are DICOM files) |
37 | | - if [[ $inextension == "zip" ]] |
38 | | - then |
39 | | - echo "Unzipping DICOM files" |
40 | | - unzip $input_file -d $INPUT_DIR |
41 | | - # Now that the DICOM files are unzipped, need to find one filename to pass to the mri deface software |
42 | | - input_file=`find $INPUT_DIR -iname '*.dcm' | head -n 1` |
43 | | - fi |
44 | | - ## Define extension of the output file |
45 | | - # Get output file extension from config file, if it exists |
46 | | - if [[ -e $CONFIG_FILE ]] |
47 | | - then |
48 | | - echo "Config file is present" |
49 | | - outextension=`cat $CONFIG_FILE | jq -r '.config.output_extension'` |
50 | | - echo "The extension read from the config file: $outextension" |
51 | | - # Otherwise, define output file extension as nifti (.nii.gz) |
52 | | - else |
53 | | - outextension=".nii.gz" |
54 | | - echo "No config file found, so default extension ($outextension) is being used for output" |
55 | | - fi |
56 | | - # Define output file to be passed to mri_deface |
57 | | - output_file=$OUTPUT_DIR/$filename'_deface'$outextension |
58 | | - /flywheel/v0/mri_deface $input_file $brain_template $face_template $output_file |
59 | | - else |
60 | | - echo "No Nifti, DICOM or MGZ inputs were found within input directory $INPUT_DIR" |
61 | | - exit 1 |
| 41 | +############################################################################## |
| 42 | +# Handle INPUT file |
| 43 | + |
| 44 | +# Find input file In input directory with the extension |
| 45 | +# .nii, .nii.gz, .mgz or .mgh.gz or a .zip DICOM file |
| 46 | +input_file=`find $INPUT_DIR -iname '*.nii' -o -iname '*.nii.gz' -o -iname '*.mgz' -o -iname '*.mgh.gz' -o -iname '*.zip'` |
| 47 | + |
| 48 | +# Check that input file exists |
| 49 | +if [[ -e $input_file ]]; then |
| 50 | + echo "${CONTAINER} Input file found: ${input_file}" |
| 51 | + |
| 52 | + # Determine the type of the input file |
| 53 | + if [[ "$input_file" == *.zip ]]; then |
| 54 | + type=".zip" |
| 55 | + elif [[ "$input_file" == *.nii ]]; then |
| 56 | + type=".nii" |
| 57 | + elif [[ "$input_file" == *.nii.gz ]]; then |
| 58 | + type=".nii.gz" |
| 59 | + elif [[ "$input_file" == *.mgz ]]; then |
| 60 | + type=".mgz" |
| 61 | + elif [[ "$input_file" == *.mgz.gz ]]; then |
| 62 | + type=".mgz.gz" |
62 | 63 | fi |
63 | 64 |
|
64 | | - # Get a list of the files in the output directory |
65 | | - outputs=`find $OUTPUT_DIR -type f -name "*"` |
66 | | - |
67 | | - # If outputs exist, then go on... |
68 | | - if [[ -z $outputs ]] |
69 | | - then |
70 | | - echo "No results found in output directory... Exiting" |
71 | | - exit 1 |
72 | | - else |
73 | | - chmod -R 777 $OUTPUT_DIR |
74 | | - echo -e "Wrote: `ls $OUTPUT_DIR`" |
| 65 | + # Get the base filename |
| 66 | + base_filename=`basename "$input_file" $type` |
| 67 | +else |
| 68 | + echo "${CONTAINER} No Nifti, DICOM or MGZ inputs were found within input directory $INPUT_DIR" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# If input extension is .zip, then unzip the files (assuming they are DICOM files) |
| 73 | +if [[ "$type" == *.zip ]]; then |
| 74 | + echo "${CONTAINER} Unzipping DICOM files..." |
| 75 | + unzip_dir=$INPUT_DIR/unzipped |
| 76 | + unzip $input_file -d $unzip_dir |
| 77 | + |
| 78 | + # Now that the DICOM files are unzipped, need to find the full path to the |
| 79 | + # first file to pass mri_deface |
| 80 | + input_file=`find $unzip_dir/* -type f | head -n 1` |
| 81 | + if [[ -e $input_file ]]; then |
| 82 | + echo "$CONTAINER Input file set to: $input_file" |
| 83 | + else |
| 84 | + echo "${CONTAINER} No input file found in: ${input_file}" |
| 85 | + exit 1 |
75 | 86 | fi |
| 87 | +fi |
| 88 | + |
| 89 | + |
| 90 | +############################################################################## |
| 91 | +# Run MRI_DEFACE algorithm |
| 92 | + |
| 93 | +# Set initial exit status |
| 94 | +mri_deface_exit_status_nifti=0 |
| 95 | +mri_deface_exit_status_mgz=0 |
| 96 | + |
| 97 | +# Set base output_file name |
| 98 | +output_file=$OUTPUT_DIR/"$base_filename"'_deface' |
| 99 | + |
| 100 | +# Check if user wanted NIfTI output |
| 101 | +if [[ $config_output_nifti == 'true' ]]; then |
| 102 | + /flywheel/v0/mri_deface "$input_file" "$brain_template" "$face_template" "$output_file"'.nii.gz' |
| 103 | + mri_deface_exit_status_nifti=$? |
| 104 | +fi |
| 105 | + |
| 106 | +# Check if user wanted MGH output |
| 107 | +if [[ $config_output_mgh == 'true' ]]; then |
| 108 | + /flywheel/v0/mri_deface "$input_file" "$brain_template" "$face_template" "$output_file"'.mgz' |
| 109 | + mri_deface_exit_status_mgz=$? |
| 110 | +fi |
| 111 | + |
| 112 | + |
| 113 | +############################################################################## |
| 114 | +# Handle Exit status |
76 | 115 |
|
| 116 | +if [[ $mri_deface_exit_status_nifti == 0 ]] && [[ $mri_deface_exit_status_mgz == 0 ]]; then |
| 117 | + echo -e "${CONTAINER} Success!" |
77 | 118 | exit 0 |
78 | | -) 2>&1 | tee $LOG_FILE |
| 119 | +else |
| 120 | + echo "${CONTAINER} Something went wrong! mri_deface exited non-zero!" |
| 121 | + exit 1 |
| 122 | +fi |
0 commit comments