import os
from json_generator import JsonGenerator

# example call:
# snpQuery = 'select chromStart as Start, chromEnd as End, name as Name, transcript, frame, alleleCount, funcCodes, alleles, codons, peptides'
# db_importer.dbImport(conn, "snp132CodingDbSnp", snpQuery, "chromEnd", "chrom", "../data/", "snp132CodingDbSnp")
def dbImport(conn, table, query, endCol, chromCol, dataDir,
             trackLabel, key = None, chunkBytes = 200000,
             compress = True,
             config = {'style': {'className': 'feature2'}}):
    query += " from %s where %s=? order by Start asc, End desc" \
             % (table, chromCol)
    cur = conn.execute("""
    select %(chrom)s, max(%(end)s), count(*) from %(table)s group by %(chrom)s
    """ % {'chrom': chromCol, 'end': endCol, 'table': table})
    chromList = cur.fetchall()
    for (chrom, refEnd, count) in chromList:
        cur = conn.execute(query, (chrom,))
        classes = [{
            'attributes': [f[0] for f in cur.description],
            'proto': {'Chrom': chrom}
            }]
        jsongen = JsonGenerator(dataDir, trackLabel, chrom, chunkBytes,
                                compress, classes, refEnd = refEnd,
                                writeHists = True, featureCount = count)
        for row in cur:
            jsongen.addSorted([0] + list(row))

        jsongen.generateTrack()
    jsongen.writeTrackEntry('FeatureTrack', config)
        
        

@@ -202,16 +202,54 @@
 peptides
+ from snp132CodingDbSnp where chrom=%25s
 '%0A# db_i
@@ -266,19 +266,18 @@
 Import(c
-onn
+ur
 , %22snp13
@@ -369,19 +369,18 @@
 Import(c
-onn
+ur
 , table,
@@ -571,117 +571,11 @@
     
-query += %22 from %25s where %25s=? order by Start asc, End desc%22 %5C%0A             %25 (table, chromCol)%0A    cur = conn
+cur
 .exe
@@ -800,16 +800,16 @@
 omList:%0A
+
         
@@ -815,15 +815,8 @@
  cur
- = conn
 .exe

support mysql in db_importer