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

import re

# Takes blast hit and returns it as a list of key value pairs
def parse_blast_hit_gene(blast_hit):
    if len(blast_hit) == 0 or blast_hit == '.':
        return ''

    attributes = []
    parts = blast_hit.split('`')

    part1_split = parts[0].split('|')
    attributes.append(["gene", re.sub(r'_(.*)$', '', part1_split[2])])

    return attributes

# Takes blast hit and returns it as a list of key value pairs
def parse_blast_hit_cds(blast_hit):
    if len(blast_hit) == 0 or blast_hit == '.':
        return [['product', 'hypothetical protein']]

    attributes = []

    matchObj = re.search( r'RecName: Full=([^;]*)', blast_hit)
    if matchObj:
        attributes.append(["product", matchObj.group(1)])
    else:
        attributes.append(["product", "hypothetical protein"])

    return attributes

# Takes gene ontology and returns it as a list of key value pairs
def parse_gene_ontology(gene_ontology):
    if len(gene_ontology) == 0 or gene_ontology == '.':
        return ''

    attributes = []
    elements = gene_ontology.split('`')
    for element in elements:
        element = element.replace(',', '')
        vals = element.split('^')
        key = ''
        if vals[1] == 'molecular_function':
            key = 'go_function'
        elif vals[1] == 'cellular_component':
            key = 'go_component'
        elif vals[1] == 'biological_process':
            key = 'go_process'
        attributes.append([key, vals[2]+'|'+vals[0][3:]+'||IEA'])
    return attributes

class Annotator:

    def __init__(self):
        self.entries = []

    def read_from_file(self, filename):
        with open(filename, 'r') as f:
            for line in f:
                line = line.strip()
                if len(line) == 0 or line[0] == '#':
                    continue

                self.entries.append(line.split('\t'))
            f.close()

    def write_to_file(self, filename):
        with open(filename, 'w') as f:
            for entry in self.entries:
                f.write('\t'.join(entry)+'\n')
            f.close()

    def get_entry(self, name):
        reDot = re.search('\.[0-9]', name)
        if reDot != None:
            name = name.replace(reDot.group(0), '')

        for entry in self.entries:
            if entry[2] == name:
                return entry
        return None

    def rename_mrna(self, old_name, new_name):
        for entry in self.entries:
            # protein id is in the third column
            if entry[2] == old_name:
                entry[2] = new_name
                return

    def annotate_gene(self, gene):
        gene.add_annotation('locus_tag', gene.name)
        for entry in self.entries:
            if entry[2][:-3] == gene.name: # Chop off the -RA stuff
                gene.add_annotations(parse_blast_hit_gene(entry[3]))
                return

    def annotate_cds(self, cds):
        split_prot_id = cds.name.split('_')
        cds.add_annotation('protein_id', 'gnl|PBARC|'+cds.name)
        cds.add_annotation('transcript_id', 'gnl|PBARC|'+split_prot_id[0]+'_mrna'+split_prot_id[1])

        for entry in self.entries:
            reDot = re.search('\.[0-9]', cds.name)
            dotSomething = ''
            if reDot != None:
                dotSomething = reDot.group(0)

            if entry[2] == cds.name.replace(dotSomething, ''):
                cds.add_annotations(parse_blast_hit_cds(entry[3]))
                cds.add_annotations(parse_gene_ontology(entry[8]))
                return

        cds.add_annotation('product', 'hypothetical protein')

    def annotate_mrna(self, mrna):
        split_prot_id = mrna.name.split('_')                
        mrna.add_annotation('protein_id', 'gnl|PBARC|'+mrna.name)
        mrna.add_annotation('transcript_id', 'gnl|PBARC|'+split_prot_id[0]+'_mrna'+split_prot_id[1])

        for entry in self.entries:
            reDot = re.search('\.[0-9]', mrna.name)
            dotSomething = ''
            if reDot != None:
                dotSomething = reDot.group(0)

            if entry[2] == mrna.name.replace(dotSomething, ''):
                mrna.add_annotations(parse_blast_hit_cds(entry[3]))
                return

        mrna.add_annotation('product', 'hypothetical protein')
<commit_msg>annotator can handle CDS and mRNA names without underscores<commit_after>#!/usr/bin/env python

import re

# Takes blast hit and returns it as a list of key value pairs
def parse_blast_hit_gene(blast_hit):
    if len(blast_hit) == 0 or blast_hit == '.':
        return ''

    attributes = []
    parts = blast_hit.split('`')

    part1_split = parts[0].split('|')
    attributes.append(["gene", re.sub(r'_(.*)$', '', part1_split[2])])

    return attributes

# Takes blast hit and returns it as a list of key value pairs
def parse_blast_hit_cds(blast_hit):
    if len(blast_hit) == 0 or blast_hit == '.':
        return [['product', 'hypothetical protein']]

    attributes = []

    matchObj = re.search( r'RecName: Full=([^;]*)', blast_hit)
    if matchObj:
        attributes.append(["product", matchObj.group(1)])
    else:
        attributes.append(["product", "hypothetical protein"])

    return attributes

# Takes gene ontology and returns it as a list of key value pairs
def parse_gene_ontology(gene_ontology):
    if len(gene_ontology) == 0 or gene_ontology == '.':
        return ''

    attributes = []
    elements = gene_ontology.split('`')
    for element in elements:
        element = element.replace(',', '')
        vals = element.split('^')
        key = ''
        if vals[1] == 'molecular_function':
            key = 'go_function'
        elif vals[1] == 'cellular_component':
            key = 'go_component'
        elif vals[1] == 'biological_process':
            key = 'go_process'
        attributes.append([key, vals[2]+'|'+vals[0][3:]+'||IEA'])
    return attributes

class Annotator:

    def __init__(self):
        self.entries = []

    def read_from_file(self, filename):
        with open(filename, 'r') as f:
            for line in f:
                line = line.strip()
                if len(line) == 0 or line[0] == '#':
                    continue

                self.entries.append(line.split('\t'))
            f.close()

    def write_to_file(self, filename):
        with open(filename, 'w') as f:
            for entry in self.entries:
                f.write('\t'.join(entry)+'\n')
            f.close()

    def get_entry(self, name):
        reDot = re.search('\.[0-9]', name)
        if reDot != None:
            name = name.replace(reDot.group(0), '')

        for entry in self.entries:
            if entry[2] == name:
                return entry
        return None

    def rename_mrna(self, old_name, new_name):
        for entry in self.entries:
            # protein id is in the third column
            if entry[2] == old_name:
                entry[2] = new_name
                return

    def annotate_gene(self, gene):
        gene.add_annotation('locus_tag', gene.name)
        for entry in self.entries:
            if entry[2][:-3] == gene.name: # Chop off the -RA stuff
                gene.add_annotations(parse_blast_hit_gene(entry[3]))
                return

    def annotate_cds(self, cds):
        split_prot_id = cds.name.split('_')
        cds.add_annotation('protein_id', 'gnl|PBARC|'+cds.name)
        if len(split_prot_id) >= 2:
            cds.add_annotation('transcript_id', 'gnl|PBARC|'+split_prot_id[0]+'_mrna')
        else:   # if not a Maker-named CDS :)
            cds.add_annotation('transcript_id', 'gnl|PBARC|'+cds.name+'_mrna'+cds.name)
           

        for entry in self.entries:
            reDot = re.search('\.[0-9]', cds.name)
            dotSomething = ''
            if reDot != None:
                dotSomething = reDot.group(0)

            if entry[2] == cds.name.replace(dotSomething, ''):
                cds.add_annotations(parse_blast_hit_cds(entry[3]))
                cds.add_annotations(parse_gene_ontology(entry[8]))
                return

        cds.add_annotation('product', 'hypothetical protein')

    def annotate_mrna(self, mrna):
        split_prot_id = mrna.name.split('_')                
        mrna.add_annotation('protein_id', 'gnl|PBARC|'+mrna.name)
        if len(split_prot_id) >= 2:
            mrna.add_annotation('transcript_id', 'gnl|PBARC|'+split_prot_id[0]+'_mrna'+split_prot_id[1])
        else:   # if not a Maker-named mRNA :)
            mrna.add_annotation('transcript_id', 'gnl|PBARC|'+mrna.name+'_mrna')

        for entry in self.entries:
            reDot = re.search('\.[0-9]', mrna.name)
            dotSomething = ''
            if reDot != None:
                dotSomething = reDot.group(0)

            if entry[2] == mrna.name.replace(dotSomething, ''):
                mrna.add_annotations(parse_blast_hit_cds(entry[3]))
                return

        mrna.add_annotation('product', 'hypothetical protein')

{'fraction_non_alphanumeric': 0.1364498493807877, 'fraction_numerical': 0.009037152739038268, 'mean_word_length': 2.1630204657727594, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2435973', 'n_tokens_mistral': 2982, 'n_tokens_neox': 2785, 'n_words': 612}