# Path: nucio.py
# def typeify(mylist, mytypes):
#     return map( lambda (t,e): t(e), izip(mytypes, mylist))
# 
# 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: seqio.py
# def iteratorFromExtension(filename):
#     '''
#     Get a sequence file iterator 
#     Based on the file's extension
#     '''
#     ext = filename.split(".")[-1]
#     if ext in ["fasta", "fa"]:
#         return fastaIterator
#     elif ext in ["fastq", "fq", "txt"]:
#         return fastqIterator
#     raise Exception, "Unknown file extension %s for file %s" % (ext,filename)
# 
# def recordToString(record):
#     f = fastqRecordToString if hasattr(record,"desc") else fastaRecordToString
#     return f(record)
# 
# def seqlen(record):
#     '''Gets the sequence length for a seqio record'''
#     return len(record.seq)


import sys
from nucio import typeify, fileIterator
from seqio import iteratorFromExtension, recordToString, seqlen
#!/usr/bin/env python

#Downsample a library




if not len(sys.argv) == 5:
    sys.exit("Usage: downsample.py genome_size desired_cov input.{fa,fq} output.{fa,fq}\n")


types = [int, float, str, str]
sysins = sys.argv[1:len(types)+1]
(genome_size, target_cov, infn, outfn) =  typeify(sysins,types)

max_bases = genome_size * target_cov 
total_bases = 0

with open(outfn, "w") as of:
    for record in fileIterator(infn,iteratorFromExtension(infn)):
        length = seqlen(record)
        if "N" in record.seq:
            continue

# Path: nucio.py
# def typeify(mylist, mytypes):
#     return map( lambda (t,e): t(e), izip(mytypes, mylist))
# 
# 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: seqio.py
# def iteratorFromExtension(filename):
#     '''
#     Get a sequence file iterator 
#     Based on the file's extension
#     '''
#     ext = filename.split(".")[-1]
#     if ext in ["fasta", "fa"]:
#         return fastaIterator
#     elif ext in ["fastq", "fq", "txt"]:
#         return fastqIterator
#     raise Exception, "Unknown file extension %s for file %s" % (ext,filename)
# 
# def recordToString(record):
#     f = fastqRecordToString if hasattr(record,"desc") else fastaRecordToString
#     return f(record)
# 
# def seqlen(record):
#     '''Gets the sequence length for a seqio record'''
#     return len(record.seq)


# Path: downsample.py
import sys
from nucio import typeify, fileIterator
from seqio import iteratorFromExtension, recordToString, seqlen

#!/usr/bin/env python

#Downsample a library




if not len(sys.argv) == 5:
    sys.exit("Usage: downsample.py genome_size desired_cov input.{fa,fq} output.{fa,fq}\n")


types = [int, float, str, str]
sysins = sys.argv[1:len(types)+1]
(genome_size, target_cov, infn, outfn) =  typeify(sysins,types)

max_bases = genome_size * target_cov 
total_bases = 0

with open(outfn, "w") as of:
    for record in fileIterator(infn,iteratorFromExtension(infn)):
        length = seqlen(record)
        if "N" in record.seq:
            continue

        if total_bases > max_bases: