def patcher(args):
    """
    %prog patcher backbone.bed other.bed

    Given optical map alignment, prepare the patchers. Use --backbone to suggest
    which assembly is the major one, and the patchers will be extracted from
    another assembly.
    """
    from jcvi.formats.bed import uniq

    p = OptionParser(patcher.__doc__)
    p.add_option("--backbone", default="OM",
                 help="Prefix of the backbone assembly [default: %default]")
    p.add_option("--object", default="object",
                 help="New object name [default: %default]")
    opts, args = p.parse_args(args)

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

    backbonebed, otherbed = args
    backbonebed = uniq([backbonebed])
    otherbed = uniq([otherbed])

    pf = backbonebed.split(".")[0]
    key = lambda x: (x.seqid, x.start, x.end)

    # Make a uniq bed keeping backbone at redundant intervals
    cmd = "intersectBed -v -wa"
    cmd += " -a {0} -b {1}".format(otherbed, backbonebed)
    outfile = otherbed.rsplit(".", 1)[0] + ".not." + backbonebed
    sh(cmd, outfile=outfile)

    uniqbed = Bed()
    uniqbedfile = pf + ".merged.bed"
    uniqbed.extend(Bed(backbonebed))
    uniqbed.extend(Bed(outfile))
    uniqbed.print_to_file(uniqbedfile, sorted=True)

    # Condense adjacent intervals, allow some chaining
    bed = uniqbed
    key = lambda x: range_parse(x.accn).seqid

    bed_fn = pf + ".patchers.bed"
    bed_fw = open(bed_fn, "w")

    for k, sb in groupby(bed, key=key):
        sb = list(sb)
        chr, start, end, strand = merge_ranges(sb)

        print("\t".join(str(x) for x in \
                (chr, start, end, opts.object, 1000, strand)), file=bed_fw)

    bed_fw.close()
def patcher ( args ) : from jcvi . formats . bed import uniq p = OptionParser ( patcher . __doc__ ) p . add_option ( "--backbone" , default = "OM" , help = "Prefix of the backbone assembly [default: %default]" ) p . add_option ( "--object" , default = "object" , help = "New object name [default: %default]" ) opts , args = p . parse_args ( args ) if len ( args ) != 2 : sys . exit ( not p . print_help ( ) ) backbonebed , otherbed = args backbonebed = uniq ( [ backbonebed ] ) otherbed = uniq ( [ otherbed ] ) pf = backbonebed . split ( "." ) [ 0 ] key = lambda x : ( x . seqid , x . start , x . end ) cmd = "intersectBed -v -wa" cmd += " -a {0} -b {1}" . format ( otherbed , backbonebed ) outfile = otherbed . rsplit ( "." , 1 ) [ 0 ] + ".not." + backbonebed sh ( cmd , outfile = outfile ) uniqbed = Bed ( ) uniqbedfile = pf + ".merged.bed" uniqbed . extend ( Bed ( backbonebed ) ) uniqbed . extend ( Bed ( outfile ) ) uniqbed . print_to_file ( uniqbedfile , sorted = True ) bed = uniqbed key = lambda x : range_parse ( x . accn ) . seqid bed_fn = pf + ".patchers.bed" bed_fw = open ( bed_fn , "w" ) for k , sb in groupby ( bed , key = key ) : sb = list ( sb ) chr , start , end , strand = merge_ranges ( sb ) print ( "\t" . join ( str ( x ) for x in ( chr , start , end , opts . object , 1000 , strand ) ) , file = bed_fw ) bed_fw . close ( )
%prog patcher backbone.bed other.bed

    Given optical map alignment, prepare the patchers. Use --backbone to suggest
    which assembly is the major one, and the patchers will be extracted from
    another assembly.