Exemplo n.º 1
0
def runAceCorrect(imgdict,params):
	imgname = imgdict['filename']
	imgpath = os.path.join(imgdict['session']['image path'], imgname+'.mrc')

	voltage = (imgdict['scope']['high tension'])
	apix    = apDatabase.getPixelSize(imgdict)

	ctfvalues = ctfdb.getBestCtfByResolution(imgdata)
	conf = ctfdb.calculateConfidenceScore(bestctfvalue)

	ctdimname = imgname
	ctdimpath = os.path.join(params['rundir'],ctdimname)
	print "Corrected Image written to " + ctdimpath

	#pdb.set_trace()
	acecorrectcommand=("ctfcorrect1('%s', '%s', '%.32f', '%.32f', '%f', '%f', '%f');" % \
		(imgpath, ctdimpath, ctfvalues['defocus1'], ctfvalues['defocus2'], -ctfvalues['angle_astigmatism'], voltage, apix))
	print acecorrectcommand
	try:
		matlab = pymat.open("matlab -nosplash")
	except:
		apDisplay.environmentError()
		raise
	pymat.eval(matlab, acecorrectcommand)
	pymat.close(matlab)

	return
	def phaseFlipAceTwo(self, inimgpath, imgdata):

		apix = apDatabase.getPixelSize(imgdata)
		bestctfvalue = self.getBestCtfValue(imgdata, True)

		if bestctfvalue is None:
			apDisplay.printWarning("No ctf estimation for current image")
			self.badprocess = True
			return None

		if bestctfvalue['acerun'] is None:
			apDisplay.printWarning("No ctf runid for current image")
			self.badprocess = True
			return None

		if bestctfvalue['ctfvalues_file'] is None:
			# Since method = ace2 requires a ctfvalues_file,
			# create file from database values

			### cannot use ACE2 correction without CS value in database
			if not 'cs' in bestctfvalue:
				apDisplay.printMsg('No spherical abberation value in database, skipping image')
				self.badprocess = True
				return None

			# create ctfvalues_file from ctf run
			ctfvaluesfile = "tmp_ctfvaluesfile.txt"

			if abs(bestctfvalue['defocus1']) < abs(bestctfvalue['defocus2']):
				## this is the canonical form
				df1 = abs(bestctfvalue['defocus1'])
				df2 = abs(bestctfvalue['defocus2'])
				angast = bestctfvalue['angle_astigmatism']
			else:
				apDisplay.printWarning("|def1| > |def2|, flipping defocus axes")
				df1 = abs(bestctfvalue['defocus2'])
				df2 = abs(bestctfvalue['defocus1'])
				angast = bestctfvalue['angle_astigmatism'] + 90
			amp = bestctfvalue['amplitude_contrast']
			kv = imgdata['scope']['high tension']/1000
			cs = self.getCS(bestctfvalue)/1000
			conf = ctfdb.calculateConfidenceScore(bestctfvalue)

			if os.path.isfile(ctfvaluesfile):
				os.remove(ctfvaluesfile)
			f = open(ctfvaluesfile,'w')
			f.write("\tFinal Params for image: %s.mrc\n"%imgdata['filename'])
			# acecorrect definition is opposite to database
			f.write("\tFinal Defocus (m,m,deg): %.6e %.6e %.6f\n"%(df1,df2,-angast))
			f.write("\tAmplitude Contrast: %.6f\n"%amp)
			f.write("\tVoltage (kV): %.6f\n"%kv)
			f.write("\tSpherical Aberration (mm): %.6e\n"%cs)
			f.write("\tAngstroms per pixel: %.6e\n"%apix)
			f.write("\tConfidence: %.6e\n"%conf)
			f.close()

		# use ace2 ctfvalues file
		else:
			ctfvaluesfile = os.path.join(bestctfvalue['acerun']['path']['path'], bestctfvalue['ctfvalues_file'])

			ctfvaluesfilesplit = os.path.splitext(ctfvaluesfile)
			while ctfvaluesfilesplit[1] != '.mrc':
				ctfvaluesfilesplit = os.path.splitext(ctfvaluesfilesplit[0])

			ctfvaluesfile = ctfvaluesfilesplit[0]+".mrc.ctf.txt"

		defocus1 = bestctfvalue['defocus1']
		defocus2 = bestctfvalue['defocus2']
		defocus = (defocus1+defocus2)/2.0*1.0e6

		apDisplay.printMsg("using ctfvaluesfile: "+ctfvaluesfile)

		if not os.path.isfile(ctfvaluesfile):
			apDisplay.printError("ctfvaluesfile does not exist")

		ace2exe = self.getACE2Path()
		outfile = os.path.join(os.getcwd(),imgdata['filename']+".mrc.corrected.mrc")

		ace2cmd = (ace2exe+" -ctf %s -apix %.3f -img %s -out %s" % (ctfvaluesfile, apix, inimgpath,outfile))
		if self.params['fliptype'] == "ace2image":
			ace2cmd += " -wiener 0.1"
		apDisplay.printMsg("ace2 command: "+ace2cmd)
		apDisplay.printMsg("phaseflipping entire micrograph with defocus "+str(round(defocus,3))+" microns")

		#apEMAN.executeEmanCmd(ace2cmd, showcmd = True)
		if self.params['verbose'] is True:
			ace2proc = subprocess.Popen(ace2cmd, shell=True)
		else:
			aceoutf = open("ace2.out", "a")
			aceerrf = open("ace2.err", "a")
			ace2proc = subprocess.Popen(ace2cmd, shell=True, stderr=aceerrf, stdout=aceoutf)
		ace2proc.wait()
		if self.stats['count'] <= 1:
			### ace2 always crashes on first image??? .fft_wisdom file??
			time.sleep(0.1)
			if self.params['verbose'] is True:
				ace2proc = subprocess.Popen(ace2cmd, shell=True)
			else:
				aceoutf = open("ace2.out", "a")
				aceerrf = open("ace2.err", "a")
				ace2proc = subprocess.Popen(ace2cmd, shell=True, stderr=aceerrf, stdout=aceoutf)
			ace2proc.wait()

		if self.params['verbose'] is False:
			aceoutf.close()
			aceerrf.close()

		if not os.path.isfile(outfile):
			apDisplay.printError("ACE 2 failed to create image file:\n%s" % outfile)

		return outfile
	def checkCtfParams(self, imgdata):
		shortname = apDisplay.short(imgdata['filename'])
		ctfvalue = self.getBestCtfValue(imgdata)

		### check if we have values and if we care
		if ctfvalue is None:
			return not self.checkRequireCtf()

		### check that CTF estimation is above confidence threshold
		conf = ctfdb.calculateConfidenceScore(ctfvalue)
		if self.params['ctfcutoff'] and conf < self.params['ctfcutoff']:
			apDisplay.printColor(shortname+" is below confidence threshold (conf="+str(round(conf,3))+")\n","cyan")
			return False

		### check resolution requirement for CTF fit at 0.8 threshold
		if self.params['ctfres80min'] is not None or self.params['ctfres80max'] is not None:
			if not 'resolution_80_percent' in ctfvalue.keys() or ctfvalue['resolution_80_percent'] is None:
				apDisplay.printColor("%s: no 0.8 resolution found"%(shortname), "cyan")
				return False
			if self.params['ctfres80max'] and ctfvalue['resolution_80_percent'] > self.params['ctfres80max']:
				apDisplay.printColor("%s is above resolution threshold %.2f > %.2f"
					%(shortname, ctfvalue['resolution_80_percent'], self.params['ctfres80max']), "cyan")
				return False
			if self.params['ctfres80min'] and ctfvalue['resolution_80_percent'] < self.params['ctfres80min']:
				apDisplay.printColor("%s is below resolution threshold %.2f > %.2f"
					%(shortname, ctfvalue['resolution_80_percent'], self.params['ctfres80min']), "cyan")
				return False

		### check resolution requirement for CTF fit at 0.5 threshold
		if self.params['ctfres50min'] is not None or self.params['ctfres50max'] is not None:
			if not 'resolution_50_percent' in ctfvalue.keys() or ctfvalue['resolution_50_percent'] is None:
				apDisplay.printColor("%s: no 0.5 resolution found"%(shortname), "cyan")
				return False
			if self.params['ctfres50max'] and ctfvalue['resolution_50_percent'] > self.params['ctfres50max']:
				apDisplay.printColor("%s is above resolution threshold %.2f > %.2f"
					%(shortname, ctfvalue['resolution_50_percent'], self.params['ctfres50max']), "cyan")
				return False
			if self.params['ctfres50min'] and ctfvalue['resolution_50_percent'] < self.params['ctfres50min']:
				apDisplay.printColor("%s is below resolution threshold %.2f > %.2f"
					%(shortname, ctfvalue['resolution_50_percent'], self.params['ctfres50min']), "cyan")
				return False

		if self.params['mindefocus'] is not None or self.params['maxdefocus'] is not None:
			### get best defocus value
			### defocus should be in negative meters
			if ctfvalue['defocus2'] is not None and ctfvalue['defocus1'] != ctfvalue['defocus2']:
				defocus = (ctfvalue['defocus1'] + ctfvalue['defocus2'])/2.0
			else:
				defocus = ctfvalue['defocus1']
			defocus = -1.0*abs(defocus)	

			### assume defocus values are ALWAYS negative but mindefocus is greater than maxdefocus
			if self.params['mindefocus']:
				self.params['mindefocus'] = -abs( self.params['mindefocus'] )
			if self.params['maxdefocus']:
				self.params['maxdefocus'] = -abs( self.params['maxdefocus'] )
			if self.params['mindefocus'] and self.params['maxdefocus']:
				if self.params['maxdefocus'] > self.params['mindefocus']:
					mindef = self.params['mindefocus']
					maxdef = self.params['maxdefocus']
					self.params['mindefocus'] = maxdef
					self.params['maxdefocus'] = mindef
			### skip micrograph that have defocus above or below min & max defocus levels
			if self.params['mindefocus'] and defocus > self.params['mindefocus']:
				#apDisplay.printColor(shortname+" defocus ("+str(round(defocus*1e6,2))+\
				#	" um) is less than mindefocus ("+str(self.params['mindefocus']*1e6)+" um)\n","cyan")
				return False
			if self.params['maxdefocus'] and defocus < self.params['maxdefocus']:
				#apDisplay.printColor(shortname+" defocus ("+str(round(defocus*1e6,2))+\
				#	" um) is greater than maxdefocus ("+str(self.params['maxdefocus']*1e6)+" um)\n","cyan")
				return False

		return True
Exemplo n.º 4
0
    def checkCtfParams(self, imgdata):
        shortname = apDisplay.short(imgdata['filename'])
        ctfvalue = self.getBestCtfValue(imgdata)

        ### check if we have values and if we care
        if ctfvalue is None:
            return not self.checkRequireCtf()

        ### check that CTF estimation is above confidence threshold
        conf = ctfdb.calculateConfidenceScore(ctfvalue)
        if self.params['ctfcutoff'] and conf < self.params['ctfcutoff']:
            apDisplay.printColor(
                shortname + " is below confidence threshold (conf=" +
                str(round(conf, 3)) + ")\n", "cyan")
            return False

        ### check resolution requirement for CTF fit at 0.8 threshold
        if self.params['ctfres80min'] is not None or self.params[
                'ctfres80max'] is not None:
            if not 'resolution_80_percent' in ctfvalue.keys(
            ) or ctfvalue['resolution_80_percent'] is None:
                apDisplay.printColor(
                    "%s: no 0.8 resolution found" % (shortname), "cyan")
                return False
            if self.params['ctfres80max'] and ctfvalue[
                    'resolution_80_percent'] > self.params['ctfres80max']:
                apDisplay.printColor(
                    "%s is above resolution threshold %.2f > %.2f" %
                    (shortname, ctfvalue['resolution_80_percent'],
                     self.params['ctfres80max']), "cyan")
                return False
            if self.params['ctfres80min'] and ctfvalue[
                    'resolution_80_percent'] < self.params['ctfres80min']:
                apDisplay.printColor(
                    "%s is below resolution threshold %.2f > %.2f" %
                    (shortname, ctfvalue['resolution_80_percent'],
                     self.params['ctfres80min']), "cyan")
                return False

        ### check resolution requirement for CTF fit at 0.5 threshold
        if self.params['ctfres50min'] is not None or self.params[
                'ctfres50max'] is not None:
            if not 'resolution_50_percent' in ctfvalue.keys(
            ) or ctfvalue['resolution_50_percent'] is None:
                apDisplay.printColor(
                    "%s: no 0.5 resolution found" % (shortname), "cyan")
                return False
            if self.params['ctfres50max'] and ctfvalue[
                    'resolution_50_percent'] > self.params['ctfres50max']:
                apDisplay.printColor(
                    "%s is above resolution threshold %.2f > %.2f" %
                    (shortname, ctfvalue['resolution_50_percent'],
                     self.params['ctfres50max']), "cyan")
                return False
            if self.params['ctfres50min'] and ctfvalue[
                    'resolution_50_percent'] < self.params['ctfres50min']:
                apDisplay.printColor(
                    "%s is below resolution threshold %.2f > %.2f" %
                    (shortname, ctfvalue['resolution_50_percent'],
                     self.params['ctfres50min']), "cyan")
                return False

        if self.params['mindefocus'] is not None or self.params[
                'maxdefocus'] is not None:
            ### get best defocus value
            ### defocus should be in negative meters
            if ctfvalue['defocus2'] is not None and ctfvalue[
                    'defocus1'] != ctfvalue['defocus2']:
                defocus = (ctfvalue['defocus1'] + ctfvalue['defocus2']) / 2.0
            else:
                defocus = ctfvalue['defocus1']
            defocus = -1.0 * abs(defocus)

            ### assume defocus values are ALWAYS negative but mindefocus is greater than maxdefocus
            if self.params['mindefocus']:
                self.params['mindefocus'] = -abs(self.params['mindefocus'])
            if self.params['maxdefocus']:
                self.params['maxdefocus'] = -abs(self.params['maxdefocus'])
            if self.params['mindefocus'] and self.params['maxdefocus']:
                if self.params['maxdefocus'] > self.params['mindefocus']:
                    mindef = self.params['mindefocus']
                    maxdef = self.params['maxdefocus']
                    self.params['mindefocus'] = maxdef
                    self.params['maxdefocus'] = mindef
            ### skip micrograph that have defocus above or below min & max defocus levels
            if self.params[
                    'mindefocus'] and defocus > self.params['mindefocus']:
                #apDisplay.printColor(shortname+" defocus ("+str(round(defocus*1e6,2))+\
                #	" um) is less than mindefocus ("+str(self.params['mindefocus']*1e6)+" um)\n","cyan")
                return False
            if self.params[
                    'maxdefocus'] and defocus < self.params['maxdefocus']:
                #apDisplay.printColor(shortname+" defocus ("+str(round(defocus*1e6,2))+\
                #	" um) is greater than maxdefocus ("+str(self.params['maxdefocus']*1e6)+" um)\n","cyan")
                return False

        return True
Exemplo n.º 5
0
def runAce(matlab, imgdata, params, showprev=True):
	imgname = imgdata['filename']

	if showprev is True:
		bestctfvalue = ctfdb.getBestCtfByResolution(imgdata)
		if bestctfvalue:
			bestconf = ctfdb.calculateConfidenceScore(bestctfvalue)
			print ( "Prev best: '"+bestctfvalue['acerun']['name']+"', conf="+
				apDisplay.colorProb(bestconf)+", defocus="+str(round(-1.0*abs(bestctfvalue['defocus1']*1.0e6),2))+
				" microns" )

	if params['uncorrected']:
		tmpname='temporaryCorrectedImage.mrc'
		imgarray = apDBImage.correctImage(imgdata)
		imgpath = os.path.join(params['rundir'],tmpname)
		apImage.arrayToMrc(imgarray, imgpath)
		print "processing", imgpath
	else:
		imgpath = os.path.join(imgdata['session']['image path'], imgname+'.mrc')

	nominal = None
	if params['nominal'] is not None:
		nominal=params['nominal']
	elif params['newnominal'] is True:
		bestctfvalue = ctfdb.getBestCtfByResolution(imgdata)
		nominal = bestctfvalue['defocus1']
	if nominal is None:
		nominal = imgdata['scope']['defocus']

	if nominal is None or nominal > 0 or nominal < -15e-6:
			apDisplay.printWarning("Nominal should be of the form nominal=-1.2e-6"+\
				" for -1.2 microns NOT:"+str(nominal))

	#Neil's Hack
	#if 'autosample' in params and params['autosample']:
	#	x = abs(nominal*1.0e6)
	#	val = 1.585 + 0.057587 * x - 0.044106 * x**2 + 0.010877 * x**3
	#	resamplefr_override = round(val,3)
	#	print "resamplefr_override=",resamplefr_override
	#	pymat.eval(matlab, "resamplefr="+str(resamplefr_override)+";")

	pymat.eval(matlab,("dforig = %e;" % nominal))

	if params['stig'] == 0:
		plist = (imgpath, params['outtextfile'], params['display'], params['stig'],\
			params['medium'], -nominal, params['tempdir']+"/")
		acecmd = makeMatlabCmd("ctfparams = ace(",");",plist)
	else:
		plist = (imgname, imgpath, params['outtextfile'], params['opimagedir'], \
			params['matdir'], params['display'], params['stig'],\
			params['medium'], -nominal, params['tempdir']+"/", params['resamplefr'])
		acecmd = makeMatlabCmd("ctfparams = measureAstigmatism(",");",plist)

	#print acecmd
	pymat.eval(matlab,acecmd)

	matfile = os.path.join(params['matdir'], imgname+".mrc.mat")
	if params['stig']==0:
		savematcmd = "save('"+matfile+"','ctfparams','scopeparams', 'dforig');"
		pymat.eval(matlab,savematcmd)

	ctfvalue = pymat.get(matlab, 'ctfparams')
	ctfvalue=ctfvalue[0]

	printResults(params, nominal, ctfvalue)

	return ctfvalue