def writesets(datasets,outputdir,outformat='std'):
    """write data with all the combinations into file """
    
    if outformat=='std':
        namlis=[ele[0] for ele in datasets]
        datalis=[list(ele[1]) for ele in datasets]
        filename=os.path.join(outputdir,'stdformat_'+'_'.join(namlis)+'.txt')
        datatrsp=fhutils.transpose(datalis,'NA')

        out=open(filename,'w')
        out.write('\t'.join(namlis)+'\n')
        for row in datatrsp:
            out.write('\t'.join([str(x) for x in row])+'\n')
        out.close()
        return filename
        
    elif outformat=='evenn':
        tabnames=[]
        for name,data in datasets:
            out=open(os.path.join(outputdir,name+'_table.txt'),'w')
            tabnames.append(name+'_table.txt')
            data=list(set(data))
            data.insert(0,name)
            for row in data:
                if isinstance(row,list):
                    out.write('\t'.join([str(x) for x in row])+'\n')
                else:
                    out.write(str(row)+'\n')
            out.close()
            
        return tabnames

    elif outformat=='venneuler':
        totalitems=[]
        hdnames=[]
        for name,data in datasets:
            totalitems.append(dict([(ele,name) for ele in data]))
            hdnames.append(name)
        totalids=fhutils.flatten([list(d.keys()) for d in totalitems])
        tablecontent=[]
        for k in set(totalids):
            outrows=[k]
            for d in totalitems:
                if k in d:
                    outrows.append('1')
                else:
                    outrows.append('0')
            tablecontent.append(outrows)

        out=open(os.path.join(outputdir,name+'_table.txt'),'w')
        out.write('id\t'+'\t'.join(hdnames)+'\n')
        for row in tablecontent:
            out.write('\t'.join([str(x) for x in row])+'\n')
        out.close()
        veninputfile = os.path.join(outputdir,name+'_table.txt')
        return veninputfile
        
        
    else:
        print 'nothing to do'
def writesets(datasets, outputdir, outformat='std'):
    """write data with all the combinations into file """

    if outformat == 'std':
        namlis = [ele[0] for ele in datasets]
        datalis = [list(ele[1]) for ele in datasets]
        filename = os.path.join(outputdir,
                                'stdformat_' + '_'.join(namlis) + '.txt')
        datatrsp = fhutils.transpose(datalis, 'NA')

        out = open(filename, 'w')
        out.write('\t'.join(namlis) + '\n')
        for row in datatrsp:
            out.write('\t'.join([str(x) for x in row]) + '\n')
        out.close()
        return filename

    elif outformat == 'evenn':
        tabnames = []
        for name, data in datasets:
            out = open(os.path.join(outputdir, name + '_table.txt'), 'w')
            tabnames.append(name + '_table.txt')
            data = list(set(data))
            data.insert(0, name)
            for row in data:
                if isinstance(row, list):
                    out.write('\t'.join([str(x) for x in row]) + '\n')
                else:
                    out.write(str(row) + '\n')
            out.close()

        return tabnames

    elif outformat == 'venneuler':
        totalitems = []
        hdnames = []
        for name, data in datasets:
            totalitems.append(dict([(ele, name) for ele in data]))
            hdnames.append(name)
        totalids = fhutils.flatten([list(d.keys()) for d in totalitems])
        tablecontent = []
        for k in set(totalids):
            outrows = [k]
            for d in totalitems:
                if k in d:
                    outrows.append('1')
                else:
                    outrows.append('0')
            tablecontent.append(outrows)

        out = open(os.path.join(outputdir, name + '_table.txt'), 'w')
        out.write('id\t' + '\t'.join(hdnames) + '\n')
        for row in tablecontent:
            out.write('\t'.join([str(x) for x in row]) + '\n')
        out.close()
        veninputfile = os.path.join(outputdir, name + '_table.txt')
        return veninputfile

    else:
        print 'nothing to do'
def venn_runner(dataset,outputfilename='vennrunner.txt',vennengine='',outputdir=''):
    outputdir = os.path.abspath(outputdir)
    outputfilename=os.path.join(outputdir,outputfilename)

    logger.info('#########\nvenn run started#########\n')
    
    if isinstance(dataset,list):
        datadic=dict(dataset)

    uniqueinfo=[]
    commoninfo=[]
    for i in range(0,len(datadic)):
        for v in itertools.combinations(datadic.keys(),i+1):
            combo=[set(datadic[x]) for x in v ]
            tmp=combo[0]
            excl=combo[0]
            for ele in combo[1:]:
                tmp=tmp&ele
                excl=excl-ele
            commoninfo.append((v,tmp))
            uniqueinfo.append((v,excl))
    tabinfo=[]
    for name,ele in commoninfo:
        logger.info('total in {name}: {lenset}'.format(name=' & '.join(name),lenset=len(ele)))
        tabinfo.append(['_&_'.join(name)]+list(ele))
    
    for name,ele in uniqueinfo:
        logger.info('total in {name}: {lenset}'.format(name=' not '.join(name),lenset=len(ele)))
        tabinfo.append([' not '.join(name)]+list(ele))
        
    tabinfotrsp=fhutils.transpose(tabinfo,'')
    out=open(outputfilename,'w')
    for ele in tabinfotrsp:
        out.write('\t'.join([str(x) for x in ele])+'\n')
        
    out.close()
    
    if vennengine=='evenn':
        logger.error('NOT WORKING EVENN')
        pyevenn(dataset,figurename=fhutils.renamefilename(outputfilename,midtag='_'+vennengine,suffix='png'),outputdir =outputdir)
    elif vennengine=='rvenn':
        pyrvenn(dataset,figurename=fhutils.renamefilename(outputfilename,midtag='_'+vennengine,suffix='tiff'),outputdir = outputdir)
    elif vennengine=='venneuler':
        pyvenneuler(dataset,figurename=fhutils.renamefilename(outputfilename,midtag='_'+vennengine,suffix='pdf'),outputdir=outputdir)
    elif vennengine=='rvennerable':
        logger.error('NOT WORKING rvennerable')
        pyrvennerable(dataset,figurename=fhutils.renamefilename(outputfilename,midtag='_'+vennengine,suffix='pdf'),outputdir=outputdir)
    else:
        print 'no venn pdf'
def venn_runner(dataset,
                outputfilename='vennrunner.txt',
                vennengine='',
                outputdir=''):
    outputdir = os.path.abspath(outputdir)
    outputfilename = os.path.join(outputdir, outputfilename)

    logger.info('#########\nvenn run started#########\n')

    if isinstance(dataset, list):
        datadic = dict(dataset)

    uniqueinfo = []
    commoninfo = []
    for i in range(0, len(datadic)):
        for v in itertools.combinations(datadic.keys(), i + 1):
            combo = [set(datadic[x]) for x in v]
            tmp = combo[0]
            excl = combo[0]
            for ele in combo[1:]:
                tmp = tmp & ele
                excl = excl - ele
            commoninfo.append((v, tmp))
            uniqueinfo.append((v, excl))
    tabinfo = []
    for name, ele in commoninfo:
        logger.info('total in {name}: {lenset}'.format(name=' & '.join(name),
                                                       lenset=len(ele)))
        tabinfo.append(['_&_'.join(name)] + list(ele))

    for name, ele in uniqueinfo:
        logger.info('total in {name}: {lenset}'.format(name=' not '.join(name),
                                                       lenset=len(ele)))
        tabinfo.append([' not '.join(name)] + list(ele))

    tabinfotrsp = fhutils.transpose(tabinfo, '')
    out = open(outputfilename, 'w')
    for ele in tabinfotrsp:
        out.write('\t'.join([str(x) for x in ele]) + '\n')

    out.close()

    if vennengine == 'evenn':
        logger.error('NOT WORKING EVENN')
        pyevenn(dataset,
                figurename=fhutils.renamefilename(outputfilename,
                                                  midtag='_' + vennengine,
                                                  suffix='png'),
                outputdir=outputdir)
    elif vennengine == 'rvenn':
        pyrvenn(dataset,
                figurename=fhutils.renamefilename(outputfilename,
                                                  midtag='_' + vennengine,
                                                  suffix='tiff'),
                outputdir=outputdir)
    elif vennengine == 'venneuler':
        pyvenneuler(dataset,
                    figurename=fhutils.renamefilename(outputfilename,
                                                      midtag='_' + vennengine,
                                                      suffix='pdf'),
                    outputdir=outputdir)
    elif vennengine == 'rvennerable':
        logger.error('NOT WORKING rvennerable')
        pyrvennerable(dataset,
                      figurename=fhutils.renamefilename(outputfilename,
                                                        midtag='_' +
                                                        vennengine,
                                                        suffix='pdf'),
                      outputdir=outputdir)
    else:
        print 'no venn pdf'