@merge( buildPicardAlignStats, "bam/picard_align_stats.load" )
def loadPicardAlignStats( infiles, outfile ):
    # Join data for all tracks into single file
    outf = P.getTempFile()
    first = True
    for f in infiles:
        track = P.snip( os.path.basename(f), ".alignstats" )
        if not os.path.exists( f ): 
            E.warn( "File %s missing" % f )
            continue
        lines = [ x for x in open( f, "r").readlines() if not x.startswith("#") and x.strip() ]
        if first: outf.write( "%s\t%s" % ("track", lines[0] ) )
        first = False
        for i in range(1, len(lines)):
            outf.write( "%s\t%s" % (track,lines[i] ))
    outf.close()
    tmpfilename = outf.name

    # Load into database
    tablename = P.toTable( outfile )
    statement = '''cat %(tmpfilename)s
                | python %(scriptsdir)s/csv2db.py
                      --index=track
                      --table=%(tablename)s 
                > %(outfile)s'''
    P.run()
    os.unlink( tmpfilename )

#########################################################################
Merge Picard alignment stats into single table and load into SQLite.
dataset/ETHPy150Open CGATOxford/cgat/obsolete/pipeline_proj012_chipseq.py/loadPicardAlignStats