// The goal of this experiment is to insure that our cell responses
// are reasonably similar to the data on back propagating action potentials
// where the site of current injection is either at the soma or the apical trunk


//load_proc("nrnmainmenu")             // load main NEURON library
//load_template("ExperimentControl")   // load a custom made library function that centralizes parameters so as to 
load_file("nrngui.hoc")
load_file("CA1_multi/template/load_templates.hoc")

strdef accstr                                                // not confuse experimental variable bindings with neurophysiological variable bindings
objref econ                          // Create an experiment object
show_errs=1
debug_lev=1
econ=new ExperimentControl(show_errs,debug_lev)
econ.self_define(econ) // points the object at itself
econ.morphology_dir = "CA1_multi/morphology/n123"                             // Setup morphology directory
econ.generic_dir = "CA1_multi/experiment/"                                    // Setup directory with cell-setup file
econ.add_lib_dir("Terrence","CA1_multi/lib")                                  // Setup directory with library functions 

econ.data_dir       = "validation-plots"                       // Define directory to save produced data 
if (unix_mac_pc() ==1 ) {
	sprint(econ.syscmd, "mkdir -p %s", econ.data_dir)  // make the data directory
	system(econ.syscmd)
}

// Setup cell

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

printf("Opening cell setup\n")
econ.xopen_generic("cell-setup")                   // load the cell-setup file (define specific 
printf("Opened. Setting up cell\n")                // channels, membrane properties etc)
cell_setup(econ)

// Set simulation parameters for the experiment 

econ.defvar("Simulation Control", "tstop", "750", "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()


// Define directories for different cases tested
strdef data_dir

// Insert current clamp into soma or dendritic section
objref ic,ic2

proc validate_bpap_somatic_stimulation() {
    access soma
    ic = new IClamp(0.5)
    // Configure current clamp
    econ.defvar("Current Clamp Control", "ic.del", "20", "Determines the delay before onset of the current clamp.")
    econ.defvar("Current Clamp Control", "ic.dur", "720", "Determines the duration of the current clamp.")
    
    // current used with somatic stimulation
    econ.defvar("Current Clamp Control", "ic.amp", ".22", "Determines the amplitude of the current clamp at the soma.")
    sprint(data_dir, "%s/BPAP/Soma", econ.data_dir)              // Define directory to save produced data for somatic stimulation 
}

proc validate_bpap_dendrite62_stimulation() {
    print "WARNING dendrite 62 is only at the trunk for the original Poirazi morphology!"
    access apical_dendrite[62]  //stimulate for dendritic bpap train
    ic = new IClamp(0.5)
    // Configure current clamp
    econ.defvar("Current Clamp Control", "ic.del", "20", "Determines the delay before onset of the current clamp.")
    econ.defvar("Current Clamp Control", "ic.dur", "720", "Determines the duration of the current clamp.")
    
    // current used with dendrite 62 stimulation, for dendritically induced trains of BPAP
    econ.defvar("Current Clamp Control", "ic.amp", ".48", "Determines the amplitude of the current clamp at dendrite [62].")
    sprint(data_dir, "%s/BPAP/Dend62", econ.data_dir)              // Define directory to save produced data for somatic stimulation 

}

proc validate_bpap_dendrite64_stimulation() {
    print "WARNING dendrite 64 is only at the trunk for the original Poirazi morphology!"
    access apical_dendrite[64]  //stimulate for single-Ca++-dendritic spike in apical_dendrite[64]
    ic = new IClamp(0.5)
    econ.defvar("Current Clamp Control", "ic.del", "20", "Determines the delay before onset of the current clamp.")
    econ.defvar("Current Clamp Control", "ic.dur", "700", "Determines the duration of the current clamp.")
    
    // current used with dendrite 64 stimulation, single Ca++ spike
    econ.defvar("Current Clamp Control", "ic.amp", ".48", "Determines the amplitude of the current clamp at debdrite [64].we") 
    sprint(data_dir, "%s/BPAP/Dend64", econ.data_dir)              // Define directory to save produced data for somatic stimulation 

}

// Default is somatic stimulation
validate_bpap_somatic_stimulation()

// Create basic graphics

econ.xopen_library("Terrence","basic-graphics")                            // open graphics library file   
// section used in train-with-single-Ca++-spike figure
//addgraph_2("apical_dendrite[64].v(0.5)", 0,tstop,-72,20)     // 233.67 um from soma

proc validate_bpap_run() {
    finitialize(v_init)
    fcurrent()
    run()
}

objref vb
vb = new VBox()
vb.intercept(1)
//sections used in BPAP figure
addgraph_2("soma.v(0.5)",              0,tstop,-72,20)
addgraph_2("apical_dendrite[46].v(0.5)",0,tstop,-72,20)    // 156.96 um from soma
addgraph_2("apical_dendrite[71].v(0.5)", 0,tstop,-72,20)   // 324.53 um from soma
print "WARNING these segments are only at the trunk for the original Poirazi morphology!"

// Buttons
xpanel("")
xradiobutton("Somatic injection", "validate_bpap_somatic_stimulation()", 1)
xradiobutton("Dendrite 62 injection", "validate_bpap_dendrite62_stimulation()")
xradiobutton("Dendrite 64 injection", "validate_bpap_dendrite64_stimulation()")
xpanel()
xpanel("")
xradiobutton("Original CaL", "cell_mods_original_apical_caR_caLH_insert()", 1)
xradiobutton("Perisomatic CaL", "cell_mods_apical_caR_caLH_insert()")
xpanel()
xpanel("")
xradiobutton("CVODE off", "cvode_active(0)", 1)
xradiobutton("CVODE on", "cvode_active(1)")
xbutton("Run", "validate_bpap_run()")
xpanel()

vb.intercept(0)
vb.map("Traces")

proc validate_bpap_print() {
    C = $2
    for i=0,windex {
        if (unix_mac_pc() ==1 ) {
	          sprint(econ.syscmd, "mkdir -p %s", data_dir)  // make the data directory
	          system(econ.syscmd)
}
        sprint(econ.tmp_str, "%s/bpap-%s-cv%d-%d.eps", data_dir, $s1, C, i)    // define the name of the .eps file 
        print(econ.tmp_str)
        win[i].printfile(econ.tmp_str)
    }
}

proc validate_bpap_plots() { local C
    for C=0, 1 {
        cvode_active(C)
        if (C) {
            cvode.atol(0.0001)
        }
        
        // Original parameters
        cell_mods_original_apical_caR_caLH_insert()
        
        validate_bpap_somatic_stimulation()
        run()
        validate_bpap_print("orig", C)
        
        validate_bpap_dendrite62_stimulation()
        run()
        validate_bpap_print("orig", C)
        
        validate_bpap_dendrite64_stimulation()
        run()
        validate_bpap_print("orig", C)

        
        // Parameters with extra CaLH
        cell_mods_apical_caR_caLH_insert()
        
        validate_bpap_somatic_stimulation()
        run()
        validate_bpap_print("calh", C)
        
        validate_bpap_dendrite62_stimulation()
        run()
        validate_bpap_print("calh", C)
        
        validate_bpap_dendrite64_stimulation()
        run()
        validate_bpap_print("calh", C)
    }
}