# Path: nucio.py
# def getNucmerAlignmentIterator(fh):
#     '''Get nucmer alignments from show-coords output
#     (Deprecated legacy)
#     '''
#     return lineRecordIterator(fh, NucRecord, NucRecordTypes)
# 
# def nucRecordToString(nuc_record):
#     fields = nuc_record._fields
#     return "\t".join(
#         imap( lambda x : str(getattr(nuc_record, x)), fields))


import sys
from nucio import getNucmerAlignmentIterator, nucRecordToString
from itertools import groupby
from operator import attrgetter
#!/usr/bin/env python


#dist from the end 
#that is considered in the middle
END_CUTOFF =  200

if not len(sys.argv) == 2:
    sys.exit("alignment_verify.py alignments.sc")

fh = open(sys.argv[1])


for pbname, alignments in groupby(getNucmerAlignmentIterator(fh), attrgetter("sname")):
    al = list(alignments)

    ##if all alignments are to the same unitig
    if all(map(lambda x: x.qname == al[0].qname , al)):
        continue
    
    for aln in al:
        if (aln.qstart > END_CUTOFF and aln.qstart < (aln.qlen-END_CUTOFF) 
            and aln.qend > END_CUTOFF and aln.qend < (aln.qlen-END_CUTOFF)):
            print nucRecordToString(aln)+"\tMIDDLE"
        else:

# Path: nucio.py
# def getNucmerAlignmentIterator(fh):
#     '''Get nucmer alignments from show-coords output
#     (Deprecated legacy)
#     '''
#     return lineRecordIterator(fh, NucRecord, NucRecordTypes)
# 
# def nucRecordToString(nuc_record):
#     fields = nuc_record._fields
#     return "\t".join(
#         imap( lambda x : str(getattr(nuc_record, x)), fields))


# Path: alignment_verify.py
import sys
from nucio import getNucmerAlignmentIterator, nucRecordToString
from itertools import groupby
from operator import attrgetter

#!/usr/bin/env python


#dist from the end 
#that is considered in the middle
END_CUTOFF =  200

if not len(sys.argv) == 2:
    sys.exit("alignment_verify.py alignments.sc")

fh = open(sys.argv[1])


for pbname, alignments in groupby(getNucmerAlignmentIterator(fh), attrgetter("sname")):
    al = list(alignments)

    ##if all alignments are to the same unitig
    if all(map(lambda x: x.qname == al[0].qname , al)):
        continue
    
    for aln in al:
        if (aln.qstart > END_CUTOFF and aln.qstart < (aln.qlen-END_CUTOFF) 
            and aln.qend > END_CUTOFF and aln.qend < (aln.qlen-END_CUTOFF)):
            print nucRecordToString(aln)+"\tMIDDLE"
        else:

            print nucRecordToString(aln)