// Determines VM-dependency of activation of IA
// cf. Figure 2(Ab) and Figure 2(Ac) in Whyment et al. 2011, Figure X in Pickering et al. 2013
// Created Linford Briant October 2013

load_file("Cell.hoc")

tstop=5000
steps_per_ms=1
dt=1

celcius=20

// Need spiking currents switched off for this protocol
access SPNcells[0].soma
SPNcells[0].soma.gbar_na3=0
SPNcells[0].axon.gbar_na3=0

objectvar stim
stim=new VClamp(0.5)
stim.amp[0]=-100
stim.dur[0]=3000
stim.dur[1]=2000

proc stimAMP() {
stim.amp[1]=$1
}

// Following sets up all recording vectors, the time vector, and the database matrices:

nstim=21

objectvar IArec[nstim], grec[nstim], vrec[nstim], EKrec[nstim]
for i=0, nstim-1 IArec[i]=new Vector()
for i=0, nstim-1 grec[i]=new Vector()
for i=0, nstim-1 vrec[i]=new Vector()
for i=0, nstim-1 EKrec[i]=new Vector()

stimAMP(0)
grec[0].record(&SPNcells[0].soma.gka_borgka(0.5))
vrec[0].record(&SPNcells[0].soma.v(0.5))
run()

// Sets up vector which is the A-current rather than the A-conductance:

objectvar DataM, DataN, DataK, M, N, K
M=new Matrix(vrec[0].size(),22)
N=new Matrix(vrec[0].size(),22)
K=new Matrix(vrec[0].size(),22)
N.setcol(1,grec[0])
K.setcol(1,vrec[0])

EKrec[0]=vrec[0].sub(ek)
IArec[0]=grec[0].mul(EKrec[0])

M.setcol(1,IArec[0])

objectvar vdt
vdt = new Vector()
vdt.resize(vrec[0].size())
vdt.indgen(dt)

M.setcol(0,vdt)
N.setcol(0,vdt)
K.setcol(0,vdt)

// Now that matrices and protocol have been set up, we can run the main procudure:

for i=1, nstim-1 {

print i

stimAMP(0-i*5)

grec[i].record(&SPNcells[0].soma.gka_borgka(0.5))
vrec[i].record(&SPNcells[0].soma.v(0.5))

run()

N.setcol(i+1,grec[i])
K.setcol(i+1,vrec[i])

// Need to set columns for these two matrices before IA else they get overwritten.

EKrec[i]=vrec[i].sub(ek)
IArec[i]=grec[i].mul(EKrec[i])

M.setcol(i+1,IArec[i])

}

DataN=new File()
DataN.wopen("ActivationProtocol_GKA.dat")
N.fprint(DataN, " %g")
DataN.close("ActivationProtocol_GKA.dat")

DataK=new File()
DataK.wopen("ActivationProtocol_V.dat")
K.fprint(DataK, " %g")
DataK.close("ActivationProtocol_V.dat")

DataM=new File()
DataM.wopen("ActivationProtocol_IA.dat")
M.fprint(DataM, " %g")
DataM.close("ActivationProtocol_IA.dat")