# Path: circlator/mapping.py
# class Error (Exception): pass
# def bwa_index(infile, outprefix=None, bwa=None, verbose=False):
# def bwa_index_clean(prefix):
# def bwa_mem(
#       ref,
#       reads,
#       outfile,
#       threads=1,
#       bwa_options = '-x pacbio',
#       verbose=False,
#       index=None
#     ):
# def aligned_read_to_read(read, revcomp=True, qual=None, ignore_quality=False):


import unittest
import copy
import filecmp
import os
import pysam
import pyfastaq
from circlator import mapping
            '2:6:214:330',
        ]

        sam_reader = pysam.Samfile(outfile, "rb")
        got_reads = []

        for read in sam_reader.fetch():
            got_reads.append(read.qname)
            self.assertFalse(read.is_unmapped)

        self.assertEqual(expected_reads, got_reads)
        os.unlink(outfile)
        os.unlink(outfile + '.bai')


    def test_aligned_read_to_read(self):
        '''test aligned_read_to_read'''
        infile = os.path.join(data_dir, 'mapping_test_aligned_read_to_read.bam')
        sam_reader = pysam.Samfile(infile, "rb")
        aln1, aln2 = [x for x in sam_reader.fetch()]
        read1_fq = pyfastaq.sequences.Fastq('read1', 'TGTGTAACACTCCACCTCTGGTTCCCAGAGTTCGGTATCCGGCCGATACTTGAGGATAGC', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIGHFEDCBA')
        read1_fa = pyfastaq.sequences.Fasta('read1', 'TGTGTAACACTCCACCTCTGGTTCCCAGAGTTCGGTATCCGGCCGATACTTGAGGATAGC')
        self.assertEqual(read1_fq, mapping.aligned_read_to_read(aln1))
        self.assertEqual(read1_fq, mapping.aligned_read_to_read(aln1, revcomp=False))
        self.assertEqual(read1_fa, mapping.aligned_read_to_read(aln1, ignore_quality=True))

        read2 = pyfastaq.sequences.Fastq('read2', 'GATCGTCACGAAAGAACCAAGCCGGATCGTGGGAGGGGTACAACTCAGGTGAATTAACGT', 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHGFEDC')
        read2_rev = copy.copy(read2)
        read2_rev.revcomp()
        self.assertEqual(read2, mapping.aligned_read_to_read(aln2))

# Path: circlator/mapping.py
# class Error (Exception): pass
# def bwa_index(infile, outprefix=None, bwa=None, verbose=False):
# def bwa_index_clean(prefix):
# def bwa_mem(
#       ref,
#       reads,
#       outfile,
#       threads=1,
#       bwa_options = '-x pacbio',
#       verbose=False,
#       index=None
#     ):
# def aligned_read_to_read(read, revcomp=True, qual=None, ignore_quality=False):


# Path: circlator/tests/mapping_test.py
import unittest
import copy
import filecmp
import os
import pysam
import pyfastaq
from circlator import mapping

            '2:6:214:330',
        ]

        sam_reader = pysam.Samfile(outfile, "rb")
        got_reads = []

        for read in sam_reader.fetch():
            got_reads.append(read.qname)
            self.assertFalse(read.is_unmapped)

        self.assertEqual(expected_reads, got_reads)
        os.unlink(outfile)
        os.unlink(outfile + '.bai')


    def test_aligned_read_to_read(self):
        '''test aligned_read_to_read'''
        infile = os.path.join(data_dir, 'mapping_test_aligned_read_to_read.bam')
        sam_reader = pysam.Samfile(infile, "rb")
        aln1, aln2 = [x for x in sam_reader.fetch()]
        read1_fq = pyfastaq.sequences.Fastq('read1', 'TGTGTAACACTCCACCTCTGGTTCCCAGAGTTCGGTATCCGGCCGATACTTGAGGATAGC', 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIGHFEDCBA')
        read1_fa = pyfastaq.sequences.Fasta('read1', 'TGTGTAACACTCCACCTCTGGTTCCCAGAGTTCGGTATCCGGCCGATACTTGAGGATAGC')
        self.assertEqual(read1_fq, mapping.aligned_read_to_read(aln1))
        self.assertEqual(read1_fq, mapping.aligned_read_to_read(aln1, revcomp=False))
        self.assertEqual(read1_fa, mapping.aligned_read_to_read(aln1, ignore_quality=True))

        read2 = pyfastaq.sequences.Fastq('read2', 'GATCGTCACGAAAGAACCAAGCCGGATCGTGGGAGGGGTACAACTCAGGTGAATTAACGT', 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHGFEDC')
        read2_rev = copy.copy(read2)
        read2_rev.revcomp()
        self.assertEqual(read2, mapping.aligned_read_to_read(aln2))

        self.assertEqual(read2_rev, mapping.aligned_read_to_read(aln2, revcomp=False))