read_openephys.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function read_openephys(varargin)
  2. %READ_OPENEPHYS Spike detection for open ephys data.
  3. % READ_OPENEPHYS loads open ephys continuous files, subtracts the average
  4. % of all channels (referencing), performs filtering, censoring and spike
  5. % detection of tetrode channels. Files are savet in TT*.mat files for
  6. % clustering.
  7. %
  8. % Optional input arguments:
  9. % DATADIR - path name for reading data
  10. % RESDIR - path name for writing result files
  11. % TTSPEC - numerical array for subslecting tetrodes
  12. % RAWDATAFILETAG - if the raw data files have a name tag appended
  13. %
  14. % Parameter-value input argumets:
  15. % REFERENCE - allows referenceing options; default: 'common_avg',
  16. % common average referencing; set to 'none' to apply no offline
  17. % referencing
  18. %
  19. % See also OEDISC.
  20. % Balazs Hangya and Panna Hegedus
  21. % Laboratory of Systems Neuroscience
  22. % Institute of Experimental Medicine, Budapest, Hungary
  23. % Default arguments
  24. prs = inputParser;
  25. addOptional(prs,'datadir','n:\Pancsi\HDB12\2017-02-28_17-57-37_HDB12\',@(s)isempty(s)|isdir(s)) % data directory
  26. addOptional(prs,'resdir','',@(s)isempty(s)|isdir(s)) % results directory
  27. addOptional(prs,'TTspec',1:8,@isnumeric) % selected tetrodes (default: 32 channels, 8 tetrodes)
  28. addOptional(prs,'rawdatafiletag','',@ischar) % switch for filtering
  29. addParameter(prs,'reference','common_avg',@(s)ischar(s)|isempty(s)) % switch for referencing
  30. parse(prs,varargin{:})
  31. g = prs.Results;
  32. % Tetrodes to convert
  33. NumChannels = 32; % number of channels
  34. NumTetrodes = NumChannels / 4; % number of tetrodes
  35. if nargin < 3 || isempty(g.TTspec)
  36. g.TTspec = 1:NumTetrodes;
  37. end
  38. % Directories
  39. if isequal(g.datadir(end),'\')
  40. g.datadir = g.datadir(1:end-1);
  41. end
  42. if isempty(g.resdir)
  43. if any(ismember(g.datadir,'-'))
  44. sessiontag = 'a';
  45. cmps = strsplit(g.datadir,'\'); % path components
  46. animalID = cmps{cellfun(@(s)~isempty(s),regexp(cmps,'HDB(\d+)'))};
  47. pd = regexp(cmps{end},'(\d+)-(\d+)-(\d+)','tokens'); % extract date
  48. sessionID = [pd{1}{1}(3:4) pd{1}{2} pd{1}{3} sessiontag];
  49. g.resdir = fullfile(getpref('cellbase','datapath'),animalID,sessionID);
  50. else
  51. cmps = strsplit(g.datadir,'\'); % path components
  52. animalID = cmps{cellfun(@(s)~isempty(s),regexp(cmps,'HDB(\d+)'))};
  53. sessionID = cmps{end};
  54. end
  55. g.resdir = fullfile(getpref('cellbase','datapath'),animalID,sessionID);
  56. g.resdir = fullfile('e:\HDBpavlovian_cellbase\',animalID,sessionID);
  57. end
  58. SaveFeatures = true; % save MClust feature files
  59. % Common average reference
  60. switch g.reference
  61. case 'common_avg'
  62. common_avg = common_avg_ref(g.datadir,32,[],g.rawdatafiletag);
  63. case {'','none'}
  64. common_avg = 0;
  65. otherwise
  66. error('read_openephys: Unknown reference option.')
  67. end
  68. % Spike detection
  69. for iT = g.TTspec
  70. [tdata, ts] = deal(cell(1,4));
  71. for iC = 1:4
  72. basechannel = (iT - 1) * 4;
  73. [tdata{iC}, ts{iC}, info] = load_open_ephys_data([ g.datadir '\' '100_CH' num2str(basechannel+iC) g.rawdatafiletag '.continuous']);
  74. end
  75. data = [tdata{1} tdata{2} tdata{3} tdata{4}];
  76. data = data - common_avg;
  77. if isequal(ts{1},ts{2},ts{3},ts{4}) % we assume that continuous channels share timestamps
  78. tss = ts{1};
  79. else
  80. error('Timestamp error.')
  81. end
  82. [AllTimeStamps, AllWaveForms] = oedisc(data,tss,info.header.sampleRate,30,[]); % filter and detect spikes
  83. TimeStamps = AllTimeStamps{1};
  84. WaveForms = AllWaveForms{1};
  85. TTname = ['TT' num2str(iT)];
  86. TT_path = fullfile(g.resdir,TTname);
  87. save(TT_path, 'TimeStamps','WaveForms');
  88. if SaveFeatures % pre-calculate MClust features
  89. TTdata.TimeStamps = TimeStamps;
  90. TTdata.WaveForms = WaveForms;
  91. openephys_SaveMClustFeatures(TTdata,{'Amplitude';'Energy';'WavePC1';'Time'},[1 1 1 1],TT_path)
  92. end
  93. clearvars -except g common_avg filepath resdir SaveFeatures iT
  94. end