// Disperse synaptic allocation of synapses in FS BCs.  


//Initialize NEURON
load_file("nrngui.hoc")  
v_init=-68        // vrest
cvode.active(0)     
//-----Objects for record data
objref cv
cv=new CVode(0)
tstop=1000 //2000//500
steps_per_ms=10
dt=1/steps_per_ms
n=int(tstop/dt)

objref all_msec
all_msec = new Vector(n,0)
for q=0,n-1 {all_msec.x[q]=q*dt}
				  
xopen("tempSomogyi1.hoc") //template with mechanisms
objref FSdetailedtemplate
FSdetailedtemplate = new FScell("Somogyi_1.hoc")	// morphology reconstruction 	
xopen("../bash_templates/current_balance_fs.hoc")
current_balanceFS(-68)
xopen("../bash_templates/basic-graphics.hoc") 
addgraph("FSdetailedtemplate.soma.v(0.5)",-90,50)

//-----------------------------------------------------------------------------------------
prox = 0
forsec FSdetailedtemplate.basal_prox {          //ctpr= basal prox
	prox = prox +1
	}

dist= 0

forsec FSdetailedtemplate.basal_dist {  //ctrd= basal dist
	dist = dist +1
	}

number_dends = prox + dist  
maxsyndend=60// max excitatory synapses in dendrites	 
            
objref vecstim[maxsyndend] // excitatory  
mean=0.02 //50 Hz

objref rp
rp = new Random()
rp.poisson(mean)
print"123"
objref stimvector[maxsyndend]
for t=0,maxsyndend-1{
	stimvector[t]= new Vector()
	for k=0,int(tstop)-1{
		if(rp.repick()){
			stimvector[t].append(k)
		}
	}
}

 

//----------------------------------record&save
objref vsoma, FSv, FSt, vdend

proc rec_soma_Voltage(){
	FSv=new Vector(n)
	FSt=new Vector(n) 
	for j=0,n-1 {FSt.x[j]=j*dt }
	FSdetailedtemplate.soma cv.record(&FSdetailedtemplate.soma.v(0.5),FSv,FSt,1)
}
strdef temp
proc save_soma_Voltage() { localobj vect
	
	vsoma = new File()		
	sprint(temp,"Cell_io/cluster/Hipp/Jan19/Hipp_1/all/sparseall/sparseall_5/Synapses_%d_seeddend_%d_seedpid.txt", $1, $2, $3)   
	vsoma.wopen(temp)
	for sb=0, FSv/*[$1][$2][$3]*/.size()-1 { 
	vsoma.printf ("%f\n",FSv/*[$1][$2][$3]*/.x[sb])
	}
	vsoma.close()

	vect = $o4
	vdend = new File()		
	sprint(temp,"Cell_io/cluster/Hipp/Jan19/Hipp_1all/sparseall/dend_sparseall_5/Synapses_%d_seeddend_%d.txt", $1,$2)  
	vdend.wopen(temp)
	for k=0,vect.size()-1 { 
		vdend.printf ("%d\n",vect.x[k])
	}
	vdend.close()
}

   
//.........................Autapse.................
PV2PVmaxsyn=12  
objref gabaain[PV2PVmaxsyn], congabaain[PV2PVmaxsyn]
proc self_inhibition() {local delstimpv localobj fpvpv

	fpvpv = new Random(100)
	delstimpv=fpvpv.normal(0.6,0.2)
	for a=0, (PV2PVmaxsyn-1) {
		FSdetailedtemplate.soma {gabaain[a]=new GABAain(0.5) }
		delstimpv = fpvpv.repick()
		if (delstimpv<0) {delstimpv=delstimpv*(-1)}
		FSdetailedtemplate.axon {congabaain[0] = new NetCon(&v(1), gabaain[0], -20, delstimpv, 5.1e-4*14)}
	}
}
//------------------------------------------------------
objref fpin,  r
fpin = new Random()                        //delstim
fpin.normal(0.6, 0.2)

//------------------------------------------------------------creat the pool of dends----------------------------------------------------------------
objref dendpool

faulty= 54//Somogyi_1
//faulty=12 //Somogyi_2
//faulty=7 //Somogyi_3
//faulty=13// Somogyi_4
//faulty=13 //Somogyi_5

//faulty= 12//DR-int PFC 64
//faulty= 7//new_DR-rat 13  PFC 47
//faulty= 11//Mar_11


dendnum=number_dends-faulty 
print dendnum		//number of correct dends
dendpool= new Vector(dendnum)		//the vector of correct dends
j=0
for i=0,number_dends-1{

if (i==0||i==1||i==2||i==9||i==13||i==15||i==18||i==21||i==33||i==34||i==35||i==38||i==40||i==44||i==48||i==57||i==59||i==65||i==67||i==69||i==70||i==73||i==82||i==93||i==98||i==108||i==111||i==115||i==116||i==117||i==121||i==127||i==134||i==135||i==137||i==138||i==139||i==141||i==145||i==146||i==148||i==149||i==154||i==157||i==160||i==164||i==171||i==182||i==173||i==187||i==190||i==195||i==198||i==210){ //Somogyi_1

//if (i==0||i==5||i==11||i==12||i==13||i==14||i==15||i==16||i==21||i==27||i==40||i==4) { //Som_2 correct 63-12=51

//if (i==0||i==3||i==9||i==13||i==26||i==41||i==48){ // Somogyi 3. 58-7=51 seeddend=0,50

//if (i==0||i==6||i==40||i==66||i==89||i==90||i==91||i==92||i==111||i==112||i==126||i==161||i==179) { //Som_4 0.186

//if (i==0||i==1||i==14||i==15||i==19||i==27||i==33||i==32||i==37||i==41||i==47||i==48||i==54) { //Som_5 0.58


//PFC
//if (i==0||i==9||i==16||i==28||i==31||i==36||i==41){ //PFC 47 48-7=41 seeddend=0,40

//if (i==0||i==12||i==13||i==14||i==23||i==24||i==29||i==32||i==43||i==48||i==50||i==64){ //PFC 64 65-12=53 seeddend=0,52
	
//if (i==0||i==6||i==14||i==17||i==27||i==34||i==52||i==53||i==55||i==56||i==65){ //Mar_11

 
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

		continue
	}else{
		dendpool.x[j]=i
		j=j+1
		} // else

}


//------------------------------------------------------------------------------------------------------------------------------------------------------
objref rd

objref ampa[maxsyndend],nmda[maxsyndend]
objref conampa[maxsyndend], connmda[maxsyndend]     

print "dendpool.size", dendpool.size
objref randomDendsPerSyn
for runs=0,11{ // allsynapses / step

	synapsesPerRun = 5+ runs*5//step.	

	for k=0, (dendpool.size -1) {
		//print "OK"
		FSdetailedtemplate.dend[k].nseg = synapsesPerRun

	}	

	print synapsesPerRun 
	print "runs=", runs 
	for seeddend= 0,dendnum-1 { //used for different random dendrites in each run			                              
		rd = new Random(seeddend*100)
		rd.uniform(0, dendnum-1)        // mix dendrites randomly
		delstim=fpin.repick()
		print "runs=", runs
		print synapsesPerRun 	
		randomDendsPerSyn = new Vector(synapsesPerRun,0)
		
              for k = 0 , randomDendsPerSyn.size()-1{
		    randomDendsPerSyn.x[k] = dendpool.x[(rd.uniform(0, dendpool.size()-1))] 
		    print "Dendrite selected is", randomDendsPerSyn.x[k]
		} 

	  	for seedpid=0,0{//4{ //used for random allocation of synapses in different segments of the same dendrite.
			r = new Random(seedpid*100)
			r.uniform(0, 1.0)
                        print "Stimulating and running the above dendritic cluster with different PID!"
			self_inhibition(runs)
			print "runs=", runs
	                print synapsesPerRun 
			           
		       	for l=0, (randomDendsPerSyn.size()-1) {
			
			    	PID = r.uniform(0., 1.0) 
			    	randomDend = randomDendsPerSyn.x[l]
                	        print PID
			     	FSdetailedtemplate.dend[randomDend] ampa[l] = new CPGLUIN(PID)
			     	FSdetailedtemplate.dend[randomDend] nmda[l] = new NMDAIN(PID)

			     	vecstim[l] = new VecStim(0.5)
			     	vecstim[l].delay =0
			     	vecstim[l].play(stimvector[l])
			//---------------------------------------------------------------
			     	delstim=fpin.repick()                             		                    
			     	if (delstim<0) {delstim=delstim*(-1) } 
			     	conampa[l] = new NetCon(vecstim[l], ampa[l], -20, delstim, 7.5e-4)  
			     	connmda[l] = new NetCon(vecstim[l], nmda[l], -20, delstim, 3.2e-4*5)
				} //l

                        self_inhibition()
			rec_soma_Voltage()
			run()
			//save_soma_Voltage(runs,seeddend,seedpid,randomDendsPerSyn)

	} //seedpid
    } //seeddend				
} // runs