@transform( (importRegionsOfInterest, 
             importGWAS, 
             importMergedGWAS ),
            suffix( ".import"),
            "_genes.import")
def importRegionsOfInterestGenes( infile, outfile ):
    '''import association between regions of interest and genes implicated in disease.'''

    intablename = infile[:-len(".import")]
    tablename = outfile[:-len(".import")]

    E.info( "tablenames: %s, %s" % (intablename, tablename))

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

    statement = "SELECT roi_id, lower(genes) FROM %(intablename)s" % locals()

    tmpf = P.getTempFile()
    tmpf.write("roi_id\tgene_id\tgene_name\n" )
    
    cc = dbhandle.cursor()
    counter = E.Counter()
    notfound = set()

    for roi_id, g in cc.execute( statement ):
        counter.input += 1

        if g == None: 
            counter.nogenes += 1
            continue

        genes = [x.lower() for x in g.split( "," )]

        for gene_name in genes:
            if gene_name in map_gene2id:
                counter.found += 1
                tmpf.write( "%s\n" %\
                            "\t".join( map(str,
                                           ( roi_id, map_gene2id[gene_name], gene_name) ) ) )
            else:
                counter.missed += 1
                notfound.add( gene_name )

    E.info( "importRegionsOfInterestGenes: %s" % (str(counter)))

    outf = open( outfile, "a" )
    outf.write( "%s\n" % (str(counter)))
    outf.write( "gene names not found: %s\n" % ",".join(notfound) )
    outf.close()

    tmpf.close()

    tmpfilename = tmpf.name
    statement = '''
   python %(scriptsdir)s/csv2db.py %(csv2db_options)s 
               --allow-empty
               --index=roi_id 
               --map=roi_id:str 
               --table=%(tablename)s 
    < %(tmpfilename)s 
    >> %(outfile)s
    '''

    P.run()
    
    os.unlink( tmpfilename )
        
###################################################################
###################################################################
###################################################################
##
###################################################################
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_vitaminD.py/importRegionsOfInterestGenes/original