xopen("chan_KAf.ses")
xopen("tautables.hoc")

npdend = 4
nmdend = 8
nddend = 16
Cao0_Ca_ion =cao0_ca_ion = 5

objref ncl_AMPA, ncl_NMDA, ncl_GABA

create cell_soma, pdend[npdend], mdend[nmdend], ddend[nddend]

proc Topology() {

	printf("Building topology...\n")

	for i = 0, (npdend/2)-1{
		connect pdend[i](0), cell_soma(0)

		for m = 2*i, 2*i +1{
			connect mdend[m](0), pdend[i](1)
			for n = 2*m, 2*m+1{connect ddend[n](0), mdend[m](1)}
		}
	}

	for i = npdend/2, npdend-1{
		connect pdend[i](0), cell_soma(1)
		
		for m = 2*i, 2*i +1{
			connect mdend[m](0), pdend[i](1)
			for n = 2*m, 2*m+1{ connect ddend[n](0), mdend[m](1)}
		}
	}
	printf("Topology built.\n")
}

proc Geometry() {

	printf("Building geometry...\n")
	cell_soma diam = 16
	cell_soma L = 16
	
	for i = 0,npdend-1{
		pdend[i] diam = 2.25
		pdend[i] L = 20
		pdend[i] nseg = 1
	}
			
	for i = 0,nmdend-1{
		mdend[i] diam = 1.1
		mdend[i] L = 24.23
		mdend[i] nseg = 1
	}
	
	for i = 0,nddend-1{
		ddend[i] diam = 0.72
		ddend[i] L = 395.2
		ddend[i] nseg = 11
	}
	
	printf("Geometry built.\n")
}

proc Biophysics () {
	printf("Incorporating biophysics...\n")
	
	forall {
		insert pas
		insert NaF
		insert NaP
		insert KAf
		insert KAs
				
		insert CaQ //Pool cai // not responsible by itself
		insert CaN // Pool cai // not responsible by itself 
		insert CaKdyn //Pool cai 
		insert CaT // Pool Cai //not responsible by itself 
		insert CaL13 // Pool Cai//not responsible by itself
		insert Cadyn // Pool Cai
		
		insert CaL12 //Pool Cai// not responsible by itself
		insert CaR //Pool cai // not responsible by itself
		
		insert BKKCa // not responsible by itself
		insert SKKCa // responsible by itself
		insert KDR
		g_pas = 11.5e-6
		Ra = 100				
		ek = -90		
	}
	
	cell_soma {
		insert KRP
		gmax_NaF = 1.5
		gmax_NaP = 4e-5
		gmax_KAf = 0.225 
		gmax_KAs = 0.0104
	}
	
	for i = 0,npdend-1{
		pdend[i] insert KRP
		pdend[i] gmax_KRP = 0				
		pdend[i] gmax_KAf = 0.225
		pdend[i] gmax_KAs = 0.0104				
	}
	
	InsertKIR(eKir)
	SetKIRmtau()
	
		
	printf("Biophysics incorporated.\n")
}

proc Synapses() {
	printf("Adding synapses...\n")
	
	ncl_AMPA = new List()
	ncl_NMDA = new List()
	ncl_GABA = new List()
	
	cell_soma {
		for j = 0, 15 { ncl_GABA.append (new GABA (0.5))}
	}

	for i = 0,npdend-1{
		pdend[i] {
			ncl_AMPA.append (new AMPA(0.5))
			ncl_NMDA.append (new NMDA(0.5))
			for j = 0, 2 {ncl_GABA.append (new GABA (0.5)) }
		}
	}
	
	for i = 0,nmdend-1{
		mdend[i] {
			for j = 0, 2 {ncl_GABA.append (new GABA (0.5))}
			
			for j = 0, 1 {
				ncl_AMPA.append (new AMPA(0.5))
				ncl_NMDA.append (new NMDA(0.5))
			}
		}
	}

	for i = 0,nddend-1{
		ddend[i] {
			ncl_GABA.append (new GABA(0.33))
			ncl_GABA.append (new GABA(0.66))
		
			for(j = 0.2; j <= 0.8; j = j + 0.2) {
				ncl_NMDA.append (new NMDA(j))
				ncl_AMPA.append (new AMPA(j))
			}
		}
	}
	
	printf("Synapses added.\n")
}


proc InsertKIR() {
	
	if($1){
		forall{
			uninsert KIR
			insert inKIR
			}
		printf("KIR inactivation enabled\n")
		v_init = -86.66
		} else {
		forall{
			uninsert inKIR
			insert KIR
			}
		v_init = -87.746
		printf("KIR inactivation disabled\n")
	}
}

proc SetKIRmtau() {
	vecmtau_KIR.resize(0)
	vecmtau_KIR.append(0.09,0.26,0.633,1.947,3.12,10.755, 12.12, 13.795, 15.385, 14.285, 11.765, 8.89, 8) //35 deg C
	printf("KIR mtau set to adjusted values.\n")
	
}

proc CreateCell() {
	printf("Beginning cell creation....\n\n")
	Topology()
	Geometry()
	Biophysics()
	Synapses()
	printf("\nCell creation complete.\n")
}

proc D1I() {
	forall{gmax_NaF *= 0.95*$1 + !$1/0.95}	
	pmax_CaQ *= 0.5*$1 + !$1/0.5
	pmax_CaN *= 0.2*$1 + !$1/0.2
	pmax_CaL12 *= 2*$1 + !$1/2
	gmax_KIR *= 1.25*$1 + !$1/1.25
	gmax_inKIR *= 1.25*$1 + !$1/1.25
	m_vh_CaL13 += -10 * ($1 - !$1)
	if($1) {printf("\nD1I enabled")} else {printf("\nD1I disabled")}
}

proc D2I() {
	forall{gmax_NaF = gmax_NaF * (1.1*$1 + !$1/1.1)}	
	forall{gmax_KAs = gmax_KAs * (1.1*$1 + !$1/1.1)}	
	pmax_CaL13 = pmax_CaL13 * (0.75*$1 + !$1/0.75)
	h_vh_NaF += 3 * ($1 - !$1)
	if($1) {printf("\nD2I enabled")} else {printf("\nD2I disabled")}
}

proc D1S(){
	gbar_NMDA *= 1.3*$1 + !$1/1.3
	if($1) {printf("\nD1S enabled")} else {printf("\nD1S disabled")}
}

proc D2S(){
	gbar_AMPA *= 0.8*$1 + !$1/0.8
	if($1) {printf("\nD2S enabled")} else {printf("\nD2S disabled")}
}

proc DRChange() {
	if ($1==1){
		eDR = 1
		if(eMod != 0) {D2I(0) D1I(1)} 
		if(eMod == 2) {D2S(0) D1S(1)}
	} else {
		eDR = 2
		if(eMod != 0) {D1I(0) D2I(1)} 
		if(eMod == 2) {D1S(0) D2S(1)}
	}
	printf( "\nDopamine Receptor type changed." )
	if ( !eMod ) { printf( "\n(DA modulation is currently inactive.)" )
	} else if ( eMod == 1 ) { printf( "\nIntrinsic DA modulation with D%d receptors.", eDR ) 
	} else { printf( "\nIntrinsic-Synaptic DA modulation with D%d receptors.", eDR ) }
	
	printf( "\n" )
}

proc DModChange() {
	
	old_eMod = eMod
	eMod = $1
	
	if ( old_eMod != eMod ) {
		if ( eMod == 0 ) {
			if ( old_eMod == 2 ) {
				if ( eDR == 1 ) { D1S(0) } else { D2S(0) }
			}  
			if ( eDR == 1 ) { D1I(0) } else { D2I(0) }
			printf( "\nDopamine modulation deactivated." ) 
		}
		if ( eMod == 1 ) {
			if ( old_eMod == 2 ) {
				if ( eDR == 1 ) { D1S(0) } else { D2S(0) }
			} else { 
				if ( eDR == 1 ) { D1I(1) } else { D2I(1) }
			}
			printf( "\nIntrinsic DA modulation with D%d receptors.", eDR )
		}
		if ( eMod == 2 ) {
			if ( old_eMod == 1 ) {
				if ( eDR == 1 ) { D1S(1) } else { D2S(1) }
			} else { 
				if ( eDR == 1 ) { 
					D1I(1) 
					D1S(1) 
				} else { 
					D2I(1) 
					D2S(1) 
				}
			}
			printf( "\nIntrinsic-Synaptic DA modulation with D%d receptors.", eDR ) 
		}
	}

	printf( "\n" )

}