# 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

                            len(seqs),
                            sum([s for s in seqLens.values()])/1e6,
                            binSize/1e6,
                            sum([s for s in seqLens.values()])*100.0/binSize)
        self.logger.info(result_str)

        MAX_BINS = 100
        plotBinSize = self.roundUpToNearest100(float(longestSeq) / MAX_BINS)
        yLabels = [x[0] for x in sortedSeqLens]

        # get position of genes in bin
        prodigalFastaParser = ProdigalFastaParser()
        geneFile = os.path.join(self.options.results_dir, 'bins', binId, DefaultValues.PRODIGAL_AA)
        genePos = prodigalFastaParser.genePositions(geneFile)

        # Set size of figure
        self.fig.clear()
        self.fig.set_size_inches(self.options.width, self.options.height)
        yLabelBounds = self.yLabelExtents(yLabels, self.options.font_size)

        heightBottomLabels = 0.4 + self.options.fig_padding  # inches
        widthSideLabel = yLabelBounds.width * self.options.width + self.options.fig_padding  # inches

        widthPerBin = (self.options.width - widthSideLabel - self.options.fig_padding) / MAX_BINS

        titleHeight = 0.2
        HEIGHT_PER_ROW = 0.2
        height = HEIGHT_PER_ROW * len(sortedSeqLens) + heightBottomLabels + self.options.fig_padding + titleHeight
        rowBinHeight = widthPerBin / HEIGHT_PER_ROW


# 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

                            len(seqs),
                            sum([s for s in seqLens.values()])/1e6,
                            binSize/1e6,
                            sum([s for s in seqLens.values()])*100.0/binSize)
        self.logger.info(result_str)

        MAX_BINS = 100
        plotBinSize = self.roundUpToNearest100(float(longestSeq) / MAX_BINS)
        yLabels = [x[0] for x in sortedSeqLens]

        # get position of genes in bin
        prodigalFastaParser = ProdigalFastaParser()
        geneFile = os.path.join(self.options.results_dir, 'bins', binId, DefaultValues.PRODIGAL_AA)
        genePos = prodigalFastaParser.genePositions(geneFile)

        # Set size of figure
        self.fig.clear()
        self.fig.set_size_inches(self.options.width, self.options.height)
        yLabelBounds = self.yLabelExtents(yLabels, self.options.font_size)

        heightBottomLabels = 0.4 + self.options.fig_padding  # inches
        widthSideLabel = yLabelBounds.width * self.options.width + self.options.fig_padding  # inches

        widthPerBin = (self.options.width - widthSideLabel - self.options.fig_padding) / MAX_BINS

        titleHeight = 0.2
        HEIGHT_PER_ROW = 0.2
        height = HEIGHT_PER_ROW * len(sortedSeqLens) + heightBottomLabels + self.options.fig_padding + titleHeight
        rowBinHeight = widthPerBin / HEIGHT_PER_ROW


        self.fig.set_size_inches(self.options.width, height)
