Example #1
0
    def get_genotype_tsv_text(cls, pgx_analysis: PgxAnalysis, panel: Panel,
                              version: str) -> str:
        gene_to_haplotype_calls = pgx_analysis.get_gene_to_haplotype_calls()

        genes_in_analysis = set(gene_to_haplotype_calls.keys())
        assert genes_in_analysis == panel.get_genes(), (
            f"Gene lists inconsistent.\n"
            f"From analysis={sorted(list(genes_in_analysis))}\n"
            f"From panel={sorted(list(panel.get_genes()))}")

        gene_to_drug_info = {}
        for gene_info in panel.get_gene_infos():
            sorted_drugs = sorted([drug for drug in gene_info.drugs],
                                  key=lambda info:
                                  (info.name, info.url_prescription_info))
            gene_to_drug_info[gene_info.gene] = (cls.DRUG_SEPARATOR.join(
                [drug.name for drug in sorted_drugs]),
                                                 cls.DRUG_SEPARATOR.join([
                                                     drug.url_prescription_info
                                                     for drug in sorted_drugs
                                                 ]))

        header = cls.TSV_SEPARATOR.join(cls.GENOTYPE_TSV_COLUMNS)
        lines = [header]
        for gene in sorted(gene_to_haplotype_calls.keys()):
            if gene_to_haplotype_calls[gene]:
                for haplotype_call in sorted(
                        gene_to_haplotype_calls[gene],
                        key=lambda call: call.haplotype_name):
                    lines.append(
                        cls.TSV_SEPARATOR.join([
                            gene,
                            haplotype_call.haplotype_name,
                            cls.__get_zygosity(haplotype_call),
                            panel.get_haplotype_function(
                                gene, haplotype_call.haplotype_name),
                            gene_to_drug_info[gene][0],
                            gene_to_drug_info[gene][1],
                            panel.get_id(),
                            version,
                        ]))
            else:
                lines.append(
                    cls.TSV_SEPARATOR.join([
                        gene,
                        cls.UNRESOLVED_HAPLOTYPE_STRING,
                        cls.NOT_APPLICABLE_ZYGOSITY_STRING,
                        UNKNOWN_FUNCTION_STRING,
                        gene_to_drug_info[gene][0],
                        gene_to_drug_info[gene][1],
                        panel.get_id(),
                        version,
                    ]))
        text = "\n".join(lines) + "\n"
        return text
Example #2
0
 def __assert_gene_in_panel(cls, gene: str, panel: Panel) -> None:
     if gene not in panel.get_genes():
         error_msg = f"Call for unknown gene:\ngene={gene}"
         raise ValueError(error_msg)