# 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])


import os
import xml.etree.cElementTree as ET
import itertools
import numpy as np
from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from jobTree.src.bioio import *
                                    insertEmissions[base].append(insertEmissions2[base])
                                    deleteEmissions[base].append(deleteEmissions2[base])
            
            #Write out a dot file representing the avg HMM transitions with std errors
            
            #Plot graphviz version of nanopore hmm, showing transitions and variances.
            fH = open(os.path.join(self.outputDir, "hmm_%s.dot" % readType), 'w')
            setupGraphFile(fH)
            #Make states
            addNodeToGraph("n0n", fH, "match")
            addNodeToGraph("n1n", fH, "short delete")
            addNodeToGraph("n2n", fH, "short insert")
            addNodeToGraph("n3n", fH, "long insert")
            addNodeToGraph("n4n", fH, "long delete")

            #Make edges with labelled transition probs.
            for fromState, toState in transitions:
                t = transitions[(fromState, toState)]
                addEdgeToGraph("n%sn" % fromState, 
                               "n%sn" % toState, fH, dir="arrow", style='""',
                                label="%.3f,%.3f" % (np.average(map(lambda x : x[0], t)), np.std(map(lambda x : x[0], t))))

            #Finish up
            finishGraphFile(fH)
            fH.close()       
            
            #Write out a matrix representing the substitutions with std errors, normalised by reference base
            matchEmissionsFile = os.path.join(self.outputDir, "matchEmissionsNormalisedByReference_%s.tsv" % readType)
            outf = open(matchEmissionsFile, "w")
            bases = "ACGT"

# 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/metaAnalyses/hmmMetaAnalysis.py
import os
import xml.etree.cElementTree as ET
import itertools
import numpy as np
from nanopore.metaAnalyses.abstractMetaAnalysis import AbstractMetaAnalysis
from jobTree.src.bioio import *

                                    insertEmissions[base].append(insertEmissions2[base])
                                    deleteEmissions[base].append(deleteEmissions2[base])
            
            #Write out a dot file representing the avg HMM transitions with std errors
            
            #Plot graphviz version of nanopore hmm, showing transitions and variances.
            fH = open(os.path.join(self.outputDir, "hmm_%s.dot" % readType), 'w')
            setupGraphFile(fH)
            #Make states
            addNodeToGraph("n0n", fH, "match")
            addNodeToGraph("n1n", fH, "short delete")
            addNodeToGraph("n2n", fH, "short insert")
            addNodeToGraph("n3n", fH, "long insert")
            addNodeToGraph("n4n", fH, "long delete")

            #Make edges with labelled transition probs.
            for fromState, toState in transitions:
                t = transitions[(fromState, toState)]
                addEdgeToGraph("n%sn" % fromState, 
                               "n%sn" % toState, fH, dir="arrow", style='""',
                                label="%.3f,%.3f" % (np.average(map(lambda x : x[0], t)), np.std(map(lambda x : x[0], t))))

            #Finish up
            finishGraphFile(fH)
            fH.close()       
            
            #Write out a matrix representing the substitutions with std errors, normalised by reference base
            matchEmissionsFile = os.path.join(self.outputDir, "matchEmissionsNormalisedByReference_%s.tsv" % readType)
            outf = open(matchEmissionsFile, "w")
            bases = "ACGT"

            outf.write("\t".join(bases) + "\n")