@files( [ ("%s.bed" % x, "%s_intervals.import" % x) for x in TRACKS_REFERENCE ] )
def importReferenceIntervals( infile, outfile ):
    '''import reference intervals'''

    ## add the bed intervals back to table
    track = infile[:-len(".bed")]

    tmpfile = tempfile.NamedTemporaryFile(delete=False)
    tmpfilename = tmpfile.name

    headers = ("AvgVal","DisttoStart","GeneList","Length",
               "PeakCenter","PeakVal","Position",
               "interval_id","nCpGs","nGenes","nPeaks",
               "nProbes","nPromoters", 
               "contig","start","end" )

    avgval,contig,disttostart,end,genelist,length,peakcenter,peakval,position,start,interval_id,ncpgs,ngenes,npeaks,nprobes,npromoters = \
        0,"",0,0,"",0,0,0,0,0,0,0,0,0,0,0,

    tmpfile.write( "\t".join( headers ) + "\n" )

    for line in open( infile, "r" ):
        contig, start, end, interval_id = line[:-1].split("\t")[:4]
        start, end = int(start), int(end)
        length = end - start
        npeaks, peakcenter = 1, start + int(length / 2)
        avgval, peakval, nprobes = 1, 1, 1

        tmpfile.write( "\t".join( map( str, (avgval,disttostart,genelist,length,peakcenter,peakval,position,interval_id,ncpgs,ngenes,npeaks,nprobes,npromoters, contig,start,end) )) + "\n" )

    tmpfile.close()

    statement = '''
   python %(scriptsdir)s/csv2db.py %(csv2db_options)s \
              --index=interval_id \
              --table=%(track)s_intervals \
    < %(tmpfilename)s > %(outfile)s
    '''

    P.run()

    os.unlink( tmpfile.name )

###################################################################
###################################################################
###################################################################
## Version 1: import from given bed files
###################################################################
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_vitaminD.py/importReferenceIntervals/original