# Path: nucio.py
# def getDeltaAlignmentIterator(fh):
# 
#     itembuf = []    
#     while True:
#         line = fh.readline()
#         if line.startswith(">") or not line:
#             record = prepareDeltaRecord(itembuf)
#             if not None == record:
#                 yield record
#             itembuf = []
#             if not line:
#                 break
#         for i in line.strip().split():
#             itembuf.append(i)
# 
# def deltaRecordToOriginalFormat(dr):
# 
#     final_str = deltaRecordHeaderToString(dr)
#     
#     for alignment in dr.alignments:
#         final_str += "\n" + deltaAlignmentHeaderToString(alignment)
#         p = "\n".join(map(str, alignment.positions))
#         final_str += "\n"+p
#         
#     return final_str
# 
# Path: nucio.py
# def deltaRecordHeaderToString(dr):
#     a = map(lambda x : str(getattr(dr, x)), dr._fields[:-1])
#     a[0] = ">"+a[0]
#     return " ".join(a)
# 
# def deltaAlignmentHeaderToString(alignment):
#     a = map(lambda x : str(getattr(alignment,x)), 
#             alignment._fields[:-1])    
#     return " ".join(a)


import sys
from collections import namedtuple
from math import fabs
from nucio import getDeltaAlignmentIterator, deltaRecordToOriginalFormat
from nucio import deltaRecordHeaderToString, deltaAlignmentHeaderToString
                start = l
            r = max(alignment.sstart,alignment.send)
            if r > end:
                end = r
    readRange[readname] = AlignmentRange(start,end)

ofh = open(outfilename, "w")
efh = open(outfilename+".log", "w")

ofh.write(delta_header)
ofh.write("\n")
for readname,drecord_list in deltaHash.iteritems():
    alignment_range = readRange[readname]
    for record in drecord_list:
       #get alignments that pass basic filtering
        pfalignments = []
        for alnmt in record.alignments:
            if alignment_is_good(record, alnmt, alignment_range):
                pfalignments.append(alnmt)
            else:
                efh.write("Remove Alignment:\n")
                efh.write(deltaRecordHeaderToString(record) + "\n")
                efh.write(deltaAlignmentHeaderToString(alnmt)+ "\n")
            
        #0 or 1 alignments, good to go! Otherwise idk
        #just log them and output them anyway
        if len(pfalignments) > 1:
            efh.write("Multiple Alignments\n")
            efh.write(deltaRecordHeaderToString(record) + "\n")
            for a in pfalignments:

# Path: nucio.py
# def getDeltaAlignmentIterator(fh):
# 
#     itembuf = []    
#     while True:
#         line = fh.readline()
#         if line.startswith(">") or not line:
#             record = prepareDeltaRecord(itembuf)
#             if not None == record:
#                 yield record
#             itembuf = []
#             if not line:
#                 break
#         for i in line.strip().split():
#             itembuf.append(i)
# 
# def deltaRecordToOriginalFormat(dr):
# 
#     final_str = deltaRecordHeaderToString(dr)
#     
#     for alignment in dr.alignments:
#         final_str += "\n" + deltaAlignmentHeaderToString(alignment)
#         p = "\n".join(map(str, alignment.positions))
#         final_str += "\n"+p
#         
#     return final_str
# 
# Path: nucio.py
# def deltaRecordHeaderToString(dr):
#     a = map(lambda x : str(getattr(dr, x)), dr._fields[:-1])
#     a[0] = ">"+a[0]
#     return " ".join(a)
# 
# def deltaAlignmentHeaderToString(alignment):
#     a = map(lambda x : str(getattr(alignment,x)), 
#             alignment._fields[:-1])    
#     return " ".join(a)


# Path: pre_delta_filter.py
import sys
from collections import namedtuple
from math import fabs
from nucio import getDeltaAlignmentIterator, deltaRecordToOriginalFormat
from nucio import deltaRecordHeaderToString, deltaAlignmentHeaderToString

                start = l
            r = max(alignment.sstart,alignment.send)
            if r > end:
                end = r
    readRange[readname] = AlignmentRange(start,end)

ofh = open(outfilename, "w")
efh = open(outfilename+".log", "w")

ofh.write(delta_header)
ofh.write("\n")
for readname,drecord_list in deltaHash.iteritems():
    alignment_range = readRange[readname]
    for record in drecord_list:
       #get alignments that pass basic filtering
        pfalignments = []
        for alnmt in record.alignments:
            if alignment_is_good(record, alnmt, alignment_range):
                pfalignments.append(alnmt)
            else:
                efh.write("Remove Alignment:\n")
                efh.write(deltaRecordHeaderToString(record) + "\n")
                efh.write(deltaAlignmentHeaderToString(alnmt)+ "\n")
            
        #0 or 1 alignments, good to go! Otherwise idk
        #just log them and output them anyway
        if len(pfalignments) > 1:
            efh.write("Multiple Alignments\n")
            efh.write(deltaRecordHeaderToString(record) + "\n")
            for a in pfalignments:

                efh.write(deltaAlignmentHeaderToString(a)+ "\n")