quartzbio.query module

class quartzbio.query.BatchQuery(queries, **kwargs)

Bases: object

BatchQuery accepts a list of Query objects and executes them in a single request to /v2/batch_query.

execute(**params)
class quartzbio.query.Filter(*raw_filters, **filters)

Bases: object

Filter objects.

Makes it easier to create filters cumulatively using & (and), | (or) and ~ (not) operations.

For example:

f = Filter()
f &= Filter(price='Free')
f |= Filter(style='Mexican')

creates a filter “price = ‘Free’ or style = ‘Mexican’”.

Each set of kwargs in a Filter are ANDed together:

  • <field>=’<value>’ matches if the field is that exact value

  • <field>__in=[<item1>, …] matches any of the terms <item1> and so on

  • <field>__range=[<start>, <end>] matches anything from <start>

    to <end>

  • <field>__between=[<start>, <end>] matches anything between <start> to

    <end> not include either <start> or <end>

String terms are not analyzed and are always assumed to be exact matches.

Numeric columns can be selected by range using:

  • <field>__gt: greater than

  • <field>__gte: greater than or equal to

  • <field>__lt: less than

  • <field>__lte: less than or equal to

Field action examples:

dataset.query(gene__in=[‘BRCA’, ‘GATA3’],

chr=’3’, start__gt=10000, end__lte=20000)

class quartzbio.query.GenomicFilter(chromosome, start, stop=None, exact=False)

Bases: Filter

Helper class that generates filters on genomic coordinates.

Range filtering only works on “genomic” datasets (where dataset.is_genomic is True).

FIELD_CHR = 'genomic_coordinates.chromosome'
FIELD_START = 'genomic_coordinates.start'
FIELD_STOP = 'genomic_coordinates.stop'
classmethod from_string(string, exact=False)

Handles UCSC-style range queries (chr1:100-200)

class quartzbio.query.Query(dataset_id, query=None, genome_build=None, filters=None, fields=None, exclude_fields=None, entities=None, ordering=None, limit=inf, page_size=100, result_class=<class 'dict'>, target_fields=None, annotator_params=None, debug=False, **kwargs)

Bases: QueryBase

A Query API request wrapper that generates a request from Filter objects, and can iterate through streaming result sets.

annotate(fields, **kwargs)
execute(offset=0, **query)

Executes a query. Additional query parameters can be passed as keyword arguments.

Returns: The request parameters and the raw query response.

export(format='json', follow=True, limit=None, **kwargs)
facets(*args, **kwargs)

Returns a dictionary with the requested facets.

The facets function supports string args, and keyword args.

q.facets(‘field_1’, ‘field_2’) will return facets for field_1 and field_2. q.facets(field_1={‘limit’: 0}, field_2={‘limit’: 10}) will return all facets for field_1 and 10 facets for field_2.

fields()

Returns all expected fields that will be found in the results.

join(query_b, key, key_b=None, prefix='b_', always_prefix=False)

Performs a left outer join between the current query (query A) and another query (query B).

Set prefix to None to use a random prefix. Enable always_prefix to always apply a prefix.

migrate(target, follow=True, **kwargs)

Migrate the data from the Query to a target dataset.

Valid optional kwargs include:

  • target_fields

  • include_errors

  • validation_params

  • metadata

  • commit_mode

position(chromosome, position, exact=False)

Shortcut to do a single position filter on genomic datasets.

range(chromosome, start, stop, exact=False)

Shortcut to do range filters on genomic datasets.

class quartzbio.query.QueryBase

Bases: object

A helper abstract mixin class that contains common methods for Query and QueryFile classes.

DEFAULT_PAGE_SIZE = 100
INF = 1000000000000000
MAX_PAGE_SIZE = 10000
static as_slice(slice_or_idx)
static bounded_slice(_slice)
count()

Returns the total number of results returned by a query. The count is dependent on the filters, but independent of any limit. It is like SQL: SELECT COUNT(*) FROM <table> [WHERE condition]. See also __len__ for a function that is dependent on limit.

filter(*filters, **kwargs)

Returns this Query/QueryFile instance with the query args combined with existing set with AND.

kwargs are simply passed to a new Filter object and combined to any other filters with AND.

By default, everything is combined using AND. If you provide multiple filters in a single filter call, those are ANDed together. If you provide multiple filters in multiple filter calls, those are ANDed together.

If you want something different, use the F class which supports & (and), | (or) and ~ (not) operators. Then call filter once with the resulting Filter instance.

limit(limit)

Returns a new Query/QueryFile instance with the new limit values.

next()

Allows the Query/QueryFile object to be an iterable.

This method will iterate through a cached result set and fetch successive pages as required.

A StopIteration exception will be raised when there aren’t any more results available or when the requested result slice range or limit has been fetched.

Returns: The next result.

class quartzbio.query.QueryFile(file_id, fields=None, exclude_fields=None, filters=None, limit=inf, page_size=1000, output_format='json', header=True, **kwargs)

Bases: QueryBase

A QueryFile API request wrapper that generates a request for an object content query, and can iterate through streaming result sets.

DEFAULT_PAGE_SIZE = 1000
execute(offset=0, **query)

Executes a query. Additional query parameters can be passed as keyword arguments.

Returns: The request parameters and the raw query response.

fields()

Returns all expected fields that will be found in the results.