# Path: nanopore/mappers/abstractMapper.py
# class AbstractMapper(Target):
#     """Base class for mappers. Inherit this class to create a mapper
#     """
#     def __init__(self, readFastqFile, readType, referenceFastaFile, outputSamFile, emptyHmmFile=None):
#         Target.__init__(self)
#         self.readFastqFile = readFastqFile
#         self.referenceFastaFile = referenceFastaFile
#         self.outputSamFile = outputSamFile
#         self.readType = readType
#         self.emptyHmmFile=emptyHmmFile
#         
#     def chainSamFile(self):
#         """Converts the sam file so that there is at most one global alignment of each read
#         """ 
#         tempSamFile = os.path.join(self.getLocalTempDir(), "temp.sam")
#         system("cp %s %s" % (self.outputSamFile, tempSamFile))
#         chainSamFile(tempSamFile, self.outputSamFile, self.readFastqFile, self.referenceFastaFile)
#     
#     def realignSamFile(self, gapGamma=0.5, matchGamma=0.0, doEm=False,  useTrainedModel=False, trainedModelFile="blasr_hmm_0.txt"):
#         """Chains and then realigns the resulting global alignments.
#         """
#         tempSamFile = os.path.join(self.getGlobalTempDir(), "temp.sam")
#         if useTrainedModel and doEm:
#             raise RuntimeError("Attempting to train stock model")
#         system("cp %s %s" % (self.outputSamFile, tempSamFile))
#         if doEm:
#             hmmFile = self.emptyHmmFile
#         elif useTrainedModel:
#             hmmFile = os.path.join(pathToBaseNanoporeDir(), "nanopore", "mappers", trainedModelFile)
#         else:
#             hmmFile = None
#         self.addChildTargetFn(realignSamFileTargetFn, args=(tempSamFile, self.outputSamFile, 
#                                                             self.readFastqFile, self.referenceFastaFile, gapGamma, matchGamma, hmmFile, doEm))


from nanopore.mappers.abstractMapper import AbstractMapper
from sonLib.bioio import system
import os

class Bwa(AbstractMapper):
    def run(self, args=""):
        localReferenceFastaFile = os.path.join(self.getLocalTempDir(), "ref.fa") #Because BWA builds these crufty index files, copy to a temporary directory
        system("cp %s %s" % (self.referenceFastaFile, localReferenceFastaFile))
        system("bwa index %s" % localReferenceFastaFile)
        system("bwa mem %s %s %s > %s" % (args, localReferenceFastaFile, self.readFastqFile, self.outputSamFile))

class BwaChain(Bwa):
    def run(self):
        Bwa.run(self)
        self.chainSamFile()

class BwaRealign(Bwa):
    def run(self):
        Bwa.run(self)
        self.realignSamFile()

class BwaRealignEm(Bwa):
    def run(self):
        Bwa.run(self)
        self.realignSamFile()


# Path: nanopore/mappers/abstractMapper.py
# class AbstractMapper(Target):
#     """Base class for mappers. Inherit this class to create a mapper
#     """
#     def __init__(self, readFastqFile, readType, referenceFastaFile, outputSamFile, emptyHmmFile=None):
#         Target.__init__(self)
#         self.readFastqFile = readFastqFile
#         self.referenceFastaFile = referenceFastaFile
#         self.outputSamFile = outputSamFile
#         self.readType = readType
#         self.emptyHmmFile=emptyHmmFile
#         
#     def chainSamFile(self):
#         """Converts the sam file so that there is at most one global alignment of each read
#         """ 
#         tempSamFile = os.path.join(self.getLocalTempDir(), "temp.sam")
#         system("cp %s %s" % (self.outputSamFile, tempSamFile))
#         chainSamFile(tempSamFile, self.outputSamFile, self.readFastqFile, self.referenceFastaFile)
#     
#     def realignSamFile(self, gapGamma=0.5, matchGamma=0.0, doEm=False,  useTrainedModel=False, trainedModelFile="blasr_hmm_0.txt"):
#         """Chains and then realigns the resulting global alignments.
#         """
#         tempSamFile = os.path.join(self.getGlobalTempDir(), "temp.sam")
#         if useTrainedModel and doEm:
#             raise RuntimeError("Attempting to train stock model")
#         system("cp %s %s" % (self.outputSamFile, tempSamFile))
#         if doEm:
#             hmmFile = self.emptyHmmFile
#         elif useTrainedModel:
#             hmmFile = os.path.join(pathToBaseNanoporeDir(), "nanopore", "mappers", trainedModelFile)
#         else:
#             hmmFile = None
#         self.addChildTargetFn(realignSamFileTargetFn, args=(tempSamFile, self.outputSamFile, 
#                                                             self.readFastqFile, self.referenceFastaFile, gapGamma, matchGamma, hmmFile, doEm))


# Path: nanopore/mappers/bwa.py
from nanopore.mappers.abstractMapper import AbstractMapper
from sonLib.bioio import system
import os


class Bwa(AbstractMapper):
    def run(self, args=""):
        localReferenceFastaFile = os.path.join(self.getLocalTempDir(), "ref.fa") #Because BWA builds these crufty index files, copy to a temporary directory
        system("cp %s %s" % (self.referenceFastaFile, localReferenceFastaFile))
        system("bwa index %s" % localReferenceFastaFile)
        system("bwa mem %s %s %s > %s" % (args, localReferenceFastaFile, self.readFastqFile, self.outputSamFile))

class BwaChain(Bwa):
    def run(self):
        Bwa.run(self)
        self.chainSamFile()

class BwaRealign(Bwa):
    def run(self):
        Bwa.run(self)
        self.realignSamFile()

class BwaRealignEm(Bwa):
    def run(self):
        Bwa.run(self)
        self.realignSamFile()


class BwaRealignTrainedModel(Bwa):