# 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 fastaRecordToString(record):
#     return "\n".join([">"+record.name,record.seq])
# 
# 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 openerFromExtension(filename, default=None):
#     '''Get an open function from a file's
#     extension
#     default is the default opener, None by default
#     returns:
#        (open_func, filename_with_extension_removed)
#     '''
#     arr = filename.split(".")
#     
#     if arr[-1] == "gz":
#         import gzip
#         return (gzip.open, ".".join(arr[:-1]))
# 
#     return (default, filename)


import sys
from itertools import starmap, chain
from functools import partial
from seqio import iteratorFromExtension, fastaRecordToString
from nucio import fileIterator, openerFromExtension

###
#Takes a bunch of seq files and outputs
#only unique reads based on their names
# (only up to first space in their names)
#everything becomes fasta format
###




if not len(sys.argv) > 1:
    sys.exit("uniqreads.py in.{fa,fq} [in2.{fa.fq} ...]\n")

files = sys.argv[1:]

(openers,nfiles) = zip(*map(partial(openerFromExtension, default=open), files))

its = map(iteratorFromExtension, nfiles)

fileits = starmap(fileIterator, zip(files, its, openers))

namehash = {}
for record in chain.from_iterable(fileits):
    cleanname = record.name.split()[0]
    if not namehash.get(cleanname, False):
        namehash[cleanname] = True
        nr = record._replace(name=cleanname)
        print fastaRecordToString(nr)
    else:

# 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 fastaRecordToString(record):
#     return "\n".join([">"+record.name,record.seq])
# 
# 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 openerFromExtension(filename, default=None):
#     '''Get an open function from a file's
#     extension
#     default is the default opener, None by default
#     returns:
#        (open_func, filename_with_extension_removed)
#     '''
#     arr = filename.split(".")
#     
#     if arr[-1] == "gz":
#         import gzip
#         return (gzip.open, ".".join(arr[:-1]))
# 
#     return (default, filename)


# Path: uniqreads.py
import sys
from itertools import starmap, chain
from functools import partial
from seqio import iteratorFromExtension, fastaRecordToString
from nucio import fileIterator, openerFromExtension


###
#Takes a bunch of seq files and outputs
#only unique reads based on their names
# (only up to first space in their names)
#everything becomes fasta format
###




if not len(sys.argv) > 1:
    sys.exit("uniqreads.py in.{fa,fq} [in2.{fa.fq} ...]\n")

files = sys.argv[1:]

(openers,nfiles) = zip(*map(partial(openerFromExtension, default=open), files))

its = map(iteratorFromExtension, nfiles)

fileits = starmap(fileIterator, zip(files, its, openers))

namehash = {}
for record in chain.from_iterable(fileits):
    cleanname = record.name.split()[0]
    if not namehash.get(cleanname, False):
        namehash[cleanname] = True
        nr = record._replace(name=cleanname)
        print fastaRecordToString(nr)
    else:

        sys.stderr.write("Duplicate : %s \n" % cleanname)