# Path: seqcluster/libs/classes.py
# class quality:
# 
#     def __init__(self, q):
#         self.qual = [ord(value) for value in q]
#         self.times = 1
# 
#     def update(self, q, counts = 1):
#         now = self.qual
#         q = [ord(value) for value in q]
#         self.qual = [x + y for x, y in zip(now, q)]
#         self.times += counts
# 
#     def get(self):
#         average = np.array(self.qual)/self.times
#         return [str(unichr(int(char))) for char in average]
# 
# class umi:
# 
#     def __init__(self, seq):
#         self.seq = defaultdict(list)
#         self.times = 0
#         self.update(seq)
# 
#     def update(self, seq, counts = 1):
#         for pos, nt in enumerate(seq):
#             self.seq[pos].append(nt)
#         self.times += counts
# 
#     def get(self):
#         seq = ""
#         for pos in sorted(self.seq.keys()):
#             lst = self.seq[pos]
#             seq += max(lst, key=lst.count)
#         return seq


import os
import gzip
import re
import logging
from collections import Counter, defaultdict
from seqcluster.libs.classes import quality, umi
from itertools import product
from __future__ import print_function


logger = logging.getLogger('seqbuster')

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:

# Path: seqcluster/libs/classes.py
# class quality:
# 
#     def __init__(self, q):
#         self.qual = [ord(value) for value in q]
#         self.times = 1
# 
#     def update(self, q, counts = 1):
#         now = self.qual
#         q = [ord(value) for value in q]
#         self.qual = [x + y for x, y in zip(now, q)]
#         self.times += counts
# 
#     def get(self):
#         average = np.array(self.qual)/self.times
#         return [str(unichr(int(char))) for char in average]
# 
# class umi:
# 
#     def __init__(self, seq):
#         self.seq = defaultdict(list)
#         self.times = 0
#         self.update(seq)
# 
#     def update(self, seq, counts = 1):
#         for pos, nt in enumerate(seq):
#             self.seq[pos].append(nt)
#         self.times += counts
# 
#     def get(self):
#         seq = ""
#         for pos in sorted(self.seq.keys()):
#             lst = self.seq[pos]
#             seq += max(lst, key=lst.count)
#         return seq


# Path: seqcluster/libs/fastq.py
import os
import gzip
import re
import logging
from collections import Counter, defaultdict
from seqcluster.libs.classes import quality, umi
from itertools import product

from __future__ import print_function


logger = logging.getLogger('seqbuster')

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)))