def parseOutputAsCtf(self, ctfFile, psdFile=None): ctf = CTFModel() readCtfModel(ctf, ctfFile) if psdFile: ctf.setPsdFile(psdFile) return ctf
def createSetOfParticles(self, setPartSqliteName, partFn, doCtf=False): # create a set of particles self.partSet = SetOfParticles(filename=setPartSqliteName) self.partSet.setAlignment(ALIGN_PROJ) self.partSet.setAcquisition( Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000)) self.partSet.setSamplingRate(samplingRate) self.partSet.setHasCTF(True) aList = [np.array(m) for m in mList] #defocus=15000 + 5000* random.random() for i, a in enumerate(aList): p = Particle() if doCtf: defocusU = defocusList[i] #+500. defocusV = defocusList[i] ctf = CTFModel(defocusU=defocusU, defocusV=defocusV, defocusAngle=defocusAngle[i]) ctf.standardize() p.setCTF(ctf) p.setLocation(i + 1, partFn) p.setTransform(Transform(a)) self.partSet.append(p) self.partSet.write()
def parseOutputAsCtf(self, filename, psdFile=None): """ Parse the output file and build the CTFModel object with the values. :param filename: input file to parse :param psdFile: if defined, set PSD for the CTF model """ ctf = CTFModel() readCtfModel(ctf, filename) if psdFile: ctf.setPsdFile(psdFile) return ctf
def createOutputStep(self): if self.predictEnable: self.ctfResults = self._createSetOfCTF() for i, img in enumerate(self.imgSet): ctf = CTFModel() ctf.setResolution(self.results[i][3]) ctf.setMicrograph(img) ctf.setPsdFile(self.psd_list[i]) ctf.setStandardDefocus(self.results[i][0], self.results[i][1], self.results[i][2]) self.ctfResults.append(ctf) self._defineOutputs(ctfResults=self.ctfResults)
def getCTFList(start=1, end=3): """ Mocks a CTF dict""" newCtfDict = {} for index in range(start, end + 1): ctf = CTFModel() mic = Micrograph() mic.setMicName("mic%s" % index) ctf.setMicrograph(mic) newCtfDict[mic.getMicName()] = ctf return newCtfDict
def _getCTFModel(self, defocusU, defocusV, defocusAngle, resol, psdFile): ctf = CTFModel() ctf.setStandardDefocus(defocusU, defocusV, defocusAngle) ctf.setResolution(resol) ctf.setPsdFile(psdFile) return ctf
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. :param inputSet: input set of particles :param outputSet: output set of particles to be populated :param parFileName: Frealign par file to read alignments """ # 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 zip(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(CTFModel()) rowToCtfModel(row, particle.getCTF()) outputSet.append(particle) outputSet.setAlignment(ALIGN_PROJ)
def _createSetOfMics(self, n=10, nOptics=2): micName = 'BPV_13%02d.mrc' psdName = 'BPV_13%02d_PSD.ctf:mrc' ogName = 'opticsGroup%d' mtfFile = 'mtfFile%d.star' cleanPath(self.getOutputPath('micrographs.sqlite')) micsDb = self.getOutputPath('micrographs.sqlite') outputMics = SetOfMicrographs(filename=micsDb) outputMics.setSamplingRate(1.234) mic = SetOfMicrographs.ITEM_TYPE() acq = Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000) og = OpticsGroups.create(rlnMtfFileName='') fog = og.first() ctf = CTFModel(defocusU=10000, defocusV=15000, defocusAngle=15) outputMics.setAcquisition(acq) mic.setAcquisition(acq) mic.setCTF(ctf) itemsPerOptics = n // nOptics for i in range(1, n + 1): mic.setFileName(micName % i) ctf = mic.getCTF() ctf.setPsdFile(psdName % i) ctf.setFitQuality(np.random.uniform()) ctf.setResolution(np.random.uniform(3, 15)) ogNumber = (i - 1) // itemsPerOptics + 1 ogDict = { 'rlnOpticsGroup': ogNumber, 'rlnOpticsGroupName': ogName % ogNumber, 'rlnMtfFileName': mtfFile % ogNumber } if ogNumber in og: og.update(ogNumber, **ogDict) else: og.add(fog._replace(**ogDict)) mic.rlnOpticsGroup = ogNumber mic.setObjId(None) outputMics.append(mic) print(">>> Writing micrograph set to: ", micsDb) outputMics.write() return outputMics
def readSetOfParticles(self, starFile, partSet, **kwargs): """ Convert a star file into a set of particles. Params: starFile: the filename of the star file partsSet: output particles set Keyword Arguments: blockName: The name of the data block (default particles) alignType: alignment type removeDisabled: Remove disabled items """ self._preprocessImageRow = kwargs.get('preprocessImageRow', None) self._alignType = kwargs.get('alignType', ALIGN_NONE) self._postprocessImageRow = kwargs.get('postprocessImageRow', None) self._optics = OpticsGroups.fromStar(starFile) self._pixelSize = getattr(self._optics.first(), 'rlnImagePixelSize', 1.0) self._invPixelSize = 1. / self._pixelSize partsReader = Table.Reader(starFile, tableName='particles') firstRow = partsReader.getRow() self._setClassId = hasattr(firstRow, 'rlnClassNumber') self._setCtf = partsReader.hasAllColumns(self.CTF_LABELS[:3]) particle = Particle() if self._setCtf: particle.setCTF(CTFModel()) self._setAcq = kwargs.get("readAcquisition", True) acq = Acquisition() acq.setMagnification(kwargs.get('magnification', 10000)) extraLabels = kwargs.get('extraLabels', []) + PARTICLE_EXTRA_LABELS self.createExtraLabels(particle, firstRow, extraLabels) self._rowToPart(firstRow, particle) partSet.setSamplingRate(self._pixelSize) partSet.setAcquisition(acq) self._optics.toImages(partSet) partSet.append(particle) for row in partsReader: self._rowToPart(row, particle) partSet.append(particle) partSet.setHasCTF(self._setCtf) partSet.setAlignment(self._alignType)
def simulateStep(self): n = 1 fnStk = self._getPath("images.mrc") Ts = self.inputParticles.get().getSamplingRate() imgSetOut = self._createSetOfParticles() imgSetOut.copyInfo(self.inputParticles.get()) imgSetOut.setHasCTF(True) acquisition = imgSetOut.getAcquisition() acquisition.setVoltage(self.voltage.get()) acquisition.setAmplitudeContrast(self.Q0.get()) acquisition.setSphericalAberration(self.cs.get()) acquisition.setMagnification(1) for particle in self.inputParticles.get(): location = particle.getLocation() fnIn = str(location[0]) + "@" + location[1] fnOut = str(n) + "@" + fnStk defocus = random.uniform(self.Defocus0.get(), self.DefocusF.get()) args = "-i %s -o %s" % (fnIn, fnOut) args += " --fourier ctfdef %f %f %f %f --sampling %f -v 0" % ( self.voltage, self.cs, self.Q0, defocus, Ts) self.runJob("xmipp_transform_filter", args) newCTF = CTFModel() newCTF.setDefocusU(defocus) newCTF.setDefocusV(defocus) newCTF.setDefocusAngle(0.0) newParticle = particle.clone() newParticle.setLocation((n, fnStk)) acquisition = newParticle.getAcquisition() acquisition.setVoltage(self.voltage.get()) acquisition.setAmplitudeContrast(self.Q0.get()) acquisition.setSphericalAberration(self.cs.get()) acquisition.setMagnification(1) newParticle.setCTF(newCTF) imgSetOut.append(newParticle) n += 1 self._defineOutputs(outputParticles=imgSetOut) self._defineSourceRelation(self.inputParticles.get(), imgSetOut)
def importCTF(self, mic, fileName): ctf = CTFModel() ctf.setMicrograph(mic) readCtfModel(ctf, fileName) # Try to find the given PSD file associated with the cttfind log file # we handle special cases of .ctf extension and _ctffindX prefix for Relion runs fnBase = pwutils.removeExt(fileName) for suffix in ['_psd.mrc', '.mrc', '.ctf']: psdPrefixes = [fnBase, fnBase.replace('_ctffind3', ''), fnBase.replace('_gctf', '')] for prefix in psdPrefixes: psdFile = prefix + suffix if os.path.exists(psdFile): if psdFile.endswith('.ctf'): psdFile += ':mrc' ctf.setPsdFile(psdFile) return ctf
def importCTF(self, mic, fileName): """ Create a CTF model and populate its values. :param mic: input micrograph object :param fileName: input file to be parsed :return: CTFModel object """ ctf = CTFModel() ctf.setMicrograph(mic) readCtfModel(ctf, fileName) # Try to find the given PSD file associated with the cttfind log file # we handle special cases of .ctf extension and _ctffind4 prefix for Relion runs fnBase = pwutils.removeExt(fileName) for suffix in ['_psd.mrc', '.mrc', '.ctf']: psdPrefixes = [fnBase, fnBase.replace('_ctffind4', '')] for prefix in psdPrefixes: psdFile = prefix + suffix if pwutils.exists(psdFile): if psdFile.endswith('.ctf'): psdFile += ':mrc' ctf.setPsdFile(psdFile) return ctf return ctf
def _updateCtf(j, ts, ti, tsOut, tiOut): tiOut.setCTF(CTFModel(defocusU=j * 1000, defocusAngle=j / 2.0))