Home > bml > annot > bml_plexon2annot.m

bml_plexon2annot

PURPOSE ^

BML_PLEXON2ANNOT loads spike events from plexon file

SYNOPSIS ^

function [annot, spike] = bml_plexon2annot(cfg)

DESCRIPTION ^

 BML_PLEXON2ANNOT loads spike events from plexon file

 Use as
   annot = bml_plexon2annot(cfg)

 cfg.roi            - roi table with neuroomega sync information
 cfg.plexon         - (path/)filename to plexon file
 cfg.plexon_src     - path to preprocessed mat file for plexon
                      inferred from cfg.plaxon_src if not given
                      and cfg.FileMapping not provided
 cfg.file_mapping   - table with file mapping to vector analyzed by
                      plexon. Loaded from cfg.plexon_src->FileMapping if not given
 cfg.label          - vector of channel names. Extracted from 'channels'
                      cell array in cfg.plexon_src->Channels if not provided
 cfg.electrode      - table with variables channel and electrode. used to 
                      rename the labels. 

 Returns
 annot - annotation table with time of spikes in global time coordinates
 spike - ft_datatype_spike structure as returned by ft_read_spike (with
         renamed labels)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [annot, spike] = bml_plexon2annot(cfg)
0002 
0003 % BML_PLEXON2ANNOT loads spike events from plexon file
0004 %
0005 % Use as
0006 %   annot = bml_plexon2annot(cfg)
0007 %
0008 % cfg.roi            - roi table with neuroomega sync information
0009 % cfg.plexon         - (path/)filename to plexon file
0010 % cfg.plexon_src     - path to preprocessed mat file for plexon
0011 %                      inferred from cfg.plaxon_src if not given
0012 %                      and cfg.FileMapping not provided
0013 % cfg.file_mapping   - table with file mapping to vector analyzed by
0014 %                      plexon. Loaded from cfg.plexon_src->FileMapping if not given
0015 % cfg.label          - vector of channel names. Extracted from 'channels'
0016 %                      cell array in cfg.plexon_src->Channels if not provided
0017 % cfg.electrode      - table with variables channel and electrode. used to
0018 %                      rename the labels.
0019 %
0020 % Returns
0021 % annot - annotation table with time of spikes in global time coordinates
0022 % spike - ft_datatype_spike structure as returned by ft_read_spike (with
0023 %         renamed labels)
0024 
0025 
0026 %loading parameters and defaults
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 %loading fieldtrip spike structure from plexon file
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     %creating sync table for the spike data
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     %creating table with spikes
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);

Generated on Tue 25-Sep-2018 10:08:19 by m2html © 2005