"""Constants for VCF-to-MAF conversion.

Defines MAF column order, Sequence Ontology term mappings, effect severity
rankings, and biotype priorities — all derived from the original mskcc/vcf2maf.
"""

from __future__ import annotations

# ---------------------------------------------------------------------------
# MAF column definitions
# ---------------------------------------------------------------------------

CORE_MAF_COLUMNS = [
    "Hugo_Symbol",
    "Entrez_Gene_Id",
    "Center",
    "NCBI_Build",
    "Chromosome",
    "Start_Position",
    "End_Position",
    "Strand",
    "Variant_Classification",
    "Variant_Type",
    "Reference_Allele",
    "Tumor_Seq_Allele1",
    "Tumor_Seq_Allele2",
    "dbSNP_RS",
    "dbSNP_Val_Status",
    "Tumor_Sample_Barcode",
    "Matched_Norm_Sample_Barcode",
    "Match_Norm_Seq_Allele1",
    "Match_Norm_Seq_Allele2",
    "Tumor_Validation_Allele1",
    "Tumor_Validation_Allele2",
    "Match_Norm_Validation_Allele1",
    "Match_Norm_Validation_Allele2",
    "Verification_Status",
    "Validation_Status",
    "Mutation_Status",
    "Sequencing_Phase",
    "Sequence_Source",
    "Validation_Method",
    "Score",
    "BAM_File",
    "Sequencer",
    "Tumor_Sample_UUID",
    "Matched_Norm_Sample_UUID",
]

EXTENDED_MAF_COLUMNS = [
    "HGVSc",
    "HGVSp",
    "HGVSp_Short",
    "Transcript_ID",
    "Exon_Number",
    "t_depth",
    "t_ref_count",
    "t_alt_count",
    "n_depth",
    "n_ref_count",
    "n_alt_count",
    "all_effects",
]

VEP_MAF_COLUMNS = [
    "Allele",
    "Gene",
    "Feature",
    "Feature_type",
    "Consequence",
    "cDNA_position",
    "CDS_position",
    "Protein_position",
    "Amino_acids",
    "Codons",
    "Existing_variation",
    "IMPACT",
    "DISTANCE",
    "STRAND_VEP",
    "SYMBOL",
    "SYMBOL_SOURCE",
    "HGNC_ID",
    "BIOTYPE",
    "CANONICAL",
    "CCDS",
    "ENSP",
    "SWISSPROT",
    "TREMBL",
    "UNIPARC",
    "RefSeq",
    "SIFT",
    "PolyPhen",
    "EXON",
    "INTRON",
    "DOMAINS",
    "AF",
    "AFR_AF",
    "AMR_AF",
    "EAS_AF",
    "EUR_AF",
    "SAS_AF",
    "gnomADe_AF",
    "gnomADe_AFR_AF",
    "gnomADe_AMR_AF",
    "gnomADe_ASJ_AF",
    "gnomADe_EAS_AF",
    "gnomADe_FIN_AF",
    "gnomADe_NFE_AF",
    "gnomADe_OTH_AF",
    "gnomADe_SAS_AF",
    "CLIN_SIG",
    "SOMATIC",
    "PUBMED",
    "MOTIF_NAME",
    "MOTIF_POS",
    "HIGH_INF_POS",
    "MOTIF_SCORE_CHANGE",
    "PICK",
    "VARIANT_CLASS",
    "TSL",
    "HGVS_OFFSET",
    "PHENO",
    "GENE_PHENO",
    "FILTER",
    "flanking_bps",
    "vcf_id",
    "vcf_qual",
    "vcf_pos",
    "vcf_ref",
    "vcf_alt",
]

ALL_MAF_COLUMNS = CORE_MAF_COLUMNS + EXTENDED_MAF_COLUMNS + VEP_MAF_COLUMNS

# ---------------------------------------------------------------------------
# Sequence Ontology term → MAF Variant_Classification
# ---------------------------------------------------------------------------
# From mskcc/vcf2maf GetVariantClassification().
# None values mean the classification depends on variant type (INS/DEL).

SO_TO_MAF: dict[str, str | None] = {
    # HIGH impact
    "transcript_ablation": "Splice_Site",
    "exon_loss_variant": "Splice_Site",
    "splice_acceptor_variant": "Splice_Site",
    "splice_donor_variant": "Splice_Site",
    "stop_gained": "Nonsense_Mutation",
    "frameshift_variant": None,  # Frame_Shift_Del / Frame_Shift_Ins
    "stop_lost": "Nonstop_Mutation",
    "start_lost": "Translation_Start_Site",
    "initiator_codon_variant": "Translation_Start_Site",
    # MODERATE impact
    "inframe_insertion": "In_Frame_Ins",
    "inframe_deletion": "In_Frame_Del",
    "protein_altering_variant": None,  # In_Frame_Del / In_Frame_Ins
    "missense_variant": "Missense_Mutation",
    "coding_sequence_variant": "Missense_Mutation",
    "conservative_missense_variant": "Missense_Mutation",
    "rare_amino_acid_variant": "Missense_Mutation",
    # LOW impact
    "splice_region_variant": "Splice_Region",
    "splice_donor_5th_base_variant": "Splice_Region",
    "splice_donor_region_variant": "Splice_Region",
    "splice_polypyrimidine_tract_variant": "Splice_Region",
    "incomplete_terminal_codon_variant": "Silent",
    "synonymous_variant": "Silent",
    "stop_retained_variant": "Silent",
    "start_retained_variant": "Silent",
    "NMD_transcript_variant": "Silent",
    # MODIFIER impact
    "mature_miRNA_variant": "RNA",
    "exon_variant": "RNA",
    "non_coding_exon_variant": "RNA",
    "non_coding_transcript_exon_variant": "RNA",
    "non_coding_transcript_variant": "RNA",
    "nc_transcript_variant": "RNA",
    "5_prime_UTR_variant": "5'UTR",
    "5_prime_UTR_premature_start_codon_gain_variant": "5'UTR",
    "3_prime_UTR_variant": "3'UTR",
    "intron_variant": "Intron",
    "intragenic_variant": "Intron",
    "INTRAGENIC": "Intron",
    "transcript_amplification": "Intron",
    "TF_binding_site_variant": "IGR",
    "regulatory_region_variant": "IGR",
    "regulatory_region": "IGR",
    "intergenic_variant": "IGR",
    "intergenic_region": "IGR",
    "upstream_gene_variant": "5'Flank",
    "downstream_gene_variant": "3'Flank",
    "sequence_feature": "Targeted_Region",
    "sequence_variant": "Targeted_Region",
    "feature_elongation": "Targeted_Region",
    "feature_truncation": "Targeted_Region",
}

# ---------------------------------------------------------------------------
# Effect severity ranking (lower = more severe)
# ---------------------------------------------------------------------------

EFFECT_PRIORITY: dict[str, int] = {
    "transcript_ablation": 1,
    "exon_loss_variant": 1,
    "splice_acceptor_variant": 2,
    "splice_donor_variant": 2,
    "stop_gained": 3,
    "frameshift_variant": 4,
    "stop_lost": 5,
    "start_lost": 6,
    "initiator_codon_variant": 6,
    "transcript_amplification": 7,
    "feature_elongation": 7,
    "feature_truncation": 7,
    "inframe_insertion": 8,
    "inframe_deletion": 8,
    "protein_altering_variant": 8,
    "missense_variant": 9,
    "coding_sequence_variant": 9,
    "conservative_missense_variant": 9,
    "rare_amino_acid_variant": 9,
    "splice_region_variant": 10,
    "splice_donor_5th_base_variant": 10,
    "splice_donor_region_variant": 10,
    "splice_polypyrimidine_tract_variant": 10,
    "incomplete_terminal_codon_variant": 11,
    "synonymous_variant": 11,
    "stop_retained_variant": 11,
    "start_retained_variant": 11,
    "NMD_transcript_variant": 12,
    "mature_miRNA_variant": 13,
    "5_prime_UTR_variant": 14,
    "5_prime_UTR_premature_start_codon_gain_variant": 14,
    "3_prime_UTR_variant": 14,
    "non_coding_exon_variant": 15,
    "non_coding_transcript_exon_variant": 15,
    "exon_variant": 15,
    "non_coding_transcript_variant": 15,
    "nc_transcript_variant": 15,
    "intron_variant": 16,
    "intragenic_variant": 16,
    "INTRAGENIC": 16,
    "upstream_gene_variant": 17,
    "downstream_gene_variant": 17,
    "TF_binding_site_variant": 18,
    "regulatory_region_variant": 18,
    "regulatory_region": 18,
    "intergenic_variant": 19,
    "intergenic_region": 19,
    "sequence_feature": 20,
    "sequence_variant": 20,
}

DEFAULT_EFFECT_PRIORITY = 99  # for unknown SO terms

# ---------------------------------------------------------------------------
# Transcript biotype priority (lower = preferred)
# ---------------------------------------------------------------------------

BIOTYPE_PRIORITY: dict[str, int] = {
    "protein_coding": 1,
    "LRG_gene": 2,
    "IG_C_gene": 2,
    "IG_D_gene": 2,
    "IG_J_gene": 2,
    "IG_LV_gene": 2,
    "IG_V_gene": 2,
    "TR_C_gene": 2,
    "TR_D_gene": 2,
    "TR_J_gene": 2,
    "TR_V_gene": 2,
    "miRNA": 3,
    "snRNA": 4,
    "snoRNA": 4,
    "rRNA": 4,
    "lncRNA": 5,
    "lincRNA": 5,
    "Mt_tRNA": 5,
    "Mt_rRNA": 5,
    "antisense": 5,
    "antisense_RNA": 5,
    "misc_RNA": 5,
    "non_coding": 5,
    "sense_intronic": 5,
    "sense_overlapping": 5,
    "3prime_overlapping_ncrna": 5,
    "3prime_overlapping_ncRNA": 5,
    "processed_transcript": 6,
    "pseudogene": 7,
    "processed_pseudogene": 7,
    "polymorphic_pseudogene": 7,
    "retrotransposed": 7,
    "transcribed_processed_pseudogene": 7,
    "transcribed_unprocessed_pseudogene": 7,
    "transcribed_unitary_pseudogene": 7,
    "translated_processed_pseudogene": 7,
    "translated_unprocessed_pseudogene": 7,
    "unitary_pseudogene": 7,
    "unprocessed_pseudogene": 7,
    "IG_C_pseudogene": 7,
    "IG_J_pseudogene": 7,
    "IG_V_pseudogene": 7,
    "TR_J_pseudogene": 7,
    "TR_V_pseudogene": 7,
    "artifact": 8,
    "TEC": 8,
    "ribozyme": 8,
    "scaRNA": 8,
    "vault_RNA": 8,
}

DEFAULT_BIOTYPE_PRIORITY = 9  # for unknown biotypes

# ---------------------------------------------------------------------------
# SnpEff → VEP field-name mapping
# ---------------------------------------------------------------------------

SNPEFF_TO_VEP_FIELDS: dict[str, str] = {
    "Allele": "Allele",
    "Annotation": "Consequence",
    "Annotation_Impact": "IMPACT",
    "Gene_Name": "SYMBOL",
    "Gene_ID": "Gene",
    "Feature_Type": "Feature_type",
    "Feature_ID": "Feature",
    "Transcript_BioType": "BIOTYPE",
    "Rank": "EXON",
    "HGVS.c": "HGVSc",
    "HGVS.p": "HGVSp",
    "cDNA.pos / cDNA.length": "cDNA_position",
    "CDS.pos / CDS.length": "CDS_position",
    "AA.pos / AA.length": "Protein_position",
    "Distance": "DISTANCE",
    "ERRORS / WARNINGS / INFO": "ERRORS",
}

# Older VEP population AF column name remapping
VEP_AF_COLUMN_REMAP: dict[str, str] = {
    "GMAF": "AF",
    "AFR_MAF": "AFR_AF",
    "AMR_MAF": "AMR_AF",
    "ASN_MAF": "ASN_AF",
    "EAS_MAF": "EAS_AF",
    "EUR_MAF": "EUR_AF",
    "SAS_MAF": "SAS_AF",
    "AA_MAF": "AA_AF",
    "EA_MAF": "EA_AF",
}

# Three-letter to one-letter amino acid codes (for HGVSp_Short)
AA3_TO_AA1: dict[str, str] = {
    "Ala": "A",
    "Arg": "R",
    "Asn": "N",
    "Asp": "D",
    "Cys": "C",
    "Gln": "Q",
    "Glu": "E",
    "Gly": "G",
    "His": "H",
    "Ile": "I",
    "Leu": "L",
    "Lys": "K",
    "Met": "M",
    "Phe": "F",
    "Pro": "P",
    "Ser": "S",
    "Thr": "T",
    "Trp": "W",
    "Tyr": "Y",
    "Val": "V",
    "Ter": "*",
}

# Amino acid mapping pattern for converting HGVSp to HGVSp_Short
# e.g., p.Val600Glu → p.V600E
