-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyFile2Output
executable file
·190 lines (159 loc) · 4.71 KB
/
copyFile2Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
CmdName=$(basename "$0")
Syntax="${CmdName} [-n][-v] [-c ConfigJsonFile][-i IdString][-o OutputDir][-t tags]{-a AtlasName} {file}"
while getopts a:c:i:no:t:v arg
do
case "$arg" in
a|c|i|n|o|t|v)
eval "opt_${arg}='${OPTARG:=1}'"
;;
esac
done
shift $(( "$OPTIND" - 1 ))
if [ -n "$opt_a" ]
then
Atlas="$opt_a"
else
echo "${CmdName} : missing -a AtlasName" 1>&2
exit 1
fi
if [ -n "$opt_c" ]
then
ConfigJsonFile="$opt_c"
else
ConfigJsonFile=config.json
fi
if [ -n "$opt_o" ]
then
OutputDir="$opt_o"
else
OutputDir=/flywheel/v0/output
fi
#
# add a leading ',' so if there aren't any tags, [ "footag" ${ArgTags} ] is valid json
#
[ -n "$opt_t" ] && ArgTags=$(echo $opt_t | sed 's/,/","/g; s/^/,"/; s/$/"/')
AshsFile="$1"
read GearName GearVersion GearId < <(jq -r '.job.gear_info | [ .name, .version, .id ] | join(" ")' "$ConfigJsonFile")
[ -n "$opt_v" ] && echo "$GearName $GearVersion $GearId" 1>&2
# because filenames can have spaces
T1Filename=$(jq -r '.inputs.T1w.location.name' "$ConfigJsonFile")
T1FileId=$(jq -r '.inputs.T1w.object.file_id' "$ConfigJsonFile")
T2Filename=$(jq -r '.inputs.T2w.location.name' "$ConfigJsonFile")
T2FileId=$(jq -r '.inputs.T2w.object.file_id' "$ConfigJsonFile")
AnalysisId=$(jq -r '.destination.id' "$ConfigJsonFile")
# Also known as the Id in other parts of the uploading process
if [ -n "$opt_i" ]
then
BaseFilename="$opt_i"
else
BaseFilename=$(basename "$T1Filename" .nii.gz)
fi
function sys {
[ -n "${opt_n}${opt_v}" ] && echo "$@" 1>&2
[ -n "$opt_n" ] || "$@"
}
case "$AshsFile" in
*tse.nii.gz)
OutputFilename="${BaseFilename}_${Atlas}_highresmri.nii.gz"
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
OutputJson="{
\"OutputFilename\": \"$OutputFilename\"
, \"Modality\": \"MR\"
, \"Tags\": [ \"HighResMri\" ${ArgTags} ]
}"
;;
*mprage.nii.gz)
OutputFilename="${BaseFilename}_${Atlas}_mprage.nii.gz"
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
OutputJson="{
\"OutputFilename\": \"$OutputFilename\"
}"
;;
*final/*_lfseg*.nii.gz)
if (echo "$AshsFile" | grep -q -i 'right')
then
Handedness=Right
elif (echo "$AshsFile" | grep -q -i 'left')
then
Handedness=Left
fi
case "$AshsFile" in
*lfseg_heur*)
SegmentationTag=Heur
;;
*lfseg_corr_usegray*)
SegmentationTag=UseGray
;;
*lfseg_corr_nogray*)
SegmentationTag=NoGray
;;
esac
if [ "$Atlas" == "ASHS-ICV" ]
then
OutputFilename="${BaseFilename}_${Atlas}_icv.nii.gz"
else
FileSuffix=$(echo "$AshsFile" | sed -E 's/^.*(left|right)_(.*)(.nii.gz)/\2_\1\3/')
OutputFilename="${BaseFilename}_${Atlas}_${FileSuffix}"
fi
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
CustomJson="{
\"GearName\": \"$GearName\"
, \"GearVersion\": \"$GearVersion\"
, \"GearId\": \"$GearId\"
, \"AnalysisId\": \"$AnalysisId\"
, \"Atlas\": \"$Atlas\"
, \"T1Input\": {
\"FileName\": \"$T1Filename\"
, \"FileId\": \"$T1FileId\"
, \"FlywheelPath\": \"$T1FlywheelPath\"
}
, \"T2Input\": {
\"FileName\": \"$T2Filename\"
, \"FileId\": \"$T2FileId\"
, \"FlywheelPath\": \"$T2FlywheelPath\"
}
}"
CustomJsonString=$(echo "$CustomJson" | jq --compact-output . | sed 's/"/\\"/g')
OutputJson="{
\"OutputFilename\": \"$OutputFilename\",
\"Metadata\": {
\"modality\": \"SEG\"
, \"tags\": [ \"${Handedness}\", \"${SegmentationTag}\", \"${Atlas}\" ${ArgTags} ]
, \"classification\": {
\"Custom\": [ \"${CustomJsonString}\" ]
}
}
}"
;;
*final/*bootstrap*.txt)
FileSuffix=$(echo "$AshsFile" | sed -E 's/^.*(left|right)_(.*)_(volumes.*)$/\2_\1_\3/')
OutputFilename="${BaseFilename}_${Atlas}_${FileSuffix}"
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
OutputJson="{
\"OutputFilename\": \"$OutputFilename\"
}"
;;
*affine_t1_to_template/greedy_t1_to_template.mat)
OutputFilename="${BaseFilename}_${Atlas}_greedy.mat"
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
OutputJson="{
\"OutputFilename\": \"$OutputFilename\"
}"
;;
*flirt*/flirt*.mat)
FileSuffix=$(echo "$AshsFile" | sed -E 's,^.*/,,; s/_t2_to_t1//')
OutputFilename="${BaseFilename}_${Atlas}_${FileSuffix}"
OutputFilePath="${OutputDir}/${OutputFilename}"
sys cp "$AshsFile" "$OutputFilePath"
OutputJson="{
\"OutputFilename\": \"$OutputFilename\"
}"
;;
esac
echo "$OutputJson"