FileRunner.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. classdef FileRunner
  2. % Summary of this class goes here
  3. % Detailed explanation goes here
  4. properties (SetAccess = private)
  5. files; % listing of the files that are read
  6. last_file; % the last file starts from 0
  7. file_read_error; % reading error starts from 1
  8. type; % type of file reading now
  9. spkdetconf; % configuration for detecting spikes
  10. recordtime; % timestamp for the current recording
  11. duration; % duration of current file
  12. samplerate;
  13. file_finished; % finishing indicator starts from 1
  14. first_ch_col; % starts with 1
  15. final_ch_col; % depends on plate type
  16. first_ch_row; % starts with 1
  17. final_ch_row; % depends on plate type
  18. first_well_col; % starts with 1
  19. final_well_col; % depends on plate type
  20. first_well_row; % starts with 1
  21. final_well_row; % depends on plate type
  22. last_ch_col; % starts with 0
  23. last_ch_row; % starts with 0
  24. last_well_row; % starts with 0
  25. last_well_col; % starts with 0
  26. Plate_type;
  27. T; % table for csv
  28. csv_directory; %where to save the csv files
  29. end
  30. methods
  31. function obj = FileRunner(p_SpikeDetConfiguration, p_TargetFiles,csv_directory)
  32. %constructor method
  33. obj.files = p_TargetFiles;
  34. obj.last_file = 0;
  35. obj.spkdetconf = p_SpikeDetConfiguration;
  36. obj.file_read_error = 0;
  37. obj.file_finished = 1;
  38. obj.T=table;
  39. obj.csv_directory=csv_directory;
  40. end
  41. function r_fname = getCurrentFile(obj)
  42. r_fname = obj.files{obj.last_file};
  43. end
  44. function [r_result, obj] = ReadSpikesFromNextChannel(obj)
  45. %proceeds the processing to the next channel/file
  46. if obj.file_finished &&...
  47. obj.last_file >= length(obj.files)
  48. Finalcsv=sortrows(obj.T, 2);
  49. thefilenamefull=strsplit(obj.getCurrentFile(), '.');
  50. thefilename=strsplit(thefilenamefull{1}, '\');
  51. thefilename=thefilename{end};
  52. writetable(Finalcsv, strcat(obj.csv_directory, '\',thefilename,'_', 'spikes.csv'));
  53. disp('All files have been read from!');
  54. r_result = [];
  55. else
  56. if obj.file_finished&&...
  57. obj.last_file>0
  58. Finalcsv=sortrows(obj.T, 2);
  59. thefilenamefull=strsplit(obj.getCurrentFile(), '.');
  60. thefilename=strsplit(thefilenamefull{1}, '\');
  61. thefilename=thefilename{end};
  62. writetable(Finalcsv, strcat(obj.csv_directory, '\',thefilename,'_', 'spikes.csv'));
  63. obj.T=table; %zeroing the table
  64. obj = obj.OpenNewFile();
  65. end
  66. if obj.file_finished
  67. obj = obj.OpenNewFile();
  68. end
  69. [r_result.error,m_data, r_result.chlabel, obj] = obj.ReadAxionChannel();
  70. obj = obj.SiirraOsoittimetSeuraavaan();
  71. if ~r_result.error
  72. obj.spkdetconf.sr = obj.samplerate;
  73. [r_result.indexes, r_result.spikes, r_result.threshold, ~, r_result.noise] = DetectSpikes_Amp(m_data, obj.spkdetconf);
  74. %only r_result.indexes are in use currently from all of the
  75. %output of DetectSpikes_Amp function
  76. Time=r_result.indexes;
  77. Channel=string(repmat(r_result.chlabel, numel(r_result.indexes),1));
  78. %repeating the channel label as many times %as the number
  79. %of the detected spikes. (For the table creation)
  80. TT=table(Channel, Time);
  81. obj.T=[obj.T; TT];
  82. end
  83. end
  84. end
  85. function [errorread,data,chlabel, obj] = ReadAxionChannel(obj)
  86. %reading the data from the electrode
  87. disp(obj.getCurrentFile())
  88. data = [];
  89. m_well=strcat(char(64+obj.last_well_row+1),num2str(obj.last_well_col+1));
  90. splitter=@(x) strsplit(x, '/');
  91. while ~any(strcmp(cellfun(@(z) z{end}, cellfun(splitter,{h5info(obj.getCurrentFile()).Groups(1).Groups.Name}, 'UniformOutput',false), 'UniformOutput',false), m_well))
  92. fprintf([m_well '_' 'seems to be excluded\n']);
  93. obj = obj.JumpNextWell;
  94. m_well=strcat(char(64+obj.last_well_row+1),num2str(obj.last_well_col+1));
  95. if obj.file_finished == 1
  96. errorread = 1;%to show that the file finished and nothing to readhere
  97. chlabel=[];
  98. return
  99. end
  100. end
  101. m_ch=strcat(num2str(obj.last_ch_col+1),num2str(obj.last_ch_row+1));
  102. chlabel = strcat(m_well,'_',m_ch); % form the label (well and electrode)
  103. disp(['now reading: ' chlabel]);
  104. try
  105. data = h5read(obj.getCurrentFile(),['/Data/' m_well '/' m_ch]);
  106. errorread = 0;
  107. catch
  108. errorread = 1;
  109. InactiveChannels=h5read(obj.getCurrentFile(), '/DataInfo/InactiveChannels');
  110. if any(contains(InactiveChannels, chlabel))
  111. disp(['Channel' ' ' chlabel ' ' 'is inactive, it is not recorded in the initial analysed file.'])
  112. else
  113. error(['Smth wrong with' ' ' chlabel ' ' 'channel reading!']);
  114. end
  115. end
  116. end
  117. function obj = SiirraOsoittimetSeuraavaan(obj)
  118. %shifts the well/electrode addresses (row/col) to the next after processing
  119. if obj.final_ch_row-1 <= obj.last_ch_row
  120. obj.last_ch_row = obj.first_ch_row-1;
  121. if obj.final_ch_col-1 <= obj.last_ch_col
  122. obj.last_ch_col = obj.first_ch_col-1;
  123. if obj.final_well_col-1 <= obj.last_well_col
  124. obj.last_well_col = obj.first_well_col-1;
  125. if obj.final_well_row-1 <= obj.last_well_row
  126. disp('All channels have been read from!');
  127. obj.file_finished = 1;
  128. else
  129. obj.last_well_row = obj.last_well_row+1;
  130. end
  131. else
  132. obj.last_well_col = obj.last_well_col+1;
  133. end
  134. else
  135. obj.last_ch_col = obj.last_ch_col+1;
  136. end
  137. else
  138. obj.last_ch_row = obj.last_ch_row+1;
  139. end
  140. end
  141. function obj = OpenNewFile(obj)
  142. %launches new file opening
  143. obj.file_finished = 0;
  144. obj.file_read_error = 1; %setting it to 1, if everything successful it will be switched to 0 during OpenAxionFile
  145. while obj.file_read_error && obj.last_file < length(obj.files)
  146. disp(['Opening new file: ' obj.files{obj.last_file+1}]);
  147. obj.type = strsplit(obj.files{obj.last_file+1},'.');
  148. obj.type = cell2mat(obj.type(end));
  149. obj = obj.OpenAxionFile(obj.files{obj.last_file+1});
  150. obj.last_file = obj.last_file+1;
  151. end
  152. if obj.file_read_error && obj.last_file == length(obj.files)
  153. error('NOT A SINGLE FILE WAS OPENED SUCCESSFULLY!')
  154. end
  155. end
  156. function obj = OpenAxionFile(obj, p_TargetFile)
  157. % sets the sample rate, voltageScale, recordtime from the file
  158. try
  159. obj.Plate_type=h5readatt(p_TargetFile, '/DataInfo/', 'Plate type');
  160. obj.first_ch_col = 1;
  161. obj.first_ch_row = 1;
  162. obj.first_well_col = 1;
  163. obj.first_well_row = 1;
  164. obj.last_ch_col = 0;
  165. obj.last_ch_row = 0;
  166. obj.last_well_row = 0;
  167. obj.last_well_col = 0;
  168. if strcmp(obj.Plate_type, '12-well plate')
  169. obj.final_ch_col = 8;
  170. obj.final_ch_row = 8;
  171. obj.final_well_col = 4;
  172. obj.final_well_row = 3;
  173. elseif strcmp(obj.Plate_type, '48-well plate')
  174. obj.final_ch_col = 4;
  175. obj.final_ch_row = 4;
  176. obj.final_well_col = 8;
  177. obj.final_well_row = 6;
  178. else
  179. disp('Unknown MEA layout!')
  180. end
  181. obj.duration = h5readatt(p_TargetFile, '/DataInfo/','DurationInSec');
  182. obj.samplerate = h5readatt(p_TargetFile, '/DataInfo/','SamplingFrequencyInHz');
  183. obj.file_read_error = 0;
  184. catch
  185. disp (['File ' p_TargetFile ' could not be opened successfully!']);
  186. obj.file_read_error = 1;
  187. end
  188. end
  189. function obj = JumpNextWell(obj)
  190. %proceeds to the next well
  191. if obj.final_well_col-1 <= obj.last_well_col
  192. obj.last_well_col = obj.first_well_col-1;
  193. if obj.final_well_row-1 <= obj.last_well_row
  194. disp('All channels have been read from!');
  195. obj.file_finished = 1;
  196. else
  197. obj.last_well_row = obj.last_well_row+1;
  198. end
  199. else
  200. obj.last_well_col = obj.last_well_col+1;
  201. end
  202. end
  203. end
  204. end