# Path: m4io.py
# def getAlignments(fn, filter_func=identityFunc):
#     '''fn is the filename 
#        returns alignments grouped by their name
#        filter_func works on a group of alignments for a specific read
#     '''
#     raw_alignments = getRawAlignments(fn)
#     groupby_first = itemgetter(0)
#     grouped = groupby(raw_alignments,groupby_first)
#     return imap(lambda (k,v) : (k,filter_func(list(v))), grouped)
# 
# def longestNonOverlapping(alignments):
#     '''
#     Gets the best non-overlapping alignment sections 
#     with respect to the query sequence
#     
#     Just greedily sorts the
#     alignments by length and adds them sequentially as long
#     as they don't overlap another alignment (with respect to the query)
#     '''
# 
#     align_len = lambda r : r.qend - r.qstart
#     alignments.sort(key=align_len, reverse=True)
# 
#     newset = []
#     not_overlaps = lambda y,other : y.qend < other.qstart or y.qstart > other.qend    
# 
#     for a in alignments:
#         if all(map(partial(not_overlaps, a), newset)):
#             newset.append(a)
#     
#     return newset
# 
# Path: nucio.py
# def recordToString( record, delim="\t" ):
#     '''Prints a generic named tuple combined by delim'''
#     fields = record._fields
#     return delim.join(imap(lambda x: str(getattr(record,x)), fields))


import sys
from m4io import getAlignments, longestNonOverlapping
from nucio import recordToString
#!/usr/bin/env python


##
#Attempts to print split reads from m4 file
##




if not len(sys.argv) == 2:
    sys.exit("getSplitReads.py in.m4")


inm4 = sys.argv[1]

for read,alignments in getAlignments(inm4, longestNonOverlapping):
    if len(alignments) > 1:
        astrings = map(recordToString, alignments)

# Path: m4io.py
# def getAlignments(fn, filter_func=identityFunc):
#     '''fn is the filename 
#        returns alignments grouped by their name
#        filter_func works on a group of alignments for a specific read
#     '''
#     raw_alignments = getRawAlignments(fn)
#     groupby_first = itemgetter(0)
#     grouped = groupby(raw_alignments,groupby_first)
#     return imap(lambda (k,v) : (k,filter_func(list(v))), grouped)
# 
# def longestNonOverlapping(alignments):
#     '''
#     Gets the best non-overlapping alignment sections 
#     with respect to the query sequence
#     
#     Just greedily sorts the
#     alignments by length and adds them sequentially as long
#     as they don't overlap another alignment (with respect to the query)
#     '''
# 
#     align_len = lambda r : r.qend - r.qstart
#     alignments.sort(key=align_len, reverse=True)
# 
#     newset = []
#     not_overlaps = lambda y,other : y.qend < other.qstart or y.qstart > other.qend    
# 
#     for a in alignments:
#         if all(map(partial(not_overlaps, a), newset)):
#             newset.append(a)
#     
#     return newset
# 
# Path: nucio.py
# def recordToString( record, delim="\t" ):
#     '''Prints a generic named tuple combined by delim'''
#     fields = record._fields
#     return delim.join(imap(lambda x: str(getattr(record,x)), fields))


# Path: getSplitReads.py
import sys
from m4io import getAlignments, longestNonOverlapping
from nucio import recordToString

#!/usr/bin/env python


##
#Attempts to print split reads from m4 file
##




if not len(sys.argv) == 2:
    sys.exit("getSplitReads.py in.m4")


inm4 = sys.argv[1]

for read,alignments in getAlignments(inm4, longestNonOverlapping):
    if len(alignments) > 1:
        astrings = map(recordToString, alignments)

        print "\n".join(astrings)