0001 function [annot, spike] = bml_plexon2annot(cfg)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 roi = bml_getopt(cfg,'roi');
0028 roi = bml_roi_table(roi);
0029 assert(~isempty(roi),"roi table required");
0030
0031 plexon = bml_getopt_single(cfg,'plexon');
0032 assert(isfile(plexon),"Valid plexon file required");
0033
0034 plexon_src = strrep(plexon,'.plx','.mat');
0035 if ~isfile(plexon_src)
0036 plexon_src = [];
0037 end
0038
0039 plexon_src = bml_getopt_single(cfg,'plexon_src',plexon_src);
0040
0041 FileMapping = [];
0042 label = [];
0043 if ~isempty(plexon_src) && isfile(plexon_src)
0044 src = matfile(plexon_src);
0045 if ismember('FileMapping',fieldnames(src))
0046 FileMapping = src.FileMapping;
0047 end
0048 if ismember('Channels',fieldnames(src))
0049 label = src.Channels;
0050 end
0051 end
0052 FileMapping = bml_getopt(cfg,'FileMapping',FileMapping);
0053 label = bml_getopt(cfg,'label',label);
0054
0055 electrode = bml_getopt(cfg,'electrode');
0056 if ~isempty(label) && ~isempty(electrode) && ...
0057 all(ismember({'electrode','channel'},electrode.Properties.VariableNames))
0058 label=bml_map(label,electrode.channel,electrode.electrode);
0059 end
0060
0061
0062 spike= ft_read_spike(plexon);
0063
0064 if isempty(label)
0065 label = spike.label;
0066 end
0067 if size(label,2) < size(label,1)
0068 label = label';
0069 end
0070 spike.label = label;
0071
0072 FM_VARS={'name','nSamples','Fs','raw1','raw2'};
0073 SY_VARS={'starts','ends','t1','s1','t2','s2','name', 'nSamples'};
0074
0075 assert(~isempty(FileMapping),"FileMapping required");
0076 assert(length(label)==length(spike.label),"Incorrect label");
0077 assert(all(ismember(FM_VARS,FileMapping.Properties.VariableNames)),"Invalid FileMapping table");
0078 assert(all(ismember(SY_VARS,roi.Properties.VariableNames)),"Invalid roi table");
0079
0080 annot=table();
0081 for i=1:length(spike.timestamp)
0082 if ~isempty(spike.timestamp{i})
0083
0084
0085 assert(max(spike.timestamp{i}) <= max(FileMapping.raw2), "Invalid timestamp");
0086
0087 fm = FileMapping(:,FM_VARS);
0088 sy = roi(ismember(roi.name,fm.name),SY_VARS);
0089 syfm=join(sy,fm,'Keys','name');
0090
0091 assert(all(syfm.nSamples_sy==syfm.nSamples_fm), "Inconsistent number of samples");
0092
0093 syfm.s1=syfm.raw1;
0094 syfm.s2=syfm.raw2;
0095 syfm.nSamples = syfm.nSamples_sy;
0096
0097 cfg1=[];
0098 cfg1.roi=syfm;
0099 cfg1.rowisfile=false;
0100 spike_sync=bml_sync_consolidate(cfg1);
0101
0102
0103 st = table(bml_idx2time(spike_sync,spike.timestamp{i})',spike.unit{i}');
0104 st.Properties.VariableNames = {'starts','unit'};
0105 st.channel(:)=label(i);
0106 st.spike_idx=(1:height(st))';
0107
0108 annot=[annot;st];
0109 end
0110 end
0111
0112 annot.ends = annot.starts;
0113 annot = bml_annot_table(annot);