transJuncVolt.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. function transJuncVolt(files, varargin)
  2. %% Analyse the effect of the transjunctional voltage on the electrical coupling
  3. % This function plots the ratio of the amplitudes of two cells during a
  4. % stimulation in relation to the differnce in their resting membrane
  5. % potentials. The latter is an estimation for the transjunctional voltage.
  6. % The plot helps to judge whether the transjunctional voltage has an effect
  7. % on the electrical coupling. If requested the trial numbers can be added
  8. % to check for other effects as well. The function can handle up to seven
  9. % stimuli at the same time. Alternatively up to 11 files can be analysed at
  10. % a time, but then only one stimulus is used.
  11. % -------------------------------------------------------------------------
  12. % Input [optional arguments are name-value pairs]
  13. % -----
  14. % files: number of the dataset to examine
  15. % Filter: select between no filter (default), hum noise ('hn') or
  16. % (optional) hum and high frequency noise ('hnhfn')
  17. % StimCell: set which cell should be the stimulated one ('T' or
  18. % (optional) 'INT')
  19. % ------
  20. % Output
  21. % ------
  22. % graphic output, scatter plot, new figure window
  23. % -------------------------------------------------------------------------
  24. % Program by: Bjarne Schultze [last modified 17.12.2021]
  25. % -------------------------------------------------------------------------
  26. %% Preparations
  27. % select the stimuli to take for the calculations by their amperage (up to
  28. % seven stimuli are possible at the same time)
  29. stimAmps = [-2,-1];
  30. % set whether the points in the scatter plot should be marked with the
  31. % trial numbers
  32. labelling = 1;
  33. % set whether each file should get its own plot (0) or the results for all
  34. % files are plottet in one plot (1)
  35. onePlot = 1;
  36. % with multiple files in one plot, only one stimulus is used
  37. if onePlot
  38. stimAmps = stimAmps(1);
  39. end
  40. % parse the user input
  41. [file_nums, ~, filter_choice, ~, ~, stimCell, ~,~,~] = ...
  42. parse_input_peakfun(files, varargin{:});
  43. % extract the requested data
  44. recordings = extract_data(file_nums, filter_choice);
  45. % load a file containing the electrode resistance values
  46. load('../AdditionalFiles/electrodeProps.mat','electrodeProps')
  47. % preallocate a string array for the resistance output
  48. d_resistance_output(1,1) = ...
  49. sprintf("\n\x0394 resistance values (StimCell %s)\n" + ...
  50. "------------------------\nFile\tT cell\t\tInt\n" + ...
  51. "------------------------\n", stimCell);
  52. % list of all the amperages of the single stimuli in ascending order
  53. amps_rows = [-2,-1,-0.5,0.03125,0.0625,0.125,0.25,0.5,0.75,1,1.25,1.5];
  54. %% Main calculations
  55. % getting the amplitudes for the both cells during stimulation
  56. amplitudes = ampAtStim(files, varargin{:});
  57. for file = 1:length(files)
  58. % select the dataset for the current iteration
  59. recData = recordings{file,1};
  60. % preallocate matrices to collect the calculated data
  61. stimNum = size(stimAmps,2); trialNum = size(recData.recordingT,2);
  62. search_StimAmp = zeros(stimNum,1);
  63. rmpT = zeros(stimNum,trialNum);
  64. rmpINT = zeros(stimNum,trialNum);
  65. % select and average 500 ms (5000 data points) before each stimulus as RMP
  66. for i = 1:stimNum
  67. % search the list of amperages for the given stimulus value
  68. search_StimAmp(i) = find(abs(amps_rows-stimAmps(i)) < 0.001);
  69. % find the times (datapoints) of the beginning of the required
  70. % stimulus
  71. dp_stimOnset = find((abs(recData.stimulus - stimAmps(i)) < 0.001),1);
  72. % select the recorded data in this time frames for all trials
  73. sel_dataT = recData.recordingT((dp_stimOnset-5000):...
  74. dp_stimOnset,:);
  75. sel_dataINT = recData.recordingINT((dp_stimOnset-5000):...
  76. dp_stimOnset,:);
  77. % average the membrane potentials within each time frame
  78. rmpT(i,:) = mean(sel_dataT);
  79. rmpINT(i,:) = mean(sel_dataINT);
  80. end
  81. % distinguish between a stimulated T cell and a stimulated interneuron
  82. if isequal(stimCell, 'INT')
  83. stimulated = setdiff(1:trialNum, recData.stimulatedT(:), 'stable');
  84. % select the RMP according to the stimulation information
  85. rmpNStim = rmpT(:,stimulated); rmpStim = rmpINT(:,stimulated);
  86. % select the amplitudes respectively
  87. amps_NStim = ...
  88. amplitudes{file,1}.AmplitudeT(search_StimAmp,stimulated);
  89. amps_Stim = ...
  90. amplitudes{file,1}.AmplitudeINT(search_StimAmp,stimulated);
  91. % define a plot title
  92. plot_subtitle = ("- interneuron stimulated -");
  93. else
  94. stimulated = recData.stimulatedT;
  95. % select the RMP according to the stimulation information
  96. rmpStim = rmpT(:,stimulated); rmpNStim = rmpINT(:,stimulated);
  97. % select the amplitudes respectively
  98. amps_Stim = ...
  99. amplitudes{file,1}.AmplitudeT(search_StimAmp,stimulated);
  100. amps_NStim = ...
  101. amplitudes{file,1}.AmplitudeINT(search_StimAmp,stimulated);
  102. % define a plot title
  103. plot_subtitle = ("- T cell stimulated -");
  104. end
  105. % calculate the difference in the RMPs
  106. rmp_diff = rmpStim - rmpNStim;
  107. % calculate the ratio between the amplitudes of the two cells
  108. amp_ratio = amps_NStim ./ amps_Stim;
  109. % calculate the changes in electrode resistances
  110. c_fileNum = files(file);
  111. if isequal(electrodeProps.cell_left(c_fileNum), 'T')
  112. d_resistance_T = electrodeProps.R_left_post(c_fileNum)-...
  113. electrodeProps.R_left(c_fileNum);
  114. d_resistance_INT = electrodeProps.R_right_post(c_fileNum)-...
  115. electrodeProps.R_right(c_fileNum);
  116. elseif isequal(electrodeProps.cell_left(c_fileNum),'I')
  117. d_resistance_INT = electrodeProps.R_left_post(c_fileNum)-...
  118. electrodeProps.R_left(c_fileNum);
  119. d_resistance_T = electrodeProps.R_right_post(c_fileNum)-...
  120. electrodeProps.R_right(c_fileNum);
  121. end
  122. % create a command window output for the delta resistance values
  123. if isnan(d_resistance_T)
  124. d_resistance_output(file+1,1) = sprintf("%02.f \t\t%02.f \t\t%02.f\n",...
  125. c_fileNum, d_resistance_T, d_resistance_INT);
  126. else
  127. d_resistance_output(file+1,1) = sprintf("%02.f \t\t%02.f \t\t\t%02.f\n",...
  128. c_fileNum, d_resistance_T, d_resistance_INT);
  129. end
  130. %% Show results
  131. % plot the results as a scatter plot
  132. if onePlot
  133. if file == 1
  134. figure();
  135. % obtain the maximum and minimum values for axes scaling
  136. maxX = max(rmp_diff, [], 'all');
  137. minX = min(rmp_diff, [], 'all');
  138. maxY = max(amp_ratio, [], 'all');
  139. minY = min(amp_ratio, [], 'all');
  140. ax = gca;
  141. % add new colors if more than 7 files are requested
  142. if length(files) > 7
  143. ax.ColorOrder((end+1:end+4),:) = [0.67 0.53 0.22; ...
  144. 0 0.56 0 ; ...
  145. 0.69 0.81 0.15; ...
  146. 0.83 0.06 0.06];
  147. end
  148. end
  149. % use the same color for all datasets if more than 11 are requested
  150. if length(files) > 11
  151. current_color = ax.ColorOrder(1,:);
  152. else
  153. current_color = ax.ColorOrder(file,:);
  154. end
  155. % plot the results of the current file
  156. hold on;
  157. plot(rmp_diff', amp_ratio', 'o', 'Color', current_color,...
  158. 'MarkerSize', 5, 'MarkerFaceColor', current_color);
  159. hold off;
  160. % gather max and min values for axes scaling
  161. if max(rmp_diff, [], 'all') > maxX
  162. maxX = max(rmp_diff, [], 'all');
  163. end
  164. if min(rmp_diff, [], 'all') < minX
  165. minX = min(rmp_diff, [], 'all');
  166. end
  167. if max(amp_ratio, [], 'all') > maxY
  168. maxY = max(amp_ratio, [], 'all');
  169. end
  170. if min(amp_ratio, [], 'all') < minY
  171. minY = min(amp_ratio, [], 'all');
  172. end
  173. else
  174. % plot the results in one plot per file
  175. figure();
  176. plot(rmp_diff', amp_ratio', 'o');
  177. end
  178. % label the points with their trial number, if requested and if the
  179. % results for each file are plotted in a seperate figure
  180. if labelling && ~onePlot
  181. % set the points to place the labels
  182. textX = rmp_diff';
  183. max_amp_ratio = max(amp_ratio, [], 'all');
  184. min_amp_ratio = min(amp_ratio, [], 'all');
  185. range_amp_ratio = abs(max_amp_ratio - min_amp_ratio);
  186. textY = (amp_ratio + range_amp_ratio * 0.04)';
  187. % create the labels
  188. textNums = num2str(stimulated');
  189. % get the current axes object to access the color order
  190. ax = gca;
  191. % label the datapoints
  192. for i = 1:stimNum
  193. text(textX(:,i), textY(:,i), textNums, 'FontSize', 7, ...
  194. 'Color', ax.ColorOrder(i,:));
  195. end
  196. % re-scale the axes to have all numbers inside the plot
  197. if maxY ~= 0 && minY ~= 0
  198. ylim([(min_amp_ratio - range_amp_ratio * 0.1), ...
  199. (max_amp_ratio + range_amp_ratio * 0.1)]);
  200. end
  201. max_rmp_diff = max(rmp_diff, [], 'all');
  202. min_rmp_diff = min(rmp_diff, [], 'all');
  203. range_rmp_diff = abs(max_rmp_diff - min_rmp_diff);
  204. xlim([(min_rmp_diff - range_rmp_diff * 0.05),...
  205. (max_rmp_diff + range_rmp_diff * 0.05)]);
  206. end
  207. % add title and axes labels
  208. ylabel("ratio of amplitudes (NStim/Stim)");
  209. xlabel("transjunctional voltage (Stim-NStim) [mV]");
  210. title(sprintf("Effect of transjunctional voltage - File: %0.f", ...
  211. files(file)));
  212. subtitle(plot_subtitle);
  213. % add a suitable legend if single plots were created
  214. if ~onePlot
  215. leg_entries = "";
  216. for i = 1:stimNum
  217. leg_entries(i) = sprintf("%g nA stimulus", stimAmps(i));
  218. end
  219. legend(leg_entries, 'Location','best');
  220. end
  221. % end of file-iteration-loop
  222. end
  223. if onePlot
  224. % rescale axes if multiple files in one plot
  225. rangeX = abs(maxX - minX); rangeY = abs(maxY - minY);
  226. xlim([(minX - 0.05*rangeX), (maxX + 0.05*rangeX)]);
  227. if minY ~= 0 && maxY ~= 0
  228. ylim([(minY - 0.1*rangeY), (maxY + 0.1*rangeY)]);
  229. end
  230. % add a suitable legend
  231. if length(files) < 12
  232. leg_entries = "";
  233. for i = 1:length(files)
  234. leg_entries(i) = sprintf("File: %g // %g nA stim",...
  235. files(i),stimAmps(1));
  236. end
  237. legend(leg_entries, 'Location', 'best');
  238. end
  239. title("Effect of transjunctional voltage");
  240. end
  241. % give back the delta resistance values to the command window
  242. fprintf("%s", d_resistance_output);
  243. %% end of function definition
  244. end