% launchClustering.m is a script for performing the clustering step within 
% a meta-analysis of neuroimaging data. The script calls the clustering 
% algorithm with default parameters, including input/output filenames, 
% that can be modified by editing this file.
%
% This file is part of Cluster4MetaAnalysis.
%     Cluster4MetaAnalysis is free software: you can redistribute it 
%     and/or modify it under the terms of the GNU General Public License 
%     as published by the Free Software Foundation, either version 3 of 
%     the License, or (at your option) any later version.
% 
%     Cluster4MetaAnalysis is distributed in the hope that it will be 
%     useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
%     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with Cluster4MetaAnalysis.  If not, see
%     <http://www.gnu.org/licenses/>.



%%% Setting Parameters %%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input/Output                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Input and output files are in this folder
IOfolder = '../myData';

% Input File
datafile = 'exampleCoord.txt';

% Output Files
filename1 = 'clusteringData.txt';
filename2 = 'clusteringIDs.txt';

% reading data from input file
data = load([IOfolder filesep datafile]);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clustering Algorithm and Parameters                               %
% Different choices are possible: leave uncommented only the block  %
% regarding the clustering algorithm you want to employ             %
% Edit the relevant parameter values according to your needs.       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% *** Hierarchical Clustering with a unique solution ***
 algorithm = @findOptimizedSolution;
%
% this parameter controls how the cluster dendrogram is cut to extract 
% the clustering solution. Refer to cutDendrogram.m for details about
% the parameter
 cutThreshold = 7.5;
 varargin = {data, cutThreshold};
% ***

% *** standard Hierarchical Clustering %%%
% algorithm = @classicalLinkage;
%
% Refer to linkage.m for the meaning of these parameters
% method = 'ward';
% distance = 'euclidean';
%
% this parameter controls how the cluster dendrogram is cut to extract 
% the clustering solution. Refer to cutDendrogram.m for details about
% the parameter
% cutThreshold = 7.5;
% varargin = {data, method, distance, cutThreshold};
% ***

% *** k-means %%%
% algorithm = @kmeans;
%
% K sets the number of clusters to be built
% K = 30;
% varargin = {data, K};   % type 'help kmeans' for more info on parameters
% ***

% *** INSERT HERE CODE FOR INCLUDING OTHER ALGORITHMS ***
% algorithm = {};
% varargin = {};
% ***


% calling main function
clusterData = [];
[clustID, clusterData]  = feval(algorithm, varargin{:});

if (isempty(clusterData))
    clusterData = computeClusterInfo(data, clustID); 
end


% write to file
f = fopen([IOfolder filesep filename1], 'w');
fprintf(f, '%f %f %f %f %f %f %d\n', clusterData');
fclose(f);
f = fopen([IOfolder filesep filename2], 'w');
fprintf(f, '%d\n', clustID);
fclose(f);

