function pooledmap=pval_Combining(pmap_names,u,method,output_file)
%input
%PMAP_NAMES:  the names of the maps of p-values to combine
%U: the partial conjunction parameter (to find voxels where at least u active)
%METHOD: 'Simes', 'Fisher', 'Stouffer'
%'Simes' for positive dependent maps (e.g. all stimi vs. same control). 
%'Fisher' or 'Stouffer' for independent maps (e.g. different subjects). 
%OUTPUT_FILE: the name of the output file.
%
%PVAL_COMBINING(PMAP_NAMES,U,METHOD,OUTPUT_FILE) calculates a p-value map
%for every partial conjunction parameter in the vector of parameters U,
%using the method specified in METHOD, and save the pooled p-value map in
%an output file with base name OUTPUT_FILE and an extension that specifies
%U. 
%
%Examples
%pmap_names = {'pmap_DP_FP'; 'pmap_SG_FP'; 'pmap_MR_FP'} 
%u=[1; 2; 3] 
%pval_Combining(pmap_names,u,'Fisher','pooledmap_DP');


pmapAll =[];
for i=1:length(pmap_names)
   load(char(pmap_names(i))); 
   pmapAll(:,i) = pmap;
end    

unames ={'u1'; 'u2'; 'u3'; 'u4'; 'u5'; 'u6'; 'u7'; 'u8'; 'u9';'u10';'u11'; 'u12'; 'u13'; 'u14'; 'u15'; 'u16'; 'u17'; 'u18'; 'u19' ; 'u20'};

for i=1:length(u)
   uparam=u(i);
    if strcmp(method, 'Simes')
        pooledmap = pval_Simes(pmapAll, uparam);
    else
        if strcmp(method,'Fisher')
            pooledmap = pval_Fisher(pmapAll, uparam);
        else
            if strcmp(method,'Stouffer')
                pooledmap = pval_Stouffer(pmapAll, uparam);
            else
                error('Method misspecified: specify  one of Simes, Fisher, Stouffer')
            end
        end
    end
    save(char(strcat(output_file,'_',unames(uparam))),'pooledmap');
end
    
