# Path: seqcluster/libs/classes.py
# class sequence_unique:
#     """
#     Object to store the sequence information like: **counts**, **sequence**, **id**
#     """
#     def __init__(self, idx, seq):
#         self.idx = idx
#         self.seq = seq
#         self.group = {}
#         self.quality = ""
#         self.total = 0
#     def add_exp(self,gr,exp):
#         """Function to add the counts for each sample
# 
#         :param gr: name of the sample
#         :param exp: counts of sample **gr**
# 
#         :returns: dict with key,values equally to name,counts.
#         """
#         self.group[gr] = exp
#         self.total = sum(self.group.values())
# 
# 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]
# 
# Path: seqcluster/libs/fastq.py
# def is_fastq(in_file):
#     """copy from bcbio package"""
#     fastq_ends = [".txt", ".fq", ".fastq"]
#     zip_ends = [".gzip", ".gz"]
#     base, first_ext = os.path.splitext(in_file)
#     second_ext = os.path.splitext(base)[1]
#     if first_ext in fastq_ends:
#         return True
#     elif (second_ext, first_ext) in product(fastq_ends, zip_ends):
#         return True
#     else:
#         return False
# 
# 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 os.path as op
import traceback
import re
import logging
from seqcluster.libs.classes import sequence_unique
from seqcluster.libs.classes import quality
from seqcluster.libs.fastq import is_fastq, open_fastq
    return seq_l, sample_l


def _create_matrix_uniq_seq(sample_l, seq_l, maout, out, min_shared):
    """ create matrix counts for each different sequence in all the fasta files

    :param sample_l: :code:`list_s` is the output of :code:`_read_fasta_files`
    :param seq_l: :code:`seq_s` is the output of :code:`_read_fasta_files`
    :param maout: is a file handler to write the matrix count information
    :param out: is a file handle to write the fasta file with unique sequences

    :returns: Null
    """
    skip = 0
    if int(min_shared) > len(sample_l):
        min_shared = len(sample_l)
    maout.write("id\tseq")
    for g in sample_l:
        maout.write("\t%s" % g)
    for s in seq_l.keys():
        seen = sum([1 for g in seq_l[s].group if seq_l[s].group[g] > 0])
        if seen < int(min_shared):
            skip += 1
            continue
        maout.write("\nseq_%s\t%s" % (seq_l[s].idx, seq_l[s].seq))
        for g in sample_l:
            if g in seq_l[s].group:
                maout.write("\t%s" % seq_l[s].group[g])
            else:
                maout.write("\t0")

# Path: seqcluster/libs/classes.py
# class sequence_unique:
#     """
#     Object to store the sequence information like: **counts**, **sequence**, **id**
#     """
#     def __init__(self, idx, seq):
#         self.idx = idx
#         self.seq = seq
#         self.group = {}
#         self.quality = ""
#         self.total = 0
#     def add_exp(self,gr,exp):
#         """Function to add the counts for each sample
# 
#         :param gr: name of the sample
#         :param exp: counts of sample **gr**
# 
#         :returns: dict with key,values equally to name,counts.
#         """
#         self.group[gr] = exp
#         self.total = sum(self.group.values())
# 
# 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]
# 
# Path: seqcluster/libs/fastq.py
# def is_fastq(in_file):
#     """copy from bcbio package"""
#     fastq_ends = [".txt", ".fq", ".fastq"]
#     zip_ends = [".gzip", ".gz"]
#     base, first_ext = os.path.splitext(in_file)
#     second_ext = os.path.splitext(base)[1]
#     if first_ext in fastq_ends:
#         return True
#     elif (second_ext, first_ext) in product(fastq_ends, zip_ends):
#         return True
#     else:
#         return False
# 
# 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/prepare_data.py
import os
import os.path as op
import traceback
import re
import logging
from seqcluster.libs.classes import sequence_unique
from seqcluster.libs.classes import quality
from seqcluster.libs.fastq import is_fastq, open_fastq

    return seq_l, sample_l


def _create_matrix_uniq_seq(sample_l, seq_l, maout, out, min_shared):
    """ create matrix counts for each different sequence in all the fasta files

    :param sample_l: :code:`list_s` is the output of :code:`_read_fasta_files`
    :param seq_l: :code:`seq_s` is the output of :code:`_read_fasta_files`
    :param maout: is a file handler to write the matrix count information
    :param out: is a file handle to write the fasta file with unique sequences

    :returns: Null
    """
    skip = 0
    if int(min_shared) > len(sample_l):
        min_shared = len(sample_l)
    maout.write("id\tseq")
    for g in sample_l:
        maout.write("\t%s" % g)
    for s in seq_l.keys():
        seen = sum([1 for g in seq_l[s].group if seq_l[s].group[g] > 0])
        if seen < int(min_shared):
            skip += 1
            continue
        maout.write("\nseq_%s\t%s" % (seq_l[s].idx, seq_l[s].seq))
        for g in sample_l:
            if g in seq_l[s].group:
                maout.write("\t%s" % seq_l[s].group[g])
            else:
                maout.write("\t0")

        qual = "".join(seq_l[s].quality)