FreesurferGoodies/generatefreesurferscripts - MRC CBU Imaging Wiki
location: FreesurferGoodies / generatefreesurferscripts

function generatefreesurferscripts(subjectNames,cbuProjectNumber,targetDirectory)

% this Matlab function generates shell scripts for the recon-all of all your subjects for a given project
% INPUT
%        subjectNames: a cell array of the CBU identifiers (e.g. CBU111111)
%        cbuProjectNumber: a string of the CBU project number e.g. MR10027
%        targetDirectory: the directory in which your csh scripts will end up. (e.g. '/imaging/user/yourprojectfolder/yourfreesurferscripts/')
% 
% This will generate as many .csh files as you have subjects in your project. Once this function has completed, in a terminal run 
% >freesurfer
% >cd $SUBJECT_DIR
% >cd ../yourfreesurferscripts/
% >sh CBU111111.csh
%
% make sure you check out the instructions on how to set up the cbu freesurfer environment here:
% http://imaging.mrc-cbu.cam.ac.uk/imaging/FreesurferInformation
% this is optimised for freesurfer 5.1.0
%
% Ian Charest - MRC-CBSU - March 2012


rawdataPath = '/mridata/cbu/';
nSubjects = length(subjectNames);

nMPRAGEimgs=192;
returnHere=pwd;

for subI=1:nSubjects
    thisSubject=subjectNames{subI};
    thisCsh = [targetDirectory,filesep,thisSubject,'.csh'];
    fid = fopen(thisCsh,'w');
    importDir = [rawdataPath,thisSubject,'_',cbuProjectNumber,filesep];
    cd(importDir)
    temp=dir(pwd);
    fullimportDir=[importDir,temp(3).name,'/'];
    seriesDirs = dir(fullfile(fullimportDir,'*Series*'));
    structural=0;
    cd(returnHere)
    for seriesI=1:length(seriesDirs)
        currDir = [fullimportDir,seriesDirs(seriesI).name];
        files = get_files(currDir,'*.dcm');
        if size(files,1) == nMPRAGEimgs && structural==0
            thisStruct =files(1,:);
            structural=1;
        end
    end
    fprintf(fid,['recon-all -i ' thisStruct ' -all -subjid ' thisSubject]);
    fclose(fid);
    clear fid
end
end

function files = get_files(direc, filt)
% =========================================================================
% return a list of files
% filt = filter string
% direc = cell array of directory names
% revised 07-2011 Ian Charest
if nargin~=2, error('get_files:missing inputs, Please input folder(s) and file filter.'); end%if
files = [];
if ischar(direc) % if direc is already a character array
    currDir = direc;
    tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
    tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
    files = char(files,tmp);
else % if direc is a cell array
    if size(direc,1)>size(direc,2)
        nRuns=size(direc,1);
    else
        nRuns=size(direc,2);
    end
    for runI=1:nRuns % loop through each EPI session
        currDir = char(direc{runI});
        tmp = dir(fullfile(currDir,filt)); % find all files matching f*.nii
        tmp = [repmat([currDir filesep],size(tmp,1),1) char(tmp.name)]; % build the full path name for these files
        files = char(files,tmp);
    end
end
files = files(~all(files'==' ')',:);
end

CbuImaging: FreesurferGoodies/generatefreesurferscripts (last edited 2013-03-07 21:23:04 by localhost)