{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pickle\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from neuron import h,gui\n",
      "\n",
      "####Sections and Connections####\n",
      "nofsections = 27 # 1soma + 25dendritic sections + 1terminating section \n",
      "nofspines =1     # number of spines is 1 throughout this simulation\n",
      "\n",
      "gc=[h.Section() for i in range(nofsections)]\n",
      "spineh=[h.Section() for i in range(nofspines)]\n",
      "spinen=[h.Section() for i in range(nofspines)]\n",
      "\n",
      "for i in range(nofsections-1):\n",
      "    gc[i+1].connect(gc[i],1,0)\n",
      "for i in range(nofspines):\n",
      "    spineh[i].connect(spinen[i],0,1)\n",
      "        \n",
      "j=0\n",
      "for i in range(nofspines): #spines start at 100um from soma, on 11th dendritic section\n",
      "    spinen[i].connect(gc[11+j],0.5,1)\n",
      "    j=j+1\n",
      "    \n",
      "####Morphology and other Parameters#### \n",
      "h.celsius=22 #temperature\n",
      "h.dt=0.025 #temporal resolution,ms\n",
      "\n",
      "for i in range(nofsections): \n",
      "    gc[i].L=10 #dendrite total length is 260 um\n",
      "    gc[i].nseg=3\n",
      "    gc[i].Ra=200\n",
      "    \n",
      "gc[0].diam=10 #soma size is 10umx10um, and there are two tapering regimes:\n",
      "for i in range(1,11):#1.tapering starts at 2.35um, ends at 1.7um , first 10 sections as in Ona-Jodar et al. Front Cell Neurosci 2017  \n",
      "    gc[i].diam=2.35-(i-1)*((2.35-1.7)/9)\n",
      "for i in range(11,27):#2.tapering starts at 1.7um ends at 1.2um, next 15 sections  \n",
      "    gc[i].diam=1.7-(i-10)*((1.7-1.2)/16)\n",
      "   \n",
      "for i in range(nofspines): \n",
      "    spineh[i].diam=1\n",
      "    spinen[i].diam=0.3 \n",
      "    spineh[i].L=1\n",
      "    spinen[i].L=2.5\n",
      "    spineh[i].nseg=3\n",
      "    spinen[i].nseg=3\n",
      "    spinen[i].Ra=4.9e3 #Ra is normalized as ohmcm.\n",
      "   \n",
      "presyn=h.Section() #with all other default specifications\n",
      "presyn.L=10\n",
      "presyn.diam=10\n",
      "\n",
      "####Settings for Ion channels and Synaptic Receptors and their Parameters#### \n",
      "for i in range(nofsections):\n",
      "    gc[i].insert('constant')#dummy current source\n",
      "    gc[i].insert('cadifusnpumpOGBenddif')#ca and buffers diffusion,ca pumps \n",
      "    gc[i].insert('nax')\n",
      "    gc[i].insert('kamt')\n",
      "    gc[i].insert('pas')\n",
      "    gc[i].g_pas=6e-4 \n",
      "    gc[i].e_pas=-85\n",
      "    gc[i].gbar_nax=0.5\n",
      "    gc[i].gbar_kamt=0.01\n",
      "    gc[i].cm=1\n",
      "    \n",
      "    gc[i].insert('canhem')#HVA Ca2+ channel \n",
      "    gc[i].insert('cathem')#T-type Ca2+ channel\n",
      "    gc[i].q10_cathem=3\n",
      "    gc[i].q10_canhem=3 \n",
      "    gc[i].a0m_cathem=0.055633 #to adjust the opening rate\n",
      "    gc[i].a0m_canhem=0.331432 #to adjust the opening rate   \n",
      "    gc[i].gcanbar_canhem=0.0005\n",
      "    gc[i].gcatbar_cathem=0.0003\n",
      "    \n",
      "    gc[i].TotalPump_cadifusnpumpOGBenddif=2e-11 \n",
      "    \n",
      "for i in range(nofspines):    \n",
      "    spineh[i].insert('constant')#dummy current source\n",
      "    spineh[i].insert('cadifusnpumpOGBenddif')#ca and buffers diffusion,ca pumps \n",
      "    spineh[i].insert('nax')\n",
      "    spineh[i].insert('kamt')\n",
      "    spineh[i].insert('pas')\n",
      "    spineh[i].gbar_nax=0.5\n",
      "    spineh[i].gbar_kamt=0.01\n",
      "    spineh[i].g_pas=2e-4\n",
      "    spineh[i].e_pas=-85\n",
      "    spineh[i].cm=1\n",
      "\n",
      "    spineh[i].insert('canhem')\n",
      "    spineh[i].insert('cathem')\n",
      "    spineh[i].q10_canhem=3\n",
      "    spineh[i].q10_cathem=3\n",
      "    spineh[i].a0m_cathem=0.055633 #to adjust the opening rate\n",
      "    spineh[i].a0m_canhem=0.331432 #to adjust the opening rate\n",
      "    spineh[i].gcatbar_cathem=0.00015 \n",
      "    spineh[i].gcanbar_canhem=0.0004 \n",
      "\n",
      "    spineh[i].TotalPump_cadifusnpumpOGBenddif=2.2e-11 \n",
      "    \n",
      "AMPARsyn=[h.AMPA5() for i in range(nofspines)]\n",
      "NMDARsyn=[h.NMDA5() for i in range(nofspines)]\n",
      "\n",
      "for i in range(nofspines): \n",
      "    AMPARsyn[i].loc(spineh[i](0.3))\n",
      "    AMPARsyn[i].gmax=2000\n",
      "    NMDARsyn[i].loc(spineh[i](0.7))\n",
      "    NMDARsyn[i].gmax=383 \n",
      "    NMDARsyn[i].gmax_ca=17 \n",
      "    ##NMDAR Setting##\n",
      "    NMDARsyn[i].Rb= 5e-3\n",
      "    NMDARsyn[i].Ru=12.9e-3\n",
      "    NMDARsyn[i].Rd=8.4e-3\n",
      "    NMDARsyn[i].Rr=6.8e-3\n",
      "    NMDARsyn[i].Ro=46.5e-3\n",
      "    NMDARsyn[i].Rc=73.8e-3 \n",
      "    ####\n",
      "    spinen[i].insert('cadifusnpumpOGBenddif')\n",
      "    spinen[i].TotalPump_cadifusnpumpOGBenddif=0 #there is no active mechanism on the neck\n",
      "\n",
      "####Setting Ca Dynamic Global Parameters####\n",
      "h.DCa_cadifusnpumpOGBenddif=0.6\n",
      "h.mg_NMDA5=1\n",
      "##endogenous buffer\n",
      "for i in range(nofsections):\n",
      "    gc[i].k1buf1_cadifusnpumpOGBenddif=1000\n",
      "    gc[i].k2buf1_cadifusnpumpOGBenddif=1\n",
      "    gc[i].TotalBuffer1_cadifusnpumpOGBenddif=0.12\n",
      "for i in range(nofspines):\n",
      "    spineh[i].k1buf1_cadifusnpumpOGBenddif=1000\n",
      "    spineh[i].k2buf1_cadifusnpumpOGBenddif=1\n",
      "    spineh[i].TotalBuffer1_cadifusnpumpOGBenddif=0.12\n",
      "##exogenous buffer    \n",
      "for i in range(nofsections):\n",
      "    gc[i].k1buf2_cadifusnpumpOGBenddif=1000\n",
      "    gc[i].k2buf2_cadifusnpumpOGBenddif=0.2\n",
      "    gc[i].TotalBuffer2_cadifusnpumpOGBenddif=0.1 #for \"no OGB case\", set this to 0\n",
      "for i in range(nofspines):\n",
      "    spineh[i].k1buf2_cadifusnpumpOGBenddif=1000\n",
      "    spineh[i].k2buf2_cadifusnpumpOGBenddif=0.2\n",
      "    spineh[i].TotalBuffer2_cadifusnpumpOGBenddif=0.1 #for \"no OGB case\", set this to 0\n",
      "\n",
      "####Mapping of Ca Concentration to fluorescence signal df/f \u2013 based on experimental data and simulations, see Figure 3C; not valid for \"no OGB case\"####\n",
      "def spine_fit(x):\n",
      "     y = 14390070 + (-49.1502 - 14390070)/(1 + (x/6632796000)**0.6715641)\n",
      "     return y\n",
      "    \n",
      "def dend_fit(x):  #not calculated and used based on experiment\n",
      "    y = 14390070 + (-49.1502 - 14390070)/(1 + (x/6632796000)**0.6715641)\n",
      "    return y\n",
      "\n",
      "####Setting Stimulation####\n",
      "#Global action potential:\n",
      "APstim1=h.IClamp(0.5,sec=gc[0])\n",
      "APstim1.delay=30 \n",
      "APstim1.dur=3 \n",
      "APstim1.amp=0 #to produce AP stimulation set this to 1, otherwise to 0 \n",
      "#Glutamate\n",
      "Rel=h.STEP_REL(0.75,presyn)\n",
      "Rel.amplitude=1 #to produce Glu stimulation set this to 1, otherwise to 0\n",
      "Rel.duration=3\n",
      "Rel.release_time=30\n",
      "\n",
      "for i in range(nofspines):\n",
      "    h.setpointer(Rel._ref_GLU,'C',AMPARsyn[i])\n",
      "    h.setpointer(Rel._ref_GLU,'C',NMDARsyn[i])\n",
      "    \n",
      "####Simulation Readout####\n",
      "time_h = h.Vector()\n",
      "time_h.record(h._ref_t)\n",
      "vrec_gc=[h.Vector() for i in range(nofsections)] #gc[0] is the soma, gc[10] is the 1th parent dendrite\n",
      "vrec_spineh=[h.Vector() for i in range(nofspines)] \n",
      "icaspineh=[h.Vector() for i in range(nofspines)] #overall influx\n",
      "ccaspineh=[h.Vector() for i in range(nofspines)] #overall concentration\n",
      "icagc=[h.Vector() for i in range(nofsections)] #overall influx\n",
      "ccagc=[h.Vector() for i in range(nofsections)] #overall concentration\n",
      "\n",
      "icanmdr=[h.Vector() for i in range(nofspines)]\n",
      "icahvacc=[h.Vector() for i in range(nofspines)]\n",
      "icattype=[h.Vector() for i in range(nofspines)]\n",
      "\n",
      "clampcurrent=h.Vector()\n",
      "\n",
      "for i in range(nofsections): \n",
      "    vrec_gc[i].record(gc[i](0.5)._ref_v)\n",
      "for i in range(nofspines): \n",
      "    vrec_spineh[i].record(spineh[i](0.5)._ref_v)    \n",
      "\n",
      "clampcurrent.record(APstim1._ref_i)\n",
      "\n",
      "for i in range(nofspines): \n",
      "    icahvacc[i].record(spineh[i](0.5)._ref_ica_canhem)\n",
      "    icanmdr[i].record(NMDARsyn[i]._ref_ica)\n",
      "    icattype[i].record(spineh[i](0.5)._ref_ica_cathem)\n",
      "\n",
      "for i in range(nofspines):    \n",
      "    icaspineh[i].record(spineh[i](0.5)._ref_ica)\n",
      "    ccaspineh[i].record(spineh[i](0.5)._ref_caiav_cadifusnpumpOGBenddif)\n",
      "    \n",
      "for i in range(nofsections):\n",
      "    icagc[i].record(gc[i](0.5)._ref_ica)\n",
      "    ccagc[i].record(gc[i](0.5)._ref_caiav_cadifusnpumpOGBenddif)     \n",
      "\n",
      "####Running the Simulation####\n",
      "h.v_init=-85 #forced resting Vm for granule cells\n",
      "h.init()\n",
      "\n",
      "for l in range(nofsections):# dummy current source to compensate current caused by the forced Vm. \n",
      "    gc[l].ic_constant=-(gc[l].ina+gc[l].ik+gc[l].ica)\n",
      "for l in range(nofspines):    \n",
      "    spineh[l].ic_constant=-(spineh[l].ina+spineh[l].ik+spineh[l].ica)\n",
      "\n",
      "if h.cvode.active():\n",
      "    h.cvode.re_init()\n",
      "else:\n",
      "    h.fcurrent()\n",
      "\n",
      "h.tstop =350\n",
      "h.run()\n",
      "\n",
      "#### Vectors and conversion of units (um to nm)####\n",
      "time_ar=np.asarray(time_h)\n",
      "clamp=np.asarray(clampcurrent)\n",
      "v_spineh=np.asarray(vrec_spineh[0])\n",
      "v_soma=np.asarray(vrec_gc[0])\n",
      "\n",
      "icanmdr_show=[np.array for i in range(nofspines)]\n",
      "icattype_show=[np.array for i in range(nofspines)]\n",
      "icahvacc_show=[np.array for i in range(nofspines)]\n",
      "ccaspineh_show=[np.array for i in range(nofspines)]\n",
      "icaspineh_show=[np.array for i in range(nofspines)]\n",
      "ccagc_show=[np.array for i in range(nofsections)]\n",
      "icagc_show=[np.array for i in range(nofsections)]\n",
      "\n",
      "for i in range(nofspines):\n",
      "    ccaspineh_show[i]=1e6*np.asarray(ccaspineh[i])#converting to nM\n",
      "    icanmdr_show[i]=(1e2*np.asarray(icanmdr[i]))/3.14 #converting nA to mA/cm2, spine area is (piXe-8)cm2 \n",
      "    icahvacc_show[i]=np.asarray(icahvacc[i]) #mA/cm2\n",
      "    icattype_show[i]=np.asarray(icattype[i]) #mA/cm2\n",
      "        \n",
      "for i in range(nofsections):\n",
      "    ccagc_show[i]=1e6*np.asarray(ccagc[i])#converting to nM\n",
      "\n",
      "####Mapping of Ca Concentration to df/f####\n",
      "y_dff_spineh=[np.array for i in range(nofspines)]\n",
      "y_dff_gc=[np.array for i in range(nofsections)]\n",
      "\n",
      "for i in range(nofspines): \n",
      "    y_dff_spineh[i]=spine_fit(ccaspineh_show[i])\n",
      "    \n",
      "for i in range(nofsections):    \n",
      "    y_dff_gc[i]=spine_fit(ccagc_show[i])    \n",
      "    \n",
      "#####Sample Plotting####\n",
      "plt.subplot(3,2,1).set_title('Soma Vm')\n",
      "plt.plot(time_h,vrec_gc[0])\n",
      "plt.ylabel('mV')\n",
      "\n",
      "plt.subplot(3,2,2).set_title('Spinehead Vm')\n",
      "plt.plot(time_h,vrec_spineh[0])\n",
      "plt.ylabel('mV')\n",
      "\n",
      "plt.subplot(3,2,3).set_title('Spinehead icahvacc')\n",
      "plt.plot(time_h,icahvacc_show[0])\n",
      "\n",
      "plt.ylabel('mA/cm2')\n",
      "\n",
      "plt.subplot(3,2,4).set_title('Spinehead icanmdr')\n",
      "plt.plot(time_h,icanmdr_show[0])\n",
      "plt.ylabel('mA/cm2') \n",
      "\n",
      "plt.subplot(3,2,5).set_title('Spinehead icattype')\n",
      "plt.plot(time_h,icattype_show[0])\n",
      "plt.ylabel('mA/cm2') \n",
      "\n",
      "plt.subplot(3,2,6).set_title('Spinehead df/f')\n",
      "plt.plot(time_h,y_dff_spineh[0],'g') #for \"no OGB case\" use ccaspineh_show[0] with nM ylabel instead\n",
      "#plt.plot(time_h,ccaspineh_show[0])\n",
      "plt.ylabel('%')\n",
      "plt.show()\n",
      "\n",
      "####For saving data vectors use: np.savetxt('file name',vector name, fmt=\"%1.8f\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}