Ejemplo n.º 1
0
def alpha_group(body,
                alpha_metric,
                summary_statistics=True,
                percentiles=None,
                return_raw=False):
    if not (summary_statistics or return_raw):
        # swagger does not account for parameter dependencies, so we should
        #  give a bad request error here
        return jsonify(error=400,
                       text='Either `summary_statistics`, `return_raw`, '
                       'or both are required to be true.'), 400

    sample_ids = body['sample_ids']

    alpha_repo = AlphaRepo()

    # figure out if the user asked for a metric we have data on
    available_metrics = alpha_repo.available_metrics()
    type_ = 'metric'
    missing_metric = validate_resource(available_metrics, alpha_metric, type_)
    if missing_metric:
        return missing_metric

    # make sure all of the data the samples the user asked for have values
    # for the given metric
    missing_ids = [
        id_ for id_ in sample_ids if not alpha_repo.exists(id_, alpha_metric)
    ]
    missing_ids_msg = check_missing_ids(missing_ids, alpha_metric, type_)
    if missing_ids_msg:
        return missing_ids_msg

    # retrieve the alpha diversity for each sample
    alpha_series = alpha_repo.get_alpha_diversity(
        sample_ids,
        alpha_metric,
    )
    alpha_ = Alpha(alpha_series, percentiles=percentiles)
    alpha_data = dict()

    if return_raw:
        # not using name right now, so give it a placeholder name
        alpha_values = alpha_.get_group_raw(name='').to_dict()
        del alpha_values['name']
        alpha_data.update(alpha_values)
    if summary_statistics:
        # not using name right now, so give it a placeholder name
        alpha_summary = alpha_.get_group(name='').to_dict()
        del alpha_summary['name']
        alpha_data.update({'alpha_metric': alpha_summary.pop('alpha_metric')})
        alpha_data.update({'group_summary': alpha_summary})

    response = jsonify(alpha_data)
    return response, 200
Ejemplo n.º 2
0
def get_alpha(sample_id, alpha_metric):
    alpha_repo = AlphaRepo()
    if not all(alpha_repo.exists([sample_id], alpha_metric)):
        return jsonify(error=404, text="Sample ID not found."), \
               404
    alpha_series = alpha_repo.get_alpha_diversity([sample_id], alpha_metric)
    alpha_ = Alpha(alpha_series)
    alpha_data = alpha_.get_group_raw().to_dict()
    ret_val = {
        'sample_id': sample_id,
        'alpha_metric': alpha_data['alpha_metric'],
        'data': alpha_data['alpha_diversity'][sample_id],
    }

    return jsonify(ret_val), 200
Ejemplo n.º 3
0
def _exists(alpha_repo, alpha_metric, samples):
    # figure out if the user asked for a metric we have data on
    available_metrics = alpha_repo.available_metrics()
    type_ = 'metric'
    validate_resource_alt(available_metrics, alpha_metric, type_)

    return jsonify(alpha_repo.exists(samples, alpha_metric)), 200
Ejemplo n.º 4
0
def get_alpha_alt(dataset, sample_id, alpha_metric):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)

    alpha_repo = AlphaRepo(alpha_resource.data)
    alpha_value = _get_alpha(alpha_repo, alpha_metric, sample_id)

    return jsonify(alpha_value), 200
Ejemplo n.º 5
0
def available_metrics_alpha():
    alpha_repo = AlphaRepo()
    ret_val = {
        'alpha_metrics': alpha_repo.available_metrics(),
    }

    return jsonify(ret_val), 200
Ejemplo n.º 6
0
def _plot_pcoa(beta_metric, fillna, metadata_categories, named_sample_set,
               metadata_repo, pcoa_repo):
    if not pcoa_repo.has_pcoa(named_sample_set, beta_metric):
        raise UnknownResource(f"No PCoA for named_sample_set="
                              f"'{named_sample_set}',beta_metric="
                              f"'{beta_metric}'")
    # check metadata repo for the requested categories
    has_category = metadata_repo.has_category(metadata_categories)
    missing_categories = [
        cat for i, cat in enumerate(metadata_categories) if not has_category[i]
    ]
    if len(missing_categories) > 0:
        raise UnknownResource(f"Missing specified metadata categories: "
                              f"{missing_categories}")
    pcoa = pcoa_repo.get_pcoa(named_sample_set, beta_metric)
    # grab the sample ids from the PCoA
    samples = pcoa.samples.index
    # metadata for samples not in the repo will be filled in as None
    metadata = metadata_repo.get_metadata(
        metadata_categories,
        sample_ids=samples,
        fillna=fillna,
    )
    response = dict()
    response['decomposition'] = {
        "coordinates":
        pcoa.samples.values.tolist(),
        "percents_explained":
        list(100 * prop for prop in pcoa.proportion_explained),
        "sample_ids":
        list(samples),
    }
    response["metadata"] = metadata.values.tolist()
    response["metadata_headers"] = metadata.columns.tolist()
    return jsonify(response), 200
Ejemplo n.º 7
0
def _taxonomy_counts(resource, taxonomy_repo, level, sample_ids):
    if sample_ids == []:
        sample_ids = None

    taxonomy_ = taxonomy_repo.model(resource)

    counts = taxonomy_.get_counts(level, sample_ids)
    response = jsonify(counts)
    return response
Ejemplo n.º 8
0
def _exists(resource, samples, taxonomy_repo):
    available_resources = taxonomy_repo.resources()

    type_ = 'resource'
    missing_resource = validate_resource(available_resources, resource, type_)
    if missing_resource:
        return missing_resource

    return jsonify(taxonomy_repo.exists(samples, resource)), 200
Ejemplo n.º 9
0
def k_nearest(dataset, beta_metric, k, sample_id):
    beta_resource = _validate_dataset_neighbors(dataset,
                                                resource_getter=get_resources)
    neigh_repo = NeighborsRepo(beta_resource.data)
    k_nearest_ids = neigh_repo.k_nearest(
        sample_id=sample_id,
        metric=beta_metric,
        k=k,
    )
    return jsonify(k_nearest_ids), 200
Ejemplo n.º 10
0
def _present_microbes_taxonomy_table(sample_ids, resource, taxonomy_repo):
    error_response = _check_resource_and_missing_ids(taxonomy_repo, sample_ids,
                                                     resource)
    if error_response:
        return error_response

    taxonomy_ = taxonomy_repo.model(resource)
    taxonomy_table = taxonomy_.presence_data_table(sample_ids)
    response = jsonify(taxonomy_table.to_dict())
    return response, 200
Ejemplo n.º 11
0
def _summarize_group(sample_ids, table_name, taxonomy_repo):

    error_response = _check_resource_and_missing_ids(taxonomy_repo, sample_ids,
                                                     table_name)
    if error_response:
        return error_response
    taxonomy_ = taxonomy_repo.model(table_name)

    taxonomy_data = taxonomy_.get_group(sample_ids, '').to_dict()
    del taxonomy_data['name']
    response = jsonify(taxonomy_data)
    return response, 200
Ejemplo n.º 12
0
def alpha_group(body,
                alpha_metric,
                summary_statistics=True,
                percentiles=None,
                return_raw=False):
    alpha_repo = AlphaRepo()
    alpha_data = _alpha_group(body, alpha_repo, _metadata_repo_getter,
                              alpha_metric, percentiles, return_raw,
                              summary_statistics)

    response = jsonify(alpha_data)
    return response, 200
Ejemplo n.º 13
0
def alpha_group_alt(body,
                    dataset,
                    alpha_metric,
                    summary_statistics=True,
                    percentiles=None,
                    return_raw=False):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    getter = partial(_metadata_repo_getter_alt, dataset=dataset)
    alpha_data = _alpha_group(body, alpha_repo, getter, alpha_metric,
                              percentiles, return_raw, summary_statistics)

    return jsonify(alpha_data), 200
Ejemplo n.º 14
0
def ranks_sample(dataset, resource, sample_size):
    taxonomy_repo = _get_taxonomy_repo(dataset)

    error_response = _check_resource_and_missing_ids(taxonomy_repo, [],
                                                     resource)
    if error_response:
        return error_response

    taxonomy_ = taxonomy_repo.model(resource)
    summary = taxonomy_.ranks_sample(sample_size)
    order = taxonomy_.ranks_order(summary['Taxon'])

    payload = summary.to_dict('list')
    payload.pop('Sample ID')
    payload['Taxa-order'] = order

    return jsonify(payload), 200
Ejemplo n.º 15
0
def available_metrics_alpha():
    alpha_repo = AlphaRepo()
    ret_val = _available_metrics(alpha_repo)

    return jsonify(ret_val), 200
Ejemplo n.º 16
0
def get_alpha(sample_id, alpha_metric):
    alpha_repo = AlphaRepo()
    ret_val = _get_alpha(alpha_repo, alpha_metric, sample_id)

    return jsonify(ret_val), 200
Ejemplo n.º 17
0
def resources():
    taxonomy_repo = TaxonomyRepo()
    ret_val = {
        'resources': taxonomy_repo.resources(),
    }
    return jsonify(ret_val), 200
Ejemplo n.º 18
0
def resources_alt(dataset):
    taxonomy_repo = _get_taxonomy_repo(dataset)
    ret_val = {
        'resources': taxonomy_repo.resources(),
    }
    return jsonify(ret_val), 200
Ejemplo n.º 19
0
def available_metrics_alpha_alt(dataset):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    return jsonify(_available_metrics(alpha_repo)), 200