// Maurice Petroccine, SUNY Albany (mpetroccione@albany.edu)
// Last updated on 11/19/2021

// This code applies a single (AMPA) input to the dendrite of a ball and stick model and records the peak amplitude and t50 of the EPSC recorded from the soma.
// A simulation is run for each segment of the dendrite while systematically varying the weight and tau of the AMPA input.
// Time step: 0.025 ms
// Window duration: 400 ms
// to run type go("FileNameGoesHere") where you substitute what you want to name the file for FileNameGoesHere
// File will appear in the same folder as the hoc file after it is done (make sure simulation is finished before opening)
// Necessary files needed to run: 

load_file ("nrngui.hoc")
model_type = 1													// choose whether the model should be WT (1) or KO (2)
num_g_step = 20 												// the number of steps over which the input weight will be incremented
num_d_step = 50													// the number of steps over which to move the input along the dendrite (= to nseg)
wAMPA = 0.1														// the initial weight of the AMPA input
dt = 0.025														// time step of simulation

strdef preface, dirstr
preface = "."
sprint(dirstr, "%s/all_tau_vecs.hoc", preface)					// opens all_tau_vecs.hoc 
xopen(dirstr)

/***** TOPOLOGY *****/
create soma, dend												// creates soma and a dendrite 
connect dend(0), soma(1)							
access dend 													// sets access to the dendrite	

soma {
	L = 15														// um, length of the soma
	diam = 15													// um, diameter of the soma
	nseg = 1
}
dend {
	L = xxx														// um, length of the dendrite
	diam = 1.75													// um, starting diameter of the dendrite	
	nseg = 51													// # of segments
	diam(0:1) = 1.75:0.5										// tapers the diameter from 1.75 um at the at soma to 0.5 um at the tip
}

/***** BIOPHYSICS *****/
forall {
	Ra = 100													// MOhm, axial resistance
	cm = 1														// uF/cm2
}

soma {															// inserts conductances in the soma
	insert caL													// experimental data obtained using Cs+ based internal solution, therefore all K+ conductances were removed
	insert caL13
		pcaLbar_caL13 = 4.25e-7 
	insert car
	insert cat
	insert naf
		gnabar_naf = 0.16										 // sodium conductances
		hshift_naf = 0
	insert nap
		gnabar_nap = 0.0005
	insert caldyn												// inserts calcium dynamics in the soma
	insert cadyn												
		celsius = 23											// experimental data obtained at room temperature
}

dend {															// inserts conductances in the dendrite - see soma for more info
	insert naf
		gnabar_naf = 0.0195
	insert nap
		gnabar_nap = 1.38e-7
	insert caL
	insert caL13
		pcaLbar_caL13 = 4.25e-7 
	insert car
	insert cat
	insert caldyn
	insert cadyn
	ena = 60
}

celsius = 23													// recordings took place at room temperature			
cao = 1.2														// mM, 
ena = 60														// mV, sodium reversal potential
v_init = -70													// mV, initial voltage
cai0_ca_ion = 0.001												// mM, modifided, [Ca2+] in external solution  
cao0_ca_ion = 1.2												// mM, Churchill 1998 
cali0_cal_ion = 0.001											// mM, Churchill 1998
calo0_cal_ion = 1.2												// mM, modifided, [Ca2+] in external solution 
CAINF = 1e-5													// mM, steady state intracell ca conc.
TAUR = 43														// ms, time const of ca diffusion - Jackson 2003
CA_DRIVE = 10000
CA_PUMP = 0.02

proc set_cainf() {	NEW_CAINF = $1								//Ca2+ concentrations and dynamics, from Mattioni
	nCA_INF = NEW_CAINF
	forall if (ismembrane("cadyn")) {cainf_cadyn = NEW_CAINF}
	forall if (ismembrane("caldyn")) {cainf_caldyn = NEW_CAINF}
}

proc set_taur() {	NEW_TAUR = $1
	nCA_TAUR = NEW_TAUR
	forall if (ismembrane("cadyn")) {taur_cadyn = NEW_TAUR}
	forall if (ismembrane("caldyn")) {taur_caldyn = NEW_TAUR}
}
proc set_cadrive() { 	NEW_DRIVE = $1
	nCA_DRIVE = NEW_DRIVE
	forall if (ismembrane("cadyn")) {drive_cadyn = NEW_DRIVE}
	forall if (ismembrane("caldyn")) {drive_caldyn = NEW_DRIVE}
}
proc set_pump() {	NEW_PUMP = $1
	nCA_PUMP = NEW_PUMP
	forall if (ismembrane("cadyn")) {pump_cadyn = NEW_PUMP}
	forall if (ismembrane("caldyn")) {pump_caldyn = NEW_PUMP}
}

// Instrumentation
objref voltage_clamp								// define volatage clamp
	soma voltage_clamp = new Voltage_Clamp(0.5)		// place voltage clamp in the soma
	{voltage_clamp.dur1 = 1000 						// ms, clamp duration
		voltage_clamp.amp1= -70						// mV, holding potential
		voltage_clamp.rs= 0.001						// Mohm???, series resistance of voltage clamp
	} 

// Graphical display 
objref g											// create a new graph that displays the voltage at the soma 
g = new Graph()
addplot(g,0)
g.exec_menu("Keep Lines")
g.size(0,50,-80,40)
g.addvar("soma.v(0.5)", 1, 1, 0.6, 0.9, 2)
g.addvar("dend.v(0.7)", 2, 1, 0.6, 0.9, 2)
g.addvar("dend.v(0.1)", 3, 1, 0.6, 0.9, 2)

objref g2											// creates a new graph that plots the holding current
	g2 = new Graph()
	addplot(g2,1)
	g2.exec_menu("Keep Lines")
	g2.size(0,1000,-80,40)
	g2.addvar("voltage_clamp.i")
	//g2.addexpr("c1.i")

//Sim control										

objref  recI										// Creates vector object, records I at the soma
	recI = new Vector()
	recI.record(&voltage_clamp.i)

objref iMin, tMin, iChange, it50, t50				// creates vectors to track:
	t50 = new Vector (101,0)						// t50	
	it50 = new Vector (101,0)						// holding current at the time of the t50
	iMin = new Vector(101,0)						// peak epsc amplitude (includes holding current)
	tMin = new Vector(101,0)						// the time of the epsc peak 
	iChange = new Vector(101,0)						// the difference between the holding current and the peak epsc amplitude
	
objref diststep										 
	diststep = new Vector(40000)					
	diststep.indgen(0,0.025)						 
													
objref distdrop										// creates a new graph - later used to plot V vs Dist
	distdrop = new Graph()
	distdrop.size(0,200,-1,0)						// SCALE OF GRAPH IS WRONG ****FIX******
	distdrop.exec_menu("Keep Lines")				// Look into plotting families of lines or vectors. Also how to label 

objref test [num_g_step]							// creates test
objref minI [num_g_step]							// creates minI

objref tfil											// creates a new file for storing the output data
	tfil = new File()
	
strdef tmpstr
strdef gstepname
strdef taustepname

objref syn_ampa, syn_nmda, syn_ampa2, syn_nmda2, ns2, nc, nc3, nc4, nc2, ns

	ns = new NetStim(0.5)							// creates a netstim to trigger an input that starts at 10 ms
		ns.interval = 1
		ns.start = 10
		ns.noise = 0
		ns.number = 1

	dend syn_ampa = new AMPA(0.1)					// inserts a input (AMPA synapse) into the dendrite
		
	nc3 = new NetCon(ns, syn_ampa)					// a netcon linking the AMPA input to the netstim
	nc3.weight = 2
	nc3.delay = 0
	 

objref avgI, tmp									//	
avgI = new Vector() 								// discard whatever is already in avg
tmp = new Vector()

proc initialize() {									// Sets model to initial state (time, voltage)
	t = 0
	finitialize(v_init)
		cai0_ca_ion = 0.001							// mM, Churchill 1998
		cao0_ca_ion = 1.2							// mM, Churchill 1998 - gives eca = 100 mV
		cali0_cal_ion = 0.001						// mM, Churchill 1998
		calo0_cal_ion = 1.2	
		CAINF = 1e-5								// mM, steady state intracell ca conc.
		TAUR = 43									// ms, time const of ca diffusion - Jackson 2003
		CA_DRIVE = 10000
		CA_PUMP = 0.02

	fcurrent()
}

proc integrate() {									// fadvance should be moving the sim forward, 
	//g.begin()										// g.plot(t) updates the graph in real time
	while (t<tstop) {
		fadvance()
		//g.plot(t)
		g2.plot(t)
	}	
	//g.flush()
	g2.flush()
}

proc go() {											// the run command - note: does not work without have the access set to the dendrite		
	sprint(tmpstr, "%s.dat", $s1)					// creates a string for the output file name
	tfil.wopen(tmpstr)								// opens that file 
	for z = 0,(num_g_step-1) {						// runs simulations for the num_g_steps
		test[z] = new Vector()						
		minI[z] = new Vector()	
		tstop = 400+ z*10							// simulation stops after 400 ms (10 ms additionally added on per loop)
		tau_d_AMPA = 35.5 * ((z*0.05)+0.05)			// each loop tau for AMPA is incremented by 5% of it's initial value
				
		nc3 = new NetCon(ns, syn_ampa)
		nc3.weight = 0.1*wAMPA						// 0.28 is WT, 0.235 for TBOA-WT, 0.2 for KO, Tau_d_AMPA is 35.5 for WT and 17 for KO
		nc3.delay = 140								// sets delay for input to be 140 msec (to allow voltage clamp to stabilize at -70 mV)
		
		changel()									//
		test[z].append(t50.x[z])					// appends t50 to test vector at position z
		minI[z].append(iMin.x[z])					// append iMin to minI vector at position z 

		print z										// This prints an update every 100 loops - comment out to run faster simulations
		j = 0 										// resets j
	}
	xytofile(test[w], tmpstr)						// writes test vector to the output file
	tfil.close()									// closes the file.
}

proc changel() {									// procedure for moving the stimulation point down the dendrite, input location is based on counter "j" 
	for i = 0,(num_d_step-1) {						// NEURON doesn't accept point processes at location "1" - this for loop places the the input in the most distal segment if j=1
		if (j>=1){
			j=0.999
		}
		
		syn_ampa.loc(j+0.00001)						// sets dendritic input location based on the loop
		tmp.record(&voltage_clamp.i)				// record the current applied by voltage_clamp to a temporary file (tmp)
		initialize()								// initialize the individual simulation
		integrate()									// run the individual simulation
		
		j=j+(1/num_d_step)							// moves the axon based on the number of distance steps
		
		if (i==0) {
			avgI.copy(tmp)  // copy tmp to avg		// when the first simulation is run copy tmp to avgI
		}else {										
			avgI.add(tmp)							// for subsequent runs add tmp to avgI element by element
		}
		if (i== num_d_step-1) {						
			avgI.div(num_d_step)					// ####on the last set of simluations run for each distance-loop, divide to get the average current response
			iMin.x[z] = avgI.x[5000]				// ???
			avgI.sub(iMin.x[z])						// subtract the holding current from the overall current injected by voltage_clamp
			avgI.line(distdrop,diststep)			// plots current-response to graph
			iMin.x[z] = avgI.min(100,12000)			// sets iMin for this loop to the peak current amplitude between point 100 and 12000
			avgI.div(iMin.x[z])						// normalizes to peak
			tMin.x[z] = avgI.max_ind()				// makes note of data point where the peak occurs
			avgI.remove(0,tMin.x[z])				// removes all points before the peak of the epsc
			it50.x[z] = avgI.indwhere("<=", 0.5)	// it50 for this loop is set to the first value where the the decays to 50% or less
			t50.x[z] = it50.x[z]*0.025				// it50 is scaled by tstep to give the time in ms, which is recorded as the t50
			avgI.resize(0)							// resizes avgI to 0 to clear all values for next loop
		}
	}
}

proc xytofile() { local b							//Procedure to write the t50 and peak amplitude of the epsc to two separate
	w=0
	print "writing to ", $s2						// notifies when writing to file
	for w=0,num_g_step-1 {							// writes the gstep and wAMPA at the beginning of the file
		if (w==0){gsteplabel(wAMPA)
		tfil.printf("\n%s\n", taustepname)
		}					
		for b=0,$o1.size()-1
		tfil.printf("%g\n", test[w].x[b])
	}
	for w=0,num_g_step-1 {
		if (w==0){gsteplabel(wAMPA)
		tfil.printf("\nAmplitude\n", taustepname)	// prints the peak amplitude of the epsc to the file
		for b=0,$o1.size()-1 
		}											
		tfil.printf("%g\n", minI[w].x[b])
	}
}
 
 proc gsteplabel (){								// writes the tau d in the first line of the file
	sprint(taustepname, "The t50 for tau_d =\t%g", $1)	
}