def buildAnnotatorSegmentsROI(tmpdir, roi_class, outfile, overlap=None):
    '''convert segments in bed format to annotator format
    from infile to outfile.
    '''

    tmpsegments = os.path.join(tmpdir, "segments")
    to_cluster = True

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

    if overlap:
        statement = '''
            SELECT roi.contig, roi.start, roi.end
            FROM regions_of_interest AS roi,
                 %(overlap)s_intervals AS i
            WHERE roi.class='%(roi_class)s' AND 
                  i.contig = roi.contig AND
                  min(roi.end, i.end) - max(roi.start, i.start) > 0
        '''
    else:
        statement = '''
            SELECT roi.contig, roi.start, roi.end
            FROM regions_of_interest AS roi
            WHERE class='%(roi_class)s'
        '''

    cc = dbhandle.cursor()
    cc.execute(statement % locals())

    noutput = 0
    contigs = collections.defaultdict(list)
    for result in cc:
        contig, start, end = result
        contigs[contig].append((start, end))
        noutput += 1

    E.info("segments for roi_class `%s` and overlap `%s`: %i" %
           (roi_class, overlap, noutput))

    outs = open(tmpsegments, "w")
    gff2annotator.outputSegments(outs, contigs,
                                 section="segments")
    outs.close()

    if noutput == 0:
        return None
    else:
        return tmpsegments
def buildAnnotatorSegmentsROI(tmpdir, roi_class, outfile, overlap=None):
    '''convert segments in bed format to annotator format
    from infile to outfile.
    '''

    tmpsegments = os.path.join(tmpdir, "segments")
    to_cluster = True

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

    if overlap:
        statement = '''
            SELECT roi.contig, roi.start, roi.end
            FROM regions_of_interest AS roi,
                 %(overlap)s_intervals AS i
            WHERE roi.class='%(roi_class)s' AND 
                  i.contig = roi.contig AND
                  min(roi.end, i.end) - max(roi.start, i.start) > 0
        '''
    else:
        statement = '''
            SELECT roi.contig, roi.start, roi.end
            FROM regions_of_interest AS roi
            WHERE class='%(roi_class)s'
        '''

    cc = dbhandle.cursor()
    cc.execute(statement % locals())

    noutput = 0
    contigs = collections.defaultdict(list)
    for result in cc:
        contig, start, end = result
        contigs[contig].append((start, end))
        noutput += 1

    E.info("segments for roi_class `%s` and overlap `%s`: %i" %
           (roi_class, overlap, noutput))

    outs = open(tmpsegments, "w")
    gff2annotator.outputSegments(outs, contigs, section="segments")
    outs.close()

    if noutput == 0:
        return None
    else:
        return tmpsegments
            ''' % locals()

    cc = dbhandle.cursor()
    try:
        cc.execute(statement)
    except sqlite3.OperationalError, msg:
        E.warn("error in sql statement: %s" % msg)
        return None

    contigs = collections.defaultdict(list)
    for result in cc:
        contig, start, end, interval_id, peakval = result
        contigs[contig].append((start, end))

    outs = open(tmpsegments, "w")
    gff2annotator.outputSegments(outs, contigs, section="segments")
    outs.close()

    return tmpsegments


############################################################
############################################################
############################################################
##
############################################################
def runAnnotator(tmpdir,
                 outfile,
                 tmpannotations,
                 tmpsegments,
                 tmpworkspaces,
            ''' % locals()
            
    cc = dbhandle.cursor()
    try:
        cc.execute( statement )
    except sqlite3.OperationalError, msg:
        E.warn( "error in sql statement: %s" % msg)
        return None

    contigs = collections.defaultdict( list )
    for result in cc:
        contig, start, end, interval_id,peakval = result
        contigs[contig].append( (start,end) )

    outs = open(tmpsegments, "w" )
    gff2annotator.outputSegments( outs, contigs,
                                  section = "segments" )
    outs.close()
    
    return tmpsegments

############################################################
############################################################
############################################################
##
############################################################
def runAnnotator( tmpdir, outfile, 
                  tmpannotations, 
                  tmpsegments, 
                  tmpworkspaces, 
                  tmpsynonyms,
                  options = ""):