function [onsetTimeVector activeMat activeTP]=nic_TCA(maskIN,imagePath,...
          imageFilter,Samp,Z0,percentThreshold,Case)
% An altered Temporal Clustering Analysis (TCA) method which could be used
% to calculate the spontaneous time point(onesetTimeVector) in resting
% state fMRI data(ImageIN)
%
% FORMAT [onsetTimeVector activeMat activeTP]=nic_TCA(maskIN,imagePath,...
% imageFilter,volumeIndex,Samp,Z0,percentThreshold,Case)
% Input:
% 1. maskIN - the path and filename of mask file, [path\*.img]
% 2. imageFilter - the path of image file, [path]
% 3. imageFilter- filter the files according to their types
% 4. Samp - the low-sampling rate for resampling the time course, in # of TR
%           e.g: 5
% 5. Z0 - the threshold of activation
% 6. percentThreshold - set the percentage of the voxels with Z>Z0 at t0
%           which appeared in the set of voxels with Z>Z0 at higher ranked
%           time points defined as activation, e.g: 0.3
% 7.Case - selecting the method to get spontaneous activation time course
%           [1,2,or 3]
%        1- Find only one reference time point, which has the greatest
%           total voxels of Z>Z0, and exceeds gaussian assumption, Other
%           time points had to satisfy 2 criteria in order to be considered
%           as Spon-TPs:
%           (a) The number of voxels with Z > 2 at this time point exceeded
%               2.5% of the total voxel number in the ROI.
%           (b) A large proportion (more than 30%) of the voxels with Z > 2
%               at this time point must have appeared in the set of voxels
%               with Z > 2 at higher ranked time points that have been
%               defined as Spon-TPs. We added this criterion to avoid
%               misinterpreting noise as spontaneous activations.
%           NOTE: if a time point is excluded, all lower-ranked time points
%           are excluded, too.
%       2 - like the case 1, except that if a time point is excluded, the
%           lower-ranked time points still have chance to be selected as
%           spontaneous time points, as long as they comply with the rules
%           a) and b)
%       3 - Find two separate reference time points, the first one has the
%           greatest total voxels of Z>Z0, and exceeds gaussian
%           assumption;the second one was the first time point whose voxels
%           with Z>2 appeared in the set of voxels with Z>2 at the first one
%           is less than 30%.
%           other time points was defined as Spon-TPs of reference 1 or 2
%           follows the rule in Case 2.
% Output:
% 1. onsetTimeVector - the vector which shows activation time point (i.e. onset time)
%                      when the element = 1, active; when = 0, non-active
% 2. activeMat - matrix contains active voxel at all time point, NOTE: not the
%                final result, just for show
% 3. activeTP - list the # of activation voxel in onsetTimeVector
%_______________________________________________________________________
% Copyright (C) 2007 Neuroimage Computering Group, State Key Laboratory of
% Cognitive Neuroscience and Learning
%
% Shuangye Ma, mashuangye@gmail.com
% @(#)nic_TCA.m  ver 1.0, 07/11/15
% Modified by Han Zhang, napoleon1982@gmail.com
% @(#)nic_TCA.m  ver 1.1, 07/11/19
%
% Reference: Hunter et al, PNAS, 2005

[Mask,Head]=nic_ReadNiftiImage(maskIN);
nic_ShowOrientation(Head);

mask=reshape(Mask,1,[]);
filearray=nic_spm_select('List',imagePath, ['^' imageFilter '.*\.img$']);
SamplingSize=Samp;
LowSamp=[1:SamplingSize:size(filearray,1)];
filearray=filearray(LowSamp,:);
N=size(filearray,1);
DATA=[];

for i=1:N
    imageIN=[imagePath,'\',filearray(i,:)];
    [data,Head]=nic_ReadNiftiImage(imageIN);
    if(i==1)
        nic_ShowOrientation(Head);
    end
    data1=reshape(data,1,[]);
    DATA=[DATA;data1(find((data1.*mask)~=0))];
end

% call the onset time course
[onsetTimeVector activeMat activeTP]=nic_TCAcore(DATA,Z0,percentThreshold,Case);

close all;
