# Path: nanopore/analyses/abstractAnalysis.py
# class AbstractAnalysis(Target):
#     """Base class to for analysis targets. Inherit this class to create an analysis.
#     """
#     def __init__(self, readFastqFile, readType, referenceFastaFile, samFile, outputDir):
#         Target.__init__(self)
#         self.readFastqFile = readFastqFile
#         self.referenceFastaFile = referenceFastaFile
#         self.samFile = samFile
#         self.outputDir = outputDir
#         self.readType = readType
#         print(str(self.samFile))
#         
#     def run(self):
#         """Base method that does some logging
#         """
#         logger.info("This analysis target has read fastq file: %s, reference fasta file: %s, sam file: %s and will output to the directory: %s" % \
#                     (self.readFastqFile, self.referenceFastaFile, self.samFile, self.outputDir))
#         
#     def finish(self):
#         """Method called when a analysis has finished successfully to indicate that it should not be repeated.
#         """
#         open(os.path.join(self.outputDir, "DONE"), 'w').close()
# 
#     @staticmethod
#     def reset(outputDir):
#         if AbstractAnalysis.isFinished(outputDir):
#             os.remove(os.path.join(outputDir, "DONE"))
#     
#     @staticmethod
#     def isFinished(outputDir):
#         return os.path.exists(os.path.join(outputDir, "DONE"))
#     
#     @staticmethod
#     def formatRatio(numerator, denominator):
#         if denominator == 0:
#             return float("nan")
#         return float(numerator)/denominator
# 
# Path: nanopore/analyses/utils.py
# def samToBamFile(samInputFile, bamOutputFile):
#     """Converts a sam file to a bam file (sorted)
#     """
#     samfile = pysam.Samfile(samInputFile, "r" )
#     bamfile = pysam.Samfile(bamOutputFile, "wb", template=samfile)
#     for line in samfile:
#         bamfile.write(line)
# 
#     samfile.close()
#     bamfile.close()


from nanopore.analyses.abstractAnalysis import AbstractAnalysis
from sonLib.bioio import system
from nanopore.analyses.utils import samToBamFile
import os
import pysam
                continue
            elif line.startswith("+"):
                qual_id[fasta_id] = line.strip()
                flag = "qual"
                qual_scr[fasta_id] = []
                continue
            if flag == "seq":
                fasta_seq[fasta_id].append(line.upper().strip())
            elif flag == "qual":
                qual_scr[fasta_id].append(line.strip())
        else:
            #print >> sys.stderr, "Warning: End of Sequence"
            break

    for fasta_id in fasta_seq.keys():
        #if len(fasta_seq[fasta_id]) > 0:
        outfile.write(fasta_id)
        outfile.write("\n")
        outfile.write("".join(fasta_seq[fasta_id]))
        outfile.write("\n")
        outfile.write(qual_id[fasta_id])
        outfile.write("\n")
        outfile.write("".join(qual_scr[fasta_id]))
        outfile.write("\n")
    
    infile.close()
    outfile.close()

class Consensus(AbstractAnalysis):
    def run(self):

# Path: nanopore/analyses/abstractAnalysis.py
# class AbstractAnalysis(Target):
#     """Base class to for analysis targets. Inherit this class to create an analysis.
#     """
#     def __init__(self, readFastqFile, readType, referenceFastaFile, samFile, outputDir):
#         Target.__init__(self)
#         self.readFastqFile = readFastqFile
#         self.referenceFastaFile = referenceFastaFile
#         self.samFile = samFile
#         self.outputDir = outputDir
#         self.readType = readType
#         print(str(self.samFile))
#         
#     def run(self):
#         """Base method that does some logging
#         """
#         logger.info("This analysis target has read fastq file: %s, reference fasta file: %s, sam file: %s and will output to the directory: %s" % \
#                     (self.readFastqFile, self.referenceFastaFile, self.samFile, self.outputDir))
#         
#     def finish(self):
#         """Method called when a analysis has finished successfully to indicate that it should not be repeated.
#         """
#         open(os.path.join(self.outputDir, "DONE"), 'w').close()
# 
#     @staticmethod
#     def reset(outputDir):
#         if AbstractAnalysis.isFinished(outputDir):
#             os.remove(os.path.join(outputDir, "DONE"))
#     
#     @staticmethod
#     def isFinished(outputDir):
#         return os.path.exists(os.path.join(outputDir, "DONE"))
#     
#     @staticmethod
#     def formatRatio(numerator, denominator):
#         if denominator == 0:
#             return float("nan")
#         return float(numerator)/denominator
# 
# Path: nanopore/analyses/utils.py
# def samToBamFile(samInputFile, bamOutputFile):
#     """Converts a sam file to a bam file (sorted)
#     """
#     samfile = pysam.Samfile(samInputFile, "r" )
#     bamfile = pysam.Samfile(bamOutputFile, "wb", template=samfile)
#     for line in samfile:
#         bamfile.write(line)
# 
#     samfile.close()
#     bamfile.close()


# Path: nanopore/analyses/consensus.py
from nanopore.analyses.abstractAnalysis import AbstractAnalysis
from sonLib.bioio import system
from nanopore.analyses.utils import samToBamFile
import os
import pysam

                continue
            elif line.startswith("+"):
                qual_id[fasta_id] = line.strip()
                flag = "qual"
                qual_scr[fasta_id] = []
                continue
            if flag == "seq":
                fasta_seq[fasta_id].append(line.upper().strip())
            elif flag == "qual":
                qual_scr[fasta_id].append(line.strip())
        else:
            #print >> sys.stderr, "Warning: End of Sequence"
            break

    for fasta_id in fasta_seq.keys():
        #if len(fasta_seq[fasta_id]) > 0:
        outfile.write(fasta_id)
        outfile.write("\n")
        outfile.write("".join(fasta_seq[fasta_id]))
        outfile.write("\n")
        outfile.write(qual_id[fasta_id])
        outfile.write("\n")
        outfile.write("".join(qual_scr[fasta_id]))
        outfile.write("\n")
    
    infile.close()
    outfile.close()

class Consensus(AbstractAnalysis):
    def run(self):

        AbstractAnalysis.run(self) #Call base method to do some logging