Example #1
0
    def __init__(self, properties):
        """ Create a new Sequence object.

		Parameters:
			- **properties**: properties of this sequence, as a dictionary.
			  Must contain at least a 'name' and 'sequence' property, or a
			  :class:`MetagenomeDB.errors.InvalidObjectError` exception is thrown.
			  A 'length' property is automatically calculated and would overwrite
			  any such property if provided.

		.. note::
			The 'name' property is unique within a collection, but not across
			the whole database. It means that two sequences with the same name
			can coexist in the database as long as they belong to two different
			collections (or if they are not related to any collection).
		"""
        if (not "name" in properties):
            raise errors.InvalidObjectError("Property 'name' is missing")

        if (not "sequence" in properties):
            raise errors.InvalidObjectError("Property 'sequence' is missing")

        sequence, length = Sequence._process_sequence(properties["sequence"])

        properties["sequence"] = sequence
        properties["length"] = length

        indices = {
            "name": False,
            "length": False,
            "class": False,
        }

        orm.PersistentObject.__init__(self, indices, properties)
Example #2
0
    def _getitem_precallback(self, key, value):
        if (key == ("sequence", )):
            if (type(value) in (str, unicode)):
                return value

            if ("data" in value):
                sequence = zlib.decompress(value["data"])
                if (zlib.crc32(sequence) != value["crc"]):
                    raise errors.InvalidObjectError(
                        "Sequence information has been corrupted.")

                return sequence

            if ("handle" in value):
                pass

            raise errors.InvalidObjectError(
                "Invalid value for 'sequence' property.")
Example #3
0
def adapt(obj):
    """
    Adapt an object to a uniform interface that can be used to easily pass
    to the C/C++ layers of skylark.
    """
    if ELEM_INSTALLED and sys.modules.has_key('El'):
        global El
        import El
        haselem = True
    else:
        haselem = False

    if KDT_INSTALLED and sys.modules.has_key('kdt'):
        global kdt
        import kdt
        haskdt = True
    else:
        haskdt = False

    if isinstance(obj, numpy.ndarray):
        return NumpyAdapter(obj)

    elif isinstance(obj, scipy.sparse.csr_matrix) or isinstance(
            obj, scipy.sparse.csc_matrix):
        return ScipyAdapter(obj)

    elif haselem and isinstance(obj, El.Matrix):
        return ElMatrixAdapter(obj)

    elif haselem and isinstance(obj, El.DistMatrix):
        return DistMatrixAdapter(obj)

    elif haskdt and isinstance(obj, kdt.Mat):
        return KDTAdapter(obj)

    else:
        raise errors.InvalidObjectError(
            "Invalid/unsupported object passed as parameter")
Example #4
0
    def __init__(self, properties):
        """ Create a new Collection object.

		Parameters:
			- **properties**: properties of this sequence, as a dictionary.
			  Must contain at least a 'name' property, or a
			  :class:`MetagenomeDB.errors.InvalidObjectError` exception is thrown.

		.. note::
			  Collection names are unique in the database; if attempting to
			  commit a collection while another collection already exists with
			  the same name a :class:`MetagenomeDB.errors.DuplicateObjectError`
			  exception is thrown.
		"""
        if (not "name" in properties):
            raise errors.InvalidObjectError("Property 'name' is missing")

        indices = {
            "name": True,
            "class": False,
        }

        orm.PersistentObject.__init__(self, indices, properties)
Example #5
0
    def apply(self, A, SA, dim=0):
        """
    Apply the transform on **A** along dimension **dim** and write
    result in **SA**. Note: for rowwise (aka right) sketching **A**
    is mapped to **A S^T**.

    :param A: Input matrix.
    :param SA: Ouptut matrix. If "None" then the output will be allocated.
    :param dim: Dimension to apply along. 0 - columnwise, 1 - rowwise.
                or can use "columnwise"/"rowwise", "left"/"right"
                default is columnwise
    :returns: SA
    """
        if dim == 0 or dim == "columnwise" or dim == "left":
            dim = 0
        if dim == "rowwise" or dim == "right":
            dim = 1
        if dim != 0 and dim != 1:
            raise ValueError(
                "Dimension must be either columnwise/rowwise or left/right or 0/1"
            )

        A = lib.adapt(A)

        # Allocate in case SA is not given, and then adapt it.
        if SA is None:
            if self._defouttype is None:
                ctor = A.getctor()
            else:
                ctor = lib.map_to_ctor[self._defouttype]

            if dim == 0:
                SA = ctor(self._s, A.getdim(1), A)
            if dim == 1:
                SA = ctor(A.getdim(0), self._s, A)
        SA = lib.adapt(SA)

        reqcomb = (self._ttype, A.ctype(), SA.ctype())
        if reqcomb not in SUPPORTED_SKETCH_TRANSFORMS:
            raise errors.UnsupportedError("Unsupported transform-input-output combination: " \
                                            + str(reqcomb))

        incomp, cinvert = A.iscompatible(SA)
        if incomp is not None:
            raise errors.UnsupportedError(
                "Input and output are incompatible: " + incomp)

        if A.getdim(dim) != self._n:
            raise errors.DimensionMistmatchError(
                "Sketched dimension is incorrect (input)")
        if SA.getdim(dim) != self._s:
            raise errors.DimensionMistmatchError(
                "Sketched dimension is incorrect (output)")
        if A.getdim(1 - dim) != SA.getdim(1 - dim):
            raise errors.DimensionMistmatchError(
                "Sketched dimension is incorrect (input != output)")

        if self._ppy:
            self._ppyapply(A.getobj(), SA.getobj(), dim)
        else:
            Aobj = A.ptr()
            SAobj = SA.ptr()
            if (Aobj == -1 or SAobj == -1):
                raise errors.InvalidObjectError(
                    "Invalid/unsupported object passed as A or SA")

            if cinvert:
                cdim = 1 - dim
            else:
                cdim = dim

            lib.callsl("sl_apply_sketch_transform", self._obj, \
                      A.ctype(), Aobj, SA.ctype(), SAobj, cdim+1)

            A.ptrcleaner()
            SA.ptrcleaner()

        return SA.getobj()