// Simulate spontaneous activity 
// Papoutsi, 2017

//Vectors of Spike trains
objref BG_basal[syn_basal], BG_apical[syn_apical], BG_Inh_soma[syn_inh_soma], BG_Inh_apical[syn_inh_apical], BG_Inh_basal[syn_inh_basal]
//Background synapses
objref bc_apical_ampa[syn_apical], bc_apical_nmda[syn_apical], bc_basal_ampa[syn_basal], bc_basal_nmda[syn_basal], bc_gaba_soma[syn_inh_soma], bc_gaba_apical[syn_inh_apical], bc_gaba_basal[syn_inh_basal]
//VecStims
objref vc_apical[syn_apical+1], vc_basal[syn_basal+1], vc_gaba_soma[syn_inh_soma+1], vc_gaba_apical[syn_inh_apical+1], vc_gaba_basal[syn_inh_basal+1]
//NetCons
objref nc_apical_ampa[syn_apical+1], nc_apical_nmda[syn_apical+1], nc_basal_ampa[syn_basal+1], nc_basal_nmda[syn_basal+1], nc_gaba_soma[syn_inh_soma+1], nc_gaba_apical[syn_inh_apical+1], nc_gaba_basal[syn_inh_basal+1]
//Random objects
objref r, r_time, r_time_inh

//Make distribution of synapses according to length
objref mat_bas, mat_ap, mat_inh_bas, mat_inh_ap,vd
mat_bas=new Vector()
mat_ap=new Vector()
mat_inh_bas=new Vector()
mat_inh_ap=new Vector()
tot_ap=0
tot_bas=0
for i=0, apical.count()-1 {tot_ap=tot_ap+apical.o(i).sec.L}
for i=0, apical.count()-1 {
	mat_ap.append(int((apical.o(i).sec.L/tot_ap)*syn_apical))
	mat_inh_ap.append(int((apical.o(i).sec.L/tot_ap)*syn_inh_apical))
}
for i=0, basal.count()-1 {tot_bas=tot_bas+basal.o(i).sec.L}
for pd=0,4 {
	for i=0, list_primary[pd].count()-1 {
		mat_bas.append(round((list_primary[pd].o(i).sec.L/tot_bas)*syn_basal))
		mat_inh_bas.append(round((list_primary[pd].o(i).sec.L/tot_bas)*syn_inh_basal))
	}	
}

proc background_activity () {	
	//Define random for position
	r=new Random(num_seed2)
	r.uniform(0,1)
	//and for poisson trains
	r_time=new Random(num_seed+1200)
	r_time.poisson(Hz/1000)

	r_time_inh=new Random(num_seed-7000)
	r_time_inh.poisson(Hz_inh/1000)
	//Basal	
	syn=0
	c_d=-1
	for pd=0,4 {
		for num=0, list_primary[pd].count()-1 {
			c_d=c_d+1
			for i=0, mat_bas.x(c_d)-1 {
				BG_basal[syn]=new Vector()
				for tt=0, tstop-1 {
					if (r_time.repick()){
						BG_basal[syn].append(tt)
					}
				}
				vc_basal[syn] = new VecStim(0.5)
				vc_basal[syn].delay = 0
				vc_basal[syn].play(BG_basal[syn])
				PID=r.repick()
				list_primary[pd].o(num).sec bc_basal_ampa[syn]=new GLU(PID)
				list_primary[pd].o(num).sec bc_basal_nmda[syn]=new nmda(PID)
				if (!cut_basal) {
					nc_basal_ampa[syn] = new NetCon(vc_basal[syn], bc_basal_ampa[syn], -20, 0, ampa_g)
					nc_basal_nmda[syn] = new NetCon(vc_basal[syn], bc_basal_nmda[syn], -20, 0, nmda_g)
				} else if (pd !=dend_id1 && pd!=dend_id2) {
					nc_basal_ampa[syn] = new NetCon(vc_basal[syn], bc_basal_ampa[syn], -20, 0, ampa_g)
					nc_basal_nmda[syn] = new NetCon(vc_basal[syn], bc_basal_nmda[syn], -20, 0, nmda_g)
				}
				syn=syn+1
			}
		}
	}
			
	//Apical
	syn=-1
	for num=0, apical.count()-1 {
		for i=0, mat_ap.x(num)-1 {
			syn=syn+1
			BG_apical[syn]=new Vector()
			for tt=0, tstop-1 {
				if (r_time.repick()){
					BG_apical[syn].append(tt)
				}
			}

			vc_apical[syn] = new VecStim(0.5)
			vc_apical[syn].delay = 0
			vc_apical[syn].play(BG_apical[syn])

			PID=r.repick()
			apical.o(num).sec bc_apical_ampa[syn]=new GLU(PID)
			apical.o(num).sec bc_apical_nmda[syn]=new nmda(PID)
			if (!ablated) {
				nc_apical_ampa[syn] = new NetCon(vc_apical[syn], bc_apical_ampa[syn], -20, 0, ampa_g)
				nc_apical_nmda[syn] = new NetCon(vc_apical[syn], bc_apical_nmda[syn], -20, 0, nmda_g)
			}
		}
	}
	
	for syn=0,syn_inh_soma-1 {
		BG_Inh_soma[syn]=new Vector()
		for tt=0, tstop-1 {
			if (r_time_inh.repick()){
				BG_Inh_soma[syn].append(tt)
			}
		}
		vc_gaba_soma[syn] = new VecStim(0.5)
		vc_gaba_soma[syn].delay = 0
		vc_gaba_soma[syn].play(BG_Inh_soma[syn])
		
		PID=r.repick()
		soma bc_gaba_soma[syn]=new GABAa(PID)
		nc_gaba_soma[syn] = new NetCon(vc_gaba_soma[syn], bc_gaba_soma[syn], -20, 0, gaba_g)
	}
	//Inhibition Basal	
	syn=0
	c_d=-1
	for pd=0,4 {
		for num=0, list_primary[pd].count()-1 {
			c_d=c_d+1
			for i=0, mat_inh_bas.x(c_d)-1 {
				BG_Inh_basal[syn]=new Vector()
				for tt=0, tstop-1 {
					if (r_time_inh.repick()){
						BG_Inh_basal[syn].append(tt)
					}
				}
				vc_gaba_basal[syn] = new VecStim(0.5)
				vc_gaba_basal[syn].delay = 0
				vc_gaba_basal[syn].play(BG_Inh_basal[syn])

				PID=r.repick()
				
				list_primary[pd].o(num).sec bc_gaba_basal[syn]=new GABAa(PID)
				if (!cut_basal) {
					nc_gaba_basal[syn] = new NetCon(vc_gaba_basal[syn], bc_gaba_basal[syn], -20, 0, gaba_g)
				} else if (pd !=dend_id1 && pd!=dend_id2) {
					nc_gaba_basal[syn] = new NetCon(vc_gaba_basal[syn], bc_gaba_basal[syn], -20, 0, gaba_g)
				}
				syn=syn+1
			}
		}
	}	
	
	syn=-1
	for num=0, apical.count()-1  {	
		for i=0, mat_inh_ap.x(num)-1 {
			syn=syn+1
			BG_Inh_apical[syn]=new Vector()
			for tt=0, tstop-1 {
				if (r_time_inh.repick()){
					BG_Inh_apical[syn].append(tt)
				}
			}
			vc_gaba_apical[syn] = new VecStim(0.5)
			vc_gaba_apical[syn].delay = 0
			vc_gaba_apical[syn].play(BG_Inh_apical[syn])

			PID=r.repick()

			apical.o(num).sec bc_gaba_apical[syn]=new GABAa(PID)
			if (!ablated) {
				nc_gaba_apical[syn] = new NetCon(vc_gaba_apical[syn], bc_gaba_apical[syn], -20, 0, gaba_g)
			}
		}
	}	
}