# 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, fastaRead
from nanopore.analyses.utils import samIterator
from itertools import product
import os, sys
import xml.etree.cElementTree as ET
import numpy

class MarginAlignMetaAnalysis(AbstractMetaAnalysis):
    def run(self):
        readTypes = set([ readType for readFastqFile, readType in self.readFastqFiles ])
        coverageLevels = set()
        hash = {}
        variantCallingAlgorithms = set()
        proportionsHeldOut = set()
        for referenceFastaFile in self.referenceFastaFiles:
            for readType in readTypes:
                for readFastqFile, readFileReadType in self.readFastqFiles:
                    if readFileReadType == readType:
                        for mapper in self.mappers:
                            analyses, resultsDir = self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)]
                            try:
                                node = ET.parse(os.path.join(resultsDir, "analysis_MarginAlignSnpCaller", "marginaliseConsensus.xml")).getroot()
                            except:
                                self.logToMaster("Parsing the file following failed: %s" % os.path.join(resultsDir, "analysis_MarginAlignSnpCaller", "marginaliseConsensus.xml"))
                                continue
                            for c in node:
                                coverage = int(c.attrib["coverage"])
                                if coverage == 10:
                                    continue #Remove the dead coverage plot
                                if coverage > 1000:
                                    coverage = "ALL"
                                coverageLevels.add(coverage)
                                proportionHeldOut = float(c.attrib["totalHeldOut"]) / (float(c.attrib["totalHeldOut"]) + float(c.attrib["totalNonHeldOut"]))
                                if proportionHeldOut == 0:
                                    continue
                                if proportionHeldOut < 0.04:

# 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/marginAlignMetaAnalysis.py
from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from jobTree.src.bioio import system, fastqRead, fastaRead
from nanopore.analyses.utils import samIterator
from itertools import product
import os, sys
import xml.etree.cElementTree as ET
import numpy


class MarginAlignMetaAnalysis(AbstractMetaAnalysis):
    def run(self):
        readTypes = set([ readType for readFastqFile, readType in self.readFastqFiles ])
        coverageLevels = set()
        hash = {}
        variantCallingAlgorithms = set()
        proportionsHeldOut = set()
        for referenceFastaFile in self.referenceFastaFiles:
            for readType in readTypes:
                for readFastqFile, readFileReadType in self.readFastqFiles:
                    if readFileReadType == readType:
                        for mapper in self.mappers:
                            analyses, resultsDir = self.experimentHash[((readFastqFile, readType), referenceFastaFile, mapper)]
                            try:
                                node = ET.parse(os.path.join(resultsDir, "analysis_MarginAlignSnpCaller", "marginaliseConsensus.xml")).getroot()
                            except:
                                self.logToMaster("Parsing the file following failed: %s" % os.path.join(resultsDir, "analysis_MarginAlignSnpCaller", "marginaliseConsensus.xml"))
                                continue
                            for c in node:
                                coverage = int(c.attrib["coverage"])
                                if coverage == 10:
                                    continue #Remove the dead coverage plot
                                if coverage > 1000:
                                    coverage = "ALL"
                                coverageLevels.add(coverage)
                                proportionHeldOut = float(c.attrib["totalHeldOut"]) / (float(c.attrib["totalHeldOut"]) + float(c.attrib["totalNonHeldOut"]))
                                if proportionHeldOut == 0:
                                    continue
                                if proportionHeldOut < 0.04:

                                    proportionHeldOut = 0.01