// This function turns on a given number of inhibitory synapses contained within a 
// given SectionList.  !!NOTE!!: this function does not attempt to turn off
// all synapses before activating the desired synapses.  It merely sets a 
// subset of synapses to "on", agnostic to whether they were previously on
// or not.  Note, however, that this function can be called twice to first
// turn all synapses off, and then turn on only the desired synapses.  See
// notes below.
//
// This function can also be used to toggle off synapses in the following way:
// A) to shut off all synapses in a given SectionList, set $2<0.
// B) to shut off all synapses in all sections, set $2<0 and set $o2 to be
//	the whole tree.
//
// Also, to activate all synapses in a given region, just set $2 to be a huge
//	number; this will activate all of the synapses in the desired region.
//
// INPUT:
// $o1: SectionList instance.  Sections to draw from.
// $2: variable.  Number of synapses to active.
// $3: variable.  The seed to assign.
// ($s4,$5 ; $s6,$7 ; ...): optional pairs of inhibitory driver line tags and logicals.
//	$s4,6,8,... should be strdefs corresponding to genotypes (ie, "vgat",
//	"sst","npy").  $5,7,9,... correspond to logicals
//	as to whether these genotypes should be required (=1), should be
//	not included at all (=0).  If it doesn't matter for analysis, can
//	leave these entries blank.
//
// OUTPUT:
// the .isOn flag is toggled as desired for the synapses selected.
//
// As an example, say one wanted to turn off all inhibitory synapses, except
// for 20 (VGAT+, SST-) interneurons in the tuft.  The calls would be:
//	activateInh(-1,cellList,1) // shuts of all inhibition, where cellList is a SectionList containing all sections.
//	activateInh(tuftList,20,1,"vgat",1,"sst",0) // turn on 20 (VGAT+,SST-) synapses in the tuft.
//

strdef curGenStr
obfunc activateInhibition() {local numGen,i,ii,jj,curGenFlag localobj allSyns,theSec,passSyns,screenedSyns,finalSyns
	allSyns = new Vector() // all (potential) synapses that qualify for activation	
	passSyns = new Vector() // binary vector representing whether each element
				// in allSyns has passed the screening process for
				// different genotypes.
	screenedSyns = new Vector() // vector of synapse indices that pass screening process
	finalSyns = new Vector() // vector of synapse indices that will be activated
	
	numGen = int(numarg()/2 - 1) // number of genotypes being screened for or against

	// interate over all synapses, adding those that are in the right
	// domain.
	for ii=0,totVgatAt-1{
		synGABA[ii].get_loc()
		theSec = new SectionRef()
		if(sectionRefInList(theSec,$o1)){
			allSyns.append(ii)
		}
		pop_section()
	}
	
	if($2<0){
		// Want to shut off all synapses in the provided region.
		for ii=0,allSyns.size()-1{
			synGABA[allSyns.x[ii]].isOn = 0
			synGABArect[allSyns.x[ii]].isOn = 0
			synGABAB[allSyns.x[ii]].isOn = 0
		}
		return finalSyns
	}else{
	
		// for all synapses that are flagged, screen for/against, as set
		// by desired arguments.
		passSyns = new Vector(allSyns.size(),1) // originally, all start with one
	
		for jj=1,numGen{
			i = 2*(jj+1) // indices
			curGenStr = $si // contains genotype flag
			i = i + 1
			curGenFlag = $i
	
			for ii=0,allSyns.size()-1{
				if(curGenFlag<0.1) {
	
					// remove all synapses with this particular flag
					if(abs(strcmp(curGenStr,"vgat"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].vgat-1)<0.1){
							passSyns.x[ii] = 0
						}
					}
					if(abs(strcmp(curGenStr,"sst"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].sst-1)<0.1){
							passSyns.x[ii] = 0
						}
					}
					if(abs(strcmp(curGenStr,"npy"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].npy-1)<0.1){
							passSyns.x[ii] = 0
						}
					}
				}else{
	
					// remove synapses without this particular flag
					if(abs(strcmp(curGenStr,"vgat"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].vgat-1)>0.1){
							passSyns.x[ii] = 0
						}
					}
					if(abs(strcmp(curGenStr,"sst"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].sst-1)>0.1){
							passSyns.x[ii] = 0
						}
					}
					if(abs(strcmp(curGenStr,"npy"))<0.1){
						if(abs(synGABA[allSyns.x[ii]].npy-1)>0.1){
							passSyns.x[ii] = 0
						}
					}
				}
			}
		}			
	
	
		// Take only synapses that have passed the screening process.
		for jj=0,allSyns.size()-1{
			if(passSyns.x[jj]>0.1){
				screenedSyns.append(allSyns.x[jj])
			}
		}
		
		// Choose random indices from desired subset.
		finalSyns = sampleNoReplace(screenedSyns,$2,$3) // supply a non-random seed
		
		// Activate these synapses.
		for ii=0,finalSyns.size()-1{
			synGABA[finalSyns.x[ii]].isOn=1
			synGABArect[finalSyns.x[ii]].isOn=1
			synGABAB[finalSyns.x[ii]].isOn=1
		}
		
		// Give a final printout.
		print "Activated ",finalSyns.size,"inhibitory inputs"
		
		
		return finalSyns
	}
}