%% PPPI Analysis

% PPPI analysis script for running connectivity analysis
% Dependency : SPM12, PPPI toolbox
% Input: 1.SPM.mat file containing Rhyme1, Rhyme2, Semantic1, Semantic2 and Control conditions.[Each subject]
%        2.Roi Directory containing binarized ROIS as .nii files
% Output : PPPI GLM for each ROI withing subject directory

% Created : Dr Avantika Mathur(PhD, PostDoc)
%% 
clear all; clc;
% restore to default path 
%restoredefaultpath;
%if ~isequal(exist('spm'),2)
    %addpath(genpath('/Users/postdoc/Mtools/spm12/'));
%end
%% Toolbox
spm('defaults','fmri')
spm_jobman('initcfg');

%Add PPI toolbox to path
addpath(genpath('/work/nl3wanglab/avantika/fMRI/Scripts/PPI/PPPIv13.1'));

%% Specify the following Inputs
% Path containing data organized as Path/SubId/GLM/SPM.mat
SubPath = '/work/nl3wanglab/avantika/fMRI/long/'; % SubPath = '/Volumes/wangLab/AM/fMRI/long/';
SubId = {'HL001'};%{'HL004';'HL007';'HL010';'HL012';'HL018';'HL023';'HL027';'HL028'};

% Roi Directory containing binarized ROIS as .nii files
RoiDir = '/work/nl3wanglab/avantika/fMRI/Scripts/Dep/ROIS/';% RoiDir = '/Volumes/wangLab/AM/fMRI/Scripts/Dep/ROIS/';
regions = {'LIFG_opercularis_11_bin.nii'; 'LIFG_orbitalis_13_bin.nii'; 'LIFG_triangularis_15_bin.nii'; 'RIFG_opercularis_12_bin.nii'; 'RIFG_orbitalis_14_bin.nii'; 'RIFG_triangularis_16_bin.nii'};

%% PPPI
P = struct;


for isub = 1:numel(SubId)
        %% create voi sphere around subject maxima  
for nvoi = 1:numel(regions)
    maxima_max_dir = fullfile(SubPath, SubId{isub},'GLM/GLM_All','maxima_mask');
    if ~exist(maxima_max_dir,'dir')
        mkdir(maxima_max_dir)
    end

    matlabbatch{1}.spm.util.imcalc.input = {fullfile(SubPath, SubId{isub},'GLM/GLM_All',['mask.nii',',1'])
                                            fullfile(RoiDir,regions{nvoi})};
    matlabbatch{1}.spm.util.imcalc.output = [strcat((strrep(regions{nvoi},'_bin.nii','')),'_modified.nii')];
    matlabbatch{1}.spm.util.imcalc.outdir = {maxima_max_dir};
    matlabbatch{1}.spm.util.imcalc.expression = 'i1.*i2';
    matlabbatch{1}.spm.util.imcalc.var = struct('name', {}, 'value', {});
    matlabbatch{1}.spm.util.imcalc.options.dmtx = 0;                                   
    matlabbatch{1}.spm.util.imcalc.options.mask = 0;    
    matlabbatch{1}.spm.util.imcalc.options.interp = 1;    
    matlabbatch{1}.spm.util.imcalc.options.dtype = 4;
    spm_jobman('run',matlabbatch);
  
    %% Create PPPI
    P.subject = SubId{isub};
    P.directory = fullfile(SubPath,SubId{isub,1},'GLM/GLM_All');% Directory for input SPM.mat file
    P.VOI = fullfile(maxima_max_dir,strcat((strrep(regions{nvoi},'_bin.nii','')),'_modified.nii')); 
    %P.VOI2 = {};
    P.Region = strrep(regions{nvoi},'_bin.nii','');
    P.outdir = fullfile(SubPath,SubId{isub},'GLM/GLM_All','PPI');
    P.Estimate = 1;
    P.contrast = 0;
    P.extract = 'eig';
    P.Tasks = {'0' 'Control'  'Rhyme'  'Semantic'};
    P.Weights=[];
    P.analysis='psy';
    P.method='cond';
    P.CompContrasts=1;
    P.Contrasts = [];
    P.Contrasts(1).Weighted=0;
    P.Contrasts(1).MinEvents=5;
    P.Contrasts(1).name='RhymeVsControl';
    % Remove VOI2
    % S = rmfield(P,P.VOI2);
    %% Con0002 =  SemanticVsControl
    P.Contrasts(2).left={'Semantic'};
    P.Contrasts(2).right={'Control'};
    P.Contrasts(2).STAT='T';
    P.Contrasts(2).Weighted=0;
    P.Contrasts(2).MinEvents=5;
    P.Contrasts(2).name='SemanticVsControl';

    %% Con0003 =  RhymeVsSemantic
    P.Contrasts(3).left={'Rhyme'};
    P.Contrasts(3).right={'Semantic'};
    P.Contrasts(3).STAT='T';
    P.Contrasts(3).Weighted=0;
    P.Contrasts(3).MinEvents=5;
    P.Contrasts(3).name='RhymeVsSemantic';

     %% Con0004 =  SemanticVsRhyme
    P.Contrasts(4).left={'Semantic'};
    P.Contrasts(4).right={'Rhyme'};
    P.Contrasts(4).STAT='T';
    P.Contrasts(4).Weighted=0;
    P.Contrasts(4).MinEvents=5;
    P.Contrasts(4).name='SemanticVsRhyme';
    
    P.FLmask = 1;
    [P, errorval] = PPPI_checkstruct(P);
    PPPI(P);
end
end