# 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 seqlen(record):
#     '''Gets the sequence length for a seqio record'''
#     return len(record.seq)
# 
# def recordToString(record):
#     f = fastqRecordToString if hasattr(record,"desc") else fastaRecordToString
#     return f(record)
# 
# 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


import sys
import os
from itertools import starmap, chain, ifilter, imap
from seqio import iteratorFromExtension, seqlen, recordToString
from nucio import fileIterator
from args import parseArgs, getHelpStr, CLArgument
#!/usr/bin/env python




description = ("Usage: length_filter.py [options] min_length(int) in1.{fa.fq} [in2.{fa,fq} ...]\n\n"
               "Filter reads by their lengths")

argument_list = [["maxlen", "maxlen", int, -1, "Maximum length of reads"]]

arguments = map(CLArgument._make, argument_list)

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

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


minlen = int(args_remaining[0])

files = args_remaining[1:]

if not all(map(os.path.exists,files)):
    sys.exit("Not all files exist")

file_readers = starmap(fileIterator, zip(files, map(iteratorFromExtension, files)))

filt_cond = lambda record : seqlen(record) > minlen
if p_arg_map["maxlen"] > 1:

# 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 seqlen(record):
#     '''Gets the sequence length for a seqio record'''
#     return len(record.seq)
# 
# def recordToString(record):
#     f = fastqRecordToString if hasattr(record,"desc") else fastaRecordToString
#     return f(record)
# 
# 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


# Path: length_filter.py
import sys
import os
from itertools import starmap, chain, ifilter, imap
from seqio import iteratorFromExtension, seqlen, recordToString
from nucio import fileIterator
from args import parseArgs, getHelpStr, CLArgument

#!/usr/bin/env python




description = ("Usage: length_filter.py [options] min_length(int) in1.{fa.fq} [in2.{fa,fq} ...]\n\n"
               "Filter reads by their lengths")

argument_list = [["maxlen", "maxlen", int, -1, "Maximum length of reads"]]

arguments = map(CLArgument._make, argument_list)

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

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


minlen = int(args_remaining[0])

files = args_remaining[1:]

if not all(map(os.path.exists,files)):
    sys.exit("Not all files exist")

file_readers = starmap(fileIterator, zip(files, map(iteratorFromExtension, files)))

filt_cond = lambda record : seqlen(record) > minlen
if p_arg_map["maxlen"] > 1:

    filt_cond = lambda record: seqlen(record) > minlen and seqlen(record) <= p_arg_map["maxlen"]