Example #1
0
    def create_movie_params(self, prot, updateIds):

        for movie in self.iter_updated_set(prot.outputMovies):
            movieId = movie.getObjId()
            if movieId in self.movies:  # this movie has been processed, skip
                continue
            movieFn = movie.getFileName()
            if self.numberOfFrames is None:
                self.numberOfFrames = movie.getNumberOfFrames()
                images_path = self.find_ispyb_path(movieFn)
                self.imageGenerator = ImageGenerator(self.project.path,
                                                     images_path,
                                                     bigThumb=True,
                                                     smallThumb=512)
            acquisition = movie.getAcquisition()

            self.movies[movieId] = {
                'experimenttype': 'single particle',
                'run_status': 'DataCollection Successful',
                'imgdir': abspath(dirname(movieFn)),
                'imgsuffix': pwutils.getExt(movieFn),
                'file_template': pwutils.removeBaseExt(movieFn) + '#####' + pwutils.getExt(movieFn),
                'file_location': abspath(dirname(movieFn)),
                'filename': movieFn,
                'n_passes': self.numberOfFrames,
                'magnification': acquisition.getMagnification(),
                'total_absorbed_dose': acquisition.getDoseInitial() + (acquisition.getDosePerFrame() * self.numberOfFrames),
                'wavelength': self.convert_volts_to_debroglie_wavelength(acquisition.getVoltage())
            }
            self.dataCollection.update(self.movies[movieId])
            self.update_from_metadata(Path(movieFn), self.movies[movieId])
            updateIds.append(movieId)
Example #2
0
    def _doShowDeformedOrigRef(self, param=None):

        scriptFile = self.protocol._getPath('morph_deformed_ref_chimera.cxc')
        fhCmd = open(scriptFile, 'w')
        fnbase = removeExt(self.protocol._getFileName('fnOutVol'))
        ext = getExt(self.protocol._getFileName('fnOutVol'))
        fninput = abspath(fnbase + ext[0:4])
        smprt = self.protocol.outputVolume.getSamplingRate()

        fnbase2 = removeExt(self.protocol._getFileName('fnRefVol'))
        ext2 = getExt(self.protocol._getFileName('fnRefVol'))
        fnref = abspath(fnbase2 + ext2[0:4])

        fhCmd.write(self.OPEN_FILE % fninput)
        fhCmd.write(self.OPEN_FILE % fnref)
        counter = 1
        fhCmd.write(self.VOXEL_SIZE % (counter, str(smprt)))
        fhCmd.write(self.VOL_HIDE % counter)
        counter += 1
        fhCmd.write(self.VOXEL_SIZE % (counter, str(smprt)))
        # fhCmd.write("focus\n")
        fhCmd.write(self.VOL_HIDE % counter)
        fhCmd.write("volume morph #%d,%d frames 500\n" %
                    (counter - 1, counter))
        fhCmd.write(self.VIEW)
        fhCmd.close()

        view = ChimeraView(scriptFile)
        return [view]
    def convertInputStep(self):
        """ Read the input volume.
        """

        self.vol1Fn = self.half1.get().getFileName()
        self.vol2Fn = self.half2.get().getFileName()
        extVol1 = getExt(self.vol1Fn)
        extVol2 = getExt(self.vol2Fn)
        if (extVol1 == '.mrc') or (extVol1 == '.map'):
            self.vol1Fn = self.vol1Fn + ':mrc'
        if (extVol2 == '.mrc') or (extVol2 == '.map'):
            self.vol2Fn = self.vol2Fn + ':mrc'

        if self.useMask.get():
            if (not self.Mask.hasValue()):
                self.ifNomask(self.vol1Fn)
            else:
                self.maskFn = self.Mask.get().getFileName()

            extMask = getExt(self.maskFn)

            if (extMask == '.mrc') or (extMask == '.map'):
                self.maskFn = self.maskFn + ':mrc'

            if self.Mask.hasValue():
                params = ' -i %s' % self.maskFn
                params += ' -o %s' % self._getFileName(BINARY_MASK)
                params += ' --select below %f' % 0.5  # Mask threshold = 0.5 self.maskthreshold.get()
                params += ' --substitute binarize'

                self.runJob('xmipp_transform_threshold', params)
    def createChimeraScript(self):
        fnRoot = "extra/"
        scriptFile = self.protocol._getPath('Chimera_resolution.cmd')
        fhCmd = open(scriptFile, 'w')
        imageFile = self.protocol._getExtraPath(OUTPUT_RESOLUTION_FILE_CHIMERA)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        min_Res = round(np.amin(imgData) * 100) / 100
        max_Res = round(np.amax(imgData) * 100) / 100

        numberOfColors = 21
        colors_labels = self.numberOfColors(min_Res, max_Res, numberOfColors)
        colorList = self.colorMapToColorList(colors_labels, self.getColorMap())

        if self.protocol.halfVolumes.get() is True:
            #fhCmd.write("open %s\n" % (fnRoot+FN_MEAN_VOL)) #Perhaps to check the use of mean volume is useful
            fnbase = removeExt(self.protocol.inputVolume.get().getFileName())
            ext = getExt(self.protocol.inputVolume.get().getFileName())
            fninput = abspath(fnbase + ext[0:4])
            fhCmd.write("open %s\n" % fninput)
        else:
            fnbase = removeExt(self.protocol.inputVolumes.get().getFileName())
            ext = getExt(self.protocol.inputVolumes.get().getFileName())
            fninput = abspath(fnbase + ext[0:4])
            fhCmd.write("open %s\n" % fninput)
        fhCmd.write("open %s\n" % (fnRoot + OUTPUT_RESOLUTION_FILE_CHIMERA))
        if self.protocol.halfVolumes.get() is True:
            smprt = self.protocol.inputVolume.get().getSamplingRate()
        else:
            smprt = self.protocol.inputVolumes.get().getSamplingRate()
        fhCmd.write("volume #0 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("volume #1 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("vol #1 hide\n")

        scolorStr = '%s,%s:' * numberOfColors
        scolorStr = scolorStr[:-1]

        line = ("scolor #0 volume #1 perPixel false cmap " + scolorStr +
                "\n") % colorList
        fhCmd.write(line)

        scolorStr = '%s %s ' * numberOfColors
        str_colors = ()
        for idx, elem in enumerate(colorList):
            if (idx % 2 == 0):
                if ((idx % 8) == 0):
                    str_colors += str(elem),
                else:
                    str_colors += '" "',
            else:
                str_colors += elem,

        line = ("colorkey 0.01,0.05 0.02,0.95 " + scolorStr +
                "\n") % str_colors
        fhCmd.write(line)

        fhCmd.close()
    def createChimeraScript(self):
        fnRoot = "extra/"
        scriptFile = self.protocol._getPath('Chimera_resolution.cmd')
        fhCmd = open(scriptFile, 'w')
        imageFile = self.protocol._getExtraPath(OUTPUT_RESOLUTION_FILE_CHIMERA)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        min_Res = round(np.amin(imgData)*100)/100
        max_Res = round(np.amax(imgData)*100)/100

        numberOfColors = 21
        colors_labels = self.numberOfColors(min_Res, max_Res, numberOfColors)
        colorList = self.colorMapToColorList(colors_labels, self.getColorMap())
        
        if self.protocol.halfVolumes.get() is True:
            #fhCmd.write("open %s\n" % (fnRoot+FN_MEAN_VOL)) #Perhaps to check the use of mean volume is useful
            fnbase = removeExt(self.protocol.inputVolume.get().getFileName())
            ext = getExt(self.protocol.inputVolume.get().getFileName())
            fninput = abspath(fnbase + ext[0:4])
            fhCmd.write("open %s\n" % fninput)
        else:
            fnbase = removeExt(self.protocol.inputVolumes.get().getFileName())
            ext = getExt(self.protocol.inputVolumes.get().getFileName())
            fninput = abspath(fnbase + ext[0:4])
            fhCmd.write("open %s\n" % fninput)
        fhCmd.write("open %s\n" % (fnRoot + OUTPUT_RESOLUTION_FILE_CHIMERA))
        if self.protocol.halfVolumes.get() is True:
            smprt = self.protocol.inputVolume.get().getSamplingRate()
        else:
            smprt = self.protocol.inputVolumes.get().getSamplingRate()
        fhCmd.write("volume #0 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("volume #1 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("vol #1 hide\n")
        
        scolorStr = '%s,%s:' * numberOfColors
        scolorStr = scolorStr[:-1]

        line = ("scolor #0 volume #1 perPixel false cmap " + scolorStr + "\n") % colorList
        fhCmd.write(line)

        scolorStr = '%s %s ' * numberOfColors
        str_colors = ()
        for idx, elem in enumerate(colorList):
            if (idx % 2 == 0):
                if ((idx % 8) == 0):
                    str_colors +=  str(elem),
                else:
                    str_colors += '" "',
            else:
                str_colors += elem,
        
        line = ("colorkey 0.01,0.05 0.02,0.95 " + scolorStr + "\n") % str_colors
        fhCmd.write(line)

        fhCmd.close()
 def convertInputStep(self):
     """ Read the input volume.
     """
     self.volFn = self.inputVolume.get().getFileName()
     self.resFn = self.resolutionVolume.get().getFileName()
     extVol = getExt(self.volFn)
     extRes = getExt(self.resFn)
     if (extVol == '.mrc') or (extVol == '.map'):
         self.volFn = self.volFn + ':mrc'
     if (extRes == '.mrc') or (extRes == '.map'):
         self.resFn = self.resFn + ':mrc'
 def convertInputStep(self):
     """ Read the input volume.
     """
     self.volFn = self.inputVolume.get().getFileName()
     self.resFn = self.resolutionVolume.get().getFileName()      
     extVol = getExt(self.volFn)
     extRes = getExt(self.resFn)        
     if (extVol == '.mrc') or (extVol == '.map'):
         self.volFn = self.volFn + ':mrc'
     if (extRes == '.mrc') or (extRes == '.map'):
         self.resFn = self.resFn + ':mrc'     
    def convertInputStep(self):
        """ Read the input volume.
        """
        
        self.micsFn = self._getPath()

        if self.halfVolumes:
            self.vol1Fn = self.inputVolume.get().getFileName()
            self.vol2Fn = self.inputVolume2.get().getFileName()
            extVol1 = getExt(self.vol1Fn)
            extVol2 = getExt(self.vol2Fn)
            if (extVol1 == '.mrc') or (extVol1 == '.map'):
                self.vol1Fn = self.vol1Fn + ':mrc'
            if (extVol2 == '.mrc') or (extVol2 == '.map'):
                self.vol2Fn = self.vol2Fn + ':mrc'
                
            if not self.Mask.hasValue():
                self.ifNomask(self.vol1Fn)
            else:
                self.maskFn = self.Mask.get().getFileName()

            self.inputVolumes.set(None)
        else:
            self.vol0Fn = self.inputVolumes.get().getFileName()
            extVol0 = getExt(self.vol0Fn)
            if (extVol0 == '.mrc') or (extVol0 == '.map'):
                self.vol0Fn = self.vol0Fn + ':mrc'
                
            if not self.Mask.hasValue():
                self.ifNomask(self.vol0Fn)
            else:
                self.maskFn = self.Mask.get().getFileName()
            
            print self.maskFn
            
            self.inputVolume.set(None)
            self.inputVolume2.set(None)


        extMask = getExt(self.maskFn)
        if (extMask == '.mrc') or (extMask == '.map'):
            self.maskFn = self.maskFn + ':mrc'
            
        if self.Mask.hasValue():
            params = ' -i %s' % self.maskFn
            params += ' -o %s' % self._getFileName(BINARY_MASK)
            params += ' --select below %f' % self.maskthreshold.get()
            params += ' --substitute binarize'
             
            self.runJob('xmipp_transform_threshold', params)
    def convertInputStep(self):
        """ Read the input volume.
        """

        self.micsFn = self._getPath()

        if self.halfVolumes:
            self.vol1Fn = self.inputVolume.get().getFileName()
            self.vol2Fn = self.inputVolume2.get().getFileName()
            extVol1 = getExt(self.vol1Fn)
            extVol2 = getExt(self.vol2Fn)
            if (extVol1 == '.mrc') or (extVol1 == '.map'):
                self.vol1Fn = self.vol1Fn + ':mrc'
            if (extVol2 == '.mrc') or (extVol2 == '.map'):
                self.vol2Fn = self.vol2Fn + ':mrc'

            if not self.Mask.hasValue():
                self.ifNomask(self.vol1Fn)
            else:
                self.maskFn = self.Mask.get().getFileName()

            self.inputVolumes.set(None)
        else:
            self.vol0Fn = self.inputVolumes.get().getFileName()
            extVol0 = getExt(self.vol0Fn)
            if (extVol0 == '.mrc') or (extVol0 == '.map'):
                self.vol0Fn = self.vol0Fn + ':mrc'

            if not self.Mask.hasValue():
                self.ifNomask(self.vol0Fn)
            else:
                self.maskFn = self.Mask.get().getFileName()

            print self.maskFn

            self.inputVolume.set(None)
            self.inputVolume2.set(None)

        extMask = getExt(self.maskFn)
        if (extMask == '.mrc') or (extMask == '.map'):
            self.maskFn = self.maskFn + ':mrc'

        if self.Mask.hasValue():
            params = ' -i %s' % self.maskFn
            params += ' -o %s' % self._getFileName(BINARY_MASK)
            params += ' --select below %f' % self.maskthreshold.get()
            params += ' --substitute binarize'

            self.runJob('xmipp_transform_threshold', params)
    def convertInputStep(self):
        """ Read the input volume.
        """
        self.micsFn = self._getPath()

        self.volFn = self.inputVolume.get().getFileName()
        self.maskFn = self.Mask.get().getFileName()

        extVol = getExt(self.volFn)
        if (extVol == '.mrc') or (extVol == '.map'):
            self.volFn = self.volFn + ':mrc'

        extMask = getExt(self.maskFn)
        if (extMask == '.mrc') or (extMask == '.map'):
            self.maskFn = self.maskFn + ':mrc'
Example #11
0
 def getEmpiarFormat(self, imagePath):
     ext = pwutils.getExt(imagePath).lower().strip('.')
     imgFormat = self._imageSetFormats.get(ext, None)
     if imgFormat is None:
         raise EmpiarMappingError('Image format not recognized: %s' % ext)
     else:
         return imgFormat, ''
    def _pickMicrograph(self, mic, args):
        # Prepare mic folder and convert if needed
        micName = mic.getFileName()
        micDir = self._getTmpPath(pwutils.removeBaseExt(micName))
        pwutils.makePath(micDir)

        ih = ImageHandler()
        # If needed convert micrograph to mrc format, otherwise link it
        if pwutils.getExt(micName) != ".mrc":
            fnMicBase = pwutils.replaceBaseExt(micName, 'mrc')
            inputMic = os.path.join(micDir, fnMicBase)
            ih.convert(mic.getLocation(), inputMic)
        else:
            inputMic = os.path.join(micDir, os.path.basename(micName))
            pwutils.createLink(micName, inputMic)

        # Prepare environment
        from appion import Plugin
        Plugin.getEnviron()

        # Program to execute and it arguments
        program = "python2"
        outputFile = self._getExtraPath(pwutils.replaceBaseExt(
            inputMic, "txt"))

        args += " --image=%s --outfile=%s" % (inputMic, outputFile)

        dogpicker = Plugin.getHome("ApDogPicker.py")
        args = dogpicker + " " + args

        self.runJob(program, args)
Example #13
0
 def getDimensions(self, locationObj):
     """ It will return a tuple with the images dimensions.
     The tuple will contains:
         (x, y, z, n) where x, y, z are image dimensions (z=1 for 2D) and 
         n is the number of elements if stack.
     """
     if self.existsLocation(locationObj):
         location = self._convertToLocation(locationObj)
         fn = location[1]
         ext = pwutils.getExt(fn).lower()
         
         if ext == '.png' or ext == '.jpg':
             im = PIL.Image.open(fn)
             x, y = im.size # (width,height) tuple
             return x, y, 1, 1
         elif ext == '.img':
             # FIXME Since now we can not read dm4 format in Scipion natively
             # or recent .img format
             # we are opening an Eman2 process to read the .img files
             from pyworkflow.em.packages.eman2.convert import getImageDimensions
             return getImageDimensions(fn) # we are ignoring index here
         else:
             self._img.read(location, xmipp.HEADER)
             return self._img.getDimensions()
     else:
         return None, None, None, None
    def getFinalGainPath(self, tifFlipped=False):
        fnBest = self.getOrientedGainPath()
        if os.path.exists(fnBest):
            # If the best orientatin has been calculated, take it
            finalGainFn = fnBest
        elif self.getInputGain() != None:
            # Elif, take the input gain provided
            finalGainFn = self.getInputGain()
        else:
            # Elif, take the estimated gain
            finalGainFn = self.searchEstimatedGainPath()
            if finalGainFn == None:
                # If no gains have been estimated, estimate one and use that
                firstMovie = self.inputMovies.get().getFirstItem()
                movieId = firstMovie.getObjId()
                if not movieId in self.estimatedIds:
                    self.estimatedIds.append(movieId)
                    self.estimateGainFun(firstMovie)
                finalGainFn = self.getEstimatedGainPath(movieId)

        ext = pwutils.getExt(
            self.inputMovies.get().getFirstItem().getFileName()).lower()
        if ext in ['.tif', '.tiff'] and tifFlipped:
            finalGainFn = xmutils.flipYImage(finalGainFn,
                                             outDir=self._getExtraPath())

        return finalGainFn
Example #15
0
    def convertStep(self, volFn, volDim, volSr, minDim, maxSr, nVoli):
        Xdim = volDim
        Ts = volSr
        newTs = self.targetResolution.get() * 1.0 / 3.0
        newTs = max(maxSr, newTs)
        newXdim = Xdim * Ts / newTs
        newRmax = self.Rmax.get() * Ts / newTs
        self.newRmax = min(newRmax, self.Rmax.get())
        fnOut = os.path.splitext(volFn)[0]
        fnOut = self._getExtraPath(
            os.path.basename(fnOut + self.OUTPUT_SUFFIX % nVoli))

        ih = ImageHandler()
        volFn = volFn if getExt(volFn) == '.vol' else volFn + ':mrc'
        if Xdim != newXdim:
            self.runJob("xmipp_image_resize",
                        "-i %s -o %s --dim %d" % (volFn, fnOut, newXdim))

        else:
            ih.convert(volFn, fnOut)

        if newXdim > minDim:
            self.runJob(
                "xmipp_transform_window",
                " -i %s -o %s --crop %d" % (fnOut, fnOut, (newXdim - minDim)))
 def convertPdbStep(self):
     vol1 = self.vol1.get()
     pdbFn = self._getPdbFileName()
     self.outFile = self._getVolName()
     if getExt(pdbFn) == ".cif":
         pdbFn2 = replaceBaseExt(pdbFn, 'pdb')
         cifToPdb(pdbFn, pdbFn2)
         pdbFn = pdbFn2
     samplingR = vol1.getSamplingRate()
     size = vol1.getDim()
     ccp4header = headers.Ccp4Header(vol1.getFileName(), readHeader=True)
     self.shifts = ccp4header.getOrigin()
     # convert pdb to volume with the size and origin of the input volume
     args = ' -i %s --sampling %f -o %s --size %d %d %d --orig %d %d %d' % \
            (pdbFn, samplingR, removeExt(self.outFile), size[2], size[1], size[0], self.shifts[0]/samplingR,
             self.shifts[1]/samplingR, self.shifts[2]/samplingR)
     program = "xmipp_volume_from_pdb"
     self.runJob(program, args)
     volpdbmrc = "%s.mrc" % removeExt(self.outFile)
     # convert volume from pdb to .mrc in order to store the origin in the mrc header
     args2 = ' -i %s -o %s -t vol' % (self.outFile, volpdbmrc)
     program2 = "xmipp_image_convert"
     self.runJob(program2, args2)
     # write origin in mrc header of volume from pdb
     ccp4headerOut = headers.Ccp4Header(volpdbmrc, readHeader=True)
     ccp4headerOut.setOrigin(self.shifts)
Example #17
0
 def getFilePreview(self, objFile):
     self._imgPreview = None
     self._imgInfo = None
     filename = objFile.getPath()
     ext = getExt(filename)
     
     if ext == '.xmd' or ext == '.ctfparam' or ext == '.pos' or ext == '.doc':
         msg = "*Metadata File* "
         blocks = xmipp.getBlocksInMetaDataFile(filename)
         nblocks = len(blocks)
         if nblocks <= 1:
             mdStr = self._getMdString(filename)
             msg += "  (single block)\n"
             if self._imgInfo:
                 msg += "\nFirst item: \n" + self._imgInfo
             msg += '\n' + mdStr
         else:
             mdStr = self._getMdString(filename, blocks[0])
             msg += "  (%d blocks) " % nblocks
             if self._imgInfo:
                 msg += "\nFirst item: \n" + self._imgInfo
             msg += "\nFirst block: \n" + mdStr
             msg += "\nAll blocks:" + ''.join(["\n  - %s" % b for b in blocks])
     elif ext == '.star':
         msg = "*Relion STAR file* \n"
         msg += self._getMdString(filename)
         
     return self._imgPreview, msg
Example #18
0
    def _convert(self, image):
        imageFn = image.getFileName()

        if self.outputDir is None:
            return imageFn

        ext = pwutils.getExt(imageFn)[1:]
        if ext in self.extensions:
            finalExt = ext
            convertFunc = pwutils.createAbsLink
        else:
            finalExt = self.extensions[0]
            convertFunc = self._ih.convert

        if self.useBaseName:
            newName = pwutils.replaceBaseExt(image.getFileName(), finalExt)
        else:
            newName = "%s_%06d.%s" % (self._prefix, image.getObjId(), finalExt)

        newPath = os.path.join(self.outputDir, newName)
        convertFunc(os.path.abspath(imageFn), newPath)
        # If there is a rootDir defined, we should return the path relative
        # to that location, probably to be used from the star file
        if self.rootDir is not None:
            newPath = os.path.relpath(newPath, self.rootDir)

        return newPath
 def getEmpiarFormat(self, imagePath):
     ext = pwutils.getExt(imagePath).lower().strip('.')
     imgFormat = self._imageSetFormats.get(ext, 'OT')
     if imgFormat == 'OT':
         return 'OT', ext
     else:
         return imgFormat, ''
Example #20
0
    def convertPdbStep(self):
        """ Although is not mandatory, usually is used by the protocol to
        register the resulting outputs in the database.
        """
        pdbFn = self._getPdbFileName()
        outFile = removeExt(self._getVolName())
        if getExt(pdbFn) == ".cif":
            pdbFn2 = replaceBaseExt(pdbFn, 'pdb')
            cifToPdb(pdbFn, pdbFn2)
            pdbFn = pdbFn2

        args = '-i %s --sampling %f -o %s' % (pdbFn, self.sampling.get(),
                                              outFile)

        if self.centerPdb:
            args += ' --centerPDB'

        if self.setSize:
            args += ' --size'

        if self.size.hasValue():
            args += ' %d' % self.size.get()

        self.info("Input file: " + pdbFn)
        self.info("Output file: " + outFile)

        program = "xmipp_volume_from_pdb"
        self.runJob(program, args)
Example #21
0
 def getDimensions(self, locationObj):
     """ It will return a tuple with the images dimensions.
     The tuple will contains:
         (x, y, z, n) where x, y, z are image dimensions (z=1 for 2D) and 
         n is the number of elements if stack.
     """
     
     if self.existsLocation(locationObj):
         
         location = self._convertToLocation(locationObj)
         fn = location[1]
         ext = getExt(fn).lower()
         
         if ext == '.png' or ext == '.jpg':
             im = PIL.Image.open(fn)
             x, y = im.size # (width,height) tuple
             return x, y, 1, 1
         
         else:
             self._img.read(location, xmipp.HEADER)
             x, y, z, n = self._img.getDimensions()
             return x, y, z, n
     
     else: 
         return None, None, None, None
Example #22
0
 def getDimensions(self, locationObj):
     """ It will return a tuple with the images dimensions.
     The tuple will contains:
         (x, y, z, n) where x, y, z are image dimensions (z=1 for 2D) and 
         n is the number of elements if stack.
     """
     if self.existsLocation(locationObj):
         location = self._convertToLocation(locationObj)
         fn = location[1]
         ext = pwutils.getExt(fn).lower()
         
         if ext == '.png' or ext == '.jpg':
             im = PIL.Image.open(fn)
             x, y = im.size # (width,height) tuple
             return x, y, 1, 1
         elif ext == '.img':
             # FIXME Since now we can not read dm4 format in Scipion natively
             # or recent .img format
             # we are opening an Eman2 process to read the .img files
             from pyworkflow.em.packages.eman2.convert import getImageDimensions
             return getImageDimensions(fn) # we are ignoring index here
         else:
             self._img.read(location, xmipp.HEADER)
             return self._img.getDimensions()
     else:
         return None, None, None, None
Example #23
0
    def _doShowStrain(self, param=None):

        scriptFile = self.protocol._getPath('strain_chimera.cxc')
        fhCmd = open(scriptFile, 'w')
        fnbase = removeExt(self.protocol._getFileName('fnInputVol'))
        ext = getExt(self.protocol._getFileName('fnInputVol'))
        fninput = abspath(fnbase + ext[0:4])
        smprt = self.protocol.outputVolume.getSamplingRate()

        fnbase2 = removeExt(self.protocol._getFileName('fnOutVol'))
        fnStrain = abspath(fnbase2)

        fhCmd.write(self.OPEN_FILE % fninput)
        fhCmd.write(self.OPEN_FILE % (fnStrain + "_strain.mrc"))
        counter = 1
        fhCmd.write(self.VOXEL_SIZE % (counter, str(smprt)))
        counter += 1
        fhCmd.write(self.VOXEL_SIZE % (counter, str(smprt)))
        fhCmd.write(self.VOL_HIDE % counter)
        fhCmd.write('color sample #%d map #%d palette rainbow\n' %
                    (counter - 1, counter))
        fhCmd.write(self.VIEW)
        fhCmd.close()

        view = ChimeraView(scriptFile)
        return [view]
Example #24
0
    def getFilePreview(self, objFile):
        self._imgPreview = None
        self._imgInfo = None
        filename = objFile.getPath()
        ext = getExt(filename)

        if ext == '.xmd' or ext == '.ctfparam' or ext == '.pos' or ext == '.doc':
            msg = "*Metadata File* "
            blocks = xmippLib.getBlocksInMetaDataFile(filename)
            nblocks = len(blocks)
            if nblocks <= 1:
                mdStr = self._getMdString(filename)
                msg += "  (single block)\n"
                if self._imgInfo:
                    msg += "\nFirst item: \n" + self._imgInfo
                msg += '\n' + mdStr
            else:
                mdStr = self._getMdString(filename, blocks[0])
                msg += "  (%d blocks) " % nblocks
                if self._imgInfo:
                    msg += "\nFirst item: \n" + self._imgInfo
                msg += "\nFirst block: \n" + mdStr
                msg += "\nAll blocks:" + ''.join(
                    ["\n  - %s" % b for b in blocks])
        elif ext == '.star':
            msg = "*Relion STAR file* \n"
            msg += self._getMdString(filename)

        return self._imgPreview, msg
Example #25
0
def needToFlipOnY(filename):
    """ Returns true if need to flip coordinates on Y"""
    ext = pwutils.getExt(filename)

    if ext in ".mrc":
        header = Ccp4Header(filename, readHeader=True)
        return header.getISPG() != 0  # ISPG 1, cryolo will not flip the image

    return ext in constants.CRYOLO_SUPPORTED_FORMATS
Example #26
0
    def getFileFormat(cls, fileName):

        ext = getExt(fileName)
        if (ext == '.mrc') or (ext == '.map'):
            return cls.MRC
        elif (ext == '.spi') or (ext == '.vol'):
            return cls.SPIDER
        else:
            return cls.UNKNOWNFORMAT
Example #27
0
 def getTypeFromFile(self, fileName):
     '''Returns the expected BioPython file type according to the filename'''
     ext = getExt(fileName)
     if ext == '.fasta':
         type = 'fasta'
     elif ext == '.genbank' or ext == '.gb':
         type = 'genbank'
     else:
         type = 'fasta'
     return type
Example #28
0
def getFileFormat(fileName):
    ext = getExt(fileName)
    if ext in [
            '.mrc', '.map', '.mrcs', '.mrc:mrc', '.mrc:mrcs', '.st', '.rec'
    ]:
        return MRC
    elif ext == '.spi' or ext == '.vol':
        return SPIDER
    else:
        return UNKNOWNFORMAT
 def _getOutputMicrograph(self, mic):
     """ Return the name of the output micrograph, given
     the input Micrograph object.
     """
     fn = mic.getFileName()
     extFn = getExt(fn)
     if extFn != ".mrc":
         fn = replaceExt(fn, "mrc")
     fnOut = self._getExtraPath(basename(fn))
     return fnOut
Example #30
0
    def convertInputStep(self):
        """ Read the input volume.
        """
        if (self.halfVolumes.get() is False):
            extVol0 = getExt(self.vol0Fn)
            if (extVol0 == '.mrc') or (extVol0 == '.map'):
                self.vol0Fn = self.vol0Fn + ':mrc'

        if self.halfVolumes.get() is True:
            extVol1 = getExt(self.vol1Fn)
            extVol2 = getExt(self.vol2Fn)
            if (extVol1 == '.mrc') or (extVol1 == '.map'):
                self.vol1Fn = self.vol1Fn + ':mrc'
            if (extVol2 == '.mrc') or (extVol2 == '.map'):
                self.vol2Fn = self.vol2Fn + ':mrc'

        extMask = getExt(self.maskFn)
        if (extMask == '.mrc') or (extMask == '.map'):
            self.maskFn = self.maskFn + ':mrc'
    def writeSetOfSubtomograms(self, subtomoSet, subtomosStar, isPyseg=False):
        currentTomo = ''
        MRC = 'mrc'
        ih = ImageHandler()
        tomoTable = self._createStarTomoTable(isPyseg)
        tmpDir = pwutils.getParentFolder(subtomosStar)
        for subtomo in subtomoSet:
            if pwutils.getExt(subtomo.getFileName().replace(':' + MRC,
                                                            '')) != '.' + MRC:
                mrcDir = join(tmpDir,
                              pwutils.removeBaseExt(subtomo.getVolName()))
                if currentTomo != subtomo.getVolName():
                    mkdir(mrcDir)
                mrcFile = join(
                    mrcDir, pwutils.replaceBaseExt(subtomo.getFileName(), MRC))
                ih.convert(subtomo.getFileName(), mrcFile)
            angles, shifts = self._getTransformInfoFromSubtomo(subtomo)
            magn = subtomo.getAcquisition().getMagnification()
            rlnMicrographName = subtomo.getVolName()
            rlnCoordinateX = subtomo.getCoordinate3D().getX(BOTTOM_LEFT_CORNER)
            rlnCoordinateY = subtomo.getCoordinate3D().getY(BOTTOM_LEFT_CORNER)
            rlnCoordinateZ = subtomo.getCoordinate3D().getZ(BOTTOM_LEFT_CORNER)
            rlnImageName = subtomo.getFileName().replace(':' + MRC, '')
            rlnCtfImage = self._getCTFFileFromSubtomo(subtomo)
            rlnMagnification = magn if magn else 10000  #64000
            rlnDetectorPixelSize = subtomo.getSamplingRate()
            rlnAngleRot = angles[0]
            rlnAngleTilt = angles[1]
            rlnAnglePsi = angles[2]
            rlnOriginX = shifts[0]
            rlnOriginY = shifts[1]
            rlnOriginZ = shifts[2]
            rlnTiltPrior = subtomo._tiltPriorAngle.get() if hasattr(
                subtomo, '_tiltPriorAngle') else rlnAngleTilt
            rlnTiltPsi = subtomo._psiPriorAngle.get() if hasattr(
                subtomo, '_psiPriorAngle') else rlnAnglePsi
            # Add row to the table which will be used to generate the STAR file
            fieldsToAdd = [
                rlnMicrographName, rlnCoordinateX, rlnCoordinateY,
                rlnCoordinateZ, rlnImageName, rlnCtfImage, rlnMagnification,
                rlnDetectorPixelSize, rlnAngleRot, rlnAngleTilt, rlnTiltPrior,
                rlnAnglePsi, rlnTiltPsi, rlnOriginX, rlnOriginY, rlnOriginZ
            ]
            if isPyseg:
                fieldsToAdd = [
                    rlnMicrographName, rlnCoordinateX, rlnCoordinateY,
                    rlnCoordinateZ, rlnImageName, rlnCtfImage, rlnAngleRot,
                    rlnAngleTilt, rlnAnglePsi, rlnOriginX, rlnOriginY,
                    rlnOriginZ
                ]

            tomoTable.addRow(*fieldsToAdd)

        # Write the STAR file
        tomoTable.write(subtomosStar)
Example #32
0
    def _visualize(self, obj, **args):
        cls = type(obj)
        if issubclass(cls, Volume) or issubclass(cls, Image):
            fn = obj.getFileName()
            if pwutils.getExt(fn) in ['.stk', '.vol', '.xmp']:
                # eman2 expects spider files with .spi extension only
                pwutils.createLink(fn, pwutils.replaceExt(fn, 'spi'))
                fn = pwutils.replaceExt(fn, 'spi')
            view = CommandView("%s %s &" %
                               (Plugin.getProgram('e2display.py'), fn),
                               env=Plugin.getEnviron())
            return [view]

        elif isinstance(obj, EmanProtBoxing):
            coords = obj.getCoords()
            if coords:
                return DataViewer._visualize(self, obj.outputCoordinates)

        elif isinstance(obj, EmanProtInitModel):
            obj = obj.outputVolumes
            fn = obj.getFileName()
            labels = 'id enabled comment _filename '
            objCommands = "'%s' '%s' '%s'" % (OBJCMD_CLASSAVG_PROJS,
                                              OBJCMD_PROJS, OBJCMD_INITVOL)

            self._views.append(
                ObjectView(self._project,
                           obj.strId(),
                           fn,
                           viewParams={
                               showj.MODE: showj.MODE_MD,
                               showj.VISIBLE: labels,
                               showj.RENDER: '_filename',
                               showj.OBJCMDS: objCommands
                           }))
            return self._views

        elif isinstance(obj, EmanProtInitModelSGD):
            obj = obj.outputVolumes
            fn = obj.getFileName()
            labels = 'id enabled comment _filename '
            objCommands = "'%s'" % OBJCMD_CLASSAVG_PROJS

            self._views.append(
                ObjectView(self._project,
                           obj.strId(),
                           fn,
                           viewParams={
                               showj.MODE: showj.MODE_MD,
                               showj.VISIBLE: labels,
                               showj.RENDER: '_filename',
                               showj.OBJCMDS: objCommands
                           }))
            return self._views
 def mrc_convert(self, fileName, outputFileName):
     """Check if the extension is .mrc, if not then uses xmipp to convert it
     """
     ext = getExt(fileName)
     if (ext != '.mrc') and (ext != '.map'):
         params = ' -i "%s"' % fileName
         params += ' -o "%s"' % outputFileName
         self.runJob('xmipp_image_convert', params)
         return outputFileName + ':mrc'
     else:
         return fileName + ':mrc'
 def _getOutputMicrograph(self, mic):
     """ Return the name of the output micrograph, given
     the input Micrograph object.
     """
     fn = mic.getFileName()
     extFn = getExt(fn)
     if extFn != ".mrc":
         fn = replaceExt(fn, "mrc")
     
     fnOut = self._getExtraPath(basename(fn))
     
     return fnOut
Example #35
0
    def cleanFileNameAnnotation(self, fileName):
        ext = getExt(fileName)
        self.isMovie = False

        if ext == '.mrcs':
            self.isMovie = True
        elif ext == '.mrc:mrcs':  # Movie --> dims = [X, Y, Z = 1, N]
            self.isMovie = True
            fileName = fileName.replace(':mrcs', '')
        elif ext == '.mrc:mrc':  # Volume --> dims = [X, Y, Z, N = 1]
            fileName = fileName.replace(':mrc', '')

        return fileName
    def createChimeraScriptDoA(self, infile, outfile, ellipfile):
        fnRoot = "extra/"
        scriptFile = self.protocol._getPath(outfile)
        fhCmd = open(scriptFile, 'w')
        imageFile = self.protocol._getExtraPath(infile)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        min_Res = 0.0  #round(np.amin(imgData)*100)/100
        max_Res = 1.0  #round(np.amax(imgData)*100)/100

        numberOfColors = 21
        colors_labels = self.numberOfColors(min_Res, max_Res, numberOfColors)
        colorList = self.colorMapToColorList(colors_labels, self.getColorMap())

        fnbase = removeExt(self.protocol.inputVolumes.get().getFileName())
        ext = getExt(self.protocol.inputVolumes.get().getFileName())
        fninput = abspath(fnbase + ext[0:4])
        fhCmd.write("open %s\n" % fninput)
        fhCmd.write("open %s\n" % (fnRoot + infile))

        fhCmd.write("open %s\n" % (fnRoot + ellipfile))
        smprt = self.protocol.inputVolumes.get().getSamplingRate()
        fhCmd.write("volume #0 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("volume #1 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("volume #2 voxelSize %s\n" % (str(smprt)))
        fhCmd.write("volume #2 style mesh\n")
        fhCmd.write("vol #1 hide\n")

        scolorStr = '%s,%s:' * numberOfColors
        scolorStr = scolorStr[:-1]

        line = ("scolor #0 volume #1 perPixel false cmap " + scolorStr +
                "\n") % colorList
        fhCmd.write(line)

        scolorStr = '%s %s ' * numberOfColors
        str_colors = ()
        for idx, elem in enumerate(colorList):
            if (idx % 2 == 0):
                if ((idx % 8) == 0):
                    str_colors += str(elem),
                else:
                    str_colors += '" "',
            else:
                str_colors += elem,

        line = ("colorkey 0.01,0.05 0.02,0.95 " + scolorStr +
                "\n") % str_colors
        fhCmd.write(line)

        fhCmd.close()
Example #37
0
    def create_movie_params(self, prot, allParams):

        for movie in self.iter_updated_set(prot.outputMovies):
            movieFn = movie.getFileName()
            if self.numberOfFrames is None:
                self.numberOfFrames = movie.getNumberOfFrames()
                images_path = self.find_ispyb_path(movieFn)
                self.imageGenerator = ImageGenerator(self.project.path,
                                                     images_path,
                                                     smallThumb=512)

            movieId = movie.getObjId()

            allParams[movieId] = {
                'id': self.allIds.get(movieId, None),
                'imgdir': dirname(movieFn),
                'imgprefix': pwutils.removeBaseExt(movieFn),
                'imgsuffix': pwutils.getExt(movieFn),
                'file_template': movieFn,
                'n_images': self.numberOfFrames
             }
Example #38
0
    def _pickMicrograph(self, mic, args):
        # Prepare mic folder and convert if needed
        micName = mic.getFileName()
        micDir = self._getTmpPath(pwutils.removeBaseExt(micName))
        pwutils.makePath(micDir)

        ih = ImageHandler()
        # If needed convert micrograph to mrc format, otherwise link it
        if pwutils.getExt(micName) != ".mrc":
            fnMicBase = pwutils.replaceBaseExt(micName, 'mrc')
            inputMic = os.path.join(micDir, fnMicBase)
            ih.convert(mic.getLocation(), inputMic)
        else:
            inputMic = os.path.join(micDir, os.path.basename(micName))
            pwutils.createLink(micName, inputMic)

        # Program to execute and it arguments
        program = "ApDogPicker.py"
        outputFile = self._getExtraPath(pwutils.replaceBaseExt(inputMic, "txt"))

        args += " --image=%s --outfile=%s" % (inputMic, outputFile)

        self.runJob(program, args)
Example #39
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 #40
0
 def getFileHandler(self, obj):
     filename = obj.getFileName()
     fileExt = getExt(filename)
     return self._FILE_HANDLERS.get(fileExt, self._DEFAULT_HANDLER)
Example #41
0
 def _getConvertExtension(self, filename):
     """ Check wether it is needed to convert to .mrc or not """
     ext = pwutils.getExt(filename).lower()
     return None if ext in ['.mrc', '.mrcs', '.tiff', '.tif'] else 'mrc'