// control_boxes.hoc

b1 = new VBox()
b1.intercept(1)
g = new Graph()
g.size(0,tstop,-70,30)
g.addvar("soma.v(0.5)",1,1,2*tstop,0,2)
g.xaxis(1)

/*c = new Graph()
c.size(0,740,0,100)
c.xaxis(1)
c.exec_menu("10% Zoom out")
c.color(1)
c.label(0.4,0.8,"peak bAP (overall time)")
c.label(0.01,.5,"back-prop. AP height")
c.label(0.01,.39,"mV")
c.label(0.36,0.04,"dist. from soma (um)")
c.label(0.93,.6,"aBeta")
c.label(0.94,.07,"Control")
*/// init_and_run_and_graph.hoc
// model specific versions of initialization and run procedures including graphing after the run
reset_e_pas = -1
proc init() {
	t=0
        forall {
        v=Vrest
        if (ismembrane("nax") || ismembrane("na3")) {ena=55}
        if (ismembrane("kdr") || ismembrane("kap") || ismembrane("kad")) {ek=-90}
        if (ismembrane("hd") ) {ehd_hd=-30}
	}
	finitialize(Vrest) // use v_init if want to change.
        fcurrent()
	forall if (ismembrane("cacum")) {
	       for(x,0) irest_cacum(x)=-ica(x)
	}
  if (reset_e_pas) {
        forall {
	for (x) {
	if (ismembrane("na3")||ismembrane("nax")){e_pas(x)=v(x)+(ina(x)+ik(x))/g_pas(x)}
	if (ismembrane("hd")) {e_pas(x)=e_pas(x)+i_hd(x)/g_pas(x)}
		}
	}
  }
	cvode.re_init()
	cvode.event(tstop)
	access soma
	g.begin()
	// init_ca()  // initialize Ca2+ channels and Ca2+ sens. K+ current
	// print "Ca2+ channels initialized"
	frecord_init() // properly resets vectors for recording
}


proc advance() {
	fadvance()
	g.plot(t)
	g.flush()
	p.flush()
	doNotify()
}
objref sect_list_2getparent
objref obdend_ca_vec  // record the Ca2+ in an oblique dendrite

proc runp() {
	// record soma and an apical dendrite's voltages in vectors
	soma_v_vec=new Vector()
	dend_v_vec=new Vector()
	obdend_v_vec=new Vector()
	soma_v_vec.record(&soma.v(0.5))
	dend_v_vec.record(&apic[16].v(0.5))  // 16 is 264.5, 14 is 233 um, 18 is 284 um from soma
	obdend_v_vec.record(&apic[76].v(0.5))  // tip of the middle oblique featured in fig 1

	obdend_ca_vec=new Vector()
	obdend_ca_vec.record(&apic[76].v(0.5))  // tip of the middle oblique featured in fig 1

      stdinit()
	continuerun(tstop)

	distrx=new Vector() // dist. from soma stored here used for several other vec's x coord's
	distry=new Vector()
//	obliquex=new Vector() // holds distance from soma to oblique dend
//	obliquey=new Vector() // holds max voltage corresponding to distance
	// in the primary apical dendrite (the trunk)
	primaryx=new Vector() // holds distance from soma to primary dend
	primaryy=new Vector() // holds max voltage corresponding to distance
	primaryca=new Vector() // holds max calcium corresponding to distance
	primaryoptical=new Vector() // holds max optical corresponding to distance
	// in the tuft
	tuftx=new Vector() // holds distance from soma to tuft dend
	tufty=new Vector() // holds max voltage corresponding to distance
	tuftca=new Vector() // holds max ca corresponding to distance
	tuftoptical=new Vector() // holds optical voltage corresponding to distance

	distrca=new Vector() // holds the maximum cai per compartment
	distr_cai_t=new Vector() // holds the time at which those max's occurred

//	forsec "apical_dendrite" {
	forsec "apic" {
		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			distrx.append(distance(x))       // note these vecs used only for markers so don't 
			distry.append(vmax_ds(x)-Vrest)  // need to be connected to previous dends like lines
			distrca.append(cmax_cacum(x))  // use absolute value to start with
			distr_cai_t.append(ca_tmax_cacum(x))
			}
		}
	}
// role taken over by bAP_peak_vecs.hoc
//	forsec obliques {
//		for (x) if (x>0 && x<1) {
//			if (diam>=0.) {
//			obliquex.append(distance(x)) 
//			obliquey.append(vmax_ds(x)-Vrest)
//			}
//		}
//	}
	forsec primary {
		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			primaryx.append(distance(x)) 
			primaryy.append(vmax_ds(x)-Vrest)
			primaryca.append(cmax_cacum(x))
			primaryoptical.append((cmax_cacum(x)-cai0_cacum(x))/cai0_cacum(x))
			}
		}
	}
	forsec tuft {
		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			tuftx.append(distance(x)) // used only for markers
			tufty.append(vmax_ds(x)-Vrest)
			tuftca.append(cmax_cacum(x))
			tuftoptical.append((cmax_cacum(x)-cai0_cacum(x))/cai0_cacum(x))
			}
		}
	}
	// also receive the time of the peak - used interactively (via gui) to study time delivery of peak bAP
	distrt=new Vector() // can be graphed with distrx as x coordinate
//	forsec "apical_dendrite" {
	forsec "apic" {
		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			distrt.append(tmax_ds(x))
			}
		}
	}
      assign_dist_peak_vecs()
      if (marker_graph) {
//         distry.mark(c,distrx,current_marker_style,current_marker_size,current_marker_color+alzheimers_flag,2)
//         obliquey.mark(d,obliquex,current_marker_style,current_marker_size+1,current_marker_color-alzheimers_flag,2)
         primaryy.mark(d,primaryx,"s",current_marker_size+1,7+alzheimers_flag,2)
         tufty.mark(d,tuftx,"t",current_marker_size+1,4+alzheimers_flag,2)
         primaryca.mark(peak_ca_graph,primaryx,"s",current_marker_size+1,7+alzheimers_flag,2)
         tuftca.mark(peak_ca_graph,tuftx,"t",current_marker_size+1,4+alzheimers_flag,2)
         primaryoptical.mark(peak_optical_graph,primaryx,"s",current_marker_size+1,7+alzheimers_flag,2)
         tuftoptical.mark(peak_optical_graph,tuftx,"t",current_marker_size+1,4+alzheimers_flag,2)
      }
      if (line_graph) {
         // if line graph then make line graphs for bAPs if abeta (alzheimers_flag) shift color
         // now done at bottom of this proc with calls to bAP_peak_vecs.hoc procs
//         forsec obliques {
//            color_choice=1+alzheimers_flag // black or red if abeta present
//            tmp_vecx=new Vector()
//            tmp_vecy=new Vector()
//		for (x) if (x>0 && x<1) {
//			if (diam>=0.) {
//			tmp_vecx.append(distance(x)) 
//			tmp_vecy.append(vmax_ds(x)-Vrest)
//			}
//		}
//            tmp_vecy.line(d,tmp_vecx, color_choice,1)
//         }
         // the primary is a special case where there are no branches and
         // only apic[25] has nseg different than 1
         tmp_vecx=new Vector()
         tmp_vecy=new Vector()
         forsec primary {
            color_choice=7+alzheimers_flag // purple or yellow if abeta present
		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			tmp_vecx.append(distance(x)) 
			tmp_vecy.append(vmax_ds(x)-Vrest)
			}
		}
         }
         tmp_vecy.line(d,tmp_vecx, color_choice,4)
         forsec tuft {
            color_choice=4+alzheimers_flag //  if abeta present
            tmp_vecx=new Vector()
            tmp_vecy=new Vector()
         // here need to add in code that will connect tuft dends to their parents
         // which are either the primary trunk or other tufts
            sect_list_2getparent = new SectionRef()
            sect_list_2getparent.parent {
                  x=0.5*((nseg-1)/nseg+1) // finds last relative (0-1) point on parent
              	tmp_vecx.append(distance(x)) // that we want to start with
			tmp_vecy.append(vmax_ds(x)-Vrest)
            }

		for (x) if (x>0 && x<1) {
			if (diam>=0.) {
			tmp_vecx.append(distance(x))
			tmp_vecy.append(vmax_ds(x)-Vrest)
			}
		}
            tmp_vecy.line(d,tmp_vecx, color_choice,1)
         }
      }
      // for model to Chen statistics comparison:
      chen_c_bpAP_peaks=new Vector() // apic[15] through 19 have middles

	// within Chen C 2005 range of 240-300 ums.  It was found by hand that these
	// compartments apic[15 through 19] correspond to the primary dendrite, the likely
	// source of Chen C's electrical recordings.
	/* oc>for i=14,19 {apic[i] print secname(),.5,distance(.5),v }
	apic[14]0.5 233.72056 -46.048939
	apic[15]0.5 253.83815 -41.003093
	apic[16]0.5 264.45777 -38.230327
	apic[17]0.5 276.62687 -34.690309
	apic[18]0.5 284.63212 -32.181721
	apic[19]0.5 295.92992 -28.539688
	oc>
	*/

	for i=15,19 {
		apic[i] chen_c_bpAP_peaks.append(vmax_ds(0.5)-Vrest)
	}
	soma chen_c_bpAP_soma_peak=vmax_ds(0.5)-Vrest
	c.flush()
	doNotify()
//      assign_dist_peak_vecs() // new procs which connect the oblique dendrites
      print "calling graph_dist_peak_vecs()"
      graph_dist_peak_vecs()
}

proc runm() {
  print "model run"
  runp()  // used to be runp(1) however the argument is no longer used
}

d = new Graph()
d.size(0,740,0,100)
d.xaxis(1)
d.exec_menu("10% Zoom out")
d.color(1)
d.label(0.4,0.8,"peak bAP (overall time)")
d.label(0.01,.5,"back-prop. AP height")
d.label(0.01,.39,"mV")
d.label(0.36,0.04,"dist. from soma (um)")
d.label(0.93,.6,"aBeta")
d.label(0.94,.07,"Control")
b1.intercept(0)
b1.map()

b2= new VBox()
b2.intercept(1)
objref hbox
hbox=new HBox()
hbox.intercept(1)

  xpanel("")
  xbutton("run model", "runm()")
  xlabel(" Compare healthy with aBeta:")
  xbutton("... back-prop APs on all dends","compare_bpAPs()")
  xbutton("... bpAPs only along primary dend","compare_primary_bpAPs()")
// these cause buttons not to fit on laptop:  xlabel(" ")
  xbutton("apply aBeta ubiquitously","apply_aBeta()")
  xbutton("apply aBeta to basal dendrites","apply_basal_aBeta()")
  xbutton("apply aBeta to apical dendrites","apply_apic_aBeta()")
  xbutton("apply aBeta to soma","apply_soma_aBeta()")
  xbutton("remove aBeta","remove_aBeta()") // calls restore_healthy_g_A(), clears flag
//  xlabel(" ")
  xlabel("set below then press confirm")
  xpvalue("aBeta concentration factor",&aBeta_concentration_factor)
  xbutton("confirm","set_concentration()")
//  xbutton("Graph peak [Ca]_i","distr_maxcai.mark(Graph[4],distrx,current_cai_marker,1,current_cai_marker_color,2)")
  xpvalue("current [Ca]_i marker",&current_cai_marker)
  xlabel("above 3=triangle 2=square, below 1=black, 2=red")
  xpvalue("current [Ca]_i marker color",&current_cai_marker_color)
  xpvalue("L,T,N's gc",&gc)
  xlabel("current gc default is 2.5x migliore 2008")
  xpvalue("Ca2+ sensitive K+ currents gKc",&gKc )
  xbutton("Set L, T, N-type chan's to gc, gkca(cagk) to gKc","init_ca()")
  xpvalue("T-type Ca2+ current gt",&gt )
  xbutton("Set T-type Ca2+ chan's gcatbar_cat to above gt","init_cat(gt)")
  xpanel()

  xpanel("second col")
  xlabel("Directions: run model without aBeta then change color to red,")
  xlabel("add aBeta either ubiquitously or in some combination of")
  xlabel("button presses.  To restore to healthy press \"remove aBeta\"")
  xbutton("apply aBeta to proximal dendrites","apply_proximal_aBeta()")
  xbutton("apply aBeta to distal dendrites","apply_distal_aBeta()")
  xbutton("apply aBeta to primary dendrite","apply_primary_aBeta()")
  xbutton("apply aBeta to oblique dendrites","apply_obliques_aBeta()")
  xbutton("apply aBeta to tuft dendrites","apply_tuft_aBeta()")

  xstatebutton("aBeta present",&alzheimers_flag)
  xpvalue("current marker color",&current_marker_color)
  xlabel("Marker colors: 0 white, 1 black, 2 red, 3 blue")
  xlabel("4 green, 5 tan, 6 brown, 7 purple, 8 yellow, 9 grey")
  xlabel(" ")
  xbutton("generate Fig 1, 2. Chen C fig 2A graph","ChenCfig2A()")
  xbutton("generate concentration graph","generate_conc_graph()")
//  xbutton("generate Chen C fig 2CD graph","ChenCfig2CD()")
  xbutton("generate oblique aBeta graph","oblique_application()")
  xbutton("Graph Oblique APs before and after ABeta","ObliqueAPgraph()")
  xlabel(" ")
  xstatebutton("marker bAP peak graph",&marker_graph)
  xstatebutton("line bAP peak graph",&line_graph)
  xlabel("Electrically remove obliques by diam shrinking")
  xbutton("Shrink obliques","shrink_obliques()")
  xbutton("Restore obliques","restore_obliques()")
  xpanel()

  xpanel("Diagnostic identifys dendritic arbors")
  xlabel("Examine regions of the cell with shape plot:")
  xbutton("Inflate proximal dendrites","inflate_proximal_dend()")
  xbutton("Deflate proximal dendrites","deflate_proximal_dend()")
  xbutton("Inflate distal dendrites","inflate_distal_dend()")
  xbutton("Deflate distal dendrites","deflate_distal_dend()")
  xbutton("Inflate primary dendrite","inflate_primary_dend()")
  xbutton("Deflate primary dendrite","deflate_primary_dend()")
  xbutton("Inflate oblique dendrites","inflate_oblique_dend()")
  xbutton("Deflate oblique dendrites","deflate_oblique_dend()")
  xbutton("Inflate tuft dendrites","inflate_tuft_dend()")
  xbutton("Deflate tuft dendrites","deflate_tuft_dend()")
  xlabel("Sect diam change:0-27 trunk, 28-52 tuft, 53-128 obliques")
  xbutton("Apical dendrite diam resize","apic_resize()")
  xpvalue("Apical multiplicative factor",&apic_resize_factor)
  xpvalue("Apical compartment index 0-128",&apic_comp_index)
  xlabel("Warning: do not apply amyloid-Beta (aBeta)")
  xlabel("in overlapping regions,blocking factor would")
  xlabel("be multiplied repeatedly (remove aBeta between")
  xlabel("studies of aBeta distributions)")
  xstatebutton("reset e_pas on init for equilib.",&reset_e_pas)

/*  xpanel()
  xpanel("third col")
  xlabel("Marker color table")
  xlabel("0 white")
  xlabel("1 black")
  xlabel("2 red")
  xlabel("3 blue")
  xlabel("4 green")
  xlabel("5 tan")
  xlabel("6 brown")
  xlabel("7 purple")
  xlabel("8 yellow")
  xlabel("9 grey")
*/
  xpanel()

hbox.intercept(0)
hbox.map()

b2.intercept(0)
b2.map()

b3 = new VBox()

make_leaky_distal_Rm()
print "default: Omori et al. 2009 leaky dendrites set"

b3.intercept(1)
  xpanel("Distributing passive and active conductances")
  xbutton("Leaky distal dends Omori et al. 09","make_leaky_distal_Rm()")
  xbutton("Const Rm in dends", "make_const_dend_Rm()")
  xlabel(" ")
  xlabel("Instructions: assign the below values in the text boxes and then")
  xlabel("press the buttons to deploy those values to the model cell")
  xbutton("Assign Migliore et al. 2005 chan. dist.possibly scaled throughout the cell","assign_migliore_distribution()")
  xbutton("Assign relative I_A slope's within the obliques","assign_relative_ka()")
/* old method would scale the Migliore's max value of I_A at
the most distal dendrite for either all the dendrites when 
"Migliore et al. 2005 chan. dist." (upper button) was pressed or
just the obliques had the max value assigned on each dendrite when the
"Assign relative I_A gmax's" button was pressed.
  xpvalue("scale Migliore A-type current by",&scale_ka)
  xpvalue("scale obliq. rel. A-type current by",&scale_obliques)
*/
/* The new method just scales the old slope to new values and
let's the values fall where they may */
  xpvalue("scale Migliore A-type current by",&scale_ka)
  xpvalue("scale obliq. rel. A-type current by",&scale_obliques)
  xlabel("assign scales then press above two buttons")
  xbutton("set this IA distrib. as healthy","store_healthy_g_A()")

  xpanel()
b3.intercept(0)
b3.map()

peak_ca_graph=new Graph()
peak_optical_graph=new Graph()