Home > vbmeg > functions > job > vb_job_brain.m

vb_job_brain

PURPOSE ^

Convert FreeSurfer data to VBMEG format.

SYNOPSIS ^

function [V,F,xx,BV_index,Vinfo] =vb_job_brain(varargin)

DESCRIPTION ^

 Convert FreeSurfer data to VBMEG format.
 - VBMEG Cortical model file(.brain.mat)
 - VBMEG Cortical area file(.area.mat)
 - VBMEG Cortical act file(.act.mat)

 [syntax]
 vb_job_brain(brain_parm)
 vb_job_brain(proj_root,brain_parm)  [old style]

 [input]
 proj_root : <<string>> VBMEG project root directory. 
 brain_parm: <<struct>> Parameters for creating cortical surface model
 --- fields of brain_parm
  analyze_file: <<string>> MRI filename (.nii/.hdr)
 brain_parm.FS_left_file         = [fs_dir '/bem/lh.smoothwm.asc'];
 brain_parm.FS_right_file        = [fs_dir '/bem/rh.smoothwm.asc'];
 brain_parm.FS_left_infl_file    = [fs_dir '/bem/lh.inflated.asc'];
 brain_parm.FS_right_infl_file   = [fs_dir '/bem/rh.inflated.asc'];
 brain_parm.FS_left_curv_file    = [fs_dir '/bem/lh.curv.asc'];
 brain_parm.FS_right_curv_file   = [fs_dir '/bem/rh.curv.asc'];
 
 brain_parm.FS_left_label_file   = [fs_dir '/label/lh.cortex.label'];
 brain_parm.FS_right_label_file  = [fs_dir '/label/rh.cortex.label'];

 brain_parm.FS_left_sphere_file  = [fs_dir,  'lh.sphere.reg.asc'];
 brain_parm.FS_right_sphere_file = [fs_dir,  'rh.sphere.reg.asc'];
 brain_parm.FS_sphere_key        = 'sphere.reg';
 brain_parm.registration_mode='FS'; % FreeSurfer sphere registration.
  
  brain_file  : <<string>> Cortical surface model file (.brain.mat).
  act_file    : <<string>> Cortical activity map file (.act.mat).
  area_file   : <<string>> Cortical area file (.area.mat).
  Nvertex     : <<int>>    Total number of vertices for cortical surface model
                           Priority: Nvertex > reduce_ratio
  Rmax        : <<float>>  Maximum radius for neighbor search 
  N_step      : <<int>>    Division number of z axis in matching between
                           cortical surface model and MRI points
  display     : <<int>>    Display progress for this steps
  brain_space : <<string>> cortical model map mode.
                           ='mni'  : normalize to MNI space 
                           ='subj' : individual space
 ---
 
 [output]
 The following files are created: 
  VBMEG cortical surface model file (.brain.mat)
  VBMEG area file (.area.mat)
  VBMEG activity map file (.act.mat)

 The following variables are stored in .brain.mat file: 

 Standard brain information.
 V         : Cortical vertex point cordinate (SPM_Right_m) 
 F         : Patch index structure
 xx        : Normal vector to cortical surface 
 xxA       : area asigned for each vertex ([m^2])

 BV_index  : original vertex index in BV/FS corresponding to brain
 Vinfo     : Vertex dimension structure

 xxF{i}    : Next-neighbor index for the vertex-i
 xxD{i}    : Distance between next-neighbor and the vertex-i
 xxT{n}    : vertex index for n-th triangle
 nextIX{i} : Neighbor index list for the vertex-i
 nextDD{i} : Distance from the vertex-i
 normal_stat
       .neighbor_org             : BV original brain neighbor for specified vertex
       .normal_stat.normal_org   : BV original brain normal vector

 subj      : Individual brain information(structure)
    .V     : V(n, :) and subj.V(n, :) has same MNI coordinate value.
    .F     : currently empty(not supported for plotting).
    .xx
    .xxA
    .BV_index
    .Vinfo
    .nextIX
    .nextDD
    .normal_stat
 
 The following areas are automatically created and stored in .area.mat
 file: 
 areakey = 'Cortex'         : cortex region without corpus area

 [history]
 Ver.2.0 New version for group analysis
 2015-12-27 M. Sato 
 2017-03-10 M. Sato added some subject elements(subj.xxA, subj.nextIX, subj.nextDD)

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [V,F,xx,BV_index,Vinfo] = ...
0002     vb_job_brain(varargin)
0003 % Convert FreeSurfer data to VBMEG format.
0004 % - VBMEG Cortical model file(.brain.mat)
0005 % - VBMEG Cortical area file(.area.mat)
0006 % - VBMEG Cortical act file(.act.mat)
0007 %
0008 % [syntax]
0009 % vb_job_brain(brain_parm)
0010 % vb_job_brain(proj_root,brain_parm)  [old style]
0011 %
0012 % [input]
0013 % proj_root : <<string>> VBMEG project root directory.
0014 % brain_parm: <<struct>> Parameters for creating cortical surface model
0015 % --- fields of brain_parm
0016 %  analyze_file: <<string>> MRI filename (.nii/.hdr)
0017 % brain_parm.FS_left_file         = [fs_dir '/bem/lh.smoothwm.asc'];
0018 % brain_parm.FS_right_file        = [fs_dir '/bem/rh.smoothwm.asc'];
0019 % brain_parm.FS_left_infl_file    = [fs_dir '/bem/lh.inflated.asc'];
0020 % brain_parm.FS_right_infl_file   = [fs_dir '/bem/rh.inflated.asc'];
0021 % brain_parm.FS_left_curv_file    = [fs_dir '/bem/lh.curv.asc'];
0022 % brain_parm.FS_right_curv_file   = [fs_dir '/bem/rh.curv.asc'];
0023 %
0024 % brain_parm.FS_left_label_file   = [fs_dir '/label/lh.cortex.label'];
0025 % brain_parm.FS_right_label_file  = [fs_dir '/label/rh.cortex.label'];
0026 %
0027 % brain_parm.FS_left_sphere_file  = [fs_dir,  'lh.sphere.reg.asc'];
0028 % brain_parm.FS_right_sphere_file = [fs_dir,  'rh.sphere.reg.asc'];
0029 % brain_parm.FS_sphere_key        = 'sphere.reg';
0030 % brain_parm.registration_mode='FS'; % FreeSurfer sphere registration.
0031 %
0032 %  brain_file  : <<string>> Cortical surface model file (.brain.mat).
0033 %  act_file    : <<string>> Cortical activity map file (.act.mat).
0034 %  area_file   : <<string>> Cortical area file (.area.mat).
0035 %  Nvertex     : <<int>>    Total number of vertices for cortical surface model
0036 %                           Priority: Nvertex > reduce_ratio
0037 %  Rmax        : <<float>>  Maximum radius for neighbor search
0038 %  N_step      : <<int>>    Division number of z axis in matching between
0039 %                           cortical surface model and MRI points
0040 %  display     : <<int>>    Display progress for this steps
0041 %  brain_space : <<string>> cortical model map mode.
0042 %                           ='mni'  : normalize to MNI space
0043 %                           ='subj' : individual space
0044 % ---
0045 %
0046 % [output]
0047 % The following files are created:
0048 %  VBMEG cortical surface model file (.brain.mat)
0049 %  VBMEG area file (.area.mat)
0050 %  VBMEG activity map file (.act.mat)
0051 %
0052 % The following variables are stored in .brain.mat file:
0053 %
0054 % Standard brain information.
0055 % V         : Cortical vertex point cordinate (SPM_Right_m)
0056 % F         : Patch index structure
0057 % xx        : Normal vector to cortical surface
0058 % xxA       : area asigned for each vertex ([m^2])
0059 %
0060 % BV_index  : original vertex index in BV/FS corresponding to brain
0061 % Vinfo     : Vertex dimension structure
0062 %
0063 % xxF{i}    : Next-neighbor index for the vertex-i
0064 % xxD{i}    : Distance between next-neighbor and the vertex-i
0065 % xxT{n}    : vertex index for n-th triangle
0066 % nextIX{i} : Neighbor index list for the vertex-i
0067 % nextDD{i} : Distance from the vertex-i
0068 % normal_stat
0069 %       .neighbor_org             : BV original brain neighbor for specified vertex
0070 %       .normal_stat.normal_org   : BV original brain normal vector
0071 %
0072 % subj      : Individual brain information(structure)
0073 %    .V     : V(n, :) and subj.V(n, :) has same MNI coordinate value.
0074 %    .F     : currently empty(not supported for plotting).
0075 %    .xx
0076 %    .xxA
0077 %    .BV_index
0078 %    .Vinfo
0079 %    .nextIX
0080 %    .nextDD
0081 %    .normal_stat
0082 %
0083 % The following areas are automatically created and stored in .area.mat
0084 % file:
0085 % areakey = 'Cortex'         : cortex region without corpus area
0086 %
0087 % [history]
0088 % Ver.2.0 New version for group analysis
0089 % 2015-12-27 M. Sato
0090 % 2017-03-10 M. Sato added some subject elements(subj.xxA, subj.nextIX, subj.nextDD)
0091 %
0092 % Copyright (C) 2011, ATR All Rights Reserved.
0093 % License : New BSD License(see VBMEG_LICENSE.txt)
0094 
0095 if length(varargin) == 1
0096   proj_root = [];
0097   brain_parm = varargin{1};
0098 elseif length(varargin) == 2
0099   proj_root = varargin{1};
0100   brain_parm = varargin{2};
0101 end
0102 
0103 if ~isfield(brain_parm, 'brain_space')
0104     brain_parm.brain_space = 'mni';
0105 end
0106 
0107 switch(lower(brain_parm.brain_space))
0108     case 'mni'
0109         vb_disp('Cortex map mode : MNI');
0110     case 'subj'
0111         % create ver1 format BRAIN-MAT file.
0112         vb_disp('Cortex map mode : Individual');
0113         vb_job_brain_subj(proj_root, brain_parm);
0114         return;
0115     otherwise
0116         error('Unknown brain_space ''%'' was specified.', brain_parm.brain_space);
0117 end
0118 
0119 global  vbmeg_inst
0120 const = vb_define_verbose;
0121 VERBOSE_LEVEL_NOTICE = const.VERBOSE_LEVEL_NOTICE;
0122 
0123 if ~exist('DEBUG_MODE','var'), DEBUG_MODE = 0; end
0124 
0125 % Define constant
0126 define = vbmeg_inst.const;
0127 
0128 proj_root = vb_rm_trailing_slash(proj_root);
0129 
0130 %
0131 % Do not modify following lines
0132 %
0133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0134 %
0135 
0136 % Output file path
0137 if isempty(proj_root)
0138     brain_file = brain_parm.brain_file;
0139     area_file  = brain_parm.area_file;
0140     act_file   = brain_parm.act_file;
0141 else
0142     brain_file = fullfile(proj_root, brain_parm.brain_file);
0143     area_file  = fullfile(proj_root, brain_parm.area_file);
0144     act_file   = fullfile(proj_root, brain_parm.act_file);
0145 end
0146 
0147 % ID for MRI image data
0148 analyze_file = brain_parm.analyze_file;
0149 [udir, fname] = vb_get_file_parts(analyze_file);
0150 MRI_ID = vb_mMD5(analyze_file);
0151 
0152 %-------------------------------------------------------
0153 % Check registration_mode
0154 %-------------------------------------------------------
0155 flag_spm = (isfield(brain_parm, 'spm_normalization_file') ...
0156        &&    ~isempty(brain_parm.spm_normalization_file)) ;
0157        
0158 flag_fs = (isfield(brain_parm, 'FS_left_sphere_file') ... 
0159        &&    ~isempty(brain_parm.FS_left_sphere_file) ...
0160        &&  isfield(brain_parm, 'FS_right_sphere_file') ...
0161        &&   ~isempty(brain_parm.FS_right_sphere_file) ...
0162        &&  isfield(brain_parm, 'FS_sphere_key') ...
0163        &&   ~isempty(brain_parm.FS_sphere_key));
0164 
0165 if ~isfield(brain_parm, 'registration_mode')
0166     brain_parm.registration_mode = [];
0167     
0168     if flag_fs == 0 && flag_spm == 1,
0169         brain_parm.registration_mode='SPM';
0170     end
0171     if flag_fs == 1 && flag_spm == 0,
0172         brain_parm.registration_mode='FS';
0173     end
0174 end
0175 
0176 % Reference standard brain
0177 [std_parm, std_brain_dir] = vb_set_icbm152(brain_parm.Nvertex);
0178 std_brain = fullfile(std_brain_dir, std_parm.brain_file);
0179 
0180 % List up standard brain resolution
0181 parent_dir = vb_get_file_parts(std_brain_dir);
0182 d = dir(fullfile(parent_dir, '*_*'));
0183 fprintf('Available standard brain model resolution : ')
0184 for k=1:length(d)
0185     if(d(k).isdir == false)
0186         continue;
0187     end
0188     ix = findstr(d(k).name, '_');
0189     resolution = d(k).name(ix(end)+1:end);
0190     fprintf('%s ', resolution);
0191     if k == length(d), fprintf('\n'); end
0192 end
0193 if exist(std_brain, 'file') ~= 2
0194     % error case
0195     fprintf('Specified resolution(Nvertex=%d) cannot be created.\n', brain_parm.Nvertex);
0196     fprintf('If you use custom resolution, please make new standard brain file as below.\n');
0197     fprintf('==========\n');
0198     fprintf('[parm, proj_root] = vb_set_icbm152(%d);\n', brain_parm.Nvertex);
0199     fprintf('make_std_brain_model(proj_root, parm);\n');
0200     fprintf('==========\n');
0201     fprintf('Then re-run this function.\n');
0202     error('Stop processing.');
0203 end
0204 
0205 %-------------------------------------------------------
0206 % Make cortex data from MNI standard brain for group analysis
0207 % 1. Map MNI standard brain to subject brain
0208 % 2. Find nearest point from subject cortical surface
0209 %    extracted by FreeSurfer/BV
0210 %-------------------------------------------------------
0211 registration_mode = upper(brain_parm.registration_mode);
0212 switch  registration_mode
0213 case    'SPM'
0214     if flag_spm == 0 || flag_fs == 1,
0215         error('Please specify SPM normalization file');
0216     end;
0217     
0218     [V,F,xx,BV_index,Vinfo,Vext,Fext] = vb_make_brain_mni(brain_parm, 'SPM');
0219 case    'FS'
0220     if flag_fs == 0 || flag_spm == 1,
0221         error('Please specify FS spherical file');
0222     end;
0223     
0224     [V,F,xx,BV_index,Vinfo,Vext,Fext] = vb_make_brain_mni(brain_parm, 'FS');
0225 otherwise
0226     error('Registration mode error');
0227 end
0228 
0229 if DEBUG_MODE==2, return; end;
0230 %----------------------------------------------
0231 % Save cortical surface model
0232 %----------------------------------------------
0233 
0234 vb_disp(['Save brain model '], VERBOSE_LEVEL_NOTICE);
0235 vb_disp(sprintf('     filename = %s', brain_file), ...
0236         VERBOSE_LEVEL_NOTICE);
0237 [p, n, e] = vb_get_file_parts(brain_file);
0238 if ~isempty(p) && ~exist(p, 'dir')
0239     vb_mkdir(p);
0240     vb_disp(['Create directory: ' p]);
0241 end
0242 copyfile(std_brain, brain_file);
0243 
0244 
0245 %----------------------------------------
0246 % Search next-point index and distance
0247 %----------------------------------------
0248 vb_disp('Search next-point index and distance ', ...
0249         VERBOSE_LEVEL_NOTICE);
0250         
0251 % neighbor index for model vertex (V)
0252 %[xxD, xxF, xxT, xxN] = vb_next_distance( F.F3, V ); % unit : m
0253 %Nzero = length(find( xxN == 0));
0254 %fprintf('# of No neibor point = %d (# of vertex = %d)', Nzero, length(xxN))
0255 
0256 % neighbor index for extended vertex (Vext)
0257 [xxD, xxF, xxT, xxN] = vb_next_distance( Fext.F3, Vext ); % unit : m
0258 
0259 %----------------------------------------------
0260 % Calculate area assigned to the Vext
0261 %----------------------------------------------
0262 %tic;
0263 [xxA_all] = vb_calc_patch_area(Vext, Fext.F3, xxT);
0264 %vb_disp(sprintf('%f[sec]',toc), VERBOSE_LEVEL_NOTICE);
0265 
0266 if DEBUG_MODE==1, return; end
0267 %----------------------------------------------
0268 % Search neighbor points along cortex sheet
0269 %----------------------------------------------
0270 Rmax    = brain_parm.R_max; 
0271 Display = brain_parm.display;
0272 
0273 % total number of vertex
0274 Ndipole = Vinfo.Ndipole;
0275 Vindx   = 1:Ndipole;
0276 Vindx_ext   = 1:size(Vext,1);
0277 
0278 [nextIXext , nextDDext] = ...
0279     vb_find_neighbor_all(Rmax, xxF, xxD, Vindx_ext, Display);
0280 
0281 % neighbor index for V
0282 [nextIX, nextDD] = vb_reduce_neighbor_data(Vindx,nextIXext,nextDDext);
0283 
0284 %  asign external vertex by base vertex according to distance
0285 [ext_index,ext_table] = ...
0286     vb_asign_neighbor_vertex(Vindx,nextIXext,nextDDext);
0287     
0288 %  asign external vertex area to base vertex area according to distance
0289 xxA = vb_asign_ext_area(xxA_all, Vindx, ext_index, ext_table);
0290 
0291 
0292 
0293 %--------------------------------------------------------------------------
0294 % Save FS spherical coordinate
0295 %--------------------------------------------------------------------------
0296 % if flag_fs==1,
0297 %     vb_disp(['Save FS spherical coordinate '], VERBOSE_LEVEL_NOTICE);
0298 %
0299 %     parm.brain_file = brain_parm.brain_file;
0300 %     parm.brain_sphR = brain_parm.FS_right_sphere_file;
0301 %     parm.brain_sphL = brain_parm.FS_left_sphere_file;
0302 %     parm.key        = brain_parm.FS_sphere_key;
0303 %     vb_job_brain_add_sphcoord(proj_root, parm);
0304 % end
0305 
0306 %--------------------------------------------------------------------------
0307 % Copy two area files derived from two anatomical divisons
0308 %--------------------------------------------------------------------------
0309     
0310 vb_disp(['Copy area files for Atras'], VERBOSE_LEVEL_NOTICE);
0311 vb_copy_std_atlas_area(brain_file, brain_parm.Nvertex);
0312 
0313 %--------------------------------------
0314 % Make new area and act files
0315 %--------------------------------------
0316 act_new.key     = 'Uniform';
0317 act_new.xxP     = ones(Ndipole,1);
0318 act_new.comment = 'homogeneous fMRI constraint';
0319 
0320 vb_add_act([act_file], act_new, MRI_ID, OFF);
0321 
0322 % Make Area file
0323 Vindx  = [Vinfo.cortexL; Vinfo.cortexR];
0324 AreaNew.key      = 'Cortex';
0325 AreaNew.Iextract = Vindx; %[1:Ndipole]';
0326 
0327 vb_add_area(area_file, AreaNew, MRI_ID, OFF);
0328 
0329 if DEBUG_MODE==1, return; end;
0330 
0331 %---------------------------------------------------
0332 % Find subject original vertex near the reduced cortical vertex
0333 %---------------------------------------------------
0334 normal_stat = vb_original_normal_statics(proj_root,brain_parm,BV_index);
0335 
0336 % normal average over neighbor points of original brain
0337 xx = vb_calc_normal_average(normal_stat.neighbor_org, normal_stat.normal_org);
0338 
0339 vb_disp('Save neighbor statics in the original brain', ...
0340         VERBOSE_LEVEL_NOTICE);
0341     
0342 subj = struct;
0343 subj.BV_index = BV_index;
0344 subj.MRI_ID   = MRI_ID;
0345 subj.Vinfo = Vinfo;
0346 subj.V        = V;
0347 subj.F        = [];
0348 subj.xx       = xx;
0349 subj.xxA      = xxA;
0350 subj.xxD      = xxD;
0351 subj.xxF      = xxF;
0352 subj.nextIX   = nextIX;
0353 subj.nextDD   = nextDD;
0354 subj.normal_stat = normal_stat;
0355 vb_save(brain_file, 'subj', 'brain_parm');
0356 
0357 %-----------------------------------------
0358 % Check brain model by 3D & MRI image
0359 %-----------------------------------------
0360 sub_fig  = vb_check_brain_model(proj_root,brain_parm,Vext,Fext);
0361 fig_file = strrep(fullfile(proj_root, brain_parm.brain_file),...
0362                   '.brain.mat', '.png');
0363 vb_savefig_as_shown(sub_fig, fig_file);
0364 
0365 %--------------------------
0366 % Save project file
0367 %--------------------------
0368 proj_file = get_project_filename;
0369 if ~isempty(proj_file)
0370     project_file_mgr('load', proj_file);
0371     project_file_mgr('add', 'brain_parm', brain_parm);
0372 end
0373

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005