Пример #1
0
 def __init__(self, **kwargs):
     EMObject.__init__(self, **kwargs)
     self.grid = String()
     self.gridSquare = String()
     self.hole = String()
     self.x = Float()
     self.y = Float()
Пример #2
0
 def __init__(self, **kwargs):
     OrderedObject.__init__(self, **kwargs)
     self.name = String('default')
     self.maxCores = Integer()
     self.allowMPI = Boolean()
     self.allowThreads = Boolean()
     self.maxHours = Integer()
    def updateItemMaxCC(self, item, row):
        from convert import locationToXmipp
        # ToDo: uncomment this lines when the output metadata has ITEM_ID
        #         if item.getObjId() != row.getValue(xmipp.MDL_ITEM_ID):
        #             raise Exception("The objId is not equal to ITEM_ID. Please, sort the metadata.")
        if isinstance(item, Class2D):
            img = item.getRepresentative()
            index, fn = img.getLocation()
        else:
            index, fn = item.getLocation()

#         objLoc = locationToXmipp(index, fn)
#         mdLoc = row.getValue(md.MDL_IMAGE)
#         if objLoc != mdLoc:
#             raise Exception("The image isn't the same. Please, sort the metadata.")
        if item.getObjId() != row.getValue(md.MDL_ITEM_ID):
            raise Exception(
                "The image isn't the same. Please, sort the metadata.")

        item._xmipp_imageRef = String(row.getValue(md.MDL_IMAGE_REF))
        item._xmipp_image = String(row.getValue(md.MDL_IMAGE))
        item._xmipp_imageResidual = String(row.getValue(md.MDL_IMAGE_RESIDUAL))
        item._xmipp_maxCC = Float(row.getValue(md.MDL_MAXCC))
        item._xmipp_cost = Float(row.getValue(md.MDL_COST))
        if isinstance(item, Class2D):
            particle = item.getRepresentative()
        else:
            particle = item
Пример #4
0
class PKSubstanceLungParameters(EMObject):
    def __init__(self, **args):
        EMObject.__init__(self, **args)
        self.fnSubst = String()
        self.multiplier = [1] * 8

    def write(self, fnOut):
        fh=open(fnOut,"w")
        fh.write("%s # name\n"%self.name)
        fh.write("%.60g # maximum dissolution rate in alveolar space in units [nmol/(cm*min)]\n"%self.kdiss_alv)
        fh.write("%.60g # maximum dissolution rate in conducting airways in units [nmol/(cm*min)]\n"%self.kdiss_br)
        fh.write("%.60g # steady-state permeability in alveolar space in [cm/min]\n"%self.kp_alv)
        fh.write("%.60g # steady-state permeability in conducting airways in [cm/min]\n"%self.kp_br)
        fh.write("%.60g # solubility in alveolar space in [nmol/cm3]=[uM]\n" % self.Cs_alv)
        fh.write("%.60g # solubility in conducting airways in [nmol/cm3]=[uM]\n" % self.Cs_br)
        fh.write("%.60g # density in [nmol/cm3] = [uM]\n" % self.rho)
        fh.write("%.60g # molecular weight [g/mol]\n"%self.MW)
        fh.write("%.60g # plasma to lung partition coefficient in alveolar space [unitless]\n" % self.Kpl_alv)
        fh.write("%.60g # plasma to lung partition coefficient in conducting airways [unitless]\n" % self.Kpl_br)
        fh.write("%.60g # fraction unbound in plasma [unitless]\n" % self.fu)
        fh.write("%.60g # blood to plasma ratio [unitless]\n" % self.R)

        fh.close()
        self.fnSubst.set(fnOut)

    def read(self, fnIn):
        fh=open(fnIn)
        self.name=fh.readline().split()[0]
        self.kdiss_alv=float(fh.readline().split()[0])
        self.kdiss_br=float(fh.readline().split()[0])
        self.kp_alv=float(fh.readline().split()[0])
        self.kp_br=float(fh.readline().split()[0])
        self.Cs_alv=float(fh.readline().split()[0])
        self.Cs_br=float(fh.readline().split()[0])
        self.rho=float(fh.readline().split()[0])
        self.MW=float(fh.readline().split()[0])
        self.Kpl_alv=float(fh.readline().split()[0])
        self.Kpl_br=float(fh.readline().split()[0])
        self.fu=float(fh.readline().split()[0])
        self.R=float(fh.readline().split()[0])
        fh.close()
        self.fnSubst.set(fnIn)

    def getData(self):
        data = {}
        data['name'] = self.name
        data['kdiss_alv'] = self.kdiss_alv * self.multiplier[3]
        data['kdiss_br'] = self.kdiss_br * self.multiplier[2]
        data['kp_alv'] = self.kp_alv * self.multiplier[5]
        data['kp_br'] = self.kp_br * self.multiplier[4]
        data['Cs_alv'] = self.Cs_alv * self.multiplier[1]
        data['Cs_br'] = self.Cs_br * self.multiplier[0]
        data['rho'] = self.rho
        data['MW'] = self.MW
        data['Kpl_alv'] = self.Kpl_alv * self.multiplier[7]
        data['Kpl_br'] = self.Kpl_br * self.multiplier[6]
        data['fu'] = self.fu
        data['R'] = self.R
        return data
Пример #5
0
 def _setPsdFiles(self, ctfModel, micDir):
     ctfModel._psdFile = String(self._getFileName('psd', micDir=micDir))
     ctfModel._xmipp_enhanced_psd = \
         String(self._getFileName('enhanced_psd', micDir=micDir))
     ctfModel._xmipp_ctfmodel_quadrant = \
         String(self._getFileName('ctfmodel_quadrant', micDir=micDir))
     ctfModel._xmipp_ctfmodel_halfplane = \
         String(self._getFileName('ctfmodel_halfplane', micDir=micDir))
Пример #6
0
class ProtPKPDExponentialFit(ProtPKPDFitBase):
    """ Fit a set of exponentials. The observed measurement is modelled as Y=sum_{i=1}^N c_i exp(-lambda_i * X).\n
Confidence intervals calculated by this fitting may be pessimistic because it assumes that all model parameters
are independent, which are not. Use Bootstrap estimates instead.\n
        Protocol created by http://www.kinestatpharma.com\n"""
    _label = 'fit exponentials'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineParams(self, form, fullForm=True):
        self._defineParams1(form,"t","Cp")
        if fullForm:
            form.addParam('fitType', params.EnumParam, choices=["Linear","Logarithmic","Relative"], label="Fit mode", default=1,
                          help='Linear: sum (Cobserved-Cpredicted)^2\nLogarithmic: sum(log10(Cobserved)-log10(Cpredicted))^2\n'\
                               "Relative: sum ((Cobserved-Cpredicted)/Cobserved)^2")
            form.addParam('Nexp', params.IntParam, label="Number of exponentials", default=1,
                          help='Number of exponentials to fit')
        else:
            self.fitType=Integer()
            self.fitType.set(1)
            self.Nexp=Integer()
            self.Nexp.set(1)
        form.addParam('bounds', params.StringParam, label="Amplitude and time constant bounds", default="", expertLevel=LEVEL_ADVANCED,
                      help='Bounds for the c_i amplitudes and lambdas.\nExample 1: (0,10);(0,1e-2) -> c1 in (0,10), lambda1 in (0,1e-2)\n'\
                           'Example 2: (0,10);(0,1e-2);(0,1);(0,1e-1) -> c1 in (0,10), lambda1 in (0,1e-2), c2 in (0,1), lambda2 in (0,1e-1)')
        form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval=", default=95, expertLevel=LEVEL_ADVANCED,
                      help='Confidence interval for the fitted parameters')
        if fullForm:
            form.addParam('reportX', params.StringParam, label="Evaluate at X=", default="", expertLevel=LEVEL_ADVANCED,
                          help='Evaluate the model at these X values\nExample 1: [0,5,10,20,40,100]\nExample 2: 0:0.55:10, from 0 to 10 in steps of 0.5')
        else:
            self.reportX=String()
            self.reportX.set("")

    def getListOfFormDependencies(self):
        return [self.predictor.get(), self.predicted.get(), self.fitType.get(), self.bounds.get()]

    def createModel(self):
        return PKPDExponentialModel()

    def setupFromFormParameters(self):
        self.model.Nexp=self.Nexp.get()

    #--------------------------- INFO functions --------------------------------------------
    def _warnings(self):
        warnings = []
        experiment = self.readExperiment(self.getInputExperiment().fnPKPD,show=False)
        incorrectList = experiment.getNonBolusDoses()
        if len(incorrectList)>0:
            warnings.append("This protocol is meant only for intravenous bolus regimens. Check the doses for %s"%(','.join(incorrectList)))
        return warnings

    def _validate(self):
        errors=ProtPKPDFitBase._validate(self)
        if self.Nexp.get()<1:
            errors.append("The number of exponentials has to be larger than 0")
        return errors
    def sortImages(self, inputId):
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        args = "-i Particles@%s --addToInput " % imagesMd
        
        if self.autoParRejection == self.REJ_MAXZSCORE:
            args += "--zcut " + str(self.maxZscore.get())
        
        elif self.autoParRejection == self.REJ_PERCENTAGE:
            args += "--percent " + str(self.percentage.get())

        self.runJob("xmipp_image_sort_by_statistics", args)
        
        self.outputMd = String(imagesMd)
Пример #8
0
    def _loadInputCoords(self, micDict):
        """ Load coordinates from the input streaming.
        """
        coordsFn = self.getCoords().getFileName()
        self.debug("Loading input db: %s" % coordsFn)
        coordSet = SetOfCoordinates(filename=coordsFn)
        # FIXME: Temporary to avoid loadAllPropertiesFail
        coordSet._xmippMd = String()
        coordSet.loadAllProperties()

        for micKey, mic in micDict.iteritems():
            micId = mic.getObjId()
            coordList = []
            self.debug("Loading coords for mic: %s (%s)" % (micId, micKey))
            for coord in coordSet.iterItems(where='_micId=%s' % micId):
                # TODO: Check performance penalty of using this clone
                coordList.append(coord.clone())
            self.debug("   Coords found: %s" % len(coordList))

            if coordList:
                self.coordDict[micId] = coordList
            else:
                del micDict[micKey]
        self.coordsClosed = coordSet.isStreamClosed()
        coordSet.close()
        self.debug("Coords are closed? %s" % self.coordsClosed)
        self.debug("Closed db.")

        return micDict
Пример #9
0
    def _checkNewInput(self):
        newMics = []
        for idx, coordSet in enumerate(self.inputCoordinates):
            coorSet = SetOfCoordinates(filename=coordSet.get().getFileName())
            coorSet._xmippMd = String()
            coorSet.loadAllProperties()
            self.streamClosed = coorSet.isStreamClosed()
            coorSet.close()
            if self.check[idx] == "":
                newMics.append(
                    [c.clone() for c in coordSet.get().getMicrographs()])
            else:
                newMics.append([
                    c.clone()
                    for c in coordSet.get().getMicrographs().iterItems(
                        orderBy='creation',
                        where='creation>"' + str(self.check[idx]) + '"')
                ])
            if len(newMics[idx]) < 1:
                continue
            else:
                for p in coordSet.get().getMicrographs().iterItems(
                        orderBy='creation', direction='DESC'):
                    self.check[idx] = p.getObjCreation()
                    break

        newMics[:] = [item for item in newMics if len(item) > 0]
        if len(newMics) > 0:
            fDeps = self.insertNewCoorsSteps(min(newMics, key=len))
            self.inputMics = self.inputMics + len(min(newMics, key=len))
            outputStep = self._getFirstJoinStep()
            if outputStep is not None:
                outputStep.addPrerequisites(*fDeps)
            self.updateSteps()
Пример #10
0
def readSetOfCoordinates(outputDir, partSet, coordSet):
    """This method is used to store an edition of the coordinates obtained by
    localized reconstruction.
    Read from Xmipp .pos files.
    Params:
        outputDir: the directory where the .pos files are.
            It is also expected a file named: config.xmd
            in this directory where the box size can be read.
        micSet: the SetOfMicrographs to associate the .pos, which
            name should be the same of the micrographs.
        coordSet: the SetOfCoordinates that will be populated.
    """
    # Read the boxSize from the config.xmd metadata
    configfile = join(outputDir, 'config.xmd')
    if exists(configfile):
        mdFn = md.MetaData('properties@' + join(outputDir, 'config.xmd'))
        boxSize = mdFn.getValue(md.MDL_PICKING_PARTICLE_SIZE,
                                mdFn.firstObject())
        coordSet.setBoxSize(boxSize)
    for part in partSet:
        baseFn = "%06d_at_%s" % (part.getIndex(),
                                 replaceBaseExt(part.getFileName(), 'pos'))
        posFile = join(outputDir, baseFn)
        readCoordinates(part, posFile, coordSet, outputDir)

    coordSet._xmippMd = String(outputDir)
    def createOutputStep(self):
        outputVols = self._createSetOfVolumes()
        imgSet = self.inputParticles.get()
        for i, vol in enumerate(self._iterInputVols()):
            volume = vol.clone()
            volDir = self._getVolDir(i + 1)
            volPrefix = 'vol%03d_' % (i + 1)
            validationMd = self._getExtraPath(volPrefix + 'validation.xmd')
            moveFile(join(volDir, 'validation.xmd'), validationMd)
            clusterMd = self._getExtraPath(volPrefix +
                                           'clusteringTendency.xmd')
            moveFile(join(volDir, 'clusteringTendency.xmd'), clusterMd)

            outImgSet = self._createSetOfParticles(volPrefix)

            outImgSet.copyInfo(imgSet)

            outImgSet.copyItems(imgSet,
                                updateItemCallback=self._setWeight,
                                itemDataIterator=md.iterRows(
                                    clusterMd, sortByLabel=md.MDL_ITEM_ID))

            mdValidatoin = md.MetaData(validationMd)
            weight = mdValidatoin.getValue(md.MDL_WEIGHT,
                                           mdValidatoin.firstObject())
            volume.weight = Float(weight)
            volume.clusterMd = String(clusterMd)
            volume.cleanObjId(
            )  # clean objects id to assign new ones inside the set
            outputVols.append(volume)
            self._defineOutputs(outputParticles=outImgSet)

        outputVols.setSamplingRate(volume.getSamplingRate())
        self._defineOutputs(outputVolumes=outputVols)
Пример #12
0
    def createChimeraScript(self, volume, pdb):
        """ Create a chimera script to visualize a pseudoatoms pdb
        obteined from a given EM 3d volume.
        A property will be set in the pdb object to 
        store the location of the script.
        """
        pseudoatoms = pdb.getFileName()
        scriptFile = pseudoatoms + '_chimera.cmd'
        pdb._chimeraScript = String(scriptFile)
        sampling = volume.getSamplingRate()
        radius = sampling * self.pseudoAtomRadius.get()
        fnIn = volume.getFileName()
        localInputFn = self._getBasePath(fnIn)
        createLink(fnIn, localInputFn)
        fhCmd = open(scriptFile, 'w')
        fhCmd.write("open %s\n" % basename(pseudoatoms))
        fhCmd.write("rangecol bfactor,a 0 white 1 red\n")
        fhCmd.write("setattr a radius %f\n" % radius)
        fhCmd.write("represent sphere\n")
        fhCmd.write("open %s\n" % basename(localInputFn))

        threshold = 0.01
        if self.maskMode == NMA_MASK_THRE:
            self.maskThreshold.get()
        xdim = volume.getDim()[0]
        origin = xdim / 2
        fhCmd.write(
            "volume #1 level %f transparency 0.5 voxelSize %f originIndex %d\n"
            % (threshold, sampling, origin))
        fhCmd.close()
Пример #13
0
 def breakSymmetryStep(self, imgsFn):
     outImagesMd = self._getPath('images.xmd')
     args = "-i Particles@%s --sym %s -o %s" % (imgsFn,
                                              self.symmetryGroup.get(),
                                              outImagesMd )
     self.runJob("xmipp_angular_break_symmetry", args)
     self.outputMd = String(outImagesMd)
Пример #14
0
 def _methods(self):
     methods = []
     if not hasattr(self, 'outputVolumes'):
         methods.append("Output volumes not ready yet.")
     else:
         inputIsString = ''
         if isinstance(self.inputParticles.get(), SetOfParticles):
             methods.append(
                 'Set of %d particles %s was employed to create an initial volume using RCT method.'
                 % (len(self.inputParticles.get()),
                    self.getObjectTag('inputParticles')))
         else:
             particlesArray = [len(s) for s in self.inputParticles.get()]
             particlesArrayString = String(particlesArray)
             methods.append(
                 'Set of %d classes %s was employed to create %d initial volumes using RCT method. '
                 % (len(self.inputParticles.get()),
                    self.getObjectTag('inputParticles'),
                    len(self.inputParticles.get())))
             methods.append(
                 'For each initial volume were used respectively %s particles'
                 % particlesArrayString)
         methods.append("Output volumes: %s" %
                        self.getObjectTag('outputVolumes'))
         if self.doFilter.get():
             methods.append("Output filtered volumes: %s" %
                            self.getObjectTag('outputFilteredVolumes'))
     return methods
Пример #15
0
    def _updateItem(self, particle, row):
        self.reader.setParticleTransform(particle, row)
        # FIXME: check if other attrs need saving
        particle._rlnImageOriginalName = String(row.rlnImageOriginalName)
        particle._rlnRandomSubset = Integer(row.rlnRandomSubset)

        newLoc = convert.relionToLocation(row.rlnImageName)
        particle.setLocation(newLoc)
Пример #16
0
def setPsdFiles(ctfModel, ctfRow):
    """ Set the PSD files of CTF estimation related
    to this ctfModel. The values will be read from
    the ctfRow if present.
    """
    for attr, label in CTF_PSD_DICT.iteritems():
        if ctfRow.containsLabel(label):
            setattr(ctfModel, attr, String(ctfRow.getValue(label)))
Пример #17
0
 def _updateParticle(self, item, row):
     item.setClassId(row.getValue(md.MDL_REF))
     item.setTransform(rowToAlignment(row, ALIGN_2D))
     if self.flag_relion:
         item._rlnLogLikeliContribution = Float(None)
         item._rlnMaxValueProbDistribution = Float(None)
         item._rlnGroupName = String(None)
         item._rlnNormCorrection = Float(None)
Пример #18
0
class PcaFile(EMObject):
    """ This is a container of files produced by CA PCA Spider protocol.
    It is possible to use the cas_IMC or cas_SEQ files.
    """
    def __init__(self, **args):
        EMObject.__init__(self, **args)

        self.filename = String()

    def getFileName(self):
        return self.filename.get()
    def denoiseImages(self, inputId, inputClassesId):
        # We start preparing writing those elements we're using as input to keep them untouched
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        classesMd = self._getPath('classes.xmd')
        writeSetOfClasses2D(self.inputClasses.get(), classesMd)

        fnRoot = self._getExtraPath('pca')
        fnRootDenoised = self._getExtraPath('imagesDenoised')

        args = '-i Particles@%s --oroot %s --eigenvectors %d --maxImages %d' % (imagesMd, fnRoot, self.maxPCABases.get(), self.maxClasses.get())
        self.runJob("xmipp_image_rotational_pca", args)

        N=min(self.maxPCABases.get(), self.PCABases2Project.get())
        args='-i %s -o %s.stk --save_metadata_stack %s.xmd --basis %s.stk %d'\
             % (imagesMd, fnRootDenoised, fnRootDenoised, fnRoot, N)

        self.runJob("xmipp_transform_filter", args)

        self.outputMd = String('%s.stk' % fnRootDenoised)
Пример #20
0
class PcaFile(EMObject):
    """ This is a container of files produced by CA PCA Spider protocol.
    It is possible to use the cas_IMC or cas_SEQ files.
    """
    def __init__(self, **args):
        EMObject.__init__(self, **args)
        
        self.filename = String()
        
    def getFileName(self):
        return self.filename.get()
def getReadyMics(coordSet):
    coorSet = SetOfCoordinates(filename=coordSet.getFileName())
    coorSet._xmippMd = String()
    coorSet.loadAllProperties()
    setClosed = coorSet.isStreamClosed()
    coorSet.close()
    currentPickMics = {
        micAgg["_micId"]
        for micAgg in coordSet.aggregate(["MAX"], "_micId", ["_micId"])
    }
    return currentPickMics, setClosed
 def _defineProcessParams(self, form):
     # First we customize the inputParticles param to fit our needs in this protocol
     form.getParam('inputParticles').pointerCondition = String('hasAlignment')
     form.getParam('inputParticles').help = String('Input images you want to filter. It is important that the images have alignment information with '
                                                   'respect to the chosen set of classes. This is the standard situation '
                                                   'after CL2D or ML2D.')
     form.addParam('inputClasses', PointerParam, label='Input Classes', important=True,
                   pointerClass='SetOfClasses', 
                   help='Select the input classes for the basis construction against images will be projected to.')
     
     form.addSection(label='Basis construction')
     form.addParam('maxClasses', IntParam, default=128,
                   label='Max. number of classes', expertLevel=LEVEL_ADVANCED,
                   help='Maximum number of classes.')
     form.addParam('maxPCABases', IntParam, default=200,
                   label='Number of PCA bases', expertLevel=LEVEL_ADVANCED,
                   help='Number of PCA bases.')
     form.addSection(label='Denoising')
     form.addParam('PCABases2Project', IntParam, default=200,
                   label='Number of PCA bases on which to project', expertLevel=LEVEL_ADVANCED,
                   help='Number of PCA bases on which to project.')
Пример #23
0
    def _loadInputCoords(self, micDict):
        """ Load coordinates from the input streaming.
        """
        # TODO: this takes for ever if you are NOT
        # doing streaming and have several thousands of mics
        # so I add a counter to keep the user entertained
        import sys
        a = datetime.now()
        counter = 1

        coordsFn = self.getCoords().getFileName()
        self.debug("Loading input db: %s" % coordsFn)
        coordSet = SetOfCoordinates(filename=coordsFn)
        # FIXME: Temporary to avoid loadAllPropertiesFail
        coordSet._xmippMd = String()
        coordSet.loadAllProperties()

        # TODO: horrible code. Rewrite using
        # for coord in coordSet.iterItems(orderBy='_micId',
        #                                 direction='ASC'):
        #     micId = coord.getMicId()
        #     if micId != lastMicId:
        #         lastMicId = micId
        #         ...
        #     ...

        for micKey, mic in micDict.iteritems():
            if counter % 50 == 0:
                b = datetime.now()
                print(b - a, 'reading coordinates for mic number',
                      "%06d" % counter)
                sys.stdout.flush()  # force buffer to print
            counter += 1

            micId = mic.getObjId()
            coordList = []
            self.debug("Loading coords for mic: %s (%s)" % (micId, micKey))
            for coord in coordSet.iterItems(where='_micId=%s' % micId):
                # TODO: Check performance penalty of using this clone
                coordList.append(coord.clone())
            self.debug("Coords found: %s" % len(coordList))

            if coordList:
                self.coordDict[micId] = coordList
            else:
                del micDict[micKey]
        self.coordsClosed = coordSet.isStreamClosed()
        coordSet.close()
        self.debug("Coords are closed? %s" % self.coordsClosed)
        self.debug("Closed db.")

        return micDict
Пример #24
0
def writeSetOfParticles(imgSet, starFile, stackFile):
    """ This function will write a SetOfImages as Bsoft metadata.
    Params:
        imgSet: the SetOfImages instance.
        starFile: the filename where to write the metadata.
    """
    mdata = md.MetaData()
    mdata.setColumnFormat(False)
    imgRow = mdata.Row()
    imgRow.setValue("micrograph.id", int(1))
    imgRow.setValue("particle.x_origin", str(stackFile))
    imgRow.writeToMd(mdata, mdata.addObject())
    imgSet._bsoftStar = String(starFile)
Пример #25
0
class QueueConfig(OrderedObject):
    def __init__(self, **kwargs):
        OrderedObject.__init__(self, **kwargs)
        self.name = String('default')
        self.maxCores = Integer()
        self.allowMPI = Boolean()
        self.allowThreads = Boolean()
        self.maxHours = Integer()

    def getName(self):
        return self.name.get()

    def getMaxCores(self):
        return self.maxCores.get()

    def getAllowMPI(self):
        return self.allowMPI.get()

    def getAllowThreads(self):
        return self.allowThreads.get()

    def getMaxHours(self):
        return self.maxHours.get()

    def setName(self, name):
        self.name.set(name)

    def setMaxCores(self, maxCores):
        self.maxCores.set(maxCores)

    def setAllowMPI(self, allowMPI):
        self.allowMPI.set(allowMPI)

    def setAllowThreads(self, allowThreads):
        self.allowThreads.set(allowThreads)

    def setMaxHours(self, maxHours):
        self.maxHours.set(maxHours)
Пример #26
0
    def sortImages(self, inputId):
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        args = "-i Particles@%s --addToInput " % imagesMd
        
        if self.autoParRejection == self.REJ_MAXZSCORE:
            args += "--zcut " + str(self.maxZscore.get())
        
        elif self.autoParRejection == self.REJ_PERCENTAGE:
            args += "--percent " + str(self.percentage.get())

        self.runJob("xmipp_image_sort_by_statistics", args)
        
        self.outputMd = String(imagesMd)
Пример #27
0
    def createOutput(self):
        fnStruct = glob.glob(self._getExtraPath("atomStructIn*"))[0]

        for fnDir in glob.glob(self._getPath('glide-*')):
            fnBase = os.path.split(fnDir)[1]
            fnGrid = os.path.join(fnDir, '%s.zip' % fnBase)
            if os.path.exists(fnGrid):
                gridFile = SchrodingerGrid(filename=fnGrid)
                gridFile.structureFile = String(fnStruct)

                n = fnDir.split('glide-')[1]
                outputDict = {'outputGrid%s' % n: gridFile}
                self._defineOutputs(**outputDict)
                self._defineSourceRelation(self.inputStructure, gridFile)
Пример #28
0
def saveConfig(filename):
    from pyworkflow.mapper import SqliteMapper
    from pyworkflow.object import String, Integer

    mapper = SqliteMapper(filename)
    o = Config()
    for k, v in globals().iteritems():
        if k.startswith('cfg'):
            if type(v) is str:
                value = String(v)
            else:
                value = Integer(v)
            setattr(o, k, value)
    mapper.insert(o)
    mapper.commit()
Пример #29
0
 def _defineParams(self, form, fullForm=True):
     self._defineParams1(form,"t","Cp")
     if fullForm:
         form.addParam('fitType', params.EnumParam, choices=["Linear","Logarithmic","Relative"], label="Fit mode", default=1,
                       help='Linear: sum (Cobserved-Cpredicted)^2\nLogarithmic: sum(log10(Cobserved)-log10(Cpredicted))^2\n'\
                            "Relative: sum ((Cobserved-Cpredicted)/Cobserved)^2")
         form.addParam('Nexp', params.IntParam, label="Number of exponentials", default=1,
                       help='Number of exponentials to fit')
     else:
         self.fitType=Integer()
         self.fitType.set(1)
         self.Nexp=Integer()
         self.Nexp.set(1)
     form.addParam('bounds', params.StringParam, label="Amplitude and time constant bounds", default="", expertLevel=LEVEL_ADVANCED,
                   help='Bounds for the c_i amplitudes and lambdas.\nExample 1: (0,10);(0,1e-2) -> c1 in (0,10), lambda1 in (0,1e-2)\n'\
                        'Example 2: (0,10);(0,1e-2);(0,1);(0,1e-1) -> c1 in (0,10), lambda1 in (0,1e-2), c2 in (0,1), lambda2 in (0,1e-1)')
     form.addParam('confidenceInterval', params.FloatParam, label="Confidence interval=", default=95, expertLevel=LEVEL_ADVANCED,
                   help='Confidence interval for the fitted parameters')
     if fullForm:
         form.addParam('reportX', params.StringParam, label="Evaluate at X=", default="", expertLevel=LEVEL_ADVANCED,
                       help='Evaluate the model at these X values\nExample 1: [0,5,10,20,40,100]\nExample 2: 0:0.55:10, from 0 to 10 in steps of 0.5')
     else:
         self.reportX=String()
         self.reportX.set("")
    def createChimeraScript(self, volume, pdb):
        """ Create a chimera script to visualize a pseudoatoms pdb
        obteined from a given EM 3d volume.
        A property will be set in the pdb object to
        store the location of the script.
        """
        pseudoatoms = pdb.getFileName()
        scriptFile = pseudoatoms + '_chimera.cmd'
        pdb._chimeraScript = String(scriptFile)
        sampling = volume.getSamplingRate()
        radius = sampling * self.pseudoAtomRadius.get()
        fnIn = getImageLocation(volume)
        if fnIn.endswith(":mrc"):
            fnIn = fnIn[:-4]

        x, y, z = volume.getOrigin(force=True).getShifts()
        xx, yy, zz = volume.getDim()

        dim = volume.getDim()[0]
        bildFileName = os.path.abspath(self._getExtraPath("axis.bild"))
        Chimera.createCoordinateAxisFile(dim,
                                         bildFileName=bildFileName,
                                         sampling=sampling)
        fhCmd = open(scriptFile, 'w')
        fhCmd.write("open %s\n" % basename(pseudoatoms))
        fhCmd.write("rangecol bfactor,a 0 white 1 red\n")
        fhCmd.write("setattr a radius %f\n" % radius)
        fhCmd.write("represent sphere\n")

        fhCmd.write("open %s\n" % abspath(fnIn))
        threshold = 0.01
        if self.maskMode == NMA_MASK_THRE:
            self.maskThreshold.get()
        # set sampling
        fhCmd.write("volume #1 level %f transparency 0.5 voxelSize %f origin "
                    "%0.2f,%0.2f,%0.2f\n" % (threshold, sampling, x, y, z))
        fhCmd.write("open %s\n" % bildFileName)
        #fhCmd.write("move %0.2f,%0.2f,%0.2f model #0 coord #2\n"
        #            % ((xx / 2. * sampling) - xv,
        #               (yy / 2. * sampling) - yv,
        #               (zz / 2. * sampling) - zv))
        fhCmd.write("move %0.2f,%0.2f,%0.2f model #0 coord #2\n" %
                    (x + (xx / 2. * sampling), y + (yy / 2. * sampling), z +
                     (zz / 2. * sampling)))
        fhCmd.close()
Пример #31
0
    def show(self, form, *params):
        protocol = form.protocol
        try:
            models = self.getModelsChainsStep(protocol)
        except Exception as e:
            print "ERROR: ", e.message
            return

        self.editionListOfChains(models)
        finalChainList = []
        for i in self.chainList:
            finalChainList.append(String(i))
        provider = ListTreeProviderString(finalChainList)
        dlg = dialog.ListDialog(
            form.root, "Model chains", provider,
            "Select one of the chains (model, chain, "
            "number of chain residues)")
        form.setVar('inputStructureChain', dlg.values[0].get())
    def createOutputSingle(self, fnSite, fnStructureFile, score, dscore,
                           srcObj):
        n = fnSite.split('@')[0]

        fnDir = self._getPath("grid-%s" % n)
        if os.path.exists(fnDir):
            fnBase = os.path.split(fnDir)[1]
            fnGrid = os.path.join(fnDir, '%s.zip' % fnBase)
            if os.path.exists(fnGrid):
                gridFile = SchrodingerGrid(filename=fnGrid)
                gridFile.structureFile = String(fnStructureFile)
                gridFile.bindingSiteScore = Float(score)
                gridFile.bindingSiteDScore = Float(dscore)

                n = fnDir.split('grid-')[1]
                outputDict = {'outputGrid%s' % n: gridFile}
                self._defineOutputs(**outputDict)
                self._defineSourceRelation(srcObj, gridFile)
Пример #33
0
    def _updateParticle(self, item, row):
        item.setClassId(row.rlnClassNumber)
        self._reader.setParticleTransform(item, row)

        # Try to create extra objects only once if item is reused
        if not hasattr(item, '_rlnNormCorrection'):
            item._rlnNormCorrection = Float()
            item._rlnLogLikeliContribution = Float()
            item._rlnMaxValueProbDistribution = Float()

        item._rlnNormCorrection.set(row.rlnNormCorrection)
        item._rlnLogLikeliContribution.set(row.rlnLogLikeliContribution)
        item._rlnMaxValueProbDistribution.set(row.rlnMaxValueProbDistribution)

        if hasattr(item, '_rlnGroupName'):
            item._rlnGroupName.set(row.rlnGroupName)
        elif hasattr(row, 'rlnGroupName'):
            item._rlnGroupName = String(row.rlnGroupName)
    def denoiseImages(self, inputId, inputClassesId):
        # We start preparing writing those elements we're using as input to keep them untouched
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        classesMd = self._getPath('classes.xmd')
        writeSetOfClasses2D(self.inputClasses.get(), classesMd)

        fnRoot = self._getExtraPath('pca')
        fnRootDenoised = self._getExtraPath('imagesDenoised')

        args = '-i Particles@%s --oroot %s --eigenvectors %d --maxImages %d' % (imagesMd, fnRoot, self.maxPCABases.get(), self.maxClasses.get())
        self.runJob("xmipp_image_rotational_pca", args)

        N=min(self.maxPCABases.get(), self.PCABases2Project.get())
        args='-i %s -o %s.stk --save_metadata_stack %s.xmd --basis %s.stk %d'\
             % (imagesMd, fnRootDenoised, fnRootDenoised, fnRoot, N)

        self.runJob("xmipp_transform_filter", args)

        self.outputMd = String('%s.stk' % fnRootDenoised)
Пример #35
0
 def __init__(self, **args):
     ProtParticlePicking.__init__(self, **args)
     # The following attribute is only for testing
     self.importFolder = String(args.get('importFolder', None))
Пример #36
0
class XmippProtDimredNMA(ProtAnalysis3D):
    """ This protocol will take the images with NMA deformations
    as points in a N-dimensional space (where N is the number
    of computed normal modes) and will project them in a reduced
    spaced (usually with less dimensions).
    """
    _label = 'nma dimred'
    
    def __init__(self, **kwargs):
        ProtAnalysis3D.__init__(self, **kwargs)
        self.mappingFile = String()
    
    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineParams(self, form):
        form.addSection(label='Input')
        form.addParam('inputNMA', PointerParam, pointerClass='XmippProtAlignmentNMA',
                      label="Conformational distribution",                        
                      help='Select a previous run of the NMA alignment.')
        
        form.addParam('dimredMethod', EnumParam, default=DIMRED_PCA,
                      choices=['Principal Component Analysis (PCA)',
                               'Local Tangent Space Alignment',
                               'Diffusion map',
                               'Linear Local Tangent Space Alignment',
                               'Linearity Preserving Projection',
                               'Kernel PCA',
                               'Probabilistic PCA',
                               'Laplacian Eigenmap',
                               'Hessian Locally Linear Embedding',
                               'Stochastic Proximity Embedding',
                               'Neighborhood Preserving Embedding'],
                      label='Dim-Red method',
                      help=""" Dimensionality Reduction method.
    PCA
       Principal Component Analysis 
    LTSA <k=12>
       Local Tangent Space Alignment, k=number of nearest neighbours 
    DM <s=1> <t=1>
       Diffusion map, t=Markov random walk, s=kernel sigma 
    LLTSA <k=12>
       Linear Local Tangent Space Alignment, k=number of nearest neighbours 
    LPP <k=12> <s=1>
       Linearity Preserving Projection, k=number of nearest neighbours, s=kernel sigma 
    kPCA <s=1>
       Kernel PCA, s=kernel sigma 
    pPCA <n=200>
       Probabilistic PCA, n=number of iterations 
    LE <k=7> <s=1>
       Laplacian Eigenmap, k=number of nearest neighbours, s=kernel sigma 
    HLLE <k=12>
       Hessian Locally Linear Embedding, k=number of nearest neighbours 
    SPE <k=12> <global=1>
       Stochastic Proximity Embedding, k=number of nearest neighbours, global embedding or not 
    NPE <k=12>
       Neighborhood Preserving Embedding, k=number of nearest neighbours 
""")
        form.addParam('extraParams', StringParam, level=LEVEL_ADVANCED,
                      label="Extra params", 
                      help='This parameters will be passed to the program.')
                      
        form.addParam('reducedDim', IntParam, default=2,
                      label='Reduced dimension')
        form.addParallelSection(threads=0, mpi=0)    
    
    
    #--------------------------- INSERT steps functions --------------------------------------------

    def _insertAllSteps(self):
        # Take deforamtions text file and the number of images and modes
        inputSet = self.getInputParticles()
        rows = inputSet.getSize()
        reducedDim = self.reducedDim.get()
        method = self.dimredMethod.get()
        extraParams = self.extraParams.get('')
        
        deformationsFile = self.getDeformationFile()
        
        self._insertFunctionStep('convertInputStep', 
                                 deformationsFile, inputSet.getObjId())
        self._insertFunctionStep('performDimredStep', 
                                 deformationsFile, method, extraParams,
                                 rows, reducedDim) 
        self._insertFunctionStep('createOutputStep')
        
        
    #--------------------------- STEPS functions --------------------------------------------   
    
    def convertInputStep(self, deformationFile, inputId):
        """ Iterate throught the images and write the 
        plain deformation.txt file that will serve as 
        input for dimensionality reduction.
        """
        inputSet = self.getInputParticles()
        f = open(deformationFile, 'w')
        
        for particle in inputSet:
            f.write(' '.join(particle._xmipp_nmaDisplacements))
            f.write('\n')
        f.close()
    
    def performDimredStep(self, deformationsFile, method, extraParams,
                          rows, reducedDim):
        outputMatrix = self.getOutputMatrixFile()
        methodName = DIMRED_VALUES[method]
        # Get number of columes in deformation files
        # it can be a subset of inputModes
        f = open(deformationsFile)
        columns = len(f.readline().split()) # count number of values in first line
        f.close()
        
        args = "-i %(deformationsFile)s -o %(outputMatrix)s -m %(methodName)s %(extraParams)s"
        args += "--din %(columns)d --samples %(rows)d --dout %(reducedDim)d"
        if method in DIMRED_MAPPINGS:
            mappingFile = self._getExtraPath('projector.txt')
            args += " --saveMapping %(mappingFile)s"
            self.mappingFile.set(mappingFile)
        self.runJob("xmipp_matrix_dimred", args % locals())
        
    def createOutputStep(self):
        pass

    #--------------------------- INFO functions --------------------------------------------
    def _summary(self):
        summary = []
        return summary
    
    def _validate(self):
        errors = []
        return errors
    
    def _citations(self):
        return []
    
    def _methods(self):
        return []
    
    #--------------------------- UTILS functions --------------------------------------------

    def getInputParticles(self):
        """ Get the output particles of the input NMA protocol. """
        return self.inputNMA.get().outputParticles
    
    def getInputPdb(self):
        return self.inputNMA.get().getInputPdb()
    
    def getOutputMatrixFile(self):
        return self._getExtraPath('output_matrix.txt')
    
    def getDeformationFile(self):
        return self._getExtraPath('deformations.txt')
    
    def getProjectorFile(self):
        return self.mappingFile.get()
    
    def getMethodName(self):
        return DIMRED_VALUES[self.dimredMethod.get()]
Пример #37
0
 def __init__(self, **args):
     EMObject.__init__(self, **args)
     
     self.filename = String()
Пример #38
0
 def __init__(self, **kwargs):
     ProtAnalysis3D.__init__(self, **kwargs)
     self.histogramData = String()
     self.plotData = String() # store some values for later plot
Пример #39
0
class XmippProtAngBreakSymmetry(ProtProcessParticles):
    """ Classify particles according their similarity to the others in order to detect outliers. """
    _label = 'break symmetry'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineProcessParams(self, form):
        
        form.addParam('symmetryGroup', StringParam, default="c1",
                      label='Symmetry group',
                      help="See http://xmipp.cnb.csic.es/twiki/bin/view/Xmipp/Symmetry"
                           " for a description of the symmetry groups format in Xmipp.\n"
                           "If no symmetry is present, use _c1_.")
        
    def _getDefaultParallel(self):
        """This protocol doesn't have mpi version"""
        return (0, 0)
     
    #--------------------------- INSERT steps functions --------------------------------------------            
    def _insertAllSteps(self):
        """ Mainly prepare the command line for call brak symmetry program"""
        # Create a metadata with the geometrical information
        # as expected by Xmipp
        imgsFn = self._getPath('input_particles.xmd')
        self._insertFunctionStep('convertInputStep', imgsFn)
        self._insertFunctionStep('breakSymmetryStep', imgsFn)
        self._insertFunctionStep('createOutputStep')

    #--------------------------- STEPS functions --------------------------------------------

    def convertInputStep(self, outputFn):
        """ Create a metadata with the images and geometrical information. """
        writeSetOfParticles(self.inputParticles.get(), outputFn)

    #--------------------------- STEPS functions --------------------------------------------
    def breakSymmetryStep(self, imgsFn):
        outImagesMd = self._getPath('images.xmd')
        args = "-i Particles@%s --sym %s -o %s" % (imgsFn,
                                                 self.symmetryGroup.get(),
                                                 outImagesMd )
        self.runJob("xmipp_angular_break_symmetry", args)
        self.outputMd = String(outImagesMd)

    def createOutputStep(self):
        imgSet = self._createSetOfParticles()
        imgSet.copyInfo(self.inputParticles.get())
        readSetOfParticles(self.outputMd.get(), imgSet)

        self._defineOutputs(outputParticles=imgSet)

    #--------------------------- INFO functions --------------------------------------------                
    def _summary(self):
        import os
        summary = []
        if not hasattr(self, 'outputParticles'):
            summary.append("Output particles not ready yet.")
        else:
            summary.append("Symmetry: %s"% self.symmetryGroup.get())
        return summary
    
    def _validate(self):
        pass
        
    def _citations(self):
        return []#['Vargas2013b']
    
    def _methods(self):
        methods = []
#        if hasattr(self, 'outputParticles'):
#            outParticles = len(self.outputParticles) if self.outputParticles is not None else None
#            particlesRejected = len(self.inputParticles.get())-outParticles if outParticles is not None else None
#            particlesRejectedText = ' ('+str(particlesRejected)+')' if particlesRejected is not None else ''
#            rejectionText = [
#                             '',# REJ_NONE
#                             ' and removing those not reaching %s%s' % (str(self.maxZscore.get()), particlesRejectedText),# REJ_MAXZSCORE
#                             ' and removing worst %s percent%s' % (str(self.percentage.get()), particlesRejectedText)# REJ_PERCENTAGE
#                             ]
#            methods.append('Input dataset %s of %s particles was sorted by'
#                           ' its ZScore using xmipp_image_sort_by_statistics'
#                           ' program%s. ' % (self.getObjectTag('inputParticles'), len(self.inputParticles.get()), rejectionText[self.autoParRejection.get()]))
#            methods.append('Output set is %s.'%self.getObjectTag('outputParticles'))
        return methods
class XmippProtDenoiseParticles(ProtProcessParticles):
    """ Remove particles noise by filtering them. This filtering process is based on a projection over a basis created from some averages (extracted from classes). This filtering is not intended for processing particles. The huge filtering they will be passed through is known to remove part of the signal 
    with the noise. However this is a good method for clearly see which particle are we going to process before it's done. """
    _label = 'denoise particles'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineProcessParams(self, form):
        # First we customize the inputParticles param to fit our needs in this protocol
        form.getParam('inputParticles').pointerCondition = String('hasAlignment')
        form.getParam('inputParticles').help = String('Input images you want to filter. It is important that the images have alignment information with '
                                                      'respect to the chosen set of classes. This is the standard situation '
                                                      'after CL2D or ML2D.')
        form.addParam('inputClasses', PointerParam, label='Input Classes', important=True,
                      pointerClass='SetOfClasses', 
                      help='Select the input classes for the basis construction against images will be projected to.')
        
        form.addSection(label='Basis construction')
        form.addParam('maxClasses', IntParam, default=128,
                      label='Max. number of classes', expertLevel=LEVEL_ADVANCED,
                      help='Maximum number of classes.')
        form.addParam('maxPCABases', IntParam, default=200,
                      label='Number of PCA bases', expertLevel=LEVEL_ADVANCED,
                      help='Number of PCA bases.')
        form.addSection(label='Denoising')
        form.addParam('PCABases2Project', IntParam, default=200,
                      label='Number of PCA bases on which to project', expertLevel=LEVEL_ADVANCED,
                      help='Number of PCA bases on which to project.')
        
    def _getDefaultParallel(self):
        """ Return the default value for thread and MPI
        for the parallel section definition.
        """
        return (2, 4)
     
    #--------------------------- INSERT steps functions --------------------------------------------            
    def _insertAllSteps(self):
        """ Insert every step of the protocol"""
        
        # Convert input images if necessary
        self._insertFunctionStep('denoiseImages', self.inputParticles.getObjId(), self.inputClasses.getObjId()) 
        
        self._insertFunctionStep('createOutputStep')

    #--------------------------- STEPS functions --------------------------------------------
    def denoiseImages(self, inputId, inputClassesId):
        # We start preparing writing those elements we're using as input to keep them untouched
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        classesMd = self._getPath('classes.xmd')
        writeSetOfClasses2D(self.inputClasses.get(), classesMd)

        fnRoot = self._getExtraPath('pca')
        fnRootDenoised = self._getExtraPath('imagesDenoised')

        args = '-i Particles@%s --oroot %s --eigenvectors %d --maxImages %d' % (imagesMd, fnRoot, self.maxPCABases.get(), self.maxClasses.get())
        self.runJob("xmipp_image_rotational_pca", args)

        N=min(self.maxPCABases.get(), self.PCABases2Project.get())
        args='-i %s -o %s.stk --save_metadata_stack %s.xmd --basis %s.stk %d'\
             % (imagesMd, fnRootDenoised, fnRootDenoised, fnRoot, N)

        self.runJob("xmipp_transform_filter", args)

        self.outputMd = String('%s.stk' % fnRootDenoised)

    def createOutputStep(self):
        imgSet = self._createSetOfParticles()
        imgSet.copyInfo(self.inputParticles.get())
        readSetOfParticles(self.outputMd.get(), imgSet)

        self._defineOutputs(outputParticles=imgSet)

    #--------------------------- INFO functions --------------------------------------------                
    def _summary(self):
        summary = []
        if not hasattr(self, 'outputParticles'):
            summary.append("Output particles not ready yet.")
        else:
            summary.append('PCA basis created by using %d classes' % len(self.inputClasses.get()))
            summary.append('Max. number of classes defined for PCA basis creation: %d' % self.maxClasses.get())
            summary.append('Max. number of PCA bases defined for PCA basis creation: %d' % self.maxPCABases.get())
            summary.append('PCA basis on which to project for denoising: %d' % self.PCABases2Project.get())
        return summary
    
    def _validate(self):
        pass
        
    def _citations(self):
        return ['zhao2013', 'ponce2011']
    
    def _methods(self):
        methods = []
        if not hasattr(self, 'outputParticles'):
            methods.append("Output particles not ready yet.")
        else:
            methods.append('An input dataset of %d particles was filtered creating a PCA basis (%d components) with '
                           'xmipp_image_rotational_pca and projecting the dataset into that base with xmipp_transform_filter.'\
                           % (len(self.inputParticles.get()), len(self.inputClasses.get())))
        return methods
Пример #41
0
class XmippProtAngBreakSymmetry(ProtProcessParticles):
    """
    Given an input set of particles with angular assignment, find an
    equivalent angular assignment for a given symmetry.

    Be aware that input symmetry values follows Xmipp conventions as described in:
    http://xmipp.cnb.csic.es/twiki/bin/view/Xmipp/Symmetry
    """
    _label = 'break symmetry'

    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineProcessParams(self, form):
        
        form.addParam('symmetryGroup', StringParam, default="c1",
                      label='Symmetry group',
                      help="See http://xmipp.cnb.csic.es/twiki/bin/view/Xmipp/Symmetry"
                           " for a description of the symmetry groups format in Xmipp.\n"
                           "If no symmetry is present, use _c1_.")
    
    def _getDefaultParallel(self):
        """This protocol doesn't have mpi version"""
        return (0, 0)
     
    #--------------------------- INSERT steps functions --------------------------------------------            
    def _insertAllSteps(self):
        """ Mainly prepare the command line for call brak symmetry program"""
        # Create a metadata with the geometrical information
        # as expected by Xmipp
        imgsFn = self._getPath('input_particles.xmd')
        self._insertFunctionStep('convertInputStep', imgsFn)
        self._insertFunctionStep('breakSymmetryStep', imgsFn)
        self._insertFunctionStep('createOutputStep')

    #--------------------------- STEPS functions --------------------------------------------

    def convertInputStep(self, outputFn):
        """ Create a metadata with the images and geometrical information. """
        writeSetOfParticles(self.inputParticles.get(), outputFn)

    #--------------------------- STEPS functions --------------------------------------------
    def breakSymmetryStep(self, imgsFn):
        outImagesMd = self._getPath('images.xmd')
        args = "-i Particles@%s --sym %s -o %s" % (imgsFn,
                                                 self.symmetryGroup.get(),
                                                 outImagesMd )
        self.runJob("xmipp_angular_break_symmetry", args)
        self.outputMd = String(outImagesMd)

    def createOutputStep(self):
        imgSet = self.inputParticles.get()
        partSet = self._createSetOfParticles()
        partSet.copyInfo(imgSet)
        partSet.copyItems(imgSet,
                          updateItemCallback=self._createItemMatrix,
                          itemDataIterator=md.iterRows(self.outputMd.get(), sortByLabel=md.MDL_ITEM_ID))
        
        self._defineOutputs(outputParticles=partSet)
        self._defineSourceRelation(imgSet, partSet)

    #--------------------------- INFO functions --------------------------------------------                
    def _summary(self):
        import os
        summary = []
        if not hasattr(self, 'outputParticles'):
            summary.append("Output particles not ready yet.")
        else:
            summary.append("Symmetry: %s"% self.symmetryGroup.get())
        return summary
    
    def _validate(self):
        pass
        
    def _citations(self):
        return []#['Vargas2013b']
    
    def _methods(self):
        methods = []
#        if hasattr(self, 'outputParticles'):
#            outParticles = len(self.outputParticles) if self.outputParticles is not None else None
#            particlesRejected = len(self.inputParticles.get())-outParticles if outParticles is not None else None
#            particlesRejectedText = ' ('+str(particlesRejected)+')' if particlesRejected is not None else ''
#            rejectionText = [
#                             '',# REJ_NONE
#                             ' and removing those not reaching %s%s' % (str(self.maxZscore.get()), particlesRejectedText),# REJ_MAXZSCORE
#                             ' and removing worst %s percent%s' % (str(self.percentage.get()), particlesRejectedText)# REJ_PERCENTAGE
#                             ]
#            methods.append('Input dataset %s of %s particles was sorted by'
#                           ' its ZScore using xmipp_image_sort_by_statistics'
#                           ' program%s. ' % (self.getObjectTag('inputParticles'), len(self.inputParticles.get()), rejectionText[self.autoParRejection.get()]))
#            methods.append('Output set is %s.'%self.getObjectTag('outputParticles'))
        return methods

    #--------------------------- Utils functions --------------------------------------------                
    def _createItemMatrix(self, item, row):
        from pyworkflow.em.packages.xmipp3.convert import createItemMatrix
        import pyworkflow.em as em
        
        createItemMatrix(item, row, align=em.ALIGN_PROJ)
Пример #42
0
 def __init__(self, **kwargs):
     ProtAnalysis3D.__init__(self, **kwargs)
     self.histogramData = String()
Пример #43
0
class XmippProtParticlePickingPairs(ProtParticlePicking, XmippProtocol):
    """ Picks particles in a set of untilted-tilted pairs of micrographs. """
    _label = 'tilt pairs particle picking'

    def __init__(self, **args):
        ProtParticlePicking.__init__(self, **args)
        # The following attribute is only for testing
        self.importFolder = String(args.get('importFolder', None))

    #--------------- DEFINE param functions ---------------------------------
    def _defineParams(self, form):
        form.addSection(label='Input')
        form.addParam('inputMicrographsTiltedPair', params.PointerParam,
                      pointerClass='MicrographsTiltPair',
                      label="Micrographs tilt pair",
                      help='Select the MicrographsTiltPair ')

        #----------- INSERT steps functions ----------------------------------
    def _insertAllSteps(self):
        """ The Particle Picking process is realized for a pair
        of set of micrographs
        """
        self.micsFn = self._getPath('input_micrographs.xmd')
        # Convert input into xmipp Metadata format
        self._insertFunctionStep('convertInputStep')

        # Launch Particle Picking GUI
        if not self.importFolder.hasValue():
            self._insertFunctionStep('launchParticlePickGUIStep', interactive=True)
        else: # This is only used for test purposes
            self._insertFunctionStep('_importFromFolderStep')

    #------------------- STEPS functions -----------------------------------
    def convertInputStep(self):
        micTiltPairs = self.inputMicrographsTiltedPair.get()
        # Get the converted input micrographs in Xmipp format
        convert.writeSetOfMicrographsPairs(micTiltPairs.getUntilted(),
                                           micTiltPairs.getTilted(),
                                           self.micsFn)

    def launchParticlePickGUIStep(self):
        process = launchTiltPairPickerGUI(self.micsFn, self._getExtraPath(),
                                          self)
        process.wait()

    def _importFromFolderStep(self):
        """ This function will copy Xmipp .pos files for
        simulating a particle picking run...this is only
        for testing purposes.
        """
        extraDir = self._getExtraPath()

        for f in pwutils.getFiles(self.importFolder.get()):
            pwutils.copyFile(f, extraDir)

        self.registerCoords(extraDir, readFromExtra=True)

    #--------------------------- INFO functions --------------------------------------------
    def _citations(self):
        return []

    #--------------------------- UTILS functions -------------------------------------------
    def __str__(self):
        """ String representation of a Particle Picking Tilt run """
        outputs = self.getOutputsSize()

        if outputs == 0:
            msg = "No particles picked yet."
        elif outputs == 1:
            picked = self.getCoords().getSize()
            mics = self.inputMicrographsTiltedPair.get().getTilted().getSize()
            msg = "Number of particles picked: %d " % picked
            msg += "(from %d micrographs)" % mics
        else:
            msg = 'Number of outputs: %d' % outputs

        return msg

    def getInputMicrographs(self):
        return self.inputMicrographsTiltedPair.get().getTilted()

    def getCoords(self):
        return self.getCoordsTiltPair()

    def _summary(self):
        summary = []
        if self.getInputMicrographs() is  not None:
            summary.append("Number of input micrographs: %d"
                           % self.getInputMicrographs().getSize())

        if self.getOutputsSize() >= 1:
            for key, output in self.iterOutputAttributes(CoordinatesTiltPair):
                summary.append("*%s:*" % key)
                summary.append("  Particles pairs picked: %d" % output.getSize())
                summary.append("  Particle size: %d \n" % output.getBoxSize())
        else:
            summary.append("Output tilpairs not ready yet.")

        return summary

    def __getOutputSuffix(self):
        maxCounter = -1
        for attrName, _ in self.iterOutputAttributes(CoordinatesTiltPair):
            suffix = attrName.replace('outputCoordinatesTiltPair', '')
            try:
                counter = int(suffix)
            except:
                counter = 1 # when there is not number assume 1
            maxCounter = max(counter, maxCounter)

        return str(maxCounter+1) if maxCounter > 0 else '' # empty if not outputs

    def _getBoxSize(self):
        """ Redefine this function to set a specific box size to the output
        coordinates untilted and tilted.
        """
        return None

    def _readCoordinates(self, coordsDir, suffix=''):
        micTiltPairs = self.inputMicrographsTiltedPair.get()
        uSuffix = 'Untilted' + suffix
        tSuffix = 'Tilted' + suffix
        uSet = micTiltPairs.getUntilted()
        tSet = micTiltPairs.getTilted()
        # Create Untilted and Tilted SetOfCoordinates
        uCoordSet = self._createSetOfCoordinates(uSet, suffix=uSuffix)
        convert.readSetOfCoordinates(coordsDir, uSet, uCoordSet)
        uCoordSet.write()
        tCoordSet = self._createSetOfCoordinates(tSet, suffix=tSuffix)
        convert.readSetOfCoordinates(coordsDir, tSet, tCoordSet)
        tCoordSet.write()
        boxSize = self._getBoxSize()
        if boxSize:
            uCoordSet.setBoxSize(boxSize)
            tCoordSet.setBoxSize(boxSize)

        return uCoordSet, tCoordSet

    def _readAngles(self, micsFn, suffix=''):
        # Read Angles from input micrographs
        anglesSet = self._createSetOfAngles(suffix=suffix)
        convert.readAnglesFromMicrographs(micsFn, anglesSet)
        anglesSet.write()
        return anglesSet

    def registerCoords(self, coordsDir, store=True, readFromExtra=False):
        micTiltPairs = self.inputMicrographsTiltedPair.get()
        suffix = self.__getOutputSuffix()

        uCoordSet, tCoordSet = self._readCoordinates(coordsDir, suffix)

        if readFromExtra:
            micsFn = self._getExtraPath('input_micrographs.xmd')
        else:
            micsFn = self._getPath('input_micrographs.xmd')

        anglesSet = self._readAngles(micsFn, suffix)
        # Create CoordinatesTiltPair object
        outputset = self._createCoordinatesTiltPair(micTiltPairs,
                                                    uCoordSet, tCoordSet,
                                                    anglesSet, suffix)
        summary = self.getSummary(outputset)
        outputset.setObjComment(summary)
        outputName = 'outputCoordinatesTiltPair' + suffix
        outputs = {outputName: outputset}
        self._defineOutputs(**outputs)
        self._defineSourceRelation(self.inputMicrographsTiltedPair, outputset)
        if store:
            self._store()
Пример #44
0
class ProtResMap(ProtAnalysis3D):
    """
    ResMap is software tool for computing the local resolution of 3D
    density maps studied in structural biology, primarily by cryo-electron
    microscopy (cryo-EM).
     
    Please find the manual at http://resmap.sourceforge.net 
    """
    _label = 'local resolution'

    INPUT_HELP = """ Input volume(s) for ResMap.
    Required volume properties:
        1. The particle must be centered in the volume.
        2. The background must not been masked out.
    Desired volume properties:
        1. The volume has not been filtered in any way (low-pass filtering, etc.)
        2. The volume has a realistic noise spectrum.
           This is sometimes obtained by so-called amplitude correction.
           While a similar effect is often obtained by B-factor sharpening,
           please make sure that the spectrum does not blow up near Nyquist.
    """
    
    def __init__(self, **kwargs):
        ProtAnalysis3D.__init__(self, **kwargs)
        self.histogramData = String()
        self.plotData = String() # store some values for later plot
             
    #--------------------------- DEFINE param functions --------------------------------------------   
    
    def _defineParams(self, form):
        form.addSection(label='Input')
        form.addParam('useSplitVolume', params.BooleanParam, default=False,
                      label="Use half volumes?",
                      help='Use to split volumes for gold-standard FSC.')
        form.addParam('inputVolume', params.PointerParam,
                      pointerClass='Volume', condition="not useSplitVolume",
                      label="Input volume", important=True,
                      help=self.INPUT_HELP)
        form.addParam('volumeHalf1', params.PointerParam,
                      label="Volume half 1", important=True,
                      pointerClass='Volume', condition="useSplitVolume",
                      help=self.INPUT_HELP)
        form.addParam('volumeHalf2', params.PointerParam,
                      pointerClass='Volume', condition="useSplitVolume",
                      label="Volume half 2", important=True,
                      help=self.INPUT_HELP)

        form.addParam('applyMask', params.BooleanParam, default=False,
                      label="Mask input volume?",
                      help="It is not necessary to provide ResMap with a mask "
                           "volume. The algorithm will attempt to estimate a "
                           "mask volume by low-pass filtering the input volume "
                           "and thresholding it using a heuristic procedure.\n"
                           "If the automated procedure does not work well for "
                           "your particle, you may provide a mask volume that "
                           "matches the input volume in size and format. "
                           "The mask volume should be a binary volume with zero "
                           "(0) denoting the background/solvent and some positive"
                           "value (0+) enveloping the particle.")
        form.addParam('maskVolume', params.PointerParam, label="Mask volume",
                      pointerClass='VolumeMask', condition="applyMask",
                      help='Select a volume to apply as a mask.')

        form.addParam('whiteningLabel', params.LabelParam, important=True,
                      label="It is strongly recommended to use the "
                            "pre-whitening wizard.")
        line = form.addLine('Pre-whitening')
        line.addParam('prewhitenAng', params.FloatParam, default=10,
                      label="Angstroms")
        line.addParam('prewhitenRamp', params.FloatParam, default=1,
                      label='Ramp')
        
        group = form.addGroup('Extra parameters')
        #form.addSection(label='Optional')
        group.addParam('stepRes', params.FloatParam, default=1,
                      label='Step size (Ang):',
                      help='in Angstroms (min 0.25, default 1.0)')
        line = group.addLine('Resolution Range (A)', 
                            help="Default (0): algorithm will start a just above\n"
                                 "             2*voxelSize until 4*voxelSize.   \n"
                                 "These fields are provided to accelerate computation "
                                 "if you are only interested in analyzing a specific "
                                 "resolution range. It is usually a good idea to provide "
                                 "a maximum resolution value to save time. Another way to "
                                 "save computation is to provide a larger step size.")
        line.addParam('minRes', params.FloatParam, default=0, label='Min')
        line.addParam('maxRes', params.FloatParam, default=0, label='Max')
        group.addParam('pVal', params.FloatParam, default=0.05,
                      label='Confidence level:',
                      help="P-value, usually between [0.01, 0.05].\n\n"
                           "This is the p-value of the statistical hypothesis test "
                           "on which ResMap is based on. It is customarily set to  "
                           "0.05 although you are welcome to reduce it (e.g. 0.01) "
                           "if you would like to obtain a more conservative result. "
                           "Empirically, ResMap results are not much affected by the p-value.")
                     
    #--------------------------- INSERT steps functions --------------------------------------------  
    
    def _insertAllSteps(self):
        # Insert processing steps
        if self.useSplitVolume:
            inputs = [self.volumeHalf1, self.volumeHalf2]
            self.inputVolume.set(None)
        else:
            inputs = [self.inputVolume]
            self.volumeHalf1.set(None)
            self.volumeHalf2.set(None)
            
        locations = [i.get().getLocation() for i in inputs]
            
        self._insertFunctionStep('convertInputStep', *locations)
        self._insertFunctionStep('estimateResolutionStep', 
                                 self.pVal.get(), 
                                 self.minRes.get(), 
                                 self.maxRes.get(), 
                                 self.stepRes.get(),
                                 self.prewhitenAng.get(),
                                 self.prewhitenRamp.get())

    #--------------------------- STEPS functions --------------------------------------------       
    
    def convertInputStep(self, volLocation1, volLocation2=None):
        """ Convert input volume to .mrc as expected by ResMap. 
        Params:
            volLocation1: a tuple containing index and filename of the input volume.
            volLocation2: if not None, a tuple like volLocation1 for the split volume.
        """
        ih = ImageHandler()
        ih.convert(volLocation1, self._getPath('volume1.map'))
        if volLocation2 is not None:
            ih.convert(volLocation2, self._getPath('volume2.map')) 

    def estimateResolutionStep(self, pValue, minRes, maxRes, stepRes, ang, rampWeight):
        """ Call ResMap.py with the appropriate parameters. """
        results = self.runResmap(self._getPath())

        self.histogramData.set(dumps(results['resHisto']))
        plotDict = {'minRes': results['minRes'],
                    'maxRes': results['maxRes'],
                    'orig_n': results['orig_n'],
                    'n': results['n'],
                    'currentRes': results['currentRes']
                    }
        self.plotData.set(dumps(plotDict))
        self._store(self.histogramData, self.plotData)

        self.savePlots(results)

    def savePlots(self, results=None):
        """ Store png images of the plots to be used as images, """
        # Add resmap libraries to the path
        sys.path.append(os.environ['RESMAP_HOME'])
        # This is needed right now because we are having
        # some memory problem with matplotlib plots right now in web
        Plotter.setBackend('Agg')
        self._plotVolumeSlices().savefig(self._getExtraPath('volume1.map.png'))
        plot = self._plotResMapSlices(results['resTOTALma'])
        plot.savefig(self._getExtraPath('volume1_resmap.map.png'))
        self._plotHistogram().savefig(self._getExtraPath('histogram.png'))

    #--------------------------- INFO functions --------------------------------------------
    
    def _summary(self):
        summary = []
        return summary
    
    def _validate(self):
        errors = []

        if self.useSplitVolume:
            half1 = self.volumeHalf1.get()
            half2 = self.volumeHalf2.get()
            if half1.getSamplingRate() != half2.getSamplingRate():
                errors.append('The selected half volumes have not the same pixel size.')
            if half1.getXDim() != half2.getXDim():
                errors.append('The selected half volumes have not the same dimensions.')
                
        return errors
    
    #--------------------------- UTILS functions --------------------------------------------
 
    def runResmap(self, workingDir, wizardMode=False):
        """ Prepare the args dictionary to be used
        and call the ResMap algorithm.
        Params:
            workingDir: where to run ResMap
            wizardMode: some custom params to be used by the wizard
                to display the pre-whitening GUI and only that.
        with the  """
        self._enterDir(workingDir)
        
        volumes = ['volume1.map', 'volume2.map']
        
        # Add resmap libraries to the path
        sys.path.append(os.environ['RESMAP_HOME'])
        from ResMap_algorithm import ResMap_algorithm
        from ResMap_fileIO import MRC_Data
        
        # Always read the first volume as mrc data
        data1 = MRC_Data(volumes[0],'ccp4')
        
        prewhitenArgs = {'display': wizardMode,
                         'force-stop': wizardMode                         
                         }
        if (self.prewhitenAng.hasValue() and 
            self.prewhitenRamp.hasValue()):
            prewhitenArgs['newElbowAngstrom'] = self.prewhitenAng.get()
            prewhitenArgs['newRampWeight'] = self.prewhitenRamp.get()
            
        args = {'pValue': self.pVal.get(),
                'minRes': self.minRes.get(), 
                'maxRes': self.maxRes.get(),
                'stepRes': self.stepRes.get(),
                'chimeraLaunch': False, # prevent ResMap to launch some graphical analysis
                'graphicalOutput': False, 
                'scipionPrewhitenParams': prewhitenArgs
                }
        
        if self.useSplitVolume:
            # Read the second splitted volume
            data2 = MRC_Data(volumes[1],'ccp4')
            args.update({'vxSize': self.volumeHalf1.get().getSamplingRate(),
                         'inputFileName1': 'volume1.map',
                         'inputFileName2': 'volume2.map',
                         'data1': data1,
                         'data2': data2,
                         })
        else:
            args.update({'vxSize': self.inputVolume.get().getSamplingRate(),
                         'inputFileName': 'volume1.map',
                         'data': data1,
                         })   
            
        results = ResMap_algorithm(**args)  
        self._leaveDir()

        return results
    
    #--------- Functions related to Plotting
    
    def _getVolumeMatrix(self, volName):
        from ResMap_fileIO import MRC_Data
        
        volPath = self._getPath(volName)
        return MRC_Data(volPath, 'ccp4').matrix
    
    def _plotVolumeSlices(self, **kwargs):
        from ResMap_visualization import plotOriginalVolume
        fig = plotOriginalVolume(self._getVolumeMatrix('volume1.map'), **kwargs)
        return Plotter(figure=fig)
        
    def _plotResMapSlices(self, data=None, **kwargs):
        from ResMap_visualization import plotResMapVolume
        plotDict = loads(self.plotData.get())
        if data is None:
            data = self._getVolumeMatrix('volume1_resmap.map')
            data  = np.ma.masked_where(data > plotDict['currentRes'], data, copy=True)
        kwargs.update(plotDict)
        fig = plotResMapVolume(data, **kwargs)
        return Plotter(figure=fig)
             
    def _plotHistogram(self):
        from ResMap_visualization import plotResolutionHistogram
        histogramData = loads(self.histogramData.get())
        fig = plotResolutionHistogram(histogramData)
        return Plotter(figure=fig)    
Пример #45
0
 def __init__(self, **kwargs):
     ProtAnalysis3D.__init__(self, **kwargs)
     self.mappingFile = String()
Пример #46
0
class ProtResMap(ProtAnalysis3D):
    """
    ResMap is software tool for computing the local resolution of 3D
    density maps studied in structural biology, primarily by cryo-electron
    microscopy (cryo-EM).
     
    Please find the manual at http://resmap.sourceforge.net 
    """
    _label = 'local resolution'
    
    def __init__(self, **kwargs):
        ProtAnalysis3D.__init__(self, **kwargs)
        self.histogramData = String()
             
    #--------------------------- DEFINE param functions --------------------------------------------   
    
    def _defineParams(self, form):
        form.addSection(label='Input')
        form.addParam('inputVolume', PointerParam, pointerClass='Volume',  
                      label="Input volume", important=True,
                      help='Select the input volume.')
        form.addParam('useSplitVolume', BooleanParam, default=False,
                      label="Use split volume?", 
                      help='Use to split volumes for gold-standard FSC.')                
        form.addParam('splitVolume', PointerParam, label="Split volume", important=True, 
                      pointerClass='Volume', condition="useSplitVolume",
                      help='Select the second split volume.')  
        form.addParam('applyMask', BooleanParam, default=False,
                      label="Mask input volume?", 
                      help='If set to <No> ResMap will automatically compute a mask.')                
        form.addParam('maskVolume', PointerParam, label="Mask volume",  
                      pointerClass='VolumeMask', condition="applyMask",
                      help='Select a volume to apply as a mask.')
        
        line = form.addLine('Pre-whitening')
        line.addParam('prewhitenAng', FloatParam, default=0, label="Angstroms")
        line.addParam('prewhitenRamp', FloatParam, default=0, label='Ramp')                      
        
        group = form.addGroup('Extra parameters')
        #form.addSection(label='Optional')
        group.addParam('stepRes', FloatParam, default=1,
                      label='Step size (Ang):',
                      help='in Angstroms (min 0.25, default 1.0)')
        line = group.addLine('Resolution Range (A)', 
                            help='Default: algorithm will start a just above 2*voxelSize')
        line.addParam('minRes', FloatParam, default=0, label='Min')
        line.addParam('maxRes', FloatParam, default=0, label='Max')
        group.addParam('pVal', FloatParam, default=0.05,
                      label='Confidence level:',
                      help='usually between [0.01, 0.05]')   
                     
    #--------------------------- INSERT steps functions --------------------------------------------  
    
    def _insertAllSteps(self):
        # Insert processing steps
        inputs = [self.inputVolume.get().getLocation()]
        if self.useSplitVolume:
            inputs.append(self.splitVolume.get().getLocation())
            
        self._insertFunctionStep('convertInputStep', *inputs)
        self._insertFunctionStep('estimateResolutionStep', 
                                 self.pVal.get(), 
                                 self.minRes.get(), 
                                 self.maxRes.get(), 
                                 self.stepRes.get(),
                                 self.prewhitenAng.get(),
                                 self.prewhitenRamp.get())
        self._insertFunctionStep('savePlotsStep')

    #--------------------------- STEPS functions --------------------------------------------       
    
    def convertInputStep(self, volLocation1, volLocation2=None):
        """ Convert input volume to .mrc as expected by ResMap. 
        Params:
            volLocation1: a tuple containing index and filename of the input volume.
            volLocation2: if not None, a tuple like volLocation1 for the split volume.
        """
        ih = ImageHandler()
        ih.convert(volLocation1, self._getPath('volume1.map'))
        if volLocation2 is not None:
            ih.convert(volLocation2, self._getPath('volume2.map')) 
            

    def estimateResolutionStep(self, pValue, minRes, maxRes, stepRes, ang, rampWeight):
        """ Call ResMap.py with the appropiate parameters. """
        results = self.runResmap(self._getPath())
        
        from cPickle import dumps
        self.histogramData.set(dumps(results['resHisto']))
        self._store(self.histogramData)
        
    def savePlotsStep(self):
        """ Store png images of the plots to be used
        from web. 
        """
        # Add resmap libraries to the path
        sys.path.append(os.environ['RESMAP_HOME'])
        # This is needed right now because we are having
        # some memory problem with matplotlib plots right now in web
        Plotter.setBackend('Agg')
        self._plotVolumeSlices().savefig(self._getExtraPath('volume1.map.png'))
        self._plotResMapSlices().savefig(self._getExtraPath('volume1_resmap.map.png'))
        self._plotHistogram().savefig(self._getExtraPath('histogram.png'))
        
    #--------------------------- INFO functions -------------------------------------------- 
    
    def _summary(self):
        summary = []
        return summary
    
    def _validate(self):
        errors = []
                
        return errors
    
    #--------------------------- UTILS functions --------------------------------------------
 
    def runResmap(self, workingDir, wizardMode=False):
        """ Prepare the args dictionary to be used
        and call the ResMap algorithm.
        Params:
            workingDir: where to run ResMap
            wizardMode: some custom params to be used by the wizard
                to display the pre-whitening GUI and only that.
        with the  """
        self._enterDir(workingDir)
        
        volumes = ['volume1.map', 'volume2.map']
        
        # Add resmap libraries to the path
        sys.path.append(os.environ['RESMAP_HOME'])
        from ResMap_algorithm import ResMap_algorithm
        from ResMap_fileIO import MRC_Data
        
        # Always read the first volume as mrc data
        data1 = MRC_Data(volumes[0],'ccp4')
        
        prewhitenArgs = {'display': wizardMode,
                         'force-stop': wizardMode                         
                         }
        if (self.prewhitenAng.hasValue() and 
            self.prewhitenRamp.hasValue()):
            prewhitenArgs['newElbowAngstrom'] = self.prewhitenAng.get()
            prewhitenArgs['newRampWeight'] = self.prewhitenRamp.get()
            
        args = {'vxSize': self.inputVolume.get().getSamplingRate(),
                'pValue': self.pVal.get(),
                'minRes': self.minRes.get(), 
                'maxRes': self.maxRes.get(),
                'stepRes': self.stepRes.get(),
                'chimeraLaunch': False, # prevent ResMap to launch some graphical analysis
                'graphicalOutput': False, 
                'scipionPrewhitenParams': prewhitenArgs
                }
        
        if self.useSplitVolume:
            # Read the second splitted volume
            data2 = MRC_Data(volumes[1],'ccp4')
            args.update({'inputFileName1': 'volume1.map',
                         'inputFileName2': 'volume2.map',
                         'data1': data1,
                         'data2': data2,
                         })
        else:
            args.update({'inputFileName': 'volume1.map',
                         'data': data1,
                         })   
            
        results = ResMap_algorithm(**args)  
        self._leaveDir()
        
        return results   
    
    #--------- Functions related to Plotting
    
    def _getVolumeMatrix(self, volName):
        from ResMap_fileIO import MRC_Data
        
        volPath = self._getPath(volName)
        return MRC_Data(volPath, 'ccp4').matrix
    
    def _plotVolumeSlices(self):
        from ResMap_visualization import plotOriginalVolume
        
        fig = plotOriginalVolume(self._getVolumeMatrix('volume1.map'))
        return Plotter(figure=fig)
        
    def _plotResMapSlices(self):
        from ResMap_visualization import plotResMapVolume
        
        fig = plotResMapVolume(self._getVolumeMatrix('volume1_resmap.map'),
                                  minRes=self.minRes.get(),
                                  maxRes=self.maxRes.get())
        return Plotter(figure=fig)
             
    def _plotHistogram(self):
        from ResMap_visualization import plotResolutionHistogram
        from cPickle import loads
        histogramData = loads(self.histogramData.get())
        fig = plotResolutionHistogram(histogramData)
        return Plotter(figure=fig)    
Пример #47
0
class XmippProtScreenParticles(ProtProcessParticles):
    """ Classify particles according their similarity to the others in order to detect outliers. """
    _label = 'screen particles'

    # Automatic Particle rejection enum
    REJ_NONE = 0
    REJ_MAXZSCORE = 1
    REJ_PERCENTAGE =2
    REJ_PERCENTAGE_SSNR =1
    #--------------------------- DEFINE param functions --------------------------------------------
    def _defineProcessParams(self, form):
        
        form.addParam('autoParRejection', EnumParam, choices=['None', 'MaxZscore', 'Percentage'],
                      label="Automatic particle rejection based on Zscore", default=self.REJ_NONE,
                      display=EnumParam.DISPLAY_COMBO, expertLevel=LEVEL_ADVANCED,
                      help='How to automatically reject particles. It can be none (no rejection), '
                      'maxZscore (reject a particle if its Zscore [a similarity index] is larger than this value), '
                      'Percentage (reject a given percentage in each one of the screening criteria). ')
        form.addParam('maxZscore', FloatParam, default=3, condition='autoParRejection==1',
                      label='Maximum Zscore', expertLevel=LEVEL_ADVANCED,
                      help='Maximum Zscore.', validators=[Positive])      
        form.addParam('percentage', IntParam, default=5, condition='autoParRejection==2',
                      label='Percentage (%)', expertLevel=LEVEL_ADVANCED,
                      help='The worse percentage of particles according to metadata labels: ZScoreShape1, ZScoreShape2, ZScoreSNR1, ZScoreSNR2, ZScoreHistogram are automatically disabled. Therefore, the total number of disabled particles belongs to [percetage, 5*percentage]', validators=[Range(0, 100, error="Percentage must be between 0 and 100.")])

        form.addParam('autoParRejectionSSNR', EnumParam, choices=['None', 'Percentage'],
                      label="Automatic particle rejection based on SSNR", default=self.REJ_NONE,
                      display=EnumParam.DISPLAY_COMBO, expertLevel=LEVEL_ADVANCED,
                      help='How to automatically reject particles. It can be none (no rejection), '
                      'Percentage (reject a given percentage of the lowest SSNRs). ')
        form.addParam('percentageSSNR', IntParam, default=5, condition='autoParRejectionSSNR==1',
                      label='Percentage (%)', expertLevel=LEVEL_ADVANCED,
                      help='The worse percentage of particles according to SSNR are automatically disabled.', 
                      validators=[Range(0, 100, error="Percentage must be between 0 and 100.")])
        form.addParallelSection(threads=0, mpi=0)
        
    def _getDefaultParallel(self):
        """This protocol doesn't have mpi version"""
        return (0, 0)
     
    #--------------------------- INSERT steps functions --------------------------------------------            
    def _insertAllSteps(self):
        """ Mainly prepare the command line for call cl2d program"""
        # Convert input images if necessary
        self._insertFunctionStep('sortImages', self.inputParticles.getObjId()) 
        self._insertFunctionStep('sortImagesSSNR', self.inputParticles.getObjId()) 
        
        self._insertFunctionStep('createOutputStep')

    #--------------------------- STEPS functions --------------------------------------------
    def sortImages(self, inputId):
        imagesMd = self._getPath('images.xmd')
        writeSetOfParticles(self.inputParticles.get(), imagesMd)
        args = "-i Particles@%s --addToInput " % imagesMd
        
        if self.autoParRejection == self.REJ_MAXZSCORE:
            args += "--zcut " + str(self.maxZscore.get())
        
        elif self.autoParRejection == self.REJ_PERCENTAGE:
            args += "--percent " + str(self.percentage.get())

        self.runJob("xmipp_image_sort_by_statistics", args)
        
        self.outputMd = String(imagesMd)

    def sortImagesSSNR(self, inputId):
        imagesMd = self._getPath('images.xmd')
        args = "-i Particles@%s " % imagesMd
        
        if self.autoParRejectionSSNR == self.REJ_PERCENTAGE_SSNR:
            args += "--ssnrpercent " + str(self.percentageSSNR.get())

        self.runJob("xmipp_image_ssnr", args)
        
    def createOutputStep(self):
        imgSet = self._createSetOfParticles()
        imgSet.copyInfo(self.inputParticles.get())
        readSetOfParticles(self.outputMd.get(), imgSet)

        self._defineOutputs(outputParticles=imgSet)

    #--------------------------- INFO functions --------------------------------------------                
    def _summary(self):
        import os
        summary = []
        if not hasattr(self, 'outputParticles'):
            summary.append("Output particles not ready yet.")
        else:
            zscores = [p._xmipp_zScore.get() for p in self.outputParticles]
            summary.append("The minimum ZScore is %.2f" % min(zscores))
            summary.append("The maximum ZScore is %.2f" % max(zscores))
            summary.append("The mean ZScore is %.2f" % (sum(zscores)*1.0/len(self.outputParticles)))
        return summary
    
    def _validate(self):
        pass
        
    def _citations(self):
        return ['Vargas2013b']
    
    def _methods(self):
        methods = []
        if hasattr(self, 'outputParticles'):
            outParticles = len(self.outputParticles) if self.outputParticles is not None else None
            particlesRejected = len(self.inputParticles.get())-outParticles if outParticles is not None else None
            particlesRejectedText = ' ('+str(particlesRejected)+')' if particlesRejected is not None else ''
            rejectionText = [
                             '',# REJ_NONE
                             ' and removing those not reaching %s%s' % (str(self.maxZscore.get()), particlesRejectedText),# REJ_MAXZSCORE
                             ' and removing worst %s percent%s' % (str(self.percentage.get()), particlesRejectedText)# REJ_PERCENTAGE
                             ]
            methods.append('Input dataset %s of %s particles was sorted by'
                           ' its ZScore using xmipp_image_sort_by_statistics'
                           ' program%s. ' % (self.getObjectTag('inputParticles'), len(self.inputParticles.get()), rejectionText[self.autoParRejection.get()]))
            methods.append('Output set is %s.'%self.getObjectTag('outputParticles'))
        return methods