<commit_before>import json

import optparse


transcript_species_dict = dict()
sequence_dict = dict()


def readgene(gene):
    for transcript in gene['Transcript']:
        transcript_species_dict[transcript['id']] = transcript['species'].replace("_", "")


def read_fasta(fp):
    for line in fp:
        line = line.rstrip()
        if line.startswith(">"):
            name = line.replace(">", "")
            print ">" + name + "_" + transcript_species_dict[name]
        else:
            print line


parser = optparse.OptionParser()
parser.add_option('-j', '--json', dest="input_gene_filename",
                  help='Gene Tree from Ensembl in JSON format')

parser.add_option('-f', '--fasta', dest="input_fasta_filename",
                  help='Gene Tree from Ensembl in JSON format')

options, args = parser.parse_args()

if options.input_gene_filename is None:
    raise Exception('-j option must be specified')

if options.input_fasta_filename is None:
    raise Exception('-f option must be specified')

with open(options.input_gene_filename) as data_file:
    data = json.load(data_file)

for gene_dict in data.values():
    readgene(gene_dict)

with open(options.input_fasta_filename) as fp:
    read_fasta(fp)<commit_msg>Use print() for Python3. No global variables. Optimisations<commit_after>from __future__ import print_function

import json
import optparse


def read_gene_info(gene_info):
    transcript_species_dict = dict()
    for gene_dict in gene_info.values():
        for transcript in gene_dict['Transcript']:
            transcript_species_dict[transcript['id']] = transcript['species'].replace("_", "")
    return transcript_species_dict


parser = optparse.OptionParser()
parser.add_option('-j', '--json', dest="input_gene_filename",
                  help='Gene feature information in JSON format')
parser.add_option('-f', '--fasta', dest="input_fasta_filename",
                  help='Sequences in FASTA format')
options, args = parser.parse_args()

if options.input_gene_filename is None:
    raise Exception('-j option must be specified')

if options.input_fasta_filename is None:
    raise Exception('-f option must be specified')

with open(options.input_gene_filename) as json_fh:
    gene_info = json.load(json_fh)
transcript_species_dict = read_gene_info(gene_info)

with open(options.input_fasta_filename) as fasta_fh:
    for line in fasta_fh:
        line = line.rstrip()
        if line.startswith(">"):
            name = line[1:].lstrip()
            print(">" + name + "_" + transcript_species_dict[name])
        else:
            print(line)
{'fraction_non_alphanumeric': 0.11719352351580571, 'fraction_numerical': 0.0007710100231303007, 'mean_word_length': 3.568661971830986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '3819695', 'n_tokens_mistral': 824, 'n_tokens_neox': 766, 'n_words': 193}