// mit_morph.hoc 
// Definition of mitral cell morphology and connections
// Andrew Davison 17-08-98


//********************************************************************************
// Definition of procedures used in this file
//********************************************************************************

proc read_parent() {
	parent = fscan()
}

//********************************************************************************
// Open input file and create sections
//********************************************************************************

create soma, axon[13], prim_dend[6], glom[94]
create sec_dendp1[3], sec_dendd1[8][5]
create sec_dendp2[3], sec_dendd2[8][5]
create sec_dendp3[3], sec_dendd3[10][5]
create sec_dendp4[3], sec_dendd4[6][5]
access soma

//********************************************************************************
// Connecting sections
//********************************************************************************

ropen("mitral.connect")	// open data file

print"<< Connecting sections >>"

soma connect axon[0](0), 1
for i = 0,11 {
   axon[i] connect axon[i+1](0), 1
}

print "<< axon connected to soma >>"

soma connect prim_dend[0](0), 0
for i = 0,4 {
   prim_dend[i] connect prim_dend[i+1](0), 1
}

print "<< primary dendrite connected to soma >>"

prim_dend[5] connect glom[0](0), 1
prim_dend[5] connect glom[47](0), 1

print "<< glomerulus connected to primary dendrite >>"

for i = 1,46 {
   read_parent()
   //print "Parent ",parent," connected to child ",i
   glom[parent] connect glom[i](0), 1
}

for i = 48,93 {
   read_parent()
   //print "Parent ",parent," connected to child ",i
   glom[parent] connect glom[i](0), 1
}

print "<< glomerulus connected up >>"

// proximal secondary dendrites

soma connect sec_dendp1[0](0), 0
soma connect sec_dendp2[0](0), 0
soma connect sec_dendp3[0](0), 0
soma connect sec_dendp4[0](0), 0

for i = 0,1 {
   sec_dendp1[i] connect sec_dendp1[i+1](0), 1
   sec_dendp2[i] connect sec_dendp2[i+1](0), 1
   sec_dendp3[i] connect sec_dendp3[i+1](0), 1
   sec_dendp4[i] connect sec_dendp4[i+1](0), 1
}

// distal secondary dendrites

for i = 0,5 {
   for j = 0,3 {
      sec_dendd1[i][j] connect sec_dendd1[i][j+1](0), 1
      sec_dendd2[i][j] connect sec_dendd2[i][j+1](0), 1
      sec_dendd3[i][j] connect sec_dendd3[i][j+1](0), 1
      sec_dendd4[i][j] connect sec_dendd4[i][j+1](0), 1
   }
} 

for i = 6,7 {
   for j = 0,3 {
      sec_dendd1[i][j] connect sec_dendd1[i][j+1](0), 1
      sec_dendd2[i][j] connect sec_dendd2[i][j+1](0), 1
      sec_dendd3[i][j] connect sec_dendd3[i][j+1](0), 1
   }
}

for i = 8,9 {
   for j = 0,3 {
      sec_dendd3[i][j] connect sec_dendd3[i][j+1](0), 1
   }
}


// connect proximal to distal
sec_dendp1[2] connect sec_dendd1[0][0](0), 1
sec_dendp1[2] connect sec_dendd1[7][0](0), 1
sec_dendp2[2] connect sec_dendd2[0][0](0), 1
sec_dendp2[2] connect sec_dendd2[7][0](0), 1
sec_dendp3[2] connect sec_dendd3[0][0](0), 1
sec_dendp3[2] connect sec_dendd3[9][0](0), 1
sec_dendp4[2] connect sec_dendd4[0][0](0), 1
sec_dendp4[2] connect sec_dendd4[5][0](0), 1

for i = 1,6 {
   read_parent()
   sec_dendd1[parent][4] connect sec_dendd1[i][0](0), 1
}

for i = 1,6 {
   read_parent()
   sec_dendd2[parent][4] connect sec_dendd2[i][0](0), 1
}

for i = 1,8 {
   read_parent()
   sec_dendd3[parent][4] connect sec_dendd3[i][0](0), 1
}

for i = 1,4 {
   read_parent()
   sec_dendd4[parent][4] connect sec_dendd4[i][0](0), 1
}

print "<< secondary dendrites all connected >>"

ropen("")	// close coordinate file

ropen("mitral.xyzd")

forall {
     pt3dadd(fscan(),fscan(),fscan(),fscan())
     pt3dadd(fscan(),fscan(),fscan(),fscan())
}

ropen("")

soma area(0.5)

// END OF FILE mit_morph.hoc