import sqlite3


def fetch_exon_locations(database, genome_build):
    """ Queries the database to create a dictionary mapping exon IDs to 
        the chromosome, start, end, and strand of the exon """

    conn = sqlite3.connect(database)
    cursor = conn.cursor()

    query = """
            SELECT 
                e.edge_ID,
                loc1.chromosome,
                MIN(loc1.position,loc2.position),
                MAX(loc1.position,loc2.position),
                e.strand
             FROM edge e
             LEFT JOIN location loc1 ON e.v1 = loc1.location_ID
             LEFT JOIN location loc2 ON e.v2 = loc2.location_ID
             WHERE loc1.genome_build = '""" + genome_build + """' AND
             loc2.genome_build = '""" + genome_build + \
             """' AND e.edge_type = 'exon';"""
    
    cursor.execute(query)
    exon_location_tuples = cursor.fetchall()

    # Create dictionary
    exon_locations = {}
    for loc_tuple in exon_location_tuples:
        exon_ID = loc_tuple[0]
        exon_locations[exon_ID] = loc_tuple[1:]

    conn.close() 
    return exon_locations