# Path: checkm/common.py
# def checkFileExists(inputFile):
#     """Check if file exists."""
#     if not os.path.exists(inputFile):
#         logger = logging.getLogger('timestamp')
#         logger.error('Input file does not exists: ' + inputFile + '\n')
#         sys.exit(1)
# 
# def reassignStdOut(outFile):
#     """Redirect standard out to a file."""
#     oldStdOut = sys.stdout
#     if(outFile != ''):
#         try:
#             # redirect stdout to a file
#             sys.stdout = open(outFile, 'w')
#         except:
#             logger = logging.getLogger('timestamp')
#             logger.error("Error diverting stdout to file: " + outFile)
#             sys.exit(1)
# 
#     return oldStdOut
# 
# def restoreStdOut(outFile, oldStdOut):
#     """Redirect standard out back to system standard out."""
#     if(outFile != ''):
#         try:
#             # redirect stdout to a file
#             sys.stdout.close()
#             sys.stdout = oldStdOut
#         except:
#             logger = logging.getLogger('timestamp')
#             logger.error("Error restoring stdout ", outFile)
#             sys.exit(1)


import logging
import checkm.prettytable
from checkm.defaultValues import DefaultValues
from checkm.common import checkFileExists, reassignStdOut, restoreStdOut

                totalMappedReads[bamId] = totalMappedReads.get(bamId, 0) + mappedReads
                readsMappedToBin[binId][bamId] = readsMappedToBin[binId].get(bamId, 0) + mappedReads

        # calculate percentage of mapped reads to binned populations
        perMappedReads = {}
        normBinCoverage = {}
        sumNormBinCoverage = {}
        for binId, bamIds in readsMappedToBin.items():
            perMappedReads[binId] = {}
            normBinCoverage[binId] = {}

            for bamId in bamIds:
                perMR = float(readsMappedToBin[binId][bamId]) / totalMappedReads[bamId]
                perMappedReads[binId][bamId] = perMR

                if binId == DefaultValues.UNBINNED:
                    continue

                normCoverage = perMR / binSize[binId]
                normBinCoverage[binId][bamId] = normCoverage
                sumNormBinCoverage[bamId] = sumNormBinCoverage.get(bamId, 0) + normCoverage

        for binId, bamIds in normBinCoverage.items():
            for bamId in bamIds:
                if sumNormBinCoverage[bamId] != 0:
                    normBinCoverage[binId][bamId] /= sumNormBinCoverage[bamId]
                else:
                    normBinCoverage[binId][bamId] = 0

        # write community profile

# Path: checkm/common.py
# def checkFileExists(inputFile):
#     """Check if file exists."""
#     if not os.path.exists(inputFile):
#         logger = logging.getLogger('timestamp')
#         logger.error('Input file does not exists: ' + inputFile + '\n')
#         sys.exit(1)
# 
# def reassignStdOut(outFile):
#     """Redirect standard out to a file."""
#     oldStdOut = sys.stdout
#     if(outFile != ''):
#         try:
#             # redirect stdout to a file
#             sys.stdout = open(outFile, 'w')
#         except:
#             logger = logging.getLogger('timestamp')
#             logger.error("Error diverting stdout to file: " + outFile)
#             sys.exit(1)
# 
#     return oldStdOut
# 
# def restoreStdOut(outFile, oldStdOut):
#     """Redirect standard out back to system standard out."""
#     if(outFile != ''):
#         try:
#             # redirect stdout to a file
#             sys.stdout.close()
#             sys.stdout = oldStdOut
#         except:
#             logger = logging.getLogger('timestamp')
#             logger.error("Error restoring stdout ", outFile)
#             sys.exit(1)


# Path: checkm/profile.py
import logging
import checkm.prettytable
from checkm.defaultValues import DefaultValues
from checkm.common import checkFileExists, reassignStdOut, restoreStdOut

                totalMappedReads[bamId] = totalMappedReads.get(bamId, 0) + mappedReads
                readsMappedToBin[binId][bamId] = readsMappedToBin[binId].get(bamId, 0) + mappedReads

        # calculate percentage of mapped reads to binned populations
        perMappedReads = {}
        normBinCoverage = {}
        sumNormBinCoverage = {}
        for binId, bamIds in readsMappedToBin.items():
            perMappedReads[binId] = {}
            normBinCoverage[binId] = {}

            for bamId in bamIds:
                perMR = float(readsMappedToBin[binId][bamId]) / totalMappedReads[bamId]
                perMappedReads[binId][bamId] = perMR

                if binId == DefaultValues.UNBINNED:
                    continue

                normCoverage = perMR / binSize[binId]
                normBinCoverage[binId][bamId] = normCoverage
                sumNormBinCoverage[bamId] = sumNormBinCoverage.get(bamId, 0) + normCoverage

        for binId, bamIds in normBinCoverage.items():
            for bamId in bamIds:
                if sumNormBinCoverage[bamId] != 0:
                    normBinCoverage[binId][bamId] /= sumNormBinCoverage[bamId]
                else:
                    normBinCoverage[binId][bamId] = 0

        # write community profile

        oldStdOut = reassignStdOut(outFile)
