<commit_before>'''
Given an input GFF3 file, the database name and a file with the relationship
between the gff3 IDs and the database accessions for each feature the script
adds the dbxref to the GFF3 file
'''

import sys
from optparse import OptionParser
import unittest
from StringIO import StringIO
from franklin.gff import (features_in_gff, get_gff_header, write_gff,
                          add_dbxref_to_feature)

def parse_options():
    'It parses the command line arguments'
    parser = OptionParser()
    parser.add_option('-i', '--ingff3', dest='ingff',
                      help='Input GFF3 file')
    parser.add_option('-o', '--outgff3', dest='outgff',
                      help='Output GFF3 file')
    parser.add_option('-r', '--relations', dest='relations',
                      help='Relationships between accessions file')
    parser.add_option('-d', '--database', dest='database',
                      help='Database name')

    return parser

def get_parameters():
    'It reads and fixes the parsed command line options'
    parser  = parse_options()
    options = parser.parse_args()[0]
    params = {}
    if not options.ingff:
        parser.error('An input GFF3 file is required')
    else:
        params['ingff_fhand'] = open(options.ingff)
    if not options.outgff:
        parser.error('An output GFF3 file is required')
    else:
        params['outgff_fhand'] = open(options.outgff, 'w')
    if not options.relations:
        parser.error('A file with the relations between accessions is required')
    else:
        params['rels_fhand'] = open(options.ingff)
    if not options.database:
        parser.error('A database name for the dbxref is required')
    else:
        params['database'] = options.database
    return params

def _add_dbxref_to_features(features, dbxref_db, acc_relations):
    'It adds dbxref to feature taking into account the kind of dbxref to add'

    for feature in  features:
        add_dbxref_to_feature(feature, dbxref_db, acc_relations[feature['id']])
        yield feature

def add_dbxref_to_gff3(ingff3_fhand, outgff3_fhand, database, rels_fhand):
    'It adds a new dbxref to a GFF3 file'

    header = get_gff_header(ingff3_fhand)

    features = features_in_gff(ingff3_fhand, 3)

    #if transforms the file in a dict
    rels_fhand.seek(0)
    acc_relations = dict(line.strip().split() for line in rels_fhand if not \<commit_msg>added a new script to add dbxrefs to a gff file<commit_after>'''
Given an input GFF3 file, the database name and a file with the relationship
between the gff3 IDs and the database accessions for each feature the script
adds the dbxref to the GFF3 file
'''

import sys
from optparse import OptionParser
import unittest
from StringIO import StringIO
from franklin.gff import (features_in_gff, get_gff_header, write_gff,
                          add_dbxref_to_feature)

def parse_options():
    'It parses the command line arguments'
    parser = OptionParser()
    parser.add_option('-i', '--ingff3', dest='ingff',
                      help='Input GFF3 file')
    parser.add_option('-o', '--outgff3', dest='outgff',
                      help='Output GFF3 file')
    parser.add_option('-r', '--relations', dest='relations',
                      help='Relationships between accessions file')
    parser.add_option('-d', '--database', dest='database',
                      help='Database name')

    return parser

def get_parameters():
    'It reads and fixes the parsed command line options'
    parser  = parse_options()
    options = parser.parse_args()[0]
    params = {}
    if not options.ingff:
        parser.error('An input GFF3 file is required')
    else:
        params['ingff3_fhand'] = open(options.ingff)
    if not options.outgff:
        parser.error('An output GFF3 file is required')
    else:
        params['outgff3_fhand'] = open(options.outgff, 'w')
    if not options.relations:
        parser.error('A file with the relations between accessions is required')
    else:
        params['rels_fhand'] = open(options.ingff)
    if not options.database:
        parser.error('A database name for the dbxref is required')
    else:
        params['database'] = options.database
    return params

def _add_dbxref_to_features(features, dbxref_db, acc_relations):
    'It adds dbxref to feature taking into account the kind of dbxref to add'

    for feature in  features:
        add_dbxref_to_feature(feature, dbxref_db, acc_relations[feature['id']])
        yield feature

def add_dbxref_to_gff3(ingff3_fhand, outgff3_fhand, database, rels_fhand):
    'It adds a new dbxref to a GFF3 file'

    header = get_gff_header(ingff3_fhand)

    features = features_in_gff(ingff3_fhand, 3)

    #if transforms the file in a dict
    rels_fhand.seek(0)
    acc_relations = dict(line.strip().split() for line in rels_fhand if not \
{'fraction_non_alphanumeric': 0.09405940594059406, 'fraction_numerical': 0.007838283828382838, 'mean_word_length': 3.0782169890664424, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1667749', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1482, 'n_words': 458}