Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit dd91be3

Browse files
authored
Merge pull request #7 from flywheel-apps/GEAR-749-Cannot-Find-rh.white.H
Fix 2 -parallel problems, add expert options file and thalamic nuclei processing
2 parents 337206d + 67bfc22 commit dd91be3

File tree

15 files changed

+9543
-21
lines changed

15 files changed

+9543
-21
lines changed

Dockerfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@ RUN (curl -sL https://rpm.nodesource.com/setup_12.x | bash -) \
1111

1212
RUN source $FREESURFER_HOME/SetUpFreeSurfer.sh
1313

14-
# extra segmentations require matlab compiled runtime
14+
# Extra segmentations require matlab compiled runtime
1515
RUN fs_install_mcr R2014b
1616

17+
# Fix known race condition bug introduced in 7.1.1
18+
# https://www.mail-archive.com/[email protected]/msg68263.html
19+
# RUN sed -i.bak '4217 s/^/#/' $FREESURFER_HOME/bin/recon-all
20+
# The above line is already in patched recon-all along with 2nd -parallel fix
21+
# https://www.mail-archive.com/[email protected]/msg68878.html
22+
RUN mv $FREESURFER_HOME/bin/recon-all $FREESURFER_HOME/bin/recon-all.bak
23+
COPY patch/recon-all $FREESURFER_HOME/bin/recon-all
24+
25+
# Fix known bug by swapping in updated script
26+
# See https://surfer.nmr.mgh.harvard.edu/fswiki/ThalamicNuclei
27+
RUN mv $FREESURFER_HOME/bin/quantifyThalamicNuclei.sh $FREESURFER_HOME/bin/quantifyThalamicNuclei.sh.bak
28+
COPY patch/quantifyThalamicNuclei.sh $FREESURFER_HOME/bin/quantifyThalamicNuclei.sh
29+
1730
# Save environment so it can be passed in when running recon-all.
1831
ENV PYTHONUNBUFFERED 1
1932
RUN python -c 'import os, json; f = open("/tmp/gear_environ.json", "w"); json.dump(dict(os.environ), f)'

README.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Gear that runs FreeSurfer [v7.1.1 Release (July 27, 2020)](https://surfer.nmr.mg
33

44
To run this gear you need to select structural MRI file(s) as inputs and set configuration parameters. Minimally, the "anatomical" input file and a Freesurfer license need to be provided.
55

6-
Note: the current version of Freesurfer has an known issue with using the `-parallel` flag: Recon-all will fail stochastically. Recon-all will be retried if it fails the first time.
6+
Note: the current version of Freesurfer has known issues with using the `-parallel` flag that caused recon-all to fail stochastically. These have been patched in this gear. In case there are more, recon-all will be retried if it fails the first time.
77

88
# Inputs
99

@@ -13,11 +13,14 @@ Note: the current version of Freesurfer has an known issue with using the `-para
1313

1414
Anatomical NIfTI file, DICOM archive, or previous freesurfer-recon-all zip archive.
1515

16+
### Expert Options File (optional)
17+
A user-created file containing special options to include in the command string. The file should contain as the first item the name of the command, and the items following it on rest of the line will be passed as the extra options. See [Freesurfer documentation](https://surfer.nmr.mgh.harvard.edu/fswiki/recon-all#ExpertOptionsFile) for more information and examples.
18+
1619
### freesurfer_license (optional)
1720
A license is required for this gear to run but it does not have to be provided as an input file.
1821
There are [three ways](https://docs.flywheel.io/hc/en-us/articles/360013235453-How-to-include-a-Freesurfer-license-file-in-order-to-run-the-fMRIPrep-gear-) to provide the license to this gear.
1922
[Obtaining a license is free](https://surfer.nmr.mgh.harvard.edu/registration.html).
20-
If you select a file here, it will by copied into the $FSHOME directory when the gear runs before launching recon-all.
23+
If you select a file here, it will be copied into the $FSHOME directory when the gear runs before launching recon-all.
2124

2225
### t1w_anatomical_2 .. t1w_anatomical_5 (optional)
2326
Additional anatomical NIfTI files. These will be averaged together to provide for better motion correction.
@@ -31,19 +34,19 @@ Note: arguments that start with "gear-" are not passed to recon-all. They contr
3134

3235
### gear-brainstem_structures (optional)
3336

34-
Generate an automated segmentation of four different brainstem structures from the input T1 scan: medulla oblongata, pons, midbrain and superior cerebellar peduncle (SCP). See: [https://surfer.nmr.mgh.harvard.edu/fswiki/BrainstemSubstructures](https://surfer.nmr.mgh.harvard.edu/fswiki/BrainstemSubstructures) for more info. Choosing this option will write <subject_id>_brainstemSsVolumes.v2.csv to the final results. The values in that spreadsheet will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
37+
Generate an automated segmentation of four different brainstem structures from the input T1 scan: medulla oblongata, pons, midbrain and superior cerebellar peduncle (SCP). See: [https://surfer.nmr.mgh.harvard.edu/fswiki/BrainstemSubstructures](https://surfer.nmr.mgh.harvard.edu/fswiki/BrainstemSubstructures) for more info. Choosing this option will write `<subject_id>_brainstemSsVolumes.v2.csv` to the final results. The values in that spreadsheet will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
3538

3639
### gear-convert_stats (optional)
3740

38-
Convert FreeSurfer stats files to CSV. (Default=true). Converts a subcortical stats file created by recon-all and/or mri_segstats (e.g., aseg.stats) into a table in which each line is a subject and each column is a segmentation. The values are the volume of the segmentation in mm3 or the mean intensity over the structure. Also Converts all cortical stats file created by recon-all and or mris_anatomical_stats (e.g., ?h.aparc.stats) into a table in which each line is a subject and each column is a parcellation. By default, the values are the area of the parcellation in mm2. These tables will be written to .csv files that will be available in the final results. The values in the tables will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
41+
Convert FreeSurfer stats files to CSV. (Default=true). Converts a subcortical stats file created by recon-all and/or mri_segstats (e.g., `aseg.stats`) into a table in which each line is a subject and each column is a segmentation. The values are the volume of the segmentation in mm3 or the mean intensity over the structure. Also Converts all cortical stats file created by recon-all and or mris_anatomical_stats (e.g., `?h.aparc.stats`) into a table in which each line is a subject and each column is a parcellation. By default, the values are the area of the parcellation in mm2. These tables will be written to .csv files that will be available in the final results. The values in the tables will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
3942

4043
### gear-convert_surfaces (optional)
4144

42-
Convert selected surfaces in subject/surf to obj in output. (Default = true)
45+
Convert selected surfaces in subject/surf to obj in output. This allows the surfaces to be readily viewed on the Flywheel platform. (Default = true)
4346

4447
### gear-convert_volumes (optional)
4548

46-
Convert selected FreeSurfer volume files (mgz) to NIfTI format. (Default=true)
49+
Convert selected FreeSurfer volume files (mgz) to NIfTI format. This allows the volumes to be readily viewed on the Flywheel platform. (Default=true)
4750

4851
### gear-dry-run (optional)
4952
Do everything except actually execute recon-all. This is useful for debugging. (Default = false)
@@ -55,7 +58,11 @@ There are [three ways](https://docs.flywheel.io/hc/en-us/articles/360013235453-H
5558

5659
### gear-hippocampal_subfields (optional)
5760

58-
Generates an automated segmentation of the hippocampal subfields based on a statistical atlas built primarily upon ultra-high resolution (~0.1 mm isotropic) ex vivo MRI data. See: [https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfieldsAndNucleiOfAmygdala](https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfieldsAndNucleiOfAmygdala) for more info. Choosing this option will write <subject_id>_HippocampalSubfields.csv to the final results. The values in this spreadsheet will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
61+
Generates an automated segmentation of the hippocampal subfields based on a statistical atlas built primarily upon ultra-high resolution (~0.1 mm isotropic) ex vivo MRI data. See: [https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfieldsAndNucleiOfAmygdala](https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfieldsAndNucleiOfAmygdala) for more info. Choosing this option will write `<subject_id>_HippocampalSubfields.csv` to the final results. The values in this spreadsheet will also be attached to the analysis as "Custom Information" ("info" metadata) so they can be found using search and in views. (Default=true)
62+
63+
### gear-thalamic_nuclei (optional)
64+
65+
Produce a parcellation of the thalamus into 25 different nuclei, using a probabilistic atlas built with histological data. Choosing this option will produce 3 files in the subject's mri directory: `ThalamicNuclei.v12.T1.volumes.txt`, `ThalamicNuclei.v12.T1.mgz`, and `ThalamicNuclei.v12.T1.FSvoxelSpace.mgz`, and 2 files in the stats directory: `thalamic-nuclei.lh.v12.T1.stats` and `thalamic-nuclei.rh.v12.T1.stats`. See: [https://surfer.nmr.mgh.harvard.edu/fswiki/ThalamicNuclei](https://surfer.nmr.mgh.harvard.edu/fswiki/ThalamicNuclei) for more info. (Default=false)
5966

6067
### gear-log-level (optional)
6168
Gear Log verbosity level (INFO|DEBUG)
@@ -73,7 +80,11 @@ Command line option to run recon-all in parallel. By default, it instructs the b
7380

7481
### reconall_options (required)
7582

76-
Command line options to the recon-all algorithm. By default we enable '-all' and '-qcache'. '-all' runs the entire pipeline and '-qcache' will resample data onto the average subject (called fsaverage) and smooth it at various FWHM (full-width/half-max) values, usually 0, 5, 10, 15, 20, and 25mm, which can speed later processing. Note that modification of these options may result in failure if the options are not recognized. (Default='-all -qcache')
83+
Command line options to the recon-all algorithm. By default we enable '-all' and '-qcache'. '-all' runs the entire pipeline and '-qcache' will resample data onto the average subject (called fsaverage) and smooth it at various FWHM (full-width/half-max) values, usually 0, 5, 10, 15, 20, and 25mm, which can speed later processing. Note that modification of these options will result in failure if the options are not recognized. (Default='-all -qcache')
84+
85+
### gear-gtmseg (optional)
86+
87+
After running recon-all, run gtmseg on the subject. This creates a high-resolution segmentation `gtmseg.mgz`. This should take about an hour or two. `gtmseg.mgz` will use `aseg.mgz` for subcortical structures, `?h.aparc.annot` for cortical structures, and will estimate some extra-cerebral structures. (Default=False).
7788

7889
### subject_id (optional)
7990

@@ -83,6 +94,7 @@ Desired subject ID. This is used to name the resulting FreeSurfer output directo
8394
This gear runs recon-all on the provided inputs with the given configuration options. See [https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferWiki](https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferWiki) in general and [https://surfer.nmr.mgh.harvard.edu/fswiki/ReconAllDevTable](https://surfer.nmr.mgh.harvard.edu/fswiki/ReconAllDevTable) in particular for complete details.
8495

8596
# Outputs
86-
All files that are the results of recon-all in the Freesurfer subject directory are compressed into a single zip archive.
97+
All files that are the results of recon-all in the Freesurfer subject directory are compressed into a single zip archive. See the tutorial "Introduction to Freesurfer Output" tutorial [here](https://surfer.nmr.mgh.harvard.edu/fswiki/Tutorials) for details.
98+
8799

88100
This gear was created using the [bdis-app-template](https://github.com/flywheel-apps/bids-app-template). For documentation on how to run the tests in this gear, please see that README file.

README_img/inputs.png

-63.1 KB
Loading

manifest.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"name": "freesurfer-recon-all",
33
"label": "FreeSurfer 7.1.1: run recon-all",
44
"description": "FreeSurfer version 7.1.1 Release (July 27, 2020). This gear takes an anatomical NIfTI file and performs all of the FreeSurfer cortical reconstruction process. Outputs are provided in a zip file and include the entire output directory tree from Recon-All. Configuration options exist for setting the subject ID and for converting outputs to NIfTI, OBJ, and CSV. FreeSurfer is a software package for the analysis and visualization of structural and functional neuroimaging data from cross-sectional or longitudinal studies. It is developed by the Laboratory for Computational Neuroimaging at the Athinoula A. Martinos Center for Biomedical Imaging. Please see https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense for license information.",
5-
"version": "1.0.0_7.1.1",
5+
"version": "1.0.9_7.1.1",
66
"custom": {
7-
"docker-image": "flywheel/freesurfer-recon-all:1.0.0_7.1.1",
7+
"docker-image": "flywheel/freesurfer-recon-all:1.0.9_7.1.1",
88
"gear-builder": {
99
"category": "analysis",
10-
"image": "flywheel/freesurfer-recon-all:1.0.0_7.1.1"
10+
"image": "flywheel/freesurfer-recon-all:1.0.9_7.1.1"
1111
},
1212
"flywheel": {
1313
"suite": "FreeSurfer"
@@ -84,6 +84,11 @@
8484
"description": "FreeSurfer license file, provided during registration with FreeSurfer. This file will by copied to the $FSHOME directory and used during execution of the Gear.",
8585
"base": "file",
8686
"optional": true
87+
},
88+
"expert": {
89+
"description": "A user-created file containing special options to include in the command string. The file should contain as the first item the name of the command, and the items following it on rest of the line will be passed as the extra options. See Freesurfer documentation https://surfer.nmr.mgh.harvard.edu/fswiki/recon-all#ExpertOptionsFile for more information and examples.",
90+
"base": "file",
91+
"optional": true
8792
}
8893
},
8994
"config": {
@@ -108,6 +113,11 @@
108113
"default": "-all -qcache",
109114
"type": "string"
110115
},
116+
"gear-gtmseg": {
117+
"type": "boolean",
118+
"default": false,
119+
"description": "After running recon-all run gtmseg on the subject. (Default=False)."
120+
},
111121
"gear-hippocampal_subfields": {
112122
"description": "Generates an automated segmentation of the hippocampal subfields based on a statistical atlas built primarily upon ultra-high resolution (~0.1 mm isotropic) ex vivo MRI data. Choosing this option will write <subject_id>_HippocampalSubfields.csv to the final results. See: https://surfer.nmr.mgh.harvard.edu/fswiki/HippocampalSubfields for more info. (Default=true)",
113123
"default": true,
@@ -118,6 +128,11 @@
118128
"default": true,
119129
"type": "boolean"
120130
},
131+
"gear-thalamic_nuclei": {
132+
"description": "Produce a parcellation of the thalamus into 25 different nuclei, using a probabilistic atlas built with histological data. Choosing this option will produce 3 files in the subject's mri directory: ThalamicNuclei.v12.T1.volumes.txt, ThalamicNuclei.v12.T1.mgz, and ThalamicNuclei.v12.T1.FSvoxelSpace.mgz, and 2 files in the stats directory: thalamic-nuclei.lh.v12.T1.stats and thalamic-nuclei.rh.v12.T1.stats. See: https://surfer.nmr.mgh.harvard.edu/fswiki/ThalamicNuclei for more info. (Default=false)",
133+
"default": false,
134+
"type": "boolean"
135+
},
121136
"gear-register_surfaces": {
122137
"description": "Runs the xhemireg and surfreg scripts on your subject after having run recon-all in order to register the subject's left and inverted-right hemispheres to the fsaverage_sym subject. (The fsaverage_sym subject is a version of the fsaverage subject with a single the left-right symmetric pseudo-hemisphere.) (Default=true)",
123138
"default": true,

patch/quantifyThalamicNuclei.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# Define a function that exits if something goes wrong.
4+
function doIt {
5+
6+
command="$1"
7+
8+
eval "$command"
9+
10+
if [ $? != 0 ]; then
11+
echo "failed to do $command"
12+
exit -1
13+
fi
14+
}
15+
16+
17+
if [ $# -le 1 ]; then
18+
echo "Usage: $0 outputFile analysisID [subjectsDirectory]"
19+
exit -1
20+
fi
21+
22+
23+
outputfile=$1
24+
analysisID=$2
25+
resultsDirectory=$SUBJECTS_DIR
26+
if [ $# -ge 3 ]; then
27+
resultsDirectory=$3
28+
fi
29+
30+
# Show what we have
31+
echo "Gathering results from subjects in: "
32+
echo " $resultsDirectory "
33+
echo "And writing them to: "
34+
echo " $outputfile "
35+
36+
37+
# Go to the output directory
38+
doIt "cd $resultsDirectory"
39+
40+
# List all subdirectories (subjects)
41+
subjectNames=(`ls -d */`) # list only directories
42+
numberOfSubjects=${#subjectNames[*]}
43+
44+
# Go inside each subject's subdirectory and collect volumes
45+
46+
namesWritten="no"
47+
for i in `eval echo {1..$numberOfSubjects}`; do
48+
49+
# Get subject name
50+
let subjectIndex=$i-1
51+
subjectName=${subjectNames[ $subjectIndex ]}
52+
subjectName=`echo "${subjectName//\/}"` # strips the /
53+
54+
# File with volumes
55+
volFile="$resultsDirectory/$subjectName/mri/ThalamicNuclei.v12.$analysisID.volumes.txt"
56+
57+
# If the file exists, collect data
58+
if [ -f $volFile ]; then
59+
60+
echo "Collecting data for subject: $subjectName"
61+
62+
# if it's the first subject, gather names of structures
63+
if [ $namesWritten == "no" ]; then
64+
namesWritten="yes"
65+
66+
nameString="Subject ";
67+
while read line; do
68+
arr=(`echo ${line}`);
69+
nameString="$nameString ${arr[0]}"
70+
done < $volFile
71+
72+
echo $nameString > $outputfile
73+
74+
fi
75+
76+
volumes="$subjectName";
77+
while read line; do
78+
arr=(`echo ${line}`);
79+
volumes="$volumes ${arr[1]}"
80+
done < $volFile
81+
82+
echo $volumes >> $outputfile
83+
84+
else
85+
# echo "Skipping directory $subjectName "
86+
dummy=" "
87+
fi
88+
89+
done # End loop over all subjects

0 commit comments

Comments
 (0)