// MultiConditions.hoc
/*
1. generate the vm traces for spines 1, 2, and 3 and the dend shaft (4 traces)
under 

1) inhibition in the spine
2) 10x inhibition in the dend
3) bAP
4) bAP + inhib in spine
5) bAP + (10x) inhib in dend
6) (5s) a) supplemental: bAP + (1x) inhibition in the dend
7)   b) supplemental: (1x) inhibition in the dend

The calcium and voltage trace files are stored in "proto_dir/file" 
where protocol directory proto_dir is 

inhibspine, 10xinhibdend, inhibdend, bAP, bAPinhibspine, bAP10xinhibdend, bAP1xinhibdend, 1xinhibdend

and file is spine0_ca, spine1_ca, spine2_ca, dend_ca, spine0_v,
spine1_v, spine2, dend_v
*/
load_file("hoc/multi_cond_declare_vecs.hoc")

for i=0, num_of_conditions-1 {
  multicond_v[i] = new Graph()
  multicond_ca[i] = new Graph()
  multicond_gates[i] = new Graph()
  multicond_gabaa_g[i] = new Graph()
}
proto_num=0  // index into above graphs of protocol results

// reinitialize vectors and setup vector records:
load_file("hoc/multi_cond_setup_vecs.hoc")

proc  write_vectors() {
  sprint(tmpstr,"multiconditions/%s/spine0_cai.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine0_cai_vec)
  sprint(tmpstr,"multiconditions/%s/spine1_cai.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine1_cai_vec)
  sprint(tmpstr,"multiconditions/%s/spine2_cai.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine2_cai_vec)
  
  sprint(tmpstr,"multiconditions/%s/spine0_v.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine0_v_vec)
  sprint(tmpstr,"multiconditions/%s/spine1_v.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine1_v_vec)
  sprint(tmpstr,"multiconditions/%s/spine2_v.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine2_v_vec)
  
  sprint(tmpstr,"multiconditions/%s/spine0_gabaa_g.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine0_gabaa_g_vec)
  sprint(tmpstr,"multiconditions/%s/spine1_gabaa_g.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine1_gabaa_g_vec)
  sprint(tmpstr,"multiconditions/%s/spine2_gabaa_g.dat" , proto_dir)
  write_vec(tmpstr,t_vec, spine2_gabaa_g_vec)
  
  sprint(tmpstr,"multiconditions/%s/dend_v.dat" , proto_dir)
  write_vec(tmpstr,t_vec,dend_v_vec)
  sprint(tmpstr,"multiconditions/%s/dend_gabaa_g.dat" , proto_dir)
  write_vec(tmpstr,t_vec,dend_gabaa_g_vec)
  sprint(tmpstr,"multiconditions/%s/dend_cai.dat" , proto_dir)
  write_vec(tmpstr,t_vec, dend_cai_vec)

  sprint(tmpstr,"multiconditions/%s/m_ca_dend_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, m_ca_dend_vec)
  
  sprint(tmpstr,"multiconditions/%s/h_ca_dend_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, h_ca_dend_vec)

  sprint(tmpstr,"multiconditions/%s/m_ca_spine1_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, m_ca_spine1_vec)

  sprint(tmpstr,"multiconditions/%s/h_ca_spine1_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, h_ca_spine1_vec)

  sprint(tmpstr,"multiconditions/%s/m_ca_spine0_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, m_ca_spine0_vec)

  sprint(tmpstr,"multiconditions/%s/h_ca_spine0_vec.dat" , proto_dir)
  write_vec(tmpstr,t_vec, h_ca_spine0_vec)

  // graphs
  // ca
  spine0_cai_vec.line(multicond_ca[proto_num],t_vec,3,1)
  spine1_cai_vec.line(multicond_ca[proto_num],t_vec,1,1)
  spine2_cai_vec.line(multicond_ca[proto_num],t_vec,2,1)
  dend_cai_vec.line(multicond_ca[proto_num],t_vec,5,1)

  multicond_ca[proto_num].exec_menu("View = plot")
  sprint(tmpstr,"multi conditions: cai under protocol %s" , proto_dir)
  multicond_ca[proto_num].label(.05,.05,tmpstr)
  multicond_ca[proto_num].view(0, 0, 500 , 0.0011, 180, 280, 300, 200)

  // gabaa_g
  spine0_gabaa_g_vec.line(multicond_gabaa_g[proto_num],t_vec,3,1)
  spine1_gabaa_g_vec.line(multicond_gabaa_g[proto_num],t_vec,1,1)
  spine2_gabaa_g_vec.line(multicond_gabaa_g[proto_num],t_vec,2,1)
  dend_gabaa_g_vec.line(multicond_gabaa_g[proto_num],t_vec,5,1)
  multicond_gabaa_g[proto_num].exec_menu("View = plot")

  sprint(tmpstr,"multi conditions: gabaa_g under protocol %s" , proto_dir)
  multicond_gabaa_g[proto_num].label(.05,.05,tmpstr)
  multicond_gabaa_g[proto_num].view(114, -70, 6 , 78, 200, 300, 300, 200)

  // v
  spine0_v_vec.line(multicond_v[proto_num],t_vec,3,1)
  spine1_v_vec.line(multicond_v[proto_num],t_vec,1,1)
  spine2_v_vec.line(multicond_v[proto_num],t_vec,2,1)
  dend_v_vec.line(multicond_v[proto_num],t_vec,5,1)
  multicond_v[proto_num].exec_menu("View = plot")

  sprint(tmpstr,"multi conditions: v under protocol %s" , proto_dir)
  multicond_v[proto_num].label(.05,.05,tmpstr)
  multicond_v[proto_num].view(114, -70, 6 , 78, 200, 300, 300, 200)

  // ca gates
  m_ca_spine0_vec.line(multicond_gates[proto_num], t_vec,1, 1)
  h_ca_spine0_vec.line(multicond_gates[proto_num], t_vec,2, 1)
  m_ca_spine1_vec.line(multicond_gates[proto_num], t_vec,3, 1)
  h_ca_spine1_vec.line(multicond_gates[proto_num], t_vec,4, 1)
  m_ca_dend_vec.line(multicond_gates[proto_num], t_vec,5, 1)
  h_ca_dend_vec.line(multicond_gates[proto_num], t_vec,6, 1)
  multicond_gates[proto_num].exec_menu("View = plot")
  sprint(tmpstr,"multi conditions: m_ca, h_ca under protocol %s" , proto_dir)
  multicond_gates[proto_num].label(.05,.05,tmpstr)
}

proc run_model() {
  print "model running with protocol ", proto_dir
  setup_vectors_to_record()
  init()
  run()
  write_vectors()
}

print "Starting Multiple conditions with conductance based inhibitory synapses."
// Start of top-level script to run model with (roughly) Multi Conditions
// I say roughly because a 6+7th condition of "bAP + 1x inhib in dend",
// and "1x inhib in dend" were added
// first switch to semi-high (0.05 is high) neck resistance state:
forsec "neck" {diam=0.07}
forsec "neck" {Ra=200}
// the NC[] index is defined: 0=dend, 1, 2, 3 is Spine[0, 1, 2]

// 1) inhibition in the spine //////////////////////////////////
// turn on spine inhibition, turn off bAP
NC[spine_choice].weight = 0.0004 // 4e-4 uS = 0.4 nS
MultIClamp[0].number=0 // turn off current injection that creates bAP

proto_dir="inhibspine"
proto_num=0
run_model()

// turn off spine inhibition
NC[spine_choice].weight = 0.0

// 2) inhibition in the dend //////////////////////////////////

// turn on dend inhibition, leave bAP off
// turn on conductance in the adjacent dendrite at the location of Spine[1] (used to be [2] at 181/L)
// at 10 times the conductance
NC[0].weight = 0.004 // 4 nS

proto_dir="10xinhibdend"
proto_num=1
run_model()

// turn off dend inhibition
NC[0].weight = 0.0

// 3) bAP                    //////////////////////////////////

// turn on bAP
MultIClamp[0].number=1 // turn on current injection that creates bAP

proto_dir="bAP"
proto_num=2
run_model()

// leave bAP on

// 4) bAP + inhib in spine   //////////////////////////////////

// bAP left on from above, now spine inihib turned back on
NC[spine_choice].weight = 0.0004 // 4e-4 uS = 0.4 nS

proto_dir="bAPinhibspine"
proto_num=3
run_model()

// turn spine inhib off
NC[spine_choice].weight = 0.0

// 5) bAP + inhib in dend    //////////////////////////////////

// bAP left on from above, turn dend inhib on
// turn on conductance in the adjacent dendrite at the location of Spine[1] (used to be [2] at 181/L)
// at 10 times the conductance
NC[0].weight = 0.004 // 4 nS

proto_dir="bAP10xinhibdend"
proto_num=4
run_model()

// leave bAP on and turn down dend inhib
// 5) bAP + inhib in dend    //////////////////////////////////

// bAP left on from above, turn dend inhib on
// turn on conductance in the adjacent dendrite at the location of Spine[1] (used to be [2] at 181/L)
// at 1 times the conductance
NC[0].weight = 0.0004 // 4 nS

proto_dir="bAP1xinhibdend"
proto_num=5
run_model()

// turn bAP off, leave dend inhib on
MultIClamp[0].number=0 // turn off current injection that creates bAP
// turn on conductance in the adjacent dendrite at the location of Spine[1] (used to be [2] at 181/L)
// at 1 times the conductance
NC[0].weight = 0.0004 // 4 nS

proto_dir="1xinhibdend"
proto_num=6
run_model()

// add case with spine inhibition and ECL=-40 (no bAP as above)
save_reversal_potential = Exp2Syn[spine_choice].e
Exp2Syn[spine_choice].e = -40
// dendritie inhibition off
NC[0].weight = 0.0 // 4 nS
// spine inhibition on
NC[spine_choice].weight = 0.0004 // 4e-4 uS = 0.4 nS
proto_dir="spineinhibEClm40"
proto_num=7
run_model()
// restore ECl reversal potential
Exp2Syn[spine_choice].e = save_reversal_potential

// turn bAP on and turn off spine inhib to leave in default mode
MultIClamp[0].number=1 // turn on current injection that creates bAP
// dend and spine inhibition off
NC[0].weight = 0.0
NC[spine_choice].weight = 0.0 // 4e-4 uS = 0.4 nS