%% Prepare_data_in_different_formats for TFR analyses in each subject
% LAunch different Frequency analysis

clear
overwrite = 1;

subject = [10 11 12 13 14 15 16 17 19 20 21 22 23 24 25 26 27 28 29 30 31 32 34 35]

param.trials_sel = 'all'; %? correct/all % {'MEG'} what trials go in the analysis?
freq_anal = 'TFR'; %  FR TFRpowitc (TFR only to generate fourier specturms), TFRpow (for individual power calculations using the fouriers generated in TFR) 
%param.design = [freq_anal '_2cond2mod1signal_cyc_OPtest']; %'_2cond2mod1signal_cyc' '_Mixcond2mod1signal_cyc' % _2cond2mod2signal_cyc' % '_1cond2mod1signal_cyc' 'FR2cond2mod2signal'; %'TFR2cond2mod2signal'; %'FR2cond2mod2signal'
%param.design = [freq_anal '_Mixcond2mod1signal_cyc']; % '_2cond2mod1signal_cyc_OPtest' '_2cond2mod1signal_cyc' '_Mixcond2mod1signal_cyc' % _2cond2mod2signal_cyc' % '_1cond2mod1signal_cyc' 'FR2cond2mod2signal'; %'TFR2cond2mod2signal'; %'FR2cond2mod2signal'
param.design = [freq_anal '_2cond2mod1signal_cyc']; % '_2cond2mod1signal_cyc_OPtest' '_2cond2mod1signal_cyc' '_Mixcond2mod1signal_cyc' % _2cond2mod2signal_cyc' % '_1cond2mod1signal_cyc' 'FR2cond2mod2signal'; %'TFR2cond2mod2signal'; %'FR2cond2mod2signal'
%param.design = [freq_anal '_2cond2mod2signal_cyc']; % '_2cond2mod1signal_cyc_OPtest' '_2cond2mod1signal_cyc' '_Mixcond2mod1signal_cyc' % _2cond2mod2signal_cyc' % '_1cond2mod1signal_cyc' 'FR2cond2mod2signal'; %'TFR2cond2mod2signal'; %'FR2cond2mod2signal'

%param.design = [freq_anal '_2cond2mod1signal_cyc_OPtest']; % '_2cond2mod1signal_cyc_OPtest' '_2cond2mod1signal_cyc' '_Mixcond2mod1signal_cyc' % _2cond2mod2signal_cyc' % '_1cond2mod1signal_cyc' 'FR2cond2mod2signal'; %'TFR2cond2mod2signal'; %'FR2cond2mod2signal'


if strcmp(freq_anal, 'TFR')
    param.toi  = [-0.6:0.015:0.3]; % el TFR lo hago en ventanas de 0.125 ms
end
param.time = [-1.2 1]; %[-1.125 1.125] [-0.52 -0.02]; %[-1.125 1.0]; %[-0.52 -0.02]; %[-1.125 1.0] % [-0.52 -0.02]; % pre [-0.52 -0.02][0.05 0.55] 
param.freq = 4:1:30; %5:5:60; %2:2:30 %[1:1:40];
param.winlength =  0.2; %[1:1:40];

param.min_number_trials = 10; % very min number of trials for each type of analysis.
filename =  'TFR30Freq_pre2cyc' %'TFR30Freq_pre2cyc' 'TFR30Freq_pre500' 'FR30Freq_new';  %'T30Freq'; % 'Freq_prestim' 'Freq_onsetstim'
  
   
for isubj = subject
  
    %% Select your parameters to save the data in one file
    
    %isubj  = 15
    format.trigger_ref = 'target'; % what is the trigger used as a reference
    
     data_file_name = [filename '_s',num2str(isubj)];  % time_freq_s  'Freq_prestim_s  'Freq_poststim_s
    % Define globals
    global G;
    G = [];
    
    %% General paths
    exp_main_path = '/project/3018029.06';
    
    SUBJS = {['0',num2str(isubj)]}
    curSubj = SUBJS{1}
    
    addpath(genpath( [exp_main_path , filesep 'MEG_analysis' filesep 'AVE_MEG' filesep 'scripts'])); % general processing scripts in this exp.
    
    G.subjPath = [exp_main_path filesep 'MEG' filesep curSubj filesep]; % main subject folder
    G.dataPath = [exp_main_path filesep 'raw', filesep 'sub-' curSubj  filesep]; % subjecf MEG data
   G.outputPath = [G.subjPath ,'func', filesep]; % subject saving artifact and preprocessing matrixes
    G.cfgbehavPath = [exp_main_path filesep 'Behavioral' filesep curSubj filesep] ; % behavioral data of participant
    
    subjscriptsPath = [G.subjPath ,'analysis', filesep]; %  participant specific scripts
    
    
    addpath(genpath('/project/3018029.06/Matlab/MEG_scripts/'));
    
    G.inputPath = [G.dataPath  'ses-meg01' filesep ];
    
     G.file_output = [G.outputPath 'TFR_results' filesep];
    if ~exist(G.file_output,'dir')
        mkdir(G.file_output);
    end
    
    do_analysis = 0;
    n = 0;
%              %% deleting useless files     
%       files = dir(fullfile( G.file_output, 'TFR30Freq_s*.mat'))
%       for K = 1 : length( files )
%           thisfile = fullfile(G.file_output, files(K).name);
%           delete(thisfile);
%       end
%   
%       



    if exist([G.file_output data_file_name,'.mat'],'file')
        load([G.file_output data_file_name,'.mat']);
        % file exist; checking whether the analyses is new...
        
        number_of_datasets = size(TFR_var,1);
        disp(['there are ' num2str(number_of_datasets) ' datasets in this participant file'])
        
        
        for i = 1 : number_of_datasets % lets evaluate whether if we have performed this data transformation for this subject before+
            data_exist = 0
            if strcmp(num2str(TFR_var(i).format.trigger_ref) ,num2str(format.trigger_ref));  data_exist = 1; end;
            if (data_exist > 0 & overwrite == 0) ; error(['this data have been already stored this way in dataset number: ' num2str(i)]); break; end;
            if (data_exist > 0 & overwrite == 1); do_analysis = 1; n = i;  end; % overwrite in case that this is the dataset repeated
            if (i == number_of_datasets & data_exist == 0); do_analysis = 1;
                n = i + 1; end; % if we did not find this analyses already performed, save data in the next folder.
        end
        
    else
        disp(['No datasets for this participant, creating first analysis'])
        TFR_var = [];
        do_analysis = 1;
        n = 1;
    end


    if do_analysis
        fun_cfg = [];      
        fun_cfg.G = G;
        fun_cfg.n = n;
        fun_cfg.param = param;
        fun_cfg.format = format;
        fun_cfg.data_file_name = data_file_name;
        %selecte the type of analysis as a function of the design. Here you
        %could send the job to the cluster
        
        if strcmp(param.design, 'TFR_2cond2mod2signal')
            %TFR_2cond2mod2signal_prep(fun_cfg)
            qsubfeval( ...
                'TFR_2cond2mod2signal_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 40*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
        if strcmp(param.design, 'TFR_2cond2mod2signal_cyc')
            %TFR_2cond2mod2signal_cyc_prep(fun_cfg)
            qsubfeval( ...
                'TFR_2cond2mod2signal_cyc_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 50*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
        
        
        if strcmp(param.design, 'TFR_1cond2mod1signal')
            %TFR_1cond2mod1signal_prep(fun_cfg)
            qsubfeval( ...
                'TFR_1cond2mod1signal_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 60*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
            
        if strcmp(param.design, 'TFR_1cond2mod1signal_cyc')
            %TFR_1cond2mod1signal_cyc_prep(fun_cfg)
            qsubfeval( ...
                'TFR_1cond2mod1signal_cyc_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 90*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
           
            
            
              if strcmp(param.design,  'TFR_2cond2mod1signal_cyc_OPtest')
            %TFR_2cond2mod1signal_cyc_OPtest_prep(fun_cfg)
            
            qsubfeval( ...
                'TFR_2cond2mod1signal_cyc_OPtest_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 300*60, ... % 300 minutos
                'timoverhead', 0);
              end
        
              
        if strcmp(param.design, 'TFR_2cond2mod1signal_cyc')
            %TFR_2cond2mod1signal_cyc_prep(fun_cfg)
            qsubfeval( ...
                'TFR_2cond2mod1signal_cyc_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 90*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
                if strcmp(param.design, 'TFR_Mixcond2mod1signal_cyc')
            %TFR_Mixcond2mod1signal_cyc_prep(fun_cfg)
            qsubfeval( ...
                'TFR_Mixcond2mod1signal_cyc_prep', ...
                fun_cfg, ...
                'memreq', 30*1024^3, ...  % 90 gbytes
                'memoverhead', 0, ...
                'timreq', 30*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
       
            
        if strcmp(param.design, 'TFRpowitc_2cond2mod2signal')
            %TFRpowitc_2cond2mod2signal_prep(fun_cfg)
            qsubfeval( ...
                'TFRpowitc_2cond2mod2signal_prep', ...
                fun_cfg, ...
                'memreq', 50*1024^3, ...  % 90 gbytes
                '5:5:50 %memoverhead', 0, ...
                'timreq', 80*60, ... % 300 minutos
                'timoverhead', 0);
        end
        
      
    end
        
end

