def reindex(args):
    """
    %prog agpfile

    assume the component line order is correct, modify coordinates, this is
    necessary mostly due to manual edits (insert/delete) that disrupts
    the target coordinates.
    """
    p = OptionParser(reindex.__doc__)
    p.add_option("--nogaps", default=False, action="store_true",
                 help="Remove all gap lines [default: %default]")
    p.add_option("--inplace", default=False, action="store_true",
                 help="Replace input file [default: %default]")
    opts, args = p.parse_args(args)

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

    agpfile, = args
    inplace = opts.inplace
    agp = AGP(agpfile, validate=False)
    pf = agpfile.rsplit(".", 1)[0]
    newagpfile = pf + ".reindexed.agp"

    fw = open(newagpfile, "w")
    agp.transfer_header(fw)
    for chr, chr_agp in groupby(agp, lambda x: x.object):
        chr_agp = list(chr_agp)
        object_beg = 1
        for i, b in enumerate(chr_agp):
            b.object_beg = object_beg
            b.part_number = i + 1
            if opts.nogaps and b.is_gap:
                continue

            if b.is_gap:
                b.object_end = object_beg + b.gap_length - 1
            else:
                b.object_end = object_beg + b.component_span - 1

            object_beg = b.object_end + 1

            print(str(b), file=fw)

    # Last step: validate the new agpfile
    fw.close()
    agp = AGP(newagpfile, validate=True)

    if inplace:
        shutil.move(newagpfile, agpfile)
        logging.debug("Rename file `{0}` to `{1}`".format(newagpfile, agpfile))
        newagpfile = agpfile

    return newagpfile
def reindex ( args ) : p = OptionParser ( reindex . __doc__ ) p . add_option ( "--nogaps" , default = False , action = "store_true" , help = "Remove all gap lines [default: %default]" ) p . add_option ( "--inplace" , default = False , action = "store_true" , help = "Replace input file [default: %default]" ) opts , args = p . parse_args ( args ) if len ( args ) != 1 : sys . exit ( p . print_help ( ) ) agpfile , = args inplace = opts . inplace agp = AGP ( agpfile , validate = False ) pf = agpfile . rsplit ( "." , 1 ) [ 0 ] newagpfile = pf + ".reindexed.agp" fw = open ( newagpfile , "w" ) agp . transfer_header ( fw ) for chr , chr_agp in groupby ( agp , lambda x : x . object ) : chr_agp = list ( chr_agp ) object_beg = 1 for i , b in enumerate ( chr_agp ) : b . object_beg = object_beg b . part_number = i + 1 if opts . nogaps and b . is_gap : continue if b . is_gap : b . object_end = object_beg + b . gap_length - 1 else : b . object_end = object_beg + b . component_span - 1 object_beg = b . object_end + 1 print ( str ( b ) , file = fw ) fw . close ( ) agp = AGP ( newagpfile , validate = True ) if inplace : shutil . move ( newagpfile , agpfile ) logging . debug ( "Rename file `{0}` to `{1}`" . format ( newagpfile , agpfile ) ) newagpfile = agpfile return newagpfile
%prog agpfile

    assume the component line order is correct, modify coordinates, this is
    necessary mostly due to manual edits (insert/delete) that disrupts
    the target coordinates.