# 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

delta_header = fh.readline() + fh.readline().strip()

#read all alignments into memory,
#could be bad for large delta files
deltaHash={}
for record in getDeltaAlignmentIterator(fh):
    if not deltaHash.has_key(record.sname):
        deltaHash[record.sname] = []
    deltaHash[record.sname].append(record)
fh.close()

#figure out the first and last alignment of each
#pb read, so we can trim.
#populate readRange with 
#key: readname
#value: tuple(start,end)
readRange = {}
for readname, drecord_list in deltaHash.iteritems():
    (start,end) = (drecord_list[0].slen, 0)
    for drecord in drecord_list:
        for alignment in drecord.alignments:
            l = min(alignment.sstart,alignment.send)
            if l < start:
                start = l
            r = max(alignment.sstart,alignment.send)
            if r > end:
                end = r
    readRange[readname] = AlignmentRange(start,end)


# 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


delta_header = fh.readline() + fh.readline().strip()

#read all alignments into memory,
#could be bad for large delta files
deltaHash={}
for record in getDeltaAlignmentIterator(fh):
    if not deltaHash.has_key(record.sname):
        deltaHash[record.sname] = []
    deltaHash[record.sname].append(record)
fh.close()

#figure out the first and last alignment of each
#pb read, so we can trim.
#populate readRange with 
#key: readname
#value: tuple(start,end)
readRange = {}
for readname, drecord_list in deltaHash.iteritems():
    (start,end) = (drecord_list[0].slen, 0)
    for drecord in drecord_list:
        for alignment in drecord.alignments:
            l = min(alignment.sstart,alignment.send)
            if l < start:
                start = l
            r = max(alignment.sstart,alignment.send)
            if r > end:
                end = r
    readRange[readname] = AlignmentRange(start,end)


ofh = open(outfilename, "w")