Exemple #1
0
	def alignClasses(self):
		classname = "classes"

		# using IMAGIC to align references
		# center the particles
		if self.params['mramethod'] == "imagic":
			#imagic centering doesn't work as well as EMAN's unfortunately
			#cenfile = imagicAlignment.alimass(classname+".hed", maxshift=0.15, ceniter=10, nproc=1)
			aligntime0 = time.time()

			# convert mask to fraction for imagic
			maskfrac = self.workingmask*2/self.workingboxsize

			# don't align references for last iteration
			if self.params['currentiter'] < self.params['iter']:
				if self.params['classiter'] is True:
					emancmd = "e2stacksort.py classes_avg.hed outtmpcls.hdf --simalign=rotate_translate --center --useali --iterative"
					apEMAN.executeEmanCmd(emancmd, verbose=False)
					apFile.removeStack(classname)
					classname = "a"+classname
					# convert hdf back to imagic
					emancmd = "proc2d outtmpcls.hdf %s.hed"%classname
					apEMAN.executeEmanCmd(emancmd, verbose=False)
					apFile.removeStack("outtmpcls.hdf")
				else:
					if self.params['nocenter'] is False and self.params['nomask'] is False:
						emancmd = "proc2d %s.hed %s_cen.hed center"%(classname,classname)
						## try cenalignint
						emancmd = "cenalignint %s.hed"%classname
						apEMAN.executeEmanCmd(emancmd, verbose=False)
						## cenalignint also makes a stack with badly centered avgs 
						if os.path.isfile("bad.hed"):
							emancmd = "proc2d bad.hed ali.hed"
							apEMAN.executeEmanCmd(emancmd, verbose=False)
							apFile.removeStack("bad")
						if self.params['keepall'] is False:
							apFile.removeStack(classname)
						classname = classname+"_cen"
						classname = "ali"

					alifile = imagicAlignment.alirefs(classname, mask=0.99, maxshift=0.1)
					if self.params['keepall'] is False:
						apFile.removeStack(classname)
					classname = alifile

			# mask the classes
			if self.params['nomask'] is False:
				maskfile = imagicFilters.softMask(classname, mask=maskfrac, falloff=0.1)
				if self.params['keepall'] is False:
					apFile.removeStack(classname)
			else:
				maskfile = classname

			# normalize the classes
			classname = imagicFilters.normalize(maskfile, sigma=10.0)
			if self.params['keepall'] is False:
				apFile.removeStack(maskfile)

			apDisplay.printColor("finished alignment in "+apDisplay.timeString(time.time()-aligntime0), "cyan")

		else:
			# for projection matching with EMAN:
			# don't align references for last iteration
			if self.params['currentiter'] < self.params['iter']:
				emancmd = "classalign2 %s.hed 10 keep=100 saveali"%classname
				apEMAN.executeEmanCmd(emancmd, verbose=False)
				apFile.removeStack(classname)
				classname = "a"+classname

			# mask the averages
			emancmd = "proc2d %s.hed %s_mask.hed mask=%i"%(classname,classname,(self.workingmask))
			apEMAN.executeEmanCmd(emancmd, verbose=False)
			apFile.removeStack(classname)
			classname = classname+"_mask"

			# normalize references
			emancmd = "proc2d %s.hed %s_norm.hed norm"%(classname,classname)
			apEMAN.executeEmanCmd(emancmd, verbose=False)
			apFile.removeStack(classname)
			classname = classname+"_norm"

		outputcls = os.path.join(self.params['rundir'],"classes%02i" % self.params['currentiter'])
		# copy normalized particles to run directory
		emancmd = "proc2d %s.hed %s.hed"%(classname,outputcls)
		apEMAN.executeEmanCmd(emancmd, verbose=False)
		apFile.removeStack(classname)

		self.params['currentcls'] = outputcls
Exemple #2
0
	def start(self):
		self.insertTopolRepJob()
		self.stack = {}
		self.stack['apix'] = apStack.getStackPixelSizeFromStackId(self.params['stackid'])
		if self.params['virtualdata'] is not None:
			self.stack['file'] = self.params['virtualdata']['filename']
		else:
			self.stack['file'] = os.path.join(self.stackdata['path']['path'], self.stackdata['name'])
		self.dumpParameters()

		self.params['canexe'] = self.getCANPath()

		### process stack to local file
		self.params['localstack'] = os.path.join(self.params['rundir'], self.timestamp+".hed")

		a = proc2dLib.RunProc2d()
		a.setValue('infile',self.stack['file'])
		a.setValue('outfile',self.params['localstack'])
		a.setValue('apix',self.stack['apix'])
		a.setValue('bin',self.params['bin'])
		a.setValue('last',self.params['numpart']-1)
		a.setValue('append',False)

		if self.params['lowpass'] is not None and self.params['lowpass'] > 1:
			a.setValue('lowpass',self.params['lowpass'])
		if self.params['highpass'] is not None and self.params['highpass'] > 1:
			a.setValue('highpass',self.params['highpass'])
		if self.params['invert'] is True:
			a.setValue('invert',True)
		if self.params['premask'] is True and self.params['mramethod'] != 'imagic':
			a.setValue('mask',self.params['mask'])

		if self.params['virtualdata'] is not None:
			vparts = self.params['virtualdata']['particles']
			plist = [int(p['particleNumber'])-1 for p in vparts]
			a.setValue('list',plist)

		if self.params['uploadonly'] is not True:
			if os.path.isfile(os.path.join(self.params['rundir'],"stack.hed")):
				self.params['localstack']=os.path.join(self.params['rundir'],"stack.hed")
			else:
				a.run()
			if self.params['numpart'] != apFile.numImagesInStack(self.params['localstack']):
				apDisplay.printError("Missing particles in stack")

			### IMAGIC mask particles before alignment
			if self.params['premask'] is True and self.params['mramethod'] == 'imagic':
				# convert mask to fraction for imagic
				maskfrac = self.workingmask*2/self.workingboxsize
				maskstack = imagicFilters.softMask(self.params['localstack'],mask=maskfrac)
				shutil.move(maskstack+".hed",os.path.splitext(self.params['localstack'])[0]+".hed")
				shutil.move(maskstack+".img",os.path.splitext(self.params['localstack'])[0]+".img")

		origstack = self.params['localstack']
		### find number of processors
#		if self.params['nproc'] is None:
		self.params['nproc'] = apParam.getNumProcessors()

		if self.params['uploadonly'] is not True:
			aligntime = time.time()
			# run through iterations
			for i in range(0,self.params['iter']+1):
				# move back to starting directory
				os.chdir(self.params['rundir'])

				# set up next iteration directory
				self.params['currentiter'] = i
				self.params['iterdir'] = os.path.abspath("iter%02i" % i)
				self.params['iterdir'] = string.replace(self.params['iterdir'],"/jetstor/APPION","")
				if os.path.exists(self.params['iterdir']):
					apDisplay.printError("Error: directory '%s' exists, aborting alignment" % self.params['iterdir'])

				# create directory for iteration
				os.makedirs(self.params['iterdir'])	
				os.chdir(self.params['iterdir'])

				# if at first iteration, create initial class averages 
				if i == 0:
					# first rewrite localstack headers if particles not pre-masked
					if self.params['premask'] is False and self.params['mramethod'] == "imagic":
						imagicFilters.takeoverHeaders(self.params['localstack'],self.params['numpart'],self.workingboxsize)
					self.params['alignedstack'] = os.path.splitext(self.params['localstack'])[0]
					if self.params['msamethod']=='imagic':
						self.runIMAGICmsa()
					else:
						self.runCAN()
					continue

				# using references from last iteration, run multi-ref alignment
				if self.params['mramethod'] == "imagic":
					# rewrite class headers
					imagicFilters.takeoverHeaders(self.params['currentcls'],self.params['currentnumclasses'],self.workingboxsize)
					self.runIMAGICmra()
				else:
					self.runEMANmra()

				# create class averages from aligned stack
				if self.params['msamethod']=='imagic':
					self.runIMAGICmsa()
				else:
					self.runCAN()
			
			aligntime = time.time() - aligntime
			apDisplay.printMsg("Alignment time: "+apDisplay.timeString(aligntime))

		## set upload information params:
		else:
			## get last iteration
			alliters = glob.glob("iter*")
			alliters.sort()

			## get iteration number from iter dir
			self.params['currentiter'] = int(alliters[-1][-2:])
			self.params['iterdir'] = os.path.join(self.params['rundir'],alliters[-1])
			self.params['currentcls'] = "classes%02i"%(self.params['currentiter'])

			## go into last iteration directory
			os.chdir(self.params['iterdir'])
			self.params['alignedstack'] = os.path.abspath("mrastack")
			if os.path.isfile(os.path.join(self.params['rundir'],self.params['currentcls']+".hed")):
				p1 = os.path.join(self.params['rundir'],self.params['currentcls'])
				p2 = os.path.join(self.params['iterdir'],self.params['currentcls'])
				shutil.move(p1+".hed",p2+".hed")
				shutil.move(p1+".img",p2+".img")

		## sort the class averages
		self.sortClassAverages()

		### get particle information from last iteration
		if self.params['mramethod']=='imagic':
			partlist = self.readPartIMAGICFile()
		else:
			partlist = self.readPartEMANFile()
		if self.params['msamethod']=='imagic':
			partrefdict = self.imagicClassificationToDict()
		else:
			partrefdict = self.canClassificationToDict()

		# move back to starting directory
		os.chdir(self.params['rundir'])

		# move aligned stack to current directory for appionweb
		if not os.path.isfile("mrastack.hed"):
			shutil.move(self.params['alignedstack']+".hed","mrastack.hed")
			shutil.move(self.params['alignedstack']+".img","mrastack.img")
			# rewrite header
			if self.params['mramethod'] == "imagic" or self.params['msamethod'] == 'imagic':
				imagicFilters.takeoverHeaders("mrastack",self.params['numpart'],self.workingboxsize)

		# move actual averages to current directory
		if self.params['msamethod']=='can':
			if not os.path.isfile("classes_avg.hed"):
				self.applySort()
			# save actual class averages as refs in database
			self.params['currentcls']="classes_avg"
		
		
		### create an average mrc of final references 
		if not os.path.isfile("average.mrc"):
			apStack.averageStack(stack=self.params['currentcls']+".hed")
			self.dumpParameters()

		### remove the filtered stack
		apFile.removeStack(origstack)

		### save to database
		if self.params['commit'] is True:
			self.insertRunIntoDatabase()
			self.insertParticlesIntoDatabase(partlist, partrefdict)