def add_tandems(mcscanfile, tandemfile):
    """
    add tandem genes to anchor genes in mcscan file
    """
    tandems = [f.strip().split(",") for f in file(tandemfile)]
    fw = must_open(mcscanfile+".withtandems", "w")
    fp = must_open(mcscanfile)
    seen =set()
    for i, row in enumerate(fp):
        if row[0] == '#':
            continue
        anchorslist = row.strip().split("\t")
        anchors = set([a.split(",")[0] for a in anchorslist])
        anchors.remove(".")
        if anchors & seen == anchors:
            continue

        newanchors = []
        for a in anchorslist:
            if a == ".":
                newanchors.append(a)
                continue
            for t in tandems:
                if a in t:
                    newanchors.append(",".join(t))
                    seen.update(t)
                    break
            else:
                newanchors.append(a)
                seen.add(a)
        print("\t".join(newanchors), file=fw)

    fw.close()
    newmcscanfile = merge_rows_local(fw.name)

    logging.debug("Tandems added to `{0}`. Results in `{1}`".\
        format(mcscanfile, newmcscanfile))
    fp.seek(0)
    logging.debug("{0} rows merged to {1} rows".\
        format(len(fp.readlines()), len(file(newmcscanfile).readlines())))
    sh("rm %s" % fw.name)

    return newmcscanfile
def add_tandems ( mcscanfile , tandemfile ) : tandems = [ f . strip ( ) . split ( "," ) for f in file ( tandemfile ) ] fw = must_open ( mcscanfile + ".withtandems" , "w" ) fp = must_open ( mcscanfile ) seen = set ( ) for i , row in enumerate ( fp ) : if row [ 0 ] == '#' : continue anchorslist = row . strip ( ) . split ( "\t" ) anchors = set ( [ a . split ( "," ) [ 0 ] for a in anchorslist ] ) anchors . remove ( "." ) if anchors & seen == anchors : continue newanchors = [ ] for a in anchorslist : if a == "." : newanchors . append ( a ) continue for t in tandems : if a in t : newanchors . append ( "," . join ( t ) ) seen . update ( t ) break else : newanchors . append ( a ) seen . add ( a ) print ( "\t" . join ( newanchors ) , file = fw ) fw . close ( ) newmcscanfile = merge_rows_local ( fw . name ) logging . debug ( "Tandems added to `{0}`. Results in `{1}`" . format ( mcscanfile , newmcscanfile ) ) fp . seek ( 0 ) logging . debug ( "{0} rows merged to {1} rows" . format ( len ( fp . readlines ( ) ) , len ( file ( newmcscanfile ) . readlines ( ) ) ) ) sh ( "rm %s" % fw . name ) return newmcscanfile
add tandem genes to anchor genes in mcscan file