// Vierling-Claassen 2010
// Processes to generate fixed random synaptic connections, connection files generated by 
// light_genconn.m 
///////////////

// Directory for fixed random connectivity, generated by light_genconn.m.  
// Input files are four columns (x_pre y_pre x_post y_post), one row for each connection
strdef Con_FN
Con_FN="./confiles/con_2-3-2010-1/"

///////////
// Pyramidal to Pyramidal connection
///////////

proc IItoII_fr(){ local weight, delay, distX, distY, distance, preX, preY, postX, postY localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connects Layer II/III to Layer II/III Pyramidals
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// Usage: IItoII( Max_Weight, Space constant, compartment [0-7] receptor type
// [0=ampa, 1=nmda],
// minimum_delay, delay_space_constant )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_EE.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=con_x1.x[k]
	postX=con_x2.x[k]
	preY=con_y1.x[k]
	postY=con_y2.x[k]
	
	distX = abs(preX-postX)
   distY = abs(preY-postY)
   distance = sqrt(distX^2 + distY^2)
   weight = $1 * exp( -(distance^2) / ( $2^2) )
   delay = $5 * 1 / (exp( -(distance^2) / ( $6^2) ) )

	rconnect(PL2[preX][preY], PL2[postX][postY], $3, $4, weight, delay, 0) 
	
} // end loop through connections
print "connected II to II, Num Cons: ", NumCon
} // end process loop

//////////////////////////////////////////
// Inhibitory to Pyramidal connections 
//////////////////////////////////////////

proc IL2toII_fr(){ local weight,delay, distX, distY, distance, preX, preY, postX, postY localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connects Layer II/III INHIBitory Basket cells to Layer II/III Pyramidals
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
 // Note that loop is through connections, not through grid as in previous connection processes
// usage: IL2toII( L.2 Pyram_compartment [0-8], 
//        receptor type [ 2=GABAa, 3-GABAb ],
//        max_weight, weight_space_constant [>1],
//        min-delay [ms.] , delay-space-constant [>1] )
//

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_IE.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k] // multiple accounts for spacing between I cells in grid
	postX=con_x2.x[k]
	preY=2*con_y1.x[k]  // multiple accounts for spacing between I cells in grid
	postY=con_y2.x[k]

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $3 * exp( -(distance^2) / ( $4^2) )
    delay = $5 * 1 / (exp( -(distance^2) / ( $6^2) ) )


   rconnect(IPL2[preX][preY], PL2[postX][postY], $1, $2, weight, delay, 0) 
} 
print "connected IL2 to II, Num Cons: ", NumCon
}

//----------------------------

proc SL2toII_fr(){ local weight,delay, distX, distY, distance, preX, preY, postX, postY localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connects Layer II/III INHIBitory Som cells to Layer II/III Pyramidals
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// usage: SL2toII( L.2 Pyram_compartment [0-8], 
//        receptor type [ 2=GABAa, 3-GABAb ],
//        max_weight, weight_space_constant [>1],
//        min-delay [ms.] , delay-space-constant [>1] )
//

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_SE.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k]+1 // multiple accounts for spacing between inhib cells in grid
	postX=con_x2.x[k]
	preY=2*con_y1.x[k]+1  // multiple accounts for spacing between inhib cells in grid
	postY=con_y2.x[k]

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $3 * exp( -(distance^2) / ( $4^2) )
    delay = $5 * 1 / (exp( -(distance^2) / ( $6^2) ) )

       rconnect(SPL2[preX][preY], PL2[postX][postY], $1, $2, weight, delay, 0) 
} 
print "connected SL2 to II, Num Cons: ", NumCon
} 

//////////////////////////////////////////
// Inhibitory to inhibitory, same population
//////////////////////////////////////////

proc IL2toIL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connect Layer II/III inhibitory cells to themselves, included target section
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// usage: IL2toIL2(Target Section in layer 2 basket, Max_Weight., Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_II.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k] // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]
	preY=2*con_y1.x[k]  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )

    rconnect(IPL2[preX][preY],IPL2[postX][postY],0,$6,weight,delay,0)
}
print "connected IL2 to IL2, Num Cons: ", NumCon
} 

//--------------------

proc SL2toSL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2
// Connect Layer II/III som cells to themselves, usually zero, but included for completeness
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// usage: SL2toSL2(Target Section in layer 2 som, Max_Weight., Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_SS.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k]+1 // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]+1
	preY=2*con_y1.x[k]+1  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]+1

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )


   rconnect(SPL2[preX][preY],SPL2[postX][postY],0,$6,weight,delay,0)
} 
print "connected SL2 to SL2, Num Cons: ", NumCon
} 

//////////////////////////////////////////
// Inhibitory to inhibitory, across populations
//////////////////////////////////////////

proc IL2toSL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connect Layer II/III inhibitory cells to themselves, included target section
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// usage: IL2toIL2(Target Section in layer 2 basket, Max_Weight., Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_IS.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k] // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]+1
	preY=2*con_y1.x[k]  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]+1

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )

    rconnect(IPL2[preX][preY],SPL2[postX][postY],0,$6,weight,delay,0)
}
print "connected IL2 to SL2, Num Cons: ", NumCon
} 

//-----------------------

proc SL2toIL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2
// Connect Layer II/III som cells to themselves, usually zero, but included for completeness
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// usage: SL2toSL2(Target Section in layer 2 som, Max_Weight., Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_SI.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	//print con_x1.x[k], con_y1.x[k], con_x2.x[k], con_y2.x[k]
	preX=3*con_x1.x[k]+1 // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]
	preY=2*con_y1.x[k]+1  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]

    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )


   rconnect(SPL2[preX][preY],IPL2[postX][postY],0,$6,weight,delay,0)
} 
print "connected SL2 to IL2, Num Cons: ", NumCon
}

//////////////////////////////////////////
//Pyramidal to Inhibitory Connections
////////////////////////////////////////////

proc IItoIL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connects Layer II/III Pyramidal to Layer II/III Inhibitory basket cells
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// Usage: IItoIL2(Target Section in layer 2 basket, Max_Weight, Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_EI.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	preX=con_x1.x[k] // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]
	preY=con_y1.x[k]  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]
    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )


       rconnect(PL2[preX][preY], IPL2[postX][postY], 0, $6, weight, delay, 0) 
} 
print "connected II to IL2, Num Cons: ", NumCon
}

//------------------------------

proc IItoSL2_fr(){ local preX, preY, postX, postY, distX, distY, distance, weight, delay localobj con_file, con_x1, con_x2, con_y1, con_y2

// Connects Layer II/III Pyramidal to Layer II/III Inhibitory basket cells
 // with fixed random connectivity read in from files generated by light_genconn.m
 // Each file read in contains the x and y coordinates of the pre and post synaptic cells in the grid
 // order is xpre, ypre, xpost, y post
// Usage: IItoIL2(Target Section in layer 2 basket, Max_Weight, Space_Constant, Min_Delay, Delay_Space_Constant, receptor_type )

con_file = new File()
strdef con_str
sprint(con_str,"%s%s",Con_FN,"Con_ES.dat")
con_file.ropen(con_str)
con_x1 = new Vector()
con_y1 = new Vector()
con_x2 = new Vector()
con_y2 = new Vector()

con_x1.scanf(con_file,1,4)
con_file.seek
con_y1.scanf(con_file,2,4)
con_file.seek
con_x2.scanf(con_file,3,4)
con_file.seek
con_y2.scanf(con_file,4,4)

NumCon=con_x1.size

for k=0,NumCon-1{
	preX=con_x1.x[k] // multiple accounts for spacing between inhib cells in grid
	postX=3*con_x2.x[k]+1
	preY=con_y1.x[k]  // multiple accounts for spacing between inhib cells in grid
	postY=2*con_y2.x[k]+1
    distX = abs(preX-postX)
    distY = abs(preY-postY)
    distance = sqrt(distX^2 + distY^2)
    weight = $2 * exp( -(distance^2) / ( $3^2) )
    delay = $4 * 1 / (exp( -(distance^2) / ( $5^2) ) )


       rconnect(PL2[preX][preY], SPL2[postX][postY], 0, $6, weight, delay, 0) 
} 
print "connected II to SL2, Num Cons: ", NumCon
}