# Path: seqcluster/prepare_data.py
# def _read_fastq_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
#     p = re.compile("^[ATCGNU]+$")
#     with open(op.join(args.out, "stats_prepare.tsv"), 'w') as out_handle:
#         for line1 in f:
#             line1 = line1.strip()
#             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)
#                             seq_l[seq].add_exp(cols[1], counts)
#                             seq_l[seq].quality = keep[seq].get()
#                     line=handle.readline()
#                 print("total\t%s\t%s" % (idx, cols[1]), file=out_handle, end="")
#                 print("added\t%s\t%s" % (len(seq_l), cols[1]), file=out_handle, end="")
#                 logger.info("%s: Total read %s ; Total added %s" % (cols[1], idx, len(seq_l)))
#     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)
#         out.write("@seq_%s\n%s\n+\n%s\n" % (seq_l[s].idx, seq_l[s].seq, qual))
#     out.close()
#     maout.close()
#     logger.info("Total skipped due to --min-shared parameter (%s) : %s" % (min_shared, skip))


from unittest import TestCase
from collections import namedtuple
from seqcluster.prepare_data import _read_fastq_files, _create_matrix_uniq_seq
from nose.plugins.attrib import attr
        from seqcluster.libs.fastq import collapse, write_output
import os
import shutil
import inspect
import seqcluster


class TestPreparedata(TestCase):
    @attr(collapse=True)
    def test_preparedata(self):
        out_dir = "test/test_out_prepare"
        if os.path.exists(out_dir):
            shutil.rmtree(out_dir)
        os.mkdir(out_dir)
        arg = namedtuple('args', 'minl maxl minc out')
        args = arg(15, 40, 1, out_dir)

# Path: seqcluster/prepare_data.py
# def _read_fastq_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
#     p = re.compile("^[ATCGNU]+$")
#     with open(op.join(args.out, "stats_prepare.tsv"), 'w') as out_handle:
#         for line1 in f:
#             line1 = line1.strip()
#             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)
#                             seq_l[seq].add_exp(cols[1], counts)
#                             seq_l[seq].quality = keep[seq].get()
#                     line=handle.readline()
#                 print("total\t%s\t%s" % (idx, cols[1]), file=out_handle, end="")
#                 print("added\t%s\t%s" % (len(seq_l), cols[1]), file=out_handle, end="")
#                 logger.info("%s: Total read %s ; Total added %s" % (cols[1], idx, len(seq_l)))
#     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)
#         out.write("@seq_%s\n%s\n+\n%s\n" % (seq_l[s].idx, seq_l[s].seq, qual))
#     out.close()
#     maout.close()
#     logger.info("Total skipped due to --min-shared parameter (%s) : %s" % (min_shared, skip))


# Path: test/test_preparedata.py
from unittest import TestCase
from collections import namedtuple
from seqcluster.prepare_data import _read_fastq_files, _create_matrix_uniq_seq
from nose.plugins.attrib import attr
        from seqcluster.libs.fastq import collapse, write_output
import os
import shutil
import inspect
import seqcluster



class TestPreparedata(TestCase):
    @attr(collapse=True)
    def test_preparedata(self):
        out_dir = "test/test_out_prepare"
        if os.path.exists(out_dir):
            shutil.rmtree(out_dir)
        os.mkdir(out_dir)
        arg = namedtuple('args', 'minl maxl minc out')
        args = arg(15, 40, 1, out_dir)

        seq_l, list_s = _read_fastq_files(open("data/examples/collapse/config"), args)