def generateFiducialModelStep(self, tsObjId):
        # TODO: check si es el landmark model correcto
        lm = self.inputSetOfLandmarkModels.get()[tsObjId]
        ts = self.inputSetOfTiltSeries.get()[tsObjId]

        tsId = ts.getTsId()
        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)
        path.makePath(extraPrefix)
        path.makePath(tmpPrefix)

        landmarkTextFilePath = os.path.join(
            extraPrefix,
            ts.getFirstItem().parseFileName(suffix="_fid", extension=".txt"))
        landmarkModelPath = os.path.join(
            extraPrefix,
            ts.getFirstItem().parseFileName(suffix="_fid", extension=".mod"))

        # Generate the IMOD file containing the information from the landmark model
        utils.generateIMODFiducialTextFile(landmarkModel=lm,
                                           outputFilePath=landmarkTextFilePath)

        # Convert IMOD file into IMOD model
        paramsPoint2Model = {
            'inputFile': landmarkTextFilePath,
            'outputFile': landmarkModelPath,
        }

        argsPoint2Model = "-InputFile %(inputFile)s " \
                          "-OutputFile %(outputFile)s"

        Plugin.runImod(self, 'point2model',
                       argsPoint2Model % paramsPoint2Model)
Example #2
0
    def projectTomogram(self, tomoObjId):
        tomo = self.inputSetOfTomograms.get()[tomoObjId]

        tomoId = os.path.splitext(os.path.basename(tomo.getFileName()))[0]

        extraPrefix = self._getExtraPath(tomoId)
        tmpPrefix = self._getTmpPath(tomoId)
        path.makePath(tmpPrefix)
        path.makePath(extraPrefix)

        paramsXYZproj = {
            'input':
            tomo.getFileName(),
            'output':
            os.path.join(extraPrefix, os.path.basename(tomo.getFileName())),
            'axis':
            self.getRotationAxis(),
            'angles':
            str(self.minAngle.get()) + ',' + str(self.maxAngle.get()) + ',' +
            str(self.stepAngle.get()),
        }

        argsXYZproj = "-input %(input)s " \
                      "-output %(output)s " \
                      "-axis %(axis)s " \
                      "-angles %(angles)s "

        Plugin.runImod(self, 'xyzproj', argsXYZproj % paramsXYZproj)
    def pickGoldBeadsStep(self, tsObjId):
        tomo = self.inputSetOfTomograms.get()[tsObjId]
        location = tomo.getLocation()[1]
        fileName, _ = os.path.splitext(location)

        extraPrefix = self._getExtraPath(os.path.basename(fileName))
        path.makePath(extraPrefix)
        """ Run findbeads3d IMOD program """
        paramsFindbeads3d = {
            'inputFile':
            location,
            'outputFile':
            os.path.join(extraPrefix, "%s.mod" % os.path.basename(fileName)),
            'beadSize':
            self.beadDiameter.get(),
            'minRelativeStrength':
            self.minRelativeStrength.get(),
            'minSpacing':
            self.minSpacing.get(),
        }

        argsFindbeads3d = "-InputFile %(inputFile)s " \
                          "-OutputFile %(outputFile)s " \
                          "-BeadSize %(beadSize)d " \
                          "-MinRelativeStrength %(minRelativeStrength)f " \
                          "-MinSpacing %(minSpacing)d "

        if self.beadsColor.get() == 1:
            argsFindbeads3d += "-LightBeads "

        Plugin.runImod(self, 'findbeads3d',
                       argsFindbeads3d % paramsFindbeads3d)
Example #4
0
 def runEtomo(self, ts):
     self.convertInputStep(ts)
     if ts is not None:
         tsId = ts.getTsId()
         extraPrefix = self._getExtraPath(tsId)
         args = '--fg '
         args += ts.getFirstItem().parseFileName(extension=".edf")
         Plugin.runImod(self, 'etomo', args, cwd=extraPrefix)
    def ctfCorrection(self, tsObjId):
        ts = self.inputSetOfTiltSeries.get()[tsObjId]
        tsId = ts.getTsId()
        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)
        """Run ctfphaseflip IMOD program"""
        paramsCtfPhaseFlip = {
            'inputStack':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName()),
            'angleFile':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName(extension=".tlt")),
            'outputFileName':
            os.path.join(extraPrefix,
                         ts.getFirstItem().parseFileName()),
            'defocusFile':
            os.path.join(
                extraPrefix,
                ts.getFirstItem().parseFileName(extension=".defocus")),
            'voltage':
            self.inputSetOfTiltSeries.get().getAcquisition().getVoltage(),
            'sphericalAberration':
            self.inputSetOfTiltSeries.get().getAcquisition(
            ).getSphericalAberration(),
            'defocusTol':
            self.defocusTol.get(),
            'pixelSize':
            self.inputSetOfTiltSeries.get().getSamplingRate() / 10,
            'amplitudeContrast':
            self.inputSetOfTiltSeries.get().getAcquisition().
            getAmplitudeContrast(),
            'interpolationWidth':
            self.interpolationWidth.get(),
        }

        argsCtfPhaseFlip = "-InputStack %(inputStack)s " \
                           "-AngleFile %(angleFile)s " \
                           "-OutputFileName %(outputFileName)s " \
                           "-DefocusFile %(defocusFile)s " \
                           "-Voltage %(voltage)d " \
                           "-SphericalAberration %(sphericalAberration)f " \
                           "-DefocusTol %(defocusTol)d " \
                           "-PixelSize %(pixelSize)f " \
                           "-AmplitudeContrast %(amplitudeContrast)f " \
                           "-InterpolationWidth %(interpolationWidth)d "

        if self.usesGpu():
            paramsCtfPhaseFlip.update({"useGPU": self.getGpuList()[0]})

            argsCtfPhaseFlip += "-UseGPU %(useGPU)d "

        Plugin.runImod(self, 'ctfphaseflip',
                       argsCtfPhaseFlip % paramsCtfPhaseFlip)
    def doseFilterStep(self, tsObjId):
        """Apply the dose fitler to every tilt series"""

        ts = self.inputSetOfTiltSeries.get()[tsObjId]
        tsId = ts.getTsId()

        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)

        path.makePath(tmpPrefix)
        path.makePath(extraPrefix)

        firstItem = ts.getFirstItem()

        paramsMtffilter = {
            'input': firstItem.getFileName(),
            'output': os.path.join(extraPrefix, firstItem.parseFileName()),
            'voltage': ts.getAcquisition().getVoltage(),
        }

        argsMtffilter = "-input %(input)s " \
                        "-output %(output)s " \
                        "-Voltage %(voltage)d "

        if self.inputDoseType.get() == SCIPION_IMPORT:
            outputDefocusFilePath = os.path.join(
                extraPrefix, firstItem.parseFileName(extension=".dose"))

            utils.generateDoseFileFromTS(ts, outputDefocusFilePath)

            paramsMtffilter.update({
                'typeOfDoseFile': 1,
                'doseWeightingFile': outputDefocusFilePath,
            })

            argsMtffilter += "-TypeOfDoseFile %(typeOfDoseFile)d " \
                             "-DoseWeightingFile %(doseWeightingFile)s "

        if self.inputDoseType.get() == FIXED_DOSE:
            paramsMtffilter.update(
                {'fixedImageDose': self.fixedImageDose.get()})

            argsMtffilter += "-FixedImageDose %(fixedImageDose)f"

        Plugin.runImod(self, 'mtffilter', argsMtffilter % paramsMtffilter)
    def convertModelToCoordinatesStep(self, tsObjId):
        tomo = self.inputSetOfTomograms.get()[tsObjId]
        location = tomo.getLocation()[1]
        fileName, _ = os.path.splitext(location)

        extraPrefix = self._getExtraPath(os.path.basename(fileName))
        """ Run model2point IMOD program """
        paramsModel2Point = {
            'inputFile':
            os.path.join(extraPrefix, "%s.mod" % os.path.basename(fileName)),
            'outputFile':
            os.path.join(extraPrefix, "%s.xyz" % os.path.basename(fileName)),
        }

        argsModel2Point = "-InputFile %(inputFile)s " \
                          "-OutputFile %(outputFile)s " \

        Plugin.runImod(self, 'model2point',
                       argsModel2Point % paramsModel2Point)
    def eraseXraysStep(self, tsObjId):
        ts = self.inputSetOfTiltSeries.get()[tsObjId]

        tsId = ts.getTsId()
        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)

        paramsCcderaser = {
            'inputFile':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName()),
            'outputFile':
            os.path.join(extraPrefix,
                         ts.getFirstItem().parseFileName()),
            'modelFile':
            os.path.join(
                extraPrefix,
                ts.getFirstItem().parseFileName(suffix="_fid",
                                                extension=".mod")),
            'betterRadius':
            self.betterRadius.get(),
            'polynomialOrder':
            0,
            'circleObjects':
            "/"
        }

        argsCcderaser = "-InputFile %(inputFile)s " \
                        "-OutputFile %(outputFile)s " \
                        "-ModelFile %(modelFile)s " \
                        "-BetterRadius %(betterRadius)d " \
                        "-PolynomialOrder %(polynomialOrder)d " \
                        "-CircleObjects %(circleObjects)s " \
                        "-MergePatches " \
                        "-ExcludeAdjacent"

        Plugin.runImod(self, 'ccderaser', argsCcderaser % paramsCcderaser)
    def computeReconstructionStep(self, tsObjId):
        ts = self.inputSetOfTiltSeries.get()[tsObjId]
        tsId = ts.getTsId()

        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)

        paramsTilt = {
            'InputProjections': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName()),
            'OutputFile': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(extension=".rec")),
            'TiltFile': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(extension=".tlt")),
            'Thickness': self.tomoThickness.get(),
            'FalloffIsTrueSigma': 1,
            'Radial': str(self.radialFirstParameter.get()) + "," + str(self.radialSecondParameter.get()),
            'Shift': str(self.tomoShiftX.get()) + " " + str(self.tomoShiftZ.get()),
            'Offset': str(self.angleOffset.get()) + " " + str(self.tiltAxisOffset.get()),
        }

        argsTilt = "-InputProjections %(InputProjections)s " \
                   "-OutputFile %(OutputFile)s " \
                   "-TILTFILE %(TiltFile)s " \
                   "-THICKNESS %(Thickness)d " \
                   "-FalloffIsTrueSigma %(FalloffIsTrueSigma)d " \
                   "-RADIAL %(Radial)s " \
                   "-SHIFT %(Shift)s " \
                   "-OFFSET %(Offset)s "

        if self.fakeInteractionsSIRT.get() != 0:
            paramsTilt.update({
                'FakeSIRTInteractions': self.fakeInteractionsSIRT.get()
            })
            argsTilt += "-FakeSIRTiterations %(FakeSIRTInteractions)d "

        if self.usesGpu():
            paramsTilt.update({
                "useGPU": self.getGpuList()[0],
                "actionIfGPUFails": "2,2",
            })

            argsTilt += "-UseGPU %(useGPU)d " \
                        "-ActionIfGPUFails %(actionIfGPUFails)s "

        Plugin.runImod(self, 'tilt', argsTilt % paramsTilt)

        paramsNewstack = {
            'input':  os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(extension=".rec")),
            'output':  os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(suffix="_flipped", extension=".mrc")),
        }

        argsNewstack = "-input %(input)s " \
                       "-output %(output)s"

        Plugin.runImod(self, 'newstack', argsNewstack % paramsNewstack)

        paramsTrimVol = {
            'input': os.path.join(tmpPrefix, ts.getFirstItem().parseFileName(suffix="_flipped", extension=".mrc")),
            'output': os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".mrc")),
            'rotation': "-yz "
        }

        argsTrimvol = "%(input)s " \
                      "%(output)s " \
                      "%(rotation)s "

        Plugin.runImod(self, 'trimvol', argsTrimvol % paramsTrimVol)
Example #10
0
    def createOutput(self):
        outputPrealiSetOfTiltSeries = None
        outputAliSetOfTiltSeries = None
        outputSetOfLandmarkModelsNoGaps = None
        outputSetOfCoordinates3D = None
        outputSetOfFullTomograms = None
        outputSetOfPostProcessTomograms = None

        for ts in self.inputSetOfTiltSeries.get():
            self.inputTiltSeries = ts
            tsId = ts.getTsId()
            """Prealigned tilt-series"""
            prealiFilePath = self.getFilePath(ts, extension=".preali")
            if os.path.exists(prealiFilePath):
                if outputPrealiSetOfTiltSeries is None:
                    outputPrealiSetOfTiltSeries = self._createSetOfTiltSeries(
                        suffix='Preali')
                    outputPrealiSetOfTiltSeries.copyInfo(self.inputTiltSeries)
                    outputPrealiSetOfTiltSeries.setDim(
                        self.inputTiltSeries.getDim())
                    self._defineOutputs(outputPrealignedSetOfTiltSeries=
                                        outputPrealiSetOfTiltSeries)
                    self._defineSourceRelation(self.inputTiltSeries,
                                               outputPrealiSetOfTiltSeries)

                newTs = ts.clone()
                newTs.copyInfo(ts)
                outputPrealiSetOfTiltSeries.append(newTs)

                ih = ImageHandler()
                index = 0
                for index, tiltImage in enumerate(ts.iterItems(iterate=False)):
                    newTi = tiltImage.clone()
                    newTi.copyInfo(tiltImage, copyId=True)
                    newTi.setLocation(index + 1, prealiFilePath)
                    index += 1
                    xPreali, _, _, _ = ih.getDimensions(newTi.getFileName() +
                                                        ":mrc")
                    newTi.setSamplingRate(
                        self.getPixSizeFromDimensions(xPreali))
                    newTs.append(newTi)

                xPreali, yPreali, zPreali, _ = ih.getDimensions(
                    newTs.getFirstItem().getFileName() + ":mrc")
                newTs.setDim((xPreali, yPreali, zPreali))

                newTs.write(properties=False)

                outputPrealiSetOfTiltSeries.setSamplingRate(
                    self.getPixSizeFromDimensions(xPreali))
                outputPrealiSetOfTiltSeries.write()
                self._store(outputPrealiSetOfTiltSeries)
            """Aligned tilt-series"""
            aligFilePath = self.getFilePath(ts, extension=".ali")

            if os.path.exists(aligFilePath):
                if outputAliSetOfTiltSeries is None:
                    outputAliSetOfTiltSeries = self._createSetOfTiltSeries(
                        suffix='Ali')
                    outputAliSetOfTiltSeries.copyInfo(self.inputTiltSeries)
                    outputAliSetOfTiltSeries.setDim(
                        self.inputTiltSeries.getDim())
                    self._defineOutputs(
                        outputAlignedSetOfTiltSeries=outputAliSetOfTiltSeries)
                    self._defineSourceRelation(self.inputSetOfTiltSeries,
                                               outputAliSetOfTiltSeries)

                newTs = ts.clone()
                newTs.copyInfo(ts)
                outputAliSetOfTiltSeries.append(newTs)

                ih = ImageHandler()

                tltFilePath = self.getFilePath(ts,
                                               suffix='_fid',
                                               extension=".tlt")
                if os.path.exists(tltFilePath):
                    tltList = utils.formatAngleList(tltFilePath)
                else:
                    tltList = None

                index = 0
                for index, tiltImage in enumerate(ts.iterItems(iterate=False)):
                    newTi = tiltImage.clone()
                    newTi.copyInfo(tiltImage, copyId=True)
                    newTi.setLocation(index + 1, aligFilePath)
                    if tltList is not None:
                        newTi.setTiltAngle(float(tltList[index]))
                    xAli, _, _, _ = ih.getDimensions(newTi.getFileName() +
                                                     ":mrc")
                    newTi.setSamplingRate(self.getPixSizeFromDimensions(xAli))
                    newTs.append(newTi)

                xAli, yAli, zAli, _ = ih.getDimensions(
                    newTs.getFirstItem().getFileName() + ":mrc")
                newTs.setDim((xAli, yAli, zAli))

                newTs.write(properties=False)

                outputAliSetOfTiltSeries.setSamplingRate(
                    self.getPixSizeFromDimensions(xAli))
                outputAliSetOfTiltSeries.write()
                self._store(outputAliSetOfTiltSeries)
            """Output set of coordinates 3D (associated to the aligned tilt-series)"""
            coordFilePath = self.getFilePath(ts,
                                             suffix='fid',
                                             extension=".xyz")
            if os.path.exists(coordFilePath):
                if outputSetOfCoordinates3D is None:
                    outputSetOfCoordinates3D = self._createSetOfCoordinates3D(
                        volSet=outputAliSetOfTiltSeries,
                        suffix='LandmarkModel')
                    outputSetOfCoordinates3D.setSamplingRate(
                        self.inputTiltSeries.getSamplingRate())
                    outputSetOfCoordinates3D.setPrecedents(
                        outputAliSetOfTiltSeries)
                    self._defineOutputs(
                        outputSetOfCoordinates3D=outputSetOfCoordinates3D)
                    self._defineSourceRelation(self.inputSetOfTiltSeries,
                                               outputSetOfCoordinates3D)

                coordList = utils.format3DCoordinatesList(coordFilePath)
                for element in coordList:
                    newCoord3D = tomoObj.Coordinate3D()
                    newCoord3D.setVolume(ts)
                    newCoord3D.setX(element[0], constants.BOTTOM_LEFT_CORNER)
                    newCoord3D.setY(element[1], constants.BOTTOM_LEFT_CORNER)
                    newCoord3D.setZ(element[2], constants.BOTTOM_LEFT_CORNER)

                    newCoord3D.setVolId(ts.getObjId())
                    outputSetOfCoordinates3D.append(newCoord3D)
                    outputSetOfCoordinates3D.update(newCoord3D)
                outputSetOfCoordinates3D.write()
                self._store(outputSetOfCoordinates3D)
            """Landmark models with no gaps"""
            if (os.path.exists(
                    self.getFilePath(ts, suffix="_nogaps", extension=".fid"))
                    and os.path.exists(self.getFilePath(ts,
                                                        extension=".resid"))):

                paramsNoGapPoint2Model = {
                    'inputFile':
                    self.getFilePath(ts, suffix="_nogaps", extension=".fid"),
                    'outputFile':
                    self.getFilePath(ts,
                                     suffix="_nogaps_fid",
                                     extension=".txt")
                }

                argsNoGapPoint2Model = "-InputFile %(inputFile)s " \
                                       "-OutputFile %(outputFile)s"

                Plugin.runImod(self, 'model2point',
                               argsNoGapPoint2Model % paramsNoGapPoint2Model)

                if outputSetOfLandmarkModelsNoGaps is None:
                    outputSetOfLandmarkModelsNoGaps = self._createSetOfLandmarkModels(
                        suffix='NoGaps')
                    outputSetOfLandmarkModelsNoGaps.copyInfo(
                        self.inputTiltSeries)
                    self._defineOutputs(outputSetOfLandmarkModelsNoGaps=
                                        outputSetOfLandmarkModelsNoGaps)
                    self._defineSourceRelation(
                        self.inputSetOfTiltSeries,
                        outputSetOfLandmarkModelsNoGaps)

                fiducialNoGapFilePath = self.getFilePath(ts,
                                                         suffix="_nogaps_fid",
                                                         extension=".txt")

                fiducialNoGapList = utils.formatFiducialList(
                    fiducialNoGapFilePath)

                fiducialModelNoGapPath = self.getFilePath(ts,
                                                          suffix="_nogaps",
                                                          extension=".fid")
                landmarkModelNoGapsFilePath = self.getFilePath(
                    ts, suffix="_nogaps", extension=".sfid")
                landmarkModelNoGapsResidPath = self.getFilePath(
                    ts, extension=".resid")
                fiducialNoGapsResidList = utils.formatFiducialResidList(
                    landmarkModelNoGapsResidPath)
                landmarkModelNoGaps = tomoObj.LandmarkModel(
                    tsId, landmarkModelNoGapsFilePath, fiducialModelNoGapPath)

                prevTiltIm = 0
                chainId = 0
                indexFake = 0
                for fiducial in fiducialNoGapList:
                    if int(float(fiducial[2])) <= prevTiltIm:
                        chainId += 1
                    prevTiltIm = int(float(fiducial[2]))
                    if (indexFake < len(fiducialNoGapsResidList)
                            and fiducial[2]
                            == fiducialNoGapsResidList[indexFake][2]):
                        landmarkModelNoGaps.addLandmark(
                            xCoor=fiducial[0],
                            yCoor=fiducial[1],
                            tiltIm=fiducial[2],
                            chainId=chainId,
                            xResid=fiducialNoGapsResidList[indexFake][3],
                            yResid=fiducialNoGapsResidList[indexFake][4])
                        indexFake += 1
                    else:
                        landmarkModelNoGaps.addLandmark(xCoor=fiducial[0],
                                                        yCoor=fiducial[1],
                                                        tiltIm=fiducial[2],
                                                        chainId=chainId,
                                                        xResid='0',
                                                        yResid='0')

                outputSetOfLandmarkModelsNoGaps.append(landmarkModelNoGaps)
                outputSetOfLandmarkModelsNoGaps.write()
                self._store(outputSetOfLandmarkModelsNoGaps)
            """Full reconstructed tomogram"""
            reconstructTomoFilePath = self.getFilePath(ts,
                                                       suffix="_full",
                                                       extension=".rec")
            if os.path.exists(reconstructTomoFilePath):
                if outputSetOfFullTomograms is None:
                    outputSetOfFullTomograms = self._createSetOfTomograms(
                        suffix='Full')
                    outputSetOfFullTomograms.copyInfo(self.inputTiltSeries)
                    self._defineOutputs(
                        outputSetOfFullTomograms=outputSetOfFullTomograms)
                    self._defineSourceRelation(self.inputSetOfTiltSeries,
                                               outputSetOfFullTomograms)

                newTomogram = tomoObj.Tomogram()
                newTomogram.setLocation(reconstructTomoFilePath)
                newTomogram.setSamplingRate(ts.getSamplingRate())
                outputSetOfFullTomograms.append(newTomogram)
                outputSetOfFullTomograms.write()
                self._store(outputSetOfFullTomograms)
            """Post-processed reconstructed tomogram"""
            posprocessedRecTomoFilePath = self.getFilePath(ts,
                                                           extension=".rec")
            if os.path.exists(posprocessedRecTomoFilePath):
                if outputSetOfPostProcessTomograms is None:
                    outputSetOfPostProcessTomograms = self._createSetOfTomograms(
                    )
                    outputSetOfPostProcessTomograms.copyInfo(
                        self.inputTiltSeries)
                    self._defineOutputs(outputSetOfPostProcessTomograms=
                                        outputSetOfPostProcessTomograms)
                    self._defineSourceRelation(
                        self.inputSetOfTiltSeries,
                        outputSetOfPostProcessTomograms)

                newTomogram = tomoObj.Tomogram()
                newTomogram.setLocation(posprocessedRecTomoFilePath)
                outputSetOfPostProcessTomograms.append(newTomogram)
                outputSetOfPostProcessTomograms.write()
                self._store(outputSetOfPostProcessTomograms)
        self.closeMappers()
Example #11
0
    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
            })
Example #12
0
    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()
Example #13
0
    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 generateOutputStackStep(self, tsObjId):
        tomo = self.inputSetOfTomograms.get()[tsObjId]
        location = tomo.getLocation()[1]
        fileName, fileExtension = os.path.splitext(location)

        extraPrefix = self._getExtraPath(os.path.basename(fileName))
        tmpPrefix = self._getTmpPath(os.path.basename(fileName))
        path.makePath(extraPrefix)
        path.makePath(tmpPrefix)

        runNewstack = False

        paramsNewstack = {
            'input': location,
            'output': os.path.join(extraPrefix, os.path.basename(location)),
            'imagebinned': 1.0,
        }

        argsNewstack = "-input %(input)s " \
                       "-output %(output)s " \
                       "-imagebinned %(imagebinned)s "

        if self.floatDensities.get() != 0:
            runNewstack = True
            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:
            runNewstack = True
            argsNewstack += " -ModeToOutput " + str(self.getModeToOutput())

        if runNewstack:
            Plugin.runImod(self, 'newstack', argsNewstack % paramsNewstack)

        if self.binning.get() != 1:
            if runNewstack:
                path.moveFile(
                    os.path.join(extraPrefix, os.path.basename(location)),
                    os.path.join(tmpPrefix, os.path.basename(location)))
                inputTomoPath = os.path.join(tmpPrefix,
                                             os.path.basename(location))
            else:
                inputTomoPath = location

            paramsBinvol = {
                'input': inputTomoPath,
                'output': os.path.join(extraPrefix,
                                       os.path.basename(location)),
                'binning': self.binning.get(),
            }

            argsBinvol = "-input %(input)s " \
                         "-output %(output)s " \
                         "-binning %(binning)d "

            Plugin.runImod(self, 'binvol', argsBinvol % paramsBinvol)

        outputNormalizedSetOfTomograms = self.getOutputNormalizedSetOfTomograms(
        )

        newTomogram = Tomogram()
        newTomogram.copyInfo(tomo)
        newTomogram.copyAttributes(tomo, '_origin')

        if not runNewstack and self.binning.get() == 1:
            newTomogram.setLocation(location)
        else:
            newTomogram.setLocation(
                os.path.join(extraPrefix, os.path.basename(location)))
        if self.binning > 1:
            newTomogram.setSamplingRate(tomo.getSamplingRate() *
                                        int(self.binning.get()))
        outputNormalizedSetOfTomograms.append(newTomogram)
        outputNormalizedSetOfTomograms.update(newTomogram)
        outputNormalizedSetOfTomograms.write()
        self._store()
Example #15
0
    def eraseXraysStep(self, tsObjId):
        ts = self.inputSetOfTiltSeries.get()[tsObjId]

        tsId = ts.getTsId()
        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)

        paramsCcderaser = {
            'input':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName()),
            'output':
            os.path.join(extraPrefix,
                         ts.getFirstItem().parseFileName()),
            'findPeaks':
            1,
            'peakCriterion':
            self.peakCriterion.get(),
            'diffCriterion':
            self.diffCriterion.get(),
            'growCriterion':
            4,
            'scanCriterion':
            3,
            'maximumRadius':
            self.maximumRadius.get(),
            'giantCriterion':
            12,
            'extraLargeRadius':
            8,
            'bigDiffCriterion':
            self.bigDiffCriterion.get(),
            'annulusWidth':
            2.0,
            'xyScanSize':
            100,
            'edgeExclusionWidth':
            4,
            'pointModel':
            os.path.join(
                extraPrefix,
                ts.getFirstItem().parseFileName(suffix="_fid",
                                                extension=".mod")),
            'borderSize':
            2,
            'polynomialOrder':
            2,
        }

        argsCcderaser = "-InputFile %(input)s " \
                        "-OutputFile %(output)s " \
                        "-FindPeaks %(findPeaks)d " \
                        "-PeakCriterion %(peakCriterion).2f " \
                        "-DiffCriterion %(diffCriterion).2f " \
                        "-GrowCriterion %(growCriterion)d " \
                        "-ScanCriterion %(scanCriterion)d " \
                        "-MaximumRadius %(maximumRadius).2f " \
                        "-GiantCriterion %(giantCriterion)d " \
                        "-ExtraLargeRadius %(extraLargeRadius)d " \
                        "-BigDiffCriterion %(bigDiffCriterion)d " \
                        "-AnnulusWidth %(annulusWidth).2f " \
                        "-XYScanSize %(xyScanSize)d " \
                        "-EdgeExclusionWidth %(edgeExclusionWidth)d " \
                        "-BorderSize %(borderSize)d " \
                        "-PolynomialOrder %(polynomialOrder)d "

        Plugin.runImod(self, 'ccderaser', argsCcderaser % paramsCcderaser)
    def ctfEstimation(self, tsObjId):
        """Run ctfplotter IMOD program"""
        ts = self._getTiltSeries(tsObjId)
        tsSet = self._getSetOfTiltSeries()
        tsId = ts.getTsId()
        extraPrefix = self._getExtraPath(tsId)
        tmpPrefix = self._getTmpPath(tsId)

        paramsCtfPlotter = {
            'inputStack':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName()),
            'angleFile':
            os.path.join(tmpPrefix,
                         ts.getFirstItem().parseFileName(extension=".tlt")),
            'defocusFile':
            os.path.join(
                extraPrefix,
                ts.getFirstItem().parseFileName(extension=".defocus")),
            'axisAngle':
            self.axisAngle.get(),
            'pixelSize':
            tsSet.getSamplingRate() / 10,
            'expectedDefocus':
            self.getExpectedDefocus(tsId),
            'autoFitRangeAndStep':
            str(self.angleRange.get()) + "," + str(self.angleStep.get()),
            'voltage':
            tsSet.getAcquisition().getVoltage(),
            'sphericalAberration':
            tsSet.getAcquisition().getSphericalAberration(),
            'amplitudeContrast':
            tsSet.getAcquisition().getAmplitudeContrast(),
            'defocusTol':
            self.defocusTol.get(),
            'psResolution':
            101,
            'leftDefTol':
            self.leftDefTol.get(),
            'rightDefTol':
            self.rightDefTol.get(),
            'tileSize':
            self.tileSize.get(),
        }

        argsCtfPlotter = "-InputStack %(inputStack)s " \
                         "-AngleFile %(angleFile)s " \
                         "-DefocusFile %(defocusFile)s " \
                         "-AxisAngle %(axisAngle)f " \
                         "-PixelSize %(pixelSize)f " \
                         "-ExpectedDefocus %(expectedDefocus)f " \
                         "-AutoFitRangeAndStep %(autoFitRangeAndStep)s " \
                         "-Voltage %(voltage)d " \
                         "-SphericalAberration %(sphericalAberration)f " \
                         "-AmplitudeContrast %(amplitudeContrast)f " \
                         "-DefocusTol %(defocusTol)d " \
                         "-PSResolution %(psResolution)d " \
                         "-LeftDefTol %(leftDefTol)f " \
                         "-RightDefTol %(rightDefTol)f " \
                         "-tileSize %(tileSize)d "

        if self.startFreq.get() != 0 or self.endFreq.get() != 0:
            paramsCtfPlotter.update({
                'startFreq': self.startFreq.get(),
                'endFreq': self.endFreq.get(),
            })

            argsCtfPlotter += "-FrequencyRangeToFit %(startFreq)f,%(endFreq)f "

        if self.extraZerosToFit.get() != 0:
            paramsCtfPlotter.update({
                'extraZerosToFit':
                self.extraZerosToFit.get(),
            })

            argsCtfPlotter += "-ExtraZerosToFit %(extraZerosToFit)f "

        if self.skipAstigmaticViews.get() == 0:
            argsCtfPlotter += "-SkipOnlyForAstigPhase "

        if self.searchAstigmatism.get() == 0:
            paramsCtfPlotter.update({
                'maximumAstigmatism':
                self.maximumAstigmatism.get(),
                'numberOfSectors':
                self.numberSectorsAstigmatism.get(),
                'minimumViewsAstigmatism':
                self.minimumViewsAstigmatism.get()
            })

            argsCtfPlotter += "-SearchAstigmatism " \
                              "-MaximumAstigmatism %(maximumAstigmatism)f " \
                              "-NumberOfSectors %(numberOfSectors)d "

        if self.searchPhaseShift.get() == 0:
            paramsCtfPlotter.update(
                {'minimumViewsPhaseShift': self.minimumViewsPhaseShift.get()})

            argsCtfPlotter += "-SearchPhaseShift "

        if self.searchAstigmatism.get() == 0 and self.searchPhaseShift.get(
        ) == 0:
            argsCtfPlotter += "-MinViewsAstigAndPhase %(minimumViewsAstigmatism)d,%(minimumViewsPhaseShift)d "
        elif self.searchAstigmatism.get() == 0:
            argsCtfPlotter += "-MinViewsAstigAndPhase %(minimumViewsAstigmatism)d,0 "
        elif self.searchPhaseShift.get() == 0:
            argsCtfPlotter += "-MinViewsAstigAndPhase 0,%(minimumViewsPhaseShift)d "

        if self.searchCutOnFreq.get() == 0:

            if self.maximumCutOnFreq.get() == -1:
                argsCtfPlotter += "-SearchCutonFrequency "

            else:
                paramsCtfPlotter.update({
                    'maximumCutOnFreq':
                    self.maximumCutOnFreq.get(),
                })

                argsCtfPlotter += "-SearchCutonFrequency " \
                                  "-MaxCutOnToSearch %(maximumCutOnFreq)f "

        if not self._interactiveMode:
            argsCtfPlotter += "-SaveAndExit "

        Plugin.runImod(self, 'ctfplotter', argsCtfPlotter % paramsCtfPlotter)
    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()
Example #18
0
    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()