[docs]classSqlSchema2Variants(QueryVariantsBase):"""Base class for Schema2 SQL like variants' query interface."""RUNNER_CLASS:type[QueryRunner]def__init__(self,dialect:Dialect,db:str|None,*,family_variant_table:str|None,summary_allele_table:str|None,pedigree_table:str,meta_table:str,gene_models:GeneModels|None=None,):assertpedigree_tableself.dialect=dialectself.db=dblogger.debug("workging with db: %s",db)self.family_variant_table=family_variant_tableself.summary_allele_table=summary_allele_tableself.has_variants=self.summary_allele_tableisnotNoneself.pedigree_table=pedigree_tableself.meta_table=meta_tableself.gene_models=gene_modelsself.pedigree_schema=self._fetch_schema(self.pedigree_table)ped_df=self._fetch_pedigree()families=FamiliesLoader\
.build_families_data_from_pedigree(ped_df)super().__init__(families)self.partition_descriptor=PartitionDescriptor.parse_string(self._fetch_tblproperties())self.combined_columns={}self.summary_allele_schema={}self.family_variant_schema={}self.serializer=Noneifself.has_variants:self.summary_allele_schema=self._fetch_summary_schema()self.family_variant_schema=self._fetch_family_schema()self.combined_columns={**self.family_variant_schema,**self.summary_allele_schema,}variants_data_schema=self._fetch_variants_data_schema()self.serializer=VariantsDataSerializer.build_serializer(variants_data_schema)def_fetch_summary_schema(self)->dict[str,str]:assertself.summary_allele_tableisnotNonereturnself._fetch_schema(self.summary_allele_table)def_fetch_family_schema(self)->dict[str,str]:assertself.family_variant_tableisnotNonereturnself._fetch_schema(self.family_variant_table)@abc.abstractmethoddef_fetch_schema(self,table:str)->dict[str,str]:"""Fetch schema of a table and return it as a Dict."""@abc.abstractmethoddef_fetch_variants_data_schema(self)->dict[str,Any]|None:"""Fetch variants data schema from metadata table."""@abc.abstractmethoddef_fetch_pedigree(self)->pd.DataFrame:"""Fetch the pedigree and return it as a data frame."""@abc.abstractmethoddef_fetch_tblproperties(self)->str:"""Fetch partion description from metadata table."""@abc.abstractmethoddef_get_connection_factory(self)->Any:"""Return the connection factory for specific SQL engine."""@abc.abstractmethoddef_deserialize_summary_variant(self,record:Any,)->SummaryVariant:"""Deserialize a summary variant from SQL record."""@abc.abstractmethoddef_deserialize_family_variant(self,record:Any,)->FamilyVariant:"""Deserialize a family variant from SQL record."""# pylint: disable=too-many-arguments
[docs]defbuild_summary_variants_query_runner(self,*,regions:list[Region]|None=None,genes:list[str]|None=None,effect_types:list[str]|None=None,variant_type:str|None=None,real_attr_filter:RealAttrFilterType|None=None,categorical_attr_filter:CategoricalAttrFilterType|None=None,ultra_rare:bool|None=None,frequency_filter:RealAttrFilterType|None=None,return_reference:bool|None=None,return_unknown:bool|None=None,limit:int|None=None,**kwargs:Any,# noqa: ARG002)->QueryRunner|None:"""Build a query selecting the appropriate summary variants."""ifself.summary_allele_tableisNone:logger.warning("No summary allele table defined in %s",self.db)returnNoneassertself.summary_allele_tableisnotNonequery_builder=SummaryQueryBuilder(self.dialect,self.db,self.family_variant_table,self.summary_allele_table,self.pedigree_table,self.family_variant_schema,self.summary_allele_schema,self.partition_descriptor.to_dict(),self.pedigree_schema,self.families,gene_models=self.gene_models,)iflimitisNoneorlimit<0:query_limit=Nonelimit=-1else:query_limit=10*limitquery=query_builder.build_query(regions=regions,genes=genes,effect_types=effect_types,variant_type=variant_type,real_attr_filter=real_attr_filter,categorical_attr_filter=categorical_attr_filter,ultra_rare=ultra_rare,frequency_filter=frequency_filter,return_reference=return_reference,return_unknown=return_unknown,limit=query_limit,)logger.info("SUMMARY VARIANTS QUERY:\n%s",query)# pylint: disable=protected-accessrunner=self.RUNNER_CLASS(connection_factory=self._get_connection_factory(),query=query,deserializer=self._deserialize_summary_variant)filter_func=RawFamilyVariants.summary_variant_filter_function(regions=regions,genes=genes,effect_types=effect_types,variant_type=variant_type,real_attr_filter=real_attr_filter,ultra_rare=ultra_rare,frequency_filter=frequency_filter,return_reference=return_reference,return_unknown=return_unknown,limit=limit,seen=set())runner.adapt(filter_func)returnrunner
[docs]defbuild_family_variants_query_runner(self,*,regions:list[Region]|None=None,genes:list[str]|None=None,effect_types:list[str]|None=None,family_ids:list[str]|None=None,person_ids:list[str]|None=None,inheritance:list[str]|None=None,roles:str|None=None,sexes:str|None=None,affected_statuses:str|None=None,variant_type:str|None=None,real_attr_filter:RealAttrFilterType|None=None,categorical_attr_filter:CategoricalAttrFilterType|None=None,ultra_rare:bool|None=None,frequency_filter:RealAttrFilterType|None=None,return_reference:bool|None=None,return_unknown:bool|None=None,limit:int|None=None,study_filters:list[str]|None=None,# noqa: ARG002**kwargs:Any,# noqa: ARG002)->QueryRunner|None:"""Build a query selecting the appropriate family variants."""ifself.family_variant_tableisNone \
orself.summary_allele_tableisNone:logger.warning("No family or summary allele table defined in %s",self.db)returnNonedo_join_allele_in_members=person_idsisnotNoneassertself.family_variant_tableisnotNoneassertself.summary_allele_tableisnotNoneiflimitisNoneorlimit<0:query_limit=Nonelimit=-1else:query_limit=10*limitquery_builder=FamilyQueryBuilder(self.dialect,self.db,# type: ignoreself.family_variant_table,self.summary_allele_table,self.pedigree_table,family_variant_schema=self.family_variant_schema,summary_allele_schema=self.summary_allele_schema,table_properties=self.partition_descriptor.to_dict(),pedigree_schema=self.pedigree_schema,families=self.families,gene_models=self.gene_models,do_join_allele_in_members=do_join_allele_in_members,)query=query_builder.build_query(regions=regions,genes=genes,effect_types=effect_types,family_ids=family_ids,person_ids=person_ids,inheritance=inheritance,roles=roles,sexes=sexes,affected_statuses=affected_statuses,variant_type=variant_type,real_attr_filter=real_attr_filter,categorical_attr_filter=categorical_attr_filter,ultra_rare=ultra_rare,frequency_filter=frequency_filter,return_reference=return_reference,return_unknown=return_unknown,limit=query_limit,)logger.info("FAMILY VARIANTS QUERY:\n%s",query)deserialize_row=self._deserialize_family_variant# pylint: disable=protected-accessrunner=self.RUNNER_CLASS(connection_factory=self._get_connection_factory(),query=query,deserializer=deserialize_row)filter_func=RawFamilyVariants.family_variant_filter_function(regions=regions,genes=genes,effect_types=effect_types,family_ids=family_ids,person_ids=person_ids,inheritance=inheritance,roles=roles,sexes=sexes,variant_type=variant_type,real_attr_filter=real_attr_filter,ultra_rare=ultra_rare,frequency_filter=frequency_filter,return_reference=return_reference,return_unknown=return_unknown,limit=limit,seen=set())runner.adapt(filter_func)returnrunner