Skip to content

Instantly share code, notes, and snippets.

@dfsp-spirit
Last active September 17, 2020 09:13
Show Gist options
  • Save dfsp-spirit/ccdc8c5efb0cff2ba5b139190bbfb397 to your computer and use it in GitHub Desktop.
Save dfsp-spirit/ccdc8c5efb0cff2ba5b139190bbfb397 to your computer and use it in GitHub Desktop.
How to load FreeSurfer morphometry data in R, transform it and save as new file
#!/usr/bin/env Rscript
#
# This script illustrates how to load morphometry data in R, apply some
# transformation (a simple log in this example), and save the result
# to a new file.
#
# If you do not have 'freesurferformats' yet, you need to install it:
# install.packages('freesurferformats');
library('freesurferformats');
subjects_dir = "~/data/study1";
subjects_list = c("subject1", "subject2");
measure = 'thickness';
# Example for native space data
for (sj in subjects_list) {
for (hemi in c('lh', 'rh')) {
morph = freesurferformats::read.fs.morph(file.path(subjects_dir, sj, 'surf', sprintf('%s.%s', hemi, measure)));
freesurferformats::write.fs.morph(file.path(subjects_dir, sj, 'surf', sprintf('%s.%s_log', hemi, measure)), log(morph));
}
}
# Example for standard space data
template_subject = 'fsaverage';
fwhm = '10';
for (sj in subjects_list) {
for (hemi in c('lh', 'rh')) {
morph = freesurferformats::read.fs.morph(file.path(subjects_dir, sj, 'surf', sprintf('%s.%s.fwhm%s.%s.mgh', hemi, measure, fwhm, template_subject)));
freesurferformats::write.fs.morph(file.path(subjects_dir, sj, 'surf', sprintf('%s.%s_log.fwhm%s.%s.mgh', hemi, measure, fwhm, template_subject)), log(morph));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment