# 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

                if sumNormBinCoverage[bamId] != 0:
                    normBinCoverage[binId][bamId] /= sumNormBinCoverage[bamId]
                else:
                    normBinCoverage[binId][bamId] = 0

        # write community profile
        oldStdOut = reassignStdOut(outFile)

        sortedBinIds = sorted(readsMappedToBin.keys())
        sortedBamIds = sorted(readsMappedToBin[sortedBinIds[0]].keys())

        header = ['Bin Id', 'Bin size (Mbp)']
        for bamId in sortedBamIds:
            header += [bamId + ': mapped reads']
            header += [bamId + ': % mapped reads']
            header += [bamId + ': % binned populations']
            header += [bamId + ': % community']

        if bTabTable:
            print('\t'.join(header))
        else:
            pTable = prettytable.PrettyTable(header)
            pTable.float_format = '.2'
            pTable.align = 'c'
            pTable.align[header[0]] = 'l'
            pTable.hrules = prettytable.FRAME
            pTable.vrules = prettytable.NONE

        for binId in sortedBinIds:
            row = [binId]

# 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

                if sumNormBinCoverage[bamId] != 0:
                    normBinCoverage[binId][bamId] /= sumNormBinCoverage[bamId]
                else:
                    normBinCoverage[binId][bamId] = 0

        # write community profile
        oldStdOut = reassignStdOut(outFile)

        sortedBinIds = sorted(readsMappedToBin.keys())
        sortedBamIds = sorted(readsMappedToBin[sortedBinIds[0]].keys())

        header = ['Bin Id', 'Bin size (Mbp)']
        for bamId in sortedBamIds:
            header += [bamId + ': mapped reads']
            header += [bamId + ': % mapped reads']
            header += [bamId + ': % binned populations']
            header += [bamId + ': % community']

        if bTabTable:
            print('\t'.join(header))
        else:
            pTable = prettytable.PrettyTable(header)
            pTable.float_format = '.2'
            pTable.align = 'c'
            pTable.align[header[0]] = 'l'
            pTable.hrules = prettytable.FRAME
            pTable.vrules = prettytable.NONE

        for binId in sortedBinIds:
            row = [binId]

            row += [float(binSize[binId]) / 1e6]
