<commit_before>"""
requires genome.db from bioplus, an sqlite3 database w/ the following structure:
tables are named according to genome name
each table has fields VARCHAR 'name' and INT UNSIGNED 'length' 
  which describes the chromosomes/scaffolds in a genome
"""
from pysam import Samfile
from pkg_resources import resource_filename, cleanup_resources
import sqlite3
_CONNECTION = sqlite3.connect(resource_filename('bioplus','data/genomes.db'))
import atexit
atexit.register(cleanup_resources)
from operator import itemgetter

def _populate_available_genomes():
    name_tuples = _CONNECTION.execute("select name from sqlite_master WHERE type='table'").fetchall()
    return map(itemgetter(0), name_tuples)

AVAILABLE_GENOMES = _populate_available_genomes()

def import_db(db, replace=False, ignore_warnings=False):
    '''
    imports a db like genome.db into the existing database
    
    note: this may not be persistent if your egg is zipped
    '''
    connection = sqlite3.connect(db)
    name_tuples = connection.execute("select name from sqlite_master WHERE type='table'").fetchall()
    available_genomes = map(itemgetter(0), name_tuples)
    for genome_name in available_genomes:
        if not ignore_warnings:
            assert genome_name in AVAILABLE_GENOMES and not replace, \
                "Genome %s already in database" % genome_name
        _CONNECTION.execute("create table %s (name VARCHAR, length INT UNSIGNED)" % 
                            genome_name)
        dict(connection.execute("select * from %s" % genome_name))
        _CONNECTION.executemany("insert into %s values (?,?)" % 
                                genome_name, dict(genome_dict).items())
    return

def add_genome(genome_name, genome_dict, replace=False):
    """
    WARNING: THIS WILL PERMANENTLY ADD A GENOME
    
    make a call to pybedtool and tries to add to genome registry
    then reloads module and repopulates globals    
    note: this may not be persistent if your egg is zipped
    """
    assert genome_name in AVAILABLE_GENOMES and not replace, "Genome %s already in database" % genome_name
    _CONNECTION.execute("create table %s (name VARCHAR, length INT UNSIGNED)" % 
                        genome_name)
    _CONNECTION.executemany("insert into %s values (?,?)" % 
                            genome_name, dict(genome_dict).items())
    return


def genome(genome_name):
    """
    if available,
    returns a dict of {'chr_name': length} for all chromosomes in that genome
    """
    if not genome_name in AVAILABLE_GENOMES: raise GenomeNotAvailableError
    else:
        try:
            return dict(_CONNECTION.execute("select * from %s" % genome_name))
        except sqlite3.OperationalError:
            raise GenomeNotAvailableError(genome_name)
    
class GenomeNotAvailableError(ValueError):
    pass

class NoMatchFound(LookupError):
    pass
<commit_msg>NoMatchFoundError name corrected<commit_after>"""
requires genome.db from bioplus, an sqlite3 database w/ the following structure:
tables are named according to genome name
each table has fields VARCHAR 'name' and INT UNSIGNED 'length' 
  which describes the chromosomes/scaffolds in a genome
"""
from pysam import Samfile
from pkg_resources import resource_filename, cleanup_resources
import sqlite3
_CONNECTION = sqlite3.connect(resource_filename('bioplus','data/genomes.db'))
import atexit
atexit.register(cleanup_resources)
from operator import itemgetter

def _populate_available_genomes():
    name_tuples = _CONNECTION.execute("select name from sqlite_master WHERE type='table'").fetchall()
    return map(itemgetter(0), name_tuples)

AVAILABLE_GENOMES = _populate_available_genomes()

def import_db(db, replace=False, ignore_warnings=False):
    '''
    imports a db like genome.db into the existing database
    
    note: this may not be persistent if your egg is zipped
    '''
    connection = sqlite3.connect(db)
    name_tuples = connection.execute("select name from sqlite_master WHERE type='table'").fetchall()
    available_genomes = map(itemgetter(0), name_tuples)
    for genome_name in available_genomes:
        if not ignore_warnings:
            assert genome_name in AVAILABLE_GENOMES and not replace, \
                "Genome %s already in database" % genome_name
        _CONNECTION.execute("create table %s (name VARCHAR, length INT UNSIGNED)" % 
                            genome_name)
        dict(connection.execute("select * from %s" % genome_name))
        _CONNECTION.executemany("insert into %s values (?,?)" % 
                                genome_name, dict(genome_dict).items())
    return

def add_genome(genome_name, genome_dict, replace=False):
    """
    WARNING: THIS WILL PERMANENTLY ADD A GENOME
    
    make a call to pybedtool and tries to add to genome registry
    then reloads module and repopulates globals    
    note: this may not be persistent if your egg is zipped
    """
    assert genome_name in AVAILABLE_GENOMES and not replace, "Genome %s already in database" % genome_name
    _CONNECTION.execute("create table %s (name VARCHAR, length INT UNSIGNED)" % 
                        genome_name)
    _CONNECTION.executemany("insert into %s values (?,?)" % 
                            genome_name, dict(genome_dict).items())
    return


def genome(genome_name):
    """
    if available,
    returns a dict of {'chr_name': length} for all chromosomes in that genome
    """
    if not genome_name in AVAILABLE_GENOMES: raise GenomeNotAvailableError
    else:
        try:
            return dict(_CONNECTION.execute("select * from %s" % genome_name))
        except sqlite3.OperationalError:
            raise GenomeNotAvailableError(genome_name)
    
class GenomeNotAvailableError(ValueError):
    pass

class NoMatchFoundError(LookupError):
    pass

{'fraction_non_alphanumeric': 0.07782369146005509, 'fraction_numerical': 0.0024104683195592287, 'mean_word_length': 3.3578394598649663, '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': '5486649', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1594, 'n_words': 512}