Example #1
0
    def start(self):

        ### some of this should go in preloop functions

        ###create param file
        param_out = self.params['seriesname'] + '.param'
        tiltfilename = self.params['seriesname'] + '.tlt'
        iters = self.params['iters']
        self.params['cos_alpha'] = math.cos(self.params['maxtilt'] * math.pi /
                                            180)
        self.params['raw_path'] = os.path.join(self.params['rundir'], 'raw')
        param_in = apProTomo2Prep.getPrototypeParamPath()
        paramdict = apProTomo2Prep.createParamDict(self.params)
        apProTomo2Prep.modifyParamFile(param_in, param_out, paramdict)
        seriesparam = protomo.param(param_out)

        apDisplay.printMsg('Starting protomo alignment')

        ###using presence of i3t to determine if protomo has been run once already

        #create series object
        i3tfile = self.params['seriesname'] + '.i3t'

        if os.path.exists(i3tfile):
            series = protomo.series(seriesparam)
        else:
            seriesgeom = protomo.geom(tiltfilename)
            series = protomo.series(seriesparam, seriesgeom)

        #figure out starting number
        start = 0
        previters = glob.glob(self.params['seriesname'] + '*.corr')
        if len(previters) > 0:
            previters.sort()
            lastiter = previters[-1]
            start = int(
                lastiter.split(self.params['seriesname'])[1].split('.')[0]) + 1

        for n in range(iters):
            series.align()
            basename = '%s%02d' % (self.params['seriesname'], (n + start))

            corrfile = basename + '.corr'
            series.corr(corrfile)
            series.fit()
            series.update()

            #archive results
            tiltfile = basename + '.tlt'
            geom = series.geom()
            geom.write(tiltfile)
        def start(self):
        
                ### some of this should go in preloop functions

                ###create param file
                param_out=self.params['seriesname']+'.param'
                tiltfilename=self.params['seriesname']+'.tlt'
                iters = self.params['iters']
                self.params['cos_alpha']=math.cos(self.params['maxtilt']*math.pi/180)
                self.params['raw_path']=os.path.join(self.params['rundir'],'raw')
                param_in=apProTomo2Prep.getPrototypeParamPath()
                paramdict = apProTomo2Prep.createParamDict(self.params)
                apProTomo2Prep.modifyParamFile(param_in, param_out, paramdict)
                seriesparam=protomo.param(param_out)

                apDisplay.printMsg('Starting protomo alignment')

                ###using presence of i3t to determine if protomo has been run once already

                #create series object
                i3tfile=self.params['seriesname']+'.i3t'
                
                if os.path.exists(i3tfile):
                        series=protomo.series(seriesparam)
                else:
                        seriesgeom=protomo.geom(tiltfilename)
                        series=protomo.series(seriesparam,seriesgeom)

                #figure out starting number
                start=0
                previters=glob.glob(self.params['seriesname']+'*.corr')
                if len(previters) > 0:
                        previters.sort()
                        lastiter=previters[-1]
                        start=int(lastiter.split(self.params['seriesname'])[1].split('.')[0])+1
                
                for n in range(iters):
                        series.align()
                        basename='%s%02d' % (self.params['seriesname'],(n+start))

                        corrfile=basename+'.corr'
                        series.corr(corrfile)
                        series.fit()
                        series.update()

                        #archive results
                        tiltfile=basename+'.tlt'
                        geom=series.geom()
                        geom.write(tiltfile)
Example #3
0
    def run(self):

        # use the tilt angles to set the reference image, which is the image with tilt values closest to zero.
        self.setRefImg(self.tilts)
        apDisplay.printMsg("Setting Reference image to: %d" % (self.refimg, ))

        # Write a default parameter file if it does not already exist
        self.params['protomo2paramfile'] = self.writeDefaultParamFile()
        apDisplay.printMsg("Setting Parameter file to: %s" %
                           (self.params['protomo2paramfile'], ))

        # Create the parameter object
        try:
            protomo2param = protomo.param(self.params['protomo2paramfile'])
        except Exception:
            msg = "The protomo2 config file (%s) has an error. Could not create the parameter object." % (
                self.params['protomo2paramfile'], )
            apDisplay.printWarning(msg)
            raise

        # Write a default tilt geometry file if it does not already exist
        geomParamFilePath = self.writeTiltGeomFile()
        apDisplay.printMsg("Setting tilt geometry file to: %s" %
                           (geomParamFilePath, ))

        # Create the protomo2 geometry object
        try:
            protomo2geom = protomo.geom(geomParamFilePath)
        except:
            apDisplay.printWarning(
                "Failed to create the protomo2 geometry object with the geometry parameter file, %s"
                % (geomParamFilePath, ))
            raise

        # Modify the parameter object with the user input values
        self.setUserModifiedParams(protomo2param)

        # Create a tilt series object (contains input geometry, creates updated geometry, info to do the alignment)
        try:
            protomo2series = protomo.series(protomo2param, protomo2geom)
        except:
            apDisplay.printWarning(
                "Failed to create the protomo2 tilt series object.")
            raise

        # Run the specified number of alignment iterations
        for i in range(0, self.params['max_iterations']):
            protomo2series.align()
            # correction factors, automatically generates a file name using the iteration number(? TODO: Hanspeter to implement)
            protomo2series.corr()  #( self.correctionFactorFile )
            protomo2series.fit()
            protomo2series.update()
	def run(self):
		
		# use the tilt angles to set the reference image, which is the image with tilt values closest to zero.
		self.setRefImg(self.tilts)
		apDisplay.printMsg("Setting Reference image to: %d" % (self.refimg, ))
		
		
		# Write a default parameter file if it does not already exist
		self.params['protomo2paramfile'] = self.writeDefaultParamFile()
		apDisplay.printMsg("Setting Parameter file to: %s" % (self.params['protomo2paramfile'], ))
		
		# Create the parameter object
		try:
			protomo2param = protomo.param( self.params['protomo2paramfile'] )
		except Exception:
			msg = "The protomo2 config file (%s) has an error. Could not create the parameter object." % (self.params['protomo2paramfile'], )
			apDisplay.printWarning(msg)
			raise
				
		
		# Write a default tilt geometry file if it does not already exist 
		geomParamFilePath = self.writeTiltGeomFile()
		apDisplay.printMsg("Setting tilt geometry file to: %s" % (geomParamFilePath, ))
		
		# Create the protomo2 geometry object
		try:
			protomo2geom = protomo.geom( geomParamFilePath )
		except:
			apDisplay.printWarning("Failed to create the protomo2 geometry object with the geometry parameter file, %s" % (geomParamFilePath, ))
			raise
			

		# Modify the parameter object with the user input values
		self.setUserModifiedParams( protomo2param )
		

		# Create a tilt series object (contains input geometry, creates updated geometry, info to do the alignment)
		try:
			protomo2series = protomo.series(protomo2param, protomo2geom)
		except:
			apDisplay.printWarning("Failed to create the protomo2 tilt series object.")
			raise
		

		# Run the specified number of alignment iterations
		for i in range(0, self.params['max_iterations']):
			protomo2series.align()
			# correction factors, automatically generates a file name using the iteration number(? TODO: Hanspeter to implement)
			protomo2series.corr() #( self.correctionFactorFile )
			protomo2series.fit()
			protomo2series.update()
import sys
import glob
import os


def parseOptions():
    parser = optparse.OptionParser()
    parser.add_option('--param', dest='param', help='Param file')
    parser.add_option('--map',
                      dest='map',
                      help='Output map name, e.g. series1.img')

    options, args = parser.parse_args()

    if len(args) != 0 or len(sys.argv) == 1:
        parser.print_help()
        sys.exit()

    return options


if __name__ == '__main__':

    options = parseOptions()

    param = protomo.param(options.param)
    series = protomo.series(param)
    series.mapfile(options.map)

    print "Done!"
import protomo
import optparse
import sys
import glob
import os

def parseOptions():
	parser=optparse.OptionParser()
	parser.add_option('--param', dest='param', help= 'Param file')
	parser.add_option('--map', dest='map', help= 'Output map name, e.g. series1.img')

	options, args=parser.parse_args()
	
	if len(args) != 0 or len(sys.argv) == 1:
		parser.print_help()
		sys.exit()
	
	return options


if __name__=='__main__':

	options=parseOptions()
	
	param=protomo.param(options.param)
	series=protomo.series(param)
	series.mapfile(options.map)
	
	print "Done!"
	def start(self):
	
		it="%03d" % (self.params['recon_iter'])
		itt="%03d" % (self.params['recon_iter']-1)
		global wd
		wd=self.params['rundir']
		raw_path=os.path.join(wd, 'raw')
		os.chdir(self.params['rundir'])
		seriesnumber = "%04d" % int(self.params['tiltseries'])
		seriesname='series'+seriesnumber
		param_out=seriesname+'.param'
		param_out_full=self.params['rundir']+'/'+param_out
		tilt_out_full=self.params['rundir']+'/'+seriesname+itt+'.tlt'
		recon_dir=self.params['rundir']+'/recons/'
		os.system('mkdir %s 2>/dev/null' % recon_dir)
		recon_param_out_full=recon_dir+'/'+param_out
		recon_tilt_out_full=recon_dir+'/'+seriesname+'.tlt'
		recon_cache_dir=recon_dir+'/cache'
		recon_out_dir=recon_dir+'out'
		os.system('cp %s %s' % (tilt_out_full, recon_tilt_out_full))
		os.system('cp %s %s' % (param_out_full,recon_param_out_full))
		
		#CTF Correction
		if (self.params['ctf_correct'] == 'True'):
			apProTomo2Prep.ctfCorrect(seriesname, self.params['rundir'], self.params['projectid'], self.params['sessionname'], int(self.params['tiltseries']), recon_tilt_out_full, self.params['frame_aligned'], self.params['pixelsize'], self.params['DefocusTol'], self.params['iWidth'], self.params['amp_contrast'])
		
		#Dose Compensation
		if (self.params['dose_presets'] != 'False'):
			apProTomo2Prep.doseCompensate(seriesname, self.params['rundir'], self.params['sessionname'], int(self.params['tiltseries']), self.params['frame_aligned'], raw_path, self.params['pixelsize'], self.params['dose_presets'], self.params['dose_a'], self.params['dose_b'], self.params['dose_c'])
		
		# Remove high tilts from .tlt file if user requests
		if (self.params['positive'] < 90) or (self.params['negative'] > -90):
			removed_images, mintilt, maxtilt = apProTomo2Aligner.removeHighTiltsFromTiltFile(recon_tilt_out_full, self.params['negative'], self.params['positive'])
			apDisplay.printMsg("Images %s have been removed before reconstruction by weighted back-projection" % removed_images)
		else:
			cmd1="awk '/ORIGIN /{print}' %s | wc -l" % (recon_tilt_out_full)
			proc=subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
			(numimages, err) = proc.communicate()
			numimages=int(numimages)
			cmd2="awk '/IMAGE /{print $2}' %s | head -n +1" % (recon_tilt_out_full)
			proc=subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)
			(tiltstart, err) = proc.communicate()
			tiltstart=int(tiltstart)
			mintilt=0
			maxtilt=0
			for i in range(tiltstart-1,tiltstart+numimages):
				try: #If the image isn't in the .tlt file, skip it
					cmd="awk '/IMAGE %s /{print}' %s | awk '{for (j=1;j<=NF;j++) if($j ~/TILT/) print $(j+2)}'" % (i+1, recon_tilt_out_full)
					proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
					(tilt_angle, err) = proc.communicate()
					tilt_angle=float(tilt_angle)
					mintilt=min(mintilt,tilt_angle)
					maxtilt=max(maxtilt,tilt_angle)
				except:
					pass
		
		# Backup then edit the Refinement param file, changing the map size, map sampling, cache dir, and out dir
		refine_param_full=self.params['rundir']+'/'+'refine_'+param_out
		command="cp %s %s" % (param_out_full, refine_param_full)
		os.system(command)
		command1="grep -n 'AP sampling' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(command1, stdout=subprocess.PIPE, shell=True)
		(samplingline, err) = proc.communicate()
		samplingline=int(samplingline)
		command2="grep -n 'AP reconstruction map size' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(command2, stdout=subprocess.PIPE, shell=True)
		(mapsizeline, err) = proc.communicate()
		mapsizeline=int(mapsizeline)
		command3="grep -n 'AP reconstruction map sampling' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(command3, stdout=subprocess.PIPE, shell=True)
		(mapsamplingline, err) = proc.communicate()
		mapsamplingline=int(mapsamplingline)
		command11="sed -i \"%ss/.*/ S = %s             (* AP sampling *)/\" %s" % (samplingline, self.params['recon_map_sampling'], recon_param_out_full)
		os.system(command11)
		command22="sed -i \"%ss/.*/   size: { %s, %s, %s }  (* AP reconstruction map size *)/\" %s" % (mapsizeline, int(self.params['recon_map_size_x']/self.params['recon_map_sampling']), int(self.params['recon_map_size_y']/self.params['recon_map_sampling']), int(round(self.params['recon_thickness']/(self.params['pixelsize']*self.params['recon_map_sampling']))), recon_param_out_full)
		os.system(command22)
		command33="sed -i \"%ss/.*/   sampling: %s  (* AP reconstruction map sampling *)/\" %s" % (mapsamplingline, self.params['recon_map_sampling'], recon_param_out_full)
		os.system(command33)
		command4="grep -n 'cachedir' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(command4, stdout=subprocess.PIPE, shell=True)
		(cachedirline, err) = proc.communicate()
		cachedirline=int(cachedirline)
		command5="grep -n 'outdir' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(command5, stdout=subprocess.PIPE, shell=True)
		(outdirline, err) = proc.communicate()
		outdirline=int(outdirline)
		command44="sed -i \'%ss|.*| cachedir: \"%s\"  (* AP directory where cache files are stored *)|\' %s" % (cachedirline, recon_cache_dir, recon_param_out_full)
		os.system(command44)
		command55="sed -i \'%ss|.*| outdir: \"%s\"  (* AP directory where other output files are stored *)|\' %s" % (outdirline, recon_out_dir, recon_param_out_full)
		os.system(command55)
		
		#Lowpass filter reconstruction?
		cmd="grep -n 'AP lowpass map' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
		proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
		(lowpassmapline, err) = proc.communicate()
		lowpassmapline=int(lowpassmapline)
		if self.params['recon_lowpass'] == "False":
			lp=''
			#Remove lowpass filter from param
			cmd2="sed -i \"%ss/.*//\" %s;" % (lowpassmapline, recon_param_out_full)
			cmd2+="sed -i \"%ss/.*//\" %s;" % (lowpassmapline+1, recon_param_out_full)
			cmd2+="sed -i \"%ss/.*//\" %s;" % (lowpassmapline+2, recon_param_out_full)
			cmd2+="sed -i \"%ss/.*//\" %s" % (lowpassmapline+3, recon_param_out_full)
			
			#Set preprocessing to false
			cmd3="grep -n 'AP enable or disable preprocessing of raw images' %s | awk '{print $1}' | sed 's/://'" % (recon_param_out_full)
			proc=subprocess.Popen(cmd3, stdout=subprocess.PIPE, shell=True)
			(preprocessingline, err) = proc.communicate()
			preprocessingline=int(preprocessingline)
			
			cmd33="sed -i \'%ss|.*| preprocessing: false  (* AP enable or disable preprocessing of raw images *)|\' %s" % (preprocessingline, recon_param_out_full)
			os.system(cmd33)
		else:
			lpavg = int((self.params['recon_lp_diam_x']+self.params['recon_lp_diam_y'])/2)
			lp='.lp%s' % lpavg
			self.params['recon_lp_diam_x'] = 2*self.params['pixelsize']*self.params['recon_map_sampling']/self.params['recon_lp_diam_x']
			self.params['recon_lp_diam_y'] = 2*self.params['pixelsize']*self.params['recon_map_sampling']/self.params['recon_lp_diam_y']
			cmd2="sed -i \"%ss/.*/     diameter:    { %s, %s } * S/\" %s" % (lowpassmapline+1, self.params['recon_lp_diam_x'], self.params['recon_lp_diam_y'], recon_param_out_full)
		os.system(cmd2)
		
		dim='%sx%s' % (self.params['recon_map_size_x'],self.params['recon_map_size_y'])
		ang='%sto%s' % (round(mintilt,1),round(maxtilt,1))
		img=seriesname+'00_bck.img'
		mrcf=seriesname+'_ite'+it+'_dim'+dim+'_ang'+ang+'_bck.bin'+str(self.params['recon_map_sampling'])+lp+'.mrc'
		mrcfn=seriesname+'_ite'+it+'_dim'+dim+'_ang'+ang+'_bck.bin'+str(self.params['recon_map_sampling'])+lp+'.norm.mrc'
		img_full=recon_out_dir+'/'+img
		mrc_full=recon_out_dir+'/'+mrcf
		mrcn_full=recon_out_dir+'/'+mrcfn
		
		# AP Get param files ready for batch processing
		batchdir=self.params['rundir']+'/'+'ready_for_batch'
		coarse_param_full=self.params['rundir']+'/coarse_out/'+'coarse_'+param_out
		recon_param='recon_'+param_out
		command="mkdir %s 2>/dev/null; cp %s %s; cp %s %s; cp %s %s/%s; rm -r %s %s %s*i3t %s/cache %s/out 2>/dev/null" % (batchdir, coarse_param_full, batchdir, refine_param_full, batchdir, param_out_full, batchdir, recon_param, img_full, mrc_full, recon_dir, recon_dir, recon_dir)
		os.system(command)
		
		# Create reconstruction
		os.chdir(recon_dir)
		os.system("rm %s/cache/%s* %s/*i3t 2>/dev/null" % (recon_dir, seriesname, recon_dir))
		seriesparam=protomo.param(recon_param_out_full)
		seriesgeom=protomo.geom(recon_tilt_out_full)
		series=protomo.series(seriesparam,seriesgeom)
		series.setparam("map.logging","true")
		series.mapfile()
		os.chdir(self.params['rundir'])
		
		# Restore refine param file
		command="cp %s %s" % (refine_param_full, param_out_full)
		os.system(command)
		
		# Convert to mrc
		os.system("i3cut -fmt mrc %s %s" % (img_full, mrc_full))
		os.system("rm %s" % img_full)
		
		# Normalize
		try:
			command="e2proc3d.py %s %s --process=normalize" % (mrc_full, mrcn_full)
			proc=subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
			(code, err) = proc.communicate()
		except:
			pass
		
		if proc.returncode != 0:
			apDisplay.printMsg("\ne2proc3d not found or failed to process reconstruction. Trying proc3d...")
			try:
				command="proc3d %s %s norm" % (mrc_full, mrcn_full)
				proc=subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
				(code, err) = proc.communicate()
			except:
				pass
		if proc.returncode != 0:
			apDisplay.printMsg("\nproc3d not found or failed to process reconstruction. Skipping normalization.")
		else:
			os.system('rm %s' % mrc_full)
		
		# Link reconstruction to directory
		try:
			cmd1="mkdir -p %s 2>/dev/null; rm %s/%s %s/%s 2>/dev/null; ln -f %s %s 2>/dev/null" % (self.params['link_recon'], self.params['link_recon'], mrcf, self.params['link_recon'], mrcfn, mrc_full, self.params['link_recon'])
			cmd2="mkdir -p %s 2>/dev/null; rm %s/%s %s/%s 2>/dev/null; ln -f %s %s" % (self.params['link_recon'], self.params['link_recon'], mrcf, self.params['link_recon'], mrcfn, mrcn_full, self.params['link_recon'])
			try:
				proc=subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
				(code, err) = proc.communicate()
			except:
				pass
			if proc.returncode != 0:
				try:
					proc=subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)
					(code, err) = proc.communicate()
				except:
					pass
			
			apDisplay.printMsg("Reconstruction can be found in this directory:")
			if (self.params['link_recon'] == None) or (self.params['link_recon'] == "") or (len(self.params['link_recon']) < 1):
				print "\n%s\n" % (recon_out_dir)
			else:
				print "\n%s\n" % (self.params['link_recon'])
		except:
			apDisplay.printMsg("Reconstruction can be found in this directory:")
			print "\n%s\n" % (recon_out_dir)
			if proc.returncode != 0:
				apDisplay.printMsg("The reconstruction in the above directory is not normalized because EMAN1 and EMAN2 were either not found or failed to process the reconstruction.")
			
		os.system("rm %s/cache/%s* %s/*i3t" % (recon_dir, seriesname, recon_dir))
	
	options, args=parser.parse_args()

	if len(args) != 0 or len(sys.argv) == 1:
		parser.print_help()
		sys.exit()
	
	return options
	
	
if __name__ == "__main__":
	options=parseOptions()
	
	seriesparam=protomo.param(options.param)	
	if options.i3tfile:
		series=protomo.series( seriesparam )
	else:
		seriesgeom=protomo.geom( options.tlt )
		options.i3tfile=options.tlt
		series=protomo.series( seriesparam, seriesgeom )

	seriesname=options.i3tfile.split('.')[0]
	iters=options.iters
	
	#figure out starting number
	start=0
	previters=glob.glob(seriesname+'*.corr')
	if len(previters) > 0:
		previters.sort()
		lastiter=previters[-1]
		start=int(lastiter.split(seriesname)[1].split('.')[0])+1
    def start(self):

        it = "%03d" % (self.params['recon_iter'])
        itt = "%03d" % (self.params['recon_iter'] - 1)
        global wd
        wd = self.params['rundir']
        raw_path = os.path.join(wd, 'raw')
        os.chdir(self.params['rundir'])
        seriesnumber = "%04d" % int(self.params['tiltseries'])
        seriesname = 'series' + seriesnumber
        param_out = seriesname + '.param'
        param_out_full = self.params['rundir'] + '/' + param_out
        tilt_out_full = self.params['rundir'] + '/' + seriesname + itt + '.tlt'
        recon_dir = self.params['rundir'] + '/recons/'
        os.system('mkdir %s 2>/dev/null' % recon_dir)
        recon_param_out_full = recon_dir + '/' + param_out
        recon_tilt_out_full = recon_dir + '/' + seriesname + '.tlt'
        recon_cache_dir = recon_dir + '/cache'
        recon_out_dir = recon_dir + 'out'
        os.system('cp %s %s' % (tilt_out_full, recon_tilt_out_full))
        os.system('cp %s %s' % (param_out_full, recon_param_out_full))

        #CTF Correction
        if (self.params['ctf_correct'] == 'True'):
            apProTomo2Prep.ctfCorrect(
                seriesname, self.params['rundir'],
                self.params['projectid'], self.params['sessionname'],
                int(self.params['tiltseries']), recon_tilt_out_full,
                self.params['frame_aligned'], self.params['pixelsize'],
                self.params['DefocusTol'], self.params['iWidth'],
                self.params['amp_contrast'])

        #Dose Compensation
        if (self.params['dose_presets'] != 'False'):
            apProTomo2Prep.doseCompensate(
                seriesname, self.params['rundir'], self.params['sessionname'],
                int(self.params['tiltseries']), self.params['frame_aligned'],
                raw_path, self.params['pixelsize'],
                self.params['dose_presets'], self.params['dose_a'],
                self.params['dose_b'], self.params['dose_c'])

        # Remove high tilts from .tlt file if user requests
        if (self.params['positive'] < 90) or (self.params['negative'] > -90):
            removed_images, mintilt, maxtilt = apProTomo2Aligner.removeHighTiltsFromTiltFile(
                recon_tilt_out_full, self.params['negative'],
                self.params['positive'])
            apDisplay.printMsg(
                "Images %s have been removed before reconstruction by weighted back-projection"
                % removed_images)
        else:
            cmd1 = "awk '/ORIGIN /{print}' %s | wc -l" % (recon_tilt_out_full)
            proc = subprocess.Popen(cmd1, stdout=subprocess.PIPE, shell=True)
            (numimages, err) = proc.communicate()
            numimages = int(numimages)
            cmd2 = "awk '/IMAGE /{print $2}' %s | head -n +1" % (
                recon_tilt_out_full)
            proc = subprocess.Popen(cmd2, stdout=subprocess.PIPE, shell=True)
            (tiltstart, err) = proc.communicate()
            tiltstart = int(tiltstart)
            mintilt = 0
            maxtilt = 0
            for i in range(tiltstart - 1, tiltstart + numimages):
                try:  #If the image isn't in the .tlt file, skip it
                    cmd = "awk '/IMAGE %s /{print}' %s | awk '{for (j=1;j<=NF;j++) if($j ~/TILT/) print $(j+2)}'" % (
                        i + 1, recon_tilt_out_full)
                    proc = subprocess.Popen(cmd,
                                            stdout=subprocess.PIPE,
                                            shell=True)
                    (tilt_angle, err) = proc.communicate()
                    tilt_angle = float(tilt_angle)
                    mintilt = min(mintilt, tilt_angle)
                    maxtilt = max(maxtilt, tilt_angle)
                except:
                    pass

        # Backup then edit the Refinement param file, changing the map size, map sampling, cache dir, and out dir
        refine_param_full = self.params['rundir'] + '/' + 'refine_' + param_out
        command = "cp %s %s" % (param_out_full, refine_param_full)
        os.system(command)
        command1 = "grep -n 'AP sampling' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(command1, stdout=subprocess.PIPE, shell=True)
        (samplingline, err) = proc.communicate()
        samplingline = int(samplingline)
        command2 = "grep -n 'AP reconstruction map size' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(command2, stdout=subprocess.PIPE, shell=True)
        (mapsizeline, err) = proc.communicate()
        mapsizeline = int(mapsizeline)
        command3 = "grep -n 'AP reconstruction map sampling' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(command3, stdout=subprocess.PIPE, shell=True)
        (mapsamplingline, err) = proc.communicate()
        mapsamplingline = int(mapsamplingline)
        command11 = "sed -i \"%ss/.*/ S = %s             (* AP sampling *)/\" %s" % (
            samplingline, self.params['recon_map_sampling'],
            recon_param_out_full)
        os.system(command11)
        command22 = "sed -i \"%ss/.*/   size: { %s, %s, %s }  (* AP reconstruction map size *)/\" %s" % (
            mapsizeline,
            int(self.params['recon_map_size_x'] /
                self.params['recon_map_sampling']),
            int(self.params['recon_map_size_y'] /
                self.params['recon_map_sampling']),
            int(
                round(self.params['recon_thickness'] /
                      (self.params['pixelsize'] *
                       self.params['recon_map_sampling']))),
            recon_param_out_full)
        os.system(command22)
        command33 = "sed -i \"%ss/.*/   sampling: %s  (* AP reconstruction map sampling *)/\" %s" % (
            mapsamplingline, self.params['recon_map_sampling'],
            recon_param_out_full)
        os.system(command33)
        command4 = "grep -n 'cachedir' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(command4, stdout=subprocess.PIPE, shell=True)
        (cachedirline, err) = proc.communicate()
        cachedirline = int(cachedirline)
        command5 = "grep -n 'outdir' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(command5, stdout=subprocess.PIPE, shell=True)
        (outdirline, err) = proc.communicate()
        outdirline = int(outdirline)
        command44 = "sed -i \'%ss|.*| cachedir: \"%s\"  (* AP directory where cache files are stored *)|\' %s" % (
            cachedirline, recon_cache_dir, recon_param_out_full)
        os.system(command44)
        command55 = "sed -i \'%ss|.*| outdir: \"%s\"  (* AP directory where other output files are stored *)|\' %s" % (
            outdirline, recon_out_dir, recon_param_out_full)
        os.system(command55)

        #Lowpass filter reconstruction?
        cmd = "grep -n 'AP lowpass map' %s | awk '{print $1}' | sed 's/://'" % (
            recon_param_out_full)
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        (lowpassmapline, err) = proc.communicate()
        lowpassmapline = int(lowpassmapline)
        if self.params['recon_lowpass'] == "False":
            lp = ''
            #Remove lowpass filter from param
            cmd2 = "sed -i \"%ss/.*//\" %s;" % (lowpassmapline,
                                                recon_param_out_full)
            cmd2 += "sed -i \"%ss/.*//\" %s;" % (lowpassmapline + 1,
                                                 recon_param_out_full)
            cmd2 += "sed -i \"%ss/.*//\" %s;" % (lowpassmapline + 2,
                                                 recon_param_out_full)
            cmd2 += "sed -i \"%ss/.*//\" %s" % (lowpassmapline + 3,
                                                recon_param_out_full)

            #Set preprocessing to false
            cmd3 = "grep -n 'AP enable or disable preprocessing of raw images' %s | awk '{print $1}' | sed 's/://'" % (
                recon_param_out_full)
            proc = subprocess.Popen(cmd3, stdout=subprocess.PIPE, shell=True)
            (preprocessingline, err) = proc.communicate()
            preprocessingline = int(preprocessingline)

            cmd33 = "sed -i \'%ss|.*| preprocessing: false  (* AP enable or disable preprocessing of raw images *)|\' %s" % (
                preprocessingline, recon_param_out_full)
            os.system(cmd33)
        else:
            lpavg = int((self.params['recon_lp_diam_x'] +
                         self.params['recon_lp_diam_y']) / 2)
            lp = '.lp%s' % lpavg
            self.params['recon_lp_diam_x'] = 2 * self.params[
                'pixelsize'] * self.params['recon_map_sampling'] / self.params[
                    'recon_lp_diam_x']
            self.params['recon_lp_diam_y'] = 2 * self.params[
                'pixelsize'] * self.params['recon_map_sampling'] / self.params[
                    'recon_lp_diam_y']
            cmd2 = "sed -i \"%ss/.*/     diameter:    { %s, %s } * S/\" %s" % (
                lowpassmapline + 1, self.params['recon_lp_diam_x'],
                self.params['recon_lp_diam_y'], recon_param_out_full)
        os.system(cmd2)

        dim = '%sx%s' % (self.params['recon_map_size_x'],
                         self.params['recon_map_size_y'])
        ang = '%sto%s' % (round(mintilt, 1), round(maxtilt, 1))
        img = seriesname + '00_bck.img'
        mrcf = seriesname + '_ite' + it + '_dim' + dim + '_ang' + ang + '_bck.bin' + str(
            self.params['recon_map_sampling']) + lp + '.mrc'
        mrcfn = seriesname + '_ite' + it + '_dim' + dim + '_ang' + ang + '_bck.bin' + str(
            self.params['recon_map_sampling']) + lp + '.norm.mrc'
        img_full = recon_out_dir + '/' + img
        mrc_full = recon_out_dir + '/' + mrcf
        mrcn_full = recon_out_dir + '/' + mrcfn

        # AP Get param files ready for batch processing
        batchdir = self.params['rundir'] + '/' + 'ready_for_batch'
        coarse_param_full = self.params[
            'rundir'] + '/coarse_out/' + 'coarse_' + param_out
        recon_param = 'recon_' + param_out
        command = "mkdir %s 2>/dev/null; cp %s %s; cp %s %s; cp %s %s/%s; rm -r %s %s %s*i3t %s/cache %s/out 2>/dev/null" % (
            batchdir, coarse_param_full, batchdir, refine_param_full, batchdir,
            param_out_full, batchdir, recon_param, img_full, mrc_full,
            recon_dir, recon_dir, recon_dir)
        os.system(command)

        # Create reconstruction
        os.chdir(recon_dir)
        os.system("rm %s/cache/%s* %s/*i3t 2>/dev/null" %
                  (recon_dir, seriesname, recon_dir))
        seriesparam = protomo.param(recon_param_out_full)
        seriesgeom = protomo.geom(recon_tilt_out_full)
        series = protomo.series(seriesparam, seriesgeom)
        series.setparam("map.logging", "true")
        series.mapfile()
        os.chdir(self.params['rundir'])

        # Restore refine param file
        command = "cp %s %s" % (refine_param_full, param_out_full)
        os.system(command)

        # Convert to mrc
        os.system("i3cut -fmt mrc %s %s" % (img_full, mrc_full))
        os.system("rm %s" % img_full)

        # Normalize
        try:
            command = "e2proc3d.py %s %s --process=normalize" % (mrc_full,
                                                                 mrcn_full)
            proc = subprocess.Popen(command,
                                    stdout=subprocess.PIPE,
                                    shell=True)
            (code, err) = proc.communicate()
        except:
            pass

        if proc.returncode != 0:
            apDisplay.printMsg(
                "\ne2proc3d not found or failed to process reconstruction. Trying proc3d..."
            )
            try:
                command = "proc3d %s %s norm" % (mrc_full, mrcn_full)
                proc = subprocess.Popen(command,
                                        stdout=subprocess.PIPE,
                                        shell=True)
                (code, err) = proc.communicate()
            except:
                pass
        if proc.returncode != 0:
            apDisplay.printMsg(
                "\nproc3d not found or failed to process reconstruction. Skipping normalization."
            )
        else:
            os.system('rm %s' % mrc_full)

        # Link reconstruction to directory
        try:
            cmd1 = "mkdir -p %s 2>/dev/null; rm %s/%s %s/%s 2>/dev/null; ln -f %s %s 2>/dev/null" % (
                self.params['link_recon'], self.params['link_recon'], mrcf,
                self.params['link_recon'], mrcfn, mrc_full,
                self.params['link_recon'])
            cmd2 = "mkdir -p %s 2>/dev/null; rm %s/%s %s/%s 2>/dev/null; ln -f %s %s" % (
                self.params['link_recon'], self.params['link_recon'], mrcf,
                self.params['link_recon'], mrcfn, mrcn_full,
                self.params['link_recon'])
            try:
                proc = subprocess.Popen(cmd1,
                                        stdout=subprocess.PIPE,
                                        shell=True)
                (code, err) = proc.communicate()
            except:
                pass
            if proc.returncode != 0:
                try:
                    proc = subprocess.Popen(cmd2,
                                            stdout=subprocess.PIPE,
                                            shell=True)
                    (code, err) = proc.communicate()
                except:
                    pass

            apDisplay.printMsg(
                "Reconstruction can be found in this directory:")
            if (self.params['link_recon']
                    == None) or (self.params['link_recon'] == "") or (len(
                        self.params['link_recon']) < 1):
                print "\n%s\n" % (recon_out_dir)
            else:
                print "\n%s\n" % (self.params['link_recon'])
        except:
            apDisplay.printMsg(
                "Reconstruction can be found in this directory:")
            print "\n%s\n" % (recon_out_dir)
            if proc.returncode != 0:
                apDisplay.printMsg(
                    "The reconstruction in the above directory is not normalized because EMAN1 and EMAN2 were either not found or failed to process the reconstruction."
                )

        os.system("rm %s/cache/%s* %s/*i3t" %
                  (recon_dir, seriesname, recon_dir))
Example #10
0
    options, args = parser.parse_args()

    if len(args) != 0 or len(sys.argv) == 1:
        parser.print_help()
        sys.exit()

    return options


if __name__ == "__main__":
    options = parseOptions()

    seriesparam = protomo.param(options.param)
    if options.i3tfile:
        series = protomo.series(seriesparam)
    else:
        seriesgeom = protomo.geom(options.tlt)
        options.i3tfile = options.tlt
        series = protomo.series(seriesparam, seriesgeom)

    seriesname = options.i3tfile.split('.')[0]
    iters = options.iters

    #figure out starting number
    start = 0
    previters = glob.glob(seriesname + '*.corr')
    if len(previters) > 0:
        previters.sort()
        lastiter = previters[-1]
        start = int(lastiter.split(seriesname)[1].split('.')[0]) + 1