def getTomoSetFromStar(prot, starFile): samplingRate = prot.pixelSize.get() imgh = ImageHandler() tomoTable = Table() tomoTable.read(starFile) tomoList = [row.get(TOMO_NAME, FILE_NOT_FOUND) for row in tomoTable] prot.tomoList = List(tomoList) tomoNamesUnique = list(set(tomoList)) # Create a Volume template object tomo = Tomogram() tomo.setSamplingRate(samplingRate) for fileName in tomoNamesUnique: x, y, z, n = imgh.getDimensions(fileName) if fileName.endswith('.mrc') or fileName.endswith('.map'): if z == 1 and n != 1: zDim = n n = 1 else: zDim = z else: zDim = z origin = Transform() origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) tomo.setOrigin(origin) # read origin from form for index in range(1, n + 1): tomo.cleanObjId() tomo.setLocation(index, fileName) tomo.setAcquisition(TomoAcquisition(**prot.acquisitionParams)) prot.tomoSet.append(tomo)
def _relionTomoStar2Subtomograms(prot, outputSubTomogramsSet, tomoTable, invert): ih = ImageHandler() samplingRate = outputSubTomogramsSet.getSamplingRate() for row, inSubtomo in zip(tomoTable, prot.inputSubtomos.get()): subtomo = SubTomogram() coordinate3d = Coordinate3D() transform = Transform() origin = Transform() volname = row.get(TOMO_NAME, FILE_NOT_FOUND) subtomoFn = row.get(SUBTOMO_NAME, FILE_NOT_FOUND) subtomo.setVolName(managePath4Sqlite(volname)) subtomo.setTransform(transform) subtomo.setAcquisition(TomoAcquisition()) subtomo.setClassId(row.get('rlnClassNumber', 0)) subtomo.setSamplingRate(samplingRate) subtomo.setCoordinate3D(coordinate3d) # Needed to get later the tomogram pointer via getCoordinate3D() x = row.get(COORD_X, 0) y = row.get(COORD_Y, 0) z = row.get(COORD_Z, 0) tiltPrior = row.get(TILT_PRIOR, 0) psiPrior = row.get(PSI_PRIOR, 0) # ctf3d = row.get(CTF_MISSING_WEDGE, FILE_NOT_FOUND) coordinate3d = subtomo.getCoordinate3D() coordinate3d.setX(float(x), BOTTOM_LEFT_CORNER) coordinate3d.setY(float(y), BOTTOM_LEFT_CORNER) coordinate3d.setZ(float(z), BOTTOM_LEFT_CORNER) coordinate3d._3dcftMrcFile = inSubtomo.getCoordinate3D()._3dcftMrcFile # Used for the ctf3d in Relion 3.0 (tomo) M = _getTransformMatrix(row, invert) transform.setMatrix(M) subtomo.setCoordinate3D(coordinate3d) subtomo._tiltPriorAngle = Float(tiltPrior) subtomo._psiPriorAngle = Float(psiPrior) # Set the origin and the dimensions of the current subtomogram x, y, z, n = ih.getDimensions(subtomoFn) zDim, _ = manageIhDims(subtomoFn, z, n) origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) subtomo.setOrigin(origin) subtomo.setFileName(managePath4Sqlite(subtomoFn)) # if subtomo is in a vesicle if 'tid_' in subtomoFn: vesicleId = subtomoFn.split('tid_')[1] vesicleId = vesicleId[0] scoor = subtomo.getCoordinate3D() scoor.setGroupId(vesicleId) subtomo.setCoordinate3D(scoor) # Add current subtomogram to the output set outputSubTomogramsSet.append(subtomo)
def updateSubTomogram(subTomogram, index): particleParams = particlesParams.get(index) if not particleParams: print("Could not get params for particle %d" % index) setattr(subTomogram, "_appendItem", False) else: setattr(subTomogram, 'coverage', Float(particleParams["coverage"])) setattr(subTomogram, 'score', Float(particleParams["score"])) # Create 4x4 matrix from 4x3 e2spt_sgd align matrix and append row [0,0,0,1] am = particleParams["alignMatrix"] angles = numpy.array([am[0:3], am[4:7], am[8:11], [0, 0, 0]]) samplingRate = outputSetOfSubTomograms.getSamplingRate() shift = numpy.array([am[3] * samplingRate, am[7] * samplingRate, am[11] * samplingRate, 1]) matrix = numpy.column_stack((angles, shift.T)) subTomogram.setTransform(Transform(matrix))
def rowToAlignment(alignmentList, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ # use all angles in 2D since we might have mirrors # is2D = alignType == em.ALIGN_2D inverseTransform = alignType == emcts.ALIGN_PROJ alignment = Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) shifts[0] = alignmentList[3] shifts[1] = alignmentList[4] shifts[2] = 0 angles[0] = alignmentList[0] angles[1] = alignmentList[1] angles[2] = alignmentList[2] matrix = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(matrix) return alignment
def importVolumesStep(self, pattern, samplingRate, setOrigCoord=False): """ Copy images matching the filename pattern Register other parameters. """ volSet = self._createSetOfVolumes() vol = emobj.Volume() if self.importFrom == self.IMPORT_FROM_FILES: self.info("Using pattern: '%s'" % pattern) # Create a Volume template object vol.setSamplingRate(samplingRate) imgh = emlib.image.ImageHandler() volSet.setSamplingRate(samplingRate) for fileName, fileId in self.iterFiles(): x, y, z, n = imgh.getDimensions(fileName) if fileName.endswith('.mrc') or fileName.endswith('.map'): fileName += ':mrc' if z == 1 and n != 1: zDim = n n = 1 else: zDim = z else: zDim = z origin = emobj.Transform() if setOrigCoord: origin.setShiftsTuple(self._getOrigCoord()) else: origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) vol.setOrigin(origin) # read origin from form if self.copyFiles or setOrigCoord: newFileName = abspath( self._getVolumeFileName(fileName, "mrc")) emconv.Ccp4Header.fixFile(fileName, newFileName, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) if self.setHalfMaps.get(): newFileName1 = abspath( self._getVolumeFileName(self.half1map.get(), "mrc")) emconv.Ccp4Header.fixFile(self.half1map.get(), newFileName1, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) newFileName2 = abspath( self._getVolumeFileName(self.half2map.get(), "mrc")) emconv.Ccp4Header.fixFile(self.half2map.get(), newFileName2, origin.getShifts(), samplingRate, emconv.Ccp4Header.ORIGIN) vol.setHalfMaps( [relpath(newFileName1), relpath(newFileName2)]) else: newFileName = abspath(self._getVolumeFileName(fileName)) if fileName.endswith(':mrc'): fileName = fileName[:-4] pwutils.createAbsLink(fileName, newFileName) if self.setHalfMaps.get(): pwutils.createAbsLink( self.half1map.get(), abspath( self._getVolumeFileName(self.half1map.get()))) pwutils.createAbsLink( self.half2map.get(), abspath( self._getVolumeFileName(self.half2map.get()))) vol.setHalfMaps([ relpath( self._getVolumeFileName(self.half1map.get())), relpath( self._getVolumeFileName(self.half2map.get())) ]) # Make newFileName relative # https://github.com/I2PC/scipion/issues/1935 newFileName = relpath(newFileName) if n == 1: vol.cleanObjId() vol.setFileName(newFileName) volSet.append(vol) else: for index in range(1, n + 1): vol.cleanObjId() vol.setLocation(index, newFileName) volSet.append(vol) else: # import from EMDB self.info("Downloading map with ID = %s" % self.emdbId) try: localFileName, sampling, origin = \ fetch_emdb_map(self.emdbId, self._getExtraPath(), self._getTmpPath()) except Exception as e: print(e) return # open volume and fill sampling and origin vol.setSamplingRate(sampling) vol.setFileName(localFileName) from pwem.objects.data import Transform originMat = Transform() originMat.setShifts(origin[0], origin[1], origin[2]) vol.setOrigin(originMat) if volSet.getSize() > 1: self._defineOutputs(outputVolumes=volSet) else: self._defineOutputs(outputVolume=vol)
def createPhantomsStep(self): mwfilter = self.mwfilter.get() rotmin = self.rotmin.get() rotmax = self.rotmax.get() tiltmin = self.tiltmin.get() tiltmax = self.tiltmax.get() psimin = self.psimin.get() psimax = self.psimax.get() fnVol = self._getExtraPath("phantom000.vol") if self.option.get() == 0: inputVol = self.inputVolume.get() fnInVol = inputVol.getLocation()[1] dim = inputVol.getDim() if mwfilter: mwangle = self.mwangle.get() self.runJob( "xmipp_transform_filter", " --fourier wedge -%d %d 0 0 0 -i %s -o %s" % (mwangle, mwangle, fnInVol, fnVol)) else: self.runJob("xmipp_image_convert", " -i %s -o %s" % (fnInVol, fnVol)) else: desc = self.create.get() fnDescr = self._getExtraPath("phantom.descr") fhDescr = open(fnDescr, 'w') fhDescr.write(desc) fhDescr.close() dim = [desc.split()[0], desc.split()[1], desc.split()[2]] self.runJob("xmipp_phantom_create", " -i %s -o %s" % (fnDescr, fnVol)) if mwfilter: mwangle = self.mwangle.get() self.runJob( "xmipp_transform_filter", " --fourier wedge -%d %d 0 0 0 -i %s -o %s" % (mwangle, mwangle, fnVol, fnVol)) self.outputSet = self._createSetOfSubTomograms( self._getOutputSuffix(SetOfSubTomograms)) self.outputSet.setDim(dim) self.outputSet.setSamplingRate(self.sampling.get()) coordsBool = self.coords.get() if coordsBool: tomos = self.tomos.get() tomo = tomos.getFirstItem() tomoDim = tomo.getDim() self.coords = self._createSetOfCoordinates3D(tomos) subtomobase = SubTomogram() acq = TomoAcquisition() subtomobase.setAcquisition(acq) subtomobase.setLocation(fnVol) subtomobase.setSamplingRate(self.sampling.get()) transformBase = Transform() transformBase.setMatrix(np.identity(4)) subtomobase.setTransform(transformBase) if coordsBool: coor = Coordinate3D() coor.setVolume(tomo) coor.setX(np.random.randint(0, tomoDim[0]), const.BOTTOM_LEFT_CORNER) coor.setY(np.random.randint(0, tomoDim[1]), const.BOTTOM_LEFT_CORNER) coor.setZ(np.random.randint(0, tomoDim[2]), const.BOTTOM_LEFT_CORNER) subtomobase.setCoordinate3D(coor) subtomobase.setVolName(tomo.getFileName()) self.coords.append(coor) self.coords.setBoxSize(subtomobase.getDim()[0]) self.outputSet.append(subtomobase) for i in range(int(self.nsubtomos.get()) - 1): fnPhantomi = self._getExtraPath("phantom%03d.vol" % int(i + 1)) rot = np.random.randint(rotmin, rotmax) tilt = np.random.randint(tiltmin, tiltmax) psi = np.random.randint(psimin, psimax) self.runJob( "xmipp_transform_geometry", " -i %s -o %s --rotate_volume euler %d %d %d " % (fnVol, fnPhantomi, rot, tilt, psi)) if mwfilter: self.runJob( "xmipp_transform_filter", " --fourier wedge -%d %d 0 0 0 -i %s -o %s" % (mwangle, mwangle, fnPhantomi, fnPhantomi)) subtomo = SubTomogram() subtomo.setAcquisition(acq) subtomo.setLocation(fnPhantomi) subtomo.setSamplingRate(self.sampling.get()) A = euler_matrix(np.deg2rad(psi), np.deg2rad(tilt), np.deg2rad(rot), 'szyz') transform = Transform() transform.setMatrix(A) subtomo.setTransform(transform) if coordsBool: coor = Coordinate3D() coor.setVolume(tomo) coor.setX(np.random.randint(0, tomoDim[0]), const.BOTTOM_LEFT_CORNER) coor.setY(np.random.randint(0, tomoDim[1]), const.BOTTOM_LEFT_CORNER) coor.setZ(np.random.randint(0, tomoDim[2]), const.BOTTOM_LEFT_CORNER) subtomo.setCoordinate3D(coor) subtomo.setVolName(tomo.getFileName()) self.coords.append(coor) self.outputSet.append(subtomo) if coordsBool: self.outputSet.setCoordinates3D(self.coords)