spikeTrigAvg_postsyn_curveset.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. function spikeTrigAvg_postsyn_curveset(files, varargin)
  2. %% Calculate spike triggered average of the postsynaptic response
  3. % For each spike in the stimulated cell the membrane potential of the not
  4. % stimulated cell is gathered and summed up from 5 ms before to 30 ms after
  5. % the spike. Devided by the number of spikes the averaged postsynaptic
  6. % membrane potential is obtained. If there is a clear peak (due to EPSPs
  7. % for example), the time of this peak is determined as the latency of the
  8. % reaction to the spike in the other cell. The latency and the number of
  9. % used spikes are returned. Alternatively the result can be plotted. The
  10. % function can handle multiple files at a time.
  11. % -------------------------------------------------------------------------
  12. % Input [additional arguments are positional,
  13. % ----- optional arguments are name-value pairs]
  14. %
  15. % file: number(s) of the '.mat' file(s) in the directory,
  16. % type: integer or vector
  17. % trials: number(s) of the trials to be analysed, type: integer
  18. % (additional) of vector, default: 'all'
  19. % Filter: select between three filter options:
  20. % (optional) 'hn' applys a hum-noise-remove filter
  21. % 'hnhfn' additonally applys a low pass filter to
  22. % remove high frequency noise
  23. % If not set, no filter will be applyed (default).
  24. % MinPeakPromT: set the value for 'MinPeakProminence' which is used in
  25. % (optional) the 'findpeaks' function to find T-cell peaks.
  26. % Default: 17
  27. % StimCell: set whether the data for a stimulated T cell or a
  28. % (optional) stimulated interneuron should be used.
  29. % 'INT' for the interneuron
  30. % 'T' for the T-cell (default)
  31. % ------
  32. % Output
  33. % ------
  34. % varargout{1}: latency from spike of the stimulated cell to peak in
  35. % average response of the not stimullated cell (no plot)
  36. % type: float (single file) or vector (multiple
  37. % files)
  38. % varargout{2}: number of spikes used from the stimulated cell
  39. % type: float (single file) or vector (multiple
  40. % files)
  41. %
  42. % visually output of the plot, if no output variable set
  43. % -------------------------------------------------------------------------
  44. % Program by: Bjarne Schultze [last modified 09.03.2022]
  45. % -------------------------------------------------------------------------
  46. %% Preparations
  47. % parse the user input
  48. [file_nums, trials, filter_choice, ~, MinPeakPromT, ...
  49. stimCell, ~,~,~] = parse_input_peakfun(files, varargin{:});
  50. % extract the requested data
  51. recData = extract_data(file_nums, filter_choice);
  52. % load a file with color values (shades)
  53. load("../AdditionalFiles/colorShades.mat", "colorShades")
  54. % assign variables to check the min and max values of the y-data
  55. minY = 100; maxY = -100;
  56. %% Main calculations
  57. % iterate through the single files as requested by the input
  58. for file = 1:length(file_nums)
  59. % get the number or trials for the current file
  60. trial_count = size(recData{file,1}.recordingT,2);
  61. % set the length of the time frame to gather the triggered data
  62. trig_len = 0.03; % in seconds
  63. trig_len_dp = trig_len * 10000; % in datapoints
  64. % distinguish between a stimulated T cell and a stimulated interneuron
  65. if isequal(stimCell, "INT")
  66. % get the columns with data for a stimulated interneuron
  67. stimulated = setdiff(1:trial_count, recData{file,1}.stimulatedT(:), ...
  68. 'stable');
  69. % select the requested trials, ensure only valid trials
  70. if ~isequal(trials, 'all')
  71. stimulated = intersect(stimulated,trials);
  72. end
  73. % set an appropriate plot title
  74. plot_title = sprintf("Postsynaptic Spike Response - File: " + ...
  75. "%02.f - Interneuron stimulated", file_nums(file));
  76. % assign the data to working variables
  77. recDataNSTIM = recData{file,1}.recordingT(:,stimulated);
  78. recDataSTIM = recData{file,1}.recordingINT(:,stimulated);
  79. else
  80. % get the columns with data for a stimulated T-cell
  81. stimulated = recData{file,1}.stimulatedT(:);
  82. % select the requested trials, ensure only valid trials
  83. if ~isequal(trials, 'all')
  84. stimulated = intersect(stimulated,trials);
  85. end
  86. % set an appropriate plot title
  87. plot_title = sprintf("Postsynaptic Spike Response - File: " + ...
  88. "%02.f - T-cell stimulated", file_nums(file));
  89. % assign the data to working variables
  90. recDataSTIM = recData{file,1}.recordingT(:,stimulated);
  91. recDataNSTIM = recData{file,1}.recordingINT(:,stimulated);
  92. MinPeakPromSTIM = MinPeakPromT;
  93. end
  94. %% Main calculations
  95. % pre-define a variable to sum up the triggered membrane potentials
  96. triggered_sum = 0;
  97. spike_counter = 0;
  98. % set up a figure window for the curvesets of each file
  99. figure();
  100. % iterate through the trials
  101. for exp = 1:length(stimulated)
  102. % assign a variable for the curve set data
  103. triggered_data_trial = 0; spike_counter_trial = 0;
  104. % search for spikes in the stimulated cell
  105. if isequal(stimCell, 'INT')
  106. [~,spike_indexSTIM_all] = findIntSpikes(recDataSTIM(:,exp));
  107. else
  108. [~, spike_indexSTIM_all] = findpeaks(recDataSTIM(:,exp), ...
  109. 'MinPeakProminence', MinPeakPromSTIM);
  110. end
  111. % exclude spikes at the very beginning and the end of one recording
  112. % to ensure a complete time frame for the triggered data
  113. spike_indexSTIM_sel = spike_indexSTIM_all > 50 & ...
  114. spike_indexSTIM_all < (size(recDataSTIM,1) - trig_len_dp);
  115. spike_indexSTIM = spike_indexSTIM_all(spike_indexSTIM_sel);
  116. % if no spikes are found the next trial is examined
  117. if isempty(spike_indexSTIM)
  118. continue
  119. else
  120. % select the membrane potentail of the not-stimulated cell in
  121. % an index frame of the length 'trig_len_dp' datapoints for
  122. % each spike of the stimulated cell
  123. for spike = 1:length(spike_indexSTIM)
  124. % select the data (triggered by a spike)
  125. triggered_data = ...
  126. recDataNSTIM((spike_indexSTIM(spike)-50):...
  127. (spike_indexSTIM(spike) + trig_len_dp), exp);
  128. % sum up the data
  129. triggered_sum = triggered_sum + triggered_data;
  130. % sum up the data for each trial, then restart
  131. triggered_data_trial = triggered_data_trial + ...
  132. triggered_data;
  133. end
  134. % count the spikes
  135. spike_counter = spike_counter + spike;
  136. % count the spikes for one trial, then restart
  137. spike_counter_trial = spike_counter_trial + spike;
  138. end
  139. % create the curve set
  140. afterSpkAvg_trial = triggered_data_trial ./ spike_counter_trial;
  141. % create an appropriate time vector
  142. timeVector = [sort(-recData{file,1}.timeVector(1:50)); 0;...
  143. recData{file,1}.timeVector(1:trig_len_dp)];
  144. timeVector = timeVector * 1000; % in ms
  145. % set the color shades, getting brighter for later trials
  146. if exp > length(colorShades.purple(:,1))
  147. plot_color = colorShades.purple(end,:);
  148. else
  149. plot_color = colorShades.blue(exp,:);
  150. end
  151. % plot the data
  152. hold on;
  153. plot(timeVector,afterSpkAvg_trial,"Color", plot_color, ...
  154. 'LineWidth', 1)
  155. hold off;
  156. % check for the min and max of the y-data
  157. if min(afterSpkAvg_trial) < minY
  158. minY = min(afterSpkAvg_trial);
  159. end
  160. if max(afterSpkAvg_trial) > maxY
  161. maxY = max(afterSpkAvg_trial);
  162. end
  163. ylim([minY,maxY])
  164. if isequal(exp,1)
  165. xline(0, 'Color', [0.6 0.6 0.6])
  166. end
  167. end
  168. % calculate the averaged postsynaptic membrane potential if more than 8
  169. % spikes were found in the T cell
  170. if spike_counter < 8
  171. fprintf("Not enough T cell spikes (n < 8)!\n")
  172. title(sprintf("File: %g - Not engough T cell spikes (n < 8)!",...
  173. file_nums(file)))
  174. xlabel("time after spike [ms]");
  175. ylabel("average membrane potentail [mV]");
  176. else
  177. afterSpkAvg = triggered_sum ./ spike_counter;
  178. hold on;
  179. plot(timeVector, afterSpkAvg, 'Color', colorShades.green(1,:), ...
  180. 'LineWidth', 2);
  181. hold off;
  182. title(plot_title);
  183. xlabel("time after spike [ms]");
  184. ylabel("average membrane potentail [mV]");
  185. end
  186. end
  187. % end of function definition
  188. end