segment.m 11.9 KB
Newer Older
Martynka's avatar
Martynka committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
clc
clear


cd('\\130.60.169.45\methlab\Neurometric\Antisaccades\code\eeglab14_1_2b')

eeglab;
close all

x = dir('\\130.60.169.45\methlab\ETH_AS\preprocessed2')
subjects = {x.name};
subjects = {subjects{4:end-3}}';
clear x
cd('\\130.60.169.45\methlab\ETH_AS')

   %%
for subj = 347:length(subjects) %186 - BA5 didnt work, 346- BY2
  
    
     datapath = strcat('\\130.60.169.45\methlab\ETH_AS\preprocessed2\',subjects{subj});

    cd (datapath)
    
       if exist(strcat('gip_',subjects{subj},'_AS_EEG.mat')) > 0
            datafile= strcat('gip_',subjects{subj},'_AS_EEG.mat');
            load (datafile)
        elseif exist(strcat('oip_',subjects{subj},'_AS_EEG.mat')) > 0
            datafile= strcat('oip_',subjects{subj},'_AS_EEG.mat');
            load (datafile)
       end 
       
       
       

%% Re-reference to average reference
       EEG = pop_reref(EEG,[])
 
       %% triggers renaming
       countblocks = 1;
       for e = 1:length(EEG.event)
           if strcmp(EEG.event(e).type, 'boundary')
               countblocks = countblocks + 1;
               continue;
           end
           if countblocks == 2 || countblocks == 3 || countblocks == 4 % antisaccade blocks
               if strcmp(EEG.event(e).type,'10  ') % change 10 to 12 for AS
                   EEG.event(e).type = '12  ';
               elseif strcmp(EEG.event(e).type,'11  ')
                   EEG.event(e).type = '13  '; % change 11 to 13 for AS 
               end
               
               if strcmp(EEG.event(e).type,'40  ')
                   EEG.event(e).type = '41  ';
               end
               
           end
       end
       
       EEG.event(strcmp('boundary',{EEG.event.type})) = [];
       rmEventsIx = strcmp('L_fixation',{EEG.event.type});
       rmEv =  EEG.event(rmEventsIx);
       EEG.event(rmEventsIx) = [];
       EEG.event(1).dir = []; %left or right
       EEG.event(1).cond = [];%pro or anti
       %% rename EEG.event.type
       previous = '';
       for e = 1:length(EEG.event)
           if strcmp(EEG.event(e).type, 'L_saccade')
               if strcmp(previous, '10  ')
                   EEG.event(e).type = 'saccade_pro_left'
                   EEG.event(e).cond = 'pro';
                   EEG.event(e).dir = 'left';
                   %pro left
               elseif strcmp(previous, '11  ')
                   EEG.event(e).type = 'saccade_pro_right'
                   EEG.event(e).cond = 'pro';
                   EEG.event(e).dir = 'right';
               elseif strcmp(previous, '12  ')
                   EEG.event(e).type = 'saccade_anti_left'
                   EEG.event(e).cond = 'anti';
                   EEG.event(e).dir = 'left';
               elseif strcmp(previous, '13  ')
                   EEG.event(e).type = 'saccade_anti_right'
                   EEG.event(e).cond = 'anti';
                   EEG.event(e).dir = 'right';
               else
                   EEG.event(e).type = 'invalid';
               end
           end      
           if ~strcmp(EEG.event(e).type, 'L_fixation') ...
                   && ~strcmp(EEG.event(e).type, 'L_blink')
               previous = EEG.event(e).type;
           end
       end
      
%% remove everything from EEG.event which is not saccade or trigger
    
        tmpinv=find(strcmp({EEG.event.type}, 'invalid') | strcmp({EEG.event.type}, 'L_blink')) 
       EEG.event(tmpinv)=[]
       
       %% removing errors
       % if 10 and the sub didn't look left then error
       % pro left sac_start_x > sac_endpos_x --> correct condition
       tmperrsacc1=find(strcmp({EEG.event.type}, 'saccade_pro_left') & [EEG.event.sac_startpos_x]< [EEG.event.sac_endpos_x])
       tmperr1=[tmperrsacc1 (tmperrsacc1-1)]
       EEG.event(tmperr1)=[];
       
       tmperrsacc2=find(strcmp({EEG.event.type}, 'saccade_anti_left') & [EEG.event.sac_startpos_x]> [EEG.event.sac_endpos_x])
       tmperr2=[tmperrsacc2 (tmperrsacc2-1)]
       EEG.event(tmperr2)=[];
       
       tmperrsacc3=find(strcmp({EEG.event.type}, 'saccade_pro_right') & [EEG.event.sac_startpos_x]> [EEG.event.sac_endpos_x])
       tmperr3=[tmperrsacc3 (tmperrsacc3-1)]
       EEG.event(tmperr3)=[];
       
       tmperrsacc4=find(strcmp({EEG.event.type}, 'saccade_anti_right') & [EEG.event.sac_startpos_x]< [EEG.event.sac_endpos_x])
       tmperr4=[tmperrsacc4 (tmperrsacc4-1)]
       EEG.event(tmperr4)=[];
       
       tmperrsacc6=find(strcmp({EEG.event.type}, 'saccade_pro_right') ...
           & [EEG.event.sac_amplitude]<1.5)
       tmperrsacc7=find(strcmp({EEG.event.type}, 'saccade_pro_left') ...
           & [EEG.event.sac_amplitude]<1.5)
       tmperrsacc8=find(strcmp({EEG.event.type}, 'saccade_anti_left') ...
           & [EEG.event.sac_amplitude]<1.5)
       tmperrsacc9=find(strcmp({EEG.event.type}, 'saccade_anti_right') ...
           & [EEG.event.sac_amplitude]<1.5)
       tmperr69=[tmperrsacc6 (tmperrsacc6-1) tmperrsacc7 (tmperrsacc7-1) tmperrsacc8 (tmperrsacc8-1) tmperrsacc9 (tmperrsacc9-1)]
       EEG.event(tmperr69)=[];
       
       clear tmperrsacc1 tmperrsacc2 tmperrsacc3 tmperrsacc4 tmperrsacc6 tmperrsacc7 tmperrsacc8 tmperrsacc9
       
     
  
       %% delete cues where there was no saccade afterwards
       tmpevent=length(EEG.event)
       %start with pro left cue 10
       tmperrsacc10=find(strcmp({EEG.event.type}, 'saccade_pro_left'))
       tmperrcue10=  find(strcmp({EEG.event.type}, '10  '))       
       for iii=1:tmpevent
           try
               while tmperrcue10(iii)~=tmperrsacc10(iii)-1
                   EEG.event(tmperrcue10(iii))=[];

       tmperrsacc10=find(strcmp({EEG.event.type}, 'saccade_pro_left'))
       tmperrcue10=  find(strcmp({EEG.event.type}, '10  '))   
       %                end 
               end
               
           catch
           end
       end
       
      % do for pro right cue 11
       tmperrsacc11=find(strcmp({EEG.event.type}, 'saccade_pro_right'))
       tmperrcue11=find(strcmp({EEG.event.type}, '11  '))
       for iii=1:tmpevent
           try
               while tmperrcue11(iii)~=tmperrsacc11(iii)-1

%                if tmperrcue11(iii)~=tmperrsacc11(iii)-1
                   EEG.event(tmperrcue11(iii))=[];

       tmperrsacc11=find(strcmp({EEG.event.type}, 'saccade_pro_right'))
       tmperrcue11=  find(strcmp({EEG.event.type}, '11  '))  
               end
              
           catch
           end
       end
       
       % do for anti right cue 13
       tmperrsacc13=find(strcmp({EEG.event.type}, 'saccade_anti_right'))
       tmperrcue13=   find(strcmp({EEG.event.type}, '13  '))
       for iii=1:tmpevent
           try
               while tmperrcue13(iii)~=tmperrsacc13(iii)-1
                   EEG.event(tmperrcue13(iii))=[];

       tmperrsacc13=find(strcmp({EEG.event.type}, 'saccade_anti_right'))
       tmperrcue13=   find(strcmp({EEG.event.type}, '13  '))              
               end
           catch
           end
       end
       
       %do for anti left cue 12
       tmperrsacc12=find(strcmp({EEG.event.type}, 'saccade_anti_left'))
       tmperrcue12=find(strcmp({EEG.event.type}, '12  '))
       for iii=1:tmpevent
           try
               while tmperrcue12(iii)~=tmperrsacc12(iii)-1
                   EEG.event(tmperrcue12(iii))=[];
                   
       tmperrsacc12=find(strcmp({EEG.event.type}, 'saccade_anti_left'))
       tmperrcue12=find(strcmp({EEG.event.type}, '12  '))
               end
           catch
           end
       end
  clear tmperrsacc10 tmperrsacc11 tmperrsacc13 tmperrsacc12
  
  
    
%% delete saccades and cues when the saccade comes faster than 100ms after cue
tmpevent=length(EEG.event)
saccpro=find(strcmp({EEG.event.type},'saccade_pro_right')==1 | strcmp({EEG.event.type},'saccade_pro_left')==1)% find rows where there is a saccade
saccanti=find(strcmp({EEG.event.type},'saccade_anti_right')==1 | strcmp({EEG.event.type},'saccade_anti_left')==1);%find rows where there is a saccade

for b=1:size(saccpro,2)
    
    if (EEG.event(saccpro(1,b)).latency-EEG.event(saccpro(1,b)-1).latency)<50 %50 because 100ms
        EEG.event(saccpro(b)).type='micro'; %saccade
        EEG.event(saccpro(b)-1).type = 'micro'; %cue
    end
end   
    
for b=1:size(saccanti,2)

    if (EEG.event(saccanti(b)).latency-EEG.event(saccanti(1,b)-1).latency)<50;
        EEG.event(saccanti(b)-1).type ='micro';
        EEG.event(saccanti(b)).type ='micro';
    end
    
end

    tmpinv=find(strcmp({EEG.event.type}, 'micro')) ;
    EEG.event(tmpinv)=[];
  %% reaction time


saccpro=find(strcmp({EEG.event.type},'saccade_pro_right')==1 | strcmp({EEG.event.type},'saccade_pro_left')==1)% find rows where there is a saccade
saccanti=find(strcmp({EEG.event.type},'saccade_anti_right')==1 | strcmp({EEG.event.type},'saccade_anti_left')==1);%find rows where there is a saccade

trialinfoprosaccadelocked.reactsacc=[]
trialinfoantisaccadelocked.reactsacc=[]

for iii=1:length(EEG.event)
    try
        tmpreactsaccpro=(EEG.event(saccpro(1,iii)-1).latency-EEG.event(saccpro(1,iii)).latency)*2
        trialinfoprosaccadelocked.reactsacc=[trialinfoprosaccadelocked.reactsacc tmpreactsaccpro]
        tmpreactsaccantisaccadelocked=(EEG.event(saccanti(1,iii)-1).latency-EEG.event(saccanti(1,iii)).latency)*2
        trialinfoantisaccadelocked.reactsacc=[trialinfoantisaccadelocked.reactsacc tmpreactsaccantisaccadelocked]
    catch
    end
end
% 

tmp=find(strcmp({EEG.event.type}, '11  ') | strcmp({EEG.event.type}, '10  '))
right= find(strcmp({EEG.event(tmp).type},'11  ')==1);
left= find(strcmp({EEG.event(tmp).type},'10  ')==1);

trialinfopro.cues  = nan(length(tmp),1);
trialinfopro.cues(left)= 1;
trialinfopro.cues(right)= 2;

tmp1=find(strcmp({EEG.event.type}, '12  ') | strcmp({EEG.event.type}, '13  '))
right= find(strcmp({EEG.event(tmp1).type},'12  ')==1);
left= find(strcmp({EEG.event(tmp1).type},'13  ')==1);

trialinfoanti.cues  = nan(length(tmp1),1);
trialinfoanti.cues(left)= 1;
trialinfoanti.cues(right)= 2;

%% how many saccades in pro and antisaccade task for epoching to saccades

indsaccadepro = find(strcmp({EEG.event.type},'saccade_pro_left')==1 | strcmp({EEG.event.type},'saccade_pro_right')==1);
indsaccadeanti = find(strcmp({EEG.event.type},'saccade_anti_left')==1 | strcmp({EEG.event.type},'saccade_anti_right')==1);

% do pro
leftsaccpro= find(strcmp({EEG.event(indsaccadepro).type},'saccade_pro_left')==1);
rightsaccpro  = find(strcmp({EEG.event(indsaccadepro).type},'saccade_pro_right')==1);
trialinfoprosaccadelocked.saccades  = nan(length(indsaccadepro),1);
trialinfoprosaccadelocked.saccades(leftsaccpro)= 1;
trialinfoprosaccadelocked.saccades(rightsaccpro)= 2;
% do anti
leftsaccanti = find(strcmp({EEG.event(indsaccadeanti).type},'saccade_anti_left')==1);
rightsaccanti  = find(strcmp({EEG.event(indsaccadeanti).type},'saccade_anti_right')==1);
trialinfoantisaccadelocked.saccades  = nan(length(indsaccadeanti),1);
trialinfoantisaccadelocked.saccades(leftsaccanti)= 1;
trialinfoantisaccadelocked.saccades(rightsaccanti)= 2;

%%     epoching
EEGprocuelong = pop_epoch(EEG, {'10','11'}, [-2, 3]);
EEGanticuelong = pop_epoch(EEG, {'12','13'}, [-2, 3]);

%how many epochs
trialinfoprolong.epochs=size(EEGprocuelong.data, 3)
% trialinfoanti.epochs=size(EEGanticue.data, 3)
%       trialinfoprosaccadelocked.leftepochs=size(EEGprosaccleft.data, 3)
%       trialinfoprosaccadelocked.rightepochs=size(EEGprosaccright.data, 3)
% trialinfoprosaccadelocked.epochstotal=size(EEGprosacc.data, 3)

%       trialinfoantisaccadelocked.leftepochs=size(EEGantisaccleft.data, 3)
%       trialinfoantisaccadelocked.rightepochs=size(EEGantisaccright.data, 3)
% trialinfoantisaccadelocked.epochstotal=size(EEGantisacc.data, 3)



%% save epoched data
     save EEGprocuelong EEGprocuelong  
%      save EEGanticue EEGanticue
%      save EEGprosacc EEGprosacc
%      save EEGantisacc EEGantisacc         
%      save EEGprosacc EEGprosaccleft EEGprosaccright EEGprosacc
%      save EEGantisacc EEGantisaccleft EEGantisaccright EEGantisacc      
%      save trialinfo trialinfopro trialinfoanti
     save trialinfocuelockedlong trialinfoprolong %trialinfoanti
%      save trialinfosaccadelocked trialinfoprosaccadelocked trialinfoantisaccadelocked
end