@files( ((PARAMS["filename_expression_kovalenko"], "expression_kovalenko.import"), ) )
def importExpressionKovalenko( infile, outfile ):
    '''import gene expression data from Kovalenko et al. (2010) 

    Converts negative fold change into ratios and % qvalues
    into qvalues.
    '''

    dbhandle = sqlite3.connect( PARAMS["database"] )
    cc = dbhandle.cursor()
    map_name2id = dict( cc.execute( "SELECT UPPER(gene_name), gene_id FROM gene_info" ).fetchall() )

    headers = (
        ("affy_id", "probeset"),
        ("gene_name", "gene_id"),
        ("fold_6", "fold_6"),
        ("qvalue_6", "qvalue_6"),
        ("fold_24", "fold_24"),
        ("qvalue_24", "qvalue_24"),
        ("fold_48", "fold_48"),
        ("qvalue_48", "qvalue_48"),
        )

    old_headers = set( [x[0] for x in headers] )
    new_headers = [x[1] for x in headers]
    take = []
    outs = P.getTempFile()
    first = True

    counts = E.Counter()

    reader = csv.reader( open(infile,"rU") )
    notfound = set()
    for row in reader:
        if first:
            first = False
            for x, old_header in enumerate(row ):
                if old_header in old_headers: take.append( x )
            outs.write("\t".join(new_headers)+ "\n")
        else:
            skip = False
            new_row = []
            for x in take:
                if headers[x][0].startswith("fold"):
                    foldval = float( row[x] )
                    if foldval < 0: 
                        row[x] = str( -1.0 / foldval )
                elif headers[x][0].startswith("qvalue"):
                    qvalue = float( row[x] )
                    row[x] = str(qvalue / 100.0)
                elif headers[x][0].startswith("gene_name"):
                    parts = [ c.upper().strip() for c in row[x].split("//") ]

                    for part in parts:
                        if part in map_name2id:
                            row[x] = map_name2id[part].upper()
                            counts.found += 1
                            break
                    else:
                        counts.notfound += 1
                        notfound.add( row[x] )
                        skip = True
                        break


                if row[x].strip() != "---":
                    new_row.append(row[x].strip())
                else:
                    new_row.append("")
                    
            if skip: continue
            outs.write("\t".join( new_row )+ "\n")

    outs.close()
    tmpname = outs.name
    tablename = outfile[:-len(".import")]

    statement = '''
   python %(scriptsdir)s/csv2db.py %(csv2db_options)s \
              --index=probeset \
              --index=gene_id \
              --table=%(tablename)s \
    < %(tmpname)s > %(outfile)s
    '''
    P.run()

    os.unlink( tmpname )

    E.info( "%s" % str(counts))
    if notfound:
        E.warn("not found: %s" % str(notfound) )

############################################################
############################################################
############################################################
## run GO analysis on differentially expressed genes
############################################################
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_vitaminD.py/importExpressionKovalenko/original