def _appendRctImages(self, particles): blockMd = "class%06d_images@%s" % (particles.getObjId(), self.rctClassesFn) classMd = emlib.MetaData() partPairs = self.inputParticlesTiltPair.get() uImages = partPairs.getUntilted() tImages = partPairs.getTilted() sangles = partPairs.getCoordsPair().getAngles() micPairs = partPairs.getCoordsPair().getMicsPair() uMics = micPairs.getUntilted() tMics = micPairs.getTilted() scaleFactor = uImages.getSamplingRate() / particles.getSamplingRate() for img in particles: imgId = img.getObjId() uImg = uImages[imgId] tImg = tImages[imgId] if uImg is None or tImg is None: print(">>> Warning, for id %d, tilted or untilted particle " "was not found. Ignored." % imgId) else: objId = classMd.addObject() pairRow = emlib.metadata.Row() pairRow.setValue(emlib.MDL_IMAGE, getImageLocation(uImg)) uCoord = uImg.getCoordinate() micId = uCoord.getMicId() uMic = uMics[micId] angles = sangles[micId] pairRow.setValue(emlib.MDL_MICROGRAPH, uMic.getFileName()) pairRow.setValue(emlib.MDL_XCOOR, uCoord.getX()) pairRow.setValue(emlib.MDL_YCOOR, uCoord.getY()) pairRow.setValue(emlib.MDL_ENABLED, 1) pairRow.setValue(emlib.MDL_ITEM_ID, int(imgId)) pairRow.setValue(emlib.MDL_REF, 1) alignment = img.getTransform() # Scale alignment by scaleFactor alignment.scale(scaleFactor) alignmentToRow(alignment, pairRow, alignType=ALIGN_2D) pairRow.setValue(emlib.MDL_IMAGE_TILTED, getImageLocation(tImg)) tMic = tMics[micId] pairRow.setValue(emlib.MDL_MICROGRAPH_TILTED, tMic.getFileName()) (angleY, angleY2, angleTilt) = angles.getAngles() pairRow.setValue(emlib.MDL_ANGLE_Y, float(angleY)) pairRow.setValue(emlib.MDL_ANGLE_Y2, float(angleY2)) pairRow.setValue(emlib.MDL_ANGLE_TILT, float(angleTilt)) pairRow.writeToMd(classMd, objId) classMd.write(blockMd, emlib.MD_APPEND)
def _insertAllSteps(self): """Insert all steps to calculate the resolution of a 3D reconstruction. """ self.inputVol = getImageLocation(self.inputVolume.get()) if self.referenceVolume.hasValue(): self.refVol = getImageLocation(self.referenceVolume.get()) if self.doFSC: self._insertFunctionStep('calculateFscStep') if self.doComputeBfactor: self._insertFunctionStep('computeBfactorStep') self._insertFunctionStep('createOutputStep') self._insertFunctionStep('createSummaryStep')
def performNmaStep(self, atomsFn, modesFn): sampling = self.inputParticles.get().getSamplingRate() discreteAngularSampling = self.discreteAngularSampling.get() trustRegionScale = self.trustRegionScale.get() odir = self._getTmpPath() imgFn = self.imgsFn args = "-i %(imgFn)s --pdb %(atomsFn)s --modes %(modesFn)s --sampling_rate %(sampling)f " args += "--discrAngStep %(discreteAngularSampling)f --odir %(odir)s --centerPDB " args += "--trustradius_scale %(trustRegionScale)d --resume " if self.getInputPdb().getPseudoAtoms(): args += "--fixed_Gaussian " if self.alignmentMethod == NMA_ALIGNMENT_PROJ: args += "--projMatch " self.runJob("xmipp_nma_alignment", args % locals()) cleanPath(self._getPath('nmaTodo.xmd')) inputSet = self.inputParticles.get() mdImgs = md.MetaData(self.imgsFn) for objId in mdImgs: imgPath = mdImgs.getValue(md.MDL_IMAGE, objId) index, fn = xmippToLocation(imgPath) # Conside the index is the id in the input set particle = inputSet[index] mdImgs.setValue(md.MDL_IMAGE, getImageLocation(particle), objId) mdImgs.setValue(md.MDL_ITEM_ID, long(particle.getObjId()), objId) mdImgs.write(self.imgsFn)
def _applyBfactor(self, e=None): bFactorFile = self.protocol._getPath('bfactor.txt') f = open(bFactorFile) values = map(float, f.readline().split()) f.close() self.protocol.setStepsExecutor() # set default execution vol = self.protocol.inputVolume.get() volPath = getImageLocation(vol) maxres = 1. / sqrt(values[2]) args = '-i %s ' % volPath pixelSize = vol.getSamplingRate() args += '--sampling %f ' % pixelSize args += '--maxres %f ' % maxres if (exists(self.protocol._getPath('fsc.xmd'))): args += '--fsc %s ' % self.protocol._getPath('fsc.xmd') args += '--adhoc %f ' % -values[4] volName = os.path.basename(volPath) volOut = self.protocol._getPath(volName) args += '-o %s ' % volOut self.protocol.runJob('xmipp_volume_correct_bfactor', args) #args = '-i %s -o %s' % (volPath, self.protocol._getPath(volName)) #self.protocol.runJob('xmipp_image_convert', args) volSet = self.protocol._createSetOfVolumes() volSet.setSamplingRate(pixelSize) newVol = vol.clone() newVol.setObjId(None) newVol.setLocation(volOut) volSet.append(newVol) volSet.write() self.objectView(volSet).show()
def createMaskFromVolumeStep(self): volume = self.inputVolume.get() fnVol = getImageLocation(volume) Ts = volume.getSamplingRate() if self.volumeOperation == OPERATION_THRESHOLD: self.runJob("xmipp_transform_threshold", "-i %s -o %s --select below %f --substitute binarize" % (fnVol, self.maskFile, self.threshold.get())) elif self.volumeOperation == OPERATION_SEGMENT: args="-i %s -o %s --method " % (fnVol, self.maskFile) if self.segmentationType == SEGMENTATION_VOXELS: args += "voxel_mass %d" % (self.nvoxels.get()) elif self.segmentationType == SEGMENTATION_AMINOACIDS: args += "aa_mass %d %f" % (self.naminoacids.get(), Ts) elif self.segmentationType == SEGMENTATION_DALTON: args += "dalton_mass %d %f" % (self.dalton.get(), Ts) else: args += "otsu" self.runJob("xmipp_volume_segment", args) elif self.volumeOperation == OPERATION_POSTPROCESS: ImageHandler().convert(fnVol,self.maskFile) return [self.maskFile]
def _insertProcessStep(self): inputFn = self.inputFn if self.source == SOURCE_MASK: inputMaskFile = getImageLocation(self.inputMask.get()) outputMaskFile = self._getTmpPath(self.MASK_FILE) self._insertFunctionStep('copyMaskFileStep', inputMaskFile, outputMaskFile) if self.fillType == MASK_FILL_VALUE: fillValue = self.fillValue.get() else: fillValue = self.getEnumText('fillType') if self.source == SOURCE_GEOMETRY: self._program = "xmipp_transform_mask" self._args += self._getGeometryCommand() self._args += " --substitute %(fillValue)s " elif self.source == SOURCE_MASK: self._program = "xmipp_image_operate" self._args += (" --mult %s" % outputMaskFile) else: raise Exception("Unrecognized mask type: %d" % self.source) self._insertFunctionStep("maskStep", self._args % locals())
def _insertAllSteps(self): inputVol = self.inputStructure.get() fnIn = getImageLocation(inputVol) fnMask = self._insertMaskStep(fnIn) self._insertFunctionStep('convertToPseudoAtomsStep', fnIn, fnMask, inputVol.getSamplingRate()) self._insertFunctionStep('createOutputStep')
def _insertAllSteps(self): #divide work in several threads numberOfThreads = self.numberOfThreads.get()-1 if numberOfThreads == 0: numberOfThreads = 1 partSet = self.inputParticles.get() nPart = len(partSet) numberOfThreads = min(numberOfThreads, nPart) samplingRate = partSet.getSamplingRate() inputVolume = getImageLocation(self.inputVolume.get()) mdFn = self._getInputParticlesFn() convertId = self._insertFunctionStep('convertInputStep', inputVolume) deps = [convertId] # Create xmipp metadata # partSet #writeSetOfParticles(partSet, mdFn, blockName="images") groupSize = nPart/numberOfThreads groupRemainder = nPart%numberOfThreads depsOutPut=[] for thread in range(0, numberOfThreads): start = long(thread * groupSize+1) end = long(thread * groupSize+groupSize) if thread == (numberOfThreads-1): end += groupRemainder idStep = self._insertFunctionStep('projectStep', start, end, samplingRate, thread, prerequisites=deps) depsOutPut.append(idStep) self._insertFunctionStep('createOutputStep', prerequisites=depsOutPut)
def _defineFilenames(self): """ Prepare the files to process """ if self._isSingleInput(): self.inputFn = getImageLocation(self.inputVolumes.get()) self.outputStk = self._getExtraPath("output_volume.mrc") else: self.inputFn = self._getTmpPath('input_volumes.xmd') self.outputStk = self._getExtraPath("output_volumes.stk") self.outputMd = self._getExtraPath('output_volumes.xmd')
def _insertAllSteps(self): self._insertFunctionStep('copyInput') self._insertFunctionStep('coarseSearch') self._insertFunctionStep('fineSearch') self._insertFunctionStep('symmetrize') self._insertFunctionStep('createOutput') self.fnVol = getImageLocation(self.inputVolume.get()) self.fnVolSym = self._getPath('volume_symmetrized.vol') [self.height, _, _] = self.inputVolume.get().getDim()
def convertInputStep(self, inputParticlesId): fnDir=self._getExtraPath() writeSetOfParticles(self.inputParticles.get(),self.imgsFn) # Choose the target sampling rate TsOrig=self.inputParticles.get().getSamplingRate() TsCurrent=max(TsOrig,self.targetResolution.get()/3) Xdim=self.inputParticles.get().getDimensions()[0] newXdim=long(round(Xdim*TsOrig/TsCurrent)) if newXdim<40: newXdim=long(40) TsCurrent=Xdim*(TsOrig/newXdim) print "Preparing images to sampling rate=",TsCurrent self.writeInfoField(fnDir,"size",xmippLib.MDL_XSIZE,newXdim) self.writeInfoField(fnDir,"sampling",xmippLib.MDL_SAMPLINGRATE,TsCurrent) # Prepare particles fnNewParticles=join(fnDir,"images.stk") if newXdim!=Xdim: self.runJob("xmipp_image_resize","-i %s -o %s --fourier %d"%(self.imgsFn,fnNewParticles,newXdim), numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get()) else: self.runJob("xmipp_image_convert","-i %s -o %s --save_metadata_stack %s"%(self.imgsFn,fnNewParticles,join(fnDir,"images.xmd")), numberOfMpi=1) R=self.particleRadius.get() if R<=0: R=self.inputParticles.get().getDimensions()[0]/2 R=min(round(R*TsOrig/TsCurrent*1.1),newXdim/2) self.runJob("xmipp_transform_mask","-i %s --mask circular -%d"%(fnNewParticles,R),numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get()) # Prepare mask imgHandler=ImageHandler() if self.nextMask.hasValue(): self.convertInputVolume(imgHandler, self.nextMask.get(), getImageLocation(self.nextMask.get()), join(fnDir,"mask.vol"), TsCurrent, newXdim) # Prepare references i=0 for vol in self.inputVolumes.get(): fnVol=join(fnDir,"volume%03d.vol"%i) self.convertInputVolume(imgHandler, vol, getImageLocation(vol), fnVol, TsCurrent, newXdim) self.runJob("xmipp_image_operate","-i %s --mult 0 -o %s"%(fnVol,join(fnDir,"volume%03d_speed.vol"%i)),numberOfMpi=1) i+=1 xmippLib.MetaData().write("best@"+self._getExtraPath("swarm.xmd")) # Empty write to guarantee this block is the first one xmippLib.MetaData().write("bestByVolume@"+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND) # Empty write to guarantee this block is the second one
def _insertAllSteps(self): self._insertFunctionStep('copyInput') if self.searchMode.get()!=self.LOCAL_SEARCH: self._insertFunctionStep('coarseSearch') if self.searchMode.get()!=self.GLOBAL_SEARCH: self._insertFunctionStep('fineSearch') self._insertFunctionStep('symmetrize') self._insertFunctionStep('createOutput') self.fnVol = getImageLocation(self.inputVolume.get()) self.fnVolSym=self._getPath('volume_symmetrized.mrc') [self.height,_,_]=self.inputVolume.get().getDim()
def convertInputStep(self, volName): # Read volume and convert to DOUBLE self.vol = emlib.Image(volName) self.vol.convert2DataType(emlib.DT_DOUBLE) # Mask volume if needed if self.refMask.get() is not None: maskName = getImageLocation(self.refMask.get()) self.mask = emlib.Image(maskName) self.mask.convert2DataType(emlib.DT_DOUBLE) self.vol.inplaceMultiply(self.mask) padding = 2 maxFreq = 0.5 splineDegree = 3 ### self.fourierProjectVol = emlib.FourierProjector( self.vol, padding, maxFreq, splineDegree) ### partSet = self.inputParticles.get() nPart = len(partSet) numberOfTasks = min(nPart, max(self.numberOfThreads.get() - 1, 1)) taskSize = int(nPart / numberOfTasks) md = emlib.MetaData() # Convert angles and shifts from volume system of coordinates to # projection system of coordinates mdCount = 0 for index, part in enumerate(partSet): objId = md.addObject() imgRow = emlib.metadata.Row() particleToRow(part, imgRow) shifts, angles = geometryFromMatrix( part.getTransform().getMatrix(), True) imgRow.setValue(emlib.MDL_SHIFT_X, -shifts[0]) imgRow.setValue(emlib.MDL_SHIFT_Y, -shifts[1]) imgRow.setValue(emlib.MDL_SHIFT_Z, 0.) imgRow.setValue(emlib.MDL_ANGLE_ROT, angles[0]) imgRow.setValue(emlib.MDL_ANGLE_TILT, angles[1]) imgRow.setValue(emlib.MDL_ANGLE_PSI, angles[2]) imgRow.writeToMd(md, objId) # Write a new metadata every taskSize number of elements # except in the last chunk where we want to add also the # remainder and the condition is the last element if ((index % taskSize == taskSize - 1 and mdCount < numberOfTasks - 1) or index == nPart - 1): md.write(self._getInputParticlesSubsetFn(mdCount)) md.clear() mdCount += 1 x, y, _ = partSet.getDim() emlib.createEmptyFile(self._getProjGalleryFn(), x, y, 1, nPart)
def createRefMd(self, vols): refVols = self._getExtraPath('ref_volumes.xmd') mdFn = md.MetaData() if self.isSetOfVolumes(): for vol in vols: imgId = vol.getObjId() row = md.Row() row.setValue(md.MDL_ITEM_ID, int(imgId)) row.setValue(md.MDL_IMAGE, getImageLocation(vol)) row.setValue(md.MDL_ENABLED, 1) row.addToMd(mdFn) else: imgId = vols.getObjId() row = md.Row() row.setValue(md.MDL_ITEM_ID, int(imgId)) row.setValue(md.MDL_IMAGE, getImageLocation(vols)) row.setValue(md.MDL_ENABLED, 1) row.addToMd(mdFn) mdFn.write(refVols, md.MD_APPEND)
def _insertMaskStep(self, fnVol, prefix=''): """ Check the mask selected and insert the necessary steps. Return the mask filename if needed. """ fnMask = '' if self.maskMode == NMA_MASK_THRE: fnMask = self._getExtraPath('mask%s.vol' % prefix) maskParams = '-i %s -o %s --select below %f --substitute binarize'\ % (fnVol, fnMask, self.maskThreshold.get()) self._insertRunJobStep('xmipp_transform_threshold', maskParams) elif self.maskMode == NMA_MASK_FILE: fnMask = getImageLocation(self.volumeMask.get()) return fnMask
def copyDeformationsStep(self, deformationMd): copyFile(deformationMd, self.imgsFn) # We need to update the image name with the good ones # and the same with the ids. inputSet = self.inputParticles.get() mdImgs = md.MetaData(self.imgsFn) for objId in mdImgs: imgPath = mdImgs.getValue(md.MDL_IMAGE, objId) index, fn = xmippToLocation(imgPath) # Conside the index is the id in the input set particle = inputSet[index] mdImgs.setValue(md.MDL_IMAGE, getImageLocation(particle), objId) mdImgs.setValue(md.MDL_ITEM_ID, long(particle.getObjId()), objId) mdImgs.write(self.imgsFn)
def _insertAllSteps(self): # Iterate through all input volumes and align them # againt the reference volume refFn = getImageLocation(self.inputReference.get()) maskArgs = self._getMaskArgs() alignArgs = self._getAlignArgs() alignSteps = [] idx = 1 for vol in self._iterInputVolumes(): volFn = getImageLocation(vol) volId = vol.getObjId() stepId = self._insertFunctionStep('alignVolumeStep', refFn, volFn, self._getExtraPath( "vol%02d.mrc" % idx), maskArgs, alignArgs, idx, prerequisites=[]) alignSteps.append(stepId) idx += 1 self._insertFunctionStep('createOutputStep', prerequisites=alignSteps)
def _insertAllSteps(self): convertId = self._insertFunctionStep('convertInputStep', self.inputParticles.get().getObjId()) deps = [] # store volumes steps id to use as dependencies for last step commonParams = self._getCommonParams() commonParamsRef = self._getCommonParamsRef() sym = self.symmetryGroup.get() for i, vol in enumerate(self._iterInputVols()): volName = getImageLocation(vol) volDir = self._getVolDir(i + 1) pmStepId = self._insertFunctionStep('projectionLibraryStep', volName, volDir, self.angularSampling.get(), prerequisites=[convertId]) sigStepId1 = self._insertFunctionStep('significantStep', volName, volDir, 'exp_particles.xmd', commonParams, prerequisites=[pmStepId]) phanProjStepId = self._insertFunctionStep('phantomProject', prerequisites=[ sigStepId1]) sigStepId2 = self._insertFunctionStep('significantStep', volName, volDir, 'ref_particles.xmd', commonParamsRef, prerequisites=[ phanProjStepId]) if (not (self.pseudoSymmetryGroup.get() == '')): sym = self.pseudoSymmetryGroup.get() volStepId = self._insertFunctionStep('alignabilityStep', volName, volDir, sym, prerequisites=[sigStepId2]) deps.append(volStepId) self._insertFunctionStep('createOutputStep', prerequisites=deps)
def createChimeraScript(self, volume, pdb): """ Create a chimera script to visualize a pseudoatoms pdb obteined from a given EM 3d volume. A property will be set in the pdb object to store the location of the script. """ pseudoatoms = pdb.getFileName() scriptFile = pseudoatoms + '_chimera.cmd' pdb._chimeraScript = String(scriptFile) sampling = volume.getSamplingRate() radius = sampling * self.pseudoAtomRadius.get() fnIn = getImageLocation(volume) if fnIn.endswith(":mrc"): fnIn = fnIn[:-4] x, y, z = volume.getOrigin(force=True).getShifts() xx, yy, zz = volume.getDim() dim = volume.getDim()[0] bildFileName = os.path.abspath(self._getExtraPath("axis.bild")) Chimera.createCoordinateAxisFile(dim, bildFileName=bildFileName, sampling=sampling) fhCmd = open(scriptFile, 'w') fhCmd.write("open %s\n" % basename(pseudoatoms)) fhCmd.write("rangecol bfactor,a 0 white 1 red\n") fhCmd.write("setattr a radius %f\n" % radius) fhCmd.write("represent sphere\n") fhCmd.write("open %s\n" % abspath(fnIn)) threshold = 0.01 if self.maskMode == NMA_MASK_THRE: self.maskThreshold.get() # set sampling fhCmd.write("volume #1 level %f transparency 0.5 voxelSize %f origin " "%0.2f,%0.2f,%0.2f\n" % (threshold, sampling, x, y, z)) fhCmd.write("open %s\n" % bildFileName) #fhCmd.write("move %0.2f,%0.2f,%0.2f model #0 coord #2\n" # % ((xx / 2. * sampling) - xv, # (yy / 2. * sampling) - yv, # (zz / 2. * sampling) - zv)) fhCmd.write("move %0.2f,%0.2f,%0.2f model #0 coord #2\n" % (x + (xx / 2. * sampling), y + (yy / 2. * sampling), z + (zz / 2. * sampling))) fhCmd.close()
def _reconstructImages(self, particles, deps): """ Function to insert the step needed to reconstruct a class (or setOfParticles) """ classNo = particles.getObjId() blockMd = "class%06d_images@%s" % (classNo, self.rctClassesFn) classNameIn = blockMd classNameOut = self._getExtraPath("rct_images_%06d.xmd" % classNo) classVolumeOut = self._getPath("rct_%06d.vol" % classNo) if particles.hasRepresentative(): classImage = getImageLocation(particles.getRepresentative()) else: classImage = None reconStep = self._insertFunctionStep('reconstructClass', classNameIn, classNameOut, classImage, classVolumeOut, prerequisites=[deps]) return reconStep
def _insertAllSteps(self): """ Mainly prepare the command line for call cl2d align program""" # Convert input images if necessary self.imgsFn = self._getExtraPath('images.xmd') self._insertFunctionStep('convertInputStep') # Prepare arguments to call program: xmipp_classify_CL2D self._params = {'imgsFn': self.imgsFn, 'extraDir': self._getExtraPath(), 'maxshift': self.maximumShift.get(), 'iter': self.numberOfIterations.get(), } args = ('-i %(imgsFn)s --odir %(extraDir)s --nref 1 --iter %(iter)d ' '--maxShift %(maxshift)d') if self.useReferenceImage: args += " --ref0 " + getImageLocation(self.referenceImage.get()) else: args += " --nref0 1" self._insertRunJobStep("xmipp_classify_CL2D", args % self._params) self._insertFunctionStep('createOutputStep')
def _getSecondVolumeFn(self): if self._isSingleInput(): return getImageLocation(self.inputVolumes2.get()) else: return self._getSecondSetFn()
def _insertAllSteps(self): cutoffStr = '' if self.cutoffMode == NMA_CUTOFF_REL: cutoffStr = 'Relative %f' % self.rcPercentage.get() else: cutoffStr = 'Absolute %f' % self.rc.get() #alignArgs = self._getAlignArgs() self.alignmentAlgorithm = 1 # Local alignment volList = [vol.clone() for vol in self._iterInputVolumes()] nVoli = 1 for voli in volList: fnIn = getImageLocation(voli) fnMask = self._insertMaskStep(fnIn) suffix = "_%d" % nVoli self._insertFunctionStep('convertToPseudoAtomsStep', fnIn, fnMask, voli.getSamplingRate(), suffix) fnPseudoAtoms = self._getPath("pseudoatoms_%d.pdb" % nVoli) self._insertFunctionStep('computeModesStep', fnPseudoAtoms, self.numberOfModes, cutoffStr) self._insertFunctionStep('reformatOutputStep', os.path.basename(fnPseudoAtoms)) stepQualify = self._insertFunctionStep( 'qualifyModesStep', self.numberOfModes, self.collectivityThreshold.get(), self._getPath("pseudoatoms_%d.pdb" % nVoli), suffix) #rigid alignment nVolj = 1 deps = [] for volj in volList: if nVolj != nVoli: inVolFn = getImageLocation(volj) if self.rigidAlignment: refFn = getImageLocation(voli) volId = volj.getObjId() outVolFn = self._getPath( 'outputRigidAlignment_vol_%d_to_%d.vol' % (nVolj, nVoli)) stepId = self._insertFunctionStep( 'alignVolumeStep', refFn, inVolFn, outVolFn, volId, prerequisites=[stepQualify]) else: outVolFn = inVolFn stepId = stepQualify deps.append( self._insertFunctionStep('elasticAlignmentStep', nVoli, voli.getSamplingRate(), nVolj, inVolFn, prerequisites=[stepId])) nVolj += 1 self._insertFunctionStep( 'gatherSingleVolumeStep', prerequisites=deps ) # This is a synchronization step, does not do any real work nVoli += 1 self._insertFunctionStep('gatherResultsStep') self._insertFunctionStep('managingOutputFilesStep')