# Path: checkm/util/seqUtils.py
# def readFasta(fastaFile, trimHeader=True):
#     '''Read sequences from FASTA file.'''
#     try:
#         if fastaFile.endswith('.gz'):
#             openFile = gzip.open
#         else:
#             openFile = open
# 
#         seqs = {}
#         for line in openFile(fastaFile):
#             # skip blank lines
#             if not line.strip():
#                 continue
# 
#             if line[0] == '>':
#                 if trimHeader:
#                     seqId = line[1:].split(None, 1)[0]
#                 else:
#                     seqId = line[1:].rstrip()
#                 seqs[seqId] = []
#             else:
#                 seqs[seqId].append(line[0:-1])
# 
#         for seqId, seq in seqs.items():
#             seqs[seqId] = ''.join(seq)
#     except:
#         logger = logging.getLogger('timestamp')
#         logger.error("Failed to process sequence file: {}".format(fastaFile))
#         sys.exit(1)
# 
#     return seqs
# 
# Path: checkm/prodigal.py
# class ProdigalFastaParser():
#     """Parses prodigal FASTA output."""
# 
#     def __init__(self):
#         pass
# 
#     def genePositions(self, filename):
#         checkFileExists(filename)
# 
#         gp = {}
#         for line in open(filename):
#             if line[0] == '>':
#                 lineSplit = line[1:].split()
# 
#                 geneId = lineSplit[0]
#                 startPos = int(lineSplit[2])
#                 endPos = int(lineSplit[4])
# 
#                 gp[geneId] = [startPos, endPos]
# 
#         return gp
# 
# Path: checkm/common.py
# def binIdFromFilename(filename):
#     """Extract bin id from bin filename."""
#     binId = os.path.basename(filename)
#     binId = os.path.splitext(binId)[0]
# 
#     return binId


import os
import math
import operator
import logging
import numpy as np
import matplotlib as mpl
from checkm.plot.AbstractPlot import AbstractPlot
from checkm.defaultValues import DefaultValues
from checkm.util.seqUtils import readFasta
from checkm.prodigal import ProdigalFastaParser
from checkm.common import binIdFromFilename
from matplotlib.patches import Rectangle

        self.logger = logging.getLogger('timestamp')

    def getMarkerGenesPerSeq(self, markerGeneStats):
        markerGenePos = {}
        markerGeneNum = {}

        for geneId in markerGeneStats:
            scaffoldId = geneId[0:geneId.rfind('_')]

            for markerId, hitList in markerGeneStats[geneId].items():
                for hit in hitList:
                    start = hit[0]
                    end = hit[1]
                markerGenePos[scaffoldId] = markerGenePos.get(scaffoldId, []) + [[geneId, markerId, start, end]]
                markerGeneNum[markerId] = len(hitList)

        return markerGenePos, markerGeneNum

    def roundUpToNearest100(self, x):
        return int(math.ceil(x / 100.0)) * 100

    def plot(self, binFile, markerGeneStats, binStats):
        binId = binIdFromFilename(binFile)

        markerGenesPerSeq, _markerGeneNum = self.getMarkerGenesPerSeq(markerGeneStats)

        if len(markerGenesPerSeq) == 0:
            return False

        # Get length of sequences with one or more marker genes

# Path: checkm/util/seqUtils.py
# def readFasta(fastaFile, trimHeader=True):
#     '''Read sequences from FASTA file.'''
#     try:
#         if fastaFile.endswith('.gz'):
#             openFile = gzip.open
#         else:
#             openFile = open
# 
#         seqs = {}
#         for line in openFile(fastaFile):
#             # skip blank lines
#             if not line.strip():
#                 continue
# 
#             if line[0] == '>':
#                 if trimHeader:
#                     seqId = line[1:].split(None, 1)[0]
#                 else:
#                     seqId = line[1:].rstrip()
#                 seqs[seqId] = []
#             else:
#                 seqs[seqId].append(line[0:-1])
# 
#         for seqId, seq in seqs.items():
#             seqs[seqId] = ''.join(seq)
#     except:
#         logger = logging.getLogger('timestamp')
#         logger.error("Failed to process sequence file: {}".format(fastaFile))
#         sys.exit(1)
# 
#     return seqs
# 
# Path: checkm/prodigal.py
# class ProdigalFastaParser():
#     """Parses prodigal FASTA output."""
# 
#     def __init__(self):
#         pass
# 
#     def genePositions(self, filename):
#         checkFileExists(filename)
# 
#         gp = {}
#         for line in open(filename):
#             if line[0] == '>':
#                 lineSplit = line[1:].split()
# 
#                 geneId = lineSplit[0]
#                 startPos = int(lineSplit[2])
#                 endPos = int(lineSplit[4])
# 
#                 gp[geneId] = [startPos, endPos]
# 
#         return gp
# 
# Path: checkm/common.py
# def binIdFromFilename(filename):
#     """Extract bin id from bin filename."""
#     binId = os.path.basename(filename)
#     binId = os.path.splitext(binId)[0]
# 
#     return binId


# Path: checkm/plot/markerGenePosPlot.py
import os
import math
import operator
import logging
import numpy as np
import matplotlib as mpl
from checkm.plot.AbstractPlot import AbstractPlot
from checkm.defaultValues import DefaultValues
from checkm.util.seqUtils import readFasta
from checkm.prodigal import ProdigalFastaParser
from checkm.common import binIdFromFilename
from matplotlib.patches import Rectangle

        self.logger = logging.getLogger('timestamp')

    def getMarkerGenesPerSeq(self, markerGeneStats):
        markerGenePos = {}
        markerGeneNum = {}

        for geneId in markerGeneStats:
            scaffoldId = geneId[0:geneId.rfind('_')]

            for markerId, hitList in markerGeneStats[geneId].items():
                for hit in hitList:
                    start = hit[0]
                    end = hit[1]
                markerGenePos[scaffoldId] = markerGenePos.get(scaffoldId, []) + [[geneId, markerId, start, end]]
                markerGeneNum[markerId] = len(hitList)

        return markerGenePos, markerGeneNum

    def roundUpToNearest100(self, x):
        return int(math.ceil(x / 100.0)) * 100

    def plot(self, binFile, markerGeneStats, binStats):
        binId = binIdFromFilename(binFile)

        markerGenesPerSeq, _markerGeneNum = self.getMarkerGenesPerSeq(markerGeneStats)

        if len(markerGenesPerSeq) == 0:
            return False

        # Get length of sequences with one or more marker genes

        seqs = readFasta(binFile)
