def xmippMicrographsToEmx(micMd, emxData, emxDir): """ Export micrographs from xmipp metadata to EMX. """ #acquisionInfo = findAcquisitionInfo(self.SelFileNameInitial) from protlib_particles import readPosCoordinates md = MetaData(micMd) acquisionInfo = findAcquisitionInfo(md.getValue(MDL_MICROGRAPH, md.firstObject())) micFn = 'mic%06d.mrc' index = 0 pIndex = 0 img = Image() hasCtf = md.containsLabel(MDL_CTF_MODEL) filesRoot = dirname(micMd) #assume sam pixelspacing for every micrograph acquisionInfo = findAcquisitionInfo(md.getValue(MDL_MICROGRAPH, md.firstObject())) if not acquisionInfo is None: mdAux = MetaData(acquisionInfo) pixelSpacing = mdAux.getValue(MDL_SAMPLINGRATE, mdAux.firstObject()) else: pixelSpacing = 1. for objId in md: fnIn = md.getValue(MDL_MICROGRAPH, objId) index += 1 fnOut = micFn % index img.read(fnIn) img.write(join(emxDir, fnOut)) micrograph = EmxMicrograph(fnOut) # Set CTF parameters if present if hasCtf: xmippCtfToEmx(md, objId, micrograph) posFile = join(filesRoot, 'extra', replaceBasenameExt(fnIn, POSENDING)) if exists(posFile): mdPos = readPosCoordinates(posFile) for pId in mdPos: pIndex += 1 # We are using here a dummy filename, since not make sense # for particles with just coordinates particle = EmxParticle("", pIndex) particle.set('centerCoord__X', mdPos.getValue(MDL_XCOOR, pId)) particle.set('centerCoord__Y', mdPos.getValue(MDL_YCOOR, pId)) particle.setMicrograph(micrograph) emxData.addObject(particle) micrograph.set('pixelSpacing__X',pixelSpacing) micrograph.set('pixelSpacing__Y',pixelSpacing) micrograph.set(COMMENT,'original filename=%s'%fnIn) emxData.addObject(micrograph) # Write EMX particles _writeEmxData(emxData, join(emxDir, 'micrographs.emx'))
def getSampling(InputFile, default=None): fnAcquisition = findAcquisitionInfo(InputFile) if fnAcquisition is not None and os.path.exists(fnAcquisition): md = xmipp.MetaData(fnAcquisition) sampling = md.getValue(xmipp.MDL_SAMPLINGRATE, md.firstObject()) else: if default is None: raise Exception('Cannot find acquisition_info.xmd from path %s.' % InputFile) sampling = float(default) return sampling
def createAcquisition(log,InputFile,WorkingDir,DoResize,NewSize): fnAcqIn = findAcquisitionInfo(InputFile) if not fnAcqIn is None: fnAcqOut = getProtocolFilename('acquisition', WorkingDir=WorkingDir) if not DoResize: createLink(log, fnAcqIn, fnAcqOut) else: md = MetaData(fnAcqIn) id = md.firstObject() Ts = md.getValue(MDL_SAMPLINGRATE, id) (Xdim, Ydim, Zdim, Ndim, _) = MetaDataInfo(InputFile) downsampling = float(Xdim)/NewSize; md.setValue(MDL_SAMPLINGRATE,Ts*downsampling,id) md.write(getProtocolFilename('acquisition', WorkingDir=WorkingDir))
def validate(self): errors = [] md = MetaData() md.read(self.ImgMd, 1) # Read only the first object in md if md.containsLabel(MDL_IMAGE): if not getattr(self, 'Is2D', False): self._validateInputSize('Ref3D', 'Reference volume', True, md, errors) self._validateInputSize('ReferenceMask', 'Reference mask', False, md, errors) self._validateInputSize('SolventMask','Solvent mask', False, md, errors) else: errors.append("Input metadata <%s> doesn't contains image label" % self.ImgMd) if self.DoCTFCorrection and not self._containsCTF(md): errors.append("CTF correction selected and input metadata <%s> doesn't contains CTF information" % self.ImgMd) # Check relion is installed if len(which('relion_refine')) == 0: errors.append('<relion_refine> was not found.') if len(which('relion_movie_handler')) == 0: errors.append('''program "relion_movie_handler" is missing. Are you sure you have relion version 1.2?.''') if self.DoContinue: errors += self.__validateContinue() if self.PrevRun: errors += self._validateContinue() else: errors += self._validate() if not findAcquisitionInfo(self.ImgMd): errors.append("""Acquisition file for metadata %s is not available. Either import images before using them or create a file named acquisition_info.xmd (see example below) and place it in the same directory than the %s file EXAMPLE: # XMIPP_STAR_1 * # change XXXXX by the sampling rate data_noname _sampling_rate XXXXX """ %(self.ImgMd,self.ImgMd)) return errors
def defineSteps(self): acquisitionInfo = findAcquisitionInfo(self.Operand1) if acquisitionInfo is not None: self.Db.insertStep("linkAcquisitionInfo",InputFile=self.Operand1,dirDest=self.WorkingDir) self.Db.insertStep("runImageOperate",WorkingDir=self.WorkingDir,O1=self.Operand1,Operation=self.Operation,O2=self.Operand2, Nproc=self.NumberOfMpi)
def xmippParticlesToEmx(imagesMd, emxData, emxDir, doAlign): """ Export particles from xmipp metadata to EMX. imagesMd: the filename of the images metadata emxData: the emxData object to be populated. """ md = MetaData(imagesMd) hasCS = md.containsLabel(MDL_CTF_CS) hasQ0 = md.containsLabel(MDL_CTF_Q0) hasDefocusU = md.containsLabel(MDL_CTF_DEFOCUSU) hasDefocusV = md.containsLabel(MDL_CTF_DEFOCUSV) hasVoltage = md.containsLabel(MDL_CTF_VOLTAGE) hasDefocusUAngle = md.containsLabel(MDL_CTF_DEFOCUS_ANGLE) md.removeDisabled() imgFn = 'particles.mrc' mrcFn = join(emxDir, imgFn) index = 0 img = Image() hasCoords = md.containsLabel(MDL_XCOOR) hasGeo = hasGeoLabels(md) hasMicrograph = md.containsLabel(MDL_MICROGRAPH) hasCtfModel = md.containsLabel(MDL_CTF_MODEL) hasCtfPart = hasCS and hasQ0 and hasDefocusU and hasVoltage # note that defocuV and angle are optional micsDict = {} acquisionInfo = findAcquisitionInfo(md.getValue(MDL_IMAGE, md.firstObject())) if not acquisionInfo is None: mdAux = MetaData(acquisionInfo) pixelSpacing = mdAux.getValue(MDL_SAMPLINGRATE, mdAux.firstObject()) else: pixelSpacing = 1. for objId in md: # Read image from filename fnIn = FileName(md.getValue(MDL_IMAGE, objId)) img.read(fnIn) # Write to mrc stack index += 1 img.write('%d@%s' % (index, mrcFn)) particle = EmxParticle(imgFn, index) # Create the micrograph object if exists micrograph=None if hasMicrograph or hasCtfModel or hasCtfPart: fn = md.getValue(MDL_MICROGRAPH, objId) if fn is None: if hasCtfModel: fn = md.getValue(MDL_CTF_MODEL, objId) else: fn='%s%06d' % (mrcFn,index) if fn in micsDict: micrograph = micsDict[fn] else: idx, path = FileName(fn).decompose() idx = idx or None micrograph = EmxMicrograph(path, idx) if hasCS: micrograph.set('cs', md.getValue(MDL_CTF_CS,objId)) if hasQ0: micrograph.set('amplitudeContrast', md.getValue(MDL_CTF_Q0,objId)) if hasVoltage: micrograph.set('acceleratingVoltage', md.getValue(MDL_CTF_VOLTAGE,objId)) emxData.addObject(micrograph) micsDict[fn] = micrograph particle.setMicrograph(micrograph) if hasCtfPart: xmippCtfToEmx(md, objId, particle=particle, micrograph=micrograph) if hasCtfModel: xmippCtfToEmx(md, objId, micrograph=micrograph) # Check if there are coordinates if hasCoords: particle.set('centerCoord__X', md.getValue(MDL_XCOOR, objId)) particle.set('centerCoord__Y', md.getValue(MDL_YCOOR, objId)) # If there is geometrical info, convert and set particle.set('pixelSpacing__X',pixelSpacing) particle.set('pixelSpacing__Y',pixelSpacing) if hasGeo: xmippTransformToEmx(md, objId, particle,doAlign) particle.set(COMMENT,'original filename=%s'%fnIn) # Add particle to emxData emxData.addObject(particle) # Write EMX particles _writeEmxData(emxData, join(emxDir, 'particles.emx'))