def readSetOfParticles(inputSet, outputSet, parFileName): """ Iterate through the inputSet and the parFile lines and populate the outputSet with the same particles of inputSet, but with the angles and shift (3d alignment) updated from the parFile info. It is assumed that the order of iteration of the particles and the lines match and have the same number. """ #create dictionary that matches input particles with param file samplingRate = inputSet.getSamplingRate() parFile = FrealignParFile(parFileName) partIter = iter( inputSet.iterItems(orderBy=['_micId', 'id'], direction='ASC')) for particle, row in izip(partIter, parFile): particle.setTransform(rowToAlignment(row, samplingRate)) # We assume that each particle have ctfModel # in order to be processed in Frealign # JMRT: Since the CTF will be set, we can setup # an empty CTFModel object if not particle.hasCTF(): particle.setCTF(em.CTFModel()) rowToCtfModel(row, particle.getCTF()) outputSet.append(particle) outputSet.setAlignment(em.ALIGN_PROJ)
def __createTemporaryCtfs(self, obj, setOfMics): """ Create a temporary .sqlite file to visualize CTF while the protocol has not finished yet. """ cleanPath(obj._getPath("ctfs_temporary.sqlite")) ctfSet = self.protocol._createSetOfCTF("_temporary") for mic in setOfMics: micFn = mic.getFileName() micDir = obj._getExtraPath(removeBaseExt(mic.getFileName())) samplingRate = mic.getSamplingRate( ) * self.protocol.ctfDownFactor.get() mic.setSamplingRate(samplingRate) out = self.protocol._getCtfOutPath(micDir) psdFile = self.protocol._getPsdPath(micDir) if exists(out) and exists(psdFile): ctfModel = em.CTFModel() readCtfModel(ctfModel, out, ctf4=self.protocol.useCtffind4.get()) ctfModel.setPsdFile(psdFile) ctfModel.setMicrograph(mic) ctfSet.append(ctfModel) if not ctfSet.isEmpty(): ctfSet.write() ctfSet.close() return ctfSet
def visualizeObjs(obj, setOfMics): if exists(obj._getPath("ctfs_temporary.sqlite")): os.remove(obj._getPath("ctfs_temporary.sqlite")) ctfSet = self.protocol._createSetOfCTF("_temporary") for fn, micDir, mic in iterMicrographs(setOfMics): samplingRate = mic.getSamplingRate( ) * self.protocol.ctfDownFactor.get() mic.setSamplingRate(samplingRate) out = self.protocol._getCtfOutPath(micDir) psdFile = self.protocol._getPsdPath(micDir) if exists(out) and exists(psdFile): ctfModel = em.CTFModel() readCtfModel(ctfModel, out) ctfModel.setPsdFile(psdFile) ctfModel.setMicrograph(mic) ctfSet.append(ctfModel) if ctfSet.getSize() < 1: raise Exception( "Has not been completed the CTF estimation of any micrograph" ) else: ctfSet.write() ctfSet.close() self._visualize(ctfSet)
def _createNewCtfModel(self, mic): micDir = self._getMicrographDir(mic) out = self._getCtfOutPath(micDir) psdFile = self._getPsdPath(micDir) ctfModel2 = em.CTFModel() readCtfModel(ctfModel2, out, ctf4=self.useCtffind4.get()) ctfModel2.setPsdFile(psdFile) ctfModel2.setMicrograph(mic) return ctfModel2
def parseOutputAsCtf(self, filename, psdFile=None): """ Parse the output file and build the CTFModel object with the values. """ ctf = pwem.CTFModel() convert.readCtfModel(ctf, filename, ctf4=True) if psdFile: ctf.setPsdFile(psdFile) return ctf
def rowToCtfModel(ctfRow): """ Create a CTFModel from a row of a meta """ if ctfRow.containsAll(CTF_DICT): ctfModel = em.CTFModel() rowToObject(ctfRow, ctfModel, CTF_DICT, extraLabels=CTF_EXTRA_LABELS) ctfModel.standardize() setPsdFiles(ctfModel, ctfRow) else: ctfModel = None return ctfModel
def rowToCtfModel(ctfRow): """ Create a CTFModel from a row of a meta """ if ctfRow.containsAll(CTF_DICT): ctfModel = em.CTFModel() rowToObject(ctfRow, ctfModel, CTF_DICT, extraLabels=CTF_EXTRA_LABELS) if ctfRow.hasLabel(md.RLN_CTF_PHASESHIFT): ctfModel.setPhaseShift(ctfRow.getValue(md.RLN_CTF_PHASESHIFT, 0)) ctfModel.standardize() setPsdFiles(ctfModel, ctfRow) else: ctfModel = None return ctfModel
def _createCtfModel(self, mic, updateSampling=True): # When downsample option is used, we need to update the # sampling rate of the micrograph associated with the CTF # since it could be downsampled if updateSampling: newSampling = mic.getSamplingRate() * self.ctfDownFactor.get() mic.setSamplingRate(newSampling) micDir = self._getMicrographDir(mic) out = self._getCtfOutPath(micDir) psdFile = self._getPsdPath(micDir) ctfModel2 = em.CTFModel() readCtfModel(ctfModel2, out) ctfModel2.setPsdFile(psdFile) ctfModel2.setMicrograph(mic) return ctfModel2
def _createOutputStep(self): ctfSet = self._createSetOfCTF() ctfSet.setMicrographs(self.inputMics) defocusList = [] for _, micDir, mic in self._iterMicrographs(): samplingRate = mic.getSamplingRate() * self.ctfDownFactor.get() mic.setSamplingRate(samplingRate) psdFile = self._getPsdPath(micDir) out = self._getCtfOutPath(micDir) ctfModel = em.CTFModel() readCtfModel(ctfModel, out, ctf4=self.useCtffind4.get()) ctfModel.setPsdFile(psdFile) ctfModel.setMicrograph(mic) defocusList.append(ctfModel.getDefocusU()) defocusList.append(ctfModel.getDefocusV()) ctfSet.append(ctfModel) self._defocusMaxMin(defocusList) self._defineOutputs(outputCTF=ctfSet) self._defineCtfRelation(self.inputMics, ctfSet)
def _getCTFModel(self, defocusU, defocusV, defocusAngle, psdFile): ctf = em.CTFModel() ctf.setStandardDefocus(defocusU, defocusV, defocusAngle) ctf.setPsdFile(psdFile) return ctf
def createAnalyzeFilesStep(self): """ This method will create two sqlite files that will be used in by the viewer in the 'Analyze result' action. ctfSet: a table with average ctf values between all methods. ctfSetPair: a table where each row is a pair between two methods """ ctfSetFn, ctfSetPairFn = self._getAnalyzeFiles() cleanPath(ctfSetFn, ctfSetPairFn) ctfSet = Set(filename=ctfSetFn) ctfSetPair = em.SetOfCTF(filename=ctfSetPairFn) #import pdb #pdb.set_trace() minimumResolution = {} maximumResolution = {} averageResolution = {} averageDefocusU = {} averageDefocusV = {} averageDefocusAngle = {} for ctf in self.setOfCTF: micFileName = self._getMicName(ctf) minimumResolution[micFileName] = 0. maximumResolution[micFileName] = 0. averageResolution[micFileName] = 0. averageDefocusU[micFileName] = 0. averageDefocusV[micFileName] = 0. averageDefocusAngle[micFileName] = 0. for method1, method2, ctfId in self._freqResol: ctf = em.CTFModel() ctf1 = self.inputCTFs[method1].get()[ctfId] ctf2 = self.inputCTFs[method2].get()[ctfId] ctf.setDefocusU((ctf1.getDefocusU() + ctf2.getDefocusU()) / 2.) ctf.setDefocusV((ctf1.getDefocusV() + ctf2.getDefocusV()) / 2.) ctf.setDefocusAngle( (ctf1.getDefocusAngle() + ctf2.getDefocusAngle()) / 2.) ctf.setMicrograph(ctf1.getMicrograph()) #Clean objId since we can have ctf from the same micrograph # and by default it is set to micrograph id ctf.cleanObjId() resolution = self._freqResol[(method1, method2, ctfId)] ctf.resolution = Float(resolution) ctf.method1 = String(self.methodNames[method1]) ctf.method2 = String(self.methodNames[method2]) # save the values of defocus for each micrograph in a list ctfSetPair.append(ctf) micFileName = self._getMicName(ctf1) if (not micFileName in minimumResolution or not micFileName in maximumResolution): pass if resolution < minimumResolution[micFileName]: minimumResolution[micFileName] = resolution if resolution > maximumResolution[micFileName]: maximumResolution[micFileName] = resolution averageResolution[micFileName] += resolution averageDefocusU[micFileName] += ctf.getDefocusU() averageDefocusV[micFileName] += ctf.getDefocusV() averageDefocusAngle[micFileName] += ctf.getDefocusAngle() size = float(len(self.setOfCTF)) for ctf in self.setOfCTF: ctfAvg = Object() micFileName = self._getMicName(ctf) ctfAvg._micObj = ctf.getMicrograph() ctfAvg.averageDefocusU = Float(averageDefocusU[micFileName] / size) ctfAvg.averageDefocusV = Float(averageDefocusV[micFileName] / size) ctfAvg.averageDefocusAngle = Float( averageDefocusAngle[micFileName] / size) ctfAvg.averageResolution = Float(averageResolution[micFileName] / size) ctfSet.append(ctfAvg) ctfSetPair.write() ctfSet.write()