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 emxParticlesToXmipp(emxData, outputFileName=PARTFILE, filesPrefix=None, ctfRoot='', _2D=False): """ This function will iterate over the EMX particles and create the equivalent Xmipp 'images.xmd' with the list of particles. If CTF information is found, for each particle will contains information about the CTF """ #iterate though emxData md = MetaData() mdMic = MetaData() if ctfRoot:# is None: ctfRoot = dirname(outputFileName) samplingRate = 0. voltage = 0. cs = 0. amplitudeContrast = 0. oldSamplingRate = -1. oldVoltage = -1. oldCs = -1. oldAmplitudeContrast = -1.0 for particle in emxData.iterClasses(PARTICLE): pIndex = particle.get(INDEX) pFileName = particle.get(FILENAME) if pFileName is None: raise Exception("emxParticlesToXmipp: Xmipp doesn't support Particles without filename") if filesPrefix is not None: pFileName = join(filesPrefix, pFileName) if pIndex is not None: pFileName = '%06d@%s' % (pIndex, pFileName) # pIndex is ignored now, in a more general solution # Xmipp should be able to handle particles in a stack # where the index has sense.... objId = md.addObject() md.setValue(MDL_IMAGE, pFileName, objId) mic = particle.getMicrograph() if mic is not None: micFileName = mic.get(FILENAME) if filesPrefix is not None: micFileName = join(filesPrefix, micFileName) md.setValue(MDL_MICROGRAPH, micFileName, objId) #### ctf = CTF() for var in CTF.ctfVarLabels.keys(): setattr(ctf, var, mic.get(var)) # Now do some adjusments to vars if ctf.defocusU is not None: ctf.defocusU *= 10. if ctf.defocusV is None: ctf.defocusV = ctf.defocusU ctf.defocusUAngle = 0. else: ctf.defocusV *= 10. # if ctf.pixelSpacing__Y is not None: # if ctf.pixelSpacing__X != ctf.pixelSpacing__Y: # raise Exception ('pixelSpacingX != pixelSpacingY. Xmipp does not support it') if ctf.pixelSpacing__X is not None: samplingRate = ctf.pixelSpacing__X if (oldSamplingRate > -1): if oldSamplingRate != samplingRate: raise Exception ('Xmipp emx import cannot import emx files with different samplingRate') oldSamplingRate = samplingRate if ctf.acceleratingVoltage is not None: voltage = ctf.acceleratingVoltage if (oldVoltage > -1): if oldVoltage != voltage: raise Exception ('Xmipp emx import cannot import emx files with different voltage') oldVoltage = voltage if ctf.cs is not None: cs = ctf.cs if (oldCs > -1): if oldCs != cs: raise Exception ('Xmipp emx import cannot import emx files with different cs') oldCs = cs if ctf.amplitudeContrast is not None: amplitudeContrast = ctf.amplitudeContrast if (oldAmplitudeContrast > -1): if oldAmplitudeContrast != amplitudeContrast: raise Exception ('Xmipp emx import cannot import emx files with different amplitudeContrast') oldAmplitudeContrast = amplitudeContrast # Create the .ctfparam, replacing the micrograph name #write only if ctf information is available if ctf.defocusU is not None: for var, label in CTF.ctfVarLabels.iteritems(): v = getattr(ctf, var) if v is not None: md.setValue(label, float(v), objId) md.setValue(MDL_CTF_K, 1.0, objId) if particle.has('centerCoord__X'): md.setValue(MDL_XCOOR, int(particle.get('centerCoord__X')), objId) md.setValue(MDL_YCOOR, int(particle.get('centerCoord__Y')), objId) if particle.has('transformationMatrix__t11'): emxTransformToXmipp(md, objId, particle,_2D) if particle.has('defocusU'): md.setValue(MDL_CTF_DEFOCUSU, particle.get('defocusU')*10., objId) if particle.has('defocusV'): md.setValue(MDL_CTF_DEFOCUSV, particle.get('defocusV')*10., objId) else: md.setValue(MDL_CTF_DEFOCUSV, particle.get('defocusU')*10., objId) if particle.has('defocusUAngle'): md.setValue(MDL_CTF_DEFOCUS_ANGLE, particle.get('defocusUAngle'), objId) # Sort metadata by particle name md.sort(MDL_IMAGE) # Write particles metadata md.write('Particles@' + outputFileName)
def emxCTFToXmipp(emxData, outputFileName=MICFILE, filesPrefix='', #dirname ctfRoot='', _voltage=None, _sphericalAberration=None, _samplingRate=None, _amplitudeContrast=None ): """ This function will iterate over the EMX micrographs and create a file .ctfparam for each micrograph. """ hasCTFMicro = False hasCTFParticle = False if emxData.objLists[PARTICLE] and (emxData.objLists[PARTICLE][0]).get('defocusU') is not None: hasCTFParticle = True objectClass = PARTICLE if (emxData.objLists[MICROGRAPH][0]).get('defocusU') is not None: hasCTFMicro = True objectClass = MICROGRAPH if not (hasCTFParticle or hasCTFMicro): print "No CTF information available" return # no ctf info #fill metadata with CTF info #objMd = MetaData() for object in emxData.iterClasses(objectClass): objIndex = object.get(INDEX) objFileName = object.get(FILENAME) if objFileName is None: raise Exception("emxCTFToXmipp: Xmipp doesn't support Objects without filename") elif objIndex is not None: objFileName = join(filesPrefix, "%06d@%s"%(int(objIndex),objFileName)) else: objFileName = join(filesPrefix, "%s"%(objFileName)) ctfModelFileName = join(ctfRoot, replaceBasenameExt(objFileName, CTFENDING)) ctf = CTFDEFOCUS() # Set the variables in the dict for var in CTF.ctfVarLabels.keys(): setattr(ctf, var, object.get(var)) if not ctfRoot:# is None: ctfRoot = dirname(outputFileName) for micrograph in emxData.iterClasses(MICROGRAPH): micIndex = micrograph.get(INDEX) micFileName = micrograph.get(FILENAME) samplingRate = 0. voltage = 0. cs = 0. amplitudeContrast = 0. oldSamplingRate = -1. oldVoltage = -1. oldCs = -1. oldAmplitudeContrast = -1.0 for micrograph in emxData.iterClasses(MICROGRAPH): micIndex = micrograph.get(INDEX) micFileName = micrograph.get(FILENAME) if micFileName is None: raise Exception("emxMicsToXmipp: Xmipp doesn't support Micrograph without filename") # if micFileName is None: # raise Exception("emxCTFToXmipp: Xmipp doesn't support Objects without filename") elif micIndex is not None: objFileName = join(filesPrefix, "%06d@%s"%(int(micIndex),micFileName)) else: objFileName = join(filesPrefix, "%s"%(micFileName)) ctfModelFileName = join(ctfRoot, replaceBasenameExt(micFileName, CTFENDING)) ctf = CTF() ctf.pixelSpacing__Y = micrograph.get('pixelSpacing__Y') # Set the variables in the dict for var in CTF.ctfVarLabels.keys(): setattr(ctf, var, micrograph.get(var)) # Now do some adjusments to vars if ctf.defocusU is not None: ctf.defocusU *= 10. if ctf.defocusV is None: ctf.defocusV = ctf.defocusU ctf.defocusUAngle = 0. else: ctf.defocusV *= 10. if ctf.pixelSpacing__Y is not None: if ctf.pixelSpacing__X != ctf.pixelSpacing__Y: raise Exception ('pixelSpacingX != pixelSpacingY. Xmipp does not support it') if ctf.pixelSpacing__X is not None: samplingRate = ctf.pixelSpacing__X if (oldSamplingRate > -1): if oldSamplingRate != samplingRate: raise Exception ('Xmipp emx import cannot import emx files with different samplingRate') oldSamplingRate = samplingRate if ctf.acceleratingVoltage is not None: voltage = ctf.acceleratingVoltage if (oldVoltage > -1): if oldVoltage != voltage: raise Exception ('Xmipp emx import cannot import emx files with different voltage') oldVoltage = voltage if ctf.cs is not None: cs = ctf.cs if (oldCs > -1): if oldCs != cs: raise Exception ('Xmipp emx import cannot import emx files with different cs') oldCs = cs if ctf.amplitudeContrast is not None: amplitudeContrast = ctf.amplitudeContrast if (oldAmplitudeContrast > -1): if oldAmplitudeContrast != amplitudeContrast: raise Exception ('Xmipp emx import cannot import emx files with different amplitudeContrast') oldAmplitudeContrast = amplitudeContrast # Create the .ctfparam, replacing the micrograph name #write only if ctf information is available if (ctf.acceleratingVoltage is not None and ctf.cs is not None and ctf.amplitudeContrast is not None ): mdCtf = RowMetaData() for var, label in CTF.ctfVarLabels.iteritems(): v = getattr(ctf, var) if v is not None: mdCtf.setValue(label, float(v)) mdCtf.setValue(MDL_CTF_K, 1.0) mdCtf.write(ctfModelFileName) # Sort metadata by micrograph name #mdMic.sort(MDL_MICROGRAPH) # Write micrographs metadata #mdMic.write('Micrographs@' + outputFileName) return voltage, cs, samplingRate