/************************************************************

	Christina Weaver
	August 2011
	
	insert channels from Vetter et al (2001) into Jennie's 
	PFC and Visual Cortex neurons.  Includes Mainen's 
	synthetic axon (from the 1995 Mainen et al. paper).

************************************************************/

load_file("nrngui.hoc")

// now load morph;  recall that 'basic_shape()' resets the morphology
load_file("May3IR2t_ImportFromSWCthenManual_Aug19-11.hoc")

load_file("aux_procs.hoc")

xopen("electro_procs.hoc")

    PFC_effective_ApicalSpineDensity = 0.83524895	//  total of 5891 spines, divided by total apical length of 7052.987 microns
    PFC_effective_BasalSpineDensity  = 1.1177593	//  total of 4014 spines, divided by total basal  length of 3591.1131 microns

    applySubtreeConstantSpineDensity(apical, SurfaceAreaOneApicalSpine, PFC_effective_ApicalSpineDensity)
    applySubtreeConstantSpineDensity(basal,  SurfaceAreaOneBasalSpine,  PFC_effective_BasalSpineDensity)
    geom_nseg(100,0.1)

// Using SEClamp, as recommended on the NEURON User Forum.  See init_PFC.hoc for details.
INITDUR = 80	

steps_per_ms = 40
dt = 0.025

xopen("PFC-V1_AddSynapses.hoc")









distance()

objref synBranches, synLoc

/*** new parameter settings as of 23 Jan 2012 ***/
VO = -70
V0 = -70	//avoid ambiguity of "Capital O" vs "zero 0"

set_epasNG(69)
scale_gpas(3.8e-5)
scaleNa(105,1e3)
scaleKV(115,16.6667)

forall { if( ismembrane("na") )   vshift_na=-10.5 }  

forall { v_init = -70 }


/**** 
    set up a Voltage Clamp
****/

objref seClamp
    soma seClamp = new SEClamp(0.5)
    seClamp.dur1 = 1e9
    seClamp.amp1 = -70
    seClamp.dur2 = 0




/***** end voltage clamp ****/






/**********************  copied from synTweak.hoc in ~/LuebkeAmatrudo_forCluster  *********/



strdef synFilename
objref synFout, tv, iv

/**********************

    Written originally in main_PFC_simEPSC_all.hoc; now copied here.

    $1  number of synapses
    $2  value of tau1
    $3  value of tau2
    $4  value of gAMPA
    $s5 file basename

**********************/
proc synTweak() { local i, vecsz, btyp

    adjust_tau1($2,$1)
    adjust_tau2($3,$1)
    adjust_gAMPA($4,$1)

    sprint(synFilename,"%s_tR%.4f_tF%.2f_gAMP%.5f.Ibin",$s5,$2,$3,$4)
    synFout = new File()
    synFout.wopen(synFilename)

    tv = new Vector()
    tv.record(&t)
    iv = new Vector()
    iv.record(&seClamp.i)

    init()
    run()

    vecsz = tv.size()
    synFout.vwrite(&vecsz)
    tv.fwrite(synFout)
    iv.fwrite(synFout)
    synFout.close()

    sprint(synFilename,"%s_tR%.4f_tF%.2f_gAMP%.5f_dist.txt",$s5,$2,$3,$4)
    synFout = new File()
    synFout.wopen(synFilename)

    i = 0
    forsec synBranches {
        btyp = 0
	ifsec apical { btyp = 1 }
	ifsec basal  { btyp = 2 }
        synFout.printf("%d\t%g\t%g\t%d\n",i,distance(synLoc.x[i]),distance(synLoc.x[i])-soma.diam,btyp)
         i+=1
    }
    synFout.close()
}



/**********************  end from synTweak.hoc in ~/LuebkeAmatrudo_forCluster  *********/





xopen("plot_seClamp_i.ses")

nBr = 0

if( doApic == 1 ) {

    // activate apical synapses
    nSynapse = AddExcSynapses_byNumber(59,0,200,200,synBranches,synLoc)

    endSyn = 200 + (nSynapse + 2)*200
    distance()
    cnt = 0

    tstop=endSyn
    synTweak(nSynapse,.15,4,.00054,"fig11_PFCapic")

} else {

    // activate basal synapses
    nSynapse = AddExcSynapses_byNumber(0,40,200,200,synBranches,synLoc)

    endSyn = 200 + (nSynapse + 2)*200
    distance()
    cnt = 0

    tstop=endSyn
    synTweak(nSynapse,.15,4,.00054,"fig11_PFCbas")
}


printf("\n\n**************************\n\n")
printf("Output has been written to a file ending in .Ibin, a customized binary file format.\n")
printf("These files can be read with MATLAB.  See the .m files contained in this directory.\n")
printf("MATLAB's Statistics Toolbox is required to analyze EPSC shapes.\n")
printf("\tSample usage:  read_EPSCsims_mdb('fig11_PFCapic',0.00054)\n")
printf("\n\n**************************\n\n")