//genesis  -  thalmodes.g
/*======================================================================
  A script which uses the cell reader to create a lumped-soma model of 
    the firing modes of a thalamocortical relay neuron. SI units are used.
  ======================================================================*/

clean -f

// Create a library of prototype elements to be used by the cell reader
 
include userprefs.g

float tmax = 1.000		// default simulation time = 1 sec
float dt = 0.00005              // simulation time step in sec
setclock  0  {dt}               // set the simulation clock
float inj = 0.0			// default current injection = 0.0 amps
float GK_INIT = 11.25e-9	// default K leakage conductance in Siemens

//===============================
//      Function Definitions
//===============================

function make_Vmgraph
    float vmin = -0.100
    float vmax = 0.05
    create xform /data [40,20,500,500] -title "Thalamic Relay Cell"
    create xgraph /data/voltage  [5,40,490,420] -title "Membrane Potential"
    set ^ XUnits sec YUnits Volts
    set ^ xmax {tmax} ymin {vmin} ymax {vmax}
    create xbutton /data/RESET [10,15,60,20]	-script reset_cell
    create xbutton /data/RUN  [430,15,60,20] -script "step_runlen"
    create xbutton /data/INJECT  [430,470,60,20] -script "set_inject"
    create xtoggle /data/OVERPLOT [10,470,60,20] -title "OVERLAY" \ 
	-script "switch_overlay <widget>"
    xshow /data
end

function step_runlen
    step -t {tmax}
end

function make_dialogs
    create xform /param [550,220,480,300] -title "THALMODES Parameters"
    create xdialog /param/Gbar_Na -title "Na-Current Conductance (Siemens)" \
        -value {get(/thalcell/soma/Na_sej_tab,Gbar)} \ 
	-script "set_Gbar_Na <widget>"
    create xdialog /param/Gbar_K \
        -title "Delayed Rectifier Conductance (Siemens)" \
        -value {get(/thalcell/soma/K_lgn_hh,Gbar)} -script "set_Gbar_K <widget>"
    create xdialog /param/Gbar_T -title "T-Current Conductance (Siemens)" \
	-value {get(/thalcell/soma/Ca_sej_tab,Gbar)} -script "set_Gbar_T <widget>"
    create xdialog /param/Gbar_H -title "H-Current Conductance (Siemens)" \
        -value {get(/thalcell/soma/H_lgn_tab,Gbar)} -script "set_Gbar_H <widget>"
    create xdialog /param/Gbar_leak -title "Leak Current Conductance (Siemens)" \
        -value {get(/thalcell/soma/K_leak,Gk)} -script "set_Gbar_Leak <widget>"
    create xdialog /param/Iinj -value {get(/thalcell/soma,inject)} \
	-script "set_inj <widget>" -title "Current Injection (amps)"
    create xdialog /param/RunLen -title "Run Length (sec)" \
	-value {tmax} -script "set_runlen <widget>"
    xshow /param
end

function set_Gbar_Na(dialog)
   str dialog
    set /thalcell/soma/Na_sej_tab Gbar {get({dialog}, value)}
end

function set_Gbar_K(dialog)
   str dialog
    set /thalcell/soma/K_lgn_hh Gbar {get({dialog}, value)}
end

function set_Gbar_T(dialog)
   str dialog
    set /thalcell/soma/Ca_sej_tab Gbar {get({dialog}, value)}
end

function set_Gbar_H(dialog)
   str dialog
    set /thalcell/soma/H_lgn_tab Gbar {get({dialog}, value)} 
end

function set_Gbar_Leak(dialog)
   str dialog
    set /thalcell/soma/K_leak Gk {get({dialog}, value)}
end

function set_inj(dialog)
   str dialog
    inj = {get({dialog},value)}
end

function set_inject
    set /thalcell/soma inject {inj}
end

function set_runlen(dialog)
   str dialog
    tmax = {get({dialog},value)}
    set /data/voltage xmax {tmax}
end

function switch_overlay(toggle)
   str toggle 
    set /data/voltage overlay {get({toggle},state)}
end

function reset_cell
   reset
   call /thalcell/soma RESET
   set /thalcell/soma Vm {EREST_ACT}
   call /thalcell/soma/Na_sej_tab RESET
   call /thalcell/soma/K_lgn_hh RESET
   call /thalcell/soma/Ca_sej_tab RESET
   call /thalcell/soma/K_leak RESET
end

//===============================
//         Main Script
//===============================

// Build the cell from a parameter file using the cell reader
read_cell thalcell.p /thalcell

set /thalcell/soma/K_leak Gk {GK_INIT}
sendmsg /thalcell/soma/K_leak /thalcell/soma CHANNEL Gk Ek
sendmsg /thalcell/soma /thalcell/soma/K_leak VOLTAGE Vm

// Make the graph to display soma Vm and pass message to the graph
xstartup
make_Vmgraph
sendmsg /thalcell/soma /data/voltage PLOT Vm *volts *red

make_dialogs

check
reset_cell
