def checkDuplicateRefBasedRun(self):
		### setup ref based run
		refrunq = appiondata.ApRefBasedRunData()
		refrunq['name'] = self.params['runname']
		refrunq['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		uniquerun = refrunq.query(results=1)
		if uniquerun:
			apDisplay.printError("Run name '"+self.params['runname']+"' and path already exist in database")
		return
	def insertRefBasedRun(self, partlist, imagicstack, insert=False):
		apDisplay.printMsg("committing results to DB")

		### setup alignment run
		alignrunq = appiondata.ApAlignRunData()
		alignrunq['runname'] = self.params['runname']
		alignrunq['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		uniquerun = alignrunq.query(results=1)
		if uniquerun:
			apDisplay.printError("Run name '"+runparams['runname']+"' and path already exist in database")

		### setup ref based run
		refrunq = appiondata.ApRefBasedRunData()
		refrunq['runname'] = self.params['runname']
		refrunq['xysearch'] = self.params['xysearch']
		refrunq['xystep'] = self.params['xystep']
		refrunq['first_ring'] = self.params['firstring']
		refrunq['last_ring'] = self.params['lastring']
		refrunq['num_iter'] = self.params['numiter']
		refrunq['invert_templs'] = self.params['inverttemplates']
		refrunq['num_templs'] = self.params['numtemplate']
		#refrunq['csym', int),
		refrunq['run_seconds'] = self.params['runtime']

		### finish alignment run
		alignrunq = appiondata.ApAlignRunData()
		alignrunq['refbasedrun'] = refrunq
		alignrunq['hidden'] = False
		alignrunq['bin'] = self.params['bin']
		alignrunq['hp_filt'] = self.params['highpass']
		alignrunq['lp_filt'] = self.params['lowpass']
		alignrunq['runname'] = self.params['runname']
		alignrunq['description'] = self.params['description']

		### setup alignment stack
		alignstackq = appiondata.ApAlignStackData()
		alignstackq['alignrun'] = alignrunq

		alignstackq['imagicfile'] = imagicstack
		alignstackq['avgmrcfile'] = "average.mrc"
		emancmd = "proc2d templatestack%02d.spi templatestack%02d.hed"%(self.params['numiter'],self.params['numiter'])
		apEMAN.executeEmanCmd(emancmd)
		alignstackq['refstackfile'] = ("templatestack%02d.hed"%(self.params['numiter']))
		alignstackq['alignrun'] = alignrunq
		alignstackq['iteration'] = self.params['numiter']
		alignstackq['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		### check to make sure files exist
		imagicfile = os.path.join(self.params['rundir'], alignstackq['imagicfile'])
		if not os.path.isfile(imagicfile):
			apDisplay.printError("could not find stack file: "+imagicfile)
		avgmrcfile = os.path.join(self.params['rundir'], alignstackq['avgmrcfile'])
		if not os.path.isfile(avgmrcfile):
			apDisplay.printError("could not find average mrc file: "+avgmrcfile)
		refstackfile = os.path.join(self.params['rundir'], alignstackq['refstackfile'])
		if not os.path.isfile(refstackfile):
			apDisplay.printError("could not find reference stack file: "+refstackfile)
		alignstackq['stack'] = self.stack['data']
		alignstackq['boxsize'] = self.boxsize
		alignstackq['pixelsize'] = self.stack['apix']*self.params['bin']
		alignstackq['description'] = self.params['description']
		alignstackq['hidden'] = False
		alignstackq['num_particles'] = self.params['numpart']

		if insert is True:
			alignstackq.insert()

		### insert reference data
		reflist = []

		for j in range(self.params['numiter']):
			iternum = j+1
			for i in range(len(self.templatelist)):
				refnum = i+1
				templateid = self.templatelist[i]
				refq = appiondata.ApAlignReferenceData()
				refq['refnum'] = refnum
				refq['iteration'] = iternum
				refq['template'] = apTemplate.getTemplateFromId(templateid)
				refq['mrcfile'] = ("templateavg%02d-%02d.mrc"%(iternum,refnum))
				refpath = os.path.join(self.params['rundir'], "templates")
				refq['path'] = appiondata.ApPathData(path=os.path.abspath(refpath))
				refq['alignrun'] = alignrunq
				if refnum  in self.resdict:
					refq['ssnr_resolution'] = self.resdict[refnum]
				if insert is True:
					refq.insert()
				if iternum == self.params['numiter']:
					reflist.append(refq)
		#refq['varmrcfile', str),
		#refq['frc_resolution', float),

		### insert particle data
		apDisplay.printColor("Inserting particle alignment data, please wait", "cyan")
		for partdict in partlist:
			### see apSpider.alignment.alignStack() for more info
			"""
			partdict.keys()
			'num': int(data[0]), #SPIDER NUMBERING: 1,2,3,...
			'template': int(abs(templatenum)), #SPIDER NUMBERING: 1,2,3,...
			'mirror': checkMirror(templatenum),
			'score': float(data[3]),
			'rot': float(data[4]),
			'xshift': float(data[5]),
			'yshift': float(data[6]),
			"""

			alignpartq = appiondata.ApAlignParticleData()
			alignpartq['ref'] = reflist[partdict['template']-1]
			alignpartq['partnum'] = partdict['num']
			alignpartq['alignstack'] = alignstackq
			stackpartdata = apStack.getStackParticle(self.params['stackid'], partdict['num'])
			alignpartq['stackpart'] = stackpartdata
			alignpartq['xshift'] = partdict['xshift']
			alignpartq['yshift'] = partdict['yshift']
			alignpartq['rotation'] = partdict['rot']
			alignpartq['score'] = partdict['score']
			alignpartq['mirror'] = partdict['mirror']

			if insert is True:
				alignpartq.insert()
		return