//One can change synapse distribution in this morphology to replicate Fig7D and Suppl fig15
//Please follow the comments where synapses are declared
//PPC L5 Regular Pyramidal cell
load_file("nrngui.hoc")
npyrcell= 1           
objref PYRcell[npyrcell]

	begintemplate Pyramidalcell

ndend1=2
ndend2=2
public subsets
public soma, pcdend1, pcdend2
public all, pcdend, pcddend,pcbdend,pcbddend

create soma, pcdend1[ndend1], pcdend2[ndend2]
objref syn, pre_list


proc init() {
	pre_list = new List()
	subsets()
	pctemp()
}
objref all, pcdend, pcddend, pcbdend,pcbddend
proc subsets(){ local i
	objref all, pcdend, pcddend, pcbdend,pcbddend
	all = new SectionList()
		soma all.append()
		pcdend1 [0] all.append() // distal apical
		pcdend1 [1] all.append() //proximal apical
                pcdend2 [0] all.append() // distal basal
		pcdend2 [1] all.append() //proximal basal

	pcddend  = new SectionList()
		pcdend1 [0] pcddend.append() //distal apical
		
        pcdend = new SectionList()
	        pcdend1 [1] pcdend.append() //proximal apical

	pcbddend  = new SectionList()
		pcdend2 [0] pcddend.append() //distal basal
		
        pcbdend = new SectionList()
	        pcdend2 [1] pcdend.append() //proximal basal
		
}
proc pctemp() {
  

 	soma {nseg=1 L=15 diam=15} // changed L & diam	
	  pcdend1 [0] {nseg=1 L=150 diam=1} //distal dend with synapses
        pcdend1 [1] {nseg=1 L=150 diam=1} //proximal with higher Ca pcdend1[1]
        pcdend2 [0] {nseg=1 L=150 diam=1} 
        pcdend2 [1] {nseg=1 L=150 diam=1}
	forsec all {
	Ra=100
         insert gskch
	 gskbar_gskch=0.00075
         esk= -80
         
	   insert ichan2
           gnatbar_ichan2= 0.065
	   gkfbar_ichan2=0.0003
	   gksbar_ichan2=0.0006
	   gl_ichan2=0.0009
 	   enat =50
           ekf = -90
           eks =-90
           el_ichan2 = -70
	}
           
        soma {
	   cm =1.5   
           insert it
           gcabar_it = 0.0024
           insert hcn1
           gpeak_hcn1=0.00001
           Vrev_hcn1=-40
                }

           forsec pcdend{ 
           cm =1.5  
           insert it
           gcabar_it =0.0048 //proximal high T-Ca
           insert hcn1
           gpeak_hcn1=0.0000125
           Vrev_hcn1=-40
	   	}
     
          forsec pcbdend{ 
           cm =1.5  
           insert it
           gcabar_it = 0.0048 //proximal high T-Ca
           insert hcn1
           gpeak_hcn1=0.0000125
           Vrev_hcn1=-40
	   	}

        forsec pcddend {
           cm =1.5 
           insert it
           gcabar_it = 0.0024
           insert hcn1
           gpeak_hcn1=0.0002
           Vrev_hcn1=-40
                       }

        forsec pcbddend {
           cm =1.5 
           insert it
           gcabar_it = 0.0024
           insert hcn1
           gpeak_hcn1=0.0002
           Vrev_hcn1=-40
                       }

        connect pcdend1[1](0), soma(0)
	connect pcdend1[0](0), pcdend1[1](1)
	connect pcdend2[1](0), soma(1)
        connect pcdend2[0](0), pcdend2[1](1)
	}

	func is_art() { return 0 }
	endtemplate Pyramidalcell
//**********************************************************************************
// NETWORK SPECIFICATION INTERFACE
for i=0, npyrcell-1 {PYRcell[i] = new Pyramidalcell(i)} 
//*************************************************************************************************
//In slice physiology we didn't see significant differences in A1 or ACC input kinetics.
//Basically one can call same mod file at different times - this is going to simulate unimodal activation.
//To reproduce slice physiology IB cell data regarding synaptic integration - use faster NMDA kinetics as predicted in the model exploration of parameters
// Right now synapses are set at distal dend that reproduces morphology4 in Fig7D
// For Suppl fig15. simply change weights to 2nS or 3nS or 4nS

//almost 4.95mV amp
objref evec
evec = new Vector(1)
evec.x[0]=4000//4000
objref vs
vs = new VecStim(0.5)
objref nc, synapse
//Synapse distribution for Fig7: Morphology 4: PYRcell[0].pcdend1[0] synapse = new A1_IB(0.5)
// To reproduce morphology5 in Fig7D change to PYRcell[0].pcdend1[0] synapse = new A1_RS(0.5) 
// To reproduce morphology6 in Fig7D (distributed syn) Set one of synapses to PYRcell[0].pcdend1[1] synapse = new A1_RS(0.5) 
//To reproduce morphology7 in Fig7D (clustered syn) Set one of synapses to PYRcell[0].pcdend1[1] synapse = new A1_RS(0.5) 

PYRcell[0].pcdend1[0] synapse = new A1_IB(0.5)
nc = new NetCon(vs, synapse)
vs.play(evec)
nc.weight[0]=3 //this is for AMPA
nc.weight[1]=3 //this is for NMDA

objref evec2
evec2 = new Vector(1)
evec2.x[0]=4050
objref vs2
vs2 = new VecStim(0.5)
objref nc2, synapse2
//Synapse distribution for Fig7: Morphology 4: PYRcell[0].pcdend1[0] synapse = new A1_IB(0.5)
// To reproduce morphology5 in Fig7D change to PYRcell[0].pcdend2[0] synapse = new A1_RS(0.5) 
// To reproduce morphology6 in Fig7D (distributed syn) Set one of synapses to PYRcell[0].pcdend2[1] synapse = new A1_RS(0.5) 
//To reproduce morphology7 in Fig7D (clustered syn) Set one of synapses to PYRcell[0].pcdend2[1] synapse = new A1_RS(0.5) 

PYRcell[0].pcdend1[0] synapse2 = new ACC_IB(0.5)
nc2 = new NetCon(vs2, synapse2)
vs2.play(evec2)
nc2.weight[0]=3 //this is for AMPA
nc2.weight[1]=3 //this is for NMDA

//*****************************************
objref stim
PYRcell[0].soma stim = new IClamp(0.5)
stim.del = 0 
stim.dur = 12000 //basically entire simulation period
stim.amp = -0.03 // Small current injetion to maintain the cell at ~ -75mV similar to slice experiments

//*************************************************************************************
// saving the memmbrane potential EPSP tarces to .txt file, uncomment this section if you want to the output file
/*objref  VmT
objref VmMat[npyrcell]  //npyrcell
VmT = new Vector()
for j=0,npyrcell-1 {
	VmMat[j] = new Vector()
	}

proc VecMx() { local j
	VmT.append(t)
	for j=0,0 {
		VmMat[j].append(gaba_syn[j].i)
		}
	}

strdef strmat
objref efile
efile = new File()

	efile.wopen("change file name here.txt")
	efile.printf("t\t")
for j = 0, 1 {
	efile.printf("%s\t",  gaba_syn[j])}
	efile.printf("\n")
        efile.close("change file name here.txt")

proc sMatrix(){ local  j
	efile.aopen("change file name here.txt")   
	efile.printf("%f\t", t)
	for j = 0, 1 {
	efile.printf("%f\t", gaba_syn[j].i)}
	efile.printf("\n")
	efile.close("change file name here.txt")

}*/

proc init() { local dtsav, temp, secsav
finitialize(v_init)
t = -1000
dtsav = dt
secondorder =0
dt= 10
	// if cvode is on, turn it off to do large fixed step
temp= cvode.active()
if (temp!=0) {cvode.active(0)}
while(t<-100) { fadvance() print t}
	//restore cvode if reqd
if (temp!=0) {cvode.active(1)}
dt = dtsav
secondorder =2
t = 0
if (cvode.active()){
cvode.re_init()
}else{
fcurrent()
}
}
proc continuerun() {local rt
	eventcount =0
	eventslow =1
	stoprun =0
	if (using_cvode_) {
	cvode.event($1)
	}
	while(t < $1 && stoprun == 0) {
	step()
	//sMatrix() // comment this code here if you want to supress the .txt file output, it's easy this way
	//VecMx() // comment this code here if you want to supress the .txt file output, it's easy this way
	rt = stopsw()
	if (rt > realtime) {
		realtime = rt
		if (!stdrun_quiet) fastflushPlot()
		doNotify()
		if (realtime == 2 && eventcount > 50) {
			eventslow = int(eventcount/50)+1
		}
		eventcount = 0
	}else{
		eventcount = eventcount +1
		if ((eventcount%eventslow) == 0) {
			doEvents()
		}
	}
	}
	flushPlot()
}

objectvar save_window_, rvp_
objectvar scene_vector_[4]
objectvar ocbox_, ocbox_list_, scene_, scene_list_
{ocbox_list_ = new List()  scene_list_ = new List()}

{
xpanel("RunControl", 0)
v_init = -60
xvalue("Init","v_init", 1,"stdinit()", 1, 1 )
xbutton("Init & Run","run()")
xbutton("Stop","stoprun=1")
runStopAt = 5
xvalue("Continue til","runStopAt", 1,"{continuerun(runStopAt) stoprun=1}", 1, 1 )
runStopIn = 1
xvalue("Continue for","runStopIn", 1,"{continuerun(t + runStopIn) stoprun=1}", 1, 1 )
xbutton("Single Step","steprun()")
t = 0
xvalue("t","t", 2 )
tstop =4500
xvalue("Tstop","tstop", 1,"tstop_changed()", 0, 1 )
dt = 0.1
xvalue("dt","dt", 1,"setdt()", 0, 1 )
steps_per_ms = 10	//40
xvalue("Points plotted/ms","steps_per_ms", 1,"setdt()", 0, 1 )
xpanel(544,121)
}
{
save_window_ = new Graph(0)
save_window_.size(0,100,-65,-40)
scene_vector_[2] = save_window_
{save_window_.view(2450, -65, tstop, 120, 290, 470, 579.84, 208)}
graphList[0].append(save_window_)
save_window_.save_name("graphList[0].")
save_window_.addexpr("PYRcell[0].soma.v(0.5)",2,2)
}


proc rrun(){
run()
}
rrun()
objectvar scene_vector_[1]
{doNotify()}