# 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

filt_fits = map( lambda fn : fileIterator(fn, filt_lii), filenames)

failrepeat = 0
failmapq = 0
failsameref = 0
total = 0
passed = 0
insertNotRF = []
insertRF = []


for read1,read2 in izip(*filt_fits):
    total += 1
    if not names_eq(name(read1), name(read2)):
        sys.exit("Error: %s not equal to %s\n" % (name(read1),name(read2)))
    
    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))

# 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


filt_fits = map( lambda fn : fileIterator(fn, filt_lii), filenames)

failrepeat = 0
failmapq = 0
failsameref = 0
total = 0
passed = 0
insertNotRF = []
insertRF = []


for read1,read2 in izip(*filt_fits):
    total += 1
    if not names_eq(name(read1), name(read2)):
        sys.exit("Error: %s not equal to %s\n" % (name(read1),name(read2)))
    
    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