[docs]@dataclassclassScoreDesc:"""Data class to describe genomic scores in GenomicScoresDb."""name:strresource_id:strsource:strhist:Histogramdescription:strhelp:strsmall_values_desc:str|Nonelarge_values_desc:str|None
[docs]@staticmethoddeffrom_json(data:dict[str,Any])->ScoreDesc:"""Build a ScoreDesc from a JSON."""assert"hist"indatahist_type=data["hist"]["config"]["type"]ifhist_type=="categorical":hist_data:Histogram=CategoricalHistogram.from_dict(data["hist"])elifhist_type=="null":hist_data=NullHistogram.from_dict(data["hist"])elifhist_type=="number":hist_data=NumberHistogram.from_dict(data["hist"])else:raiseValueError(f"Unknown histogram type {hist_type}")returnScoreDesc(data["name"],data["resource_id"],data["source"],hist_data,data["description"],data["help"],data.get("small_values_desc"),data.get("large_values_desc"),)
[docs]@staticmethoddefbuild_genomic_scores_registry(pipeline:AnnotationPipeline,)->GenomicScoresRegistry:"""Build a genomic scores registry from an annotation pipeline."""score_annotators:list[GenomicScoreAnnotatorBase]=[]ifpipelineisnotNoneandlen(pipeline.annotators)>0:forannotatorinpipeline.annotators:annotator_info=annotator.get_info()ifannotator_info.typenotin \
{"position_score","np_score","allele_score"}:continuescore_annotators.append(cast(GenomicScoreAnnotatorBase,annotator))annotation_scores:dict[str,ScoreDesc]={}forannotatorinscore_annotators:score_descs=GenomicScoresRegistry._build_annotator_scores_desc(annotator)annotation_scores.update(score_descs)returnGenomicScoresRegistry(annotation_scores)
@staticmethoddef_build_annotator_scores_desc(annotator:GenomicScoreAnnotatorBase,)->dict[str,ScoreDesc]:annotator_info=annotator.get_info()resource=annotator_info.resources[0]score=build_score_from_resource(resource)result={}forattrinannotator_info.attributes:ifattr.internal:continuescore_def=score.score_definitions[attr.source]help_doc=_build_score_help(annotator,attr,score)score_desc=ScoreDesc(attr.name,resource.resource_id,attr.source,score.get_score_histogram(attr.source),f"{attr.name} - {attr.description}",help_doc,score_def.small_values_desc,score_def.large_values_desc,)ifscore_desc.histisNone:logger.warning("unable to load histogram for %s: %s (%s)",score.resource_id,attr.name,attr.source)continueresult[attr.name]=score_descreturnresult
[docs]defget_scores(self)->list[tuple[str,ScoreDesc]]:"""Return all genomic scores histograms."""result=[]forscore_id,scoreinself.scores.items():ifscore.hist.type!="null_histogram":result.append((score_id,score))returnresult