# 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
            cols = line1.split("\t")
            # if not is_fastq(cols[0]):
            #    raise ValueError("file is not fastq: %s" % cols[0])
            with open_fastq(cols[0]) as handle:
                sample_l.append(cols[1])
                total = added = 0
                line = handle.readline()
                while line:
                    if line.startswith("@") or line.startswith(">"):
                        seq = handle.readline().strip()
                        if not p.match(seq):
                            continue
                        idx += 1
                        total += 1
                        keep = {}
                        counts = int(re.search("x([0-9]+)", line.strip()).group(1))
                        if is_fastq(cols[0]):
                            handle.readline().strip()
                            qual = handle.readline().strip()
                        else:
                            qual = "I" * len(seq)
                        qual = qual[0:int(args.maxl)] if len(qual) > int(args.maxl) else qual
                        seq = seq[0:int(args.maxl)] if len(seq) > int(args.maxl) else seq
                        if counts > int(args.minc) and len(seq) > int(args.minl):
                            added += 1
                            if seq in keep:
                                keep[seq].update(qual)
                            else:
                                keep[seq] = quality(qual)
                            if seq not in seq_l:

# 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

            cols = line1.split("\t")
            # if not is_fastq(cols[0]):
            #    raise ValueError("file is not fastq: %s" % cols[0])
            with open_fastq(cols[0]) as handle:
                sample_l.append(cols[1])
                total = added = 0
                line = handle.readline()
                while line:
                    if line.startswith("@") or line.startswith(">"):
                        seq = handle.readline().strip()
                        if not p.match(seq):
                            continue
                        idx += 1
                        total += 1
                        keep = {}
                        counts = int(re.search("x([0-9]+)", line.strip()).group(1))
                        if is_fastq(cols[0]):
                            handle.readline().strip()
                            qual = handle.readline().strip()
                        else:
                            qual = "I" * len(seq)
                        qual = qual[0:int(args.maxl)] if len(qual) > int(args.maxl) else qual
                        seq = seq[0:int(args.maxl)] if len(seq) > int(args.maxl) else seq
                        if counts > int(args.minc) and len(seq) > int(args.minl):
                            added += 1
                            if seq in keep:
                                keep[seq].update(qual)
                            else:
                                keep[seq] = quality(qual)
                            if seq not in seq_l:

                                seq_l[seq] = sequence_unique(idx, seq)