def applyAlignmentStep(self, tsObjId): ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) path.makePath(extraPrefix) outputTsFileName = os.path.join(extraPrefix, "%s.mrc" % tsId) ts.applyTransform(outputTsFileName) outputInterpolatedSetOfTiltSeries = self.getOutputInterpolatedSetOfTiltSeries() newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputInterpolatedSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(index + 1, outputTsFileName) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write() outputInterpolatedSetOfTiltSeries.update(newTs) outputInterpolatedSetOfTiltSeries.updateDim() outputInterpolatedSetOfTiltSeries.write() self._store()
def assignTransformationMatricesStep(self, tsObjId): outputAssignedTransformSetOfTiltSeries = self.getOutputAssignedTransformSetOfTiltSeries( ) setTMTS = self.setTMSetOfTiltSeries.get()[tsObjId] getTMTS = self.getTMSetOfTiltSeries.get()[tsObjId] tsId = getTMTS.getTsId() newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(setTMTS) outputAssignedTransformSetOfTiltSeries.append(newTs) for tiltImageGetTM, tiltImageSetTM in zip(getTMTS, setTMTS): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImageSetTM, copyId=True) newTi.setLocation(tiltImageSetTM.getLocation()) newTi.setTransform(tiltImageGetTM.getTransform()) newTs.append(newTi) newTs.setDim(setTMTS.getDim()) newTs.write() outputAssignedTransformSetOfTiltSeries.update(newTs) outputAssignedTransformSetOfTiltSeries.updateDim() outputAssignedTransformSetOfTiltSeries.write() self._store()
def interpolateTiltSeries(self, tsObjId): missAliTs = self.outputMisalignedSetOfTiltSeries[tsObjId] tsId = missAliTs.getTsId() outputInterpolatedSetOfTiltSeries = self.getOutputInterpolatedSetOfTiltSeries( ) extraPrefix = self._getExtraPath(tsId) outputTsFileName = os.path.join(extraPrefix, "%s_missAli.st" % tsId) """Apply the transformation form the input tilt-series""" missAliTs.applyTransform(outputTsFileName) missAliInterTs = tomoObj.TiltSeries(tsId=tsId) missAliInterTs.copyInfo(missAliTs) outputInterpolatedSetOfTiltSeries.append(missAliInterTs) for index, tiltImage in enumerate(missAliTs): missAliInterTi = tomoObj.TiltImage() missAliInterTi.copyInfo(tiltImage, copyId=True) missAliInterTi.setLocation(index + 1, outputTsFileName) missAliInterTs.append(missAliInterTi) missAliInterTs.write() outputInterpolatedSetOfTiltSeries.update(missAliInterTs) outputInterpolatedSetOfTiltSeries.write() self._store()
def introduceRandomMisalignment(self, tsObjId): ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() outputMisalignedSetOfTiltSeries = self.getOutputMisalignedSetOfTiltSeries( ) missAliTs = tomoObj.TiltSeries(tsId=tsId) missAliTs.copyInfo(ts) outputMisalignedSetOfTiltSeries.append(missAliTs) for index, ti in enumerate(ts): missAliTi = tomoObj.TiltImage() missAliTi.copyInfo(ti, copyId=True) missAliTi.setLocation(ti.getLocation()) if ti.hasTransform(): transformMat = ti.getTransform().getMatrix() else: transformMat = np.identity(3) newTransformMat = self.modifyTransformMatrix( transformMat, index, ts.getSize(), tsId) newTransform = data.Transform() newTransform.setMatrix(newTransformMat) missAliTi.setTransform(newTransform) missAliTs.append(missAliTi) missAliTs.write() outputMisalignedSetOfTiltSeries.update(missAliTs) outputMisalignedSetOfTiltSeries.write() self._store()
def assignTransformationMatricesStep(self): self.getOutputAssignedTransformSetOfTiltSeries() inputSetOfTiltSeries = self.inputSetOfTiltSeries.get() for ts in inputSetOfTiltSeries: tsId = ts.getTsId() tsFileName = ts.getFirstItem().parseFileName(extension='') for tmFilePath, _ in self.iterFiles(): tmFileName = os.path.basename(os.path.splitext(tmFilePath)[0]) if tsFileName == tmFileName: alignmentMatrix = utils.formatTransformationMatrix( tmFilePath) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) self.outputAssignedTransformSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(tiltImage.getLocation()) transform = data.Transform() if tiltImage.hasTransform(): previousTransform = tiltImage.getTransform( ).getMatrix() newTransform = alignmentMatrix[:, :, index] previousTransformArray = np.array( previousTransform) newTransformArray = np.array(newTransform) outputTransformMatrix = np.matmul( previousTransformArray, newTransformArray) transform.setMatrix(outputTransformMatrix) newTi.setTransform(transform) else: transform.setMatrix(alignmentMatrix[:, :, index]) newTi.setTransform(transform) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions( newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) self.outputAssignedTransformSetOfTiltSeries.update(newTs) self.outputAssignedTransformSetOfTiltSeries.updateDim() self.outputAssignedTransformSetOfTiltSeries.write() self._store()
def generateOutputStackStep(self, tomoObjId): tomo = self.inputSetOfTomograms.get()[tomoObjId] tomoId = os.path.splitext(os.path.basename(tomo.getFileName()))[0] extraPrefix = self._getExtraPath(tomoId) outputProjectedSetOfTiltSeries = self.getOutputProjectedSetOfTiltSeries( ) newTs = tomoObj.TiltSeries(tsId=tomoId) newTs.copyInfo(tomo) newTs.setTsId(tomoId) # Add origin to output tilt-series origin = Transform() outputProjectedSetOfTiltSeries.append(newTs) tiltAngleList = self.getTiltAngleList() for index in range(self.getProjectionRange()): newTi = tomoObj.TiltImage() newTi.setTiltAngle(tiltAngleList[index]) newTi.setTsId(tomoId) newTi.setLocation( index + 1, os.path.join(extraPrefix, os.path.basename(tomo.getFileName()))) newTi.setSamplingRate( self.inputSetOfTomograms.get().getSamplingRate()) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) # Set origin to output tilt-series origin.setShifts( x / -2. * self.inputSetOfTomograms.get().getSamplingRate(), y / -2. * self.inputSetOfTomograms.get().getSamplingRate(), 0) newTs.setOrigin(origin) newTs.write(properties=False) outputProjectedSetOfTiltSeries.update(newTs) outputProjectedSetOfTiltSeries.updateDim() outputProjectedSetOfTiltSeries.write() self._store()
def createOutputStep(self, tsObjId): ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) outputSetOfTiltSeries = self.getOutputSetOfTiltSeries() newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputSetOfTiltSeries.append(newTs) newTs.setSamplingRate(self.samplingRate) fileName = ts.getFirstItem().parseFileName() for index, ti in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(ti, copyId=True) newTi.setLocation(index + 1, os.path.join(extraPrefix, fileName)) if ti.hasTransform(): newTi.setTransform(ti.getTransform()) newTi.setSamplingRate(self.samplingRate) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) outputSetOfTiltSeries.update(newTs) outputSetOfTiltSeries.updateDim() outputSetOfTiltSeries.write() self._store()
def createOutputStep(self, tsObjId): outputXraysErasedSetOfTiltSeries = self.getOutputSetOfXraysErasedTiltSeries( ) ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputXraysErasedSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation( index + 1, (os.path.join(extraPrefix, tiltImage.parseFileName()))) newTs.append(newTi) newTs.write(properties=False) outputXraysErasedSetOfTiltSeries.update(newTs) outputXraysErasedSetOfTiltSeries.write() self._store()
def generateOutputStackStep(self, tsObjId): outputNormalizedSetOfTiltSeries = self.getOutputNormalizedSetOfTiltSeries( ) ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) tmpPrefix = self._getTmpPath(tsId) paramsNewstack = { 'input': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName()), 'output': os.path.join(extraPrefix, ts.getFirstItem().parseFileName()), 'bin': int(self.binning.get()), 'imagebinned': 1.0, } argsNewstack = "-input %(input)s " \ "-output %(output)s " \ "-bin %(bin)d " \ "-imagebinned %(imagebinned)s " if self.floatDensities.get() != 0: argsNewstack += " -FloatDensities " + str( self.floatDensities.get()) if self.floatDensities.get() == 2: if self.meanSdToggle.get() == 0: argsNewstack += " -MeanAndStandardDeviation " + str(self.scaleMean.get()) + "," + \ str(self.scaleSd.get()) elif self.floatDensities.get() == 4: argsNewstack += " -ScaleMinAndMax " + str( self.scaleMax.get()) + "," + str(self.scaleMin.get()) else: if self.scaleRangeToggle.get() == 0: argsNewstack += " -ScaleMinAndMax " + str(self.scaleRangeMax.get()) + "," + \ str(self.scaleRangeMin.get()) if self.getModeToOutput() is not None: argsNewstack += " -ModeToOutput " + str(self.getModeToOutput()) Plugin.runImod(self, 'newstack', argsNewstack % paramsNewstack) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputNormalizedSetOfTiltSeries.append(newTs) if self.binning > 1: newTs.setSamplingRate(ts.getSamplingRate() * int(self.binning.get())) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation( index + 1, (os.path.join(extraPrefix, tiltImage.parseFileName()))) if self.binning > 1: newTi.setSamplingRate(tiltImage.getSamplingRate() * int(self.binning.get())) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) outputNormalizedSetOfTiltSeries.update(newTs) outputNormalizedSetOfTiltSeries.updateDim() outputNormalizedSetOfTiltSeries.write() self._store()
def convertInputStep(self, ts): tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) tmpPrefix = self._getTmpPath(tsId) path.makePath(tmpPrefix) path.makePath(extraPrefix) firstItem = ts.getFirstItem() outputTsFileName = self.getFilePath(ts, extension=".st") """Apply transformation matrices and remove excluded views""" if self.excludeList.get() == '': """Apply the transformation form the input tilt-series""" ts.applyTransform(outputTsFileName) """Generate angle file""" angleFilePath = self.getFilePath(ts, extension=".rawtlt") ts.generateTltFile(angleFilePath) else: interpolatedTsFileName = os.path.join(tmpPrefix, firstItem.parseFileName()) angleFilePath = self.getFilePath(ts, extension=".rawtlt") """Apply the transformation form the input tilt-series and generate a new ts object""" ts.applyTransform(interpolatedTsFileName) interpolatedSetOfTiltSeries = self._createSetOfTiltSeries( suffix='Interpolated') interpolatedSetOfTiltSeries.copyInfo(ts) interpolatedSetOfTiltSeries.setDim(ts.getDim()) interpolatedTs = tomoObj.TiltSeries(tsId=tsId) interpolatedTs.copyInfo(ts) interpolatedSetOfTiltSeries.append(interpolatedTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(index + 1, interpolatedTsFileName) interpolatedTs.append(newTi) interpolatedTs.write() """Write a new stack discarding excluded tilts""" excludeList = [int(i) for i in self.excludeList.get().split()] tiList = [ti.clone() for ti in interpolatedTs] tiList.sort(key=lambda ti: ti.getTiltAngle()) writeTiStack(tiList, outputStackFn=outputTsFileName, outputTltFn=angleFilePath, excludeList=excludeList) """Generate etomo config file""" args = '-name %s ' % firstItem.parseFileName(extension="") args += '-gold %0.3f ' % self.markersDiameter # Imod use the pixel size in NM pixelSizeNm = ts.getSamplingRate() / 10. args += '-pixel %0.3f ' % pixelSizeNm args += '-rotation %0.3f ' % self.rotationAngle args += '-userawtlt ' # 0 for output image files to have descriptive extensions like ".preali", 1 for extension ".mrc", or 2 for # extension ".hdf". In the latter two cases the usual descriptive text is put before the extension, and command # files will contain an environment variable setting to make programs generate files of the corresponding type. # From: https://bio3d.colorado.edu/imod/doc/man/copytomocoms.html args += '-NamingStyle 0 ' # Extension of raw stack excluding the period. If this is not specified, the program will assume the extension # ".st" unless the -style option is entered. With a -style option and no specified stack extension, it will # look for ".st", ".mrc", ".hdf",".tif", and ".tiff" and require that only one of those types is present. With # this entry, which could in principle be arbitrary, it will not care if files with other extensions are # present. # From: https://bio3d.colorado.edu/imod/doc/man/copytomocoms.html args += '-StackExtension ""' Plugin.runImod(self, 'copytomocoms', args, cwd=extraPrefix) edfFn = self.getFilePath(ts, extension=".edf") minTilt = min( utils.formatAngleList(self.getFilePath(ts, extension=".rawtlt"))) self._writeEtomoEdf( edfFn, { 'date': pw.utils.prettyTime(), 'name': firstItem.parseFileName(extension=''), 'pixelSize': pixelSizeNm, 'version': pw.__version__, 'minTilt': minTilt, 'markerDiameter': self.markersDiameter, 'rotationAngle': self.rotationAngle })
def generateOutputStackStep(self, tsObjId): outputInterpolatedSetOfTiltSeries = self.getOutputInterpolatedSetOfTiltSeries( ) ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) firstItem = ts.getFirstItem() paramsAlignment = { 'input': firstItem.getFileName(), 'output': os.path.join(extraPrefix, firstItem.parseFileName()), 'xform': os.path.join(extraPrefix, firstItem.parseFileName(extension=".prexg")), 'bin': int(self.binning.get()), 'imagebinned': 1.0 } argsAlignment = "-input %(input)s " \ "-output %(output)s " \ "-xform %(xform)s " \ "-bin %(bin)d " \ "-imagebinned %(imagebinned)s " rotationAngleAvg = utils.calculateRotationAngleFromTM(ts) # Check if rotation angle is greater than 45º. If so, swap x and y dimensions to adapt output image sizes to # the final sample disposition. if rotationAngleAvg > 45 or rotationAngleAvg < -45: paramsAlignment.update( {'size': "%d,%d" % (firstItem.getYDim(), firstItem.getXDim())}) argsAlignment += "-size %(size)s " Plugin.runImod(self, 'newstack', argsAlignment % paramsAlignment) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputInterpolatedSetOfTiltSeries.append(newTs) if self.binning > 1: newTs.setSamplingRate(ts.getSamplingRate() * int(self.binning.get())) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation( index + 1, (os.path.join(extraPrefix, tiltImage.parseFileName()))) if self.binning > 1: newTi.setSamplingRate(tiltImage.getSamplingRate() * int(self.binning.get())) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) outputInterpolatedSetOfTiltSeries.update(newTs) outputInterpolatedSetOfTiltSeries.updateDim() outputInterpolatedSetOfTiltSeries.write() self._store()
def computeInterpolatedStackStep(self, tsObjId): outputInterpolatedSetOfTiltSeries = self.getOutputInterpolatedSetOfTiltSeries( ) ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) tmpPrefix = self._getTmpPath(tsId) paramsAlignment = { 'input': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName()), 'output': os.path.join(extraPrefix, ts.getFirstItem().parseFileName()), 'xform': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexg")), 'bin': int(self.binning.get()), 'imagebinned': 1.0 } argsAlignment = "-input %(input)s " \ "-output %(output)s " \ "-xform %(xform)s " \ "-bin %(bin)d " \ "-imagebinned %(imagebinned)s" Plugin.runImod(self, 'newstack', argsAlignment % paramsAlignment) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputInterpolatedSetOfTiltSeries.append(newTs) if self.binning > 1: newTs.setSamplingRate(ts.getSamplingRate() * int(self.binning.get())) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation( index + 1, (os.path.join(extraPrefix, tiltImage.parseFileName()))) if self.binning > 1: newTi.setSamplingRate(tiltImage.getSamplingRate() * int(self.binning.get())) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) newTs.write(properties=False) outputInterpolatedSetOfTiltSeries.update(newTs) outputInterpolatedSetOfTiltSeries.updateDim() outputInterpolatedSetOfTiltSeries.write() self._store()
def computeXcorrStep(self, tsObjId): """Compute transformation matrix for each tilt series""" ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) tmpPrefix = self._getTmpPath(tsId) paramsXcorr = { 'input': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName()), 'output': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexf")), 'tiltfile': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(extension=".tlt")), 'rotationAngle': self.rotationAngle.get(), 'filterSigma1': self.filterSigma1.get(), 'filterSigma2': self.filterSigma2.get(), 'filterRadius1': self.filterRadius1.get(), 'filterRadius2': self.filterRadius2.get() } argsXcorr = "-input %(input)s " \ "-output %(output)s " \ "-tiltfile %(tiltfile)s " \ "-RotationAngle %(rotationAngle)f " \ "-FilterSigma1 %(filterSigma1)f " \ "-FilterSigma2 %(filterSigma2)f " \ "-FilterRadius1 %(filterRadius1)f " \ "-FilterRadius2 %(filterRadius2)f " Plugin.runImod(self, 'tiltxcorr', argsXcorr % paramsXcorr) paramsXftoxg = { 'input': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexf")), 'goutput': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexg")), } argsXftoxg = "-input %(input)s " \ "-goutput %(goutput)s" Plugin.runImod(self, 'xftoxg', argsXftoxg % paramsXftoxg) """Generate output tilt series""" outputSetOfTiltSeries = self.getOutputSetOfTiltSeries() alignmentMatrix = utils.formatTransformationMatrix( os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".prexg"))) newTs = tomoObj.TiltSeries(tsId=tsId) newTs.copyInfo(ts) outputSetOfTiltSeries.append(newTs) for index, tiltImage in enumerate(ts): newTi = tomoObj.TiltImage() newTi.copyInfo(tiltImage, copyId=True) newTi.setLocation(tiltImage.getLocation()) transform = data.Transform() transform.setMatrix(alignmentMatrix[:, :, index]) newTi.setTransform(transform) newTs.append(newTi) newTs.write(properties=False) outputSetOfTiltSeries.update(newTs) outputSetOfTiltSeries.write() self._store()