# Path: seqcluster/libs/fastq.py
# def collapse(in_file):
#     """collapse identical sequences and keep Q"""
#     keep = Counter()
#     with open_fastq(in_file) as handle:
#         line = handle.readline()
#         while line:
#             if line.startswith("@"):
#                 if line.find("UMI") > -1:
#                     logger.info("Find UMI tags in read names, collapsing by UMI.")
#                     return collapse_umi(in_file)
#                 seq = handle.readline().strip()
#                 handle.readline()
#                 qual = handle.readline().strip()
#                 if seq in keep:
#                     keep[seq].update(qual)
#                 else:
#                     keep[seq] = quality(qual)
#             if line.startswith(">"):
#                 seq = handle.readline().strip()
#                 if seq not in keep:
#                     keep[seq] = quality("".join(["I"] * len(seq)))
#                 else:
#                     keep[seq].update("".join(["I"] * len(seq)))
#             line = handle.readline()
#     logger.info("Sequences loaded: %s" % len(keep))
#     return keep
# 
# def splitext_plus(f):
#     """Split on file extensions, allowing for zipped extensions.
#     copy from bcbio
#     """
#     base, ext = os.path.splitext(f)
#     if ext in [".gz", ".bz2", ".zip"]:
#         base, ext2 = os.path.splitext(base)
#         ext = ext2 + ext
#     return base, ext
# 
# def write_output(out_file, seqs, minimum=1, size=15):
#     idx =0
#     logger.info("Writing %s sequences to %s" % (len(seqs.keys()), out_file))
#     with open(out_file, 'w') as handle:
#         for s in seqs:
#             idx += 1
#             if isinstance(seqs[s], list):
#                 seq = seqs[s][0].get()
#                 qual = "".join(seqs[s][1].get())
#                 counts = seqs[s][0].times
#             else:
#                 seq = s
#                 qual = "".join(seqs[s].get())
#                 counts = seqs[s].times
#             if int(counts) >= minimum and len(seq) > size:
#                 handle.write(("@seq_{idx}_x{counts}\n{seq}\n+\n{qual}\n").format(**locals()))
#     return out_file
# 
# def open_fastq(in_file):
#     """ open a fastq file, using gzip if it is gzipped
#     from bcbio package
#     """
#     _, ext = os.path.splitext(in_file)
#     if ext == ".gz":
#         return gzip.open(in_file, 'rt')
#     if ext in [".fastq", ".fq", ".fasta", ".fa"]:
#         return open(in_file, 'r')
#     return ValueError("File needs to be fastq|fasta|fq|fa [.gz]")


import os
import logging
from seqcluster.libs.fastq import collapse, splitext_plus, write_output, open_fastq


logger = logging.getLogger('seqbuster')


def collapse_fastq(args):
    """collapse fasq files after adapter trimming
    """
    try:
        umi_fn = args.fastq
        if _is_umi(args.fastq):
            umis = collapse(args.fastq)
            umi_fn = os.path.join(args.out, splitext_plus(os.path.basename(args.fastq))[0] + "_umi_trimmed.fastq")
            write_output(umi_fn, umis, args.minimum)

# Path: seqcluster/libs/fastq.py
# def collapse(in_file):
#     """collapse identical sequences and keep Q"""
#     keep = Counter()
#     with open_fastq(in_file) as handle:
#         line = handle.readline()
#         while line:
#             if line.startswith("@"):
#                 if line.find("UMI") > -1:
#                     logger.info("Find UMI tags in read names, collapsing by UMI.")
#                     return collapse_umi(in_file)
#                 seq = handle.readline().strip()
#                 handle.readline()
#                 qual = handle.readline().strip()
#                 if seq in keep:
#                     keep[seq].update(qual)
#                 else:
#                     keep[seq] = quality(qual)
#             if line.startswith(">"):
#                 seq = handle.readline().strip()
#                 if seq not in keep:
#                     keep[seq] = quality("".join(["I"] * len(seq)))
#                 else:
#                     keep[seq].update("".join(["I"] * len(seq)))
#             line = handle.readline()
#     logger.info("Sequences loaded: %s" % len(keep))
#     return keep
# 
# def splitext_plus(f):
#     """Split on file extensions, allowing for zipped extensions.
#     copy from bcbio
#     """
#     base, ext = os.path.splitext(f)
#     if ext in [".gz", ".bz2", ".zip"]:
#         base, ext2 = os.path.splitext(base)
#         ext = ext2 + ext
#     return base, ext
# 
# def write_output(out_file, seqs, minimum=1, size=15):
#     idx =0
#     logger.info("Writing %s sequences to %s" % (len(seqs.keys()), out_file))
#     with open(out_file, 'w') as handle:
#         for s in seqs:
#             idx += 1
#             if isinstance(seqs[s], list):
#                 seq = seqs[s][0].get()
#                 qual = "".join(seqs[s][1].get())
#                 counts = seqs[s][0].times
#             else:
#                 seq = s
#                 qual = "".join(seqs[s].get())
#                 counts = seqs[s].times
#             if int(counts) >= minimum and len(seq) > size:
#                 handle.write(("@seq_{idx}_x{counts}\n{seq}\n+\n{qual}\n").format(**locals()))
#     return out_file
# 
# def open_fastq(in_file):
#     """ open a fastq file, using gzip if it is gzipped
#     from bcbio package
#     """
#     _, ext = os.path.splitext(in_file)
#     if ext == ".gz":
#         return gzip.open(in_file, 'rt')
#     if ext in [".fastq", ".fq", ".fasta", ".fa"]:
#         return open(in_file, 'r')
#     return ValueError("File needs to be fastq|fasta|fq|fa [.gz]")


# Path: seqcluster/collapse.py
import os
import logging
from seqcluster.libs.fastq import collapse, splitext_plus, write_output, open_fastq



logger = logging.getLogger('seqbuster')


def collapse_fastq(args):
    """collapse fasq files after adapter trimming
    """
    try:
        umi_fn = args.fastq
        if _is_umi(args.fastq):
            umis = collapse(args.fastq)
            umi_fn = os.path.join(args.out, splitext_plus(os.path.basename(args.fastq))[0] + "_umi_trimmed.fastq")
            write_output(umi_fn, umis, args.minimum)

        seqs = collapse(umi_fn)