# 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
def _read_fasta_files(f, args):
    """ read fasta files of each sample and generate a seq_obj
    with the information of each unique sequence in each sample

    :param f: file containing the path for each fasta file and
    the name of the sample. Two column format with `tab` as field
    separator

    :returns: * :code:`seq_l`: is a list of seq_obj objects, containing
               the information of each sequence
             * :code:`sample_l`: is a list with the name of the samples
               (column two of the config file)
    """
    seq_l = {}
    sample_l = []
    idx = 1
    for line1 in f:
        line1 = line1.strip()
        cols = line1.split("\t")
        with open(cols[0], 'r') as fasta:
            sample_l.append(cols[1])
            for line in fasta:
                if line.startswith(">"):
                    idx += 1
                    counts = int(re.search("x([0-9]+)", line.strip()).group(1))
                else:
                    seq = line.strip()
                    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):
                        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

def _read_fasta_files(f, args):
    """ read fasta files of each sample and generate a seq_obj
    with the information of each unique sequence in each sample

    :param f: file containing the path for each fasta file and
    the name of the sample. Two column format with `tab` as field
    separator

    :returns: * :code:`seq_l`: is a list of seq_obj objects, containing
               the information of each sequence
             * :code:`sample_l`: is a list with the name of the samples
               (column two of the config file)
    """
    seq_l = {}
    sample_l = []
    idx = 1
    for line1 in f:
        line1 = line1.strip()
        cols = line1.split("\t")
        with open(cols[0], 'r') as fasta:
            sample_l.append(cols[1])
            for line in fasta:
                if line.startswith(">"):
                    idx += 1
                    counts = int(re.search("x([0-9]+)", line.strip()).group(1))
                else:
                    seq = line.strip()
                    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):
                        if seq not in seq_l:

                            seq_l[seq] = sequence_unique(idx, seq)