phenomedb.query_factory
Creating queries can be done either via the Query Factory view or the QueryFactory Python API. In PhenomeDB Queries are created by chaining QueryFilter objects containing boolean operators and QueryMatches, which specifying the fields and comparison operators and values. An overview of this can be seen below. With the collection of QueryFilters and QueryMatches, the QueryFactory then calculates/transpiles the query definition into an SQLAlchemy query, and executes the query. The QueryFactory can then construct a combined-format and 3-file format dataset of the results, and store them in the PhenomeDB Cache, an extended version of Redis that enables file-system persistency of objects. Generating the dataframes can currently take a long time depending on the number of records the query returns, for this reason once the query has been defined the user should run the CreateSavedQueryDataframeCache task to execute the query and set it into the cache. This can be run manually via the interface or via the QueryFactory UI.
The QueryFilter and QueryMatch architecture. Multiple QueryFilters can be added, each with AND or OR boolean operators. Each QueryFilter can have multiple QueryMatches, targeting a specific Model.property, with a specific comparison operator and value.
An example of using these to construct a query is shown below.
# Instantiate the QueryFactory
query_factory = QueryFactory(query_name='Users under 40', query_description='test description')
# Add a filter with the match properties added in the constructor (default 'AND')
query_factory.add_filter(model='Project', property='name', operator='eq', value='My Project')
# Create another filter with the match properties added in the constructor
filter = QueryFilter(model='HarmonisedMetadataField',property='name',operator='eq', value='Age')
# Add another match to the filter
filter.add_match(model='MetadataValue',property='harmonised numeric value',operator='lt', value=40)
# Add the filter to the query factory (default 'AND')
query_factory.add_filter(query_filter=filter)
#4. Save the query in the SavedQuery data model
query_factory.save_query()
#5. Generate the summary statistics
query_factory.calculate_summary_statistics()
#6. Execute the query, build the 3-file format, load into cache, and return dataframes
intensity_data = query_factory.load_dataframe('intensity_data',harmonise_annotations=True)
sample_metadata = query_factory.load_dataframe('sample_metadata',harmonise_annotations=True)
feature_metadata = query_factory.load_dataframe('feature_metadata',harmonise_annotations=True)
To simplify querying HarmonisedMetadataFields, the following MetadataFilter can be used
# Instantiate the QueryFactory
query factory = QueryFactory(query_name='Users under 40', query_description='test description')
# Add a filter with the match properties added in the constructor (default 'AND')
query factory.add_filter(QueryFilter(model='Project',property='name',operator='eq',value='My Project'))
# Add a Metadata filter with the match properties added in the constructor (default 'AND')
query factory.add_filter(MetadataFilter('Age','lt',value=40))
#4. Save the query in the SavedQuery data model
query factory.save_query()
#5. Generate the summary statistics
query_factory.calculate_summary_statistics()
#6. Execute the query, build the 3-file format, load into cache, and return dataframes
intensity_data = query_factory.load_dataframe('intensity_data',harmonise_annotations=True)
sample_metadata = query_factory.load_dataframe('sample_metadata',harmonise_annotations=True)
feature_metadata = query_factory.load_dataframe('feature_metadata',harmonise_annotations=True)
SavedQueries can be created and explored using the QueryFactory user interface. Through the interface the summary statistics for the query can be visually explored to assess the composition of the generated cohort. Once you are happy with the composition you should then execute the CreateSavedQueryDataframeCache task for the SavedQuery to build the query dataframes and store them in the Cache.
Creating a SavedQuery using the UI.
The QueryFactory summary stats output.
Buttons to trigger a CreateSavedQueryDataframe task via the QueryFactory UI
QueryFactory options for downloading a dataframe. Options include the ability to bin harmonised metadata fields, include or exclude specific columns, and specify the output format.
Query dict structure:
query_dict = { 'model': str
'joins':[str,str],
'filters': [{ 'filter_operator': str,
'matches': [{ 'model': str,
'property': str,
"operator": str,
'value': str, float, list
}]
}]
}
query_dict[‘model’]: The model object to return query_dict[‘joins’]: The list of models to join query_dict[‘filters’] : The list of ‘filters’. query_dict[‘filters’][‘filter_operator]: The type of filter - ‘AND’, ‘OR’ query_dict[‘filters’][‘matches’]: A list of ‘matches’ query_dict[‘filters’][‘matches’][‘model’]: The name of the model to match against. query_dict[‘filters’][‘matches’][‘property’]: The name of the property to match against. query_dict[‘filters’][‘matches’][‘operator’]: The operator to use - ‘eq’,’not_eq’,’gt’,’lt’,’gte’,’lte’,’between’,’not_between’,’in’,’not_in’,’like’,’not_like’,’ilike’,’not_ilike’ query_dict[‘filters’][‘matches’][‘value’]: The value to match on. Must be of expected type - ie str, float, [0,1] (for between) or [0,N] (for in or in)
Example usage:
Creating and saving new query with the following structure:
query_dict = { 'model': 'Sample',
'joins':['Subject','Project','MetadataValue','MetadataField','HarmonisedMetadataField'],
'filters': [{
'filter_operator': 'OR',
'matches': [{
'model': 'Project',
'property':'name',
'operator': 'eq',
'value': 'PipelineTesting',
}]
},{
'filter_operator': 'AND',
'matches': [{
'model': 'HarmonisedMetadataField',
'property': 'name',
"operator": 'eq',
'value': 'Age',
},
{
'model': 'MetadataValue',
'property': 'harmonised_numeric_value',
"operator": 'in',
'value': ["30","35","40"]
}]
}]
}
query_factory = CohortQuery()
filter = QueryFilter(filter_operator='AND')
filter.add_match(model='Project',property='name',operator='eq',value='PipelineTesting')
query_factory.add_filter(query_filter=filter)
filter = QueryFilter(filter_operator='AND')
filter.add_match(model='HarmonisedMetadataField',property='name',operator='eq',value='Age')
filter.add_match(model='MetadataValue',property='harmonised_numeric_value',operator='in',value=[30,40,50])
query_factory.add_filter(query_filter=filter)
query = query_factory.save()
query_retrieved_from_db = db_session.query(CohortQuery).filter(CohortQuery.id==query.id).first()
query_factory_retrieved = CohortQuery(query=query_retrieved_from_db)
rows = query_factory_retrieved.get_query_rows()
Example queries and query_dicts:
All Samples from PipelineTesting project
query = db_session.query(Sample).join(Subject,Project).filter(Project.name=='PipelineTesting')
query_dict = { 'model': 'Sample',
'joins':['Subject','Project'],
'filters': [{
'filter_operator': 'AND',
'matches': [{
'model': 'Project',
'property': 'name',
"operator": 'eq',
'value': 'PipelineTesting',
}]
}]
}
All from every project NOT PipelineTesting
query = db_session.query(Sample).join(Subject,Project).filter(Project.name != 'PipelineTesting')
query_dict = { 'model': 'Sample',
'joins':['Subject','Project'],
'filters': [{
'filter_operator': 'AND',
'matches': [{
'model': 'Project',
'property': 'name',
"operator": 'not_eq',
'value': 'PipelineTesting',
}]
}]
}
All “Plasma” samples
query = db_session.query(Sample).filter(Sample.sample_type=='Plasma')
query_dict = { 'model': 'Sample',
'joins':[],
'filters': [{
'filter_operator': 'AND',
'matches': [{
'model': 'Sample',
'property': 'sample_type',
"operator": 'eq',
'value': 'Plasma',
}]
}]
}
All “HPOS” samples
query = db_session.query(Sample).join(SampleAssay,Assay).filter(Assay.name=='HPOS')
query_dict = { 'model': 'Sample',
'joins':['SampleAssay','Assay'],
'filters': [{
'filter_operator': 'AND',
'matches': [{
'model': 'Assay',
'property': 'name',
"operator": 'eq',
'value': 'HPOS',
}]
}]
}
All samples with subjects between ages 30 and 40
query = db_session.query(Sample).join(MetadataValue,MetadataField,HarmonisedMetadataField)\
.filter(HarmonisedMetadataField.name=='Age')\
.filter(MetadataValue.harmonised_numeric_value.between(30,40))
query_dict = { 'model': 'Sample',
'joins':['MetadataValue','MetadataField','HarmonisedMetadataField'],
'filters': [{
'filter_operator': 'AND',
'matches': [{
'model': 'HarmonisedMetadataField',
'property': 'name',
"operator": 'eq',
'value': 'Age',
}]
},
{
'filter_operator': 'AND',
'matches': [{
'model': 'MetadataValue',
'property': 'harmonised_numeric_value',
"operator": 'between',
'value': [30,40]
}]
}]
}
All samples with ‘Glucose’ annotations, with intensity > 0.3
query = db_session.query(Sample)\
.join(SampleAssay,AnnotatedFeature,Annotation,AnnotationCompound,Compound)\
.filter(Compound.name=='Glucose')\
.filter(AnnotatedFeature.intensity > 0.3)
query_dict = { 'model': 'Sample',
'joins':['SampleAssay','AnnotatedFeature','Annotation','AnnotationCompound','Compound'],
'filters': [{ 'filter_operator': 'AND',
'matches': [{
'model': 'Compound',
'property': 'name',
"operator": 'eq',
'value': 'Glucose',
}]
},
{ 'filter_operator': 'AND',
'matches': [{
'model': 'AnnotatedFeature',
'property': 'intensity',
"operator": 'gt',
'value': 0.3
}]
},
]}
}
All samples with Pubchem CID 16217534 annotations, with intensity > 0.3
query = db_session.query(Sample)\
.join(SampleAssay,AnnotatedFeature,Annotation,AnnotationCompound,Compound,CompoundExternalDB,ExternalDB)\
.filter(ExternalDB.name=='PubChem CID')\
.filter(CompoundExternalDB.database_ref=='16217534')\
.filter(AnnotatedFeature.intensity > 0.3)
query_dict = { 'model': 'Sample',
'joins':['SampleAssay','AnnotatedFeature','Annotation','AnnotationCompound','Compound'
'CompoundExternalDB','ExternalDB'],
'filters': [{ 'filter_operator': 'AND',
'matches': [{
'model': 'ExternalDB',
'property': 'name',
"operator": 'eq',
'value': 'PubChem CID',
}]
},
{ 'filter_operator': 'AND',
'matches': [{
'model': 'CompoundExternalDB',
'property': 'database_ref',
"operator": 'eq',
'value': '16217534',
}]
},
{ 'filter_operator': 'AND',
'matches': [{
'model': 'AnnotatedFeature',
'property': 'intensity',
"operator": 'gt',
'value': 0.3,
}]
},
]}
}
All samples from PipelineTesting project OR nPYc-toolbox-tutorials
query = db_session.query(Sample).join(Subject,Project).filter(Project.name=='PipelineTesting' | Project.name=='nPYc-toolbox-tutorials')
query_dict = { 'model': 'Sample',
'joins':['Subject','Project'],
'filters': [{
'filter_operator': 'OR',
'matches': [{
'model': 'Project',
'property': 'name',
"operator": 'eq',
'value': 'PipelineTesting',
},
{
'model': 'Project',
'property': 'name',
"operator": 'eq',
'value': 'nPYc-toolbox-tutorials',
}]
}]
}
All from PipelineTesting project OR nPYc-toolbox-tutorials and with annotations with Pubchem CID ref IN (‘16217534’, ‘3082637’)
query = db_session.query(Sample).join(Subject,Project,SampleAssay,AnnotatedFeature,\
Annotation,AnnotationCompound,Compound,CompoundExternalDB,ExternalDB)\
.filter(Project.name=='PipelineTesting' | Project.name=='nPYc-toolbox-tutorials')\
.filter(ExternalDB.name=='PubChem CID' & CompoundExternalDB.database_ref.in_(['16217534','3082637'])
query_dict = { 'model': 'Sample',
'joins':['Subject','Project','SampleAssay','Annotation','AnnotatedFeature',\
'AnnotationCompound','Compound','CompoundExternalDB','ExternalDB'],
'filters': [{
'filter_operator': 'OR',
'matches': [{
'model': 'Project',
'property':'name',
"operator": 'eq',
'value': 'PipelineTesting',
},
{
'model': 'Project',
'property': 'name',
"operator": 'eq',
'value': 'nPYc-toolbox-tutorials'
}]
},{
'filter_operator': 'AND',
'matches': [{
'model': 'ExternalDB'
'property': 'name',
"operator": 'eq',
'value': 'PubChem CID',
},
{
'model': 'CompoundExternalDB'
'property': 'database_ref',
"operator": 'in',
'value': ['16217534','3082637']
}]
}]
}
All annotations that a not PubChem CID (‘16217534’, ‘3082637’)
query = db_session.query(Sample).join(SampleAssay, Annotation,AnnotatedFeature,\
AnnotationCompound,Compound,CompoundExternalDB,ExternalDB)\
.filter(ExternalDB.name=='PubChem CID' & ~CompoundExternalDB.database_ref.in_(['16217534','3082637'])
query_dict = {
'model': 'Sample',
'joins':['SampleAssay','Annotation','AnnotatedFeature',\
'AnnotationCompound','Compound','CompoundExternalDB','ExternalDB'],
'filters': [{
'matches': [{
'model': 'ExternalDB'
'property': 'name',
"operator": 'eq',
'value': 'PubChem CID',
},
{
'model': 'CompoundExternalDB'
'property': 'database_ref',
"operator": 'not_in',
'value': ['16217534','3082637']
}]
}]
}
- class phenomedb.query_factory.MetadataFilter(harmonised_metadata_field_name, operator=None, value=None)
MetadataFilter class. Creates a sub_filter with 2 matches.
- Parameters:
harmonised_metadata_field_name (str) – The name of the harmonised_metadata_field.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
- class phenomedb.query_factory.ProjectRoleFilter(role_id)
The Role ID to add the project filter to.
- Parameters:
role_id (int) – The Role ID to filter projects by.
- class phenomedb.query_factory.QueryFactory(saved_query=None, saved_query_id=None, filters=None, query_dict=None, db_env=None, db_session=None, project_short_label=None, query_name=None, query_description=None, username=None, role_id=None, output_model='SampleAssay')
Class for building, executing, and saving SQLAlchemy queries that define ‘SavedQueries’, and generating and caching result set dataframes, such as the nPYc format.
A QueryFactory is simply a collection of filters that match on table properties.
Generates an SQLAlchemy query object that allows for further filters to be added as required, or .all() or .count() methods to be used.
A join route calculator helps build the query by knowing the route between filter tables and the output model.
- Parameters:
saved_query (
phenomedb.models.SavedQuery, optional) – Aphenomedb.models.SavedQueryobject, defaults to Nonesaved_query_id (int, optional) – A
phenomedb.models.SavedQueryID, defaults to Nonefilters (dict, optional) – A dictionary of specifying the query filters, defaults to None
query_dict (dict, optional) – A dictionary specifying an entire query definition, defaults to None
db_env (str, optional) – The db to use ‘PROD’ or ‘TEST’, defaults to None
db_session (object, optional) – The db session to use, defaults to None
project_short_label (str, optional) – A short label to add to a query for easier visualisation, defaults to None
query_name (str, optional) – The name of the query, defaults to None
query_description (_type_, optional) – The description of the query, defaults to None
username (str, optional) – The username of the user who created the query, defaults to None
output_model (str, optional) – The output model to use, defaults to ‘SampleAssay’, but typically might be ‘AnnotatedFeature’
- Raises:
Exception – _description_
- add_filter(filter_operator='AND', match_dicts=[], filter_dict=None, query_filter=None, model=None, property=None, operator=None, value=None)
Add a filter to the query_dict. Either generate a new one with this method or pass in a filter_dict or QueryFilter()
- Parameters:
filter_operator (str, optional) – The top-level filter operator, “AND” or “OR”, defaults to “AND”.
match_dicts (list, optional) – List of match dictionaries, defaults to [].
filter_dict (dict, optional) – The filter dictionary to use, defaults to None.
query_filter (
phenomedb.query_factory.QueryFilter, optional) – The query filter object to use, defaults to None.model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
- build_comparison_operation(match)
Builds a comparison operation.
- Parameters:
match (dict) – The match dictionary.
- build_filter_string(filter)
Builds the filter string based on a query_dict filter
- Parameters:
filter (dict) – The filter to build.
- build_function_operation(match)
Builds a function operation.
- Parameters:
match (dict) – The match dictionary.
- build_intensity_data_sample_metadata_and_feature_metadata(output_dir=None, exclude_features_with_na_feature_values=False, columns_to_exclude=None, columns_to_include=None, harmonise_annotations=False, output_model='AnnotatedFeature', class_type=None, class_level=None, aggregate_function=None, correction_type=None, save_cache=True, compound_fields_to_include=None, compound_class_types_to_include=None, convert_units=True, master_unit='mmol/L')
Takes a combined dataframe and build the 3-file format
- Parameters:
output_dir (_type_, optional) – _description_, defaults to None
exclude_features_with_na_feature_values (bool, optional) – _description_, defaults to False
columns_to_exclude (_type_, optional) – _description_, defaults to None
columns_to_include (_type_, optional) – _description_, defaults to None
harmonise_annotations (bool, optional) – _description_, defaults to False
output_model (str, optional) – _description_, defaults to ‘AnnotatedFeature’
class_type (_type_, optional) – _description_, defaults to None
class_level (_type_, optional) – _description_, defaults to None
aggregate_function (_type_, optional) – _description_, defaults to None
correction_type (_type_, optional) – _description_, defaults to None
save_cache (bool, optional) – _description_, defaults to True
compound_fields_to_include (_type_, optional) – _description_, defaults to None
compound_class_types_to_include (_type_, optional) – _description_, defaults to None
convert_units (bool, optional) – _description_, defaults to True
master_unit (str, optional) – _description_, defaults to ‘mmol/L’
- Raises:
Exception – _description_
Exception – _description_
Exception – _description_
- build_logical_operator(operator)
Builds a logical operator.
- Parameters:
operator (str) – The operator to use (AND or OR).
- Raises:
Exception – If the filter type is unknown.
- build_match_string(match)
Build the match string.
- Parameters:
match (dict) – The match dictionary.
- build_match_value(match)
Builds the match value.
- Parameters:
match (dict) – The match dictionary.
- build_model_property_name(match)
Builds the model property name.
- Parameters:
match (dict) – The match dictionary.
- build_query_string(output_model='SampleAssay', harmonise_annotations=True)
Translates the query_dict into an SQLAlchemy query object string
- build_sub_filter_string(sub_filter)
Builds the sub filter string based on the query_dict subfilter.
- Parameters:
sub_filter (dict) – The subfilter to build.
- calculate_joins(output_model='SampleAssay')
Calculates the joins necessary to execute the query
Uses prior information of the join routes from the main model to the match model
- Parameters:
output_model (str, optional) – The output model to calculate for, defaults to ‘SampleAssay’, but could be ‘AnnotatedFeature’
- calculate_summary_statistics()
Calculate the summary statistics of the query.
- Returns:
summary: dictionary of summary statistics.
- Return type:
dict
- delete_cache()
Delete any saved cache entries for this query
- execute_and_build_dataframe(annotations_only=False, csv_path=None, sort_by=None, annotation_version='latest', output_model='SampleAssay', class_level=None, class_type=None, aggregate_function='mean', sort_by_ascending=(True, True), convert_units=True, master_unit='mmol/L', method=None, correction_type=None, zero_lloq=True, inf_uloq=True, harmonise_annotations=False)
Builds dataframe with following structure.
project_name, sample_name, unique_name, metadata::age, h_metadata::age, cpd::HPOS-PPR::Glucose::mmol/L, m::HPOS::rt:37:mz:12.13::mmol/L npc-devset, sample_1, npc-devset-sample_1, 20, 20, 0.1
metadata::age -> MetadataField called “age” h_metadata::Age -> HarmonisedMetadataField called “Age” cpd::HPOS-PPR:Glucose -> HPOS-PPR AnnotationCompound called “Glucose”
Converts units by default to mmol/L
- Returns:
- execute_query(type='all', limit=None, offset=None)
Execute the query.
- Parameters:
type (str, optional) – What kind of query, ‘all’, ‘first’, or ‘count’, defaults to “all”.
limit (int, optional) – The query limit, defaults to None.
offset (int, optional) – The query offset, defaults to None.
- Raises:
Exception – type not recognised.
Exception – no query object to execute.
- generate_and_execute_query(output_model='SampleAssay', type='all', limit=None, offset=None)
Generate and execute the query.
- Parameters:
type (str, optional) – What kind of query, ‘all’, ‘first’, or ‘count’, defaults to “all”.
limit (int, optional) – The query limit, defaults to None.
offset (int, optional) – The query offset, defaults to None.
- Returns:
the query results.
- Return type:
int,
phenomedb.models.*, or list
- generate_query(output_model='SampleAssay', harmonise_annotations=True)
Generate the query. Builds the query code string and evaluates it.
- Raises:
Exception – If the query is invalid.
- get_code_string()
Gets the SQLAlchemy code string
- Returns:
The SQLAlchemy code string
- Return type:
str
- get_dataframe_key(type, model, class_type=None, class_level=None, correction_type=None, aggregate_function=None, annotation_version=None, db_env=None, scaling=None, harmonise_annotations=False, transform=None, sample_orientation=None, sample_label=None, feature_orientation=None, feature_label=None, metadata_bin_definition=None, convert_units=True, master_unit='mmol/L')
Get the dataframe cache key
- Parameters:
type (str) – The file type, for example ‘combined’,’intensity_data’,’sample_metadata’, ‘feature_metadata’,’feature_id_matrix’,’feature_id_combined_dataframe’, ‘metaboanalyst_data’,’metaboanalyst_metadata’
model (str) – The output model, for example ‘AnnotatedFeature’
class_type (str, optional) – The class type (for CompoundClass aggregations), defaults to None
class_level (str, optional) – The class level to aggregate by (for CompoundClass aggregations), defaults to None
correction_type (str, optional) – The correction type to use. Typically you want to specificy None for NMR data and ‘SR’ for LC-MS data, defaults to None
aggregate_function (str, optional) – The aggregration function to use, mean, sum etc (for CompoundClass aggregations), defaults to None
annotation_version (str, optional) – The verison fo the annotation to use, will use latest if not specified, defaults to None
db_env (str, optional) – The DB env, ‘PROD’ or ‘TEST’, defaults to None
harmonise_annotations (bool, optional) – Whether to harmonise the annotations or not, defaults to False
convert_units (bool, optional) – Whether to convert units, defaults to True
master_unit (str, optional) – The master unit to convert to (for absolute quantified data), defaults to ‘mmol/L’
- Raises:
Exception – _description_
Exception – _description_
- Returns:
_description_
- Return type:
_type_
- get_query_rows()
Get the query result rows.
- Returns:
The query results.
- Return type:
int,
phenomedb.models.*, or list
- get_unique_match_models()
Get all unique models and map the joins between these models and the main model. Add the
- Returns:
- load_dataframe(type='combined', combined_csv_path=None, convert_units=True, master_unit='mmol/L', reload_cache=False, correction_type=None, annotation_version=None, output_model='AnnotatedFeature', output_dir=None, harmonise_annotations=False, class_level=None, class_type=None, zero_lloq=True, inf_uloq=True, aggregate_function=None, save_cache=True, sample_label=None, feature_label=None)
The main access point for queries. Checks the cache first, if it doesn’t exist, executes the query, builds the dataframes and stores them in the cache.
- Parameters:
type (str, optional) – _description_, defaults to ‘combined’
combined_csv_path (_type_, optional) – _description_, defaults to None
convert_units (bool, optional) – _description_, defaults to True
master_unit (str, optional) – _description_, defaults to ‘mmol/L’
reload_cache (bool, optional) – _description_, defaults to False
correction_type (_type_, optional) – _description_, defaults to None
annotation_version (_type_, optional) – _description_, defaults to None
output_model (str, optional) – _description_, defaults to ‘AnnotatedFeature’
output_dir (_type_, optional) – _description_, defaults to None
harmonise_annotations (bool, optional) – _description_, defaults to False
class_level (_type_, optional) – _description_, defaults to None
class_type (_type_, optional) – _description_, defaults to None
zero_lloq (bool, optional) – _description_, defaults to True
inf_uloq (bool, optional) – _description_, defaults to True
aggregate_function (_type_, optional) – _description_, defaults to None
save_cache (bool, optional) – _description_, defaults to True
sample_label (_type_, optional) – _description_, defaults to None
feature_label (_type_, optional) – _description_, defaults to None
- Returns:
_description_
- Return type:
_type_
- save_query(type='custom')
Save the query definition.
- Parameters:
type (str, optional) – The query definition type, defaults to ‘custom’.
- Returns:
The SavedQuery.
- Return type:
- set_db_session(db_session=None, db_env=None)
Set the db session
- Parameters:
db_session (
sqlalchemy.orm.Session) – The db_session to use, default None.db_env (str) – The db to use, “PROD”, “BETA”, or “TEST, default None (“PROD”)
- class phenomedb.query_factory.QueryFilter(filter_operator='AND', filter_dict=None, model=None, property=None, operator=None, value=None)
QueryFilter class. Class for storing filter objects to simplify generating queries.
- Example usage:
AND filter: filter = QueryFilter(“AND”) sub_filter = QuerySubFilter(“AND”) subfilter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”PipelineTesting”) cohort_query = CohortQuery() cohort_query.add_filter(filter)
OR filter: filter = QueryFilter(‘OR’) sub_filter = QuerySubFilter(“AND”) filter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”PipelineTesting”) filter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”nPYc-toolbox-tutorials”) cohort_query = CohortQuery() cohort_query.add_filter(filter)
simple AND: filter = QueryFilter(“AND”,model=”Project”,property=”name”,operator=”eq”,value=”PipelineTesting”)
- Parameters:
filter_operator (str, optional) – The top-level filter operator, “AND” or “OR”, defaults to “AND”.
filter_dict (dict, optional) – The filter dictionary to use, defaults to None.
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
- add_match(query_sub_filter_match=None, model=None, property=None, operator=None, value=None, match_dict=None)
Add a match to the Filter.
- Parameters:
query_sub_filter_match (dict, optional) – The QuerySubFilter + QueryMatch to add, defaults to None.
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
match_dict (dict, optional) – The match dictionary to add, defaults to None
- add_sub_filter(sub_filter=None, sub_filter_dict=None, sub_filter_operator='AND', model=None, property=None, operator=None, value=None)
Add a subfilter to the filter. Either QuerySubFilter, or sub_filter_dict, or match properties.
- Parameters:
sub_filter (
phenomedb.query_factory.QuerySubFilter, optional) – The QuerySubFilter to add, defaults to None.sub_filter_dict (dict, optional) – The sub_filter dictionary to add, defaults to None.
sub_filter_operator (str, optional) – The sub_filter operator, ‘AND’ or ‘OR’, defaults to ‘AND’.
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
- get_filter_dict()
Get the filter dictionary corresponding to this filter.
- Returns:
The filter dictionary.
- Return type:
dict
- class phenomedb.query_factory.QueryMatch(model=None, property=None, operator=None, value=None, match_dict=None)
QueryMatch class. Class for building Match object to simplify generating queries.
- Parameters:
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
match_dict (dict, optional) – The match dictionary to add, defaults to None
- Raises:
Exception – If not all match properties are set.
- get_match_dict()
Get the match_dict from the QueryMatch.
- Returns:
the match_dictionary.
- Return type:
dict
- class phenomedb.query_factory.QuerySubFilter(sub_filter_operator='AND', sub_filter_dict=None, model=None, property=None, operator=None, value=None)
QuerySubFilter class. Class for storing subfilter objects to simplify generating nested and/or queries.
- Example usage:
- AND filter:
filter = QueryFilter(filter_operator=”AND”) sub_filter = QuerySubFilter(sub_filter_operator=”AND”) sub_filter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”PipelineTesting”) filter.add_sub_filter(add_sub_filter) cohort_factory = SampleFactory() cohort_factory.add_filter(filter)
- OR filter:
filter = QueryFilter(filter_operator=’OR’) sub_filter = QuerySubFilter(sub_filter_operator=”AND”) sub_filter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”PipelineTesting”) sub_filter.add_match(model=”Project”,property=”name”,operator=”eq”,value=”nPYc-toolbox-tutorials”) filter.add_sub_filter(sub_filter) cohort_factory = SampleFactory() cohort_factory.add_filter(filter)
- Parameters:
sub_filter_operator (str, optional) – The sub_filter operator, ‘AND’ or ‘OR’, defaults to ‘AND’.
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
- add_match(query_sub_filter_match=None, model=None, property=None, operator=None, value=None, match_dict=None)
Add a match to the QuerySubFilter.
- Parameters:
query_sub_filter_match (dict, optional) – The QuerySubFilter + QueryMatch to add, defaults to None.
model (str, optional) – The model to match on, defaults to None.
property (str, optional) – The property to match on, defaults to None.
operator (str, optional) – The operator to use, defaults to None.
value (str, int, float, or list, optional) – The value to match on, defaults to None.
match_dict (dict, optional) – The match dictionary to add, defaults to None
- get_sub_filter_dict()
Get the sub_filter_dict from the QuerySubFilter.
- Returns:
sub_filter_dictionary
- Return type:
dict