Exemplo n.º 1
0
def create_data_link_params(
        params: Dict[str, Any]) -> Tuple[DataUnitID, SampleNodeAddress, bool]:
    '''
    Given a dict, extract the parameters to create parameters for creating a data link.

    Expected keys:
    id - sample id
    version - sample version
    node - sample node
    upa - workspace object UPA
    dataid - ID of the data within the workspace object
    update - whether the link should be updated

    :param params: the parameters.
    :returns: a tuple consisting of:
        1) The data unit ID that is the target of the link,
        2) The sample node that is the target of the link,
        3) A boolean that indicates whether the link should be updated if it already exists.
    :raises MissingParameterError: if any of the required arguments are missing.
    :raises IllegalParameterError: if any of the arguments are illegal.
    '''
    _check_params(params)
    sna = SampleNodeAddress(
        _SampleAddress(
            _cast(UUID, get_id_from_object(params, ID, required=True)),
            _cast(int, get_version_from_object(params, required=True))),
        _cast(str, _check_string_int(params, 'node', True)))
    duid = get_data_unit_id_from_object(params)
    return (duid, sna, bool(params.get('update')))
Exemplo n.º 2
0
    def get_sample_via_data(self, ctx, params):
        """
        Get a sample via a workspace object. Read permissions to a workspace object grants
        read permissions to all versions of any linked samples, whether the links are expired or
        not. This method allows for fetching samples when the user does not have explicit
        read access to the sample.
        :param params: instance of type "GetSampleViaDataParams"
           (get_sample_via_data parameters. upa - the workspace UPA of the
           target object. id - the target sample id. version - the target
           sample version.) -> structure: parameter "upa" of type "ws_upa" (A
           KBase Workspace service Unique Permanent Address (UPA). E.g. 5/6/7
           where 5 is the workspace ID, 6 the object ID, and 7 the object
           version.), parameter "id" of type "sample_id" (A Sample ID. Must
           be globally unique. Always assigned by the Sample service.),
           parameter "version" of type "version" (The version of a sample.
           Always > 0.)
        :returns: instance of type "Sample" (A Sample, consisting of a tree
           of subsamples and replicates. id - the ID of the sample. user -
           the user that saved the sample. node_tree - the tree(s) of sample
           nodes in the sample. The the roots of all trees must be
           BioReplicate nodes. All the BioReplicate nodes must be at the
           start of the list, and all child nodes must occur after their
           parents in the list. name - the name of the sample. Must be less
           than 255 characters. save_date - the date the sample version was
           saved. version - the version of the sample.) -> structure:
           parameter "id" of type "sample_id" (A Sample ID. Must be globally
           unique. Always assigned by the Sample service.), parameter "user"
           of type "user" (A user's username.), parameter "node_tree" of list
           of type "SampleNode" (A node in a sample tree. id - the ID of the
           node. parent - the id of the parent node for the current node.
           BioReplicate nodes, and only BioReplicate nodes, do not have a
           parent. type - the type of the node. meta_controlled - metadata
           restricted by the sample controlled vocabulary and validators.
           meta_user - unrestricted metadata.) -> structure: parameter "id"
           of type "node_id" (A SampleNode ID. Must be unique within a Sample
           and be less than 255 characters.), parameter "parent" of type
           "node_id" (A SampleNode ID. Must be unique within a Sample and be
           less than 255 characters.), parameter "type" of type
           "samplenode_type" (The type of a sample node. One of: BioReplicate
           - a biological replicate. Always at the top of the sample tree.
           TechReplicate - a technical replicate. SubSample - a sub sample
           that is not a technical replicate.), parameter "meta_controlled"
           of type "metadata" (Metadata attached to a sample. The
           UnspecifiedObject map values MUST be a primitive type - either
           int, float, string, or equivalent typedefs.) -> mapping from type
           "metadata_key" (A key in a metadata key/value pair. Less than 1000
           unicode characters.) to mapping from type "metadata_value_key" (A
           key for a value associated with a piece of metadata. Less than
           1000 unicode characters. Examples: units, value, species) to
           unspecified object, parameter "meta_user" of type "metadata"
           (Metadata attached to a sample. The UnspecifiedObject map values
           MUST be a primitive type - either int, float, string, or
           equivalent typedefs.) -> mapping from type "metadata_key" (A key
           in a metadata key/value pair. Less than 1000 unicode characters.)
           to mapping from type "metadata_value_key" (A key for a value
           associated with a piece of metadata. Less than 1000 unicode
           characters. Examples: units, value, species) to unspecified
           object, parameter "name" of type "sample_name" (A sample name.
           Must be less than 255 characters.), parameter "save_date" of type
           "timestamp" (A timestamp in epoch milliseconds.), parameter
           "version" of type "version" (The version of a sample. Always > 0.)
        """
        # ctx is the context object
        # return variables are: sample
        #BEGIN get_sample_via_data
        upa = _get_upa_from_object(params)
        sid, ver = _get_sample_address_from_object(params, version_required=True)
        sample = self._samples.get_sample_via_data(
            _UserID(ctx[_CTX_USER]), upa, _SampleAddress(sid, ver))
        sample = _sample_to_dict(sample)
        #END get_sample_via_data

        # At some point might do deeper type checking...
        if not isinstance(sample, dict):
            raise ValueError('Method get_sample_via_data return value ' +
                             'sample is not type dict as required.')
        # return the results
        return [sample]
Exemplo n.º 3
0
    def get_data_links_from_sample(self, ctx, params):
        """
        Get data links to Workspace objects originating from a sample.
                The user must have read permissions to the sample. Only Workspace objects the user
                can read are returned.
        :param params: instance of type "GetDataLinksFromSampleParams"
           (get_data_links_from_sample parameters. id - the sample ID.
           version - the sample version. effective_time - the effective time
           at which the query should be run - the default is the current
           time. Providing a time allows for reproducibility of previous
           results. as_admin - run the method as a service administrator. The
           user must have read administration permissions.) -> structure:
           parameter "id" of type "sample_id" (A Sample ID. Must be globally
           unique. Always assigned by the Sample service.), parameter
           "version" of type "version" (The version of a sample. Always >
           0.), parameter "effective_time" of type "timestamp" (A timestamp
           in epoch milliseconds.), parameter "as_admin" of type "boolean" (A
           boolean value, 0 for false, 1 for true.)
        :returns: instance of type "GetDataLinksFromSampleResults"
           (get_data_links_from_sample results. links - the links.
           effective_time - the time at which the query was run. This
           timestamp, if saved, can be used when running the method again to
           ensure reproducible results. Note that changes to workspace
           permissions may cause results to change over time.) -> structure:
           parameter "links" of list of type "DataLink" (A data link from a
           KBase workspace object to a sample. upa - the workspace UPA of the
           linked object. dataid - the dataid of the linked data, if any,
           within the object. If omitted the entire object is linked to the
           sample. id - the sample id. version - the sample version. node -
           the sample node. createdby - the user that created the link.
           created - the time the link was created. expiredby - the user that
           expired the link, if any. expired - the time the link was expired,
           if at all.) -> structure: parameter "upa" of type "ws_upa" (A
           KBase Workspace service Unique Permanent Address (UPA). E.g. 5/6/7
           where 5 is the workspace ID, 6 the object ID, and 7 the object
           version.), parameter "dataid" of type "data_id" (An id for a unit
           of data within a KBase Workspace object. A single object may
           contain many data units. A dataid is expected to be unique within
           a single object. Must be less than 255 characters.), parameter
           "id" of type "sample_id" (A Sample ID. Must be globally unique.
           Always assigned by the Sample service.), parameter "version" of
           type "version" (The version of a sample. Always > 0.), parameter
           "node" of type "node_id" (A SampleNode ID. Must be unique within a
           Sample and be less than 255 characters.), parameter "createdby" of
           type "user" (A user's username.), parameter "created" of type
           "timestamp" (A timestamp in epoch milliseconds.), parameter
           "expiredby" of type "user" (A user's username.), parameter
           "expired" of type "timestamp" (A timestamp in epoch
           milliseconds.), parameter "effective_time" of type "timestamp" (A
           timestamp in epoch milliseconds.)
        """
        # ctx is the context object
        # return variables are: results
        #BEGIN get_data_links_from_sample
        sid, ver = _get_sample_address_from_object(params, version_required=True)
        dt = _get_datetime_from_epochmillseconds_in_object(params, 'effective_time')
        admin = _check_admin(
            self._user_lookup, ctx[_CTX_TOKEN], _AdminPermission.READ,
            # pretty annoying to test ctx.log_info is working, do it manually
            'get_data_links_from_sample', ctx.log_info, skip_check=not params.get('as_admin'))
        links, ts = self._samples.get_links_from_sample(
            _UserID(ctx[_CTX_USER]), _SampleAddress(sid, ver), dt, as_admin=admin)
        results = {'links': _links_to_dicts(links),
                   'effective_time': _datetime_to_epochmilliseconds(ts)
                   }
        #END get_data_links_from_sample

        # At some point might do deeper type checking...
        if not isinstance(results, dict):
            raise ValueError('Method get_data_links_from_sample return value ' +
                             'results is not type dict as required.')
        # return the results
        return [results]