def subset(args):
    """
    %prog subset pairsfile ksfile1 ksfile2 ... -o pairs.ks

    Subset some pre-calculated ks ka values (in ksfile) according to pairs
    in tab delimited pairsfile/anchorfile.
    """
    p = OptionParser(subset.__doc__)
    p.add_option("--noheader", action="store_true",
                 help="don't write ksfile header line [default: %default]")
    p.add_option("--block", action="store_true",
                 help="preserve block structure in input [default: %default]")
    p.set_stripnames()
    p.set_outfile()

    opts, args = p.parse_args(args)

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

    pairsfile, ksfiles = args[0], args[1:]
    noheader = opts.noheader
    block = opts.block
    if block:
        noheader = True
    outfile = opts.outfile

    ksvals = {}
    for ksfile in ksfiles:
        ksvals.update(dict((line.name, line) for line in \
                        KsFile(ksfile, strip_names=opts.strip_names)))

    fp = open(pairsfile)
    fw = must_open(outfile, "w")

    if not noheader:
        print(fields, file=fw)

    i = j = 0
    for row in fp:
        if row[0] == '#':
            if block:
                print(row.strip(), file=fw)
            continue
        a, b = row.split()[:2]
        name = ";".join((a, b))
        if name not in ksvals:
            name = ";".join((b, a))
            if name not in ksvals:
                j += 1
                print("\t".join((a, b, ".", ".")), file=fw)
                continue
        ksline = ksvals[name]
        if block:
            print("\t".join(str(x) for x in (a, b, ksline.ks)), file=fw)
        else:
            ksline.name = ";".join((a, b))
            print(ksline, file=fw)
        i += 1
    fw.close()

    logging.debug("{0} pairs not found in ksfiles".format(j))
    logging.debug("{0} ks records written to `{1}`".format(i, outfile))
    return outfile
def subset ( args ) : p = OptionParser ( subset . __doc__ ) p . add_option ( "--noheader" , action = "store_true" , help = "don't write ksfile header line [default: %default]" ) p . add_option ( "--block" , action = "store_true" , help = "preserve block structure in input [default: %default]" ) p . set_stripnames ( ) p . set_outfile ( ) opts , args = p . parse_args ( args ) if len ( args ) != 2 : sys . exit ( not p . print_help ( ) ) pairsfile , ksfiles = args [ 0 ] , args [ 1 : ] noheader = opts . noheader block = opts . block if block : noheader = True outfile = opts . outfile ksvals = { } for ksfile in ksfiles : ksvals . update ( dict ( ( line . name , line ) for line in KsFile ( ksfile , strip_names = opts . strip_names ) ) ) fp = open ( pairsfile ) fw = must_open ( outfile , "w" ) if not noheader : print ( fields , file = fw ) i = j = 0 for row in fp : if row [ 0 ] == '#' : if block : print ( row . strip ( ) , file = fw ) continue a , b = row . split ( ) [ : 2 ] name = ";" . join ( ( a , b ) ) if name not in ksvals : name = ";" . join ( ( b , a ) ) if name not in ksvals : j += 1 print ( "\t" . join ( ( a , b , "." , "." ) ) , file = fw ) continue ksline = ksvals [ name ] if block : print ( "\t" . join ( str ( x ) for x in ( a , b , ksline . ks ) ) , file = fw ) else : ksline . name = ";" . join ( ( a , b ) ) print ( ksline , file = fw ) i += 1 fw . close ( ) logging . debug ( "{0} pairs not found in ksfiles" . format ( j ) ) logging . debug ( "{0} ks records written to `{1}`" . format ( i , outfile ) ) return outfile
%prog subset pairsfile ksfile1 ksfile2 ... -o pairs.ks

    Subset some pre-calculated ks ka values (in ksfile) according to pairs
    in tab delimited pairsfile/anchorfile.