def makeIntervalCorrelation( infiles, outfile, field ):
    '''compute correlation of interval properties between sets
    '''

    dbhandle = sqlite3.connect( PARAMS["database"] )

    tracks, idx = [], []
    for infile in infiles:
        track = infile[:-len(".bed")]
        cc = dbhandle.cursor()
        statement = "SELECT contig, start, end, %(field)s FROM %(track)s_intervals" % locals()
        cc.execute( statement )
        ix = IndexedGenome.IndexedGenome()
        for contig, start, end, peakval in cc:
            ix.add( contig, start, end, peakval )        
        idx.append( ix )
        tracks.append( track )
    outs = open( outfile, "w" )
    outs.write( "id\tcontig\tstart\tend\t" + "\t".join( tracks ) + "\n" )

    for bed in Bed.iterator( infile = open( "merged.bed", "r") ):
        
        row = []
        for ix in idx:
            try:
                intervals = list(ix.get( bed.contig, bed.start, bed.end ))
            except __HOLE__:
                row.append( "" )
                continue
        
            if len(intervals) == 0:
                peakval = ""
            else:
                peakval = str( (max( [ x[2] for x in intervals ] )) )
            row.append( peakval )

        outs.write( str(bed) + "\t" + "\t".join( row ) + "\n" )

    outs.close()
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_vitaminD_intervals.py/makeIntervalCorrelation