def defineSteps(self):
        self.insertImportOfFiles([self.Input["microscope"]])
        # self.insertStep('createLink2', filename="microscope.xmd",dirSrc=self.importDir,dirDest=self.WorkingDir)
        self.insertStep("createDir", verifyfiles=[self.ExtraDir], path=self.ExtraDir)
        self.insertStep(
            "changeSamplingRate",
            fnIn=self.Input["acquisition"],
            fnOut=self.getFilename("acquisition"),
            downsampleFactor=self.DownsampleFactor,
        )

        MD = xmipp.MetaData(self.Input["micrographs"])
        previousId = XmippProjectDb.FIRST_STEP
        IOTable = {}
        MD.removeDisabled()
        for i in MD:
            fnMicrograph = MD.getValue(xmipp.MDL_MICROGRAPH, i)
            fnOut = os.path.join(self.ExtraDir, replaceBasenameExt(fnMicrograph, ".mrc"))
            IOTable[fnMicrograph] = fnOut
            self.insertParallelStep(
                "doDownsample",
                verifyfiles=[fnOut],
                parent_step_id=XmippProjectDb.FIRST_STEP,
                fnMicrograph=fnMicrograph,
                fnOut=fnOut,
                downsampleFactor=self.DownsampleFactor,
            )
        self.insertStep(
            "gatherResults",
            WorkingDir=self.WorkingDir,
            ImportDir=self.importDir,
            IOTable=IOTable,
            downsampleFactor=self.DownsampleFactor,
            tiltPairs=self.TiltPairs,
        )
예제 #2
0
def xmippMicrographsToEmx(micMd, emxData, emxDir):
    """ Export micrographs from xmipp metadata to EMX.
    """
    #acquisionInfo = findAcquisitionInfo(self.SelFileNameInitial)
    from protlib_particles import readPosCoordinates
    md = MetaData(micMd)
    acquisionInfo = findAcquisitionInfo(md.getValue(MDL_MICROGRAPH, md.firstObject()))
    micFn = 'mic%06d.mrc'
    index = 0
    pIndex = 0
    img = Image()
    hasCtf = md.containsLabel(MDL_CTF_MODEL)
    filesRoot = dirname(micMd)
    #assume sam pixelspacing for every micrograph
    acquisionInfo = findAcquisitionInfo(md.getValue(MDL_MICROGRAPH, md.firstObject()))
    if not acquisionInfo is None:
        mdAux = MetaData(acquisionInfo)
        pixelSpacing = mdAux.getValue(MDL_SAMPLINGRATE, mdAux.firstObject())
    else:
        pixelSpacing = 1.
        
    for objId in md:
        fnIn = md.getValue(MDL_MICROGRAPH, objId)
        index += 1
        fnOut = micFn % index
        img.read(fnIn)
        img.write(join(emxDir, fnOut))
        
        micrograph = EmxMicrograph(fnOut)
        # Set CTF parameters if present
        if hasCtf:
            xmippCtfToEmx(md, objId, micrograph)
            
        posFile = join(filesRoot, 'extra', replaceBasenameExt(fnIn, POSENDING))
        
        if exists(posFile):
            mdPos = readPosCoordinates(posFile)            
            for pId in mdPos:
                pIndex += 1
                # We are using here a dummy filename, since not make sense
                # for particles with just coordinates
                particle = EmxParticle("", pIndex)
                particle.set('centerCoord__X', mdPos.getValue(MDL_XCOOR, pId))
                particle.set('centerCoord__Y', mdPos.getValue(MDL_YCOOR, pId))
                particle.setMicrograph(micrograph)
                emxData.addObject(particle)
        micrograph.set('pixelSpacing__X',pixelSpacing)
        micrograph.set('pixelSpacing__Y',pixelSpacing)
        micrograph.set(COMMENT,'original filename=%s'%fnIn)

        emxData.addObject(micrograph)
    # Write EMX particles
    _writeEmxData(emxData, join(emxDir, 'micrographs.emx'))
예제 #3
0
def emxCoordsToXmipp(emxData, filesRoot, mdFn=None):
    """ This function will iterate for each particle and 
    create several .pos metadata (one per micrograph) where 
    the coordinates will written.
    """
    mdDict = {}
    # Create a dictionary for each micrograph
    for mic in emxData.iterClasses(MICROGRAPH):
        micFileName = mic.get(FILENAME)

        if micFileName is None:
            raise Exception("emxCoordsToXmipp: Xmipp doesn't support Micrograph without filename")
        mdDict[micFileName] = MetaData()
        
    #loop through particles and save coordinates in the right place
    for part in emxData.iterClasses(PARTICLE):
        mic = part.getMicrograph()
        micFileName = mic.get(FILENAME)
        md = mdDict[micFileName]
        objId = md.addObject()
        md.setValue(MDL_XCOOR, int(part.get('centerCoord__X')), objId)
        md.setValue(MDL_YCOOR, int(part.get('centerCoord__Y')), objId)        
    
    #save metadtas with coordinates
    createFn = False
    if mdFn is None:
        createFn=True
    for mic in emxData.iterClasses(MICROGRAPH):
        micFileName = mic.get(FILENAME)
        md = mdDict[micFileName]
        if createFn:
            mdFn = join(filesRoot, replaceBasenameExt(micFileName, POSENDING))
        #else reading single file in which I give the filename
        md.write('particles@' + mdFn)

    #save boxsize
    part = emxData.iterClasses(PARTICLE)[0]
    if part.has('boxSize__X'):
        boxSize = part.get('boxSize__X')
        md = MetaData()
        objId = md.addObject()
        md.setValue(MDL_PICKING_PARTICLE_SIZE, int(boxSize), objId)
        md.write('properties@' + join(filesRoot, 'config.xmd'))
예제 #4
0
    def defineSteps(self): 
        #temporary normalized files
        images_xmd = join(self.ExtraDir, 'images.xmd')
        #create extra output directory
        self.insertStep('createDir', verifyfiles=[self.ExtraDir], path=self.ExtraDir)
        self.insertStep("linkAcquisitionInfo", InputFile=self.ImgMd, dirDest=self.WorkingDir) 
        #normalize data

        
        if self.DoContinue:
            self._insertStepsContinue()
            firstIteration = getIteration(self.optimiserFileName)
        else:
            if self.DoNormalizeInputImage:
        	images_stk = join(self.ExtraDir, 'images_normalized.stk')
        	images_xmd = join(self.ExtraDir, 'images_normalized.xmd')
        	self.insertStep('runNormalizeRelion', verifyfiles=[images_stk, images_xmd],
                        	inputMd  = self.ImgMd,
                        	outputMd = images_stk,
                        	normType = 'NewXmipp',
                        	bgRadius = int(self.MaskRadiusA/self.SamplingRate),
                        	Nproc    = self.NumberOfMpi*self.NumberOfThreads
                        	)
            else:
        	imgFn = FileName(self.ImgMd).removeBlockName()
        	self.insertStep('createLink',
                        	   verifyfiles=[images_xmd],
                        	   source=imgFn,
                        	   dest=images_xmd)
            # convert input metadata to relion model
            self.ImgStar = self.extraPath(replaceBasenameExt(images_xmd, '.star'))
            self.insertStep('convertImagesMd', verifyfiles=[self.ImgStar],
                            inputMd=images_xmd, 
                            outputRelion=self.ImgStar                            
                            )
            self._insertSteps()
            firstIteration = 1
            
        self.defineSteps2(firstIteration, self.NumberOfIterations, self.NumberOfClasses)
예제 #5
0
def emxMicsToXmipp(emxData, 
                   outputFileName=MICFILE, 
                   filesPrefix=None,
                   ctfRoot=''
                   ):
    """ This function will iterate over the EMX micrographs and create
    the equivalent Xmipp 'micrographs.xmd' with the list of micrographs.
    If CTF information is found, for each micrograph, an attribute CTF_MODEL
    will be added to the micrograph file as expected by Xmipp.
    This function will NOT create the CTF param files. See emxCTFToXmipp
    """
    samplingRate    =  0.
    oldSamplingRate = -1.
    mdMic           = MetaData()
    mdCtfParam      = MetaData()
    hasCTF          = False
#    if (emxData.objLists[PARTICLE][0]).get('defocusU') is not None\
#       or \
    if (emxData.objLists[MICROGRAPH][0]).get('defocusU') is not None:
        hasCTF = True

    for micrograph in emxData.iterClasses(MICROGRAPH):
        micIndex = micrograph.get(INDEX)
        micFileName = micrograph.get(FILENAME)

        if micFileName is None:
            raise Exception("emxMicsToXmipp: Xmipp doesn't support Micrograph without filename")

        if filesPrefix is not None:
            micFileName = join(filesPrefix, micFileName)
        # micIndex is ignored now, in a more general solution
        # Xmipp should be able to handle micrographs in a stack
        # where the index has sense....

        mdMicId = mdMic.addObject()
        mdMic.setValue(MDL_MICROGRAPH, micFileName, mdMicId)
        if hasCTF:
            ctfModelFileName = join(ctfRoot, replaceBasenameExt(micFileName, CTFENDING))
            mdMic.setValue(MDL_CTF_MODEL, ctfModelFileName, mdMicId)

        #sampling is set in another function
        _samRateX = micrograph.get('pixelSpacing__X')
        _samRateY = micrograph.get('pixelSpacing__Y')

        if _samRateY is not None:
            if _samRateX != _samRateY:
                raise Exception ('pixelSpacingX != pixelSpacingY. Xmipp does not support it')

        if _samRateX is not None:
            samplingRate    = _samRateX
            if (oldSamplingRate > -1):
                if oldSamplingRate != samplingRate:
                    raise Exception ('Xmipp emx import cannot import emx files with different samplingRate') 
            oldSamplingRate = samplingRate


    # Sort metadata by micrograph name
    mdMic.sort(MDL_MICROGRAPH)
    # Write micrographs metadata
    mdMic.write('Micrographs@' + outputFileName)
    return samplingRate
예제 #6
0
def emxCTFToXmipp(emxData, 
                   outputFileName=MICFILE, 
                   filesPrefix='', #dirname
                   ctfRoot='', 
                   _voltage=None,
                   _sphericalAberration=None,
                   _samplingRate=None,
                   _amplitudeContrast=None
                   ):
    """ This function will iterate over the EMX micrographs and create
    a file .ctfparam for each micrograph.
    """
    hasCTFMicro = False
    hasCTFParticle = False
    if emxData.objLists[PARTICLE] and (emxData.objLists[PARTICLE][0]).get('defocusU') is not None:
        hasCTFParticle = True
        objectClass = PARTICLE

    if (emxData.objLists[MICROGRAPH][0]).get('defocusU') is not None:
        hasCTFMicro = True
        objectClass = MICROGRAPH

    if not (hasCTFParticle or hasCTFMicro):
        print "No CTF information available"
        return # no ctf info

    #fill metadata with CTF info
    #objMd = MetaData()
    for object in emxData.iterClasses(objectClass):
        objIndex = object.get(INDEX)
        objFileName = object.get(FILENAME)
        if objFileName is None:
            raise Exception("emxCTFToXmipp: Xmipp doesn't support Objects without filename")
        elif objIndex is not None:
            objFileName = join(filesPrefix, "%06d@%s"%(int(objIndex),objFileName))
        else:
            objFileName = join(filesPrefix, "%s"%(objFileName))

        ctfModelFileName = join(ctfRoot, replaceBasenameExt(objFileName, CTFENDING))
        ctf = CTFDEFOCUS()
        # Set the variables in the dict
        for var in CTF.ctfVarLabels.keys():
            setattr(ctf, var, object.get(var))

    if not ctfRoot:# is None:
        ctfRoot = dirname(outputFileName)

    for micrograph in emxData.iterClasses(MICROGRAPH):
        micIndex = micrograph.get(INDEX)
        micFileName = micrograph.get(FILENAME)

    samplingRate = 0.
    voltage      = 0.
    cs           = 0.
    amplitudeContrast = 0.
    
    oldSamplingRate = -1.
    oldVoltage      = -1.
    oldCs           = -1.
    oldAmplitudeContrast = -1.0
    for micrograph in emxData.iterClasses(MICROGRAPH):
        micIndex    = micrograph.get(INDEX)
        micFileName = micrograph.get(FILENAME)

        if micFileName is None:
            raise Exception("emxMicsToXmipp: Xmipp doesn't support Micrograph without filename")
#        if micFileName is None:
#            raise Exception("emxCTFToXmipp: Xmipp doesn't support Objects without filename")
        elif micIndex is not None:
            objFileName = join(filesPrefix, "%06d@%s"%(int(micIndex),micFileName))
        else:
            objFileName = join(filesPrefix, "%s"%(micFileName))

        ctfModelFileName = join(ctfRoot, replaceBasenameExt(micFileName, CTFENDING))

        ctf = CTF()
        ctf.pixelSpacing__Y = micrograph.get('pixelSpacing__Y')
        
        # Set the variables in the dict
        for var in CTF.ctfVarLabels.keys():
            setattr(ctf, var, micrograph.get(var))

        # Now do some adjusments to vars        
        if ctf.defocusU is not None:
            ctf.defocusU *= 10.
        
        if ctf.defocusV is None:
            ctf.defocusV = ctf.defocusU
            ctf.defocusUAngle = 0.
        else:
            ctf.defocusV *= 10.
            
        if ctf.pixelSpacing__Y is not None:
            if ctf.pixelSpacing__X != ctf.pixelSpacing__Y:
                raise Exception ('pixelSpacingX != pixelSpacingY. Xmipp does not support it')
            
        if ctf.pixelSpacing__X is not None:
            samplingRate    = ctf.pixelSpacing__X
            if (oldSamplingRate > -1):
                if oldSamplingRate != samplingRate:
                    raise Exception ('Xmipp emx import cannot import emx files with different samplingRate') 
            oldSamplingRate = samplingRate

        if ctf.acceleratingVoltage is not None:
            voltage         = ctf.acceleratingVoltage
            if (oldVoltage > -1):
                if oldVoltage != voltage:
                    raise Exception ('Xmipp emx import cannot import emx files with different voltage') 
            oldVoltage      = voltage

        if ctf.cs is not None:
            cs              = ctf.cs
            if (oldCs > -1):
                if oldCs != cs:
                    raise Exception ('Xmipp emx import cannot import emx files with different cs') 
            oldCs           = cs
            
        if ctf.amplitudeContrast is not None:
            amplitudeContrast              = ctf.amplitudeContrast
            if (oldAmplitudeContrast > -1):
                if oldAmplitudeContrast != amplitudeContrast:
                    raise Exception ('Xmipp emx import cannot import emx files with different amplitudeContrast') 
            oldAmplitudeContrast           = amplitudeContrast
            
        # Create the .ctfparam, replacing the micrograph name
        #write only if ctf information is available
        if (ctf.acceleratingVoltage is not None and
            ctf.cs is not None and
            ctf.amplitudeContrast is not None
            ):
            mdCtf = RowMetaData()
            
            for var, label in CTF.ctfVarLabels.iteritems():
                v = getattr(ctf, var)
                if v is not None:
                    mdCtf.setValue(label, float(v))

            mdCtf.setValue(MDL_CTF_K, 1.0)
            mdCtf.write(ctfModelFileName)

    # Sort metadata by micrograph name
    #mdMic.sort(MDL_MICROGRAPH)
    # Write micrographs metadata
    #mdMic.write('Micrographs@' + outputFileName)
    return voltage, cs, samplingRate