// This hoc file reproduces simulations of a place cell on a linear track formed from 8 inhibitory theta cell inputs
// (Fig. 9A of Welday et al.)
// 
// Theta cell spike trains (shown in the Fig. 9A rasters) are read into NEURON from disk by the accompanying hoc file:
//  read_thetaspikes_Fig9A.hoc. 
// 
//To save the postsynaptic neuron's (place cell's) spike times, type:
//
//   >>load_file("savespikes.hoc")
//
//at the interpreter prompt after the simulation has finished running. Spikes will be saved in a file called 'SPIKOUT.dat'.  
//
//To save the postsynaptic neuron's (place cell's) membrane voltage trace, type:
//
//   >>load_file("savevm.hoc")
//
//at the interpreter prompt after the simulation has finished running. Spikes will be saved in a file called 'VmOUT.dat'.  

load_file("nrngui.hoc")

// -----------------------------------------------------------------
// read theta cell spike trains from disk files into NEURON vectors
// -----------------------------------------------------------------

load_file("placetrack_vecstims_Fig9A.ses") 	//create vecstim objects for delivering theta spike trains to the model neuron

numinputs = 8				//number of inhibitory theta inputs to the model neuron

objref evec[numinputs] 			//array of event vectors (VecStim.mod) for reading theta spike trains
objref spikefile       			//file object through which to read in theta cell spike times from disk
objref pplist          			//list of point processes containing theta spike trains
objref vmvec
objref vmfile

spikefile = new File()
pplist = new List()

vmfile = new File()


load_file("read_thetaspikes_Fig9A.hoc")  //read in theta cell spike timestamps from files
simlength=1000*200/25


// --------------------------------------------------------------
// build a single-compartment postsynaptic cell
// --------------------------------------------------------------

ra        = 150 	// axial resistance through cytoplasm (ohms)
global_ra = ra		
rm        = 15000 	// passive membrane resistance (ohms) 
c_m       = 1   	// membrane capacitance (microFarads per centimeter squared, uF/cm^2)

create soma		//single somatic compartment
access soma

PI=3.14159
{L=10/PI  diam=10}
nseg=1
Ra = ra
cm = c_m 

vmvec = new Vector()
vmvec.record(&v(.5))

//Insert voltage-activated persistent sodium current (Nap.mod)
    insert nap
    gbar_nap=.00005	//Nap conductance
    sh_nap=-10		//Nap voltage activation threshold shift parameter (mV)

//Insert Hodkin-Huxley kinetics (hh.mod, standard NEURON mechanism)
    insert hh
 
    gkbar_hh=.005	//delayed rectifier K+ conductance
    gnabar_hh=.05	//voltage-gated Na+ conductance
    el_hh=-65		//leak reversal potential 
    gl_hh=1/rm		//leak conductance

// --------------------------------------------------------------
// connect theta spike train inputs to the model neuron
// --------------------------------------------------------------

objref nclist          			//list of netcon objects for synaptic connections
nclist = new List()

load_file("placetrack_GABAinputs_Fig9A.ses") 	//synaptic currents

// Uncomment the following two lines to run the simulation with excitatory theta cell inputs (uncomment for inhibitory inputs):
//synw=.000006			//set conductance of AMPA synapses
//gbar_nap=0			//uncomment to shut off Nap conductance

// Uncomment the following three lines to run the simulation with inhibitory theta cell inputs (comment for excitatory inputs):
Erev_AMPA_S=-80			//set reversal potential to -80 mV to convert the AMPA synapse to GABA
Beta_AMPA_S=.12			//slow down the decay time a little bit for GABA currents
synw=.001			//set conductance of GABA synapses

// Uncomment the following two lines to disable active currents and run the simulation without spikes (comment to run with spikes):   
//gkbar_hh=0			//delayed rectifier K+ conductance
//gnabar_hh=0			//voltage-gated Na+ conductance

for i=0,numinputs-1 {
	nclist.append(new NetCon(pplist.o(i), AMPA_S[i], -20, 1, synw)) //make the input connections
}

tstop=simlength

// ------------------------------------------------------------------------
// store model neuron's spike times in a vector to be saved later if needed
// ------------------------------------------------------------------------

objref spiketimes	//vector in which to store timestamps of spikes generated by the model neuron
objref spikenc		//netcon object through which spikes are passed into the 'spiketimes' vector
objref null		//null object

spiketimes = new Vector()
spikenc = new NetCon(&v(0.5), null)
spikenc.threshold = -25    // store a spike timestamp if the postsynaptic membrane voltage exceeds -25 mV
spikenc.record(spiketimes) // record the spike times to the 'spiketimes' vector