Ejemplo n.º 1
0
    def createOutputStep(self):
        tomograms = self.tomograms.get()
        coordinates = self.coordinates.get()

        output = SetOfCoordinates3D.create(self._getPath())
        # output.copyInfo(input)
        output.setPrecedents(tomograms)
        output.setBoxSize(coordinates.getBoxSize())
        output.setSamplingRate(coordinates.getMicrographs().getSamplingRate())

        # Get a dictionary of tomograms by tsId
        tomoDict = dict()
        for tomogram in tomograms:
            tomo = tomogram.clone()
            tomoDict[removeExt(basename(tomo.getFileName()))] = tomo

        # For each 2d coordinate
        for cord2d in coordinates:

            # Extract Z
            micName = coordinates.getMicrographs()[
                cord2d.getMicId()].getFileName()
            z, fileName = sliceAndNameFromMicName(micName)
            newCoord = Coordinate3D()
            newCoord.setVolume(tomoDict[fileName])
            newCoord.setX(cord2d.getX(), BOTTOM_LEFT_CORNER)
            newCoord.setY(cord2d.getY(), BOTTOM_LEFT_CORNER)
            newCoord.setZ(z, BOTTOM_LEFT_CORNER)
            newCoord.setBoxSize(coordinates.getBoxSize())
            newCoord.setGroupId(1)
            output.append(newCoord)

        self._defineOutputs(
            **{Prot2DcoordsTo3DCoordsOutput.outputCoordinates.name: output})
        self._defineSourceRelation(self.coordinates, output)
Ejemplo n.º 2
0
def readCoordinate3D(box, inputTomo, origin=const.BOTTOM_LEFT_CORNER, scale=1):
    from tomo.objects import Coordinate3D
    x, y, z = scale * numpy.asarray(box[:3])
    coord = Coordinate3D()
    coord.setVolume(inputTomo)
    coord.setPosition(x, y, z, origin)
    return coord
Ejemplo n.º 3
0
def _relionTomoStar2Subtomograms(prot, outputSubTomogramsSet, tomoTable, invert):
    ih = ImageHandler()
    samplingRate = outputSubTomogramsSet.getSamplingRate()
    for row, inSubtomo in zip(tomoTable, prot.inputSubtomos.get()):
        subtomo = SubTomogram()
        coordinate3d = Coordinate3D()
        transform = Transform()
        origin = Transform()

        volname = row.get(TOMO_NAME, FILE_NOT_FOUND)
        subtomoFn = row.get(SUBTOMO_NAME, FILE_NOT_FOUND)

        subtomo.setVolName(managePath4Sqlite(volname))
        subtomo.setTransform(transform)
        subtomo.setAcquisition(TomoAcquisition())
        subtomo.setClassId(row.get('rlnClassNumber', 0))
        subtomo.setSamplingRate(samplingRate)
        subtomo.setCoordinate3D(coordinate3d)  # Needed to get later the tomogram pointer via getCoordinate3D()

        x = row.get(COORD_X, 0)
        y = row.get(COORD_Y, 0)
        z = row.get(COORD_Z, 0)
        tiltPrior = row.get(TILT_PRIOR, 0)
        psiPrior = row.get(PSI_PRIOR, 0)
        # ctf3d = row.get(CTF_MISSING_WEDGE, FILE_NOT_FOUND)
        coordinate3d = subtomo.getCoordinate3D()
        coordinate3d.setX(float(x), BOTTOM_LEFT_CORNER)
        coordinate3d.setY(float(y), BOTTOM_LEFT_CORNER)
        coordinate3d.setZ(float(z), BOTTOM_LEFT_CORNER)
        coordinate3d._3dcftMrcFile = inSubtomo.getCoordinate3D()._3dcftMrcFile  # Used for the ctf3d in Relion 3.0 (tomo)
        M = _getTransformMatrix(row, invert)
        transform.setMatrix(M)
        subtomo.setCoordinate3D(coordinate3d)
        subtomo._tiltPriorAngle = Float(tiltPrior)
        subtomo._psiPriorAngle = Float(psiPrior)

        # Set the origin and the dimensions of the current subtomogram
        x, y, z, n = ih.getDimensions(subtomoFn)
        zDim, _ = manageIhDims(subtomoFn, z, n)
        origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate)
        subtomo.setOrigin(origin)

        subtomo.setFileName(managePath4Sqlite(subtomoFn))
        # if subtomo is in a vesicle
        if 'tid_' in subtomoFn:
            vesicleId = subtomoFn.split('tid_')[1]
            vesicleId = vesicleId[0]
            scoor = subtomo.getCoordinate3D()
            scoor.setGroupId(vesicleId)
            subtomo.setCoordinate3D(scoor)

        # Add current subtomogram to the output set
        outputSubTomogramsSet.append(subtomo)
Ejemplo n.º 4
0
    def importCoordinates3D(self, fileName, addCoordinate):
        from tomo.objects import Coordinate3D
        if pwutils.exists(fileName):
            ext = pwutils.getExt(fileName)

        if ext == ".txt":
            md = MetaData()
            md.readPlain(fileName, "xcoor ycoor zcoor")
            for objId in md:
                x = md.getValue(MDL_XCOOR, objId)
                y = md.getValue(MDL_YCOOR, objId)
                z = md.getValue(MDL_ZCOOR, objId)
                coord = Coordinate3D()
                addCoordinate(coord, x, y, z)

        else:
            raise Exception(
                'Unknown extension "%s" to import Eman coordinates' % ext)
Ejemplo n.º 5
0
    def importCoordinates3D(self, fileName, addCoordinate):
        from tomo.objects import Coordinate3D
        if pwutils.exists(fileName):
            ext = pwutils.getExt(fileName)

            if ext == ".json":
                jsonPosDict = loadJson(fileName)
                boxes = []

                if "boxes_3d" in jsonPosDict:
                    boxes = jsonPosDict["boxes_3d"]
                if boxes:
                    for box in boxes:
                        x, y, z = box[:3]
                        coord = Coordinate3D()
                        addCoordinate(coord, x, y, z)

            else:
                raise Exception(
                    'Unknown extension "%s" to import Eman coordinates' % ext)
Ejemplo n.º 6
0
def json2Coordinates3D(jsonFile, coordinates):
    """ Loads a json file containing picking 3D coordinates and populates a
    SetOfCoordinates"""
    # NOTE: What is the box size?

    # Format is like this:
    # [{"peak": {"loc": [149, 12, 15]}}, ....]
    print("File to parse: %s" % jsonFile)

    # read file
    with open(jsonFile, 'r') as myfile:
        data = myfile.read()

    # load json file
    peaks = json.loads(data)

    for item in peaks:
        x, y, z = item['peak']['loc']
        coord = Coordinate3D()
        coord.setPosition(x, y, z)
        coordinates.append(coord)
Ejemplo n.º 7
0
def _pysegStar2Coords3D(prot, output3DCoordSet, tomoTable, invert):
    for tomoNum, tomo in enumerate(prot.tomoSet.iterItems()):
        tomoName = tomo.getFileName().replace(':mrc', '')
        for row in tomoTable:
            # Create the set of coordinates referring each of them to its corresponding tomogram (ancestor)
            if row.get(TOMO_NAME) == tomoName:
                coordinate3d = Coordinate3D()
                coordinate3d.setVolId(tomoNum)
                coordinate3d.setVolume(tomo)
                x = row.get(COORD_X, 0)
                y = row.get(COORD_Y, 0)
                z = row.get(COORD_Z, 0)
                M = _getTransformMatrix(row, invert)
                coordinate3d.setX(float(x), BOTTOM_LEFT_CORNER)
                coordinate3d.setY(float(y), BOTTOM_LEFT_CORNER)
                coordinate3d.setZ(float(z), BOTTOM_LEFT_CORNER)
                coordinate3d.setMatrix(M)
                coordinate3d.setGroupId(_getVesicleIdFromSubtomoName(row.get(SUBTOMO_NAME, FILE_NOT_FOUND)))

                # Add current subtomogram to the output set
                output3DCoordSet.append(coordinate3d)
    def createOutputStep(self):
        # Convert DeepFinder annotation output to Scipion SetOfCoordinates3D
        setTomograms = self.inputTomograms.get()

        # First, determine the classes that have been annotated (check in all object lists):
        objl = []
        for tomo in setTomograms.iterItems():
            # Get objl filename:
            fname_tomo = os.path.splitext(tomo.getFileName())
            fname_tomo = os.path.basename(fname_tomo[0])
            fname_objl = 'objl_annot_' + fname_tomo + '.xml'

            # Read objl:
            objl_tomo = cv.objl_read(os.path.abspath(os.path.join(self._getExtraPath(), fname_objl)))
            objl.extend(objl_tomo)

        lbl_list = cv.objl_get_labels(objl)  # get unique class labels
        self.objl = objl  # store as class attribute for _summary

        # Test summary string:
        lbl_list = cv.objl_get_labels(self.objl)
        print(str(len(lbl_list)) + ' classes have been annotated.')
        for lbl in lbl_list:
            objl_class = cv.objl_get_class(self.objl, lbl)
            print('Class ' + str(lbl) + ': ' + str(len(objl_class)) + ' objects')

        # For each class, iterate over all object lists (1 per tomo) and store coordinates
        # in SetOfCoordinates3D (1 per class)
        # Remark: only 1 setOfCoordinates3D can exist at a time, else:
        # "Protocol failed: Cannot operate on a closed database."
        for lbl in lbl_list:
            coord3DSet = self._createSetOfCoordinates3D(setTomograms, lbl) # lbl is a suffix for sqlite filename. Important, else overwrite!
            coord3DSet.setName('Class '+str(lbl))
            coord3DSet.setPrecedents(setTomograms)
            coord3DSet.setSamplingRate(setTomograms.getSamplingRate())

            for tomo in setTomograms.iterItems():
                # Get objl filename:
                fname_tomo = os.path.splitext(tomo.getFileName())
                fname_tomo = os.path.basename(fname_tomo[0])
                fname_objl = 'objl_annot_' + fname_tomo + '.xml'

                # Read objl:
                objl_tomo = cv.objl_read(os.path.abspath(os.path.join(self._getExtraPath(), fname_objl)))
                objl_class = cv.objl_get_class(objl_tomo, lbl)
                for idx in range(len(objl_class)):
                    x = objl_class[idx]['x']
                    y = objl_class[idx]['y']
                    z = objl_class[idx]['z']

                    coord = Coordinate3D()
                    coord.setPosition(x, y, z)
                    coord.setVolume(tomo)
                    coord.setVolName(tomo.getFileName())
                    coord3DSet.append(coord)

            # Link to output:
            name = 'outputCoordinates3Dclass'+str(lbl)
            args = {}
            coord3DSet.setStreamState(Set.STREAM_OPEN)
            args[name] = coord3DSet

            self._defineOutputs(**args)
            self._defineSourceRelation(setTomograms, coord3DSet)

        # I (emoebel) don't know what this is for, but is apparently necessary (copied from Estrella's XmippProtCCroi)
        for outputset in self._iterOutputsNew():
            outputset[1].setStreamState(Set.STREAM_CLOSED)
        self._store()
Ejemplo n.º 9
0
 def __init__(self, **kwargs):
     Coordinate3D.__init__(self, **kwargs)
     self._score = pwobj.Scalar(kwargs.get('score', None))
    def createPhantomsStep(self):
        mwfilter = self.mwfilter.get()
        rotmin = self.rotmin.get()
        rotmax = self.rotmax.get()
        tiltmin = self.tiltmin.get()
        tiltmax = self.tiltmax.get()
        psimin = self.psimin.get()
        psimax = self.psimax.get()

        fnVol = self._getExtraPath("phantom000.vol")
        if self.option.get() == 0:
            inputVol = self.inputVolume.get()
            fnInVol = inputVol.getLocation()[1]
            dim = inputVol.getDim()
            if mwfilter:
                mwangle = self.mwangle.get()
                self.runJob(
                    "xmipp_transform_filter",
                    " --fourier wedge -%d %d 0 0 0 -i %s -o %s" %
                    (mwangle, mwangle, fnInVol, fnVol))
            else:
                self.runJob("xmipp_image_convert",
                            " -i %s -o %s" % (fnInVol, fnVol))
        else:
            desc = self.create.get()
            fnDescr = self._getExtraPath("phantom.descr")
            fhDescr = open(fnDescr, 'w')
            fhDescr.write(desc)
            fhDescr.close()
            dim = [desc.split()[0], desc.split()[1], desc.split()[2]]
            self.runJob("xmipp_phantom_create",
                        " -i %s -o %s" % (fnDescr, fnVol))
            if mwfilter:
                mwangle = self.mwangle.get()
                self.runJob(
                    "xmipp_transform_filter",
                    " --fourier wedge -%d %d 0 0 0 -i %s -o %s" %
                    (mwangle, mwangle, fnVol, fnVol))

        self.outputSet = self._createSetOfSubTomograms(
            self._getOutputSuffix(SetOfSubTomograms))
        self.outputSet.setDim(dim)
        self.outputSet.setSamplingRate(self.sampling.get())

        coordsBool = self.coords.get()
        if coordsBool:
            tomos = self.tomos.get()
            tomo = tomos.getFirstItem()
            tomoDim = tomo.getDim()
            self.coords = self._createSetOfCoordinates3D(tomos)

        subtomobase = SubTomogram()
        acq = TomoAcquisition()
        subtomobase.setAcquisition(acq)
        subtomobase.setLocation(fnVol)
        subtomobase.setSamplingRate(self.sampling.get())
        transformBase = Transform()
        transformBase.setMatrix(np.identity(4))
        subtomobase.setTransform(transformBase)
        if coordsBool:
            coor = Coordinate3D()
            coor.setVolume(tomo)
            coor.setX(np.random.randint(0, tomoDim[0]),
                      const.BOTTOM_LEFT_CORNER)
            coor.setY(np.random.randint(0, tomoDim[1]),
                      const.BOTTOM_LEFT_CORNER)
            coor.setZ(np.random.randint(0, tomoDim[2]),
                      const.BOTTOM_LEFT_CORNER)
            subtomobase.setCoordinate3D(coor)
            subtomobase.setVolName(tomo.getFileName())
            self.coords.append(coor)
            self.coords.setBoxSize(subtomobase.getDim()[0])
        self.outputSet.append(subtomobase)

        for i in range(int(self.nsubtomos.get()) - 1):
            fnPhantomi = self._getExtraPath("phantom%03d.vol" % int(i + 1))
            rot = np.random.randint(rotmin, rotmax)
            tilt = np.random.randint(tiltmin, tiltmax)
            psi = np.random.randint(psimin, psimax)
            self.runJob(
                "xmipp_transform_geometry",
                " -i %s -o %s --rotate_volume euler %d %d %d " %
                (fnVol, fnPhantomi, rot, tilt, psi))

            if mwfilter:
                self.runJob(
                    "xmipp_transform_filter",
                    " --fourier wedge -%d %d 0 0 0 -i %s -o %s" %
                    (mwangle, mwangle, fnPhantomi, fnPhantomi))

            subtomo = SubTomogram()
            subtomo.setAcquisition(acq)
            subtomo.setLocation(fnPhantomi)
            subtomo.setSamplingRate(self.sampling.get())
            A = euler_matrix(np.deg2rad(psi), np.deg2rad(tilt),
                             np.deg2rad(rot), 'szyz')
            transform = Transform()
            transform.setMatrix(A)
            subtomo.setTransform(transform)
            if coordsBool:
                coor = Coordinate3D()
                coor.setVolume(tomo)
                coor.setX(np.random.randint(0, tomoDim[0]),
                          const.BOTTOM_LEFT_CORNER)
                coor.setY(np.random.randint(0, tomoDim[1]),
                          const.BOTTOM_LEFT_CORNER)
                coor.setZ(np.random.randint(0, tomoDim[2]),
                          const.BOTTOM_LEFT_CORNER)
                subtomo.setCoordinate3D(coor)
                subtomo.setVolName(tomo.getFileName())
                self.coords.append(coor)
            self.outputSet.append(subtomo)
        if coordsBool:
            self.outputSet.setCoordinates3D(self.coords)