# Path: nucio.py
# 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
# 
# def lineItemIterator(fh, filter_func=trueFunc):
#     '''Takes a file handle and returns a list of the split line
#     Will also take a filter function to filter out lines before they
#     are split
#     '''
#     filtered = ifilter( filter_func, fh)
#     return imap(str.split, filtered)


import sys
import numpy as np
from itertools import ifilter, izip
from functools import partial
from operator import itemgetter
from nucio import fileIterator, lineItemIterator
    if not status(read1) == "U" or not status(read2) == "U":
        failrepeat += 1
        continue
    if not aqual(read1) >= MAPQ_THRESH or not aqual(read2) >= MAPQ_THRESH:
        failmapq += 1
        continue
    
    if ref(read1) == ref(read2):
        failsameref += 1
        if not (strand(read1) == "R" and strand(read2) =="F"):
            insertNotRF.append(pos(read2) - pos(read1))
        else:
            insertRF.append(pos(read2) - pos(read1))
        continue

    #just have good pairs print them
    print "\n".join(map("\t".join, [read1,read2]))
    passed += 1

with open("filterpairs.stats", "w") as ofh:
    ofh.write("All numbers are counts of mates: \n")
    ofh.write("One or both reads in repeat or failed to align: %d\n" % failrepeat)
    ofh.write("One or both reads <= mapq %d : %d \n" % (MAPQ_THRESH, failmapq))
    ofh.write("Map to same contig : %d \n" % failsameref)
    ofh.write("Map to different contigs (kept ones): %d of %d : %f\n\n" % (passed, total, float(passed)/total)) 
    ofh.write("Histogram of Mates not in RF orientation \n")
    h = np.histogram(insertNotRF,bins=1000)
    for v,b in zip(*h):
        ofh.write("%d %d\n" % (b,v))
    ofh.write("\n\n")

# Path: nucio.py
# 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
# 
# def lineItemIterator(fh, filter_func=trueFunc):
#     '''Takes a file handle and returns a list of the split line
#     Will also take a filter function to filter out lines before they
#     are split
#     '''
#     filtered = ifilter( filter_func, fh)
#     return imap(str.split, filtered)


# Path: filterpairs.py
import sys
import numpy as np
from itertools import ifilter, izip
from functools import partial
from operator import itemgetter
from nucio import fileIterator, lineItemIterator

    if not status(read1) == "U" or not status(read2) == "U":
        failrepeat += 1
        continue
    if not aqual(read1) >= MAPQ_THRESH or not aqual(read2) >= MAPQ_THRESH:
        failmapq += 1
        continue
    
    if ref(read1) == ref(read2):
        failsameref += 1
        if not (strand(read1) == "R" and strand(read2) =="F"):
            insertNotRF.append(pos(read2) - pos(read1))
        else:
            insertRF.append(pos(read2) - pos(read1))
        continue

    #just have good pairs print them
    print "\n".join(map("\t".join, [read1,read2]))
    passed += 1

with open("filterpairs.stats", "w") as ofh:
    ofh.write("All numbers are counts of mates: \n")
    ofh.write("One or both reads in repeat or failed to align: %d\n" % failrepeat)
    ofh.write("One or both reads <= mapq %d : %d \n" % (MAPQ_THRESH, failmapq))
    ofh.write("Map to same contig : %d \n" % failsameref)
    ofh.write("Map to different contigs (kept ones): %d of %d : %f\n\n" % (passed, total, float(passed)/total)) 
    ofh.write("Histogram of Mates not in RF orientation \n")
    h = np.histogram(insertNotRF,bins=1000)
    for v,b in zip(*h):
        ofh.write("%d %d\n" % (b,v))
    ofh.write("\n\n")

    ofh.write("Histogram of Mates in RF orientation \n")        