def checkNumberOfParticles(self):
        #determine number of particle in input stack
        header = mrc.readHeaderFromFile(self.splitStacks['right'])
        #print header['shape']
        rightnumpart = header['shape'][0] - 1
        rightboxsize = max(header['shape'][1], header['shape'][2])
        apDisplay.printColor(
            "Found %d particles in the right stack file with box size %d x %d"
            % (rightnumpart, header['shape'][1], header['shape'][2]), "green")
        #determine number of particle in input stack
        header = mrc.readHeaderFromFile(self.splitStacks['left'])
        #print header['shape']
        leftnumpart = header['shape'][0] - 1
        leftboxsize = max(header['shape'][1], header['shape'][2])
        apDisplay.printColor(
            "Found %d particles in the left stack file with box size %d x %d" %
            (leftnumpart, header['shape'][1], header['shape'][2]), "green")

        if leftboxsize != rightboxsize:
            apDisplay.printError("particles have different box size")
        self.boxsize = leftboxsize

        if leftnumpart != rightnumpart:
            apDisplay.printWarning("particles have different count")
        self.numpart = min(leftnumpart, rightnumpart)

        return
	def checkNumberOfParticles(self):
		#determine number of particle in input stack
		header = mrc.readHeaderFromFile(self.splitStacks['right'])
		#print header['shape']
		rightnumpart = header['shape'][0]-1
		rightboxsize = max(header['shape'][1], header['shape'][2])
		apDisplay.printColor("Found %d particles in the right stack file with box size %d x %d"
			%(rightnumpart, header['shape'][1], header['shape'][2]), "green")
		#determine number of particle in input stack
		header = mrc.readHeaderFromFile(self.splitStacks['left'])
		#print header['shape']
		leftnumpart = header['shape'][0]-1
		leftboxsize = max(header['shape'][1], header['shape'][2])
		apDisplay.printColor("Found %d particles in the left stack file with box size %d x %d"
			%(leftnumpart, header['shape'][1], header['shape'][2]), "green")
			
		if leftboxsize != rightboxsize:
			apDisplay.printError("particles have different box size")
		self.boxsize = leftboxsize
			
		if leftnumpart != rightnumpart:
			apDisplay.printWarning("particles have different count")
		self.numpart = min(leftnumpart, rightnumpart)
			
		return
def getSubTomoBoundary(processdir,seriesname,axis):
	'''
	Get the start and end coordinate at a particular axis.
	The definition of origin may or may not apply to tomograms
	generated outside imod.
	'''
	fulltomopath = os.path.join(processdir,seriesname+'_full.rec')
	subtomopath = os.path.join(processdir,seriesname+'.rec')
	fullheader = mrc.readHeaderFromFile(fulltomopath)
	subheader = mrc.readHeaderFromFile(subtomopath)

	lenkey = axis+'len'
	originkey = axis+'origin'
	mkey = 'm'+axis
	# There is a rotation around x-axis in full tomogram
	fullorigin = {'xorigin':fullheader['xorigin'],'yorigin':fullheader['zorigin'],'zorigin':fullheader['yorigin']}
	fullm = {'mx':fullheader['mx'],'my':fullheader['mz'],'mz':fullheader['my']}
	# full and sub tomo should have the same pixelsize
	pixelsize = subheader[lenkey] / subheader[mkey]
	if axis != 'z':
		start = int((fullorigin[originkey] - subheader[originkey]) / pixelsize)
	else:
		# z origin is defined different from x and y
		start = -int((fullorigin[originkey] + subheader[originkey]) / pixelsize) + int(fullm[mkey])
	end = start + int(subheader[mkey])
	return (start,end)
def shiftToCenter(infile,shiftfile,isEMAN=False):
	'''
	EMAN defines the rotation origin differently from other packages.
	Therefore, it needs to be recenterred according to the package
	after using EMAN proc3d rotation functions.
	'''
	# center of rotation for eman is not at length/2.
	if isEMAN:
		formatoffset = getEmanCenter()
		prefix = ''
	else:
		formatoffset = (0,0,0)
		prefix = 'non-'

	apDisplay.printMsg('Shifting map center for %sEMAN usage' % (prefix,))
	# Find center of mass of the density map
	a = mrc.read(infile)
	t = a.mean()+2*a.std()
	numpy.putmask(a,a>=t,t)
	numpy.putmask(a,a<t,0)
	center = ndimage.center_of_mass(a)
	offset = (center[0]+formatoffset[0]-a.shape[0]/2,center[1]+formatoffset[1]-a.shape[1]/2,center[2]+formatoffset[2]-a.shape[2]/2)
	offset = (-offset[0],-offset[1],-offset[2])
	apDisplay.printMsg('Shifting map center by (x,y,z)=(%.2f,%.2f,%.2f)' % (offset[2],offset[1],offset[0]))
	# shift the map
	a = mrc.read(infile)
	a = ndimage.interpolation.shift(a,offset)
	mrc.write(a,shiftfile)
	h = mrc.readHeaderFromFile(infile)
	mrc.update_file_header(shiftfile,h)
 def checkExistingFile(self):
         savedtomopath = os.path.join(self.params['rundir'], self.params['name']+".rec")
         origtomopath = self.params['file']
         apDisplay.printWarning("A Tomogram by the same filename already exists: '"+savedtomopath+"'")
         ### a tomogram by the same name already exists
         savedheader = mrc.readHeaderFromFile(savedtomopath)
         savedshape = savedheader['shape']
         origheader = self.origheader
         origshape = self.origshape
         order = self.params['order']
         if self.params['full']:
                 newshape = (self.imageshape[0], origshape[order.find('Z')], self.imageshape[1])
         else:
                 newshape = (origshape[order.find('X')], origshape[order.find('Y')], origshape[order.find('Z')])
         # not using md5 for file comparison because it takes too long.  With padding, only min and max are stable
         if newshape != savedshape or savedheader['amax'] != origheader['amax'] or savedheader['amin'] != origheader['amin']:
                 different = True
         else:
                 different = False
                 mdnew = None
         if different:
                 apDisplay.printWarning("The tomograms are different, cannot overwrite")
                 return True
         elif apDatabase.isTomoInDB(mdnew,self.params['full'],savedtomopath):
                 ### they are the same and its in the database already
                 apDisplay.printWarning("Identical Tomogram already exists in the database!")
                 apDisplay.printWarning("Upload Not Allowed")
                 self.params['commit'] = False
                 return True
         else:
                 ### they are the same, but it is not in the database
                 apDisplay.printWarning("The same tomogram with name '"+savedtomopath+"' already exists, but is not uploaded!")
                 if self.params['commit'] is True:
                         apDisplay.printMsg("Inserting tomogram into database...")
Example #6
0
def shiftToCenter(infile, shiftfile, isEMAN=False):
    '''
	EMAN defines the rotation origin differently from other packages.
	Therefore, it needs to be recenterred according to the package
	after using EMAN proc3d rotation functions.
	'''
    # center of rotation for eman is not at length/2.
    if isEMAN:
        formatoffset = getEmanCenter()
        prefix = ''
    else:
        formatoffset = (0, 0, 0)
        prefix = 'non-'

    apDisplay.printMsg('Shifting map center for %sEMAN usage' % (prefix, ))
    # Find center of mass of the density map
    a = mrc.read(infile)
    t = a.mean() + 2 * a.std()
    numpy.putmask(a, a >= t, t)
    numpy.putmask(a, a < t, 0)
    center = ndimage.center_of_mass(a)
    offset = (center[0] + formatoffset[0] - a.shape[0] / 2,
              center[1] + formatoffset[1] - a.shape[1] / 2,
              center[2] + formatoffset[2] - a.shape[2] / 2)
    offset = (-offset[0], -offset[1], -offset[2])
    apDisplay.printMsg('Shifting map center by (x,y,z)=(%.2f,%.2f,%.2f)' %
                       (offset[2], offset[1], offset[0]))
    # shift the map
    a = mrc.read(infile)
    a = ndimage.interpolation.shift(a, offset)
    mrc.write(a, shiftfile)
    h = mrc.readHeaderFromFile(infile)
    mrc.update_file_header(shiftfile, h)
	def checkExistingFile(self):
		savedtomopath = os.path.join(self.params['rundir'], self.params['name']+".rec")
		origtomopath = self.params['file']
		apDisplay.printWarning("A Tomogram by the same filename already exists: '"+savedtomopath+"'")
		### a tomogram by the same name already exists
		savedheader = mrc.readHeaderFromFile(savedtomopath)
		savedshape = savedheader['shape']
		origheader = self.origheader
		origshape = self.origshape
		order = self.params['order']
		if self.params['full']:
			newshape = (self.imageshape[0], origshape[order.find('Z')], self.imageshape[1])
		else:
			newshape = (origshape[order.find('X')], origshape[order.find('Y')], origshape[order.find('Z')])
		# not using md5 for file comparison because it takes too long.  With padding, only min and max are stable
		if newshape != savedshape or savedheader['amax'] != origheader['amax'] or savedheader['amin'] != origheader['amin']:
			different = True
		else:
			different = False
			mdnew = None
		if different:
			apDisplay.printWarning("The tomograms are different, cannot overwrite")
			return True
		elif apDatabase.isTomoInDB(mdnew,self.params['full'],savedtomopath):
			### they are the same and its in the database already
			apDisplay.printWarning("Identical Tomogram already exists in the database!")
			apDisplay.printWarning("Upload Not Allowed")
			self.params['commit'] = False
			return True
		else:
			### they are the same, but it is not in the database
			apDisplay.printWarning("The same tomogram with name '"+savedtomopath+"' already exists, but is not uploaded!")
			if self.params['commit'] is True:
				apDisplay.printMsg("Inserting tomogram into database...")
 def checkReconVolume(self):
     from pyami import mrc
     h = mrc.readHeaderFromFile(self.tasklogfile)
     if h['amax'] == h['amin']:
         apDisplay.printError(
             'Reconstruction gives no real density.  Is the mask too tight? If not, consider using larger "hard" value to exclude fewer images in recostruction.',
             False)
Example #9
0
	def readFileHeader(self, filename):
		"""
		reads header information for any type of image or stack file
		"""
		try:
			return self.headercache[filename]
		except KeyError:
			pass
		if not os.path.exists(filename):
			return None
		#Determine extension
		if filename.endswith('.mrc') or filename.endswith('.mrcs'):
			header = mrc.readHeaderFromFile(filename)
		elif filename.endswith('.hed') or filename.endswith('.img'):
			header = imagic.readImagicHeader(filename)
		elif filename.endswith('.spi'):
			### to be implemented
			apDisplay.printError("SPIDER is not implemented yet")
		elif filename.endswith('.hdf'):
			### to be implemented
			apDisplay.printError("HDF is not implemented yet")
		else:
			apDisplay.printError("unknown stack type")
		self.headercache[filename] = header
		return header
Example #10
0
def shiftMRCStartToZero(filename):
    h = mrc.readHeaderFromFile(filename)
    if h['nxstart'] != 0 or h['nystart'] != 0:
        apDisplay.printMsg("Shifting image header start to zero on %s" %
                           (os.path.basename(filename)))
        a = mrc.read(filename)
        mrc.write(a, filename)
def writeTiltSeriesStack(stackdir, stackname, ordered_mrc_files, apix=1):
    stackpath = os.path.join(stackdir, stackname)
    print stackpath
    apixdict = {'x': apix, 'y': apix}
    if os.path.exists(stackpath):
        stheader = mrc.readHeaderFromFile(stackpath)
        stshape = stheader['shape']
        imageheader = mrc.readHeaderFromFile(ordered_mrc_files[0])
        imageshape = imageheader['shape']
        if stshape[1:] == imageshape and stshape[0] == len(ordered_mrc_files):
            apDisplay.printMsg("No need to get new stack of the tilt series")
        else:
            apImage.writeMrcStack(stackdir, stackname, order3dmrc_files, 1)
            mrc.updateFilePixelSize(stackpath, apixdict)
    else:
        apImage.writeMrcStack(stackdir, stackname, ordered_mrc_files, 1)
        mrc.updateFilePixelSize(stackpath, apixdict)
Example #12
0
def getModelDimensions(mrcfile):
    print "calculating dimensions..."
    h = mrc.readHeaderFromFile(mrcfile)
    (x, y, z) = h['mx'], h['my'], h['mz']
    if x != y != z:
        apDisplay.printWarning("starting model is not a cube")
        return max(x, y, z)
    return x
    def getImageDimensions(self, mrcfile):
        '''
		Returns dictionary of x,y dimension for an mrc image/image stack
		'''
        mrcheader = mrc.readHeaderFromFile(mrcfile)
        x = int(mrcheader['nx'].astype(numpy.uint16))
        y = int(mrcheader['ny'].astype(numpy.uint16))
        return {'x': x, 'y': y}
def writeTiltSeriesStack(stackdir,stackname,ordered_mrc_files,apix=1):
                stackpath = os.path.join(stackdir, stackname)
                print stackpath
                apixdict = {'x':apix,'y':apix}
                if os.path.exists(stackpath):
                        stheader = mrc.readHeaderFromFile(stackpath)
                        stshape = stheader['shape']
                        imageheader = mrc.readHeaderFromFile(ordered_mrc_files[0])
                        imageshape = imageheader['shape']
                        if stshape[1:] == imageshape and stshape[0] == len(ordered_mrc_files):
                                apDisplay.printMsg("No need to get new stack of the tilt series")
                        else:
                                apImage.writeMrcStack(stackdir,stackname,order3dmrc_files, 1)
                                mrc.updateFilePixelSize(stackpath,apixdict)
                else:
                        apImage.writeMrcStack(stackdir,stackname,ordered_mrc_files, 1)
                        mrc.updateFilePixelSize(stackpath,apixdict)
def getModelDimensions(mrcfile):
	print "calculating dimensions..."
	h=mrc.readHeaderFromFile(mrcfile)
	(x,y,z)=h['mx'],h['my'],h['mz']
	if x!=y!=z:
		apDisplay.printWarning("starting model is not a cube")
		return max(x,y,z)
	return x
	def getFullTomoShape(self):
		'''
		This function returns tuple of array shape as int, not numpy.int without reading the whole map
		'''
		fulltomopath = os.path.join(self.fulltomodata['reconrun']['path']['path'], self.seriesname+"_full.rec")
		fulltomoheader = mrc.readHeaderFromFile(fulltomopath)
		# conversion to int is important for sinedon insert
		return tuple(map((lambda x:int(x)),fulltomoheader['shape']))
	def getImageDimensions(self, mrcfile):
		'''
		Returns dictionary of x,y dimension for an mrc image/image stack
		'''
		mrcheader = mrc.readHeaderFromFile(mrcfile)
		x = int(mrcheader['nx'].astype(numpy.uint16))
		y = int(mrcheader['ny'].astype(numpy.uint16))
		return {'x': x, 'y': y}
	def start(self):
		commit = self.params['commit']
		description = self.params['description']
		processdir = self.params['fulltomodir']
		runname = self.params['runname']
		offsetz = self.params['offsetz']
		subbin = self.params['bin']
		invert = self.params['invert']
		fulltomodata = apTomo.getFullTomoData(self.params['fulltomoId'])
		#subvolume making
		if (self.params['selexonId'] is not None or self.params['stackId']) and fulltomodata is not None:
			sessiondata = fulltomodata['session']
			seriesname = fulltomodata['name'].rstrip('_full')
			fullbin = fulltomodata['bin']
			if not fullbin:
				apDisplay.printWarning("no binning in full tomogram, something is wrong, use alignment bin for now")
				fullbin = fulltomodata['aligner']['alignrun']['bin']
			fulltomopath = os.path.join(fulltomodata['reconrun']['path']['path'], seriesname+"_full.rec")
			fulltomoheader = mrc.readHeaderFromFile(fulltomopath)
			fulltomoshape = fulltomoheader['shape']
			if self.params['sizez'] > fulltomoshape[1]*fullbin :
				self.params['sizez'] = fulltomoshape[1]*fullbin
			subrunname = self.params['subrunname']
			volumeindex = apTomo.getLastVolumeIndex(fulltomodata) + 1
			dimension = {'x':int(self.params['sizex']),'y':int(self.params['sizey']),'z':int(self.params['sizez'])}
			zprojimagedata = fulltomodata['zprojection']
			apDisplay.printMsg("getting pixelsize")
			pixelsize = apTomo.getTomoPixelSize(zprojimagedata) * fullbin * subbin
			gtransform = [1,0,0,1,0,0]
			if self.params['selexonId']:
				particles = apParticle.getParticles(zprojimagedata, self.params['selexonId'])
			if self.params['stackId']:
				particles,stackparticles = apStack.getImageParticles(zprojimagedata, self.params['stackId'])
				stackdata = apStack.getOnlyStackData(self.params['stackId'])
			for p, particle in enumerate(particles):
				print particle['xcoord'],particle['ycoord'],fullbin
				center = apTomo.transformParticleCenter(particle,fullbin,gtransform)
				size = (dimension['x']/fullbin,dimension['y']/fullbin,dimension['z']/fullbin)
				volumename = 'volume%d'% (volumeindex,)
				volumedir = os.path.join(processdir,subrunname+'/',volumename+'/')
				apParam.createDirectory(volumedir)
				apImod.trimVolume(processdir, subrunname,seriesname,volumename,center,offsetz,size,True)
				long_volumename = seriesname+'_'+volumename
				subvolumepath = os.path.join(processdir, runname+"/",volumename+"/",long_volumename+".rec")
				if subbin > 1 or invert:
					apTomo.modifyVolume(subvolumepath,subbin,invert)
				if commit:
					subtomorundata = apTomo.insertSubTomoRun(sessiondata,
							self.params['selexonId'],self.params['stackId'],subrunname,invert,subbin)
					subtomodata = apTomo.insertSubTomogram(fulltomodata,subtomorundata,
							particle,offsetz,dimension,
							volumedir,long_volumename,volumeindex,pixelsize,
							description)
					apTomo.makeMovie(subvolumepath)
					apTomo.makeProjection(subvolumepath)
				volumeindex += 1
	def start(self):
		commit = self.params['commit']
		description = self.params['description']
		processdir = self.params['fulltomodir']
		runname = self.params['runname']
		offsetz = self.params['offsetz']
		subbin = self.params['bin']
		invert = self.params['invert']
		fulltomodata = apTomo.getFullTomoData(self.params['fulltomoId'])
		#subvolume making
		if (self.params['selexonId'] is not None or self.params['stackId']) and fulltomodata is not None:
			sessiondata = fulltomodata['session']
			seriesname = fulltomodata['name'].rstrip('_full')
			fullbin = fulltomodata['bin']
			if not fullbin:
				apDisplay.printWarning("no binning in full tomogram, something is wrong, use alignment bin for now")
				fullbin = fulltomodata['aligner']['alignrun']['bin']
			fulltomopath = os.path.join(fulltomodata['reconrun']['path']['path'], seriesname+"_full.rec")
			fulltomoheader = mrc.readHeaderFromFile(fulltomopath)
			fulltomoshape = fulltomoheader['shape']
			if self.params['sizez'] > fulltomoshape[1]*fullbin :
				self.params['sizez'] = fulltomoshape[1]*fullbin
			subrunname = self.params['subrunname']
			volumeindex = apTomo.getLastVolumeIndex(fulltomodata) + 1
			dimension = {'x':int(self.params['sizex']),'y':int(self.params['sizey']),'z':int(self.params['sizez'])}
			zprojimagedata = fulltomodata['zprojection']
			apDisplay.printMsg("getting pixelsize")
			pixelsize = apTomo.getTomoPixelSize(zprojimagedata) * fullbin * subbin
			gtransform = [1,0,0,1,0,0]
			if self.params['selexonId']:
				particles = apParticle.getParticles(zprojimagedata, self.params['selexonId'])
			if self.params['stackId']:
				particles,stackparticles = apStack.getImageParticles(zprojimagedata, self.params['stackId'])
				stackdata = apStack.getOnlyStackData(self.params['stackId'])
			for p, particle in enumerate(particles):
				print particle['xcoord'],particle['ycoord'],fullbin
				center = apTomo.transformParticleCenter(particle,fullbin,gtransform)
				size = (dimension['x']/fullbin,dimension['y']/fullbin,dimension['z']/fullbin)
				volumename = 'volume%d'% (volumeindex,)
				volumedir = os.path.join(processdir,subrunname+'/',volumename+'/')
				apParam.createDirectory(volumedir)
				apImod.trimVolume(processdir, subrunname,seriesname,volumename,center,offsetz,size,True)
				long_volumename = seriesname+'_'+volumename
				subvolumepath = os.path.join(processdir, runname+"/",volumename+"/",long_volumename+".rec")
				if subbin > 1 or invert:
					apTomo.modifyVolume(subvolumepath,subbin,invert)
				if commit:
					subtomorundata = apTomo.insertSubTomoRun(sessiondata,
							self.params['selexonId'],self.params['stackId'],subrunname,invert,subbin)
					subtomodata = apTomo.insertSubTomogram(fulltomodata,subtomorundata,
							particle,offsetz,dimension,
							volumedir,long_volumename,volumeindex,pixelsize,
							description)
					apTomo.makeMovie(subvolumepath)
					apTomo.makeProjection(subvolumepath)
				volumeindex += 1
Example #20
0
 def getFullTomoShape(self):
     '''
             This function returns tuple of array shape as int, not numpy.int without reading the whole map
             '''
     fulltomopath = os.path.join(
         self.fulltomodata['reconrun']['path']['path'],
         self.seriesname + "_full.rec")
     fulltomoheader = mrc.readHeaderFromFile(fulltomopath)
     # conversion to int is important for sinedon insert
     return tuple(map((lambda x: int(x)), fulltomoheader['shape']))
Example #21
0
    def collectResults(self, imgdata, targetdict):
        """
		Overwrite collectResults to do final processing of the
		queue job result and commit
		"""
        #cleanup and reformat image
        try:
            innamepath = glob.glob(os.path.join(targetdict['outpath'],
                                                '*.mrc'))[0]
            print innamepath
        except IndexError:
            apDisplay.printWarning('queued job for %s failed' %
                                   (imgdata['filename']))
            return
        outname = imgdata['filename'] + '-' + self.params['alignlabel'] + '.mrc'
        outnamepath = os.path.join(targetdict['outpath'], outname)
        if self.params['border'] != 0:
            command = ['proc2d', innamepath, outnamepath]
            header = mrc.readHeaderFromFile(innamepath)
            origx = header['nx']
            origy = header['ny']
            newx = origx - self.params['border']
            newy = origy - self.params['border']
            command.append('clip=%d,%d' % (newx, newy))
            print command
            subprocess.call(command)

            command = ['proc2d', outnamepath, outnamepath]
            command.append('clip=%d,%d' % (origx, origy))
            command.append('edgenorm')
            print command
            subprocess.call(command)

        newimg_array = mrc.read(outnamepath)
        self.commitAlignedImageToDatabase(imgdata, newimg_array,
                                          self.params['alignlabel'])
        # return None since everything is committed within this function.

        if self.params['hackcopy'] is True:
            origpath = imgdata['session']['image path']
            archivecopy = os.path.join(origpath,
                                       imgdata['filename'] + '.orig.mrc')
            if os.path.exists(archivecopy) is True:
                apDisplay.printMsg(
                    'archive copy for %s already exists, so skipping archive' %
                    (archivecopy))
            else:
                shutil.move(
                    os.path.join(origpath, imgdata['filename'] + '.mrc'),
                    archivecopy)
            shutil.copyfile(
                outnamepath,
                os.path.join(origpath, imgdata['filename'] + '.mrc'))

        return None
	def __init__(self,filepath):
		if not os.path.isfile(filepath):
			print "ERROR: file does not exist"
		self.file_bytes = os.path.getsize(filepath)
		self.header = mrc.readHeaderFromFile(filepath)
		self.valid_exttypes = ['CCP4','MRCO','SERI','AGAR','FEI1']
		self.is2D = True
		self.isCrystal = False
		self.errors = []
		self.updates = []
		self.infos = []
Example #23
0
 def __init__(self, filepath):
     if not os.path.isfile(filepath):
         print "ERROR: file does not exist"
     self.file_bytes = os.path.getsize(filepath)
     self.header = mrc.readHeaderFromFile(filepath)
     self.valid_exttypes = ['CCP4', 'MRCO', 'SERI', 'AGAR', 'FEI1']
     self.is2D = True
     self.isCrystal = False
     self.errors = []
     self.updates = []
     self.infos = []
Example #24
0
def getSubTomoBoundary(processdir, seriesname, axis):
    '''
        Get the start and end coordinate at a particular axis.
        The definition of origin may or may not apply to tomograms
        generated outside imod.
        '''
    fulltomopath = os.path.join(processdir, seriesname + '_full.rec')
    subtomopath = os.path.join(processdir, seriesname + '.rec')
    fullheader = mrc.readHeaderFromFile(fulltomopath)
    subheader = mrc.readHeaderFromFile(subtomopath)

    lenkey = axis + 'len'
    originkey = axis + 'origin'
    mkey = 'm' + axis
    # There is a rotation around x-axis in full tomogram
    fullorigin = {
        'xorigin': fullheader['xorigin'],
        'yorigin': fullheader['zorigin'],
        'zorigin': fullheader['yorigin']
    }
    fullm = {
        'mx': fullheader['mx'],
        'my': fullheader['mz'],
        'mz': fullheader['my']
    }
    # full and sub tomo should have the same pixelsize
    pixelsize = subheader[lenkey] / subheader[mkey]
    if axis != 'z':
        start = int((fullorigin[originkey] - subheader[originkey]) / pixelsize)
    else:
        # z origin is defined different from x and y
        start = -int(
            (fullorigin[originkey] + subheader[originkey]) / pixelsize) + int(
                fullm[mkey])
    end = start + int(subheader[mkey])
    return (start, end)
	def collectResults(self, imgdata,targetdict):
		"""
		Overwrite collectResults to do final processing of the
		queue job result and commit
		"""
		#cleanup and reformat image
		try:
			innamepath=glob.glob(os.path.join(targetdict['outpath'],'*.mrc'))[0]
			print innamepath
		except IndexError:
			apDisplay.printWarning('queued job for %s failed' % (imgdata['filename']))
			return
		outname=imgdata['filename']+'-'+self.params['alignlabel']+'.mrc'
		outnamepath=os.path.join(targetdict['outpath'],outname)
		if self.params['border'] != 0:
			command=['proc2d',innamepath, outnamepath]
			header=mrc.readHeaderFromFile(innamepath)
			origx=header['nx']
			origy=header['ny']
			newx=origx-self.params['border']
			newy=origy-self.params['border']
			command.append('clip=%d,%d' % (newx,newy))
			print command
			subprocess.call(command)
			
			
			command=['proc2d',outnamepath,outnamepath]
			command.append('clip=%d,%d' % (origx,origy))
			command.append('edgenorm')
			print command
			subprocess.call(command)
		
		newimg_array = mrc.read(outnamepath)
		self.commitAlignedImageToDatabase(imgdata,newimg_array,self.params['alignlabel'])
		# return None since everything is committed within this function.
	
		if self.params['hackcopy'] is True:
			origpath=imgdata['session']['image path']
			archivecopy=os.path.join(origpath,imgdata['filename']+'.orig.mrc')
			if os.path.exists(archivecopy) is True:
				apDisplay.printMsg('archive copy for %s already exists, so skipping archive' % (archivecopy))
			else:
				shutil.move(os.path.join(origpath,imgdata['filename']+'.mrc'), archivecopy)
			shutil.copyfile(outnamepath, os.path.join(origpath,imgdata['filename']+'.mrc'))

		return None
Example #26
0
def getMrcFileShape(mrcpath):
	header = mrc.readHeaderFromFile(mrcpath)
	return header['shape']
	def checkReconVolume(self):
		from pyami import mrc
		h = mrc.readHeaderFromFile(self.tasklogfile)
		if h['amax'] == h['amin']:
			apDisplay.printError('Reconstruction gives no real density.  Is the mask too tight? If not, consider using larger "hard" value to exclude fewer images in recostruction.',False)
	def getSubTomogramSize(self,subtomopath):
		tomoheader = mrc.readHeaderFromFile(subtomopath)
		# conversion to int is important for sinedon insert
		return (int(tomoheader['mx']),int(tomoheader['my']),int(tomoheader['mz']))
	def getNumberOfFrames(self, mrcfile):
		'''
		Returns number of frames of an mrc image/image stack
		'''
		mrcheader = mrc.readHeaderFromFile(mrcfile)
		return max(1,int(mrcheader['nz'].astype(numpy.uint16)))
	def getNumberOfFrames(self, mrcfile):
		mrcheader = mrc.readHeaderFromFile(mrcfile)
		return max(1,int(mrcheader['nz'].astype(numpy.uint16)))
def shiftMRCStartToZero(filename):
        h = mrc.readHeaderFromFile(filename)
        if h['nxstart'] != 0 or h['nystart'] !=0:
                apDisplay.printMsg("Shifting image header start to zero on %s" %(os.path.basename(filename)))
                a = mrc.read(filename)
                mrc.write(a, filename)
Example #32
0
def getMrcFileShape(mrcpath):
    header = mrc.readHeaderFromFile(mrcpath)
    return header['shape']
    def getNumberOfFrames(self, mrcfile):
        '''
		Returns number of frames of an mrc image/image stack
		'''
        mrcheader = mrc.readHeaderFromFile(mrcfile)
        return max(1, int(mrcheader['nz'].astype(numpy.uint16)))
Example #34
0
from optparse import OptionParser

if __name__ == '__main__':
	parser = OptionParser()
	parser.add_option("-f", "--file", dest="mrcfile",
		help="MRC file", metavar="FILE")
	parser.add_option("-c", "--contour", "--threshold", dest="threshold", default=1.0,
		help="Contour threshold for density", metavar="#", type="float")
	(options, args) = parser.parse_args()

	mrcfile = options.mrcfile
	if not mrcfile or not os.path.isfile(mrcfile):
		parser.print_help()
		
	floatdata = mrc.read(mrcfile)
	header = mrc.readHeaderFromFile(mrcfile)

	#del floatdata

	s = numpy.ones((3,3,3))
	print s
	print "Median filter"
	floatdata = ndimage.median_filter(floatdata, size=3)
	
	print "Closing operation"
	booldata = numpy.array(floatdata > options.threshold, dtype=int)
	closebooldata = ndimage.morphology.binary_closing(booldata, iterations=3)
	floatdata = numpy.where(closebooldata > booldata, floatdata+options.threshold/1.5, floatdata)
	mrc.write(numpy.array(closebooldata, dtype=int), "bool.mrc", header)

Example #35
0
 def getOriginalVolumeShape(self):
     origtomopath = self.params['file']
     self.origheader = mrc.readHeaderFromFile(origtomopath)
     self.origshape = self.origheader['shape']
Example #36
0
 def start(self):
     cleanlist = []
     # imod needs the recon files named as .rec  It may need some fix later
     if self.params['name'] is None:
         self.setNewFileName()
     apDisplay.printColor("Naming tomogram as: " + self.params['name'],
                          "cyan")
     newtomopath = os.path.join(self.params['rundir'],
                                self.params['name'] + ".rec")
     origtomopath = self.params['file']
     origxfpath = self.params['oldxffile']
     apParam.createDirectory(self.params['aligndir'])
     newxfpath = os.path.join(self.params['aligndir'],
                              self.params['newxffile'])
     order = self.params['order']
     voltransform = self.params['transform']
     bin = self.params['bin']
     self.getImageShapeFromTiltSeries()
     self.getOriginalVolumeShape()
     if os.path.isfile(newtomopath):
         uploaded_before = self.checkExistingFile()
         if uploaded_before:
             return
     else:
         currenttomopath = origtomopath
         if self.params['full']:
             ### full tomogram upload, may need to pad to the image size
             if self.params['order'] == 'XZY' and not voltransform:
                 apDisplay.printMsg(
                     "Default full tomogram orientation with original handness"
                 )
             else:
                 if voltransform:
                     apDisplay.printMsg(
                         "Transforming original tomogram....")
                     currenttomopath = apImod.transformVolume(
                         origtomopath, voltransform)
                     cleanlist.append(currenttomopath)
             ### padding the XZY tomogram to the image size
             currentheader = mrc.readHeaderFromFile(currenttomopath)
             currentshape = currentheader['shape']
             currentxyshape = currentshape[0], currentshape[2]
             imageshape = self.imageshape
             if currentxyshape[0] < imageshape[0] / bin or currentxyshape[
                     1] < imageshape[1] / bin:
                 currenttomopath = apImod.pad(currenttomopath,
                                              currentxyshape, imageshape,
                                              bin, 'XZY')
                 cleanlist.append(currenttomopath)
         else:
             ### subtomogram simple upload, just copy file to Tomo folder
             if self.params['order'] == 'XYZ' and not voltransform:
                 apDisplay.printMsg("Default sub tomogram orientation")
             else:
                 if voltransform:
                     apDisplay.printMsg(
                         "Transforming original tomogram....")
                     currenttomopath = apImod.transformVolume(
                         origtomopath, voltransform)
                     cleanlist.append(currenttomopath)
         ### simple upload, just copy file to Tomo folder
         apDisplay.printMsg(
             "Copying original tomogram to a new location: " + newtomopath)
         shutil.copyfile(currenttomopath, newtomopath)
         if origxfpath and os.path.isfile(origxfpath):
             apDisplay.printMsg(
                 "Copying original alignment to a new location: " +
                 newxfpath)
             shutil.copyfile(origxfpath, newxfpath)
         if self.params['image']:
             shutil.copyfile(self.params['image'],
                             self.params['rundir'] + '/snapshot.png')
     ### inserting tomogram
     tomoheader = mrc.readHeaderFromFile(newtomopath)
     self.params['shape'] = tomoheader['shape']
     if self.params['full']:
         seriesname = "%s_%03d" % (self.params['sessionname'],
                                   self.params['tiltseriesnumber'])
         self.params['zprojfile'] = apImod.projectFullZ(
             self.params['rundir'], self.params['runname'], seriesname, bin,
             True, False)
     else:
         apTomo.makeMovie(newtomopath)
         apTomo.makeProjection(newtomopath)
     apTomo.uploadTomo(self.params)
     ### clean up
     for tmpfilepath in cleanlist:
         apFile.removeFile(tmpfilepath)
 def getOriginalVolumeShape(self):
     origtomopath = self.params["file"]
     self.origheader = mrc.readHeaderFromFile(origtomopath)
     self.origshape = self.origheader["shape"]
Example #38
0
        print "Bisecting along Y-axis"
        y_index = y_linedensity.argmax()
        cut_1a = orig1.copy()
        cut_1b = orig1.copy()
        cut_2a = orig2.copy()
        cut_2b = orig2.copy()
        cut_1a[:, :y_index, :] = 0
        cut_1b[:, y_index:, :] = 0
        cut_2a[:, :y_index, :] = 0
        cut_2b[:, y_index:, :] = 0
    elif z_maxcrossarea == maxcrossarea:
        print "Bisecting along Z-axis"
        z_index = z_linedensity.argmax()
        cut_1a = orig1.copy()
        cut_1b = orig1.copy()
        cut_2a = orig2.copy()
        cut_2b = orig2.copy()
        cut_1a[:, :, :z_index] = 0
        cut_1b[:, :, z_index:] = 0
        cut_2a[:, :, :z_index] = 0
        cut_2b[:, :, z_index:] = 0
    header = mrc.readHeaderFromFile(file1)
    print "Writing %s to file" % (root1 + "-top.mrc")
    mrc.write(cut_1a, root1 + "-top.mrc", header)
    print "Writing %s to file" % (root1 + "-bot.mrc")
    mrc.write(cut_1b, root1 + "-bot.mrc", header)
    print "Writing %s to file" % (root2 + "-top.mrc")
    mrc.write(cut_2a, root2 + "-top.mrc", header)
    print "Writing %s to file" % (root2 + "-bot.mrc")
    mrc.write(cut_2b, root2 + "-bot.mrc", header)
 def start(self):
     cleanlist = []
     # imod needs the recon files named as .rec  It may need some fix later
     if self.params["name"] is None:
         self.setNewFileName()
     apDisplay.printColor("Naming tomogram as: " + self.params["name"], "cyan")
     newtomopath = os.path.join(self.params["rundir"], self.params["name"] + ".rec")
     origtomopath = self.params["file"]
     origxfpath = self.params["oldxffile"]
     apParam.createDirectory(self.params["aligndir"])
     newxfpath = os.path.join(self.params["aligndir"], self.params["newxffile"])
     order = self.params["order"]
     voltransform = self.params["transform"]
     bin = self.params["bin"]
     self.getImageShapeFromTiltSeries()
     self.getOriginalVolumeShape()
     if os.path.isfile(newtomopath):
         uploaded_before = self.checkExistingFile()
         if uploaded_before:
             return
     else:
         currenttomopath = origtomopath
         if self.params["full"]:
             ### full tomogram upload, may need to pad to the image size
             if self.params["order"] == "XZY" and not voltransform:
                 apDisplay.printMsg("Default full tomogram orientation with original handness")
             else:
                 if voltransform:
                     apDisplay.printMsg("Transforming original tomogram....")
                     currenttomopath = apImod.transformVolume(origtomopath, voltransform)
                     cleanlist.append(currenttomopath)
             ### padding the XZY tomogram to the image size
             currentheader = mrc.readHeaderFromFile(currenttomopath)
             currentshape = currentheader["shape"]
             currentxyshape = currentshape[0], currentshape[2]
             imageshape = self.imageshape
             if currentxyshape[0] < imageshape[0] / bin or currentxyshape[1] < imageshape[1] / bin:
                 currenttomopath = apImod.pad(currenttomopath, currentxyshape, imageshape, bin, "XZY")
                 cleanlist.append(currenttomopath)
         else:
             ### subtomogram simple upload, just copy file to Tomo folder
             if self.params["order"] == "XYZ" and not voltransform:
                 apDisplay.printMsg("Default sub tomogram orientation")
             else:
                 if voltransform:
                     apDisplay.printMsg("Transforming original tomogram....")
                     currenttomopath = apImod.transformVolume(origtomopath, voltransform)
                     cleanlist.append(currenttomopath)
         ### simple upload, just copy file to Tomo folder
         apDisplay.printMsg("Copying original tomogram to a new location: " + newtomopath)
         shutil.copyfile(currenttomopath, newtomopath)
         if origxfpath and os.path.isfile(origxfpath):
             apDisplay.printMsg("Copying original alignment to a new location: " + newxfpath)
             shutil.copyfile(origxfpath, newxfpath)
         if self.params["image"]:
             shutil.copyfile(self.params["image"], self.params["rundir"] + "/snapshot.png")
     ### inserting tomogram
     tomoheader = mrc.readHeaderFromFile(newtomopath)
     self.params["shape"] = tomoheader["shape"]
     if self.params["full"]:
         seriesname = "%s_%03d" % (self.params["sessionname"], self.params["tiltseriesnumber"])
         self.params["zprojfile"] = apImod.projectFullZ(
             self.params["rundir"], self.params["runname"], seriesname, bin, True, False
         )
     else:
         apTomo.makeMovie(newtomopath)
         apTomo.makeProjection(newtomopath)
     apTomo.uploadTomo(self.params)
     ### clean up
     for tmpfilepath in cleanlist:
         apFile.removeFile(tmpfilepath)
Example #40
0
 def getSubTomogramSize(self, subtomopath):
     tomoheader = mrc.readHeaderFromFile(subtomopath)
     # conversion to int is important for sinedon insert
     return (int(tomoheader['mx']), int(tomoheader['my']),
             int(tomoheader['mz']))
	def getImageDimensions(self, mrcfile):
		mrcheader = mrc.readHeaderFromFile(mrcfile)
		x = int(mrcheader['nx'].astype(numpy.uint16))
		y = int(mrcheader['ny'].astype(numpy.uint16))
		return {'x': x, 'y': y}
			# integration half box size
			self.panel.box = box

			self.sizer.Add(self.panel, 1, wx.EXPAND|wx.ALL)
			frame.SetSizerAndFit(self.sizer)
			self.SetTopWindow(frame)
			frame.Show(True)
			return True

	array = None
	if filename is None:
		filename = raw_input('Enter file path: ')
	if not filename:
		array = None
	elif filename[-4:] == '.mrc':
		h = mrc.readHeaderFromFile(filename)
		if h['mz'] == 0:
			print 'invalid mz, assumes 1'
			h['mz'] = 1
		nframes = h['nz']/h['mz']
		frame = 0
		# 2D image stack
		if h['mz'] == 1:
			if nframes > 1:
				# mrc2014 image stack
				frame_str = raw_input('This is an image stack of %d frames.\n Enter 0 to %d to select a frame to load: ' % (nframes,nframes-1))
				frame = int(frame_str)
		else:
			if nframes > 1:
				slice_str = raw_input('This is a stack of %d volume.\n Enter 0 to %d to select a slice to load: ' % (nframes,h['nz']-1))
			else:
Example #43
0
        z_maxcrossarea = 0
    maxcrossarea = max(x_maxcrossarea, y_maxcrossarea, z_maxcrossarea)

    if x_maxcrossarea == maxcrossarea:
        print "Bisecting along X-axis"
        x_index = x_linedensity.argmax()
        cut_1 = orig.copy()
        cut_2 = orig.copy()
        cut_1[:x_index, :, :] = 0
        cut_2[x_index:, :, :] = 0
    elif y_maxcrossarea == maxcrossarea:
        print "Bisecting along Y-axis"
        y_index = y_linedensity.argmax()
        cut_1 = orig.copy()
        cut_2 = orig.copy()
        cut_1[:, :y_index, :] = 0
        cut_2[:, y_index:, :] = 0
    elif z_maxcrossarea == maxcrossarea:
        print "Bisecting along Z-axis"
        z_index = z_linedensity.argmax()
        cut_1 = orig.copy()
        cut_2 = orig.copy()
        cut_1[:, :, :z_index] = 0
        cut_2[:, :, z_index:] = 0

    header = mrc.readHeaderFromFile(filename)
    print "Writing %s to file" % (rootname + "-top.mrc")
    mrc.write(cut_1, rootname + "-top.mrc", header)
    print "Writing %s to file" % (rootname + "-bot.mrc")
    mrc.write(cut_2, rootname + "-bot.mrc", header)
 def getImageDimensions(self, mrcfile):
     mrcheader = mrc.readHeaderFromFile(mrcfile)
     x = int(mrcheader["nx"].astype(numpy.uint16))
     y = int(mrcheader["ny"].astype(numpy.uint16))
     return {"x": x, "y": y}