Data import from CED's Spike II into STAToolkit

The following Spike II script creates .stam and .stad files to import Spike II data, similar to that used by 'demo-directformal.m'. The spikes need to be in an event channel. A second event channel marks the beginning of trials of either repeated stimulus presentation (box checked) or random presentation (box not checked). The script only exports complete trials between cursor 1 and 2. Script provided with no warranty or promises.

spikeII2STAToolkit.s2s
        ' write_sta_data Spike II script
        ' writes .stam and .stad files that then can be imported in STAToolkit (http://neuroanalysis.org)
        ' needs one event channel that corresponds to the spikes and one event channel that corresponds
        ' to the start of repeats (or segments of data for random stimulation)
        ' B. Ludwar 06/01/2010
        ' comments:
        ' use the unix command dos2unix to convert Windows files for unix systems!
        ' check .stam file to make sure it has the right file path for the .stad file
        ' for unix also make sure to replace \ in the path with /!
        '
        var oview%, fview%;     'handles for original and output views
        var spkch%, markch%;    'handles for channels
        var cu1, cu2;           'position of cursor 1 & 2
        var n%, m%;
        var clength;            'trial length
        var cycletimes[1000],cycles%;   'trial start times
        var spiketimes[100000],spikes%; 'spike times within trial <- make this array larger if you have more than 10K spikes / trial
        var fname$, rpt%;               'file name and value of repeat checkbox
        '
        ' init
        spkch%  := 4;
        markch% := 6;
        cu1 := cursor(1);
        cu2 := cursor(2);
        fname$ := "\\\\LINUX-TJDD\\bludwar\\Documents\\MATLAB\\";
        rpt% := 0;
        '
        ' user interaction 
        DlgCreate("WriteSTA v. 0.1 - data export for STAToolkit (http://neuroanalysis.org)");
        DlgString (1,"Filename incl. path?",60,"",21,2);
        DlgCheck  (2,"Is this a repeat trial?",21,3);
        DlgInteger(3,"Which channel holds the spike events?",0,255,40,4);
        DlgInteger(4,"Which channel marks traces/repeats?",0,255,40,5);
        DlgText("Don't forget to use dos2unix to convert the files to the correct line termination on Unix systems!",3,7);
        DlgShow(fname$,rpt%,spkch%,markch%);
        '
        ' make a new text view, then go back to the file
        oview% := view();
        fview% := FileNew(1);
        WindowTitle$("stad file");
        WindowVisible(1);
        view(oview%);
        '
        'write data for stad file
        'get number of repeat cycles (=cycles%) and start times(=cycletimes[])
        cycles%:= ChanData(markch%,cycletimes[],cu1,cu2);	
        clength := cycletimes[1]-cycletimes[0];
        for n%:=0 to cycles%-1  do        'go through all repeat cycles, but skip last (partial one)
            spikes%:= ChanData(spkch%,spiketimes[],cycletimes[n%],cycletimes[n%+1]);
            for m%:=0 to spikes%-1 do     'go through all the spike times and write them into stad file
                view(fview%).Print("%.6f ",(spiketimes[m%]-cycletimes[n%]));
            next 'm%    
            'remove last space as staread is very picky about extra spaces
            view(fview%).MoveBy(1,-1);
            view(fview%).EditClear();    
            view(fview%).Print("%s\r\n","");
        next 'n%
        '
        'save the file
        view(fview%);
        FileSaveAs((fname$+".stad"),1);
        FileClose();
        '
        'write data for stam file
        view(oview%);
        fview% := FileNew(1);
        WindowTitle$("stam file");
        WindowVisible(1);
        Print("%s\r\n","# Data filename");
        Print("%s\r\n",("datafile="+fname$+".stad;"));
        Print("%s\r\n","#");
        Print("%s\r\n","# Site metadata");
        Print("%s\r\n","site=1; label=only one cell; recording_tag=episodic; time_scale=1.000000; time_resolution=0.000400;");
        Print("%s\r\n","#");
        Print("%s\r\n","# Category metadata");
        if rpt%=0 then 
            for n%:=0 to cycles%-2 do
                Print("%s%d%s%d%s\r\n","category=",(n%+1),"; label=Trial ",(n%+1),";"); 
            next 'n%   
        else
            Print("%s\r\n","category=1; label=repeated;");
        endif;      
        Print("%s\r\n","#");
        Print("%s\r\n","# Trace metadata");
        if rpt%=0 then 
            'noise is different for every trace
            for n%:=0 to cycles%-2 do
                Print("%s%d%s%d%s%6f%s\r\n","trace=",(n%+1),"; catid=",(n%+1),"; trialid=1; siteid=1; start_time=0.000000; end_time=",clength,";"); 
            next 'n%
            'remove last CRLF as staread is very picky about extra spaces
            view(fview%).MoveBy(1,-2);
            view(fview%).EditClear();    
        else 
            'noise is repeated for every trace
            for n%:=0 to cycles%-2 do
                Print("%s%d%s%d%s%6f%s\r\n","trace=",(n%+1),"; catid=1; trialid=",(n%+1),"; siteid=1; start_time=0.000000; end_time=",clength,";"); 
            next 'n%
            'remove last CRLF as staread is very picky about extra spaces
            view(fview%).MoveBy(1,-2);
            view(fview%).EditClear();
        endif;
        '
        'save the file
        view(fview%);
        FileSaveAs((fname$+".stam"),1);
        FileClose();
        '
        'view(oview%);
        halt;
usage/tutorials/data_import.txt · Last modified: 2010/06/04 13:40 by Bjoern Ludwar