halfpipe.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #!/bin/bash
  2. #$ -N halfpipe
  3. #$ -q compute7.q
  4. #$ -j y
  5. #$ -o halfpipe.log.txt
  6. #$ -cwd
  7. #$ -pe smp 2 -binding linear
  8. #$ -l h_rt=48:0:0
  9. #$ -l virtual_free=20480M
  10. #$ -t 101-10000
  11. set -e
  12. set -x
  13. base_directory=/ifs/loni/faculty/njahansh/collaborators/lea/ukbiobank
  14. clinical_directory=${base_directory}/clinical_data
  15. dataset_directory=$(mktemp -d)
  16. bids_directory=$(mktemp -d)
  17. synthseg_directory=$(mktemp -d)
  18. halfpipe_directory=$(mktemp -d)
  19. synthseg_container=${base_directory}/synthseg.sif
  20. halfpipe_container=${base_directory}/halfpipe-latest-240307.sif
  21. read -r -a files < <(sed -n "${SGE_TASK_ID}p" ${base_directory}/tasks.txt)
  22. for file in "${files[@]}"; do
  23. ln -s "${file}" "${dataset_directory}"
  24. done
  25. mamba run --name clinica \
  26. clinica convert ukb-to-bids \
  27. "${dataset_directory}" \
  28. "${clinical_directory}" \
  29. "${bids_directory}"
  30. t1w_file=$(find "${bids_directory}" -name "*_T1w.nii.gz" -print -quit)
  31. mv -v "${t1w_file}" "${synthseg_directory}/t1w.nii.gz"
  32. singularity run \
  33. --bind "${synthseg_directory}" \
  34. ${synthseg_container} \
  35. --i "${synthseg_directory}/t1w.nii.gz" \
  36. --o "${synthseg_directory}/synthseg.nii.gz" \
  37. --robust \
  38. --cpu --threads 1
  39. singularity exec \
  40. --bind "${synthseg_directory}" \
  41. ${halfpipe_container} \
  42. antsApplyTransforms \
  43. --dimensionality 3 \
  44. --input "${synthseg_directory}/synthseg.nii.gz" \
  45. --reference-image "${synthseg_directory}/t1w.nii.gz" \
  46. --output "${synthseg_directory}/synthseg_in_t1w_space.nii.gz" \
  47. --interpolation MultiLabel \
  48. --verbose
  49. singularity exec \
  50. --bind "${base_directory}" \
  51. --bind "${synthseg_directory}" \
  52. ${halfpipe_container} \
  53. ${base_directory}/apply_mask.py \
  54. "${synthseg_directory}/t1w.nii.gz" \
  55. "${synthseg_directory}/synthseg_in_t1w_space.nii.gz" \
  56. "${t1w_file}"
  57. cat <<EOF >"${halfpipe_directory}/spec.json"
  58. {
  59. "halfpipe_version": "1.2.2.post1.dev535+gc31d205",
  60. "schema_version": "3.0",
  61. "timestamp": "2023-08-08_16-01",
  62. "global_settings": {
  63. "dummy_scans": 0,
  64. "slice_timing": true,
  65. "use_bbr": null,
  66. "skull_strip_algorithm": "none",
  67. "run_mriqc": false,
  68. "run_fmriprep": true,
  69. "run_halfpipe": true,
  70. "fd_thres": 0.5,
  71. "anat_only": false,
  72. "write_graph": false,
  73. "hires": false,
  74. "run_reconall": false,
  75. "t2s_coreg": false,
  76. "medial_surface_nan": false,
  77. "bold2t1w_dof": 9,
  78. "fmap_bspline": true,
  79. "force_syn": false,
  80. "longitudinal": false,
  81. "regressors_all_comps": false,
  82. "regressors_dvars_th": 1.5,
  83. "regressors_fd_th": 0.5,
  84. "skull_strip_fixed_seed": false,
  85. "skull_strip_template": "OASIS30ANTs",
  86. "aroma_err_on_warn": false,
  87. "aroma_melodic_dim": -200,
  88. "sloppy": false
  89. },
  90. "files": [
  91. {
  92. "metadata": {},
  93. "datatype": "bids",
  94. "path": "${bids_directory}"
  95. },
  96. {
  97. "suffix": "events",
  98. "path": "${bids_directory}/task-{task}_events.tsv",
  99. "extension": ".tsv",
  100. "datatype": "func"
  101. }
  102. ],
  103. "settings": [
  104. {
  105. "ica_aroma": true,
  106. "smoothing": {
  107. "fwhm": 6.0
  108. },
  109. "grand_mean_scaling": {
  110. "mean": 10000.0
  111. },
  112. "bandpass_filter": {
  113. "hp_width": 125.0,
  114. "lp_width": null,
  115. "type": "gaussian"
  116. },
  117. "name": "icaAromaSetting",
  118. "filters": [
  119. {
  120. "type": "tag",
  121. "action": "include",
  122. "values": [
  123. "facesshapesemotion"
  124. ],
  125. "entity": "task"
  126. }
  127. ],
  128. "output_image": false
  129. },
  130. {
  131. "ica_aroma": false,
  132. "smoothing": {
  133. "fwhm": 6.0
  134. },
  135. "grand_mean_scaling": {
  136. "mean": 10000.0
  137. },
  138. "bandpass_filter": {
  139. "hp_width": 125.0,
  140. "lp_width": null,
  141. "type": "gaussian"
  142. },
  143. "confounds_removal": [
  144. "(trans|rot)_[xyz]"
  145. ],
  146. "name": "motionParametersSetting",
  147. "filters": [
  148. {
  149. "type": "tag",
  150. "action": "include",
  151. "values": [
  152. "facesshapesemotion"
  153. ],
  154. "entity": "task"
  155. }
  156. ],
  157. "output_image": false
  158. },
  159. {
  160. "ica_aroma": false,
  161. "smoothing": {
  162. "fwhm": 6.0
  163. },
  164. "grand_mean_scaling": {
  165. "mean": 10000.0
  166. },
  167. "bandpass_filter": {
  168. "hp_width": 125.0,
  169. "lp_width": null,
  170. "type": "gaussian"
  171. },
  172. "name": "noConfoundsSetting",
  173. "filters": [
  174. {
  175. "type": "tag",
  176. "action": "include",
  177. "values": [
  178. "facesshapesemotion"
  179. ],
  180. "entity": "task"
  181. }
  182. ],
  183. "output_image": false
  184. }
  185. ],
  186. "features": [
  187. {
  188. "name": "icaAroma",
  189. "setting": "icaAromaSetting",
  190. "contrasts": [
  191. {
  192. "type": "t",
  193. "name": "faceNegVsControl",
  194. "values": {
  195. "Face": 1.0,
  196. "Shape": -1.0
  197. }
  198. }
  199. ],
  200. "hrf": "dgamma",
  201. "conditions": [
  202. "Face",
  203. "Shape"
  204. ],
  205. "type": "task_based",
  206. "high_pass_filter_cutoff": 125.0
  207. },
  208. {
  209. "name": "motionParameters",
  210. "setting": "motionParametersSetting",
  211. "contrasts": [
  212. {
  213. "type": "t",
  214. "name": "faceNegVsControl",
  215. "values": {
  216. "Face": 1.0,
  217. "Shape": -1.0
  218. }
  219. }
  220. ],
  221. "hrf": "dgamma",
  222. "conditions": [
  223. "Face",
  224. "Shape"
  225. ],
  226. "type": "task_based",
  227. "high_pass_filter_cutoff": 125.0
  228. },
  229. {
  230. "name": "noConfounds",
  231. "setting": "noConfoundsSetting",
  232. "contrasts": [
  233. {
  234. "type": "t",
  235. "name": "faceNegVsControl",
  236. "values": {
  237. "Face": 1.0,
  238. "Shape": -1.0
  239. }
  240. }
  241. ],
  242. "hrf": "dgamma",
  243. "conditions": [
  244. "Face",
  245. "Shape"
  246. ],
  247. "type": "task_based",
  248. "high_pass_filter_cutoff": 125.0
  249. }
  250. ],
  251. "models": []
  252. }
  253. EOF
  254. singularity run \
  255. --contain \
  256. --cleanenv \
  257. --bind /tmp \
  258. ${halfpipe_container} \
  259. --workdir "${halfpipe_directory}" \
  260. --skip-spec-ui \
  261. --nipype-n-procs "$(nproc)" \
  262. --keep none
  263. derivatives_directory=${halfpipe_directory}/derivatives
  264. mask_file=$(find "${derivatives_directory}" -name "*_desc-brain_mask.nii.gz" \( -not -name "*space*" \) -print -quit)
  265. report_file=$(find "${halfpipe_directory}/reports" -name "*_skull_strip_report*svg" -print -quit)
  266. singularity exec \
  267. --bind /tmp \
  268. ${halfpipe_container} \
  269. python ${base_directory}/plot_skull_strip.py \
  270. --mask-file "${mask_file}" \
  271. --background-file "${synthseg_directory}/t1w.nii.gz" \
  272. --output-file "${report_file}"
  273. subject_id=$(basename "$(find "${derivatives_directory}"/fmriprep/* -maxdepth 0 -print -quit)")
  274. fmriprep_directory=$(find "${derivatives_directory}"/fmriprep/"${subject_id}"/ses-* -maxdepth 0 -print -quit)
  275. transform_suffix="_from-T1w_to-MNI152NLin2009cAsym_mode-image_xfm.h5"
  276. transform_file=$(find "${derivatives_directory}" -name "*${transform_suffix}" -print -quit)
  277. jacobian_file=$(dirname "${transform_file}")/$(basename "${transform_file}" ${transform_suffix})_jacobian.nii.gz
  278. singularity exec \
  279. --cleanenv \
  280. --bind /tmp \
  281. ${halfpipe_container} \
  282. python ${base_directory}/calculate_jacobian.py \
  283. --transform-file "${transform_file}" \
  284. --output-file "${jacobian_file}"
  285. mapfile -t anat_files < <(find "${fmriprep_directory}"/anat \( -name "*.nii.gz" -o -name "*.json" \) -not -name "*_desc-preproc_T1w.nii.gz" -print)
  286. tar_file="${base_directory}/halfpipe/${subject_id}.tar"
  287. tar --verbose \
  288. --create \
  289. --dereference \
  290. --file="${tar_file}" \
  291. "${halfpipe_directory}"/reports/"${subject_id}" \
  292. "${derivatives_directory}"/halfpipe/"${subject_id}" \
  293. "${fmriprep_directory}"/func \
  294. "${anat_files[@]}"
  295. zstd --ultra --threads=0 -22 "${tar_file}"
  296. rm "${tar_file}"
  297. rm -rf "${dataset_directory}" "${bids_directory}" "${halfpipe_directory}"