//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.
 ======================================================================*/

deleteall -force

include X1compat

// Create a library of prototype elements to be used by the cell reader

include userprefs.g 

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

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

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

function step_runlen
    step {tmax} -time
end

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

function set_Gbar_Na(dialog)
   str dialog
    setfield /thalcell/soma/Na_sej_tab Gbar {getfield {dialog} value}
end

function set_Gbar_K(dialog)
   str dialog
    setfield /thalcell/soma/K_lgn_hh Gbar {getfield {dialog} value}
end

function set_Gbar_T(dialog)
   str dialog
    setfield /thalcell/soma/Ca_sej_tab Gbar {getfield {dialog} value}
end

function set_Gbar_H(dialog)
   str dialog
    setfield /thalcell/soma/H_lgn_tab Gbar {getfield {dialog} value}
end

function set_Gbar_Leak(dialog)
   str dialog
    setfield /thalcell/soma/K_leak Gk {getfield {dialog} value}
end

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

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

function set_runlen(dialog)
   str dialog
    tmax = ({getfield {dialog} value})
    setfield /data/voltage xmax {tmax}
end

function switch_overlay(toggle)
   str toggle
    setfield /data/voltage overlay {getfield {toggle} state}
end

function reset_cell
   reset
   call /thalcell/soma RESET
   setfield /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
readcell thalcell.p /thalcell

setfield /thalcell/soma/K_leak Gk {GK_INIT}
addmsg /thalcell/soma/K_leak /thalcell/soma CHANNEL Gk Ek
addmsg /thalcell/soma /thalcell/soma/K_leak VOLTAGE Vm

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

make_dialogs

check
reset_cell






