// This hoc file reproduces the simulation of a grid cell in a circular environment from Fig. 7F of Welday et al.
// 
// Before running this simulation, the theta cell spike trains from which the grid cell is formed must first be created
// and saved in files. This is done by running the MATLAB script 'grid_thetaspikes.m' from within the simulation directory.
// 
// Since we are modeling a 1 hour recording session (3600 seconds), the simulation runs for a long time. To maximize speed 
// and performance, you may wish to close the graph windows that plot the voltage trace (Graph[2]) and the spike raster
// (SpikePlot[0] for NetData[0]) before hitting the 'Init & Run' button to start the simulation.
//
// To save the glace cell's spike times when the simulation finishes, 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',  
// and you can then create a path plot of the simulated grid cell's spike output by running the MATLAB script 'plotnrn.m'.
//
// The grid cell is formed by a target neuron that receives inhibitory input from three theta cells.  

load_file("nrngui.hoc")

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

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

numinputs = 3				//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

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

load_file("read_grid_cell_theta_spikes.hoc")  //read in theta cell spike timestamps from files
simlength=1000*3600

// --------------------------------------------------------------
// 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 

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

//Insert Hodgkin-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("gridcell_GABA_inputs.ses") //GABA-A synapses are simulated by modifying an AMPA current (found in ampa.mod)
//Convert AMPA currents to GABA
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			//conductance of GABA synapses


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 -20 mV
spikenc.record(spiketimes) // record the spike times to the 'spiketimes' vector

//To save the postsynaptic neuron'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'.  MATLAB code 
//(plotnrn.m) is provided for generating a "path plot" of the model 
//neuron's spatial tuning function from the 'SPIKEOUT.dat' file.