Ejemplo n.º 1
0
    def _insertAllSteps(self):
        atomsFn = self.getInputPdb().getFileName()
        # Define some outputs filenames
        self.imgsFn = self._getExtraPath('images.xmd')
        self.modesFn = self._getExtraPath('modes.xmd')
        self.structureEM = self.inputModes.get().getPdb().getPseudoAtoms()
        if self.structureEM:
            self.atomsFn = self._getExtraPath(basename(atomsFn))
            copyFile(atomsFn, self.atomsFn)
        else:
            localFn = self._getExtraPath(
                replaceBaseExt(basename(atomsFn), 'pdb'))
            cifToPdb(atomsFn, localFn)
            self.atomsFn = self._getExtraPath(basename(localFn))

        self._insertFunctionStep('convertInputStep', atomsFn)

        if self.copyDeformations.empty():  #ONLY FOR DEBUGGING
            self._insertFunctionStep("performNmaStep", self.atomsFn,
                                     self.modesFn)
        else:
            # TODO: for debugging and testing it will be useful to copy the deformations
            # metadata file, not just the deformation.txt file
            self._insertFunctionStep('copyDeformationsStep',
                                     self.copyDeformations.get())

        self._insertFunctionStep('createOutputStep')
Ejemplo n.º 2
0
 def getBestVolumesStep(self):
     volumes = []
     inliers = []
     
     for n in range(self.nRansac.get()):
         fnAngles = self._getTmpPath("angles_ransac%05d"%n+".xmd")
         md=xmipp.MetaData("inliers@"+fnAngles)
         numInliers=md.getValue(xmipp.MDL_WEIGHT,md.firstObject())
         volumes.append(fnAngles)
         inliers.append(numInliers)
     
     index = sorted(range(inliers.__len__()), key=lambda k: inliers[k])
     fnBestAngles = ''
     threshold=self.getCCThreshold()
  
     i = self.nRansac.get()-1
     indx = 0
     while i >= 0 and indx < self.numVolumes:
         fnBestAngles = volumes[index[i]]
         fnBestAnglesOut = self._getPath("proposedVolume%05d"%indx+".xmd")
         copyFile(fnBestAngles, fnBestAnglesOut)
         self._log.info("Best volume %d = %s" % (indx, fnBestAngles))
         if not self.useAll:
             self.runJob("xmipp_metadata_utilities","-i %s -o %s --query select \"maxCC>%f \" --mode append" %(fnBestAnglesOut,fnBestAnglesOut,threshold))
             if not isMdEmpty(fnBestAnglesOut):
                 indx += 1
         else:
             indx += 1
         i -= 1
         
     # Remove unnecessary files
     for n in range(self.nRansac.get()):
         fnAngles = self._getTmpPath("angles_ransac%05d"%n+".xmd")
         cleanPath(fnAngles)
Ejemplo n.º 3
0
    def exportAtomStructStep(self):
        exportAtomStruct = self.exportAtomStruct.get()
        originStructPath = exportAtomStruct.getFileName()
        dirName = self.filesPath.get()
        destinyStructPath = os.path.join(dirName, self.COORDINATEFILENAME)
        destinySympleStructPath = os.path.join(dirName, self.SYMPLIFIED_STRUCT)

        # save input atom struct with no change
        baseName = os.path.basename(originStructPath)
        localPath = os.path.abspath(os.path.join(dirName, baseName))
        copyFile(originStructPath, localPath)

        # call biopython to simplify atom struct and save it
        aSH = AtomicStructHandler()
        aSH.read(originStructPath)
        aSH.write(destinySympleStructPath)

        # if pdb convert to mmcif calling maxit twice
        if originStructPath.endswith(".pdb"):
            # convert pdb to cif using maxit program
            log = self._log
            fromPDBToCIF(originStructPath, destinyStructPath, log)
            try:
                # convert cif to mmCIF by using maxit program
                fromCIFTommCIF(destinyStructPath, destinyStructPath, log)
            except Exception as e:
                pass
        # if cif convert to mmcif using maxit
        elif originStructPath.endswith(".cif"):
            # convert cif to mmCIF by using maxit program
            log = self._log
            try:
                fromCIFTommCIF(originStructPath, destinyStructPath, log)
            except Exception as e:
                pass
    def importStep(self):
        if not self.multiple.get():
            copyFile(self.filePath.get(),self._getPath(os.path.split(self.filePath.get())[1]))
            fh = open(self.filePath.get())
            for line in fh.readlines():
                tokens = line.split(',')
                if len(tokens)==2:
                    fhSmile = open(self._getExtraPath(tokens[0].strip()+".smi"),'w')
                    fhSmile.write(tokens[1].strip()+"\n")
                    fhSmile.close()
        else:
            for filename in glob.glob(os.path.join(self.filesPath.get(), self.filesPattern.get())):
                fnSmall = self._getExtraPath(os.path.split(filename)[1])
                copyFile(filename, fnSmall)

        outputSmallMolecules = SetOfSmallMolecules().create(path=self._getPath(),suffix='SmallMols')
        for fnSmall in glob.glob(self._getExtraPath("*")):
            smallMolecule = SmallMolecule(smallMolFilename=fnSmall)

            if not fnSmall.endswith('.mae') and not fnSmall.endswith('.maegz'):
                fnRoot = os.path.splitext(os.path.split(fnSmall)[1])[0]
                fnOut = self._getExtraPath("%s.png" % fnRoot)
                args = Plugin.getPluginHome('utils/rdkitUtils.py') + " draw %s %s" % (fnSmall, fnOut)
                try:
                    Plugin.runRDKit(self, "python3", args)
                    smallMolecule._PDBLigandImage = pwobj.String(fnOut)
                except:
                    smallMolecule._PDBLigandImage = pwobj.String("Not available")

            outputSmallMolecules.append(smallMolecule)
        self._defineOutputs(outputSmallMols=outputSmallMolecules)
Ejemplo n.º 5
0
 def getBestVolumesStep(self):
     volumes = []
     inliers = []
     
     for n in range(self.nRansac.get()):
         fnAngles = self._getTmpPath("angles_ransac%05d"%n+".xmd")
         md=emlib.MetaData("inliers@"+fnAngles)
         numInliers=md.getValue(emlib.MDL_WEIGHT,md.firstObject())
         volumes.append(fnAngles)
         inliers.append(numInliers)
     
     index = sorted(range(inliers.__len__()), key=lambda k: inliers[k])
     fnBestAngles = ''
     threshold=self.getCCThreshold()
  
     i = self.nRansac.get()-1
     indx = 0
     while i >= 0 and indx < self.numVolumes:
         fnBestAngles = volumes[index[i]]
         fnBestAnglesOut = self._getPath("proposedVolume%05d"%indx+".xmd")
         copyFile(fnBestAngles, fnBestAnglesOut)
         self._log.info("Best volume %d = %s" % (indx, fnBestAngles))
         if not self.useAll:
             self.runJob("xmipp_metadata_utilities","-i %s -o %s --query select \"maxCC>%f \" --mode append" %(fnBestAnglesOut,fnBestAnglesOut,threshold))
             if not isMdEmpty(fnBestAnglesOut):
                 indx += 1
         else:
             indx += 1
         i -= 1
         
     # Remove unnecessary files
     for n in range(self.nRansac.get()):
         fnAngles = self._getTmpPath("angles_ransac%05d"%n+".xmd")
         cleanPath(fnAngles)
Ejemplo n.º 6
0
 def elasticAlignmentStep(self, nVoli, voli, nVolj):
     
     makePath(self._getExtraPath("modes%d"%nVoli))
     
     for i in range(self.numberOfModes.get() + 1):
         if i == 0 :
             i += 1 
         copyFile (self._getPath("modes/vec.%d"%i),
                   self._getExtraPath("modes%d/vec.%d"%(nVoli, i)))
         
     mdVol = xmipp.MetaData()
     fnOutMeta = self._getExtraPath('RigidAlignVol_%d_To_Vol_%d.xmd' % (nVolj, nVoli))
     mdVol.setValue(xmipp.MDL_IMAGE, self._getPath('outputRigidAlignment_vol_%d_to_%d.vol' % 
                                                   (nVolj, nVoli)), mdVol.addObject())      
     mdVol.write(fnOutMeta)
                                           
     fnPseudo = self._getPath("pseudoatoms_%d.pdb"%nVoli)
     fnModes = self._getPath("modes_%d.xmd"%nVoli)
     Ts = voli.getSamplingRate()
     fnDeform = self._getExtraPath('compDeformVol_%d_To_Vol_%d.xmd' % (nVolj, nVoli))
     sigma = Ts * self.pseudoAtomRadius.get()
     fnPseudoOut = self._getExtraPath('PseudoatomsDeformedPDB_Vol_%d_To_Vol_%d.pdb' % 
                                      (nVolj, nVoli))
     self.runJob('xmipp_nma_alignment_vol', 
                 "-i %s --pdb %s --modes %s --sampling_rate %s -o %s --fixed_Gaussian %s --opdb %s"%\
                (fnOutMeta, fnPseudo, fnModes, Ts, fnDeform, sigma, fnPseudoOut))
     
     fnVolOut = self._getExtraPath('DeformedVolume_Vol_%d_To_Vol_%d' % (nVolj, nVoli))
     self.runJob('xmipp_volume_from_pdb', "-i %s -o %s --sampling %s --fixed_Gaussian %s" % 
                 (fnPseudoOut, fnVolOut, Ts, sigma))
    def elasticAlignmentStep(self, nVoli, Ts, nVolj, fnAlignedVolj):
        fnVolOut = self._getExtraPath('DeformedVolume_Vol_%d_To_Vol_%d' %
                                      (nVolj, nVoli))
        if os.path.exists(fnVolOut + ".pdb"):
            return

        makePath(self._getExtraPath("modes%d" % nVoli))

        for i in range(self.numberOfModes.get() + 1):
            if i == 0:
                i += 1
            copyFile(self._getPath("modes/vec.%d" % i),
                     self._getExtraPath("modes%d/vec.%d" % (nVoli, i)))

        mdVol = xmippLib.MetaData()
        fnOutMeta = self._getExtraPath('RigidAlignVol_%d_To_Vol_%d.xmd' %
                                       (nVolj, nVoli))
        mdVol.setValue(xmippLib.MDL_IMAGE, fnAlignedVolj, mdVol.addObject())
        mdVol.write(fnOutMeta)

        fnPseudo = self._getPath("pseudoatoms_%d.pdb" % nVoli)
        fnModes = self._getPath("modes_%d.xmd" % nVoli)
        fnDeform = self._getExtraPath('compDeformVol_%d_To_Vol_%d.xmd' %
                                      (nVolj, nVoli))
        sigma = Ts * self.pseudoAtomRadius.get()
        fnPseudoOut = self._getExtraPath(
            'PseudoatomsDeformedPDB_Vol_%d_To_Vol_%d.pdb' % (nVolj, nVoli))
        self.runJob('xmipp_nma_alignment_vol',
                    "-i %s --pdb %s --modes %s --sampling_rate %s -o %s --fixed_Gaussian %s --opdb %s"%\
                   (fnOutMeta, fnPseudo, fnModes, Ts, fnDeform, sigma, fnPseudoOut))

        self.runJob(
            'xmipp_volume_from_pdb',
            "-i %s -o %s --sampling %s --fixed_Gaussian %s" %
            (fnPseudoOut, fnVolOut, Ts, sigma))
    def maskVolume(self, volFn, maskFn, index, halfString):
        ih = ImageHandler()

        # Check if there is a mask if not then use a spherical mask
        if maskFn is None:
            # Compute sphere radius and make a spherical mask
            maskFn = self._getFileName('mask', index + 1)
            volSize = ih.getDimensions(volFn)[0]
            radius = volSize / 2 - 1
            emlib.createEmptyFile(maskFn, volSize, volSize, volSize)
            program = "xmipp_transform_mask"
            args = "-i %s --mask circular %d --create_mask %s " % (
                maskFn, -1 * radius, maskFn)
            self.runJob(program, args)
        else:
            putils.copyFile(maskFn, self._getFileName('mask', index + 1))
            maskFn = self._getFileName('mask', index + 1)

        # Read the volume if it is provided
        if volFn.endswith(".mrc"):
            volFn = volFn + ":mrc"
        # Apply mask to the volume
        volMasked = self._getFileName('volume', 'masked', index + 1,
                                      halfString)
        program = 'xmipp_image_operate'
        args = '-i %s --mult %s -o %s' % (volFn, maskFn, volMasked)
        self.runJob(program, args)
 def calculateAverage(self, iteration):
     fnAvg = self._getExtraPath("volumeAvg.vol")
     N = 0
     for i in range(self.inputVolumes.get().getSize()):
         if iteration <= 2:
             fnVol = self._getExtraPath("volume%03d.vol" % i)
             if i == 0:
                 copyFile(fnVol, fnAvg)
             else:
                 self.runJob("xmipp_image_operate",
                             "-i %s --plus %s" % (fnAvg, fnVol),
                             numberOfMpi=1)
         else:
             fnVol = self._getExtraPath("volume%03d_best.vol" % i)
             self.runJob("xmipp_image_operate",
                         "-i %s --plus %s" % (fnAvg, fnVol),
                         numberOfMpi=1)
         N += 1
     if iteration > 0:
         self.runJob("xmipp_image_operate",
                     "-i %s --plus %s" %
                     (fnAvg, self._getExtraPath("volumeBest.vol")),
                     numberOfMpi=1)
         N += 1
     self.runJob("xmipp_image_operate",
                 "-i %s --divide %f" % (fnAvg, N),
                 numberOfMpi=1)
Ejemplo n.º 10
0
 def convertInputStep(self, atomsFn):
     # Write the modes metadata taking into account the selection
     self.writeModesMetaData()
     # Write a metadata with the normal modes information
     # to launch the nma alignment programs
     writeSetOfParticles(self.inputParticles.get(), self.imgsFn)
     # Copy the atoms file to current working dir
     copyFile(atomsFn, self.atomsFn)
Ejemplo n.º 11
0
 def convertInputStep(self, atomsFn):
     # Write the modes metadata taking into account the selection
     self.writeModesMetaData()
     # Write a metadata with the normal modes information
     # to launch the nma alignment programs
     writeSetOfParticles(self.inputParticles.get(), self.imgsFn)
     # Copy the atoms file to current working dir
     copyFile(atomsFn, self.atomsFn)
Ejemplo n.º 12
0
 def copyPdbStep(self, inputFn, localFn, isEM):
     """ Copy the input pdb file and also create a link 'atoms.pdb' """
     copyFile(inputFn, localFn)
     if isEM:
         fnOut = self._getPath('pseudoatoms.pdb')
     else:
         fnOut = self._getPath('atoms.pdb')
     if not os.path.exists(fnOut):
         createLink(localFn, fnOut)
Ejemplo n.º 13
0
 def copyPdbStep(self, inputFn, localFn, isEM):
     """ Copy the input pdb file and also create a link 'atoms.pdb' """
     copyFile(inputFn, localFn)
     if isEM:
         fnOut=self._getPath('pseudoatoms.pdb')
     else:
         fnOut=self._getPath('atoms.pdb')
     if not os.path.exists(fnOut):
         createLink(localFn, fnOut)
Ejemplo n.º 14
0
    def importMicrographs(self, pattern, suffix, voltage, sphericalAberration,
                          amplitudeContrast):
        """ Copy images matching the filename pattern
        Register other parameters.
        """
        filePaths = glob(expandPattern(pattern))

        #imgSet = SetOfMicrographs(filename=self.micsPairsSqlite, prefix=suffix)
        imgSet = self._createSetOfMicrographs(suffix=suffix)
        acquisition = imgSet.getAcquisition()
        # Setting Acquisition properties
        acquisition.setVoltage(voltage)
        acquisition.setSphericalAberration(sphericalAberration)
        acquisition.setAmplitudeContrast(amplitudeContrast)

        # Call a function that should be implemented by each subclass
        self._setOtherPars(imgSet)

        outFiles = [imgSet.getFileName()]
        imgh = ImageHandler()
        img = imgSet.ITEM_TYPE()
        n = 1
        size = len(filePaths)

        filePaths.sort()

        for i, fn in enumerate(filePaths):
            #             ext = os.path.splitext(basename(f))[1]
            dst = self._getExtraPath(basename(fn))
            if self.copyToProj:
                copyFile(fn, dst)
            else:
                createLink(fn, dst)

            if n > 1:
                for index in range(1, n + 1):
                    img.cleanObjId()
                    img.setFileName(dst)
                    img.setIndex(index)
                    imgSet.append(img)
            else:
                img.cleanObjId()
                img.setFileName(dst)
                # Fill the micName if img is a Micrograph.
                self._fillMicName(img, fn, pattern)
                imgSet.append(img)
            outFiles.append(dst)

            sys.stdout.write("\rImported %d/%d" % (i + 1, size))
            sys.stdout.flush()

        print "\n"

        imgSet.write()

        return imgSet
Ejemplo n.º 15
0
    def createReport(self):
        fnTex = "report.tex"
        fhTex = open(self._getExtraPath(fnTex), "w")
        template = """
\\documentclass[12pt]{article}
\\usepackage{amsmath,amsthm,amssymb,amsfonts} 
\\usepackage{graphicx}
\\usepackage{pdfpages}
\\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\\begin{document}
\\title{User Report}
\\author{Created by Scipion}
\\maketitle
"""
        fhTex.write(template)

        fnDir = self.filesPath.get()

        if not os.path.isdir(fnDir):
            fnDir = os.path.basename(fnDir)

        for fileName in sorted(glob.glob(os.path.join(fnDir, "*"))):
            fnDest = os.path.basename(fileName).lower()
            fnDest = fnDest.replace(" ", "_")
            fnDest = fnDest.replace(":", "_")
            fnDest = fnDest.replace(";", "_")
            copyFile(fileName, self._getExtraPath(fnDest))

            if fnDest.endswith(".tex") or fnDest.endswith(".txt"):
                fhTex.write("\\input{%s}\n" % fnDest)
                fhTex.write("\n")
            elif fnDest.endswith(".png") or fnDest.endswith(".jpg"):
                fhTex.write("\\begin{center}\n")
                fhTex.write("\\includegraphics[width=14cm]{%s}\n" % fnDest)
                fhTex.write("\\end{center}\n")
                fhTex.write("\n")
            elif fnDest.endswith(".pdf"):
                fhTex.write("\\includepdf[pages=-]{%s}\n" % fnDest)
                fhTex.write("\\clearpage\n")
                fhTex.write("\n")

        template = """ 
\\end{document}
"""
        fhTex.write(template)
        fhTex.close()

        args = "-interaction=nonstopmode " + fnTex
        self.runJob("pdflatex", args, cwd=self._getExtraPath())

        fnPDF = self._getExtraPath("report.pdf")

        if os.path.exists(fnPDF):
            moveFile(fnPDF, self._getPath("report.pdf"))
        else:
            raise Exception("PDF file was not produced.")
Ejemplo n.º 16
0
    def importMicrographs(self, pattern, suffix, voltage, sphericalAberration, amplitudeContrast):
        """ Copy images matching the filename pattern
        Register other parameters.
        """
        filePaths = glob(expandPattern(pattern))
        
        #imgSet = SetOfMicrographs(filename=self.micsPairsSqlite, prefix=suffix)
        imgSet = self._createSetOfMicrographs(suffix=suffix)
        acquisition = imgSet.getAcquisition()
        # Setting Acquisition properties
        acquisition.setVoltage(voltage)
        acquisition.setSphericalAberration(sphericalAberration)
        acquisition.setAmplitudeContrast(amplitudeContrast)
        
        # Call a function that should be implemented by each subclass
        self._setOtherPars(imgSet)
        
        outFiles = [imgSet.getFileName()]
        imgh = ImageHandler()
        img = imgSet.ITEM_TYPE()
        n = 1
        size = len(filePaths)
        
        filePaths.sort()
        
        for i, fn in enumerate(filePaths):
#             ext = os.path.splitext(basename(f))[1]
            dst = self._getExtraPath(basename(fn))
            if self.copyToProj:
                copyFile(fn, dst)
            else:
                createLink(fn, dst)
            
            if n > 1:
                for index in range(1, n+1):
                    img.cleanObjId()
                    img.setFileName(dst)
                    img.setIndex(index)
                    imgSet.append(img)
            else:
                img.cleanObjId()
                img.setFileName(dst)
                # Fill the micName if img is a Micrograph.
                self._fillMicName(img, fn, pattern)
                imgSet.append(img)
            outFiles.append(dst)
            
            sys.stdout.write("\rImported %d/%d" % (i+1, size))
            sys.stdout.flush()
            
        print "\n"
        
        imgSet.write()
        
        
        return imgSet
Ejemplo n.º 17
0
    def createReport(self):
        fnTex = "report.tex"
        fhTex = open(self._getExtraPath(fnTex), "w")
        template ="""
\\documentclass[12pt]{article}
\\usepackage{amsmath,amsthm,amssymb,amsfonts} 
\\usepackage{graphicx}
\\usepackage{pdfpages}
\\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\\begin{document}
\\title{User Report}
\\author{Created by Scipion}
\\maketitle
"""
        fhTex.write(template)
        
        fnDir = self.filesPath.get()

        if not os.path.isdir(fnDir):
            fnDir = os.path.basename(fnDir)

        for fileName in sorted(glob.glob(os.path.join(fnDir,"*"))):
            fnDest = os.path.basename(fileName).lower()
            fnDest = fnDest.replace(" ","_")
            fnDest = fnDest.replace(":","_")
            fnDest = fnDest.replace(";","_")
            copyFile(fileName, self._getExtraPath(fnDest))

            if fnDest.endswith(".tex") or fnDest.endswith(".txt"):
                fhTex.write("\\input{%s}\n" % fnDest)
                fhTex.write("\n")
            elif fnDest.endswith(".png") or fnDest.endswith(".jpg"):
                fhTex.write("\\begin{center}\n")
                fhTex.write("\\includegraphics[width=14cm]{%s}\n" % fnDest)
                fhTex.write("\\end{center}\n")
                fhTex.write("\n")
            elif fnDest.endswith(".pdf"):
                fhTex.write("\\includepdf[pages=-]{%s}\n" % fnDest)
                fhTex.write("\\clearpage\n")
                fhTex.write("\n")
        
        template = """ 
\\end{document}
"""
        fhTex.write(template)
        fhTex.close()

        args = "-interaction=nonstopmode " + fnTex
        self.runJob("pdflatex", args, cwd=self._getExtraPath())

        fnPDF = self._getExtraPath("report.pdf")

        if os.path.exists(fnPDF):
            moveFile(fnPDF,self._getPath("report.pdf"))
        else:
            raise Exception("PDF file was not produced.")
Ejemplo n.º 18
0
    def _moveCoordsToInfo(self, tomo):
        fnCoor = '*%s_info.json' % pwutils.removeBaseExt(tomo.getFileName().split("__")[0])
        pattern = os.path.join(self.path, fnCoor)
        files = glob.glob(pattern)

        if files:
            infoDir = pwutils.join(os.path.abspath(self.path), 'info')
            pathCoor = os.path.join(infoDir, os.path.basename(files[0]))
            pwutils.makePath(infoDir)
            copyFile(files[0], pathCoor)
Ejemplo n.º 19
0
def copyOrLinkFileName(imgRow, prefixDir, outputDir, copyFiles=False):
    index, imgPath = relionToLocation(imgRow.getValue(md.RLN_IMAGE_NAME))
    baseName = os.path.basename(imgPath)
    newName = os.path.join(outputDir, baseName)
    if not os.path.exists(newName):
        if copyFiles:
            copyFile(os.path.join(prefixDir, imgPath), newName)
        else:
            createLink(os.path.join(prefixDir, imgPath), newName)
            
    imgRow.setValue(md.RLN_IMAGE_NAME, locationToRelion(index, newName))
Ejemplo n.º 20
0
def copyOrLinkFileName(imgRow, prefixDir, outputDir, copyFiles=False):
    index, imgPath = relionToLocation(imgRow.getValue(md.RLN_IMAGE_NAME))
    baseName = os.path.basename(imgPath)
    newName = os.path.join(outputDir, baseName)
    if not os.path.exists(newName):
        if copyFiles:
            copyFile(os.path.join(prefixDir, imgPath), newName)
        else:
            createLink(os.path.join(prefixDir, imgPath), newName)

    imgRow.setValue(md.RLN_IMAGE_NAME, locationToRelion(index, newName))
Ejemplo n.º 21
0
    def createOutputStep(self, pdbPath):
        """ Copy the PDB structure and register the output object.
        """
        if not exists(pdbPath):
            raise Exception("PDB not found at *%s*" % pdbPath)

        baseName = basename(pdbPath)
        localPath = self._getExtraPath(baseName)
        copyFile(pdbPath, localPath)
        pdb = PdbFile()
        pdb.setFileName(localPath)
        self._defineOutputs(outputPdb=pdb)
Ejemplo n.º 22
0
 def createOutputStep(self, pdbPath):
     """ Copy the PDB structure and register the output object.
     """
     if not exists(pdbPath):
         raise Exception("PDB not found at *%s*" % pdbPath)
     
     baseName = basename(pdbPath)
     localPath = self._getExtraPath(baseName)
     copyFile(pdbPath, localPath)
     pdb = PdbFile()
     pdb.setFileName(localPath)
     self._defineOutputs(outputPdb=pdb)
Ejemplo n.º 23
0
 def copyDeformationsStep(self, deformationMd):
     copyFile(deformationMd, self.imgsFn)
     # We need to update the image name with the good ones
     # and the same with the ids.
     inputSet = self.inputParticles.get()
     mdImgs = md.MetaData(self.imgsFn)
     for objId in mdImgs:
         imgPath = mdImgs.getValue(md.MDL_IMAGE, objId)
         index, fn = xmippToLocation(imgPath)
         # Conside the index is the id in the input set
         particle = inputSet[index]
         mdImgs.setValue(md.MDL_IMAGE, getImageLocation(particle), objId)
         mdImgs.setValue(md.MDL_ITEM_ID, long(particle.getObjId()), objId)
     mdImgs.write(self.imgsFn)
Ejemplo n.º 24
0
 def copyDeformationsStep(self, deformationMd):
     copyFile(deformationMd, self.imgsFn)
     # We need to update the image name with the good ones
     # and the same with the ids.
     inputSet = self.inputParticles.get()
     md = xmipp.MetaData(self.imgsFn)
     for objId in md:
         imgPath = md.getValue(xmipp.MDL_IMAGE, objId)
         index, fn = xmippToLocation(imgPath)
         # Conside the index is the id in the input set
         particle = inputSet[index]
         md.setValue(xmipp.MDL_IMAGE, getImageLocation(particle), objId)
         md.setValue(xmipp.MDL_ITEM_ID, long(particle.getObjId()), objId)
     md.write(self.imgsFn)
Ejemplo n.º 25
0
    def weightParticles(self):
        iteration = 1
        fnDirCurrent = self._getExtraPath("Iter%03d" % iteration)
        from math import exp
        # Grab file
        fnDirGlobal = join(fnDirCurrent, "globalAssignment")
        fnAnglesDisc = join(fnDirGlobal, "anglesDisc.xmd")
        fnAngles = join(fnDirCurrent, "angles.xmd")

        copyFile(fnAnglesDisc, fnAngles)
        TsCurrent = self.readInfoField(fnDirGlobal, "sampling",
                                       xmipp.MDL_SAMPLINGRATE)
        Xdim = self.readInfoField(fnDirGlobal, "size", xmipp.MDL_XSIZE)
        self.writeInfoField(fnDirCurrent, "sampling", xmipp.MDL_SAMPLINGRATE,
                            TsCurrent)
        self.writeInfoField(fnDirCurrent, "size", xmipp.MDL_XSIZE, Xdim)

        if self.weightSSNR:
            self.runJob("xmipp_metadata_utilities","-i %s --set join %s particleId"%\
                        (fnAngles,self._getExtraPath("ssnrWeights.xmd")),numberOfMpi=1)

        md = xmipp.MetaData(fnAngles)
        doWeightSignificance = self.weightSignificance and md.containsLabel(
            xmipp.MDL_WEIGHT_SIGNIFICANT)
        for objId in md:
            weight = 1
            if self.weightSSNR:
                aux = md.getValue(xmipp.MDL_WEIGHT_SSNR, objId)
                weight *= aux
            if doWeightSignificance:
                aux = md.getValue(xmipp.MDL_WEIGHT_SIGNIFICANT, objId)
                weight *= aux
            md.setValue(xmipp.MDL_WEIGHT, weight, objId)
        md.write(fnAngles)

        # Weight by Astigmatism
        if self.weightAstigmatism:
            self.runJob("xmipp_transform_filter","-i %s --fourier astigmatism %f --sampling %f"%\
                        (fnAngles,self.weightAstigmatismSigma.get(),TsCurrent))
    def _importFromFolderStep(self):
        """ This function will copy Xmipp .pos files for
        simulating a particle picking run...this is only
        for testing purposes.
        """
        for f in getFiles(self.importFolder.get()):
            copyFile(f, self._getExtraPath())

        extradir = self._getExtraPath()  
        
        inputset = self.inputMicrographsTiltedPair.get()
        uSet = inputset.getUntilted()
        tSet = inputset.getTilted()

        # Create Untilted and Tilted SetOfCoordinates
        uCoordSet = self._createSetOfCoordinates(uSet, suffix='Untilted')
        readSetOfCoordinates(extradir, uSet, uCoordSet)
        uCoordSet.write()
        tCoordSet = self._createSetOfCoordinates(tSet, suffix='Tilted')
        readSetOfCoordinates(extradir, tSet, tCoordSet)
        tCoordSet.write()
        
        # Read Angles from faked input micrographs
        micsFn = self._getExtraPath('input_micrographs.xmd')
        setAngles = self._createSetOfAngles()
        readAnglesFromMicrographs(micsFn, setAngles)
        setAngles.write()
        # Create CoordinatesTiltPair object
        outputset = CoordinatesTiltPair(filename=self._getPath('coordinates_pairs.sqlite'))
        outputset.setTilted(tCoordSet)
        outputset.setUntilted(uCoordSet)
        outputset.setAngles(setAngles)
        outputset.setMicsPair(inputset)
        outputset.setObjComment(self.getSummary(outputset))
        for coordU, coordT in izip(uCoordSet, tCoordSet):
            outputset.append(TiltPair(coordU, coordT))
        
        self._defineOutputs(outputCoordinatesTiltPair=outputset)
        self._defineSourceRelation(inputset, outputset)
Ejemplo n.º 27
0
    def weightParticles(self):
        iteration=1
        fnDirCurrent=self._getExtraPath("Iter%03d"%iteration)
        from math import exp
        # Grab file
        fnDirGlobal=join(fnDirCurrent,"globalAssignment")
        fnAnglesDisc=join(fnDirGlobal,"anglesDisc.xmd")
        fnAngles=join(fnDirCurrent,"angles.xmd")

        copyFile(fnAnglesDisc, fnAngles)
        TsCurrent=self.readInfoField(fnDirGlobal,"sampling",xmipp.MDL_SAMPLINGRATE)
        Xdim=self.readInfoField(fnDirGlobal,"size",xmipp.MDL_XSIZE)
        self.writeInfoField(fnDirCurrent,"sampling",xmipp.MDL_SAMPLINGRATE,TsCurrent)
        self.writeInfoField(fnDirCurrent,"size",xmipp.MDL_XSIZE,Xdim)
            
        if self.weightSSNR:
            self.runJob("xmipp_metadata_utilities","-i %s --set join %s particleId"%\
                        (fnAngles,self._getExtraPath("ssnrWeights.xmd")),numberOfMpi=1)
        
        md=xmipp.MetaData(fnAngles)
        doWeightSignificance=self.weightSignificance and md.containsLabel(xmipp.MDL_WEIGHT_SIGNIFICANT)
        for objId in md:
            weight=1
            if self.weightSSNR:
                aux=md.getValue(xmipp.MDL_WEIGHT_SSNR,objId)
                weight*=aux
            if doWeightSignificance:
                aux=md.getValue(xmipp.MDL_WEIGHT_SIGNIFICANT,objId)
                weight*=aux
            md.setValue(xmipp.MDL_WEIGHT,weight,objId)
        md.write(fnAngles)
        
        # Weight by Astigmatism
        if self.weightAstigmatism:
            self.runJob("xmipp_transform_filter","-i %s --fourier astigmatism %f --sampling %f"%\
                        (fnAngles,self.weightAstigmatismSigma.get(),TsCurrent))
Ejemplo n.º 28
0
    def initializeStep(self, protId):
        protRef = self.protRefine.get()
        protClassName = protRef.getClassName()
        if protClassName.startswith('ProtRelionRefine3D'):
            protRef._createFilenameTemplates()
            half1Map = protRef._getFileName("final_half1_volume", ref3d=1)
            half2Map = protRef._getFileName("final_half2_volume", ref3d=1)

            putils.copyFile(self._getRelionMapFn(half1Map), self._getTmpPath())
            putils.copyFile(self._getRelionMapFn(half2Map), self._getTmpPath())

        elif protClassName.startswith('ProtFrealign'):
            protRef._createFilenameTemplates()
            lastIter = protRef._getLastIter()

            half1Map = protRef._getFileName('iter_vol1', iter=lastIter)
            half2Map = protRef._getFileName('iter_vol2', iter=lastIter)

            putils.copyFile(half1Map, self._getFileName("half1"))
            putils.copyFile(half2Map, self._getFileName("half2"))

        elif protClassName.startswith('XmippProtProjMatch'):
            iterN = protRef.getLastIter()
            protRef._initialize()
            half1 = protRef._getFileName('reconstructedFileNamesItersSplit1',
                                         iter=iterN,
                                         ref=1)
            half2 = protRef._getFileName('reconstructedFileNamesItersSplit2',
                                         iter=iterN,
                                         ref=1)
            ih = ImageHandler()
            ih.convert(half1, self._getFileName("half1"))
            ih.convert(half2, self._getFileName("half2"))

        elif protClassName.startswith('EmanProtRefine'):
            protRef._createFilenameTemplates()
            numRun = protRef._getRun()
            protRef._createIterTemplates(numRun)
            iterN = protRef._lastIter()
            print "RUN ITER: ", numRun, iterN, protRef._iterTemplate
            half1 = protRef._getFileName("mapEvenUnmasked", run=numRun)
            half2 = protRef._getFileName("mapOddUnmasked", run=numRun)

            ih = ImageHandler()
            ih.convert(half1, self._getFileName("half1"))
            ih.convert(half2, self._getFileName("half2"))
Ejemplo n.º 29
0
    def initializeStep(self, protId):
        protRef = self.protRefine.get()
        protClassName = protRef.getClassName()
        if protClassName.startswith('ProtRelionRefine3D'):
            protRef._createFilenameTemplates()
            half1Map = protRef._getFileName("final_half1_volume", ref3d=1)
            half2Map = protRef._getFileName("final_half2_volume", ref3d=1)
            
            putils.copyFile(self._getRelionMapFn(half1Map), self._getTmpPath())
            putils.copyFile(self._getRelionMapFn(half2Map), self._getTmpPath())
            
        elif protClassName.startswith('ProtFrealign'):
            protRef._createFilenameTemplates()
            lastIter = protRef._getLastIter()

            half1Map = protRef._getFileName('iter_vol1', iter=lastIter)
            half2Map = protRef._getFileName('iter_vol2', iter=lastIter)
            
            putils.copyFile(half1Map, self._getFileName("half1"))
            putils.copyFile(half2Map, self._getFileName("half2"))

        elif protClassName.startswith('XmippProtProjMatch'):
            iterN = protRef.getLastIter()
            protRef._initialize()
            half1 = protRef._getFileName('reconstructedFileNamesItersSplit1',
                                         iter=iterN, ref=1)
            half2 = protRef._getFileName('reconstructedFileNamesItersSplit2',
                                         iter=iterN, ref=1)
            ih = ImageHandler()
            ih.convert(half1, self._getFileName("half1"))
            ih.convert(half2, self._getFileName("half2"))
            
        elif protClassName.startswith('EmanProtRefine'):
            protRef._createFilenameTemplates()
            numRun = protRef._getRun()
            protRef._createIterTemplates(numRun)
            iterN = protRef._lastIter()
            print "RUN ITER: ", numRun, iterN, protRef._iterTemplate
            half1 = protRef._getFileName("mapEvenUnmasked", run=numRun)
            half2 = protRef._getFileName("mapOddUnmasked", run=numRun)
            
            ih = ImageHandler()
            ih.convert(half1, self._getFileName("half1"))
            ih.convert(half2, self._getFileName("half2"))
Ejemplo n.º 30
0
 def managingOutputFilesStep(self): 
     
     volList = [vol.clone() for vol in self._iterInputVolumes()]
     copyFile (self._getExtraPath ("CoordinateMatrixColumnF1.txt"),
               self._defineResultsName1())
     copyFile (self._getExtraPath ("CoordinateMatrixColumnF2.txt"),
               self._defineResultsName2())
     copyFile (self._getExtraPath ("CoordinateMatrixColumnF3.txt"),
               self._defineResultsName3())             
     cleanPattern(self._getExtraPath('pseudoatoms*'))
     cleanPattern(self._getExtraPath('vec_ani.pkl'))  
     cleanPattern(self._getExtraPath('CoordinateMatrixColumnF*'))  
     cleanPattern(self._getPath('modes/vec*'))
     cleanPath(self._getPath('modes'))
     
     if not self.keepingOutputFiles.get():
         cleanPattern(self._getPath('warnings*'))
         cleanPattern(self._getPath('outputRigid*'))
         cleanPattern(self._getExtraPath('RigidAlign*'))
         cleanPattern(self._getExtraPath('Pseudoatoms*'))
         cleanPattern(self._getExtraPath('comp*'))
         cleanPattern(self._getExtraPath('Deform*'))
    def managingOutputFilesStep(self):

        volList = [vol.clone() for vol in self._iterInputVolumes()]
        copyFile(self._getExtraPath("CoordinateMatrixColumnF1.txt"),
                 self._defineResultsName1())
        copyFile(self._getExtraPath("CoordinateMatrixColumnF2.txt"),
                 self._defineResultsName2())
        copyFile(self._getExtraPath("CoordinateMatrixColumnF3.txt"),
                 self._defineResultsName3())
        cleanPattern(self._getExtraPath('pseudoatoms*'))
        cleanPattern(self._getExtraPath('vec_ani.pkl'))
        cleanPattern(self._getExtraPath('CoordinateMatrixColumnF*'))
        cleanPattern(self._getPath('modes/vec*'))
        cleanPath(self._getPath('modes'))

        if not self.keepingOutputFiles.get():
            cleanPattern(self._getPath('warnings*'))
            cleanPattern(self._getPath('outputRigid*'))
            cleanPattern(self._getExtraPath('RigidAlign*'))
            cleanPattern(self._getExtraPath('Pseudoatoms*'))
            cleanPattern(self._getExtraPath('comp*'))
            cleanPattern(self._getExtraPath('Deform*'))
Ejemplo n.º 32
0
    def globalAssignment(self):
        iteration=1
        fnDirCurrent=self._getExtraPath("Iter%03d"%iteration)
        makePath(fnDirCurrent)

        fnGlobal=join(fnDirCurrent,"globalAssignment")
        makePath(fnGlobal)

        targetResolution=self.significantMaxResolution.get()
        TsCurrent=max(self.TsOrig,targetResolution/3)
        self.prepareImages(fnGlobal,TsCurrent)
        self.prepareReferences(fnGlobal,TsCurrent,targetResolution)

        # Calculate angular step at this resolution
        ResolutionAlignment=targetResolution
        newXdim=self.readInfoField(fnGlobal,"size",xmipp.MDL_XSIZE)
        angleStep=self.calculateAngStep(newXdim, TsCurrent, ResolutionAlignment)
        self.writeInfoField(fnGlobal,"angleStep",xmipp.MDL_ANGLE_DIFF,float(angleStep))
        
        # Significant alignment
        alpha=1-0.01*self.significantSignificance.get()
        fnDirSignificant=join(fnGlobal,"significant")
        fnImgs=join(fnGlobal,"images.xmd")
        makePath(fnDirSignificant)

        # Create defocus groups
        row=getFirstRow(fnImgs)
        if row.containsLabel(xmipp.MDL_CTF_MODEL) or row.containsLabel(xmipp.MDL_CTF_DEFOCUSU):
            self.runJob("xmipp_ctf_group","--ctfdat %s -o %s/ctf:stk --pad 2.0 --sampling_rate %f --phase_flipped  --error 0.1 --resol %f"%\
                        (fnImgs,fnDirSignificant,TsCurrent,targetResolution),numberOfMpi=1)
            moveFile("%s/ctf_images.sel"%fnDirSignificant,"%s/ctf_groups.xmd"%fnDirSignificant)
            cleanPath("%s/ctf_split.doc"%fnDirSignificant)
            md = xmipp.MetaData("numberGroups@%s"%join(fnDirSignificant,"ctfInfo.xmd"))
            fnCTFs="%s/ctf_ctf.stk"%fnDirSignificant
            numberGroups=md.getValue(xmipp.MDL_COUNT,md.firstObject())
            ctfPresent=True
        else:
            numberGroups=1
            ctfPresent=False

        # Generate projections
        fnReferenceVol=join(fnGlobal,"volumeRef.vol")
        fnGallery=join(fnDirSignificant,"gallery.stk")
        fnGalleryMd=join(fnDirSignificant,"gallery.xmd")
        args="-i %s -o %s --sampling_rate %f --sym %s --min_tilt_angle %f --max_tilt_angle %f"%\
             (fnReferenceVol,fnGallery,angleStep,self.symmetryGroup,self.angularMinTilt.get(),self.angularMaxTilt.get())
        self.runJob("xmipp_angular_project_library",args)
        cleanPath(join(fnDirSignificant,"gallery_angles.doc"))
        moveFile(join(fnDirSignificant,"gallery.doc"), fnGalleryMd)

        fnAngles=join(fnGlobal,"anglesDisc.xmd")
        for j in range(1,numberGroups+1):
            fnAnglesGroup=join(fnDirSignificant,"angles_group%02d.xmd"%j)
            if not exists(fnAnglesGroup):
                if ctfPresent:
                    fnGroup="ctfGroup%06d@%s/ctf_groups.xmd"%(j,fnDirSignificant)
                    fnGalleryGroup=join(fnDirSignificant,"gallery_group%06d.stk"%j)
                    fnGalleryGroupMd=join(fnDirSignificant,"gallery_group%06d.xmd"%j)
                    self.runJob("xmipp_transform_filter",
                                "-i %s -o %s --fourier binary_file %d@%s --save_metadata_stack %s --keep_input_columns"%\
                                (fnGalleryMd,fnGalleryGroup,j,fnCTFs,fnGalleryGroupMd))
                else:
                    fnGroup=fnImgs
                    fnGalleryGroupMd=fnGalleryMd
                args='-i %s --initgallery %s --odir %s --sym %s --iter 1 --alpha0 %f --alphaF %f --angularSampling %f --maxShift %d '\
                     '--minTilt %f --maxTilt %f --useImed --angDistance %f --dontReconstruct'%\
                     (fnGroup,fnGalleryGroupMd,fnDirSignificant,self.symmetryGroup,alpha,alpha,angleStep,\
                      round(self.angularMaxShift.get()*newXdim/100),self.angularMinTilt.get(),self.angularMaxTilt.get(),2*angleStep)
                self.runJob('xmipp_reconstruct_significant',args,numberOfMpi=self.numberOfThreads.get())
                moveFile(join(fnDirSignificant,"angles_iter001_00.xmd"),join(fnDirSignificant,"angles_group%02d.xmd"%j))
                self.runJob("rm -f",fnDirSignificant+"/images_*iter00?_*.xmd",numberOfMpi=1)
                if j==1:
                    copyFile(fnAnglesGroup, fnAngles)
                else:
                    self.runJob("xmipp_metadata_utilities","-i %s --set union %s"%(fnAngles,fnAnglesGroup),numberOfMpi=1)
        if ctfPresent:
            self.runJob("rm -f",fnDirSignificant+"/gallery*",numberOfMpi=1)
    def importStep(self):

        if not self.multiple.get():

            fnSmall = self._getExtraPath(os.path.split(self.filePath.get())[1])
            copyFile(self.filePath.get(), fnSmall)

            if fnSmall.endswith(".mol2"):  # Multiple mol2
                with open(fnSmall) as f:
                    lines = f.readlines()
                    i = 0
                    for line in lines:
                        if line.startswith("#"):
                            continue

                        if line.startswith("@<TRIPOS>MOLECULE"):
                            zincID = lines[i + 1].split()[0]
                            fname_small = self._getExtraPath("%s.mol2" %
                                                             zincID)
                            f_small = open(fname_small, 'w+')
                            f_small.write(line)

                        f_small.write(line)
                        try:
                            if lines[i + 1].startswith("@<TRIPOS>MOLECULE"):
                                f_small.close()
                        except:
                            f_small.close()

                        i += 1

                os.remove(fnSmall)

            elif fnSmall.endswith(".sdf"):  # Multiple sdf
                with open(fnSmall) as f:
                    lines = f.readlines()
                    lines2write = []
                    i = 0
                    for line in lines:
                        lines2write.append(i)

                        if line.startswith("$$$$"):
                            zincID = lines[i - 5].split()[0]
                            fname_small = self._getExtraPath("%s.sdf" % zincID)
                            f_small = open(fname_small, 'w+')
                            for l in lines2write:
                                f_small.write(lines[l])

                            lines2write = []
                            f_small.close()

                        i += 1

                os.remove(fnSmall)

            else:
                fh = open(self.filePath.get())
                for line in fh.readlines():
                    tokens = line.split(',')
                    if len(tokens) == 2:
                        fhSmile = open(
                            self._getExtraPath(tokens[0].strip() + ".smi"),
                            'w')
                        fhSmile.write(tokens[1].strip() + "\n")
                        fhSmile.close()
                fh.close()

        else:
            for filename in glob.glob(
                    os.path.join(self.filesPath.get(),
                                 self.filesPattern.get())):
                fnSmall = self._getExtraPath(os.path.split(filename)[1])
                copyFile(filename, fnSmall)

        outputSmallMolecules = SetOfSmallMolecules().create(
            path=self._getPath(), suffix='SmallMols')

        for fnSmall in glob.glob(self._getExtraPath("*")):
            smallMolecule = SmallMolecule(smallMolFilename=fnSmall)

            if len(os.listdir(self._getExtraPath())) <= 100:  # costly
                if not fnSmall.endswith('.mae') or not fnSmall.endswith(
                        '.maegz'):
                    fnRoot = os.path.splitext(os.path.split(fnSmall)[1])[0]
                    fnOut = self._getExtraPath("%s.png" % fnRoot)
                    args = Plugin.getPluginHome(
                        'utils/rdkitUtils.py') + " draw %s %s" % (fnSmall,
                                                                  fnOut)
                    try:
                        Plugin.runRDKit(self, "python3", args)
                        smallMolecule._PDBLigandImage = pwobj.String(fnOut)
                    except:
                        smallMolecule._PDBLigandImage = pwobj.String(
                            "Not available")

            outputSmallMolecules.append(smallMolecule)
        self._defineOutputs(outputSmallMols=outputSmallMolecules)
Ejemplo n.º 34
0
 def exportImageStep(self):
     imageBaseFileName = os.path.basename(self.exportPicture.get())
     outputFile = os.path.join(self.dirName, imageBaseFileName)
     copyFile(self.exportPicture.get(), outputFile)
Ejemplo n.º 35
0
    def globalAssignment(self):
        iteration = 1
        fnDirCurrent = self._getExtraPath("Iter%03d" % iteration)
        makePath(fnDirCurrent)

        fnGlobal = join(fnDirCurrent, "globalAssignment")
        makePath(fnGlobal)

        targetResolution = self.significantMaxResolution.get()
        TsCurrent = max(self.TsOrig, targetResolution / 3)
        self.prepareImages(fnGlobal, TsCurrent)
        self.prepareReferences(fnGlobal, TsCurrent, targetResolution)

        # Calculate angular step at this resolution
        ResolutionAlignment = targetResolution
        newXdim = self.readInfoField(fnGlobal, "size", xmipp.MDL_XSIZE)
        angleStep = self.calculateAngStep(newXdim, TsCurrent,
                                          ResolutionAlignment)
        self.writeInfoField(fnGlobal, "angleStep", xmipp.MDL_ANGLE_DIFF,
                            float(angleStep))

        # Significant alignment
        alpha = 1 - 0.01 * self.significantSignificance.get()
        fnDirSignificant = join(fnGlobal, "significant")
        fnImgs = join(fnGlobal, "images.xmd")
        makePath(fnDirSignificant)

        # Create defocus groups
        row = getFirstRow(fnImgs)
        if row.containsLabel(xmipp.MDL_CTF_MODEL) or row.containsLabel(
                xmipp.MDL_CTF_DEFOCUSU):
            self.runJob("xmipp_ctf_group","--ctfdat %s -o %s/ctf:stk --pad 2.0 --sampling_rate %f --phase_flipped  --error 0.1 --resol %f"%\
                        (fnImgs,fnDirSignificant,TsCurrent,targetResolution),numberOfMpi=1)
            moveFile("%s/ctf_images.sel" % fnDirSignificant,
                     "%s/ctf_groups.xmd" % fnDirSignificant)
            cleanPath("%s/ctf_split.doc" % fnDirSignificant)
            md = xmipp.MetaData("numberGroups@%s" %
                                join(fnDirSignificant, "ctfInfo.xmd"))
            fnCTFs = "%s/ctf_ctf.stk" % fnDirSignificant
            numberGroups = md.getValue(xmipp.MDL_COUNT, md.firstObject())
            ctfPresent = True
        else:
            numberGroups = 1
            ctfPresent = False

        # Generate projections
        fnReferenceVol = join(fnGlobal, "volumeRef.vol")
        fnGallery = join(fnDirSignificant, "gallery.stk")
        fnGalleryMd = join(fnDirSignificant, "gallery.xmd")
        args="-i %s -o %s --sampling_rate %f --sym %s --min_tilt_angle %f --max_tilt_angle %f"%\
             (fnReferenceVol,fnGallery,angleStep,self.symmetryGroup,self.angularMinTilt.get(),self.angularMaxTilt.get())
        self.runJob("xmipp_angular_project_library", args)
        cleanPath(join(fnDirSignificant, "gallery_angles.doc"))
        moveFile(join(fnDirSignificant, "gallery.doc"), fnGalleryMd)

        fnAngles = join(fnGlobal, "anglesDisc.xmd")
        for j in range(1, numberGroups + 1):
            fnAnglesGroup = join(fnDirSignificant, "angles_group%02d.xmd" % j)
            if not exists(fnAnglesGroup):
                if ctfPresent:
                    fnGroup = "ctfGroup%06d@%s/ctf_groups.xmd" % (
                        j, fnDirSignificant)
                    fnGalleryGroup = join(fnDirSignificant,
                                          "gallery_group%06d.stk" % j)
                    fnGalleryGroupMd = join(fnDirSignificant,
                                            "gallery_group%06d.xmd" % j)
                    self.runJob("xmipp_transform_filter",
                                "-i %s -o %s --fourier binary_file %d@%s --save_metadata_stack %s --keep_input_columns"%\
                                (fnGalleryMd,fnGalleryGroup,j,fnCTFs,fnGalleryGroupMd))
                else:
                    fnGroup = fnImgs
                    fnGalleryGroupMd = fnGalleryMd
                args='-i %s --initgallery %s --odir %s --sym %s --iter 1 --alpha0 %f --alphaF %f --angularSampling %f --maxShift %d '\
                     '--minTilt %f --maxTilt %f --useImed --angDistance %f --dontReconstruct'%\
                     (fnGroup,fnGalleryGroupMd,fnDirSignificant,self.symmetryGroup,alpha,alpha,angleStep,\
                      round(self.angularMaxShift.get()*newXdim/100),self.angularMinTilt.get(),self.angularMaxTilt.get(),2*angleStep)
                self.runJob('xmipp_reconstruct_significant',
                            args,
                            numberOfMpi=self.numberOfThreads.get())
                moveFile(join(fnDirSignificant, "angles_iter001_00.xmd"),
                         join(fnDirSignificant, "angles_group%02d.xmd" % j))
                self.runJob("rm -f",
                            fnDirSignificant + "/images_*iter00?_*.xmd",
                            numberOfMpi=1)
                if j == 1:
                    copyFile(fnAnglesGroup, fnAngles)
                else:
                    self.runJob("xmipp_metadata_utilities",
                                "-i %s --set union %s" %
                                (fnAngles, fnAnglesGroup),
                                numberOfMpi=1)
        if ctfPresent:
            self.runJob("rm -f", fnDirSignificant + "/gallery*", numberOfMpi=1)
    def ligPrepStep(self):
        progLigPrep = Plugin.getHome('ligprep')
        progStructConvert = Plugin.getHome('utilities/structconvert')

        outputSmallMolecules = SetOfSmallMolecules().create(
            path=self._getPath(), suffix='SmallMols')
        outputSmallMoleculesDropped = SetOfSmallMolecules().create(
            path=self._getPath(), suffix='SmallMolsDropped')

        for mol in self.inputSmallMols.get():
            fnSmall = mol.smallMoleculeFile.get()
            fnMol = os.path.split(fnSmall)[1]
            fnRoot = os.path.splitext(fnMol)[0]

            existingFiles = glob.glob(self._getExtraPath(fnRoot + "*"))
            if len(existingFiles) == 0:
                fnSmallExtra = self._getTmpPath(fnMol)
                copyFile(fnSmall, fnSmallExtra)

                args = '-WAIT -LOCAL'
                if self.ionization.get() != 0:
                    if self.ionization.get() == 1:
                        args += " -epik"
                        if self.emb.get():
                            args += " -epik_metal_binding"
                    else:
                        args += " -i %d" % self.ionization.get() - 2
                    args += " -ph %f -pht %f" % (self.pH.get(),
                                                 self.pHrange.get())

                if self.stereoisomers.get():
                    args += " -g"
                else:
                    args += " -ac -s %d" % self.Niso.get()

                if self.optimization.get() == 1:
                    args += " -bff 14"
                elif self.optimization.get() == 2:
                    args += " -bff 16"

                if fnMol.endswith('.smi'):
                    args += " -ismi tmp/%s" % (fnMol)
                elif fnMol.endswith('.mae') or fnMol.endswith('.maegz'):
                    args += " -imae tmp/%s" % (fnMol)
                elif fnMol.endswith('.sdf'):
                    args += " -isd tmp/%s" % (fnMol)
                else:
                    print("Skipping %s" % fnSmall)
                    continue

                fnMae = "extra/%s.maegz" % fnRoot
                if not os.path.exists(fnMae):
                    args += " -omae %s" % fnMae
                    self.runJob(progLigPrep, args, cwd=self._getPath())

                if os.path.exists(self._getPath(fnMae)):
                    fnOmae = "extra/o%s.maegz" % fnRoot
                    args = "-imae %s -omae %s -split-nstructures 1" % (fnMae,
                                                                       fnOmae)
                    self.runJob(progStructConvert, args, cwd=self._getPath())
                    for fn in glob.glob(
                            self._getExtraPath("o%s*.maegz" % fnRoot)):
                        fnDir, fnOut = os.path.split(fn)
                        fnOut = self._getExtraPath(fnOut[1:])
                        moveFile(fn, fnOut)
                        smallMolecule = SmallMolecule(smallMolFilename=fnOut)
                        outputSmallMolecules.append(smallMolecule)
                    if len(glob.glob(self._getExtraPath(
                            "%s-*.maegz" % fnRoot))) > 0:
                        cleanPath(self._getPath(fnMae))
                else:
                    smallMolecule = SmallMolecule(smallMolFilename=fnSmall)
                    outputSmallMoleculesDropped.append(smallMolecule)
            else:
                for fn in glob.glob(self._getExtraPath("%s*.maegz" % fnRoot)):
                    print("Reading %s" % fn)
                    smallMolecule = SmallMolecule(smallMolFilename=fn)
                    outputSmallMolecules.append(smallMolecule)

        if len(outputSmallMolecules) > 0:
            self._defineOutputs(outputSmallMols=outputSmallMolecules)
            self._defineSourceRelation(self.inputSmallMols,
                                       outputSmallMolecules)
        if len(outputSmallMoleculesDropped) > 0:
            self._defineOutputs(
                outputSmallMolsDropped=outputSmallMoleculesDropped)
            self._defineSourceRelation(self.inputSmallMols,
                                       outputSmallMoleculesDropped)
Ejemplo n.º 37
0
    def evaluateIndividuals(self,iteration):
        fnDir = self._getExtraPath()
        newXdim = self.readInfoField(fnDir,"size",xmippLib.MDL_XSIZE)
        angleStep = max(math.atan2(1,newXdim/2),self.minAngle.get())
        TsOrig=self.inputParticles.get().getSamplingRate()
        TsCurrent = self.readInfoField(fnDir,"sampling",xmippLib.MDL_SAMPLINGRATE)
        fnMask = self._getExtraPath("mask.vol")
        fnImages = join(fnDir,"images.xmd")
        
        maxShift=round(0.1*newXdim)
        R=self.particleRadius.get()
        if R<=0:
            R=self.inputParticles.get().getDimensions()[0]/2
        R=R*TsOrig/TsCurrent

        bestWeightVol={}
        bestIterVol={}
        if iteration>0:
            mdPrevious = xmippLib.MetaData("bestByVolume@"+self._getExtraPath("swarm.xmd"))
            for objId in mdPrevious:
                idx = int(mdPrevious.getValue(xmippLib.MDL_IDX,objId))
                bestWeightVol[idx]=mdPrevious.getValue(xmippLib.MDL_WEIGHT,objId)
                bestIterVol[idx]=mdPrevious.getValue(xmippLib.MDL_ITER,objId)

        # Global alignment
        md=xmippLib.MetaData()
        if iteration>1:
            mdBest = xmippLib.MetaData("best@"+self._getExtraPath("swarm.xmd"))
            objId = mdBest.firstObject()
            bestWeight = mdBest.getValue(xmippLib.MDL_WEIGHT,objId)
        else:
            bestWeight = -1e38
        for i in range(self.inputVolumes.get().getSize()):
            # Filter and mask volumes
            fnVol = self._getExtraPath("volume%03d.vol"%i)
            self.runJob("xmipp_transform_filter","-i %s --fourier low_pass %f --sampling %f"%(fnVol,self.targetResolution,TsCurrent),numberOfMpi=1)
            if exists(fnMask):
                self.runJob("xmipp_image_operate","-i %s --mult %s"%(fnVol,fnMask),numberOfMpi=1)
            
            # Prepare subset of experimental images
            fnTest =  join(fnDir,"imgs_%03d.xmd"%i)
            self.runJob("xmipp_metadata_utilities","-i %s --operate random_subset %d -o %s"%(fnImages,self.NimgTest,fnTest),numberOfMpi=1)
            
            # Generate projections
            fnGallery=join(fnDir,"gallery%02d.stk"%i)
            fnGalleryMd=join(fnDir,"gallery%02d.doc"%i)
#             args="-i %s -o %s --sampling_rate %f --perturb %f --sym %s"%\
#                  (fnVol,fnGallery,angleStep,math.sin(angleStep*math.pi/180.0)/4,self.symmetryGroup)
            args="-i %s -o %s --sampling_rate %f --sym %s"%\
                 (fnVol,fnGallery,angleStep,self.symmetryGroup)
            args+=" --compute_neighbors --angular_distance -1 --experimental_images %s"%fnTest
            self.runJob("xmipp_angular_project_library",args,numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get())
            
            # Assign angles
            args='-i %s --initgallery %s --maxShift %d --odir %s --dontReconstruct --useForValidation 1'%\
                 (fnTest,fnGalleryMd,maxShift,fnDir)
            self.runJob('xmipp_reconstruct_significant',args,numberOfMpi=self.numberOfMpi.get()*self.numberOfThreads.get())
            
            # Evaluate 
            fnAngles = join(fnDir,"angles_iter001_00.xmd")
            if exists(fnAngles):
                # Significant may decide not to write it if it is not significant
                mdAngles = xmippLib.MetaData(fnAngles)
                weight = mdAngles.getColumnValues(xmippLib.MDL_MAXCC)
                avgWeight = reduce(lambda x, y: x + y, weight) / len(weight)
                print("Average weight for "+fnVol+" = "+str(avgWeight))
                objId = md.addObject()
                md.setValue(xmippLib.MDL_IDX,long(i),objId)
                md.setValue(xmippLib.MDL_IMAGE,fnVol,objId)
                md.setValue(xmippLib.MDL_WEIGHT,avgWeight,objId)
                md.setValue(xmippLib.MDL_ITER,iteration,objId)
                
                # Is global best
                if avgWeight>bestWeight:
                    bestWeight = avgWeight
                    if iteration==0:
                        # None of the input volumes can be the best volume in the first iteration since their gray values may significantly
                        # differ from those in the projections
                        self.runJob("xmipp_image_operate","-i %s --mult 0 -o %s"%(fnVol,self._getExtraPath("volumeBest.vol")),numberOfMpi=1)
                    else:
                        copyFile(fnVol, self._getExtraPath("volumeBest.vol"))
                    mdBest=xmippLib.MetaData()
                    objId=mdBest.addObject()
                    mdBest.setValue(xmippLib.MDL_IMAGE,fnVol,objId)
                    mdBest.setValue(xmippLib.MDL_WEIGHT,bestWeight,objId)
                    mdBest.setValue(xmippLib.MDL_ITER,iteration,objId)
                    mdBest.write("best@"+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND)
                
                # Is local best
                if iteration==0:
                    self.runJob("xmipp_image_operate","-i %s --mult 0 -o %s"%(fnVol, self._getExtraPath("volume%03d_best.vol"%i)),numberOfMpi=1)
                elif avgWeight>bestWeightVol[i] or iteration==1:
                    bestWeightVol[i]=avgWeight
                    bestIterVol[i]=iteration
                    copyFile(fnVol,self._getExtraPath("volume%03d_best.vol"%i))

            # Clean
            cleanPath(fnTest)
            self.runJob("rm -f",fnDir+"/*iter00?_00.xmd",numberOfMpi=1)
            self.runJob("rm -f",fnDir+"/gallery*",numberOfMpi=1)
        md.write("evaluations_%03d@"%iteration+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND)
        
        # Update best by volume
        if iteration==0:
            md.write("bestByVolume@"+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND)
        else:
            md.clear()
            for i in range(self.inputVolumes.get().getSize()):
                objId = md.addObject()
                md.setValue(xmippLib.MDL_IDX,long(i),objId)
                md.setValue(xmippLib.MDL_IMAGE,self._getExtraPath("volume%03d_best.vol"%i),objId)
                md.setValue(xmippLib.MDL_WEIGHT,bestWeightVol[i],objId)
                md.setValue(xmippLib.MDL_ITER,bestIterVol[i],objId)
            md.write("bestByVolume@"+self._getExtraPath("swarm.xmd"),xmippLib.MD_APPEND)