# Path: nanopore/metaAnalyses/abstractMetaAnalysis.py
# class AbstractMetaAnalysis(Target):
#     """Base class to for meta-analysis targets. Inherit this class to create a meta-analysis.
#     """
#     def __init__(self, outputDir, experiments):
#         Target.__init__(self)
#         self.experiments = experiments
#         self.outputDir = outputDir
#         
#         #Quadruples of (readFastqFile, readType, referenceFastaFile, mapper) to pairs of (analyses, resultsDir)
#         self.experimentHash = {}
#         #Mappers
#         self.mappers = set()
#         #Read file readType double
#         self.readFastqFiles = set()
#         #Reference files
#         self.referenceFastaFiles = set()
#         #readTypes
#         self.readTypes = set()
#         #base mappers (lastz, last, bwa, blasr)
#         self.baseMappers = set()
#         
#         #Store all this stuff
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)] = (analyses, resultsDir)
#             self.mappers.add(mapper)
#             self.readFastqFiles.add((readFastqFile, readType))
#             self.referenceFastaFiles.add(referenceFastaFile)
#             self.readTypes.add(readType)
#             self.baseMappers.add(re.findall("[A-Z][a-z]*", mapper.__name__)[0])
# 
# Path: nanopore/analyses/utils.py
# def samIterator(sam):
#     """Creates an iterator over the aligned reads in a sam file, filtering out
#     any reads that have no reference alignment.
#     """
#     for aR in sam:
#         if aR.rname != -1:
#             yield aR


from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from jobTree.src.bioio import system, fastqRead
from nanopore.analyses.utils import samIterator
import os, sys
import xml.etree.cElementTree as ET
import pysam

class Read():
    """stores a individual read and everything about it"""
    def __init__(self, name, seq, readType, readFastqFile, mapRefPairs):
        self.seq = seq
        self.name = name
        self.readType = readType
        self.readFastqFile = readFastqFile
        self.mapRefPairs = mapRefPairs

        if mapRefPairs is not None:
            self.is_mapped = True
            self.mappers, self.references = set(mapRefPairs[0]), set(mapRefPairs[1])
        else:
            self.is_mapped = False
            self.mappers, self.references = None, None

    def get_map_ref_pair(self):
        if self.mapRefPairs is not None:
            for mapper, reference in zip(self.mapRefPairs[0], self.mapRefPairs[1]):
                yield (mapper, reference)

class AbstractUnmappedMetaAnalysis(AbstractMetaAnalysis):
    """Builds a database of reads and the information gathered about them during analysis"""
    def __init__(self, outputDir, experiments):

# Path: nanopore/metaAnalyses/abstractMetaAnalysis.py
# class AbstractMetaAnalysis(Target):
#     """Base class to for meta-analysis targets. Inherit this class to create a meta-analysis.
#     """
#     def __init__(self, outputDir, experiments):
#         Target.__init__(self)
#         self.experiments = experiments
#         self.outputDir = outputDir
#         
#         #Quadruples of (readFastqFile, readType, referenceFastaFile, mapper) to pairs of (analyses, resultsDir)
#         self.experimentHash = {}
#         #Mappers
#         self.mappers = set()
#         #Read file readType double
#         self.readFastqFiles = set()
#         #Reference files
#         self.referenceFastaFiles = set()
#         #readTypes
#         self.readTypes = set()
#         #base mappers (lastz, last, bwa, blasr)
#         self.baseMappers = set()
#         
#         #Store all this stuff
#         for readFastqFile, readType, referenceFastaFile, mapper, analyses, resultsDir in self.experiments:
#             self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)] = (analyses, resultsDir)
#             self.mappers.add(mapper)
#             self.readFastqFiles.add((readFastqFile, readType))
#             self.referenceFastaFiles.add(referenceFastaFile)
#             self.readTypes.add(readType)
#             self.baseMappers.add(re.findall("[A-Z][a-z]*", mapper.__name__)[0])
# 
# Path: nanopore/analyses/utils.py
# def samIterator(sam):
#     """Creates an iterator over the aligned reads in a sam file, filtering out
#     any reads that have no reference alignment.
#     """
#     for aR in sam:
#         if aR.rname != -1:
#             yield aR


# Path: nanopore/metaAnalyses/abstractUnmappedAnalysis.py
from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from jobTree.src.bioio import system, fastqRead
from nanopore.analyses.utils import samIterator
import os, sys
import xml.etree.cElementTree as ET
import pysam


class Read():
    """stores a individual read and everything about it"""
    def __init__(self, name, seq, readType, readFastqFile, mapRefPairs):
        self.seq = seq
        self.name = name
        self.readType = readType
        self.readFastqFile = readFastqFile
        self.mapRefPairs = mapRefPairs

        if mapRefPairs is not None:
            self.is_mapped = True
            self.mappers, self.references = set(mapRefPairs[0]), set(mapRefPairs[1])
        else:
            self.is_mapped = False
            self.mappers, self.references = None, None

    def get_map_ref_pair(self):
        if self.mapRefPairs is not None:
            for mapper, reference in zip(self.mapRefPairs[0], self.mapRefPairs[1]):
                yield (mapper, reference)

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)