def layout(args):
    """
    %prog layout omgfile taxa

    Build column formatted gene lists after omgparse(). Use species list
    separated by comma in place of taxa, e.g. "BR,BO,AN,CN"
    """
    p = OptionParser(layout.__doc__)
    p.add_option("--sort",
                 help="Sort layout file based on bedfile [default: %default]")
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    omgfile, taxa = args
    listfile = omgfile.rsplit(".", 1)[0] + ".list"
    taxa = taxa.split(",")
    ntaxa = len(taxa)
    fw = open(listfile, "w")

    data = []
    fp = open(omgfile)
    for row in fp:
        genes, idxs = row.split()
        row = ["."] * ntaxa
        genes = genes.split(",")
        ixs = [int(x) for x in idxs.split(",")]
        for gene, idx in zip(genes, ixs):
            row[idx] = gene
        txs = ",".join(taxa[x] for x in ixs)
        print("\t".join(("\t".join(row), txs)), file=fw)
        data.append(row)

    coldata = zip(*data)
    ngenes = []
    for i, tx in enumerate(taxa):
        genes = [x for x in coldata[i] if x != '.']
        genes = set(x.strip("|") for x in genes)
        ngenes.append((len(genes), tx))

    details = ", ".join("{0} {1}".format(a, b) for a, b in ngenes)
    total = sum(a for a, b in ngenes)
    s = "A list of {0} orthologous families that collectively".format(len(data))
    s += " contain a total of {0} genes ({1})".format(total, details)
    print(s, file=sys.stderr)

    fw.close()
    lastcolumn = ntaxa + 1
    cmd = "sort -k{0},{0} {1} -o {1}".format(lastcolumn, listfile)
    sh(cmd)

    logging.debug("List file written to `{0}`.".format(listfile))
    sort = opts.sort
    if sort:
        thread = Bed(sort)
        sort_layout(thread, listfile)
def layout ( args ) : p = OptionParser ( layout . __doc__ ) p . add_option ( "--sort" , help = "Sort layout file based on bedfile [default: %default]" ) opts , args = p . parse_args ( args ) if len ( args ) != 2 : sys . exit ( not p . print_help ( ) ) omgfile , taxa = args listfile = omgfile . rsplit ( "." , 1 ) [ 0 ] + ".list" taxa = taxa . split ( "," ) ntaxa = len ( taxa ) fw = open ( listfile , "w" ) data = [ ] fp = open ( omgfile ) for row in fp : genes , idxs = row . split ( ) row = [ "." ] * ntaxa genes = genes . split ( "," ) ixs = [ int ( x ) for x in idxs . split ( "," ) ] for gene , idx in zip ( genes , ixs ) : row [ idx ] = gene txs = "," . join ( taxa [ x ] for x in ixs ) print ( "\t" . join ( ( "\t" . join ( row ) , txs ) ) , file = fw ) data . append ( row ) coldata = zip ( * data ) ngenes = [ ] for i , tx in enumerate ( taxa ) : genes = [ x for x in coldata [ i ] if x != '.' ] genes = set ( x . strip ( "|" ) for x in genes ) ngenes . append ( ( len ( genes ) , tx ) ) details = ", " . join ( "{0} {1}" . format ( a , b ) for a , b in ngenes ) total = sum ( a for a , b in ngenes ) s = "A list of {0} orthologous families that collectively" . format ( len ( data ) ) s += " contain a total of {0} genes ({1})" . format ( total , details ) print ( s , file = sys . stderr ) fw . close ( ) lastcolumn = ntaxa + 1 cmd = "sort -k{0},{0} {1} -o {1}" . format ( lastcolumn , listfile ) sh ( cmd ) logging . debug ( "List file written to `{0}`." . format ( listfile ) ) sort = opts . sort if sort : thread = Bed ( sort ) sort_layout ( thread , listfile )
%prog layout omgfile taxa

    Build column formatted gene lists after omgparse(). Use species list
    separated by comma in place of taxa, e.g. "BR,BO,AN,CN"