<commit_before>#!/usr/bin/env python

#############

# CSQ: Consequence|Codons|Amino_acids|Gene|hgnc|Feature|EXON|polyphen|sift|Protein_position|BIOTYPE
# missense_variant|gAg/gTg|E/V|ENSG00000188157||ENST00000379370|12/36|probably_damaging(0.932)|deleterious(0.02)|728/2045_protein_coding
# nc_transcript_variant|||ENSG00000116254|CHD5|ENST00000491020|5/6|||||
#############

import re
from collections import namedtuple
from collections import defaultdict


class EffectDetails(object):
    def __init__(self, impact_string, severity, detail_string, counter):
        fields = detail_string.split("|")
        self.effect_severity = severity
        self.effect_name = impact_string
        self.anno_id = counter
        self.codon_change = fields[1] if fields[1] != '' else None
        self.aa_change = fields[2] if fields[2] != '' else None
        self.ensembl_gene = fields[3] if fields[3] != '' else None
        self.hgnc = fields[4] if fields[4] != '' else None
        self.gene = self.hgnc if fields[4] != '' else self.ensembl_gene
        self.transcript = fields[5] if fields[5] != '' else None
        self.exon = fields[6] if fields[6] != '' else None
        self.polyphen = fields[7] if fields[7] != '' else None
        self.sift = fields[8] if fields[8] != '' else None
        self.aa_length = fields[9] if fields[9] != '' else None
        self.biotype = fields[10] if fields[10] != '' else None
        self.warnings = None
        self.consequence = effect_dict[
            self.effect_name] if self.effect_severity != None else self.effect_name
        if len(fields) > 11:
            self.warnings = fields[11]

        # rules for being exonic.
        # 1. the impact must be in the list of exonic impacts
        # 3. must be protein_coding
        self.is_exonic = 0
        if self.effect_name in exonic_impacts and \
                self.biotype == "protein_coding":
            self.is_exonic = 1
        
        # rules for being loss-of-function (lof).
        #  must be protein_coding
        #  must be a coding variant with HIGH impact
        self.is_lof = 0 
        if self.effect_severity == "HIGH" and self.biotype == "protein_coding":
            self.is_lof = 1
        
        # Rules for being coding
        # must be protein_coding
        # Exonic but not UTR's
        self.is_coding = 0
        if self.is_exonic and not (self.effect_name == "5_prime_UTR_variant" or \
                                   self.effect_name == "3_prime_UTR_variant"):
            self.is_coding = 1
            
        # parse Polyphen predictions
        if self.polyphen is not None:
            self.polyphen_b = self.polyphen.split("(")
            self.polyphen_pred = self.polyphen_b[0]
            self.polyphen2 = self.polyphen_b[1].split(")")
            self.polyphen_score = self.polyphen2[0]
        else:
            self.polyphen_pred = None
            self.polyphen_score = None
        # parse SIFT predictions
        if self.sift is not None:
            self.sift_b = self.sift.split("(")
            self.sift_pred = self.sift_b[0]
            self.sift2 = self.sift_b[1].split(")")
            self.sift_score = self.sift2[0]
        else:
            self.sift_pred = None
            self.sift_score = None

    def __str__(self):

        return "\t".join([self.consequence, self.effect_severity, str(self.codon_change),
                          str(self.aa_change), str(self.aa_length), str(self.biotype),
                          str(self.ensembl_gene), str(self.gene), str(self.transcript),
                          str(self.exon), str(self.is_exonic), str(self.anno_id), str(self.polyphen_pred),
                          str(self.polyphen_score), str(self.sift_pred), str(self.sift_score),
                          str(self.is_coding), str(self.is_lof)])

    def __repr__(self):
        return self.__str__()


exonic_impacts = ["stop_gained",
                  "stop_lost",
                  "frameshift_variant",
                  "initiator_codon_variant",
                  "inframe_deletion",
                  "inframe_insertion",
                  "missense_variant",
                  "incomplete_terminal_codon_variant",
                  "stop_retained_variant",
                  "synonymous_variant",
                  "coding_sequence_variant",
                  "5_prime_UTR_variant",
                  "3_prime_UTR_variant",
                  "transcript_ablation",
                  "transcript_amplification",
                  "feature_elongation",
                  "feature_truncation"]


effect_names = ["splice_acceptor_variant", "splice_donor_variant",
                "stop_gained", "stop_lost",
                "non_coding_exon_variant", "frameshift_variant",
                "initiator_codon_variant", "inframe_deletion",
                "inframe_insertion", "missense_variant",
                "splice_region_variant", "incomplete_terminal_codon_variant",
                "stop_retained_variant", "synonymous_variant",
                "coding_sequence_variant", "mature_miRNA_variant",
                "5_prime_UTR_variant", "3_prime_UTR_variant",
                "intron_variant", "NMD_transcript_variant",
                "nc_transcript_variant", "upstream_gene_variant",
                "downstream_gene_variant", "regulatory_region_variant",
                "TF_binding_site_variant", "intergenic_variant",
                "regulatory_region_ablation", "regulatory_region_amplification",
                "transcript_ablation", "transcript_amplification",
                "TFBS_ablation", "TFBS_amplification",
                "feature_elongation", "feature_truncation"]


effect_dict = defaultdict()
effect_dict = {
    'splice_acceptor_variant': 'splice_acceptor', 'splice_donor_variant': 'splice_donor',
    'stop_gained': 'stop_gain', 'stop_lost': 'stop_loss',
    'non_coding_exon_variant': 'nc_exon', 'frameshift_variant': 'frame_shift',
    'initiator_codon_variant': 'transcript_codon_change', 'inframe_deletion': 'inframe_codon_loss',
    'inframe_insertion': 'inframe_codon_gain', 'missense_variant': 'non_syn_coding',
    'splice_region_variant': 'splice_region', 'incomplete_terminal_codon_variant': 'incomplete_terminal_codon',<commit_msg>SO defaulted to VEP impact terms<commit_after>#!/usr/bin/env python

#############

# CSQ: Consequence|Codons|Amino_acids|Gene|hgnc|Feature|EXON|polyphen|sift|Protein_position|BIOTYPE
# missense_variant|gAg/gTg|E/V|ENSG00000188157||ENST00000379370|12/36|probably_damaging(0.932)|deleterious(0.02)|728/2045_protein_coding
# nc_transcript_variant|||ENSG00000116254|CHD5|ENST00000491020|5/6|||||
#############

import re
from collections import namedtuple
from collections import defaultdict


class EffectDetails(object):
    def __init__(self, impact_string, severity, detail_string, counter):
        fields = detail_string.split("|")
        self.effect_severity = severity
        self.effect_name = impact_string
        self.anno_id = counter
        self.codon_change = fields[1] if fields[1] != '' else None
        self.aa_change = fields[2] if fields[2] != '' else None
        self.ensembl_gene = fields[3] if fields[3] != '' else None
        self.hgnc = fields[4] if fields[4] != '' else None
        self.gene = self.hgnc if fields[4] != '' else self.ensembl_gene
        self.transcript = fields[5] if fields[5] != '' else None
        self.exon = fields[6] if fields[6] != '' else None
        self.polyphen = fields[7] if fields[7] != '' else None
        self.sift = fields[8] if fields[8] != '' else None
        self.aa_length = fields[9] if fields[9] != '' else None
        self.biotype = fields[10] if fields[10] != '' else None
        self.warnings = None
        self.consequence = effect_dict[
            self.effect_name] if self.effect_severity != None else self.effect_name
        self.so = self.effect_name #VEP impacts are SO by default
        
        if len(fields) > 11:
            self.warnings = fields[11]

        # rules for being exonic.
        # 1. the impact must be in the list of exonic impacts
        # 3. must be protein_coding
        self.is_exonic = 0
        if self.effect_name in exonic_impacts and \
                self.biotype == "protein_coding":
            self.is_exonic = 1
        
        # rules for being loss-of-function (lof).
        #  must be protein_coding
        #  must be a coding variant with HIGH impact
        self.is_lof = 0 
        if self.effect_severity == "HIGH" and self.biotype == "protein_coding":
            self.is_lof = 1
        
        # Rules for being coding
        # must be protein_coding
        # Exonic but not UTR's
        self.is_coding = 0
        if self.is_exonic and not (self.effect_name == "5_prime_UTR_variant" or \
                                   self.effect_name == "3_prime_UTR_variant"):
            self.is_coding = 1
            
        # parse Polyphen predictions
        if self.polyphen is not None:
            self.polyphen_b = self.polyphen.split("(")
            self.polyphen_pred = self.polyphen_b[0]
            self.polyphen2 = self.polyphen_b[1].split(")")
            self.polyphen_score = self.polyphen2[0]
        else:
            self.polyphen_pred = None
            self.polyphen_score = None
        # parse SIFT predictions
        if self.sift is not None:
            self.sift_b = self.sift.split("(")
            self.sift_pred = self.sift_b[0]
            self.sift2 = self.sift_b[1].split(")")
            self.sift_score = self.sift2[0]
        else:
            self.sift_pred = None
            self.sift_score = None

    def __str__(self):

        return "\t".join([self.consequence, self.effect_severity, str(self.codon_change),
                          str(self.aa_change), str(self.aa_length), str(self.biotype),
                          str(self.ensembl_gene), str(self.gene), str(self.transcript),
                          str(self.exon), str(self.is_exonic), str(self.anno_id), str(self.polyphen_pred),
                          str(self.polyphen_score), str(self.sift_pred), str(self.sift_score),
                          str(self.is_coding), str(self.is_lof), str(self.so)])

    def __repr__(self):
        return self.__str__()


exonic_impacts = ["stop_gained",
                  "stop_lost",
                  "frameshift_variant",
                  "initiator_codon_variant",
                  "inframe_deletion",
                  "inframe_insertion",
                  "missense_variant",
                  "incomplete_terminal_codon_variant",
                  "stop_retained_variant",
                  "synonymous_variant",
                  "coding_sequence_variant",
                  "5_prime_UTR_variant",
                  "3_prime_UTR_variant",
                  "transcript_ablation",
                  "transcript_amplification",
                  "feature_elongation",
                  "feature_truncation"]


effect_names = ["splice_acceptor_variant", "splice_donor_variant",
                "stop_gained", "stop_lost",
                "non_coding_exon_variant", "frameshift_variant",
                "initiator_codon_variant", "inframe_deletion",
                "inframe_insertion", "missense_variant",
                "splice_region_variant", "incomplete_terminal_codon_variant",
                "stop_retained_variant", "synonymous_variant",
                "coding_sequence_variant", "mature_miRNA_variant",
                "5_prime_UTR_variant", "3_prime_UTR_variant",
                "intron_variant", "NMD_transcript_variant",
                "nc_transcript_variant", "upstream_gene_variant",
                "downstream_gene_variant", "regulatory_region_variant",
                "TF_binding_site_variant", "intergenic_variant",
                "regulatory_region_ablation", "regulatory_region_amplification",
                "transcript_ablation", "transcript_amplification",
                "TFBS_ablation", "TFBS_amplification",
                "feature_elongation", "feature_truncation"]

effect_dict = defaultdict()
effect_dict = {
    'splice_acceptor_variant': 'splice_acceptor', 'splice_donor_variant': 'splice_donor',
    'stop_gained': 'stop_gain', 'stop_lost': 'stop_loss',
    'non_coding_exon_variant': 'nc_exon', 'frameshift_variant': 'frame_shift',
    'initiator_codon_variant': 'transcript_codon_change', 'inframe_deletion': 'inframe_codon_loss',
    'inframe_insertion': 'inframe_codon_gain', 'missense_variant': 'non_syn_coding',
    'splice_region_variant': 'splice_region', 'incomplete_terminal_codon_variant': 'incomplete_terminal_codon',
{'fraction_non_alphanumeric': 0.10983437673349711, 'fraction_numerical': 0.01838497503764165, 'mean_word_length': 2.343046357615894, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7005772', 'n_tokens_mistral': 4190, 'n_tokens_neox': 3838, 'n_words': 693}