% ts1_realign.m
%
%

global dataroot spm_path subject_ids proc_subs sinfo

global COREG_WITH_MUTUAL_INFO sptl_UsMtlInfrmtn sptl_Vx

sptl_Vx = 1;				% Force reading of voxel sizes from
                                        % settings in the normalization
                                        % structure.  Overrides settings in spm_defaults

nsub = length(subject_ids);

subj_nsess = length(sinfo(1).cond_order);

nsub_proc = length(proc_subs);

%
%  Specifications for realignment step
%

realign = struct(...
    'subject_nb', nsub_proc, ...
    'num_sessions', subj_nsess, ...
    'sessions', proc_subs, ...
    'option', 1, ...			% 1=coregister only, 2=reslice only, 3=coreg&reslice
    'modality', 1, ...			% 1=EPI, 2=PET, 3=T1 MR, 4=T2 MR
    'reslice_method', 2, ...		% 1=trilinear, 2=sinc, 3=fourier
    'create', 4, ...			% 1=all images, 2=2..N, 3=All+mean, 4=mean only
    'mask', 1, ...
    'adjust_sampling_errors', 0 ...
    );

for isub = 1:nsub
  for isess = 1:subj_nsess;
    sessions(isub).images{isess} = spm_get('Files', ... 
	sprintf('%s%s/epi/run%d/', dataroot, char(subject_ids(isub)), isess), ...
	sprintf('%s*.img', char(subject_ids(isub))) ...
	);    
  end  % for isess=
end  % for isub=

%
%  Specifications for creating mean resliced image step
%

realign(2) = realign(1);
realign(2).option = 2;
realign(2).create = 4;

%
%  Specifications for reslicing
%

realign(3) = realign(2);
realign(3).create = 3;

%
%  Specifications for coregistration step -- mean EPI to coplanar
%

coreg = realign(1);

rmfield(coreg,'sessions');		% Try removing the sessions field to
                                        % make sure that coreg doesn't try
                                        % taking data from there
rmfield(coreg,'option');
coreg.opt = 1;				% coregister only
					
coreg.target_mod = 2;		% T1 MRI
coreg.object_mod = 5;		% EPI

sptl_UsMtlInfrmtn = COREG_WITH_MUTUAL_INFO	
coreg.UsMtlInfrmtn = COREG_WITH_MUTUAL_INFO; % Use mutual information registration
                                        % instead of default

for isub = 1:nsub_proc
  
  curr_sub_idx = proc_subs(isub);
  
  % Specify the target image
  coreg.target_image{isub} = spm_get('files', ...
      sprintf('%s%s/coplanar/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Specify the first object image -- could be mean EPI image
  coreg.object_image{isub} = spm_get('files', ...
      sprintf('%s%s/epi/run1/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('mean%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Select other images to apply transformation to
  coreg.other_image{isub} = {};
  tmp = '';
  for isess = 1:subj_nsess
    tmp{isess} = spm_get('Files', ... 
	sprintf('%s%s/epi/run%d/', dataroot, char(subject_ids(curr_sub_idx)), isess), ...
	sprintf('%s*.img', char(subject_ids(curr_sub_idx))));
  end
  coreg.other_image{isub} = char(tmp);
end % for isub=

%
%  Specifications for coregistration step -- first EPI to coplanar
%

coreg(2) = coreg(1);

for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);
 
  % Specify the target image
  coreg(2).target_image{isub} = spm_get('files', ...
      sprintf('%s%s/coplanar/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Specify the first object image -- could be mean EPI image
  coreg(2).object_image{isub} = spm_get('files', ...
      sprintf('%s%s/epi/run1', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*0001.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Select other images to apply transformation to
  coreg(2).other_image{isub} = coreg(1).other_image{isub};

end % for isub=


%
%  Specifications for coregistration step -- COPLANAR TO HIRES.
%

coreg(3) = coreg(1);

coreg(3).target_mod = 2;		% T1 MRI (hires)
coreg(3).object_mod = 2;		% T1 MRI (coplanar)


for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);

  % Specify the target image - hires
  coreg(3).target_image{isub} = spm_get('files', ...
      sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Specify the first object image -- coplanar
  coreg(3).object_image{isub} = spm_get('files', ...
      sprintf('%s%s/coplanar/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Select other images to apply transformation to the EPIs
  coreg(3).other_image{isub} = {};
  tmp = '';
  for isess = 1:subj_nsess
    tmp{isess} = spm_get('Files', ... 
	sprintf('%s%s/epi/run%d/', dataroot, char(subject_ids(curr_sub_idx)), isess), ...
	sprintf('%s*.img', char(subject_ids(curr_sub_idx))));
  end

  % Propagate transformations to mean EPI image
  if ~unix(sprintf('ls %s >> /dev/null', sprintf('%s%s/epi/run1/mean%s*.img', dataroot, char(subject_ids(curr_sub_idx)),char(subject_ids(curr_sub_idx)))))
    tmp{subj_nsess+1} = spm_get('Files', ... 
	sprintf('%s%s/epi/run1/', dataroot, char(subject_ids(curr_sub_idx))), ...
	sprintf('mean%s*.img', char(subject_ids(curr_sub_idx))));
  end
  
  coreg(3).other_image{isub} = char(tmp);
end % for isub=


%
%  Specifications for coregistration step --  HIRES to TEMPLATE (canonical avg152T1.img)
%

coreg(4) = coreg(3);

% Specify the target image - TEMPLATE / canoncial
%coreg(4).target_image =spm_get('files', ...
%    sprintf('%s/canonical/', spm_path), sprintf('avg152T1.img'));

for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);
    
  % Specify the target image - TEMPLATE / canoncial
  coreg(4).target_image{isub} =spm_get('files', ...
    sprintf('%s/canonical/', spm_path), sprintf('avg152T1.img')); 
  
  % Specify the first object image -- HIRES
  coreg(4).object_image{isub} = spm_get('files', ...
      sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Select other images to apply transformation -- COPLANAR and EPIs
  coreg(4).other_image{isub} = {};
  tmp = '';
  
  for isess = 1:subj_nsess
    tmp{isess} = spm_get('Files', ... 
	sprintf('%s%s/epi/run%d/', dataroot, char(subject_ids(curr_sub_idx)), isess), ...
	sprintf('%s*.img', char(subject_ids(curr_sub_idx))));
  end

  if ~unix(sprintf('ls %s >> /dev/null', sprintf('%s%s/epi/run1/mean%s*.img', dataroot, char(subject_ids(curr_sub_idx)),char(subject_ids(curr_sub_idx)))))
    tmp{end+1} = spm_get('Files', ... 
	sprintf('%s%s/epi/run1/', dataroot, char(subject_ids(curr_sub_idx))), ...
	sprintf('mean%s*.img', char(subject_ids(curr_sub_idx))));
  end
  
  tmp{end+1} = spm_get('files', ...
      sprintf('%s%s/coplanar/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      );
  
  coreg(4).other_image{isub} = char(tmp); 
  
end % for isub=


%
%  Specifications for normalization step
%

% voxel_def contains the voxelsize : hires, coplanar, epi
% voxel_def=[1.2 0.938 0.938; 0.937 0.938 5; 3.75 3.75 5];
voxel_def=[1.2 0.938 0.938; 0.937 0.938 5; 3.75 3.75 5];

% NORMALIZE (1) - parameters and reslice for HIRES

normalize = struct(... 
    'defaults',  {''}, ...
    'estimates',  1, ...
    'custom_norm', -1, ...
    'sessions', proc_subs, ...
    'option', 3, ...
    'nbsubjects',  nsub_proc, ... 
    'object_masking', 0, ...
    'image', {{''}}, ...
    'objmask', {{''}}, ...
    'matname', {{''}}, ...
    'images', {{''}}, ...
    'template', '/usr/local/matlab/toolbox/local/spm99/templates/T1.img', ...
    'type', 1, ...
    'nonlin_func_nb', 13, ...
    'func_nb', 0, ...
    'nonlin_ite_nb', 12, ...
    'nonlin_regular', 0.01, ...
    'mask_brain', 1, ...
    'interp', -9, ...
    'bounding_box', 3, ...
    'direction1', [], ...
    'direction2', [], ...
    'direction3', [], ...
    'voxel_sizes', 0, ...
    'voxel_sizes_custom', voxel_def(1,:));
    
% bounding_box = bboxes = 1 in default of spm [-78, 78,-112,76,-50,85]
% used = 3 [-90 91 -126 91 -72 109]

for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);
  
  % Specify the image to estimate parameters from (hires?)
  normalize(1).image{isub} = spm_get('files', ...
      sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
  
  % Specify the images to be normalized
  %the hires
%   normalize(1).images{isub} =   spm_get('files', ...
%         sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
%         sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
%         ); 
  normalize(1).images{isub} = '';
end % for isub=


% NORMALIZE (2) - parameters and reslice for COPLANAR   

normalize(2)=normalize(1);
normalize(2).voxel_sizes_custom=voxel_def(2,:);

for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);

  normalize(2).image{isub} = spm_get('files', ...
      sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
   %the coplanar
   normalize(2).images{isub} =  spm_get('files', ...
      sprintf('%s%s/coplanar/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 

end % for isub=


% NORMALIZE (3) - parameters and reslice for EPI

normalize(3)=normalize(1);
normalize(3).voxel_sizes_custom=voxel_def(3,:);
tmp = '';
for isub = 1:nsub_proc
  curr_sub_idx = proc_subs(isub);

  normalize(3).image{isub} = spm_get('files', ...
      sprintf('%s%s/hires/', dataroot, char(subject_ids(curr_sub_idx))), ...
      sprintf('%s*.img', char(subject_ids(curr_sub_idx))) ...
      ); 
   %the epis:
   for isess = 1:subj_nsess
      tmp{isess} = spm_get('Files', ... 
	sprintf('%s%s/epi/run%d/', dataroot, char(subject_ids(curr_sub_idx)), isess), ...
	sprintf('%s*.img', char(subject_ids(curr_sub_idx))));
  end 
  normalize(3).images{isub} = char(tmp);
end  % for isub=    
  

% NORMALIZE (4) - parameters and reslice for EPI with bounding box designed to
% match original EPI volume size

normalize(4)=normalize(3);
normalize(4).bounding_box = 0;
normalize(4).direction1 = [-120;119];
normalize(4).direction2 = [-139;100];
normalize(4).direction3 = [-64;77];


% NORMALIZE (5) - parameters and reslice for EPI with bounding box designed to
% match original EPI volume size

normalize(5)=normalize(3);
normalize(5).bounding_box = 1;		% Default SPM99 bounding box [-78:78
                                        % -112:76 -50:85]
					
					
% NORMALIZE (6) - parameters and reslice for EPI with bounding box designed to
% match original EPI volume size

normalize(6)=normalize(3);
normalize(6).bounding_box = 4;		% SPM99 bounding box 4 [-95:95
                                        % -112:76 -50:95]
					
					
% NORMALIZE (7) - parameters and reslice for EPI with bounding box designed to
% match original EPI volume size

normalize(7)=normalize(3);
normalize(7).bounding_box = 2;		% SPM99 bounding box 'SPM95' [-64:64
                                        % -104:68 -28:72]
					
					

