# Path: nanopore/metaAnalyses/abstractUnmappedAnalysis.py
# class AbstractUnmappedMetaAnalysis(AbstractMetaAnalysis):
#     """Builds a database of reads and the information gathered about them during analysis"""
#     def __init__(self, outputDir, experiments):
#         AbstractMetaAnalysis.__init__(self, outputDir, experiments)
#         
#         allReads = {(name, readFastqFile, readType, seq) for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir \
#             in self.experiments for name, seq, qual in fastqRead(readFastqFile)}
#         
#         mappedReads = dict()
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             for record in samIterator(pysam.Samfile(os.path.join(resultsDir, "mapping.sam"))):
#                 if not record.is_unmapped:
#                     if (record.qname, readFastqFile) not in mappedReads:
#                         mappedReads[(record.qname, readFastqFile)] = set()
#                     mappedReads[(record.qname, readFastqFile)].add((mapper.__name__, referenceFastaFile))
# 
#         self.reads = list()
#         for name, readFastqFile, readType, seq in allReads:
#             if (name, readFastqFile) in mappedReads:
#                 mappers, referenceFastaFiles = map(tuple, zip(*mappedReads[(name, readFastqFile)]))
#                 self.reads.append(Read(name, seq, readType, readFastqFile, (mappers, referenceFastaFiles)))
#             else:
#                 self.reads.append(Read(name, seq, readType, readFastqFile, None))


from nanopore.metaAnalyses.abstractUnmappedAnalysis import AbstractUnmappedMetaAnalysis
from jobTree.src.bioio import system
import os, sys

class UnmappedLengthDistributionAnalysis(AbstractUnmappedMetaAnalysis):
    """runs length distribution analysis on all mapped/unmapped per read Type
    as well as per reference"""
    def run(self):
        for readType in self.readTypes:
            unmapped = open(os.path.join(self.outputDir, readType + "_unmapped.txt"), "w")
            mapped = open(os.path.join(self.outputDir, readType + "_mapped.txt"), "w")
            for read in self.reads:

# Path: nanopore/metaAnalyses/abstractUnmappedAnalysis.py
# class AbstractUnmappedMetaAnalysis(AbstractMetaAnalysis):
#     """Builds a database of reads and the information gathered about them during analysis"""
#     def __init__(self, outputDir, experiments):
#         AbstractMetaAnalysis.__init__(self, outputDir, experiments)
#         
#         allReads = {(name, readFastqFile, readType, seq) for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir \
#             in self.experiments for name, seq, qual in fastqRead(readFastqFile)}
#         
#         mappedReads = dict()
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             for record in samIterator(pysam.Samfile(os.path.join(resultsDir, "mapping.sam"))):
#                 if not record.is_unmapped:
#                     if (record.qname, readFastqFile) not in mappedReads:
#                         mappedReads[(record.qname, readFastqFile)] = set()
#                     mappedReads[(record.qname, readFastqFile)].add((mapper.__name__, referenceFastaFile))
# 
#         self.reads = list()
#         for name, readFastqFile, readType, seq in allReads:
#             if (name, readFastqFile) in mappedReads:
#                 mappers, referenceFastaFiles = map(tuple, zip(*mappedReads[(name, readFastqFile)]))
#                 self.reads.append(Read(name, seq, readType, readFastqFile, (mappers, referenceFastaFiles)))
#             else:
#                 self.reads.append(Read(name, seq, readType, readFastqFile, None))


# Path: nanopore/metaAnalyses/unmappedLengthDistributionAnalysis.py
from nanopore.metaAnalyses.abstractUnmappedAnalysis import AbstractUnmappedMetaAnalysis
from jobTree.src.bioio import system
import os, sys


class UnmappedLengthDistributionAnalysis(AbstractUnmappedMetaAnalysis):
    """runs length distribution analysis on all mapped/unmapped per read Type
    as well as per reference"""
    def run(self):
        for readType in self.readTypes:
            unmapped = open(os.path.join(self.outputDir, readType + "_unmapped.txt"), "w")
            mapped = open(os.path.join(self.outputDir, readType + "_mapped.txt"), "w")
            for read in self.reads:

                if read.is_mapped is True and read.readType == readType: