# Path: nucio.py
# def lineRecordIterator(fh, nt, nt_types, filter_func=trueFunc, delim=None, cleaner_func=None):
#     '''Create an iterator with given file handle (fh)
#     as well as named tuple type (nt)
#     and the column types (nt_types)
# 
#     filter_func filters lines before they are split, return True to keep the line
#     cleaner_func takes a line and returns a list, default just splits on delim,
#                  returned list must match the number of fields in the nt
# 
#     '''
# 
#     if cleaner_func == None:
#         cleaner_func = lambda line : line.strip().split(delim) 
#     
#     filtered_lines = ifilter(filter_func, fh)
#     split_clean_lines = imap(cleaner_func, filtered_lines)
#     typed = imap(lambda splitline : typeify(splitline, nt_types), split_clean_lines)
# 
#     return imap(nt._make, typed)
# 
# def fileIterator(filename, itemIterator, open_func=open):
#     '''Handles the life cyle of a file,
#        The itemIterator will be passed the 
#        opened file handle
#     '''
#     with open_func(filename) as fh:
#         for item in itemIterator(fh):
#             yield item
# 
# Path: nucio.py
# def lineRecordIterator(fh, nt, nt_types, filter_func=trueFunc, delim=None, cleaner_func=None):
# def lineItemIterator(fh, filter_func=trueFunc):
# def getNucmerAlignmentIterator(fh):
# def fileIterator(filename, itemIterator, open_func=open):
# def recordToString( record, delim="\t" ):
# def nucRecordToString(nuc_record):
# def typeify(mylist, mytypes):
# def prepareDeltaRecord(buf):
# def getDeltaAlignmentIterator(fh):
# def deltaRecordHeaderToString(dr):
# def deltaAlignmentHeaderToString(alignment):
# def deltaRecordToOriginalFormat(dr):
#     def __init__(self, s, *args ):
#     def __enter__(self):
#     def __exit__(self, ttype ,value, traceback):
# def openerFromExtension(filename, default=None):
# class FileOrStream:


import sys
from itertools import ifilter, imap
from nucio import lineRecordIterator, fileIterator
from nucio import M4Record, M4RecordTypes, recordToString
from args import parseArgs, getHelpStr, argflag, CLArgument
#!/usr/bin/env python

## takes an m4 file and a region chrx:start-end
## and finds all alignments that intersect that region




description = ("Usage: m4region.py [options] input.m4 chr:start-end\n"
               "Returns alignments that touch a region\n")

argument_list = [["span","span", argflag, False, "Only alignments that span the region"]]

arguments = map(CLArgument._make, argument_list)

(p_arg_map, args_remaining) = parseArgs(sys.argv[1:], arguments)

if not len(args_remaining) == 2:
    sys.exit(getHelpStr(description,arguments) + "\n")


inm4 = args_remaining[0]
(chrom, rest) = args_remaining[1].split(":")
(start,end) = map(int,rest.split("-"))

it = lambda fh : lineRecordIterator(fh, M4Record, M4RecordTypes)

cond = lambda r : r.tname == chrom and not r.tend < start and not r.tstart > end

# Path: nucio.py
# def lineRecordIterator(fh, nt, nt_types, filter_func=trueFunc, delim=None, cleaner_func=None):
#     '''Create an iterator with given file handle (fh)
#     as well as named tuple type (nt)
#     and the column types (nt_types)
# 
#     filter_func filters lines before they are split, return True to keep the line
#     cleaner_func takes a line and returns a list, default just splits on delim,
#                  returned list must match the number of fields in the nt
# 
#     '''
# 
#     if cleaner_func == None:
#         cleaner_func = lambda line : line.strip().split(delim) 
#     
#     filtered_lines = ifilter(filter_func, fh)
#     split_clean_lines = imap(cleaner_func, filtered_lines)
#     typed = imap(lambda splitline : typeify(splitline, nt_types), split_clean_lines)
# 
#     return imap(nt._make, typed)
# 
# def fileIterator(filename, itemIterator, open_func=open):
#     '''Handles the life cyle of a file,
#        The itemIterator will be passed the 
#        opened file handle
#     '''
#     with open_func(filename) as fh:
#         for item in itemIterator(fh):
#             yield item
# 
# Path: nucio.py
# def lineRecordIterator(fh, nt, nt_types, filter_func=trueFunc, delim=None, cleaner_func=None):
# def lineItemIterator(fh, filter_func=trueFunc):
# def getNucmerAlignmentIterator(fh):
# def fileIterator(filename, itemIterator, open_func=open):
# def recordToString( record, delim="\t" ):
# def nucRecordToString(nuc_record):
# def typeify(mylist, mytypes):
# def prepareDeltaRecord(buf):
# def getDeltaAlignmentIterator(fh):
# def deltaRecordHeaderToString(dr):
# def deltaAlignmentHeaderToString(alignment):
# def deltaRecordToOriginalFormat(dr):
#     def __init__(self, s, *args ):
#     def __enter__(self):
#     def __exit__(self, ttype ,value, traceback):
# def openerFromExtension(filename, default=None):
# class FileOrStream:


# Path: m4region.py
import sys
from itertools import ifilter, imap
from nucio import lineRecordIterator, fileIterator
from nucio import M4Record, M4RecordTypes, recordToString
from args import parseArgs, getHelpStr, argflag, CLArgument

#!/usr/bin/env python

## takes an m4 file and a region chrx:start-end
## and finds all alignments that intersect that region




description = ("Usage: m4region.py [options] input.m4 chr:start-end\n"
               "Returns alignments that touch a region\n")

argument_list = [["span","span", argflag, False, "Only alignments that span the region"]]

arguments = map(CLArgument._make, argument_list)

(p_arg_map, args_remaining) = parseArgs(sys.argv[1:], arguments)

if not len(args_remaining) == 2:
    sys.exit(getHelpStr(description,arguments) + "\n")


inm4 = args_remaining[0]
(chrom, rest) = args_remaining[1].split(":")
(start,end) = map(int,rest.split("-"))

it = lambda fh : lineRecordIterator(fh, M4Record, M4RecordTypes)

cond = lambda r : r.tname == chrom and not r.tend < start and not r.tstart > end

if p_arg_map["span"]: