def polishStep(self, params):
     """ Create the input file in STAR format as expected by Relion.
     If the input particles comes from Relion, just link the file. 
     """
     from pyworkflow.utils.path import copyTree
     copyTree(self.refineRun.get()._getExtraPath(), self._getExtraPath())
     self.runJob(self._getProgram('relion_particle_polish'), params, env=getEnviron(self.refineRun.get().useRelion14))
Example #2
0
    def __init__(self, inputFile, **kwargs):

        # Small trick to handle .vol Spider volumes
        if inputFile.endswith('.vol'):
            inputFile += ":spi"

        CommandView.__init__(self, 'bshow "%s" &' % inputFile,
                             env=getEnviron(), **kwargs)
Example #3
0
    def __init__(self, inputFile, **kwargs):

        # Small trick to handle .vol Spider volumes
        if inputFile.endswith('.vol'):
            inputFile += ":spi"

        CommandView.__init__(self,
                             'bshow "%s" &' % inputFile,
                             env=getEnviron(),
                             **kwargs)
Example #4
0
This sub-package contains data and protocol classes
wrapping Kai Zhang's GCTF program
"""
import os

_logo = "gctf_logo.png"
GCTF_HOME = 'GCTF_HOME'

from bibtex import _bibtex  # Load bibtex dict with references
from convert import getEnviron

from protocol_gctf import ProtGctf

# Wizards
from wizard import GctfCTFWizard
_environ = getEnviron()

# We need this import to register the specific viewing command
# when visualizing Gctf results
from viewer import GctfViewer


def validateInstallation():
    """ This function will be used to check if package is properly installed."""
    missingPaths = [
        "%s: %s" % (var, _environ[var]) for var in [GCTF_HOME]
        if not os.path.exists(_environ[var])
    ]

    if missingPaths:
        return ["Missing variables:"] + missingPaths
Example #5
0
This sub-package contains cryoMethods protocols and tools.
"""

# from bibtex import _bibtex # Load bibtex dict with references
_logo = "cryomethods_logo.png"
_references = []
import os
from convert import getSupportedVersions, getVersion, getEnviron
from protocol_volume_selector import ProtInitialVolumeSelector

# Wizards
from wizard import (TargetFilterWizard, InitialPassFilterWizard,
                    MaskDiameterWizard)

#Viewer
from viewer import VolumeSelectorViewer

_environ = getEnviron()


def validateInstallation():
    """ This function will be used to check if RELION is properly installed. """
    missingPaths = ["%s: %s" % (var, _environ[var])
                    for var in ['RELION_HOME']
                    if not os.path.exists(_environ[var])]

    if missingPaths:
        return ["Missing variables:"] + missingPaths
    else:
        return [] # No errors
    def _processMovie(self, movie):
        inputMovies = self.inputMovies.get()
        movieFolder = self._getOutputMovieFolder(movie)
        outputMicFn = self._getRelPath(self._getOutputMicName(movie),
                                       movieFolder)
        outputMovieFn = self._getRelPath(self._getOutputMovieName(movie),
                                         movieFolder)
        movieBaseName = pwutils.removeExt(movie.getFileName())
        aveMicFn = movieBaseName + '_uncorrected_avg.mrc'
        logFile = self._getRelPath(self._getMovieLogFile(movie), movieFolder)

        a0, aN = self._getRange(movie, 'align')

        if not self.useMotioncor2:
            # Get the number of frames and the range to be used
            # for alignment and sum
            s0, sN = self._getRange(movie, 'sum')

            argsDict = {
                '-crx': self.cropOffsetX.get(),
                '-cry': self.cropOffsetY.get(),
                '-cdx': self.cropDimX.get(),
                '-cdy': self.cropDimY.get(),
                '-bin': self.binFactor.get(),
                '-nst': '%d' % a0,
                '-ned': '%d' % aN,
                '-nss': '%d' % s0,
                '-nes': '%d' % sN,
                '-gpu': self.GPUIDs.get(),
                '-flg': logFile,
            }

            args = '"%s" ' % movie.getBaseName()
            args += ' '.join(
                ['%s %s' % (k, v) for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -fgr "%s"' % inputMovies.getGain()

            if inputMovies.getDark():
                args += ' -fdr "%s"' % inputMovies.getDark()

            if self.doSaveAveMic:
                args += ' -fcs "%s" ' % outputMicFn

            if self.doSaveMovie:
                args += ' -fct "%s" -ssc 1' % outputMovieFn

            args += ' ' + self.extraParams.get()
            program = MOTIONCORR_PATH

        else:
            logFileBase = (logFile.replace('0-Full.log',
                                           '').replace('0-Patch-Full.log', ''))
            # default values for motioncor2 are (1, 1)
            cropDimX = self.cropDimX.get() or 1
            cropDimY = self.cropDimY.get() or 1

            numbOfFrames = self._getNumberOfFrames(movie)

            if self.doApplyDoseFilter:
                preExp, dose = self._getCorrectedDose(inputMovies)
            else:
                preExp, dose = 0.0, 0.0

            # reset values = 1 to 0 (motioncor2 does it automatically,
            # but we need to keep this for consistency)
            if self.patchX.get() == 1:
                self.patchX.set(0)
            if self.patchY.get() == 1:
                self.patchY.set(0)

            argsDict = {
                '-OutMrc': '"%s"' % outputMicFn,
                '-Patch': '%d %d' % (self.patchX, self.patchY),
                '-MaskCent': '%d %d' % (self.cropOffsetX, self.cropOffsetY),
                '-MaskSize': '%d %d' % (cropDimX, cropDimY),
                '-FtBin': self.binFactor.get(),
                '-Tol': self.tol.get(),
                '-Group': self.group.get(),
                '-FmDose': dose,
                '-Throw': '%d' % a0,
                '-Trunc': '%d' % (abs(aN - numbOfFrames + 1)),
                '-PixSize': inputMovies.getSamplingRate(),
                '-kV': inputMovies.getAcquisition().getVoltage(),
                '-Gpu': self.GPUIDs.get(),
                '-LogFile': logFileBase,
            }
            if getVersion('MOTIONCOR2') != '03162016':
                argsDict['-InitDose'] = preExp
                argsDict['-OutStack'] = 1 if self.doSaveMovie else 0

            if self.isSemVersion():
                if self.defectFile.get():
                    argsDict['-DefectFile'] = self.defectFile.get()

                if self.versionGE(
                        '1.0.1'):  # Patch overlap was introduced in 1.0.1
                    patchOverlap = self.getAttributeValue('patchOverlap', None)
                    if patchOverlap:  # 0 or None is False
                        argsDict['-Patch'] += " %d" % patchOverlap

            if self._supportsMagCorrection() and self.doMagCor:
                if self.useEst:
                    inputEst = self.inputEst.get().getOutputLog()
                    if getVersion('MOTIONCOR2') == '01302017':
                        input_params = parseMagCorrInput(inputEst)
                        # this version uses stretch parameters as following:
                        # 1/maj, 1/min, -angle
                        argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (
                            1.0 / input_params[1], 1.0 / input_params[2],
                            -1 * input_params[0])
                    else:
                        # While motioncor2 >=1.0.0 uses estimation params AS IS
                        input_params = parseMagEstOutput(inputEst)
                        argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (
                            input_params[1], input_params[2], input_params[0])
                else:
                    argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (
                        self.scaleMaj, self.scaleMin, self.angDist)

            ext = pwutils.getExt(movie.getFileName()).lower()

            if ext in ['.mrc', '.mrcs']:
                args = ' -InMrc "%s" ' % movie.getBaseName()
            elif ext in ['.tif', '.tiff']:
                args = ' -InTiff "%s" ' % movie.getBaseName()
            else:
                raise Exception("Unsupported format: %s" % ext)

            args += ' '.join(
                ['%s %s' % (k, v) for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -Gain "%s" ' % inputMovies.getGain()

            if inputMovies.getDark():
                args += ' -Dark "%s"' % inputMovies.getDark()

            args += ' ' + self.extraParams2.get()
            program = MOTIONCOR2_PATH

        try:
            self.runJob(program,
                        args,
                        cwd=movieFolder,
                        env=getEnviron(self.useMotioncor2))
            self._fixMovie(movie)

            # Compute PSDs
            outMicFn = self._getExtraPath(self._getOutputMicName(movie))
            if not os.path.exists(outMicFn):
                # if only DW mic is saved
                outMicFn = self._getExtraPath(self._getOutputMicWtName(movie))

            if self.doComputePSD:
                # Compute uncorrected avg mic
                roi = [
                    self.cropOffsetX.get(),
                    self.cropOffsetY.get(),
                    self.cropDimX.get(),
                    self.cropDimY.get()
                ]
                fakeShiftsFn = self.writeZeroShifts(movie)
                self.averageMovie(movie,
                                  fakeShiftsFn,
                                  aveMicFn,
                                  binFactor=self.binFactor.get(),
                                  roi=roi,
                                  dark=inputMovies.getDark(),
                                  gain=inputMovies.getGain())

                self.computePSDs(movie,
                                 aveMicFn,
                                 outMicFn,
                                 outputFnCorrected=self._getPsdJpeg(movie))

            self._saveAlignmentPlots(movie)

            if self._doComputeMicThumbnail():
                self.computeThumbnail(
                    outMicFn, outputFn=self._getOutputMicThumbnail(movie))
        except:
            print("ERROR: Movie %s failed\n" % movie.getName())
Example #7
0
    def _processMovie(self, movie):
        inputMovies = self.inputMovies.get()
        movieFolder = self._getOutputMovieFolder(movie)
        outputMicFn = self._getRelPath(self._getOutputMicName(movie),
                                       movieFolder)
        outputMovieFn = self._getRelPath(self._getOutputMovieName(movie),
                                         movieFolder)
        movieBaseName = pwutils.removeExt(movie.getFileName())
        aveMicFn = movieBaseName + '_uncorrected_avg.mrc'
        logFile = self._getRelPath(self._getMovieLogFile(movie),
                                   movieFolder)

        a0, aN = self._getRange(movie, 'align')

        if not self.useMotioncor2:
            # Get the number of frames and the range to be used
            # for alignment and sum
            s0, sN = self._getRange(movie, 'sum')

            argsDict = {'-crx': self.cropOffsetX.get(),
                        '-cry': self.cropOffsetY.get(),
                        '-cdx': self.cropDimX.get(),
                        '-cdy': self.cropDimY.get(),
                        '-bin': self.binFactor.get(),
                        '-nst': '%d' % a0,
                        '-ned': '%d' % aN,
                        '-nss': '%d' % s0,
                        '-nes': '%d' % sN,
                        '-flg': logFile,
                        }

            args = '"%s" ' % movie.getBaseName()
            args += ' '.join(
                ['%s %s' % (k, v) for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -fgr "%s"' % inputMovies.getGain()

            if inputMovies.getDark():
                args += ' -fdr "%s"' % inputMovies.getDark()

            if self.doSaveAveMic:
                args += ' -fcs "%s" ' % outputMicFn

            if self.doSaveMovie:
                args += ' -fct "%s" -ssc 1' % outputMovieFn

            args += ' -gpu %(GPU)s'
            args += ' ' + self.extraParams.get()
            program = MOTIONCORR_PATH

        else:
            logFileBase = (logFile.replace('0-Full.log', '').replace(
                '0-Patch-Full.log', ''))
            # default values for motioncor2 are (1, 1)
            cropDimX = self.cropDimX.get() or 1
            cropDimY = self.cropDimY.get() or 1

            numbOfFrames = self._getNumberOfFrames(movie)

            if self.doApplyDoseFilter:
                preExp, dose = self._getCorrectedDose(inputMovies)
            else:
                preExp, dose = 0.0, 0.0
            
            # reset values = 1 to 0 (motioncor2 does it automatically,
            # but we need to keep this for consistency)
            if self.patchX.get() == 1:
                self.patchX.set(0)
            if self.patchY.get() == 1:
                self.patchY.set(0)

            argsDict = {'-OutMrc': '"%s"' % outputMicFn,
                        '-Patch': '%d %d' % (self.patchX, self.patchY),
                        '-MaskCent': '%d %d' % (self.cropOffsetX,
                                                self.cropOffsetY),
                        '-MaskSize': '%d %d' % (cropDimX, cropDimY),
                        '-FtBin': self.binFactor.get(),
                        '-Tol': self.tol.get(),
                        '-Group': self.group.get(),
                        '-FmDose': dose,
                        '-Throw': '%d' % a0,
                        '-Trunc': '%d' % (abs(aN - numbOfFrames + 1)),
                        '-PixSize': inputMovies.getSamplingRate(),
                        '-kV': inputMovies.getAcquisition().getVoltage(),
                        '-LogFile': logFileBase,
                        }
            if getVersion('MOTIONCOR2') != '03162016':
                argsDict['-InitDose'] = preExp
                argsDict['-OutStack'] = 1 if self.doSaveMovie else 0

            if self.isSemVersion():
                if self.defectFile.get():
                    argsDict['-DefectFile'] = self.defectFile.get()

                if self.versionGE('1.0.1'):  # Patch overlap was introduced in 1.0.1
                    patchOverlap = self.getAttributeValue('patchOverlap', None)
                    if patchOverlap: # 0 or None is False
                        argsDict['-Patch'] += " %d" % patchOverlap

            if self._supportsMagCorrection() and self.doMagCor:
                if self.useEst:
                    inputEst = self.inputEst.get().getOutputLog()
                    if getVersion('MOTIONCOR2') == '01302017':
                        input_params = parseMagCorrInput(inputEst)
                        # this version uses stretch parameters as following:
                        # 1/maj, 1/min, -angle
                        argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (
                            1.0 / input_params[1],
                            1.0 / input_params[2],
                            -1 * input_params[0])
                    else:
                        # While motioncor2 >=1.0.0 uses estimation params AS IS
                        input_params = parseMagEstOutput(inputEst)
                        argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (
                            input_params[1],
                            input_params[2],
                            input_params[0])
                else:
                    argsDict['-Mag'] = '%0.3f %0.3f %0.3f' % (self.scaleMaj,
                                                              self.scaleMin,
                                                              self.angDist)

            ext = pwutils.getExt(movie.getFileName()).lower()

            if ext in ['.mrc', '.mrcs']:
                args = ' -InMrc "%s" ' % movie.getBaseName()
            elif ext in ['.tif', '.tiff']:
                args = ' -InTiff "%s" ' % movie.getBaseName()
            else:
                raise Exception("Unsupported format: %s" % ext)

            args += ' '.join(['%s %s' % (k, v)
                              for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -Gain "%s" ' % inputMovies.getGain()

            if inputMovies.getDark():
                args += ' -Dark "%s"' % inputMovies.getDark()

            args += ' -Gpu %(GPU)s'
            args += ' ' + self.extraParams2.get()
            program = MOTIONCOR2_PATH

        try:
            self.runJob(program, args, cwd=movieFolder,
                        env=getEnviron(self.useMotioncor2))
            self._fixMovie(movie)

            # Compute PSDs
            outMicFn = self._getExtraPath(self._getOutputMicName(movie))
            if not os.path.exists(outMicFn):
                # if only DW mic is saved
                outMicFn = self._getExtraPath(self._getOutputMicWtName(movie))

            def _extraWork():
                if self.doComputePSD:
                    # Compute uncorrected avg mic
                    roi = [self.cropOffsetX.get(), self.cropOffsetY.get(),
                           self.cropDimX.get(), self.cropDimY.get()]
                    fakeShiftsFn = self.writeZeroShifts(movie)
                    self.averageMovie(movie, fakeShiftsFn, aveMicFn,
                                      binFactor=self.binFactor.get(),
                                      roi=roi, dark=inputMovies.getDark(),
                                      gain=inputMovies.getGain())

                    self.computePSDs(movie, aveMicFn, outMicFn,
                                     outputFnCorrected=self._getPsdJpeg(movie))

                self._saveAlignmentPlots(movie)

                if self._doComputeMicThumbnail():
                    self.computeThumbnail(outMicFn,
                                          outputFn=self._getOutputMicThumbnail(
                                              movie))
                # This protocols takes control of clean up the temporary movie folder
                # which is required mainly when using a thread for this extra work
                self._cleanMovieFolder(movieFolder)

            if self._useWorkerThread():
                thread = Thread(target=_extraWork)
                thread.start()
            else:
                _extraWork()
        except:
            print("ERROR: Movie %s failed\n" % movie.getName())
Example #8
0
    def _processMovie(self, movie):
        inputMovies = self.inputMovies.get()
        movieFolder = self._getOutputMovieFolder(movie)
        outputMicFn = self._getRelPath(self._getOutputMicName(movie),
                                       movieFolder)
        outputMovieFn = self._getRelPath(self._getOutputMovieName(movie),
                                         movieFolder)
        movieBaseName = pwutils.removeExt(movie.getFileName())
        aveMicFn = movieBaseName + '_uncorrected_avg.mrc'
        logFile = self._getRelPath(self._getMovieLogFile(movie), movieFolder)

        a0, aN = self._getRange(movie, 'align')

        if not self.useMotioncor2:
            # Get the number of frames and the range to be used
            # for alignment and sum
            s0, sN = self._getRange(movie, 'sum')

            argsDict = {
                '-crx': self.cropOffsetX.get(),
                '-cry': self.cropOffsetY.get(),
                '-cdx': self.cropDimX.get(),
                '-cdy': self.cropDimY.get(),
                '-bin': self.binFactor.get(),
                '-nst': '%d' % a0,
                '-ned': '%d' % aN,
                '-nss': '%d' % s0,
                '-nes': '%d' % sN,
                '-gpu': self.GPUIDs.get(),
                '-flg': logFile,
            }

            args = '"%s" ' % movie.getBaseName()
            args += ' '.join(
                ['%s %s' % (k, v) for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -fgr "%s"' % inputMovies.getGain()

            if inputMovies.getDark():
                args += ' -fdr "%s"' % inputMovies.getDark()

            if self.doSaveAveMic:
                args += ' -fcs "%s" ' % outputMicFn

            if self.doSaveMovie:
                args += ' -fct "%s" -ssc 1' % outputMovieFn

            args += ' ' + self.extraParams.get()
            program = MOTIONCORR_PATH

        else:
            logFileBase = (logFile.replace('0-Full.log',
                                           '').replace('0-Patch-Full.log', ''))
            # default values for motioncor2 are (1, 1)
            cropDimX = self.cropDimX.get() or 1
            cropDimY = self.cropDimY.get() or 1

            numbOfFrames = self._getNumberOfFrames(movie)

            if self.doApplyDoseFilter:
                preExp, dose = self._getCorrectedDose(inputMovies)
            else:
                preExp, dose = 0.0, 0.0

            argsDict = {
                '-OutMrc': '"%s"' % outputMicFn,
                '-Patch': '%d %d' % (self.patchX, self.patchY),
                '-MaskCent': '%d %d' % (self.cropOffsetX, self.cropOffsetY),
                '-MaskSize': '%d %d' % (cropDimX, cropDimY),
                '-FtBin': self.binFactor.get(),
                '-Tol': self.tol.get(),
                '-Group': self.group.get(),
                '-FmDose': dose,
                '-Throw': '%d' % a0,
                '-Trunc': '%d' % (abs(aN - numbOfFrames + 1)),
                '-PixSize': inputMovies.getSamplingRate(),
                '-kV': inputMovies.getAcquisition().getVoltage(),
                '-Gpu': self.GPUIDs.get(),
                '-LogFile': logFileBase,
            }
            if getVersion('MOTIONCOR2') != '03162016':
                argsDict['-InitDose'] = preExp
                argsDict['-OutStack'] = 1 if self.doSaveMovie else 0

            if self._supportsMagCorrection() and self.doMagCor:
                if self.useEst:
                    inputEst = self.inputEst.get().getOutputLog()
                    input_params = parseMagCorrInput(inputEst)
                    # mag dist angle is inverted due to a different convention
                    argsDict['-Mag'] = '%0.2f %0.2f %0.2f' % (
                        input_params[1], input_params[2], -1 * input_params[0])
                else:
                    argsDict['-Mag'] = '%0.2f %0.2f %0.2f' % (
                        self.scaleMaj.get(), self.scaleMin.get(),
                        self.angDist.get())

            args = ' -InMrc "%s" ' % movie.getBaseName()
            args += ' '.join(
                ['%s %s' % (k, v) for k, v in argsDict.iteritems()])

            if inputMovies.getGain():
                args += ' -Gain "%s" ' % inputMovies.getGain()

            args += ' ' + self.extraParams2.get()
            program = MOTIONCOR2_PATH

        try:
            self.runJob(program,
                        args,
                        cwd=movieFolder,
                        env=getEnviron(self.useMotioncor2))
            self._fixMovie(movie)

            # Compute PSDs
            outMicFn = self._getExtraPath(self._getOutputMicName(movie))
            if not os.path.exists(outMicFn):
                # if only DW mic is saved
                outMicFn = self._getExtraPath(self._getOutputMicWtName(movie))

            if self.doComputePSD:
                uncorrectedPSD = movieBaseName + '_uncorrected'
                correctedPSD = movieBaseName + '_corrected'
                # Compute uncorrected avg mic
                roi = [
                    self.cropOffsetX.get(),
                    self.cropOffsetY.get(),
                    self.cropDimX.get(),
                    self.cropDimY.get()
                ]
                fakeShiftsFn = self.writeZeroShifts(movie)
                self.averageMovie(movie,
                                  fakeShiftsFn,
                                  aveMicFn,
                                  binFactor=self.binFactor.get(),
                                  roi=roi,
                                  dark=None,
                                  gain=inputMovies.getGain())

                self.computePSD(aveMicFn, uncorrectedPSD)
                self.computePSD(outMicFn, correctedPSD)
                self.composePSD(uncorrectedPSD + ".psd", correctedPSD + ".psd",
                                self._getPsdCorr(movie))

            self._saveAlignmentPlots(movie)

            if self._doComputeMicThumbnail():
                self.computeThumbnail(
                    outMicFn, outputFn=self._getOutputMicThumbnail(movie))
        except:
            print("ERROR: Movie %s failed\n" % movie.getName())
Example #9
0
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307  USA
# *
# *  All comments concerning this program package may be sent to the
# *  e-mail address '*****@*****.**'
# *
# **************************************************************************
"""
This sub-package contains data and protocol classes
wrapping Pytom programs http://www.pytom.org
"""
from bibtex import _bibtex # Load bibtex dict with references

_logo = 'pytomLogo.jpg'

import convert


_environ = convert.getEnviron()

from protocol_import import ProtPyTomImport
from protocol_autofocus_classify import ProtAutofocusClassify
from protocol_frm_alignment import ProtFrmAlign

from viewer import PyTomAutofocusViewer, PyTomFrmAlignmentViewer