def applyBfactor(infile, fscfile, apix, mass=None, outfile=None):
        embfactorfile = "embfactor64.exe"
        embfactorexe = apParam.getExecPath("embfactor64.exe")
        if embfactorexe is None:
                apDisplay.printWarning("Could not find %s"%(embfactorfile))
                return infile
        if outfile is None:
                outfile = os.path.splitext(infile)[0]+"-bfactor.mrc"
        cmd = embfactorexe
        cmd += " -FSC %s"%(fscfile)
        cmd += " -sampling %.3f"%(apix)
        ### this option always failed for me -neil
        #if mass is not None:
        #       cmd += " -molweight %d"%(mass*1000)
        cmd += " %s"%(infile)
        cmd += " %s"%(outfile)
        apParam.runCmd(cmd, package="B-factor", verbose=True, showcmd=True)

        if not apVolume.isValidVolume(outfile):
                apDisplay.printWarning("B-factor correction failed %s"%(embfactorfile))
                return infile

        return outfile
def applyBfactor(infile, fscfile, apix, mass=None, outfile=None):
    embfactorfile = "embfactor64.exe"
    embfactorexe = apParam.getExecPath("embfactor64.exe")
    if embfactorexe is None:
        apDisplay.printWarning("Could not find %s" % (embfactorfile))
        return infile
    if outfile is None:
        outfile = os.path.splitext(infile)[0] + "-bfactor.mrc"
    cmd = embfactorexe
    cmd += " -FSC %s" % (fscfile)
    cmd += " -sampling %.3f" % (apix)
    ### this option always failed for me -neil
    #if mass is not None:
    #	cmd += " -molweight %d"%(mass*1000)
    cmd += " %s" % (infile)
    cmd += " %s" % (outfile)
    apParam.runCmd(cmd, package="B-factor", verbose=True, showcmd=True)

    if not apVolume.isValidVolume(outfile):
        apDisplay.printWarning("B-factor correction failed %s" %
                               (embfactorfile))
        return infile

    return outfile
	def start(self):
		if self.params['method'] == 'any':
			### startAny uses class averages
			clusterstack, numimages = self.getClusterStack()
		else:
			### starticos, startoct, startcsym uses individual particles
			clusterstack, numimages = self.getClusterParticles()

		if self.params['method'] != 'any':
			if self.params['numkeep'] is not None and numimages/10 < int(self.params['numkeep']):
				apDisplay.printWarning("particle number of "+ self.params['numkeep']
					+ " is greater than 10% of the number of selected classes")
			elif self.params['numkeep'] is None:
				self.params['numkeep'] = int(math.floor(numimages/20.0))+1
				apDisplay.printWarning("numkeep was not defined, using %d particles"%(self.params['numkeep']))

		nproc = apParam.getNumProcessors()

		#construct command for each of the EMAN commonline method
		if self.params['method'] == 'any':
			startcmd = "startAny "+clusterstack+" proc="+str(nproc)
			startcmd +=" sym="+self.symmdata['eman_name']
			if self.params['mask'] is not None:
				startcmd +=" mask="+str(self.params['mask'])
			else:
				maskrad = math.floor(self.clusterstackdata['clusterrun']['boxsize']/2.0)
				startcmd +=" mask=%d"%(maskrad)
			if self.params['rounds'] is not None:
				startcmd +=" rounds="+str(self.params['rounds'])

		elif self.params['method'] == 'csym':
			startcmd = "startcsym "+clusterstack+" "
			startcmd +=" "+str(self.params['numkeep'])
			startcmd +=" sym="+self.symmdata['eman_name']
			if self.params['imask'] is not None:
				startcmd +=" imask="+self.params['imask']

		elif self.params['method'] == 'oct':
			startcmd = "startoct "+clusterstack+" "
			startcmd +=" "+str(self.params['numkeep'])

		elif self.params['method'] == 'icos':
			startcmd = "starticos "+clusterstack+" "
			startcmd +=" "+str(self.params['numkeep'])
			if self.params['imask'] is not None:
				startcmd +=" imask="+self.params['imask']

		apDisplay.printMsg("Creating 3D model with EMAN function: start"+self.params['method'])
		apFile.removeFile("threed.0a.mrc")
		apFile.removeFile("eman.log")
		apEMAN.executeEmanCmd(startcmd, verbose=False, logfile="eman.log")
		#apEMAN.executeEmanCmd(startcmd, verbose=True)

		finalmodelname = "threed-%s-eman_start%s.mrc"%(self.timestamp, self.params['method'])
		finalmodelpath = os.path.join(self.params['rundir'], finalmodelname)
		apDisplay.printMsg("Final model name: "+finalmodelname)
		finalmodel = "threed.0a.mrc"
		if os.path.isfile(finalmodel):
			emancmd = "proc3d %s %s norm=0,1 origin=0,0,0"%(finalmodel, finalmodelpath)
			#shutil.move(finalmodel, finalmodelpath)
			apEMAN.executeEmanCmd(emancmd, verbose=True)
			if not apVolume.isValidVolume(finalmodelpath):
				apDisplay.printError("Created volume is not valid")
		else:
			apDisplay.printError("No 3d model was created")

		### upload it
		self.uploadDensity(finalmodelpath)

		### chimera imaging
		apChimera.renderSnapshots(finalmodelpath, contour=self.params['contour'],
			zoom=self.params['zoom'], sym=self.symmdata['eman_name'])
		apChimera.renderAnimation(finalmodelpath, contour=self.params['contour'],
			zoom=self.params['zoom'], sym=self.symmdata['eman_name'])

		### remove stack
		if apFile.stackSize(clusterstack)/1024**2 > 10:
			### file bigger than 10MB
			apFile.removeStack(clusterstack)
		apFile.removeFile("threed.0a.mrc")
        def start(self):
                if self.params['method'] == 'any':
                        ### startAny uses class averages
                        clusterstack, numimages = self.getClusterStack()
                else:
                        ### starticos, startoct, startcsym uses individual particles
                        clusterstack, numimages = self.getClusterParticles()

                if self.params['method'] != 'any':
                        if self.params['numkeep'] is not None and numimages/10 < int(self.params['numkeep']):
                                apDisplay.printWarning("particle number of "+ self.params['numkeep']
                                        + " is greater than 10% of the number of selected classes")
                        elif self.params['numkeep'] is None:
                                self.params['numkeep'] = int(math.floor(numimages/20.0))+1
                                apDisplay.printWarning("numkeep was not defined, using %d particles"%(self.params['numkeep']))

                nproc = apParam.getNumProcessors()

                #construct command for each of the EMAN commonline method
                if self.params['method'] == 'any':
                        startcmd = "startAny "+clusterstack+" proc="+str(nproc)
                        startcmd +=" sym="+self.symmdata['eman_name']
                        if self.params['mask'] is not None:
                                startcmd +=" mask="+str(self.params['mask'])
                        else:
                                maskrad = math.floor(self.clusterstackdata['clusterrun']['boxsize']/2.0)
                                startcmd +=" mask=%d"%(maskrad)
                        if self.params['rounds'] is not None:
                                startcmd +=" rounds="+str(self.params['rounds'])

                elif self.params['method'] == 'csym':
                        startcmd = "startcsym "+clusterstack+" "
                        startcmd +=" "+str(self.params['numkeep'])
                        startcmd +=" sym="+self.symmdata['eman_name']
                        if self.params['imask'] is not None:
                                startcmd +=" imask="+self.params['imask']

                elif self.params['method'] == 'oct':
                        startcmd = "startoct "+clusterstack+" "
                        startcmd +=" "+str(self.params['numkeep'])

                elif self.params['method'] == 'icos':
                        startcmd = "starticos "+clusterstack+" "
                        startcmd +=" "+str(self.params['numkeep'])
                        if self.params['imask'] is not None:
                                startcmd +=" imask="+self.params['imask']

                apDisplay.printMsg("Creating 3D model with EMAN function: start"+self.params['method'])
                apFile.removeFile("threed.0a.mrc")
                apFile.removeFile("eman.log")
                apEMAN.executeEmanCmd(startcmd, verbose=False, logfile="eman.log")
                #apEMAN.executeEmanCmd(startcmd, verbose=True)

                finalmodelname = "threed-%s-eman_start%s.mrc"%(self.timestamp, self.params['method'])
                finalmodelpath = os.path.join(self.params['rundir'], finalmodelname)
                apDisplay.printMsg("Final model name: "+finalmodelname)
                finalmodel = "threed.0a.mrc"
                if os.path.isfile(finalmodel):
                        emancmd = "proc3d %s %s norm=0,1 origin=0,0,0"%(finalmodel, finalmodelpath)
                        #shutil.move(finalmodel, finalmodelpath)
                        apEMAN.executeEmanCmd(emancmd, verbose=True)
                        if not apVolume.isValidVolume(finalmodelpath):
                                apDisplay.printError("Created volume is not valid")
                else:
                        apDisplay.printError("No 3d model was created")

                ### upload it
                self.uploadDensity(finalmodelpath)

                ### chimera imaging
                apChimera.renderSnapshots(finalmodelpath, contour=self.params['contour'],
                        zoom=self.params['zoom'], sym=self.symmdata['eman_name'])
                apChimera.renderAnimation(finalmodelpath, contour=self.params['contour'],
                        zoom=self.params['zoom'], sym=self.symmdata['eman_name'])

                ### remove stack
                if apFile.stackSize(clusterstack)/1024**2 > 10:
                        ### file bigger than 10MB
                        apFile.removeStack(clusterstack)
                apFile.removeFile("threed.0a.mrc")