Esempio n. 1
0
    def importVolumesStep(self, pattern, samplingRate, setOrigCoord=False):
        """ Copy images matching the filename pattern
        Register other parameters.
        """
        self.info("Using pattern: '%s'" % pattern)

        # Create a Volume template object
        vol = Volume()
        vol.setSamplingRate(samplingRate)

        imgh = ImageHandler()

        volSet = self._createSetOfVolumes()
        volSet.setSamplingRate(samplingRate)

        for fileName, fileId in self.iterFiles():
            x, y, z, n = imgh.getDimensions(fileName)
            if fileName.endswith('.mrc') or fileName.endswith('.map'):
                fileName += ':mrc'
                if (z == 1 and n != 1):
                    zDim = n
                    n = 1
                else:
                    zDim = z
            else:
                zDim = z
            origin = Transform()
            if setOrigCoord:
                origin.setShiftsTuple(self._getOrigCoord())
            else:
                origin.setShifts(x / -2. * samplingRate,
                                 y / -2. * samplingRate,
                                 zDim / -2. * samplingRate)

            vol.setOrigin(origin)  # read origin from form

            if self.copyFiles or setOrigCoord:
                newFileName = abspath(self._getVolumeFileName(fileName, "mrc"))
                Ccp4Header.fixFile(fileName, newFileName, origin.getShifts(),
                                   samplingRate, Ccp4Header.ORIGIN)
            else:
                newFileName = abspath(self._getVolumeFileName(fileName))

                if fileName.endswith(':mrc'):
                    fileName = fileName[:-4]
                createAbsLink(fileName, newFileName)

            # Make newFileName relative
            # https://github.com/I2PC/scipion/issues/1935
            newFileName = relpath(newFileName)
            if n == 1:
                vol.cleanObjId()
                vol.setFileName(newFileName)
                volSet.append(vol)
            else:
                for index in range(1, n + 1):
                    vol.cleanObjId()
                    vol.setLocation(index, newFileName)
                    volSet.append(vol)

        if volSet.getSize() > 1:
            self._defineOutputs(outputVolumes=volSet)
        else:
            self._defineOutputs(outputVolume=vol)
    def extractunitCell(self, sym, offset=0, cropZ=False):
        """ extract unit cell from icosahedral phantom
            using xmipp_i2 symmetry
        """
        # create phantom (3D map)
        _samplingRate = 1.34
        _, outputFile1 = mkstemp(suffix=".mrc")
        command = "xmipp_phantom_create "
        args = " -i %s" % self.filename[sym]
        args += " -o %s" % outputFile1
        runJob(None, command, args, env=Plugin.getEnviron())
        ccp4header = Ccp4Header(outputFile1, readHeader=True)
        x, y, z = ccp4header.getDims()
        t = Transform()

        if cropZ:
            _, outputFile2 = mkstemp(suffix=".mrc")
            args = "-i %s -o %s" % (outputFile1, outputFile2)
            args += " --corners "
            args += " %d " % (-x / 2.)
            args += " %d " % (-y / 2.)
            args += " %d " % (0.)
            args += " %d " % (+x / 2.)
            args += " %d " % (+y / 2.)
            args += " %d " % (+z / 2.)
            runJob(None,
                   "xmipp_transform_window",
                   args,
                   env=Plugin.getEnviron())
            t.setShifts(0, 0, 0)
            outputFile = outputFile2
            ccp4header = Ccp4Header(outputFile2, readHeader=True)

        else:
            t.setShifts(0, 0, 0)
            outputFile = outputFile1

        ccp4header.setSampling(_samplingRate)
        ccp4header.setOrigin(t.getShifts())
        ccp4header.writeHeader()

        # import volume
        if cropZ:
            args = {
                'filesPath': outputFile,
                'filesPattern': '',
                'samplingRate': _samplingRate,
                'copyFiles': True,
                'setOrigCoord': True,
                'x': 90. * _samplingRate,
                'y': 90. * _samplingRate,
                'z': 0.
                # x, y, z in Angstroms
            }
        else:
            args = {
                'filesPath': outputFile,
                'filesPattern': '',
                'samplingRate': _samplingRate,
                'copyFiles': True,
                'setDefaultOrigin': False,
            }
        prot = self.newProtocol(ProtImportVolumes, **args)
        prot.setObjLabel('import volume(%s)' % XMIPP_SYM_NAME[sym])
        self.launchProtocol(prot)
        # execute protocol extract unitCell
        args = {
            'inputVolumes': prot.outputVolume,
            'symmetryGroup': sym,
            'symmetryOrder': self.symOrder,
            'innerRadius': self.innerRadius,
            'outerRadius': self.outerRadius,
            'expandFactor': .2,
            'offset': offset
        }
        prot = self.newProtocol(XmippProtExtractUnit, **args)
        prot.setObjLabel('extract unit cell')
        self.launchProtocol(prot)

        # check results
        ih = ImageHandler()
        xdim, ydim, zdim, ndim = \
            ih.getDimensions(prot.outputVolume.getFileName())
        self.assertTrue(abs(xdim - self.box[sym][0]) < 2)
        self.assertTrue(abs(ydim - self.box[sym][1]) < 2)
        self.assertTrue(abs(zdim - self.box[sym][2]) < 2)

        # create pdb fileoutput
        args = {
            'inputStructure': prot.outputVolume,
            'maskMode': NMA_MASK_THRE,
            'maskThreshold': 0.5,
            'pseudoAtomRadius': 1.5
        }
        prot = self.newProtocol(XmippProtConvertToPseudoAtoms, **args)
        prot.setObjLabel('get pdb')
        self.launchProtocol(prot)

        # check results
        filenamePdb = prot._getPath('pseudoatoms.pdb')
        self.assertTrue(os.path.isfile(filenamePdb))
        # delete temporary files
        os.remove(self.filename[sym])
        os.remove(outputFile)