def getMinMax(self, imageFile):
     img = ImageHandler().read(imageFile)
     imgData = img.getData()
     imgData = imgData[imgData != 0]
     min_res = round(np.amin(imgData) * 100) / 100
     max_res = round(np.amax(imgData) * 100) / 100
     median_res = round(np.median(imgData) * 100) / 100
     return min_res, max_res, median_res
Example #2
0
    def sharpeningAndMonoResStep(self):
        last_Niters = -1
        last_lambda_sharpening = 1e38
        nextIter = True

        while nextIter is True:
            self.iteration = self.iteration + 1
            #print iteration
            print('\n====================\n'
                  'Iteration  %s' % (self.iteration))
            self.sharpenStep(self.iteration)
            mtd = md.MetaData()
            mtd.read(self._getFileName('METADATA_PARAMS_SHARPENING'))

            lambda_sharpening = mtd.getValue(MDL_COST, 1)
            Niters = mtd.getValue(MDL_ITER, 1)

            #             if (Niters == last_Niters):
            #                 nextIter = False
            #                 break

            if (abs(lambda_sharpening - last_lambda_sharpening) <= 0.2):
                nextIter = False
                break

            last_Niters = Niters
            last_lambda_sharpening = lambda_sharpening

            self.MonoResStep(self.iteration)

            imageFile = self._getFileName('OUTPUT_RESOLUTION_FILE')

            img = ImageHandler().read(imageFile)
            imgData = img.getData()
            max_res = np.amax(imgData)
            min_res = 2 * self.inputVolume.get().getSamplingRate()

            #print ("minres %s  y maxres  %s"  % (min_res, max_res))

            if (max_res - min_res < 0.75):
                nextIter = False
                break

        # TODO: please copy the file using python not the operating system
        os.system('cp ' + self._getExtraPath('sharpenedMap_' +
                                             str(self.iteration) + '.mrc') +
                  ' ' + self._getExtraPath('sharpenedMap_last.mrc'))

        resFile = self.resolutionVolume.get().getFileName()
        pathres = dirname(resFile)
        if not exists(os.path.join(pathres, 'mask_data.xmd')):

            print(
                '\n====================\n'
                ' WARNING---This is not the ideal case because resolution map has been imported.'
                ' The ideal case is to calculate it previously'
                ' in the same project using MonoRes.'
                '\n====================\n')
    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 sharpeningAndMonoResStep(self):
        last_Niters = -1
        last_lambda_sharpening = 1e38
        nextIter = True
        iteration = 0
        while nextIter is True:
            iteration = iteration + 1
            #print iteration
            print ('\n====================\n'
                'Iteration  %s'  % (iteration))
            self.sharpenStep(iteration)           
            mtd = md.MetaData()
            mtd.read(self._getFileName('METADATA_PARAMS_SHARPENING'))
            
            lambda_sharpening = mtd.getValue(MDL_COST,1)
            Niters = mtd.getValue(MDL_ITER,1)
            
#             if (Niters == last_Niters):
#                 nextIter = False
#                 break
               
            if (abs(lambda_sharpening - last_lambda_sharpening)<= 0.2):
                nextIter = False   
                break

            last_Niters = Niters
            last_lambda_sharpening = lambda_sharpening
            
            self.MonoResStep(iteration)
            
            imageFile = self._getFileName('OUTPUT_RESOLUTION_FILE')

            img = ImageHandler().read(imageFile)
            imgData = img.getData()
            max_res = np.amax(imgData)
            min_res = 2*self.inputVolume.get().getSamplingRate()
            
            #print ("minres %s  y maxres  %s"  % (min_res, max_res))          
        
            if (max_res-min_res<0.75):
                nextIter = False
                break
                
        os.system('cp '  +self._getExtraPath('sharpenedMap_'+str(iteration)+'.mrc')+
                   ' '  +self._getExtraPath('sharpenedMap_last.mrc'))
        
        resFile = self.resolutionVolume.get().getFileName()        
        pathres=dirname(resFile)
        if  not exists(os.path.join(pathres,'mask_data.xmd')):     

            print ('\n====================\n' 
                   ' WARNING---This is not the ideal case because resolution map has been imported.'
                   ' The ideal case is to calculate it previously' 
                   ' in the same project using MonoRes.'
                   '\n====================\n')           
Example #6
0
 def _plotHistogram(self, param=None):
     imageFile = self.protocol._getFileName(FN_RESOLMAP)
     img = ImageHandler().read(imageFile)
     imgData = img.getData()
     imgList = imgData.flatten()
     imgListNoZero = filter(lambda x: x > 0, imgList)
     nbins = 30
     plotter = EmPlotter(x=1, y=1, mainTitle="  ")
     plotter.createSubPlot("Resolution histogram", "Resolution (A)",
                           "# of Counts")
     fig = plotter.plotHist(imgListNoZero, nbins)
     return [plotter]
Example #7
0
    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()
    def checkBackgroundStep(self): 
        
        initRes=self.resolutionVolume.get().getFileName() 

        img = ImageHandler().read(initRes)
        imgData = img.getData()
        max_value = np.amax(imgData)
        min_value = np.amin(imgData) 
        #print ("minvalue %s  y maxvalue  %s"  % (min_value, max_value)) 
        
        if (min_value > 0.01):
            params = ' -i %s' % self.resFn  
            params += ' -o %s' % self.resFn    
            params += ' --select above %f' % (max_value-1)   
            params += ' --substitute value 0'
            
            self.runJob('xmipp_transform_threshold', params)  
    def checkBackgroundStep(self):

        initRes = self.resolutionVolume.get().getFileName()

        img = ImageHandler().read(initRes)
        imgData = img.getData()
        max_value = np.amax(imgData)
        min_value = np.amin(imgData)
        #print ("minvalue %s  y maxvalue  %s"  % (min_value, max_value))

        if (min_value > 0.01):
            params = ' -i %s' % self.resFn
            params += ' -o %s' % self.resFn
            params += ' --select above %f' % (max_value - 1)
            params += ' --substitute value 0'

            self.runJob('xmipp_transform_threshold', params)
    def _showVolumeColorSlices(self, param=None):
        imageFile = self.protocol.resolution_Volume.getFileName()
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        max_Res = np.amax(imgData)

        #  This is to generate figures for the paper
        # min_Res = np.amin(imgData)
        # imgData2 = imgData
        imgData2 = np.ma.masked_where(imgData < 0.1, imgData, copy=True)
        
        min_Res = np.amin(imgData2)
        fig, im = self._plotVolumeSlices('MonoRes slices', imgData2,
                                         min_Res, max_Res, self.getColorMap(), dataAxis=self._getAxis())
        cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
        cbar = fig.colorbar(im, cax=cax)
        cbar.ax.invert_yaxis()

        return plt.show(fig)
    def _showVolumeColorSlices(self, param=None):
        imageFile = self.protocol._getExtraPath(OUTPUT_RESOLUTION_FILE)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        max_Res = np.amax(imgData)

        #  This is to generate figures for the paper
        # min_Res = np.amin(imgData)
        # imgData2 = imgData
        imgData2 = np.ma.masked_where(imgData < 0.1, imgData, copy=True)
        
        min_Res = np.amin(imgData2)
        fig, im = self._plotVolumeSlices('MonoRes slices', imgData2,
                                         min_Res, max_Res, self.getColorMap(), dataAxis=self._getAxis())
        cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
        cbar = fig.colorbar(im, cax=cax)
        cbar.ax.invert_yaxis()

        return plt.show(fig)
    def MonoResStep(self, iter):
        sampling = self.inputVolume.get().getSamplingRate()

        if (iter == 1):
            resFile = self.resolutionVolume.get().getFileName()
        else:
            resFile = self._getFileName('OUTPUT_RESOLUTION_FILE')

        pathres = dirname(resFile)

        img = ImageHandler().read(resFile)
        imgData = img.getData()
        max_res = np.amax(imgData)

        significance = 0.95

        mtd = md.MetaData()
        if exists(os.path.join(pathres, 'mask_data.xmd')):
            mtd.read(os.path.join(pathres, 'mask_data.xmd'))
            radius = mtd.getValue(MDL_SCALE, 1)
        else:
            xdim, _ydim, _zdim = self.inputVolume.get().getDim()
            radius = xdim * 0.5

        params = ' --vol %s' % self._getExtraPath('sharpenedMap_' + str(iter) +
                                                  '.mrc')
        params += ' --mask %s' % self._getFileName('BINARY_MASK')
        params += ' --sampling_rate %f' % sampling
        params += ' --minRes %f' % (2 * sampling)
        params += ' --maxRes %f' % max_res
        params += ' --step %f' % 0.25
        params += ' --mask_out %s' % self._getTmpPath('refined_mask.vol')
        params += ' -o %s' % self._getFileName('OUTPUT_RESOLUTION_FILE')
        params += ' --volumeRadius %f' % radius
        params += ' --exact'
        params += ' --chimera_volume %s' % self._getFileName(
            'OUTPUT_RESOLUTION_FILE_CHIMERA')
        params += ' --sym %s' % 'c1'
        params += ' --significance %f' % significance
        params += ' --md_outputdata %s' % self._getTmpPath('mask_data.xmd')
        params += ' --threads %i' % self.numberOfThreads.get()

        self.runJob('xmipp_resolution_monogenic_signal', params)
    def MonoResStep(self, iter):
        sampling = self.inputVolume.get().getSamplingRate()
        
        if (iter == 1):
            resFile = self.resolutionVolume.get().getFileName()
        else:
            resFile = self._getFileName('OUTPUT_RESOLUTION_FILE')
            
        pathres=dirname(resFile)
                       
        img = ImageHandler().read(resFile)
        imgData = img.getData()
        max_res = np.amax(imgData)
        
        significance = 0.95

        mtd = md.MetaData()
        if exists(os.path.join(pathres,'mask_data.xmd')):    
            mtd.read(os.path.join(pathres,'mask_data.xmd')) 
            radius = mtd.getValue(MDL_SCALE,1)
        else:
            xdim, _ydim, _zdim = self.inputVolume.get().getDim()
            radius = xdim*0.5 
        
        params = ' --vol %s' % self._getExtraPath('sharpenedMap_'+str(iter)+'.mrc')
        params += ' --mask %s' % self._getFileName('BINARY_MASK')
        params += ' --sampling_rate %f' % sampling
        params += ' --minRes %f' % (2*sampling)
        params += ' --maxRes %f' % max_res           
        params += ' --step %f' % 0.25
        params += ' --mask_out %s' % self._getTmpPath('refined_mask.vol')
        params += ' -o %s' % self._getFileName('OUTPUT_RESOLUTION_FILE')
        params += ' --volumeRadius %f' % radius
        params += ' --exact'
        params += ' --chimera_volume %s' % self._getFileName(
                                                'OUTPUT_RESOLUTION_FILE_CHIMERA')
        params += ' --sym %s' % 'c1'
        params += ' --significance %f' % significance
        params += ' --md_outputdata %s' % self._getTmpPath('mask_data.xmd')
        params += ' --threads %i' % self.numberOfThreads.get()

        self.runJob('xmipp_resolution_monogenic_signal', params)
Example #14
0
    def ifNomask(self, fnVol):
        xdim, _ydim, _zdim = self.inputVolume.get().getDim()
        params = ' -i %s' % fnVol
        params += ' -o %s' % self._getFileName(FN_GAUSSIAN_MAP)
        setsize = 0.02 * xdim
        params += ' --fourier real_gaussian %f' % (setsize)

        self.runJob('xmipp_transform_filter', params)
        img = ImageHandler().read(self._getFileName(FN_GAUSSIAN_MAP))
        imgData = img.getData()
        max_val = np.amax(imgData) * 0.05

        params = ' -i %s' % self._getFileName(FN_GAUSSIAN_MAP)
        params += ' --select below %f' % max_val
        params += ' --substitute binarize'
        params += ' -o %s' % self._getFileName(BINARY_MASK)

        self.runJob('xmipp_transform_threshold', params)

        self.maskFn = self._getFileName(BINARY_MASK)
Example #15
0
    def _showColorSlices(self, fileName, setrangelimits, titleFigure, lowlim,
                         highlim):
        imageFile = self.protocol._getExtraPath(fileName)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()
        imgData2 = np.ma.masked_where(imgData < 0.001, imgData, copy=True)
        if setrangelimits is True:
            fig, im = self._plotVolumeSlices(titleFigure,
                                             imgData2,
                                             lowlim,
                                             highlim,
                                             self.getColorMap(),
                                             dataAxis=self._getAxis())
        else:
            md = MetaData()
            md.read(self.protocol._getExtraPath(OUTPUT_THRESHOLDS_FILE))
            idx = 1
            val1 = md.getValue(MDL_RESOLUTION_FREQ, idx)
            val2 = md.getValue(MDL_RESOLUTION_FREQ2, idx)

            if val1 >= val2:
                max_Res = val1 + 0.01
            else:
                max_Res = val2

#             max_Res = np.nanmax(imgData2)
            min_Res = np.nanmin(imgData2)
            fig, im = self._plotVolumeSlices(titleFigure,
                                             imgData2,
                                             min_Res,
                                             max_Res,
                                             self.getColorMap(),
                                             dataAxis=self._getAxis())
        cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
        cbar = fig.colorbar(im, cax=cax)
        cbar.ax.invert_yaxis()

        return plt.show(fig)
 def ifNomask(self, fnVol):
     if self.halfVolumes:
         xdim, _ydim, _zdim = self.inputVolume.get().getDim()
         params = ' -i %s' % fnVol
     else:
         xdim, _ydim, _zdim = self.inputVolumes.get().getDim()
         params = ' -i %s' % fnVol
     params += ' -o %s' % self._getFileName(FN_GAUSSIAN_MAP)
     setsize = 0.02*xdim
     params += ' --fourier real_gaussian %f' % (setsize)
  
     self.runJob('xmipp_transform_filter', params)
     img = ImageHandler().read(self._getFileName(FN_GAUSSIAN_MAP))
     imgData = img.getData()
     max_val = np.amax(imgData)*0.05
      
     params = ' -i %s' % self._getFileName(FN_GAUSSIAN_MAP)
     params += ' --select below %f' % max_val
     params += ' --substitute binarize'
     params += ' -o %s' % self._getFileName(BINARY_MASK)
  
     self.runJob('xmipp_transform_threshold', params)
     
     self.maskFn = self._getFileName(BINARY_MASK)
 def getMinMax(self, imageFile):
     img = ImageHandler().read(imageFile)
     imgData = img.getData()
     min_res = round(np.amin(imgData) * 100) / 100
     max_res = round(np.amax(imgData) * 100) / 100
     return min_res, max_res