def getSessionData(self):
		sessiondata = None
		if 'sessionname' in self.params and self.params['sessionname'] is not None:
			sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		if not sessiondata and 'stackid' in self.params:
			from appionlib import apStack
			sessiondata = apStack.getSessionDataFromStackId(self.params['stackid'])
		if not sessiondata:
			### works with only canonical session names
			s = re.search('/([0-9][0-9][a-z][a-z][a-z][0-9][0-9][^/]*)/', self.params['rundir'])
			if s:
				self.params['sessionname'] = s.groups()[0]
				sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		return sessiondata
    def getSessionData(self):
        sessiondata = None
        if "sessionname" in self.params and self.params["sessionname"] is not None:
            sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
        if not sessiondata and "stackid" in self.params:
            from appionlib import apStack

            sessiondata = apStack.getSessionDataFromStackId(self.params["stackid"])
        if not sessiondata:
            ### works with only canonical session names
            s = re.search("/([0-9][0-9][a-z][a-z][a-z][0-9][0-9][^/]*)/", self.params["rundir"])
            if s:
                self.params["sessionname"] = s.groups()[0]
                sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
        return sessiondata
 def insertHipParamsData(self):
     HipParams = appiondata.ApHipParamsData()
     sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
     HipParams["session"] = sessiondata
     HipParams["hipRun"] = self.params["HipRun"]
     HipParams["numpart"] = self.params["numpart"]
     HipParams["replen"] = self.params["replen"]
     HipParams["diam"] = self.params["diameter"]
     HipParams["diaminner"] = self.params["diaminner"]
     HipParams["subunits"] = self.params["subunits"]
     HipParams["xlngth"] = self.params["xlngth"]
     HipParams["yht2"] = self.params["yht2"]
     HipParams["padval"] = self.params["padval"]
     HipParams["rescut"] = self.params["rescut"]
     HipParams["filval"] = self.params["filval"]
     HipParams["strong"] = os.path.join(self.params["rundir"], "strong.sa")
     HipParams["range"] = os.path.join(self.params["rundir"], "range.sa")
     HipParams["llbo"] = os.path.join(self.params["rundir"], "llbo.sa")
     HipParams["final_stack"] = os.path.join(self.params["rundir"], "start.hed")
     if self.params["commit"] is True:
         HipParams.insert()
     else:
         print HipParams
     self.params["HipParamsID"] = HipParams.dbid
     print "self.params['HipParamsID']", self.params["HipParamsID"]
     return
	def onInit(self):
		"""
		standard appionScript
		"""
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		if sessiondata:
			### METHOD 1 : session already exists
			apDisplay.printColor("Add images to an existing session", "cyan")
			### what about linking an existing session with project id to a new project id
			oldprojectid = apProject.getProjectIdFromSessionName(self.params['sessionname'])
			if oldprojectid != self.params['projectid']:
				apDisplay.printError("You cannot assign an existing session (PID %d) to a different project (PID %d)"%
					(oldprojectid, self.params['projectid']))
		else:
			### METHOD 2 : create new session
			apDisplay.printColor("Creating a new session", "cyan")
			### die for now
			apDisplay.printError("Uploading stack to a new session not allowed")
			try:
				directory = leginon.leginonconfig.mapPath(leginon.leginonconfig.IMAGE_PATH)
			except AttributeError:
				apDisplay.printWarning("Could not set directory")
				directory = ''
			userdata = None
			sessiondata = self.createSession(userdata, self.params['sessionname'], self.params['description'], directory)
			self.linkSessionProject(sessiondata, self.params['projectid'])

		self.sessiondata = sessiondata
		return
 def insertHipParamsData(self):
         HipParams = appiondata.ApHipParamsData()
         sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
         HipParams['session'] = sessiondata
         HipParams['hipRun'] = self.params['HipRun']
         HipParams['numpart'] = self.params['numpart']
         HipParams['replen'] = self.params['replen']
         HipParams['diam'] = self.params['diameter']
         HipParams['diaminner'] = self.params['diaminner']
         HipParams['subunits'] = self.params['subunits']
         HipParams['xlngth'] = self.params['xlngth']
         HipParams['yht2'] = self.params['yht2']
         HipParams['padval'] = self.params['padval']
         HipParams['rescut'] = self.params['rescut']
         HipParams['filval'] = self.params['filval']
         HipParams['strong'] = os.path.join(self.params['rundir'], "strong.sa")
         HipParams['range'] = os.path.join(self.params['rundir'], "range.sa")
         HipParams['llbo'] = os.path.join(self.params['rundir'], "llbo.sa")
         HipParams['final_stack'] = os.path.join(self.params['rundir'], "start.hed")
         if self.params['commit'] is True:
                 HipParams.insert()      
         else:
                 print HipParams
         self.params['HipParamsID'] = HipParams.dbid
         print "self.params['HipParamsID']",self.params['HipParamsID']
         return
def getStackIdFromSubStackName(substackname, sessionname, msg=True):
	"""
	For a given run name and session name find stack id
	"""
	sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)

	stackrunq = appiondata.ApStackRunData()
	stackrunq['session'] = sessiondata

	stackq = appiondata.ApStackData()
	stackq['substackname'] = substackname

	runsinstackq = appiondata.ApRunsInStackData()
	runsinstackq['stackRun'] = stackrunq
	runsinstackq['stack'] = stackq
	runsindatas = runsinstackq.query()
	if not runsindatas:
		return None
	if len(runsindatas) == 1:
		### simpe case
		stackid = runsindatas[0]['stack'].dbid
	else:
		for runsindata in runsindatas:
			print runsindata
		apDisplay.printError("Found too many sub-stacks for specified criteria")

	apDisplay.printMsg("Found stack id %d with substackname %s from session %s"%(stackid, substackname, sessionname))
	return stackid
def getNumAlignRunsFromSession(sessionname):
        sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)

        stackrunq = appiondata.ApStackRunData()
        stackrunq['session'] = sessiondata

        runsinstackq = appiondata.ApRunsInStackData()
        runsinstackq['stackRun'] = stackrunq
        runsindatas = runsinstackq.query()

        if not runsindatas:
                return 0
        alignidlist = []
        for runsindata in runsindatas:
                alignstackq = appiondata.ApAlignStackData()
                alignstackq['stack'] = runsindata['stack']
                alignstackdatas = alignstackq.query()
                if not alignstackdatas:
                        continue
                for alignstackdata in alignstackdatas:
                        alignrunid = alignstackdata['alignrun'].dbid

                        if not alignrunid in alignidlist:
                                alignidlist.append(alignrunid)

        return len(alignidlist)
	def checkPreviousTemplateRun(self):
		### check if we have a previous selection run
		selectrunq = appiondata.ApSelectionRunData()
		selectrunq['name'] = self.params['runname']
		selectrunq['session'] = sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		rundatas = selectrunq.query(results=1)
		if not rundatas:
			return True
		rundata = rundatas[0]

		### check if we have a previous template run
		templaterunq = appiondata.ApTemplateRunData(selectionrun=rundata)
		templatedatas = templaterunq.query()
		if not templatedatas:
			return True

		### make sure of using same number of templates
		if len(self.params['templateIds']) != len(templatedatas):
			apDisplay.printError("different number of templates from last run")

		### make sure we have same rotation parameters
		for i, templateid in enumerate(self.params['templateIds']):
			templaterunq  = appiondata.ApTemplateRunData()
			templaterunq['selectionrun'] = rundata
			templaterunq['template']     = appiondata.ApTemplateImageData.direct_query(templateid)
			### this is wrong only check last template run not this run
			queryresult = templaterunq.query(results=1)
			if queryresult:
				templatedata=queryresult[0]
		return True
	def checkConflicts(self):
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		self.sessiondata = sessiondata
		if self.params['goodalignerid'] is None:
			if self.params['tiltseriesnumber'] is None :
				apDisplay.printError("There is no tilt series specified")
			if self.params['runname'] is None:
				apDisplay.printError("enter a run name")
			if self.params['description'] is None:
				apDisplay.printError("enter a description, e.g. --description='awesome data'")
			tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseriesnumber'],sessiondata)
			self.params['tiltseries'] = tiltdata
			if self.params['othertilt'] is not None:
				othertiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['othertilt'],sessiondata)
				self.params['othertiltseries'] = othertiltdata
			else:
				self.params['othertiltseries'] = None
			if self.params['alignmethod'] == 'leginon':
				self.params['cycle'] = 0
				self.params['region'] = 100
		else:
			self.createParamsFromGoodAligner(self.params['goodalignerid'])
			if self.params['goodcycle'] is None:
				if self.params['goodstart'] or self.params['goodend']:
						apDisplay.printError("Cannot specify reseting image range without knowing the cycle number to reset to")
			elif self.params['goodcycle'] >= self.params['cycle']:
				apDisplay.printError("Only older cycle can be used as the base to reset image origin to")

		# Sampling is needed for protomo1 and 2, not imod
		if self.params['sample'] is not None:
			if self.params['sample'] < 1:
				apDisplay.printError("Sampling factor (--sample) must be greater or equal to 1.")
	def setRunDir(self):
		"""
		this function only runs if no rundir is defined at the command line
		"""
		if self.params['rundir'] is None:
			if ('sessionname' in self.params and self.params['sessionname'] is not None ):
				# command line users may use sessionname rather than expId
				sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
				self.params['rundir'] = self.getDefaultBaseAppionDir(sessiondata,[self.processdirname,self.params['runname']])
			else:
				if ('expId' in self.params and self.params['expId']):
					# expId should  always be included from appionwrapper derived appionscript
					sessiondata = apDatabase.getSessionDataFromSessionId(self.params['expId'])
					self.params['rundir'] = self.getDefaultBaseAppionDir(sessiondata,[self.processdirname,self.params['runname']])
		# The rest should not be needed with appionwrapper format
		from appionlib import apStack
		if ( self.params['rundir'] is None
		and 'reconid' in self.params
		and self.params['reconid'] is not None ):
			self.params['stackid'] = apStack.getStackIdFromRecon(self.params['reconid'], msg=False)
		if ( self.params['rundir'] is None
		and 'stackid' in self.params
		and self.params['stackid'] is not None ):
			#auto set the run directory
			stackdata = apStack.getOnlyStackData(self.params['stackid'], msg=False)
			path = os.path.abspath(stackdata['path']['path'])
			path = os.path.dirname(path)
			path = os.path.dirname(path)
			self.params['rundir'] = os.path.join(path, self.processdirname, self.params['runname'])
		self.params['outdir'] = self.params['rundir']
        def checkConflicts(self):
                if self.params['sessionname'] is None:
                        apDisplay.printError("Enter a session name (e.g. --session=06mar12a)")
                if self.params['pdbid'] is None and self.params['pdbfile'] is None:
                        apDisplay.printError("Specify a PDB id or PDB file")
                if self.params['pdbid'] is not None and self.params['pdbfile'] is not None:
                        apDisplay.printError("Specify only one of PDB id or PDB file")
                if self.params['pdbfile'] is not None and not os.path.isfile(self.params['pdbfile']):
                        apDisplay.printError("PDB file does not exist")
                ### PDB is case-insensitve, so make all caps for consistency
                if self.params['pdbid'] is not None:
                        self.params['pdbid'] = self.params['pdbid'].upper()

                if self.params['lowpass'] is None:
                        apDisplay.printError("Enter a lowpass value")
                if self.params['apix'] is None:
                        apDisplay.printError("Specify a pixel size")
                if self.params['lowpass'] < 1.5*self.params['apix']:
                        apDisplay.printWarning("Lowpass is less than pixelsize, overriding lp=1.5*apix")
                        self.params['lowpass'] = 1.5*self.params['apix']
                if self.params['symmetry'] is None and self.params['pdbfile'] is not None:
                        apDisplay.printError("Please specify symmetry group")
                elif self.params['symmetry'] is None:
                        apDisplay.printWarning("No symmetry specified using 'c1'")
                        self.params['symmetry'] = 'c1'
                self.params['symdata'] = apSymmetry.findSymmetry(self.params['symmetry'])
                self.sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
    def insertHipIterData(self):
        for i in range(3):
            iternum = i + 1
            apDisplay.printColor("\nUploading iteration %d of %d\n" % (iternum, 3), "green")
            HipIter = appiondata.ApHipIterData()
            sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
            HipIter["session"] = sessiondata
            if iternum == 1:
                avgpath = os.path.join(self.params["rundir"], "avgsnif1")
                HipIter["chop1"] = os.path.join(avgpath, "chop1.dek")
            elif iternum == 2:
                avgpath = os.path.join(self.params["rundir"], "avgsnif1/avgsnif2")
                HipIter["chop2"] = os.path.join(avgpath, "chop2.dek")
            elif iternum == 3:
                avgpath = os.path.join(self.params["rundir"], "avgsnif1/avgsnif2/avg3")
            volumeMrcFile = os.path.join(avgpath, "avglist3_%dp.mrc" % (self.params["rescut"]))
            apChimera.filterAndChimera(
                volumeMrcFile,
                res=10,
                apix=self.params["step"],
                chimtype="snapshot",
                contour=1.0,
                zoom=1,
                sym="c9",
                silhouette=True,
            )

            HipIter["hipRun"] = self.params["HipRun"]
            HipIter["iteration"] = iternum
            HipIter["iterpath"] = avgpath
            HipIter["volumeDensity"] = volumeMrcFile
            HipIter["resolution"] = self.getResolutionData(avgpath, iternum)
            HipIter["rMeasure"] = self.getRMeasureData(volumeMrcFile)
            HipIter["cutfit1"] = os.path.join(avgpath, "cutfit1.dek")
            HipIter["cutfit2"] = os.path.join(avgpath, "cutfit2.dek")
            HipIter["cutfit3"] = os.path.join(avgpath, "cutfit3.dek")
            HipIter["avglist_file"] = os.path.join(avgpath, "avglist3_%dp.list" % (self.params["rescut"]))
            f = open(HipIter["avglist_file"], "r")
            lines = f.readlines()
            f.close()
            HipIter["final_numpart"] = len(lines)
            HipIter["asymsu"] = int(
                ((self.params["boxsize"] * self.params["step"]) / self.params["replen"])
                * self.params["subunits"]
                * len(lines)
            )
            HipIter["avg_file"] = os.path.join(avgpath, "avglist3_%dp.avg" % (self.params["rescut"]))
            HipIter["map_file"] = os.path.join(avgpath, "avglist3_%dp.map" % (self.params["rescut"]))
            HipIter["mrc_file"] = os.path.join(avgpath, "avglist3_%dp.mrc" % (self.params["rescut"]))
            HipIter["ll_file"] = os.path.join(avgpath, "pll.list")
            HipIter["op_file"] = os.path.join(avgpath, "pop.list")
            HipIter["output_file"] = os.path.join(avgpath, "avglist3_%dp.OUTPUT" % (self.params["rescut"]))
            if self.params["commit"] is True:
                HipIter.insert()
            else:
                print HipIter
            self.params["HipIterID"] = HipIter.dbid
            print "self.params['HipIterID']", self.params["HipIterID"]
        return
	def showAssessedMask(self,imgfile,imgdata):
		self.filename = imgfile
		image = pyami.mrc.read(imgfile)
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		maskassessname = self.params['checkmask']
		mask,maskbin = apMask.makeInspectedMask(sessiondata,maskassessname,imgdata)
		overlay = apMask.overlayMask(image,mask)
		self.app.panel.setImage(overlay.astype(numpy.float32))		
def getNumCtfRunsFromSession(sessionname):
        sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)
        ctfrunq = appiondata.ApAceRunData()
        ctfrunq['session'] = sessiondata
        ctfrundatas = ctfrunq.query()
        if not ctfrundatas:
                return 0
        return len(ctfrundatas)
def getRecentSelectionIdFromSession(sessionname):
	sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)
	selectq = appiondata.ApSelectionRunData()
	selectq['session'] = sessiondata
	selectdatas = selectq.query(results=1)
	if not selectdatas:
		return None
	return selectdatas[0].dbid
def getNumSelectionRunsFromSession(sessionname):
	sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)
	selectq = appiondata.ApSelectionRunData()
	selectq['session'] = sessiondata
	selectdatas = selectq.query()
	if not selectdatas:
		return 0
	return len(selectdatas)
	def setRunDir(self):
		#auto set the output directory
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		path = os.path.abspath(sessiondata['image path'])
		pieces = path.split('leginon')
		path = 'leginon'.join(pieces[:-1]) + 'appion' + pieces[-1]
		path = re.sub("/rawdata","",path)
		path = os.path.join(path, self.processdirname, self.params['runname'])
		self.params['rundir'] = path
	def getFrameType(self):
		# set how frames are saved depending on what is found in the basepath
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		if sessiondata['frame path']:
			# 3.0+
			return ddinfo.getRawFrameType(sessiondata['frame path'])
		else:
			# pre-3.0
			return ddinfo.getRawFrameType(sessiondata['image path'])
 def setRunDir(self):
     # auto set the output directory
     sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
     path = os.path.abspath(sessiondata["image path"])
     pieces = path.split("leginon")
     path = "leginon".join(pieces[:-1]) + "appion" + pieces[-1]
     path = re.sub("/rawdata", "", path)
     path = os.path.join(path, self.processdirname, self.params["runname"])
     self.params["rundir"] = path
	def checkConflicts(self):
		if self.params['sessionname'] is None:
			apDisplay.printError("Enter a session name (e.g. --session=06mar12a)")
		if self.params['emdbid'] is None:
			apDisplay.printError("specify a emdb id")
		if self.params['symmetry'] is None:
			#apSymmetry.printSymmetries()
			apDisplay.printError("Enter a symmetry group, e.g. --symm=c1")
		self.params['symdata'] = apSymmetry.findSymmetry(self.params['symmetry'])
		self.sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
        def start(self):
        
                ### some of this should go in preloop functions
        
                ###do queries
                sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                self.sessiondata = sessiondata
                tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseries'],sessiondata)
                tiltseriessettings= apTomo.getTomographySettings(sessiondata,tiltseriesdata)
                tiltdata=apTomo.getImageList([tiltseriesdata])
                description = self.params['description']
                apDisplay.printMsg("getting imagelist")
                print "ordering list"
                tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(tiltdata)
                #tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int

                ###set up files
                seriesname=self.params['seriesname']
                print type(seriesname)
#               param_out=seriesname+'.param'

                ###insert protomo run params
                print "first insert"
                protomodata = apProTomo.insertProtomoParams(seriesname)

                print "second insert"
                alignrun = apTomo.insertTomoAlignmentRun(sessiondata,tiltseriessettings,None,protomodata,None,1,self.params['runname'],self.params['rundir'],self.params['description'])

                ###insert protomo alignment

                ###hack to get around need to parse protomo param file
                #should read imgref from tlt file
                refineparamdict={'alismp':None,'alibox_x':None,'alibox_y':None,'cormod':None,'imgref':None}
                ###
                self.params['goodcycle']=None
                if self.params['goodrange'] is None:
                        self.params['goodstart']=1
                        self.params['goodend']=len(tilts)
                alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata, self.params, refineparamdict,ordered_imagelist[refimg])

                # read tlt file
                print "third insert"
                alignmentdict, geometrydict, seriesname = apProTomo.parseTilt(self.params['tltfile'])

                # insert geometry model
                modeldata = apProTomo.insertModel2(alignerdata, geometrydict)

                #insert image alignments
                for i,imagedata in enumerate(ordered_imagelist):
                        #Caution...assumes ordered_imagelist is in same order as tlt file
                        apProTomo.insertTiltAlignment(alignerdata,imagedata,i,alignmentdict[i+1],center=None)

                print "fourth insert"
                apTomo.insertTiltsInAlignRun(alignrun, tiltseriesdata,tiltseriessettings,True)
 def setRunDir(self):
     if self.params["sessionname"] is None:
         apDisplay.printError("Please provide a sessionname or run directory")
     else:
         print self.params["sessionname"]
     sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
     path = os.path.abspath(sessiondata["image path"])
     path = re.sub("leginon", "appion", path)
     path = re.sub("/rawdata", "", path)
     path = os.path.join(path, self.processdirname, self.params["runname"])
     self.params["rundir"] = path
 def setRunDir(self):
         if self.params['reconid'] is not None:
                 self.recondata = apRecon.getRefineRunDataFromID(self.params['reconid'])
                 path = self.recondata['path']['path']
         if self.params['fulltomoid'] is not None:
                 self.tomodata = apTomo.getFullTomoData(self.params['fulltomoid'])
                 path = self.tomodata['path']['path']
         if self.params['sessionname'] is not None:
                 sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                 path = self.getDefaultBaseAppionDir(self.sessiondata,[])
         self.params['rundir'] = os.path.join(path,"misc")
 def setRunDir(self):
     sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
     tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params["tiltseriesnumber"], sessiondata)
     path = self.getDefaultBaseAppionDir(sessiondata, ["tomo"])
     tiltseriespath = "tiltseries%d" % self.params["tiltseriesnumber"]
     if self.params["full"]:
         tomovolumepath = ""
     else:
         tomovolumepath = self.params["volume"]
     intermediatepath = os.path.join(tiltseriespath, self.params["runname"], tomovolumepath)
     self.params["rundir"] = os.path.join(path, intermediatepath)
     self.params["aligndir"] = os.path.abspath(os.path.join(path, tiltseriespath, "align", self.params["runname"]))
	def checkConflicts(self):
		# check to make sure that incompatible parameters are not set
		if self.params['diam'] is None or self.params['diam']==0:
			apDisplay.printError("Please input the diameter of your particle (for display purposes only)")

		# get list of input images, since wildcards are supported
		if self.params['files'] is None:
			apDisplay.printError("Please enter the image names with picked particle files")

		if self.params['sessionname'] is None:
			apDisplay.printError("Please enter Name of session to upload to, e.g., --session=09dec07a")
		self.sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
 def getHipRunData(self):
     HipRun = appiondata.ApHipRunData()
     HipRun["runname"] = self.params["runname"]
     HipRun["path"] = appiondata.ApPathData(path=os.path.abspath(self.params["rundir"]))
     HipRun["description"] = self.params["description"]
     HipRun["REF|projectdata|projects|project"] = apProject.getProjectIdFromStackId(self.params["stackid"])
     sessiondata = apDatabase.getSessionDataFromSessionName(self.params["sessionname"])
     HipRun["session"] = sessiondata
     HipRun["apix"] = self.params["step"]
     self.stackdata = apStack.getOnlyStackData(self.params["stackid"], msg=False)
     HipRun["stack"] = self.stackdata
     return HipRun
 def getHipRunData(self):
         HipRun = appiondata.ApHipRunData()
         HipRun['runname'] = self.params['runname']
         HipRun['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
         HipRun['description'] = self.params['description']
         HipRun['REF|projectdata|projects|project'] = apProject.getProjectIdFromStackId(self.params['stackid'])
         sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
         HipRun['session'] = sessiondata
         HipRun['apix'] = self.params['step']
         self.stackdata = apStack.getOnlyStackData(self.params['stackid'], msg=False)
         HipRun['stack'] = self.stackdata
         return HipRun
	def setRunDir(self):
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseriesnumber'],sessiondata)
		path = self.getDefaultBaseAppionDir(sessiondata,['tomo'])
		tiltseriespath = "tiltseries%d" % self.params['tiltseriesnumber']
		if self.params['full']:
			tomovolumepath = ""
		else:
			tomovolumepath = self.params['volume']
		intermediatepath = os.path.join(tiltseriespath,self.params['runname'],tomovolumepath)
		self.params['rundir'] = os.path.join(path,intermediatepath)
		self.params['aligndir'] = os.path.abspath(os.path.join(path,tiltseriespath,'align',self.params['runname']))
        def insertFunctionRun(self):
                if self.params is None:
                        params = self.defaultparams.copy()
                if self.params['commit']==True:
                        sessionname = self.params['sessionname']
                        sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)

                        paramdata =self.insertFunctionParams(self.params)
                        maskRdata=apMask.createMaskMakerRun(sessiondata,self.params['rundir'],self.params['runname'],paramdata)
                        maskRdata.insert()
                else:
                        maskRdata = apMask.createMaskMakerRun(None,None,None,None)

                return maskRdata
	def start(self):
	
		### some of this should go in preloop functions
	
		###do queries
		os.chdir(self.params['rundir'])
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		self.sessiondata = sessiondata
		tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseries'],sessiondata)
		tiltdata = apTomo.getImageList([tiltseriesdata])
		description = self.params['description']
		apDisplay.printMsg("getting imagelist")

		tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(tiltdata)
Exemple #31
0
	def insertHipRunData(self):
		HipRun = appiondata.ApHipRunData()
		HipRun['runname'] = self.params['runname']
		HipRun['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		HipRun['description'] = self.params['description']
		HipRun['REF|projectdata|projects|project'] = apProject.getProjectIdFromStackId(self.params['stackid'])
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		HipRun['session'] = sessiondata
		HipRun['apix'] = self.params['step']
		self.stackdata = apStack.getOnlyStackData(self.params['stackid'], msg=False)
		HipRun['stack'] = self.stackdata
		if self.params['commit'] is True:
			HipRun.insert()
		else:
			print HipRun
		self.params['HipRunID'] = HipRun.dbid
		print "self.params['HipRunID']",self.params['HipRunID']
		self.params['HipRun'] = HipRun
		return HipRun
Exemple #32
0
    def checkConflicts(self):
        sessiondata = apDatabase.getSessionDataFromSessionName(
            self.params['sessionname'])
        self.sessiondata = sessiondata
        if self.params['goodalignerid'] is None:
            if self.params['tiltseriesnumber'] is None:
                apDisplay.printError("There is no tilt series specified")
            if self.params['runname'] is None:
                apDisplay.printError("enter a run name")
            if self.params['description'] is None:
                apDisplay.printError(
                    "enter a description, e.g. --description='awesome data'")
            tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
                self.params['tiltseriesnumber'], sessiondata)
            self.params['tiltseries'] = tiltdata
            if self.params['othertilt'] is not None:
                othertiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
                    self.params['othertilt'], sessiondata)
                self.params['othertiltseries'] = othertiltdata
            else:
                self.params['othertiltseries'] = None
            if self.params['alignmethod'] == 'leginon':
                self.params['cycle'] = 0
                self.params['region'] = 100
        else:
            self.createParamsFromGoodAligner(self.params['goodalignerid'])
            if self.params['goodcycle'] is None:
                if self.params['goodstart'] or self.params['goodend']:
                    apDisplay.printError(
                        "Cannot specify reseting image range without knowing the cycle number to reset to"
                    )
            elif self.params['goodcycle'] >= self.params['cycle']:
                apDisplay.printError(
                    "Only older cycle can be used as the base to reset image origin to"
                )

        # Sampling is needed for protomo1 and 2, not imod
        if self.params['sample'] is not None:
            if self.params['sample'] < 1:
                apDisplay.printError(
                    "Sampling factor (--sample) must be greater or equal to 1."
                )
Exemple #33
0
 def checkConflicts(self):
     sessiondata = apDatabase.getSessionDataFromSessionName(
         self.params['sessionname'])
     self.sessiondata = sessiondata
     if self.params['tiltseriesnumber'] is None:
         apDisplay.printError("There is no tilt series specified")
     if self.params['runname'] is None:
         apDisplay.printError("enter a run name")
     if self.params['description'] is None:
         apDisplay.printError(
             "enter a description, e.g. --description='awesome data'")
     tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
         self.params['tiltseriesnumber'], sessiondata)
     self.params['tiltseries'] = tiltdata
     if self.params['othertilt'] is not None:
         othertiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
             self.params['othertilt'], sessiondata)
         self.params['othertiltseries'] = othertiltdata
     else:
         self.params['othertiltseries'] = None
Exemple #34
0
    def insertRun(self):
        sessiondata = apDatabase.getSessionDataFromSessionName(
            self.params['sessionname'])
        pathq = appiondata.ApPathData(path=self.params['rundir'])
        pmparamq = appiondata.ApParticleMovieParamsData()

        paramlist = pmparamq.keys()
        for p in paramlist:
            if p.lower() in self.params:
                pmparamq[p] = self.params[p.lower()]
            else:
                print "missing", p.lower()
        pmrunq = appiondata.ApParticleMovieRunData(
            path=pathq,
            movieRunName=self.params['runname'],
            session=sessiondata,
            movieParams=pmparamq,
            selectionrun=self.selectiondata)
        r = pmrunq.insert()
        self.rundata = pmrunq
Exemple #35
0
        def start(self):
        
        
                ###do queries
                sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                self.sessiondata = sessiondata
                tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseries'],sessiondata)
                tiltseriessettings= apTomo.getTomographySettings(sessiondata,tiltseriesdata)
                tiltdata=apTomo.getImageList([tiltseriesdata])
                description = self.params['description']
                apDisplay.printMsg("getting imagelist")
                print "ordering list"
                tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(tiltdata)
                #tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int

                ###set up files
                seriesname='series'+str(self.params['tiltseries'])
                tiltfilename=seriesname+'.tlt'
                param_out=seriesname+'.param'
                maxtilt=max([abs(tilts[0]),abs(tilts[-1])])
                apDisplay.printMsg("highest tilt angle is %f" % maxtilt)
                self.params['cos_alpha']=math.cos(maxtilt*math.pi/180)
                self.params['raw_path']=os.path.join(self.params['rundir'],'raw')

                rawexists=apParam.createDirectory(self.params['raw_path'])

                apDisplay.printMsg("copying raw images")
                newfilenames=apProTomo.getImageFiles(ordered_imagelist,self.params['raw_path'], link=False)
                
                #get alignment data
                alignerdata = apTomo.getAlignerdata(self.params['alignerid'])
                imgshape = apTomo.getTomoImageShape(ordered_imagelist[0])
                imgcenter = {'x':self.imgshape[1]/2,'y':self.imgshape[0]/2}
                specimen_euler, azimuth, origins, rotations = apTomo.getAlignmentFromDB(alignerdata,imgcenter)

                #write protomo2 tilt file
                outtltfile='series.tlt'
                seriesname='series'
                
                apProTomo.writeTiltFile2(outfilename, seriesname, specimen_eulers, azimuth, referenceimage ):
 def setRunDir(self):
     """
             this function only runs if no rundir is defined at the command line
             """
     if self.params['rundir'] is None:
         if ('sessionname' in self.params
                 and self.params['sessionname'] is not None):
             # command line users may use sessionname rather than expId
             sessiondata = apDatabase.getSessionDataFromSessionName(
                 self.params['sessionname'])
             self.params['rundir'] = self.getDefaultBaseAppionDir(
                 sessiondata, [self.processdirname, self.params['runname']])
         else:
             if ('expId' in self.params and self.params['expId']):
                 # expId should  always be included from appionwrapper derived appionscript
                 sessiondata = apDatabase.getSessionDataFromSessionId(
                     self.params['expId'])
                 self.params['rundir'] = self.getDefaultBaseAppionDir(
                     sessiondata,
                     [self.processdirname, self.params['runname']])
     # The rest should not be needed with appionwrapper format
     from appionlib import apStack
     if (self.params['rundir'] is None and 'reconid' in self.params
             and self.params['reconid'] is not None):
         self.params['stackid'] = apStack.getStackIdFromRecon(
             self.params['reconid'], msg=False)
     if (self.params['rundir'] is None and 'stackid' in self.params
             and self.params['stackid'] is not None):
         #auto set the run directory
         stackdata = apStack.getOnlyStackData(self.params['stackid'],
                                              msg=False)
         path = os.path.abspath(stackdata['path']['path'])
         path = os.path.dirname(path)
         path = os.path.dirname(path)
         self.params['rundir'] = os.path.join(path, self.processdirname,
                                              self.params['runname'])
     self.params['outdir'] = self.params['rundir']
Exemple #37
0
    def onInit(self):
        """
		standard appionScript
		"""
        sessiondata = apDatabase.getSessionDataFromSessionName(
            self.params['sessionname'])
        if sessiondata:
            ### METHOD 1 : session already exists
            apDisplay.printColor("Add images to an existing session", "cyan")
            ### what about linking an existing session with project id to a new project id
            oldprojectid = apProject.getProjectIdFromSessionName(
                self.params['sessionname'])
            if oldprojectid != self.params['projectid']:
                apDisplay.printError(
                    "You cannot assign an existing session (PID %d) to a different project (PID %d)"
                    % (oldprojectid, self.params['projectid']))
        else:
            ### METHOD 2 : create new session
            apDisplay.printColor("Creating a new session", "cyan")
            ### die for now
            apDisplay.printError(
                "Uploading stack to a new session not allowed")
            try:
                directory = leginon.leginonconfig.mapPath(
                    leginon.leginonconfig.IMAGE_PATH)
            except AttributeError:
                apDisplay.printWarning("Could not set directory")
                directory = ''
            userdata = None
            sessiondata = self.createSession(userdata,
                                             self.params['sessionname'],
                                             self.params['description'],
                                             directory)
            self.linkSessionProject(sessiondata, self.params['projectid'])

        self.sessiondata = sessiondata
        return
 def eliminateMaskedParticles(self, particles, imgdata):
     newparticles = []
     eliminated = 0
     sessiondata = apDatabase.getSessionDataFromSessionName(
         self.params['sessionname'])
     if self.params['defocpair']:
         imgdata = apDefocalPairs.getTransformedDefocPair(imgdata, 2)
     maskimg, maskbin = apMask.makeInspectedMask(sessiondata,
                                                 self.params['maskassess'],
                                                 imgdata)
     if maskimg is not None:
         for prtl in particles:
             binnedcoord = (int(prtl['ycoord'] / maskbin),
                            int(prtl['xcoord'] / maskbin))
             if maskimg[binnedcoord] != 0:
                 eliminated += 1
             else:
                 newparticles.append(prtl)
         apDisplay.printMsg("%i particle(s) eliminated due to masking" %
                            eliminated)
     else:
         apDisplay.printMsg("no masking")
         newparticles = particles
     return newparticles
Exemple #39
0
	def uploadTemplateStack(self, insert=False):
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])

		uploadq = appiondata.ApTemplateStackData()
		uploadq['REF|projectdata|projects|project'] = self.params['projectid']
		if self.params['clusterId'] is not None:
			uploadq['clusterstack'] = appiondata.ApClusteringStackData.direct_query(self.params['clusterId'])
		elif self.params['alignId'] is not None:
			uploadq['alignstack'] = appiondata.ApAlignStackData.direct_query(self.params['alignId'])
		elif self.params['templatestack'] is not None:
			uploadq['origfile'] = self.params['templatestack']+".hed"
		uploadq['templatename'] = self.params['runname']+".hed"
		if self.params['templatetype'] == "clsavg":
			uploadq['cls_avgs'] = True
		if self.params['templatetype'] == "forward_proj":
			uploadq['forward_proj'] = True
		uploadq['description'] = self.params['description']
		uploadq['session'] = sessiondata
		uploadq['apix'] = self.params['apix']
		uploadq['boxsize'] = self.params['boxsize']
		uploadq['numimages'] = self.numimages
		uploadq['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		if insert is True:
			uploadq.insert()
 def getFrameType(self):
         # set how frames are saved depending on what is found in the basepath
         sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
         return ddinfo.getRawFrameType(sessiondata['image path'])
	def uploadData(self, ctfpartlist):

		### read mean /stdev for uploading
		self.getPartMeanTree(os.path.join(self.params['rundir'], self.params['finalstack']), ctfpartlist)

		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		if self.params['projectid'] is not None:
			projectnum = self.params['projectid']
		else:
			projectnum = apProject.getProjectIdFromSessionName(self.params['sessionname'])

		### create synthetic stack object ... not saving global params like runname, session, project, description, etc. here; that's in ApStackData
		syntheticq = appiondata.ApSyntheticStackParamsData()
		### get number of fakestack runs
		numentries = len(syntheticq)
		syntheticq['modelid'] = appiondata.ApInitialModelData.direct_query(self.params['modelid'])
		syntheticq['boxsize'] = self.params['box']
		syntheticq['apix'] = self.params['apix']
		syntheticq['projcount'] = self.params['projcount']
		syntheticq['projstdev'] = self.params['projstdev']
		syntheticq['shiftrad'] = self.params['shiftrad']
		syntheticq['rotang'] = self.params['rotang']
		syntheticq['flip'] = self.params['flip']
		syntheticq['kilovolts'] = self.params['kv']
		syntheticq['spher_aber'] = self.params['cs']
		syntheticq['defocus_x'] = self.params['df1']
		syntheticq['defocus_y'] = self.params['df2']
		syntheticq['randomdef'] = self.params['randomdef']
		if self.params['randomdef'] is True:
			syntheticq['randomdef_std'] = self.params['randomdef_std']
		syntheticq['astigmatism'] = self.params['astigmatism']
		syntheticq['snr1'] = self.params['snr1']
		syntheticq['snrtot'] = self.params['snrtot']
		syntheticq['envelope'] = os.path.basename(self.params['envelopefile'])
		syntheticq['ace2correct'] = self.params['ace2correct']
		syntheticq['ace2correct_rand'] = self.params['ace2correct_rand']
		if self.params['ace2correct_rand'] is True:
			syntheticq['ace2correct_std'] = self.params['ace2correct_std']
		syntheticq['ace2estimate'] = self.params['ace2estimate']
		syntheticq['lowpass'] = self.params['lpfilt']
		syntheticq['highpass'] = self.params['hpfilt']
		syntheticq['norm'] = self.params['norm']

		### fill stack parameters
		stparamq = appiondata.ApStackParamsData()
		stparamq['boxSize'] = self.params['box']
		stparamq['bin'] = 1
		stparamq['fileType'] = "imagic"
		stparamq['defocpair'] = 0
		stparamq['lowpass'] = self.params['lpfilt']
		stparamq['highpass'] = self.params['hpfilt']
		stparamq['norejects'] = 1
		if self.params['invert'] is True:
			stparamq['inverted'] = 1
		else:
			stparamq['inverted'] = 0
		if self.params['ace2correct'] is True or self.params['ace2correct_rand'] is True:
			stparamq['phaseFlipped'] = 1
			stparamq['fliptype'] = "ace2part"
		else:
			stparamq['phaseFlipped'] = 0
		stparamq['normalized'] = self.params['norm']

		paramslist = stparamq.query()

		### create a stack object
		stackq = appiondata.ApStackData()
		stackq['path'] = appiondata.ApPathData(path=os.path.abspath(self.params['rundir']))
		### see if stack already exists in the database (just checking path & name)
		uniqstackdatas = stackq.query(results=1)

		### create a stackRun object
		runq = appiondata.ApStackRunData()
		runq['stackRunName'] = self.params['runname']
		runq['session'] = sessiondata
		### see if stack run already exists in the database (just checking runname & session)
		uniqrundatas = runq.query(results=1)

		### finish stack object
		stackq['name'] = self.params['finalstack']
		stackq['description'] = self.params['description']
		stackq['hidden'] = 0
		stackq['pixelsize'] = self.params['apix'] * 1e-10
		stackq['boxsize'] = self.params['box']
		self.stackdata = stackq

		### finish stackRun object
		runq['stackParams'] = stparamq
		runq['syntheticStackParams'] = syntheticq
		self.stackrundata = runq

		### create runinstack object
		rinstackq = appiondata.ApRunsInStackData()
		rinstackq['stackRun'] = runq

        	### if not in the database, make sure run doesn't already exist
		if not uniqstackdatas and not uniqrundatas:
			if self.params['commit'] is True:
				apDisplay.printColor("Inserting stack parameters into database", "cyan")
				rinstackq['stack'] = stackq
				rinstackq.insert()
			else:
				apDisplay.printWarning("NOT INSERTING stack parameters into database")

		elif uniqrundatas and not uniqstackdatas:
			apDisplay.printError("Weird, run data without stack already in the database")
		else:

			rinstack = rinstackq.query(results=1)

			prevrinstackq = appiondata.ApRunsInStackData()
			prevrinstackq['stackRun'] = uniqrundatas[0]
			prevrinstackq['stack'] = uniqstackdatas[0]
			prevrinstack = prevrinstackq.query(results=1)

			## if no runinstack found, find out which parameters are wrong:
			if not rinstack:
				for i in uniqrundatas[0]:
					print "r =======",i,"========"
					if uniqrundatas[0][i] != runq[i]:
						apDisplay.printError("the value for parameter '"+str(i)+"' is different from before")
					else:
						print i,uniqrundatas[0][i],runq[i]
				for i in uniqrundatas[0]['stackParams']:
					print "p =======",i,"========"
					if uniqrundatas[0]['stackParams'][i] != stparamq[i]:
						apDisplay.printError("the value for parameter '"+str(i)+"' is different from before")
					else:
						print i, uniqrundatas[0]['stackParams'][i], stparamq[i]
				for i in uniqstackdatas[0]:
					print "s =======",i,"========"
					if uniqstackdatas[0][i] != stackq[i]:
						apDisplay.printError("the value for parameter '"+str(i)+"' is different from before")
					else:
						print i,uniqstackdatas[0][i],stackq[i]
				for i in prevrinstack[0]:
					print "rin =======",i,"========"
					if prevrinstack[0][i] != rinstackq[i]:
						print i,prevrinstack[0][i],rinstackq[i]
						apDisplay.printError("the value for parameter '"+str(i)+"' is different from before")
					else:
						print i,prevrinstack[0][i],rinstackq[i]
				apDisplay.printError("All parameters for a particular stack must be identical! \n"+\
											 "please check your parameter settings.")
			apDisplay.printWarning("Stack already exists in database! Will try and appending new particles to stack")

		### create a fake selection run
#		selectq = appiondata.ApSelectionRunData()
#		selectq['session'] = sessiondata
#		selectq['name'] = "fakerun"
#		self.selectq = selectq
		if self.params['commit'] is True:
			apDisplay.printColor("Inserting fake selection parameters into the database", "cyan")
#			selectq.insert()
		else:
			apDisplay.printWarning("NOT INSERTING fake selection parameters into the database")

		partNumber = 0
		### loop over the particles and insert
		if self.params['commit'] is True:
			apDisplay.printColor("inserting particle parameters into database", "cyan")
		else:
			apDisplay.printWarning("NOT INSERTING particle parameters into database")
		for i in range(len(ctfpartlist)):
			partNumber += 1
			partfile = ctfpartlist[i]
			partmeandict = self.partmeantree[i]

			partq = appiondata.ApParticleData()
#			partq['selectionrun'] = selectq
			partq['xcoord'] = partNumber

			stpartq = appiondata.ApStackParticleData()

			### check unique params
			stpartq['stack'] = self.stackdata
			stpartq['stackRun'] = self.stackrundata
			stpartq['particleNumber'] = partNumber
			stpartdata = stpartq.query(results=1)
			if stpartdata:
				apDisplay.printError("trying to insert a duplicate particle")

			stpartq['particle'] = partq
			stpartq['mean'] = partmeandict['mean']
			stpartq['stdev'] = partmeandict['stdev']
			if self.params['commit'] is True:
				stpartq.insert()

		return
Exemple #42
0
def getSessionDataByName(name):
	sessiondata = apDatabase.getSessionDataFromSessionName(name)
	return sessiondata
def uploadTomo(params):
    if not params['commit']:
        apDisplay.printWarning("not commiting tomogram to database")
        return
    apDisplay.printMsg("Commiting tomogram to database")
    sessiondata = apDatabase.getSessionDataFromSessionName(
        params['sessionname'])
    tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
        params['tiltseriesnumber'], sessiondata)
    runname = params['runname']
    name = params['name']
    if params['full']:
        fullbin = params['bin']
    else:
        fullbin = 1
        subbin = params['bin']
    alignrun = insertTomoAlignmentRun(sessiondata, None, None, None, None,
                                      fullbin, runname, params['aligndir'],
                                      'manual alignment from upload')
    # only tilt series in one alignrun for now
    insertTiltsInAlignRun(alignrun, tiltdata, None, True)
    alignerdata = insertAlignerParams(alignrun, params)
    firstimagedata = getFirstImage(tiltdata)
    path = os.path.abspath(params['rundir'])
    description = params['description']
    if params['full']:
        thickness = params['shape'][0] * fullbin
        uploadfile = params['zprojfile']
        projectimagedata = uploadZProjection(runname, firstimagedata,
                                             uploadfile)
        fullrundata = insertFullTomoRun(sessiondata, path, runname, 'upload')
        return insertFullTomogram(sessiondata, tiltdata, alignerdata,
                                  fullrundata, name, description,
                                  projectimagedata, thickness, None, fullbin,
                                  [])
    else:
        projectimagedata = None
        fulltomopath = params['rundir'].replace('/' + params['volume'], '')
        dummyname = 'dummy'
        dummydescription = 'fake full tomogram for subtomogram upload'
        thickness = params['shape'][0] * subbin
        fullrundata = insertFullTomoRun(sessiondata, fulltomopath, runname,
                                        'upload')
        fulltomogram = insertFullTomogram(sessiondata, tiltdata, alignerdata,
                                          fullrundata, dummyname,
                                          dummydescription, projectimagedata,
                                          thickness, None, fullbin, [])
        apix = apDatabase.getPixelSize(firstimagedata)
        tomoq = appiondata.ApTomogramData()
        tomoq['session'] = sessiondata
        tomoq['tiltseries'] = tiltdata
        results = tomoq.query()
        index = len(results) + 1
        pixelsize = 1e-10 * apix * subbin
        runname = params['volume']
        shape = map((lambda x: x * subbin), params['shape'])
        dimension = {'x': shape[2], 'y': shape[1], 'z': shape[0]}
        subtomorundata = insertSubTomoRun(sessiondata, None, None, runname,
                                          params['invert'], subbin)
        return insertSubTomogram(fulltomogram, subtomorundata, None, 0,
                                 dimension, path, name, index, pixelsize,
                                 description)
Exemple #44
0
        def insertHipParticleData(self):
                self.particleNumber = 0
                alldir = os.listdir(self.params['rundir'])
                ems_list = []
                for file in alldir:
                        if re.search(".ems.out", file):
                                ems_list.append(file)
                ems_list.sort()

                avgdir = os.listdir(os.path.join(self.params['rundir'],"avgsnif1/avgsnif2/avg3"))
                fhlxavg_list = []       
                for file in avgdir:
                        if re.search("f.hlxavg_dek", file):
                                fhlxavg_list.append(file)
                fhlxavg_list.sort()

                nhlxavg_list = []       
                for file in avgdir:
                        if re.search("n.hlxavg_dek", file):
                                nhlxavg_list.append(file)
                nhlxavg_list.sort()

                for i in range(len(ems_list)):
                        filename = ems_list[i]
                        ### Will the filenames always be part000000.ext with apXmipp.breakStackIntoSingleFiles??  This is a risky way to get the file prefix. Find a better way!
                        self.filep = filename[0:10]
                        self.particleNumber += 1
                        HipParticle = appiondata.ApHipParticleData()
                        HipParticle['hipRun'] = self.params['HipRun']

                        for file in fhlxavg_list:
                                if re.search(self.filep, file):
                                        f = open(os.path.join(self.params['rundir'], "avgsnif1/avgsnif2/avg3", file), 'r')
                                        lines = f.readlines()
                                        keyline = lines[2]
                                        pzr_list = keyline.split()
                                        f.close()
                                        HipParticle['far_phi'] = pzr_list[2]
                                        HipParticle['far_z'] = pzr_list[3]
                                        HipParticle['far_rscale'] = pzr_list[4]
                                        HipParticle['far_ampscale'] = lines[-1]
                        for file in nhlxavg_list:
                                if re.search(self.filep, file):
                                        f = open(os.path.join(self.params['rundir'], "avgsnif1/avgsnif2/avg3", file), 'r')
                                        lines = f.readlines()
                                        keyline = lines[2]
                                        pzr_list = keyline.split()
                                        f.close()
                                        HipParticle['ner_phi'] = pzr_list[2]
                                        HipParticle['ner_z'] = pzr_list[3]
                                        HipParticle['ner_rscale'] = pzr_list[4]
                                        HipParticle['ner_ampscale'] = lines[-1]
                        f = open(ems_list[i], 'r')
                        lines = f.readlines()
                        lastline = lines[-1]
                        tsr_list = lastline.split()
                        f.close()
                        sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                        HipParticle['session'] = sessiondata
                        HipParticle['filename'] = self.filep
                        HipParticle['stack'] = self.stackdata
                        HipParticle['mrc_file'] = os.path.join(self.params['rundir'], self.filep + ".mrc")
                        HipParticle['s_file'] = os.path.join(self.params['rundir'], self.filep + ".s")
                        HipParticle['dft_file'] = os.path.join(self.params['rundir'], self.filep + ".dft")
                        HipParticle['colb_file'] = os.path.join(self.params['rundir'], self.filep + ".colb")
                        HipParticle['ner_file'] = os.path.join(self.params['rundir'], self.filep + ".ner")
                        HipParticle['far_file'] = os.path.join(self.params['rundir'], self.filep + ".far")
                        HipParticle['fft_file'] = os.path.join(self.params['rundir'], self.filep + ".fft")
                        self.stackrundata = appiondata.ApStackRunData()
                        HipParticle['stackRun'] = self.stackrundata
                        HipParticle['particleNumber'] = self.particleNumber
                        HipParticle['tilt'] = tsr_list[0]
                        HipParticle['shift'] = tsr_list[1]
                        HipParticle['resid'] = tsr_list[2]
                        HipParticledata = HipParticle.query(results=1)
                        if HipParticledata:
                                apDisplay.printError("trying to insert a duplicate particle")
                        if self.params['commit'] is True:
                                HipParticle.insert()
                        self.params['HipParticleID'] = HipParticle.dbid
                        print "self.params['HipParticleID']",self.params['HipParticleID']
                return
 def getImageShapeFromTiltSeries(self):
         sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
         tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseriesnumber'],sessiondata)
         firstimagedata = apTomo.getFirstImage(tiltdata)
         self.imageshape = apTomo.getTomoImageShape(firstimagedata)
Exemple #46
0
 def setRunDir(self):
     #auto set the run directory
     sessiondata = apDatabase.getSessionDataFromSessionName(
         self.params['sessionname'])
     path = os.path.abspath(sessiondata['image path'])
     self.params['rundir'] = path
    def start(self):

        ### some of this should go in preloop functions

        ###do queries
        sessiondata = apDatabase.getSessionDataFromSessionName(
            self.params['sessionname'])
        self.sessiondata = sessiondata
        tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
            self.params['tiltseries'], sessiondata)
        tiltseriessettings = apTomo.getTomographySettings(
            sessiondata, tiltseriesdata)
        tiltdata = apTomo.getImageList([tiltseriesdata])
        description = self.params['description']
        apDisplay.printMsg("getting imagelist")
        print "ordering list"
        tilts, ordered_imagelist, ordered_mrc_files, refimg = apTomo.orderImageList(
            tiltdata)
        #tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int

        ###set up files
        seriesname = self.params['seriesname']
        print type(seriesname)
        #		param_out=seriesname+'.param'

        ###insert protomo run params
        print "first insert"
        protomodata = apProTomo.insertProtomoParams(seriesname)

        print "second insert"
        alignrun = apTomo.insertTomoAlignmentRun(sessiondata,
                                                 tiltseriessettings, None,
                                                 protomodata, None, 1,
                                                 self.params['runname'],
                                                 self.params['rundir'],
                                                 self.params['description'])

        ###insert protomo alignment

        ###hack to get around need to parse protomo param file
        #should read imgref from tlt file
        refineparamdict = {
            'alismp': None,
            'alibox_x': None,
            'alibox_y': None,
            'cormod': None,
            'imgref': None
        }
        ###
        self.params['goodcycle'] = None
        if self.params['goodrange'] is None:
            self.params['goodstart'] = 1
            self.params['goodend'] = len(tilts)
        alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata,
                                                     self.params,
                                                     refineparamdict,
                                                     ordered_imagelist[refimg])

        # read tlt file
        print "third insert"
        alignmentdict, geometrydict, seriesname = apProTomo.parseTilt(
            self.params['tltfile'])

        # insert geometry model
        modeldata = apProTomo.insertModel2(alignerdata, geometrydict)

        #insert image alignments
        for i, imagedata in enumerate(ordered_imagelist):
            #Caution...assumes ordered_imagelist is in same order as tlt file
            apProTomo.insertTiltAlignment(alignerdata,
                                          imagedata,
                                          i,
                                          alignmentdict[i + 1],
                                          center=None)

        print "fourth insert"
        apTomo.insertTiltsInAlignRun(alignrun, tiltseriesdata,
                                     tiltseriessettings, True)
Exemple #48
0
	def setRunDir(self):
		#auto set the output directory
		sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
		self.params['rundir'] = self.getDefaultBaseAppionDir(sessiondata,['templatestacks',self.params['runname']])