%% Modified by Leehyun Yoon 20161111 (Original ver. Bob Spunt, 20111107)

%% ==== Let's make VOI (spehre image)
clc; clear all; close all;
subjectDir = {'Parti_1', 'Parti_2','Parti_3','Parti_4','Parti_5','Parti_6','Parti_7','Parti_8','Parti_9', 'Parti_10', 'Parti_11','Parti_12','Parti_13','Parti_14','Parti_15','Parti_16','Parti_17','Parti_18','Parti_19', 'Parti_21','Parti_22','Parti_23','Parti_24','Parti_25','Parti_26','Parti_27','Parti_28','Parti_29','Parti_30', 'Parti_31', 'Parti_32', 'Parti_33', 'Parti_34', 'Parti_35', 'Parti_36', 'Parti_37', 'Parti_38', 'Parti_40', 'Parti_41', 'Parti_42', 'Parti_43', 'Parti_44', 'Parti_45', 'Parti_46', 'Parti_47', 'Parti_48', 'Parti_49', 'Parti_50', 'Parti_51', 'Parti_52', 'Parti_53', 'Parti_54', 'Parti_55', 'Parti_56', 'Parti_57', 'Parti_58', 'Parti_59', 'Parti_60'}; % subject name
% loop for multi subjects
baseDir = '/Volumes/MGTEC/NFR_60Devel_Analysis';
    cd (baseDir)   
    addpath('/Volumes/MGTEC/NFR_60Devel_Analysis/Parti_1/Analysis_10th/cnpFB_parametric_onlythisparameter');
    create_sphere_image('SPM.mat', [-24 60 0], 'IntermediateFPC_-24_50_0_r0', 0);   

clear all; 

% USER INPUT: Paths, file-finding patterns, and analysis parameters
%---------------------------------------------------------------------%

% -----------Paths-----------
gPPIpath = '/Users/YoonLeehyun/Documents/PPPIv13.1/PPPIv13'; % path for gPPI toolbox
baseDir = '/Volumes/MGTEC/NFR_60Devel_Analysis'; % path for study directory
spmpath=fileparts(which('spm.m'));
spm('defaults','fmri');

% maskpath = '/Applications/fmri/data/matchrest/_rois_'; % path for folder containing ROIs
maskpath = '/Volumes/MGTEC/NFR_60Devel_Analysis'; % path for folder containing ROIs
level1path = 'Analysis_10th/cnpFB_parametric_onlythisparameter'; % path of level analysis (relative to subject folder)


% -----------Patterns for finding files and folders -----------

% subjectpattern = 'Parti_*'; % pattern for finding subject folders (use wildcards)

subjectDir = {'Parti_1', 'Parti_2','Parti_3','Parti_4','Parti_5','Parti_6','Parti_7','Parti_8','Parti_9', 'Parti_10', 'Parti_11','Parti_12','Parti_13','Parti_14','Parti_15','Parti_16','Parti_17','Parti_18','Parti_19', 'Parti_21','Parti_22','Parti_23','Parti_24','Parti_25','Parti_26','Parti_27','Parti_28','Parti_29','Parti_30', 'Parti_31', 'Parti_32', 'Parti_33', 'Parti_34', 'Parti_35', 'Parti_36', 'Parti_37', 'Parti_38', 'Parti_40', 'Parti_41', 'Parti_42', 'Parti_43', 'Parti_44', 'Parti_45', 'Parti_46', 'Parti_47', 'Parti_48', 'Parti_49', 'Parti_50', 'Parti_51', 'Parti_52', 'Parti_53', 'Parti_54', 'Parti_55', 'Parti_56', 'Parti_57', 'Parti_58', 'Parti_59', 'Parti_60'}; % subject name

%subTAG = [1:19, 21:38, 40:60]; %% 

maskpattern = '*nii'; % pattern for finding image files in ROI directory


% -----------General PPI analysis parameters-----------
spmversion = 8; % version of spm (5 or 8)
estimateTAG = 1; % estimate the level 1 models? (1 = yes, 0 = no)
ppi_folder_affix = 'cond'; % string to affix to name of your PPI analysis folder


% -----------Defining the conditions  (psychological effects)-----------
method = 'cond'; % 'trad' for traditional method, 'cond' for generalized (condition-specific) method
conditions = {'FB.cnpFB'}; % conditions to compute PPIs for (must match names used in level 1 analysis)

% weights = [1]; % weights to apply to conditions in traditional PPI (ignore if doing condition-spec)


% -----------Definining the seed timecourse (physiological effect)-----------
% fcontrast = 0; % in my case, I will do default advised by https://www.nitrc.org/forum/message.php?msg_id=16257 . F contrast to adjust for (corresponds to number of the ess image in level 1 folder)
extract='eig'; % extract timecourse as first eigenvariate ('eig') or mean eigenvariate ('mean') default is eig
masks={'IntermediateFPC_-24_50_0_r0_mask.nii'}; % name of images to use to constrain definition of the seed region
threshold = [0.99]; % values to threshold the images above at (number of values must match number of masks)
VOImin = 0;  % minimum number of voxels to accept a seed region as valid

%---------------------------------------------------------------------%
addpath(gPPIpath)

% Find subjects
%---------------------------------------------------------------------%
cd(baseDir);
fprintf('\nSUBJECT LIST:\n');
d=subjectDir;
for i=1:length(d)
    subnam{i}=d(i);
    subdir{i} = [baseDir '/' subnam{i}];
%     fprintf('\tAdding %s to subject list\n',subnam{i})
end

nsubs = length(subnam);
dosubs= 1:nsubs;

% Find mask files
%---------------------------------------------------------------------%
cd(maskpath)
fprintf('\n\nMASK LIST:\n');
d=dir(maskpattern);
for i=1:length(d)
    voiNAMES{i}=d(i).name;
    voi_anal_names{i} = voiNAMES{i}(1:end-4);
  %  fprintf('\tAdding %s to mask list\n',voiNAMES{i})
end
nmasks = length(voiNAMES);
domasks = 1:nmasks;
%---------------------------------------------------------------------%

%---------------------------------------------------------------------%
% Begin looping through regions and subjects
%---------------------------------------------------------------------%
for v=domasks
    for i=dosubs
        try
            cd(gPPIpath)
         %  load('parameters.mat')
            P.subject=subnam{i};
            P.VOI.VOI = [maskpath filesep voiNAMES{v}];
            P.VOI.masks = masks;
            P.VOI.thresh = threshold;
            P.VOI.min = VOImin;
            P.VOImin = VOImin;
            P.Region = [voi_anal_names{v} '_' ppi_folder_affix];
            P.SPMver = spmversion;
            P.directory=[subdir{i} filesep level1path];
            P.method = method;
            P.Estimate = estimateTAG;
           % P.contrast = fcontrast; %% I would do this default 
            P.extract = extract;     
            if strcmp(method,'trad')
                P.Tasks = conditions;
                P.Weights = weights;
                P.Contrasts(1).name = 'PPI';
                P.Contrasts(1).left = convec;
                P.Contrasts(1).right = [];
            else
                P.Tasks = ['0' conditions];
            
            end
            
            if strcmp(method,'cond')
                
            % USER INPUT: Specify your contrasts of interest (Cond PPI only)
            %--------------------------------------------------------------%
%             
                 P.Contrasts(1).name = 'FB.cnpFB';
                 P.Contrasts(1).left = {'FB.cnpFB'};
                 P.Contrasts(1).right = {'none'};  


            %--------------------------------------------------------------%
            end
            % now run! 
            PPPI(P)

    end
end
end