# Path: scripts/demultiplexer.py
# def demultiplex(left_fastq, right_fastq, barcode_files):
#     fastq_pairs = itertools.izip(
#         FastqStanza.stanza_generator(left_fastq), FastqStanza.stanza_generator(right_fastq))
#     for (left_stanza, right_stanza) in fastq_pairs:
#         assert_sequence_ids_match(left_stanza, right_stanza)
#         barcode = left_stanza.sequence_id.split(':')[0][1:]
#         try:
#             (left_out, right_out) = barcode_files[barcode]
#         except KeyError:
#             raise UndefinedBarcodeError(left_stanza)
#         left_out.write(left_stanza.as_text())
#         right_out.write(right_stanza.as_text())
# 
# class MismatchedSequenceIdError(DemultiplexError):
#     def __init__(self, left_stanza, right_stanza):
#         super(MismatchedSequenceIdError, self).__init__()
#         self.left_sequence_id = left_stanza.sequence_id
#         self.right_sequence_id = right_stanza.sequence_id
# 
#     def __str__(self):
#         return repr("left/right sequence_ids do not match [{0}] != [{1}].". \
#             format(self.left_sequence_id), self.right_sequence_id)
# 
# class UndefinedBarcodeError(DemultiplexError):
#     def __init__(self, stanza):
#         super(UndefinedBarcodeError, self).__init__()
#         self.sequence_id = stanza.sequence_id
#     
#     def __str__(self):
#         return repr("sequence_id [{0}] is not in recognized barcodes." \
#             .format(self.sequence_id))


import unittest
from scripts.demultiplexer import demultiplex, MismatchedSequenceIdError, UndefinedBarcodeError
    def test_demultiplex_singleton_identity(self):
        left_stanza = "@CAT:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza = "@CAT:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        source_left = MockReader(left_stanza)
        source_right = MockReader(right_stanza)
        dest_left = MockWriter()
        dest_right = MockWriter()
        barcode_files = {'CAT' : [dest_left, dest_right] }

        demultiplex(source_left, source_right, barcode_files)

        self.assertEqual(left_stanza.splitlines(), dest_left.lines())
        self.assertEqual(right_stanza.splitlines(), dest_right.lines())


    def test_demultiplex(self):
        left_stanza1 = "@AAACCC:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza1 = "@AAACCC:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        left_stanza2 = "@GGGTTT:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza2 = "@GGGTTT:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        source_left = MockReader(left_stanza1 + "\n" + left_stanza2)
        source_right = MockReader(right_stanza1 + "\n" + right_stanza2)
        left_barcodeA = MockWriter()
        right_barcodeA = MockWriter()
        left_barcodeB = MockWriter()
        right_barcodeB = MockWriter()
        barcode_files = { \
            'AAACCC' : [left_barcodeA, right_barcodeA], \
            'GGGTTT' : [left_barcodeB, right_barcodeB] }


# Path: scripts/demultiplexer.py
# def demultiplex(left_fastq, right_fastq, barcode_files):
#     fastq_pairs = itertools.izip(
#         FastqStanza.stanza_generator(left_fastq), FastqStanza.stanza_generator(right_fastq))
#     for (left_stanza, right_stanza) in fastq_pairs:
#         assert_sequence_ids_match(left_stanza, right_stanza)
#         barcode = left_stanza.sequence_id.split(':')[0][1:]
#         try:
#             (left_out, right_out) = barcode_files[barcode]
#         except KeyError:
#             raise UndefinedBarcodeError(left_stanza)
#         left_out.write(left_stanza.as_text())
#         right_out.write(right_stanza.as_text())
# 
# class MismatchedSequenceIdError(DemultiplexError):
#     def __init__(self, left_stanza, right_stanza):
#         super(MismatchedSequenceIdError, self).__init__()
#         self.left_sequence_id = left_stanza.sequence_id
#         self.right_sequence_id = right_stanza.sequence_id
# 
#     def __str__(self):
#         return repr("left/right sequence_ids do not match [{0}] != [{1}].". \
#             format(self.left_sequence_id), self.right_sequence_id)
# 
# class UndefinedBarcodeError(DemultiplexError):
#     def __init__(self, stanza):
#         super(UndefinedBarcodeError, self).__init__()
#         self.sequence_id = stanza.sequence_id
#     
#     def __str__(self):
#         return repr("sequence_id [{0}] is not in recognized barcodes." \
#             .format(self.sequence_id))


# Path: test/demultiplexer_test.py
import unittest
from scripts.demultiplexer import demultiplex, MismatchedSequenceIdError, UndefinedBarcodeError

    def test_demultiplex_singleton_identity(self):
        left_stanza = "@CAT:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza = "@CAT:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        source_left = MockReader(left_stanza)
        source_right = MockReader(right_stanza)
        dest_left = MockWriter()
        dest_right = MockWriter()
        barcode_files = {'CAT' : [dest_left, dest_right] }

        demultiplex(source_left, source_right, barcode_files)

        self.assertEqual(left_stanza.splitlines(), dest_left.lines())
        self.assertEqual(right_stanza.splitlines(), dest_right.lines())


    def test_demultiplex(self):
        left_stanza1 = "@AAACCC:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza1 = "@AAACCC:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        left_stanza2 = "@GGGTTT:DUPE_3:ID_1047:FLAG_1 1\nACGTACGT\n+\nAAAAAAAA"
        right_stanza2 = "@GGGTTT:DUPE_3:ID_1047:FLAG_1 2\nACGTACGT\n+\nAAAAAAAA"
        source_left = MockReader(left_stanza1 + "\n" + left_stanza2)
        source_right = MockReader(right_stanza1 + "\n" + right_stanza2)
        left_barcodeA = MockWriter()
        right_barcodeA = MockWriter()
        left_barcodeB = MockWriter()
        right_barcodeB = MockWriter()
        barcode_files = { \
            'AAACCC' : [left_barcodeA, right_barcodeA], \
            'GGGTTT' : [left_barcodeB, right_barcodeB] }


        demultiplex(source_left, source_right, barcode_files)