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 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 readPartsFromMics(self, micList, outputParts): """ Read the particles extract for the given list of micrographs and update the outputParts set with new items. """ relionToLocation = relion.convert.relionToLocation p = Particle() p._rlnOpticsGroup = Integer() acq = self.getInputMicrographs().getAcquisition() # JMRT: Ideally I would like to disable the whole Acquisition for each # particle row, but the SetOfImages will set it again. # Another option could be to disable in the set, but then in # streaming, other protocols might get the wrong optics info pAcq = Acquisition(magnification=acq.getMagnification(), voltage=acq.getVoltage(), amplitudeContrast=acq.getAmplitudeContrast(), sphericalAberration=acq.getSphericalAberration()) p.setAcquisition(pAcq) tmp = self._getTmpPath() extra = self._getExtraPath() for mic in micList: posSet = set() coordDict = {self._getPos(c): c for c in self.coordDict[mic.getObjId()]} del self.coordDict[mic.getObjId()] ogNumber = mic.getAttributeValue('_rlnOpticsGroup', 1) partsStar = self.__getMicFile(mic, '_extract.star', folder=tmp) partsTable = relion.convert.Table(fileName=partsStar) stackFile = self.__getMicFile(mic, '.mrcs', folder=tmp) endStackFile = self.__getMicFile(mic, '.mrcs', folder=extra) pwutils.moveFile(stackFile, endStackFile) for part in partsTable: pos = (int(float(part.rlnCoordinateX)), int(float(part.rlnCoordinateY))) if pos in posSet: print("Duplicate coordinate at: %s, IGNORED. " % str(pos)) coord = None else: coord = coordDict.get(pos, None) if coord is not None: # scale the coordinates according to particles dimension. coord.scale(self.getBoxScale()) p.copyObjId(coord) idx, fn = relionToLocation(part.rlnImageName) p.setLocation(idx, endStackFile) p.setCoordinate(coord) p.setMicId(mic.getObjId()) p.setCTF(mic.getCTF()) p._rlnOpticsGroup.set(ogNumber) outputParts.append(p) posSet.add(pos)
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 generateAtlasStep(self, movieDict): # Movie is a dictionary, convert it to a proper movie object movie = Movie() movie.setAcquisition(Acquisition()) movie.setAttributesFromDict(movieDict, setBasic=True, ignoreMissing=True) # Generate the output outputLocations = self._getOutputSet() al = self._getAtlasInfo(movie) if al is not None: outputLocations.append(al) outputLocations.write() self._store()
def launchTest(self, fileKey, mList, alignType=None, **kwargs): """ Helper function to launch similar alignment tests given the EMX transformation matrix. Params: fileKey: the file where to grab the input stack images. mList: the matrix list of transformations (should be the same length of the stack of images) """ print("\n") print("*" * 80) print("* Launching test: ", fileKey) print("*" * 80) is2D = alignType == ALIGN_2D if fileKey == 'alignShiftRotExp': # relion requires mrcs stacks origFn = self.dataset.getFile(fileKey) stackFn = replaceExt(origFn, ".mrcs") createLink(origFn, stackFn) else: stackFn = self.dataset.getFile(fileKey) partFn1 = self.getOutputPath(fileKey + "_particles1.sqlite") mdFn = self.getOutputPath(fileKey + "_particles.star") partFn2 = self.getOutputPath(fileKey + "_particles2.sqlite") if self.IS_ALIGNMENT: outputFn = self.getOutputPath(fileKey + "_output.mrcs") outputFnRelion = self.getOutputPath(fileKey + "_output") goldFn = self.dataset.getFile(fileKey + '_Gold_output_relion.mrcs') else: outputFn = self.getOutputPath(fileKey + "_output.vol") goldFn = self.dataset.getFile("reconstruction/gold/" + fileKey + '_Gold_rln_output.vol') if PRINT_FILES: print("BINARY DATA: ", stackFn) print("SET1: ", partFn1) print(" MD: ", mdFn) print("SET2: ", partFn2) print("OUTPUT: ", outputFn) print("GOLD: ", goldFn) if alignType == ALIGN_2D or alignType == ALIGN_PROJ: partSet = SetOfParticles(filename=partFn1) else: partSet = SetOfVolumes(filename=partFn1) partSet.setAlignment(alignType) acq = Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000) og = OpticsGroups.create(rlnMtfFileName="mtfFile1.star", rlnImageSize=128) partSet.setSamplingRate(1.0) partSet.setAcquisition(acq) og.toImages(partSet) # Populate the SetOfParticles with images # taken from images.mrc file # and setting the previous alignment parameters aList = [np.array(m) for m in mList] for i, a in enumerate(aList): p = Particle() p.setLocation(i + 1, stackFn) p.setTransform(Transform(a)) partSet.append(p) # Write out the .sqlite file and check that are correctly aligned print("Partset", partFn1) partSet.printAll() partSet.write() # Convert to a Xmipp metadata and also check that the images are # aligned correctly if alignType == ALIGN_2D or alignType == ALIGN_PROJ: starWriter = convert.createWriter() starWriter.writeSetOfParticles(partSet, mdFn, alignType=alignType) partSet2 = SetOfParticles(filename=partFn2) else: convert.writeSetOfVolumes(partSet, mdFn, alignType=alignType) partSet2 = SetOfVolumes(filename=partFn2) # Let's create now another SetOfImages reading back the written # Xmipp metadata and check one more time. partSet2.copyInfo(partSet) if alignType == ALIGN_2D or alignType == ALIGN_PROJ: convert.readSetOfParticles(mdFn, partSet2, alignType=alignType) else: convert.readSetOfParticles(mdFn, partSet2, rowToFunc=convert.rowToVolume, alignType=alignType) partSet2.write() if PRINT_MATRIX: for i, img in enumerate(partSet2): m1 = aList[i] m2 = img.getTransform().getMatrix() print("-" * 5) print(img.getFileName(), img.getIndex()) print('m1:\n', m1, convert.geometryFromMatrix(m1, False)) print('m2:\n', m2, convert.geometryFromMatrix(m2, False)) self.assertTrue(np.allclose(m1, m2, rtol=1e-2)) # Launch apply transformation and check result images runRelionProgram(self.CMD % locals()) if SHOW_IMAGES: runRelionProgram('scipion show %(outputFn)s' % locals()) if os.path.exists(goldFn): self.assertTrue( ImageHandler().compareData(goldFn, outputFn, tolerance=0.001), "Different data files:\n>%s\n<%s" % (goldFn, outputFn)) if CLEAN_IMAGES: cleanPath(outputFn)
def getAcquisition(self): """ Build and fill an acquisition object. """ acquisition = Acquisition() self.fillAcquisition(acquisition) return acquisition
def particlesFactory(index): newParticle = Particle(location=(1, self.particlesStk)) newParticle.setCoordinate(Coordinate(x=1, y=2)) newParticle.setAcquisition(Acquisition()) return newParticle