示例#1
0
def writeSetOfClasses2D(classes2DSet, filename, classesBlock='classes', writeParticles=True):    
    """ This function will write a SetOfClasses2D as Xmipp metadata.
    Params:
        classes2DSet: the SetOfClasses2D instance.
        filename: the filename where to write the metadata.
    """
    classFn = '%s@%s' % (classesBlock, filename)
    classMd = xmipp.MetaData()
    classMd.write(classFn) # Empty write to ensure the classes is the first block
    
    classRow = XmippMdRow()
    for class2D in classes2DSet:        
        class2DToRow(class2D, classRow)
        classRow.writeToMd(classMd, classMd.addObject())
        if writeParticles:
            ref = class2D.getObjId()
            imagesFn = 'class%06d_images@%s' % (ref, filename)
            imagesMd = xmipp.MetaData()
            imgRow = XmippMdRow()
            if class2D.getSize() > 0:
                for img in class2D:
                    particleToRow(img, imgRow)
                    imgRow.writeToMd(imagesMd, imagesMd.addObject())
            imagesMd.write(imagesFn, xmipp.MD_APPEND)
    
    classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block
示例#2
0
def writeSetOfClassesVol(classesVolSet, filename, classesBlock='classes'):    
    """ This function will write a SetOfClassesVol as Xmipp metadata.
    Params:
        classesVolSet: the SetOfClassesVol instance.
        filename: the filename where to write the metadata.
    """
    classFn = '%s@%s' % (classesBlock, filename)
    classMd = xmipp.MetaData()
    classMd.write(classFn) # Empty write to ensure the classes is the first block
    
    classRow = XmippMdRow()
    for classVol in classesVolSet:        
        classVolToRow(classVol, classRow)
        classRow.writeToMd(classMd, classMd.addObject())
        ref = class3D.getObjId()
        imagesFn = 'class%06d_images@%s' % (ref, filename)
        imagesMd = xmipp.MetaData()
        imgRow = XmippMdRow()
        
        for vol in classVol:
            volumeToRow(vol, imgRow)
            imgRow.writeToMd(imagesMd, imagesMd.addObject())
        imagesMd.write(imagesFn, xmipp.MD_APPEND)
  
    classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block
示例#3
0
    def _appendRctImages(self, particles):

        blockMd = "class%06d_images@%s" % (particles.getObjId(),
                                           self.rctClassesFn)

        classMd = xmipp.MetaData()

        uImages = self.inputParticlesTiltPair.get().getUntilted()
        tImages = self.inputParticlesTiltPair.get().getTilted()
        sangles = self.inputParticlesTiltPair.get().getCoordsPair().getAngles()

        uMics = self.inputParticlesTiltPair.get().getCoordsPair().getMicsPair(
        ).getUntilted()
        tMics = tImages.getCoordinates().getMicrographs()

        scaleFactor = uImages.getSamplingRate() / particles.getSamplingRate()

        for img in particles:
            imgId = img.getObjId()

            uImg = uImages[imgId]
            tImg = tImages[imgId]

            if uImg is None or tImg is None:
                print ">>> Warning, for id %d, tilted or untilted particle was not found. Ignored." % imgId
            else:
                objId = classMd.addObject()
                pairRow = XmippMdRow()
                pairRow.setValue(xmipp.MDL_IMAGE, getImageLocation(uImg))
                uCoord = uImg.getCoordinate()
                micId = uCoord.getMicId()
                uMic = uMics[micId]
                angles = sangles[micId]
                pairRow.setValue(xmipp.MDL_MICROGRAPH, uMic.getFileName())
                pairRow.setValue(xmipp.MDL_XCOOR, uCoord.getX())
                pairRow.setValue(xmipp.MDL_YCOOR, uCoord.getY())
                pairRow.setValue(xmipp.MDL_ENABLED, 1)
                pairRow.setValue(xmipp.MDL_ITEM_ID, long(imgId))
                pairRow.setValue(xmipp.MDL_REF, 1)

                alignment = img.getTransform()

                # Scale alignment by scaleFactor
                alignment.scale(scaleFactor)
                alignmentToRow(alignment, pairRow, alignType=ALIGN_2D)

                pairRow.setValue(xmipp.MDL_IMAGE_TILTED,
                                 getImageLocation(tImg))
                tMic = tMics[micId]
                pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED,
                                 tMic.getFileName())
                (angleY, angleY2, angleTilt) = angles.getAngles()
                pairRow.setValue(xmipp.MDL_ANGLE_Y, float(angleY))
                pairRow.setValue(xmipp.MDL_ANGLE_Y2, float(angleY2))
                pairRow.setValue(xmipp.MDL_ANGLE_TILT, float(angleTilt))

                pairRow.writeToMd(classMd, objId)

        classMd.write(blockMd, xmipp.MD_APPEND)
示例#4
0
def iterMdRows(md):
    """ Iterate over the rows of the given metadata. """
    # If md is string, take as filename and create the metadata
    if isinstance(md, basestring):
        md = xmipp.MetaData(md)

    row = XmippMdRow()

    for objId in md:
        row.readFromMd(md, objId)
        yield row
示例#5
0
def writeCTFModel(ctfModel, ctfFile):
    """ Given a CTFModel object write as Xmipp ctfparam
    """
    md = xmipp.MetaData()

    objId = md.addObject()
    ctfRow = XmippMdRow()
    ctfModelToRow(ctfModel, ctfRow)
    ctfRow.writeToMd(md, objId)

    md.setColumnFormat(False)
    md.write(ctfFile)
示例#6
0
def getMdFirstRow(filename):
    """ Create a MetaData but only read the first row.
    This method should be used for validations of labels
    or metadata size, but the full metadata is not needed.
    """
    md = xmipp.MetaData()
    md.read(filename, 1)
    if md.getParsedLines():
        row = XmippMdRow()
        row.readFromMd(md, md.firstObject())
    else:
        row = None

    return row
示例#7
0
def setOfImagesToMd(imgSet, md, imgToFunc, **kwargs):
    """ This function will fill Xmipp metadata from a SetOfMicrographs
    Params:
        imgSet: the set of images to be converted to metadata
        md: metadata to be filled
        rowFunc: this function can be used to setup the row before 
            adding to metadata.
    """
    
    if 'alignType' not in kwargs:
        kwargs['alignType'] = imgSet.getAlignment()
        
    for img in imgSet:
        objId = md.addObject()
        imgRow = XmippMdRow()
        imgToFunc(img, imgRow, **kwargs)
        imgRow.writeToMd(md, objId)
示例#8
0
def writeSetOfMicrographsPairs(uSet, tSet, filename):
    """ This function will write a MicrographsTiltPair as Xmipp metadata.
    Params:
        uSet: the untilted set of micrographs to be written
        tSet: the tilted set of micrographs to be written
        filename: the filename where to write the metadata.
    """
    md = xmipp.MetaData()

    for micU, micT in izip(uSet, tSet):
        objId = md.addObject()
        pairRow = XmippMdRow()
        pairRow.setValue(xmipp.MDL_ITEM_ID, long(micU.getObjId()))
        pairRow.setValue(xmipp.MDL_MICROGRAPH, micU.getFileName())
        pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED, micT.getFileName())
        pairRow.writeToMd(md, objId)
        
    md.write(filename)   
示例#9
0
def writeSetOfMovies(moviesSet, filename, moviesBlock='movies'):    
    """ This function will write a SetOfMovies as Xmipp metadata.
    Params:
        moviesSet: the SetOfMovies instance.
        filename: the filename where to write the metadata.
    """
       
    for movie in moviesSet:        
        
        ref = movie.getObjId()
        micrographsFn = 'movie%06d_micrographs@%s' % (ref, filename)
        micrographsMd = xmipp.MetaData()
        micRow = XmippMdRow()
        
        for mic in movie:
            micrographToRow(mic, micRow)
            micRow.writeToMd(micrographsMd, micrographsMd.addObject())
        micrographsMd.write(micrographsFn, xmipp.MD_APPEND)
示例#10
0
def writeSetOfCTFs(ctfSet, mdCTF):
    """ Write a ctfSet on metadata format. 
    Params:
        ctfSet: the SetOfCTF that will be read.
        mdCTF: The file where metadata should be written.
    """
    md = xmipp.MetaData()
            
    for ctfModel in ctfSet:
        objId = md.addObject()
        ctfRow = XmippMdRow()
        ctfRow.setValue(xmipp.MDL_MICROGRAPH, ctfModel.getMicFile())
        if ctfModel.getPsdFile():
            ctfRow.setValue(xmipp.MDL_PSD, ctfModel.getPsdFile())
        ctfModelToRow(ctfModel, ctfRow)
        ctfRow.writeToMd(md, objId)
        
    md.write(mdCTF)
    ctfSet._xmippMd = String(mdCTF)
示例#11
0
def writeSetOfDefocusGroups(defocusGroupSet, fnDefocusGroup): # also metadata
    """ Write a defocuGroupSet on metadata format. 
    Params:
        defocusGroupSet: the SetOfDefocus that will be read.
        fnDefocusGroup: The file where defocusGroup should be written.
    """
    md = xmipp.MetaData()
            
    for defocusGroup in defocusGroupSet:
        objId = md.addObject()
        defocusGroupRow = XmippMdRow()
        defocusGroupSetToRow(defocusGroup, defocusGroupRow)
        defocusGroupRow.setValue(xmipp.MDL_CTF_GROUP, defocusGroup.getObjId())
        defocusGroupRow.setValue(xmipp.MDL_MIN, defocusGroup.getDefocusMin())
        defocusGroupRow.setValue(xmipp.MDL_MAX, defocusGroup.getDefocusMax())
        defocusGroupRow.setValue(xmipp.MDL_AVG, defocusGroup.getDefocusAvg())
        defocusGroupRow.writeToMd(md, objId)
        
    md.write(fnDefocusGroup)
    defocusGroupSet._xmippMd = String(fnDefocusGroup)
示例#12
0
def micrographToCTFParam(mic, ctfparam):
    """ This function is used to convert a Micrograph object
    to the .ctfparam metadata needed by some Xmipp programs.
    If the micrograph already comes from xmipp, the ctfparam
    will be returned, if not, the new file. 
    """
    ctf = mic.getCTF()
    
    if hasattr(ctf, '_xmippMd'):
        return ctf._xmippMd.get()
    
    md = xmipp.MetaData()
    md.setColumnFormat(False)
    row = XmippMdRow()
    ctfModelToRow(ctf, row)
    acquisitionToRow(mic.getAcquisition(), row)
    row.writeToMd(md, md.addObject())
    md.write(ctfparam)
    
    return ctfparam
示例#13
0
def rowFromMd(md, objId):
    row = XmippMdRow()
    row.readFromMd(md, objId)
    return row