// Single pulse synaptic stimulation of two stimuli (A, C) located
// on two different tip sections individually (A, C) and together (A+C).
// This experiment is used to show how A+C type stimualtion can be linear or sublinear. 

// No inhibition is used in the single pulse experiments.
// The variable "times" is used to select among a set of blockade cases to test
// times: 0 = control , 1 = block_A, 2 =  block_NMDA, 3 = block_A_NMDA, 4 = block_Na, 5 =  block_Ca

times = 0

//load_proc("nrnmainmenu")          
//load_template("ExperimentControl")  // load needed templates
//load_template("EPSPTuning")  
//load_template("RangeRef")         
load_file("nrngui.hoc")
load_file("../../template/load_templates.hoc")

objref econ                         // initialize template parameters
show_errs=1
debug_lev=1
econ=new ExperimentControl(show_errs,debug_lev)
econ.self_define(econ)
econ.morphology_dir = "../../morphology/n123"                                // set location for morphology files
econ.add_lib_dir("Terrence","../../lib")                                     // set location for library files
econ.generic_dir    = "../../experiment/"                                    // set location for cell-setup file

econ.data_dir  = "data"                                                     // set directory to store data
sprint(econ.syscmd, "mkdir -p %s", econ.data_dir)
system(econ.syscmd)
                                   // maximum nseg number
actual_resolution=75               // used in ..lib/choose-secs.hoc (not currently) to count 
desired_resolution=1               // how many copies  of a given synapse to put in the band

econ.xopen_geometry_dependent("cell")                                        // load raw cell morphology	
econ.xopen_geometry_dependent("cell-analysis")                               // load user-defined semantics on morphology
cell_analysis(econ)

printf("Opening cell setup\n")                                               // load cell-setup to	
econ.xopen_generic("cell-setup")                                             // specify all mechanisms,	
printf("Opened. Setting up cell\n")                                          // membrane properties etc
maximum_segment_length=actual_resolution
cell_setup(econ)

// Set simulation parameters for the experiment 

econ.defvar("Simulation Control", "tstop", "250", "Defines when the simulation stops.")
econ.defvar("Simulation Control", "dt", "0.1", "Timestep")
econ.defvar("Simulation Control", "steps_per_ms", "10", "How many points are plotted per ms")
setdt()

// open files with NMDA/AMPA ratios
econ.xopen_geometry_dependent("nmda-ampa-ratio")

// Open file with tuned AMPA conductance values for all sections
strdef tunings_file, select, temp, accstr
objref tune_epsp_list
tune_epsp_list=new List()
sprint(tunings_file, "%s", "tunings")
xopen("../tune-synapses/tunings.dat")

// Open library functions that will be needed

econ.xopen_library("Terrence","choose-secs")    // used to randomly select sections from a list
econ.xopen_library("Terrence","salloc")         // used to allocate synapses on sections
econ.xopen_library("Terrence","deduce-ratio")   // used to extract NMDA/AMPA ratios
econ.xopen_library("Terrence","basic-graphics") // used to plot graphics 
econ.xopen_library("Terrence","spikecount")     // used to count spikes

temporal_offset=10  // synapses are stimulated simultaneously after 10 ms

all_synapses = 100     // Maximum number of AMPA/NMDA synapses
objref ampa[all_synapses], nmda[all_synapses],  splot
objref somavrec, vf, vtip[2], somarecf, dendrecf
objref tip_list, vsoma, rpid, sl, Branch_ref[all_synapses]

double synapses[2], maxdendrec[2], meandendrec[2]
synapses[0] = 0
synapses[1] = 0
max_cases = 41
double all_syn[2*max_cases]
max_syn = 14

//create all synapse pairs for stimuli A, B

all_syn[0] = 2
all_syn[1] = 2
all_syn[2] = 2
all_syn[3] = 4
all_syn[4] = 4
all_syn[5] = 2
all_syn[6] = 2
all_syn[7] = 6
all_syn[8] = 6
all_syn[9] = 2
all_syn[10] = 2
all_syn[11] = 8
all_syn[12] = 8
all_syn[13] = 2
all_syn[14] = 2
all_syn[15] = 10
all_syn[16] = 10
all_syn[17] = 2
all_syn[18] = 2
all_syn[19] = 14
all_syn[20] = 14
all_syn[21] = 2
all_syn[22] = 4
all_syn[23] = 6
all_syn[24] = 6
all_syn[25] = 4
all_syn[26] = 4
all_syn[27] = 8
all_syn[28] = 8
all_syn[29] = 4
all_syn[30] = 4
all_syn[31] = 10
all_syn[32] = 10
all_syn[33] = 4
all_syn[34] = 4
all_syn[35] = 12
all_syn[36] = 12
all_syn[37] = 4
all_syn[38] = 4
all_syn[39] = 14
all_syn[40] = 14
all_syn[41] = 4
all_syn[42] = 6
all_syn[43] = 8
all_syn[44] = 8
all_syn[45] = 6
all_syn[46] = 6
all_syn[47] = 10
all_syn[48] = 10
all_syn[49] = 6
all_syn[50] = 6
all_syn[51] = 12
all_syn[52] = 12
all_syn[53] = 6
all_syn[54] = 6
all_syn[55] = 14
all_syn[56] = 14
all_syn[57] = 6
all_syn[58] = 8
all_syn[59] = 10
all_syn[60] = 10
all_syn[61] = 8
all_syn[62] = 8
all_syn[63] = 12
all_syn[64] = 12
all_syn[65] = 8
all_syn[66] = 8
all_syn[67] = 14
all_syn[68] = 14
all_syn[69] = 8
all_syn[70] = 10
all_syn[71] = 12
all_syn[72] = 12
all_syn[73] = 10
all_syn[74] = 10
all_syn[75] = 14
all_syn[76] = 14
all_syn[77] = 10
all_syn[78] = 12
all_syn[79] = 14
all_syn[80] = 14
all_syn[81] = 12


// Make a list of all sections to choose sections from
tip_list=new SectionList()
forsec apical_tip_list {
       tip_list.append()
}
forsec apical_tip_list_addendum {
       tip_list.append()
}

// make a reference list for all sections used
m=0
forsec tip_list {
   Branch_ref[m] = new SectionRef() 
   m = m + 1
}

Deadtime_GLU=dt
Deadtime_NMDA=dt
hertz=2				         // frequency of stimulation for all synapses: single shock 
synch=1					 // synapses are stimulated synchronously
perio=0					 // spike trains for each synapse are NOT periodic
dendritic_spike_threshold=-25
somatic_spike_threshold=0 

//Proceedures for the different cases to be tested

proc Ca_block(){   // Block all Ca++ channels
  forall if(ismembrane("cat")) { 
     for (x) { gcatbar_cat(x) = 0 }
  }
  forall if(ismembrane("calH")) { 
     for (x) { gcalbar_calH(x) = 0 }
  }
  forall if(ismembrane("cal")) { 
     for (x) { gcalbar_cal(x) = 0 }
  }
  forall if(ismembrane("car")) { 
     for (x) { gcabar_car(x) = 0 }
  }
  forall if(ismembrane("somacar")) { 
     for (x) { gcabar_somacar(x) = 0 }
  }
}

proc Na_block(){  // Block all Na+ channels
  forall if(ismembrane("hha2")) { 
     for (x) { gnabar_hha2(x) = 0 }
  }
  forall if(ismembrane("hha_old")) { 
     for (x) { gnabar_hha_old(x) = 0 }
  }
}

proc NMDA_block(){  // Block NMDA current 
 for i=0, synapses[0] + synapses[1] -1 {
   nmda[i].gmax = 0  
 }
}

proc A_block() {  // Block all A-type K+ channels 
f = 0
forall if(ismembrane("kad")) { // distal conductances
     for(x) { gkabar_kad(x)= gkabar_kad(x)*f }
  } else if(ismembrane("kap")) { // proximal conductances
     for(x) { gkabar_kap(x)= gkabar_kap(x)*f }
  }
}

proc A_NMDA_block(){ // block both A-current and NMDA current
 NMDA_block()
 A_block()
}

if (times == 0 || times == 2) {
  temporal_offset = 10
}else {
  temporal_offset = 120
}
tstop = tstop + temporal_offset 

addgraph_2("soma.v(0.5)",0,tstop, -72,-10)

r = 3
for try = 1, 30  {   // try selecting a pair of sections for 30 times
   r = r + 1 
   sl = new SectionList()
   rpid = new Random(r)
   PID = rpid.discunif(0,m) 
   access Branch_ref[PID].sec    
   sl.append()

   rpid = new Random(2*r+1) 
   PID = rpid.discunif(0,m)
   access Branch_ref[PID].sec  
   sl.append()

   for c = 0, max_cases-1 {   // for the sections selected, create all possible    
     k=-1                     // A, C stimulation cases
     splot=new Shape()        // make a shape graph 
     all_synapses = 0         // initialize  
     strdef dir_str
     sprint(dir_str, "data/%s/Apical_Tips/Shock/Both_Tips/",tunings_file) // define data directory
     forsec sl {              // for both selected sections

       k = k+1
       synapses[k]= all_syn[2*c+k]  // get number of synapses to put in section     
       print secname(), "is where we assess potency."
       COLOR=k+2
       t = 0
       if ( k > 0) { t = k*synapses[k-1] }
         nseg = synapses[k]
         for si=1,synapses[k] {       // uniformly distribute synapses on sections
              posn = (2*si -1)/(2*synapses[k]) 
              ampa[t+si-1] = new GLU(posn)  
              nmda[t+si-1] = new NMDA(posn)  
              salloc(ampa[t+si-1],nmda[t+si-1],posn)
              splot.point_mark(ampa[t+si-1],COLOR)    // mark synapses on shape graph            
           }

       splot.flush()
       splot.show(1)
       sprint(dir_str, "%s%s",  dir_str, secname()) // set directory name to section name 
       all_synapses = all_synapses + synapses[k]    // update number of synapses allocated 

       strdef recordsec           
       sprint(recordsec, "%s.v(0.5)",secname()) 
       if (c==0) {
         addgraph_2(recordsec,0,tstop,-72,-10) // plot trace of current branch
       }     
    }
    
     GABA_flag = 0    // Don't make both AMPA/NMDA and GABA trains in shiftsyn_init        
     // create the stimulation trains for AMPA & NMDA synapses   
     econ.xopen_library("Terrence","shiftsyn-initA")
     shiftsyn_init(all_synapses,tstop,dt,hertz,synch,perio,PID,temporal_offset,GABA_flag,"ampa","nmda")
     
     // Execute current-blockade proceedure specified 
     
     if (times == 0) {
             sprint(select, "%s", "control")}  
        if (times == 1) {
             A_block()
             sprint(select, "%s", "block_A")}
        if (times == 2) {
             NMDA_block() 
             sprint(select, "%s", "block_NMDA")}
        if (times == 3) {
             A_NMDA_block() 
             sprint(select, "%s", "block_A_NMDA")}
        if (times == 4) {
             Na_block()
             sprint(select, "%s", "block_Na")}
        if (times == 5) {
             Ca_block()
             sprint(select, "%s", "block_Ca")}

     print "secname = ", secname(), " synapses[1] = ", synapses[0], " synapses[2] = ", synapses[1], "case = ", select
  
     somavrec=new Vector(tstop/dt)
     somavrec.record(&soma.v(0.5)) // prepare to record at soma

     ind=0
     forsec sl {                                        // prepare to record at dendrites
       vtip[ind]=new Vector(tstop/dt)
       sprint(temp, "vtip[%d].record(&%s.v(0.5))", ind, secname())
       execute1(temp)
       ind=ind+1
     }
   
     finitialize(v_init)           // Initialize and run experiment
     fcurrent()
     run()
  
      meanvrec = somavrec.mean(temporal_offset/dt,tstop/dt -1)      // mean somatic depolarization
      maxvrec = somavrec.max(temporal_offset/dt,tstop/dt -1)        // max somatic depolarization
      ind = 0
      forsec sl {
         meandendrec[ind] = vtip[ind].mean(temporal_offset/dt,tstop/dt -1) // mean dendritic depolarization
         maxdendrec[ind] = vtip[ind].max(temporal_offset/dt,tstop/dt -1)   // max dendritic depolarization
         ind = ind + 1      
      }
     soma_spikes =  spikecount(somavrec,somatic_spike_threshold)     // count current number of somatic spikes
        
     sprint(econ.syscmd,  "mkdir -p %s", dir_str)                    // make the output directory
     system(econ.syscmd) 

     vf=new File()
     sprint(econ.tmp_str2, "%s/%s_shock", dir_str, select)             // make the output file
     
 // Print number of synapses in each section, mean and max somatic voltage and number of spikes
     vf.aopen(econ.tmp_str2)
     vf.printf("%d %g %g %g %g %g %g %g %g\n", synapses[0], synapses[1], meanvrec, maxvrec, meandendrec[0], maxdendrec[0], meandendrec[1], maxdendrec[1], soma_spikes)
     vf.close()

 //Print somatic traces for stimulus conditions A+C
     sprint(econ.tmp_str, "%s", dir_str)
     somarecf=new File()
     sprint(temp, "%s/Vsoma_%d_%d", dir_str, synapses[0], synapses[1])
     somarecf.wopen(temp)
     somavrec.printf(somarecf, "%g\n")
     somarecf.close()
  
// Print graphics to eps files
     econ.xopen_library("Terrence","verbose-system")
     for i=0,windex {
        sprint(econ.tmp_str, "%s", dir_str)
        sprint(econ.tmp_str2, "%s/graph-syns=%d_%d-%d.eps",econ.tmp_str, synapses[0], synapses[1], i)      
        win[i].printfile(econ.tmp_str2)
     }

}

}