preprocessingTEP_pipeline.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. %%% Pipeline case gtec files
  2. clear all
  3. close all
  4. %main_dir = fileparts(mfilename('fullpath')); % path of the script folder
  5. %% paths for PLUG-INs functions
  6. addpath([main_dir '\PLUG-INs\SOUND']);%SOUND
  7. addpath([main_dir '\PLUG-INs\TESA1.1.1']); %TESA
  8. addpath([main_dir '\PLUG-INs\M_functions']); % specific functions for the script
  9. %path of the raw data
  10. %addpath('/raw_data');
  11. % initialize eeglab;
  12. %addpath ('eeglab2022.1/') %% add path eeglab
  13. %eeglab; close; %initialize eeg lab
  14. %initialize fieldtrip
  15. ft_defaults
  16. %% set folders for input and output: Y
  17. %%%% folder with data to be processed
  18. % datafolder_parent = [main_dir '\raw_data']; % parent data folder includes all the subdir of all dataset to be processed
  19. % mkdir(main_dir, '\analysis_MOBI'); % build output folder
  20. % output = [main_dir, '\analysis_MOBI'];
  21. %% Variables
  22. %%control to overwrite steps
  23. global_overwrite=0 ;% 1 to overwrite all steps
  24. prep1_1_overwrite=0; % 1 to overwrite step 1.1
  25. prep1_2_overwrite=0; % 1 to overwrite step 1.2
  26. sound_overwrite=0; %1 to overwrite step 2 SOUND
  27. rej_overwrite=0; % 1 to overwrite step 3 artifact rejection
  28. ICA_overwrite=0; % 1 to overwrite step 4.1 ICA
  29. ICA_remove=0; % 1 to overwrite step 4.2 ICA selection components
  30. SSP_SIR_step1 =0; % 1 to overwrite step 5.1 SSP-SIR
  31. SSP_SIR_step2 =0; % 1 to overwrite step 5.2 SSP-SIR components selection
  32. %%step 1.1
  33. chan_loc_st = 'standard-10-5-cap385.elp';% standard 10-20 channel location
  34. load([main_dir '\PLUG-INs\M_functions\EEG_chanlocs_73' ]); chan_loc=EEG_chanlocs_73;%custom channel location
  35. ref_chan = 2;%row of the reference channel
  36. emg_refchan= [76, 78];
  37. emg_chan = [77, 79];
  38. eeg_channels=73;
  39. tot_cond=7;
  40. epoching_long=[-0.7 0.7];% interval to epoch data, latency in sec relative to time-locking event
  41. rmBase = [-200 -5]; % interval for baseline correction
  42. %%step1.2
  43. interp = [0 3]; %interval to cut and interpolate TMS pulse for TESA method, time in ms related to the event
  44. interp_Win = [0.35,0.35];%times before and after artifact window for fitting cubic function(TESA)
  45. HP_f = 1; % % HP filter frequency
  46. fftflag=1; % filterinf in the frequency domain, faster for high-order filters
  47. DS= 4800; %down sampling frequency
  48. %%step2 SOUND
  49. ref_before_sound= 'FPz';
  50. lambda_value = 0.1; % SOUND parameter
  51. iter = 5;%SOUND parameter
  52. %%step 3 artifact rejection
  53. SD_first=5; %number of stnd dev to consider when computing joint probability and remove trial (first trial rejection)
  54. SD_last=5; %number of stnd dev to consider when computing joint probability and remove trial(last trial rejection)
  55. %%step 4.2 ICA
  56. threshold = [0 0;0 0; 0.5 1; 0 0; 0 0; 0 0; 0 0];% threshold to label eye components (50%)
  57. %%step 5 SSP-SIR
  58. SSP_SIR_window = [0,50];%the time window on which SSP-SIR is applied
  59. limx=[-100 350];% xlim for figure with TEPs
  60. limy = [-30 30]; %ylim for figure with TEPs
  61. %%step 6 final preprocesing
  62. LP_f = 70;% frequency in Hz for lowpass filter
  63. %chan_loc2 = 'standard-10-5-cap385.elp';% standard 10-20 channel location
  64. old_ref = 'FPz';
  65. reref = {'TP9' 'TP10'};
  66. epoching_short = [-0.2 0.4];
  67. latency = [-0.1 0.4]; %epoch limit in final fieldtrip set
  68. ylimit=[-15 15];%ylimit for final figure
  69. %% 0. Get file ready for processing
  70. % % function to build the mat file row data
  71. [subjects,n_subjects]= get_data_MOBI(datafolder_parent,output,ref_chan,emg_refchan,tot_cond);
  72. save('subjects.mat', 'subjects')
  73. save('n_subjects.mat', 'n_subjects')
  74. %% 1. BASIC PREPROCESSING
  75. % Step 1.1
  76. % load set, add channel location, remove unwanted electrodes, define
  77. % reference, save the set
  78. %uncomment these two lines if n_sibjects.mat already exists (and skip/comment
  79. %section 0.):
  80. %load n_subjects.mat
  81. %load subjects.mat
  82. for iSub = 1:n_subjects
  83. subj = subjects{iSub}
  84. load([output '\' subj '\0_Data\Block_cond.mat'])
  85. for n_cond= 1:tot_cond
  86. name_condition = [subj '_' Block_cond{n_cond,2}];
  87. name_folder= [subj '\' name_condition];
  88. mkdir([output, '\' name_folder '\1_Preprocessing\step1_1']);
  89. current_output_folder = [output, '\' name_folder '\1_Preprocessing\step1_1'];
  90. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step1_1.set']) ...
  91. && isfile([current_output_folder '\' Block_cond{n_cond,1} '_EMG_step1_1.set'])...
  92. && global_overwrite==0 && prep1_1_overwrite==0
  93. warning(['skipping step1_1 for ' name_condition ' , file already present'])
  94. else
  95. %load row data
  96. load([output '\' subj '\0_Data\' subj '_' Block_cond{n_cond,2}]);
  97. data = eval([Block_cond{n_cond,2}]);
  98. % load raw data,discard EMG channels,read the trigger,load channel names
  99. [EEG, EMG]= load_rawdata_MOBI(data,name_folder,Block_cond{n_cond,3},Block_cond{n_cond,5},Block_cond{n_cond,4},eeg_channels,chan_loc);
  100. %plot before starting preprocessing signals. Epoching and baseline correction only
  101. %for visual purpose
  102. myplot_F(EEG,[-100 500],[-1000 45000],['Step 1.1 signals'],epoching_long);
  103. saveas(gcf, [current_output_folder '\step1_1'] )
  104. close
  105. EEG = pop_saveset(EEG, 'filename',[Block_cond{n_cond,1} '_step1_1'],'filepath', current_output_folder);
  106. EMG = pop_saveset(EMG, 'filename',[Block_cond{n_cond,1} '_EMG_step1_1.set'], 'filepath',current_output_folder );
  107. end
  108. disp('Step 1.1 ended')
  109. %% STEP 1.2 Remove TMS-pulse artifact and interpolate, HP filter, dwnsampling,epoching
  110. previous_folder= [output, '\' name_folder '\1_Preprocessing\step1_1'];
  111. mkdir([output, '\' name_folder '\1_Preprocessing\step1_2']);
  112. current_output_folder = [output, '\' name_folder '\1_Preprocessing\step1_2'];
  113. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step1_2.set']) && global_overwrite==0 && prep1_2_overwrite ==0
  114. warning(['skipping step1_2 for ' name_condition ' , file already present'])
  115. else
  116. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step1_1.set'],'filepath', previous_folder);
  117. % function to remove TMS pulse with TESA Method
  118. % inputs: EEG, interpolation interval,times before and after artifact window for fitting cubic function
  119. EEG= remove_TMS_TESA_G(EEG,interp,interp_Win);
  120. %plot with epoching and baseline correction only for visual purpose
  121. myplot_F(EEG,[-100 300],[-300 300],['After interpolation TESA'],epoching_long,rmBase);
  122. saveas(gcf, [current_output_folder '\after_interpolation_TESA'])
  123. close
  124. %%%%% High pass filter data
  125. tic
  126. EEG = high_pass_filter(EEG,HP_f,fftflag);
  127. toc
  128. %plot after filtering without baseline correction
  129. myplot_F(EEG,[-100 300],[-500 500],['After filtering'],epoching_long);
  130. saveas(gcf, [current_output_folder '\after_filtering'])
  131. close
  132. % DownSample
  133. EEG = pop_resample(EEG, DS);
  134. myplot_F(EEG,[-100 300],[-500 500],['After resampling'],epoching_long);
  135. saveas(gcf, [current_output_folder '\after_resampling'])
  136. close
  137. % Epoching data
  138. EEG = pop_epoch(EEG, {}, epoching_long);
  139. %save figure
  140. myplot_F(EEG,[-100 300],[-500 500],['Step 1.2']);
  141. saveas(gcf,[current_output_folder '\step1_2'])
  142. close
  143. savename = [Block_cond{n_cond,1} '_step1_2'];
  144. EEG = pop_saveset( EEG, 'filename',savename,'filepath',current_output_folder);
  145. end
  146. disp('Step 1.2 ended')
  147. %% STEP 2 SOUND
  148. % 2.1 - prepare for Sound
  149. previous_folder= [output, '\' name_folder '\1_Preprocessing\step1_2'];
  150. mkdir([output, '\' name_folder '\2_SOUND']);
  151. current_output_folder = [output, '\' name_folder '\2_SOUND'];
  152. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step2_2.set']) && global_overwrite==0 && sound_overwrite==0
  153. warning(['skipping step 2 for ' name_condition ' , file already present'])
  154. else
  155. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step1_2.set'],'filepath',previous_folder);
  156. %function to construct leadfield and to re-referencing
  157. [EEG,LFM_sphere]= before_SOUND_G(EEG,ref_before_sound,chan_loc_st);
  158. % plot butterfly and save before SOUND
  159. myplot_F(EEG,[-100 300],[-50 50],['Before SOUND']);
  160. saveas(gcf,[current_output_folder '\' name_condition '_before_SOUND'])
  161. close
  162. %save dateset and leadfield matrix
  163. %savename = [name_condition '_beforeSound'];
  164. %EEG = pop_saveset(EEG, 'filename',savename,'filepath', current_output_folder);
  165. % save leadfield
  166. save([current_output_folder '\LFM_sphere.txt'], 'LFM_sphere', '-ascii');
  167. disp('Step 2.1 pre-SOUND ended');
  168. %%%% 2.2 SOUND
  169. %function for SOUND algorithm
  170. EEG_clean = SOUND_to_samples(EEG,LFM_sphere,lambda_value,iter);
  171. delete(gcp)
  172. close
  173. EEG = EEG_clean;
  174. % plot butterfly and save after SOUND
  175. myplot_F(EEG,[-100 300],[-50 50],['After SOUND']);
  176. saveas(gcf,[current_output_folder '\step2_2'])
  177. close
  178. %save set after SOUND
  179. EEG.setname= [Block_cond{n_cond,1} '_afterSOUND'];
  180. EEG = pop_saveset(EEG, 'filename',[Block_cond{n_cond,1} '_step2_2'],'filepath', current_output_folder);
  181. end
  182. disp(' step 2.2 SOUND ended')
  183. %% 3 Automated Artifact rejection
  184. previous_folder= [output, '\' name_folder '\2_SOUND'];
  185. mkdir([output, '\' name_folder '\3_Artifact_rejection']);
  186. current_output_folder = [output, '\' name_folder '\3_Artifact_rejection'];
  187. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step3.set']) && global_overwrite==0 && rej_overwrite==0
  188. warning(['skipping step 3 for ' name_condition ' , file already present'])
  189. else
  190. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step2_2.set'],'filepath',previous_folder);
  191. % Automated Trial rejetion
  192. [EEG,rejected_epochs] = reject_artifact(EEG,SD_first,SD_last);
  193. %save rejected epochs
  194. save([current_output_folder '\rejected_epochs'], 'rejected_epochs');
  195. %plot
  196. myplot_F(EEG,[-100 300],[-50 50],['After trial rejction']);
  197. saveas(gcf,[current_output_folder '\after_trialrejction'])
  198. close
  199. % save data after analysisG rejection:
  200. EEG.setname= 'trialrejction';
  201. EEG = pop_saveset(EEG, 'filename',[Block_cond{n_cond,1} '_step3'],'filepath', current_output_folder);
  202. end
  203. disp('step 3 Artifact rejection ended')
  204. %% 4 ICA
  205. % 4.1 RUN ICA
  206. previous_folder= [output, '\' name_folder '\3_Artifact_rejection'];
  207. mkdir([output,'\' name_folder '\4_ICA\4_1_RUNICA']);
  208. current_output_folder = [output,'\' name_folder '\4_ICA\4_1_RUNICA'];
  209. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step4_1.set']) && global_overwrite==0 && ICA_overwrite==0
  210. warning(['skipping step 4.1 for ' name_condition ' , file already present'])
  211. else
  212. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step3.set'],'filepath', previous_folder);
  213. %EEG = pop_tesa_removedata( EEG,interp);
  214. % Ica decomposition
  215. EEG = pop_runica(EEG, 'chanind', [1: size(EEG.data,1)], 'interupt','on');
  216. %save set with ICA all components
  217. EEG.setname= 'After ICA';
  218. savename = [Block_cond{n_cond,1} '_step4_1'];
  219. EEG = pop_saveset(EEG, 'filename',savename,'filepath', current_output_folder);
  220. end
  221. disp('Step 4.1 ICA ended')
  222. end
  223. end
  224. %% 4.2 ICA - Remove ocular artifat with ICA
  225. for iSub = 1:n_subjects
  226. subj = subjects{iSub}
  227. for n_cond =1:tot_cond
  228. load([output '\' subj '\0_Data\Block_cond.mat']);
  229. name_condition = [subj '_' Block_cond{n_cond,2}];
  230. name_folder= [subj '\' name_condition]
  231. previous_folder= [output,'\' name_folder '\4_ICA\4_1_RUNICA'];
  232. mkdir([output, '\' name_folder '\4_ICA\4_2_Remove_ocular_Artifact']);
  233. current_output_folder = [output, '\' name_folder '\4_ICA\4_2_Remove_ocular_Artifact'];
  234. if isfile([ current_output_folder '\' Block_cond{n_cond,1} '_step4_2.set']) && global_overwrite==0 && ICA_remove==0
  235. warning(['skipping step 4.2 for ' name_condition ' , file already present'])
  236. else
  237. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step4_1.set'],'filepath', previous_folder);
  238. Select_ICA_components
  239. end
  240. disp('Step 4.2 ICA selection components ended')
  241. end
  242. end
  243. %% 5. Use SSP-SIR to clean muscle artifacts
  244. % SSP-SIR
  245. for iSub = 1:n_subjects
  246. subj = subjects{iSub}
  247. for n_cond= 1:tot_cond
  248. load([output '\' subj '\0_Data\Block_cond.mat']);
  249. name_condition = [subj '_' Block_cond{n_cond,2}];
  250. name_folder= [subj '\' name_condition]
  251. previous_folder= [output, '\' name_folder '\4_ICA\4_2_Remove_ocular_Artifact'];
  252. mkdir([output, '\' name_folder '\5_SSP-SIR\5_1_SSP-SIR calculations']);
  253. current_output_folder = [output, '\' name_folder '\5_SSP-SIR\5_1_SSP-SIR calculations'];
  254. if size(dir([current_output_folder '/*.set' ]),1)== 6 && global_overwrite==0 && SSP_SIR_step1==0
  255. warning(['skipping step 5.1 for ' name_condition ' , files already present'])
  256. else
  257. % re-load LFM_sphere and EEG from previuos step
  258. LFM_sphere= load([ output '\' name_folder '\2_SOUND\LFM_sphere.txt']);
  259. EEG = pop_loadset('filename',[Block_cond{n_cond,1} '_step4_2.set'],'filepath', previous_folder);
  260. EEG = pop_tesa_removedata( EEG,interp);
  261. % SSP-SIR with TEP with incremental number of removed components, up to 5
  262. SSP_SIR_components(EEG,LFM_sphere,SSP_SIR_window, current_output_folder,name_condition,rmBase,limx,limy)
  263. end
  264. disp('Step 5.1 SSP-SIR and TEPs with incremental number of removed components ended ')
  265. end
  266. end
  267. %% 5.2 SSP-SIR manual components selection
  268. for iSub = 1:n_subjects
  269. subj = subjects{iSub}
  270. for n_cond= 1:tot_cond
  271. load([output '\' subj '\0_Data\Block_cond.mat']);
  272. name_condition = [subj '_' Block_cond{n_cond,2}];
  273. name_folder= [subj '\' name_condition]
  274. previous_folder= [output, '\' name_folder '\5_SSP-SIR\5_1_SSP-SIR calculations'];
  275. mkdir([output, '\' name_folder '\5_SSP-SIR\5_2_SSP-SIR_components']);
  276. current_output_folder = [output,'\' name_folder '\5_SSP-SIR\5_2_SSP-SIR_components'];
  277. % if size(dir([current_output_folder '/*.mat' ]),1)==1 && global_overwrite==0 && SSP_SIR_step2==0
  278. % warning(['skipping step 5.2 for ' subj ' , files already present'])
  279. % else
  280. decide1_SSPSIR_comp_MOBI
  281. % end
  282. disp('Step 5.2 SSP-SIR removed components ended ')
  283. end
  284. end
  285. %% 6. Last step of processing after SSP-SIR
  286. for iSub = 1:n_subjects
  287. subj = subjects{iSub};
  288. for n_cond= 1: tot_cond
  289. load([output '\' subj '\0_Data\Block_cond.mat']);
  290. name_condition = [subj '_' Block_cond{n_cond,2}];
  291. name_folder= [subj '\' name_condition]
  292. savename= [Block_cond{n_cond,1}];
  293. previous_folder= [output,'\' name_folder '\5_SSP-SIR\5_2_SSP-SIR_components'];
  294. mkdir([output, '\' name_folder '\6_final_preprocessing_new_interp']);
  295. current_output_folder = [output,'\' name_folder '\6_final_preprocessing_new_interp'];
  296. if isfile([current_output_folder '\' savename '_TEPs_def.set'])&& ...
  297. isfile([current_output_folder '\' savename '_TEPs_def.mat'])
  298. warning(['skipping step 6 for ' name_condition ' , files already present'])
  299. else
  300. % to know the number of removed components in previuos step
  301. load([previous_folder '\SSPSIR_delcomponents.mat'])
  302. k = SSPSIR_delcomponents(1,1);
  303. % load the set after SSP-SIR with correct number of removed variables
  304. EEG = pop_loadset('filename',[name_condition '_5SSP-SIR_' num2str(k) '.set'],'filepath', [output,'\' name_folder '\5_SSP-SIR\5_1_SSP-SIR calculations']);
  305. EEG.comments=pop_comments(EEG.comments,'','SSP-SIR components evaluated by 2 scientists',1);
  306. %EEG = pop_tesa_interpdata( EEG, 'cubic',interp_Win);
  307. EEG= last_steps_MOBI_new(EEG,savename,LP_f,old_ref,chan_loc_st,rmBase,current_output_folder,epoching_short,reref);
  308. %myplot_F(EEG,[-100 400],[-15 15],['Final']);
  309. %artefact rejection 2 done in folder 6_final_preprocessing
  310. load([output,'/' name_folder '/6_final_preprocessing/artrej2_rejepochs.mat'])
  311. trialrej=zeros([1 size(EEG.data,3)]);
  312. trialrej(artrej2_rejepochs)=1;
  313. EEG = pop_rejepoch(EEG,trialrej,0);
  314. %remove data between 0 and 3
  315. EEG = pop_tesa_removedata( EEG,interp);
  316. myplot_F(EEG,[-100 400],[-20 40],['final remove']);
  317. saveas(gcf,[current_output_folder '/final_remove'])
  318. close
  319. %save
  320. EEG.setname= 'TEPs_def';
  321. EEG = pop_saveset( EEG, 'filename',[savename '_TEPs_def.set'],'filepath',current_output_folder);
  322. % convert to fieldtrip structure and save figure TEPs def
  323. TEP_final2=convert_to_fieldtrip(EEG,latency,current_output_folder,savename,ylimit);
  324. end
  325. disp('Step 6 ended ')
  326. end
  327. end