# Path: crumbs/plot.py
# def get_fig_and_canvas(num_rows=1, num_cols=1, figsize=None):
#     if figsize is None:
#         height = 5.0 * num_rows
#         width = 7.5 * num_cols
#         if height > 320.0:
#             height = 320.0
#         figsize = (width, height)
#     try:
#         fig = Figure(figsize=figsize)
#         canvas = FigureCanvas(fig)
#     except NameError:
#         msg = 'Matplotlib module is required to draw graphical histograms'
#         raise OptionalRequirementError(msg)
#     return fig, canvas
# 
# Path: crumbs/vcf/snv.py
# class VCFReader(object):
#     def __init__(self, fhand, compressed=None, filename=None,
#                  min_calls_for_pop_stats=DEF_MIN_CALLS_FOR_POP_STATS):
#         self.fhand = fhand
#         self.pyvcf_reader = pyvcfReader(fsock=fhand, compressed=compressed,
#                                         filename=filename)
#         self.min_calls_for_pop_stats = min_calls_for_pop_stats
#         self._snpcaller = None
# 
#     def parse_snvs(self):
#         min_calls_for_pop_stats = self.min_calls_for_pop_stats
#         last_snp = None
#         try:
#             counter =0
#             for snp in self.pyvcf_reader:
#                 counter +=1
#                 snp = SNV(snp, reader=self,
#                           min_calls_for_pop_stats=min_calls_for_pop_stats)
#                 last_snp = snp
#                 yield snp
#         except Exception:
#             from traceback import print_exception
#             exc_type, exc_value, exc_traceback = sys.exc_info()
# 
#             print_exception(exc_type, exc_value, exc_traceback,
#                             limit=20, file=sys.stderr)
# 
#             if last_snp is not None:
#                 chrom = str(last_snp.chrom)
#                 pos = last_snp.pos
#                 msg = 'Last parsed SNP was: {} {}\n'.format(chrom, pos + 1)
#                 sys.stderr.write(msg)
#                 raise
# 
#     def fetch_snvs(self, chrom, start, end=None):
#         min_calls_for_pop_stats = self.min_calls_for_pop_stats
#         try:
#             snvs = self.pyvcf_reader.fetch(chrom, start + 1, end=end)
#         except KeyError:
#             snvs = []
#         if snvs is None:
#             snvs = []
# 
#         for snp in snvs:
#             snp = SNV(snp, reader=self,
#                       min_calls_for_pop_stats=min_calls_for_pop_stats)
#             yield snp
# 
#     def sliding_windows(self, size, step=None, ref_fhand=None,
#                         min_num_snps=DEF_MIN_NUM_SNPS_IN_WIN):
#         random_snp_reader = VCFReader(open(self.fhand.name))
#         sliding_window = _SNPSlidingWindow(snp_reader=random_snp_reader,
#                                            win_size=size, win_step=step,
#                                            min_num_snps=min_num_snps,
#                                            ref_fhand=ref_fhand)
#         for window in sliding_window.windows():
#             yield window
# 
#     @property
#     def snpcaller(self):
#         if self._snpcaller is not None:
#             return self._snpcaller
# 
#         metadata = self.pyvcf_reader.metadata
#         if 'source' in metadata:
#             if 'VarScan2' in metadata['source']:
#                 snpcaller = VARSCAN
#             elif 'freebayes' in metadata['source'][0].lower():
#                 snpcaller = FREEBAYES
#             else:
#                 snpcaller = GENERIC
#         elif 'UnifiedGenotyper' in metadata:
#             snpcaller = GATK
#         else:
#             snpcaller = GENERIC
#         self._snpcaller = snpcaller
#         return snpcaller
# 
#     @property
#     def samples(self):
#         return self.pyvcf_reader.samples
# 
#     @property
#     def filters(self):
#         return self.pyvcf_reader.filters
# 
#     @property
#     def infos(self):
#         return self.pyvcf_reader.infos
# 
#     @property
#     def header(self):
#         header = '\n'.join(self.pyvcf_reader._header_lines)
#         header += '\n#' + '\t'.join(self.pyvcf_reader._column_headers)
#         header += '\t' + '\t'.join(self.pyvcf_reader.samples)
#         return header


from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
from crumbs.plot import get_fig_and_canvas
from crumbs.vcf.snv import VCFReader
    for item in iterator:
        chrom = item['chrom']
        start = item['start']
        end = item['end']
        item['values']

        if prev_chrom is None:
            x_vals = []
            y_vals = {}
        elif prev_chrom != chrom:
            yield prev_chrom, x_vals, y_vals
            x_vals = []
            y_vals = {}

        x_vals.append((start + end) / 2)
        for key, value in item['values'].items():
            if key not in y_vals:
                y_vals[key] = []
            y_vals[key].append(value)
        prev_chrom = chrom
    else:
        if x_vals:
            yield chrom, x_vals, y_vals


def plot_in_genome(iterator, out_base, labels):
    xsize = 100
    ysize = 10

    for chrom, x_vals, y_vals in window_data_by_chrom(iterator):

# Path: crumbs/plot.py
# def get_fig_and_canvas(num_rows=1, num_cols=1, figsize=None):
#     if figsize is None:
#         height = 5.0 * num_rows
#         width = 7.5 * num_cols
#         if height > 320.0:
#             height = 320.0
#         figsize = (width, height)
#     try:
#         fig = Figure(figsize=figsize)
#         canvas = FigureCanvas(fig)
#     except NameError:
#         msg = 'Matplotlib module is required to draw graphical histograms'
#         raise OptionalRequirementError(msg)
#     return fig, canvas
# 
# Path: crumbs/vcf/snv.py
# class VCFReader(object):
#     def __init__(self, fhand, compressed=None, filename=None,
#                  min_calls_for_pop_stats=DEF_MIN_CALLS_FOR_POP_STATS):
#         self.fhand = fhand
#         self.pyvcf_reader = pyvcfReader(fsock=fhand, compressed=compressed,
#                                         filename=filename)
#         self.min_calls_for_pop_stats = min_calls_for_pop_stats
#         self._snpcaller = None
# 
#     def parse_snvs(self):
#         min_calls_for_pop_stats = self.min_calls_for_pop_stats
#         last_snp = None
#         try:
#             counter =0
#             for snp in self.pyvcf_reader:
#                 counter +=1
#                 snp = SNV(snp, reader=self,
#                           min_calls_for_pop_stats=min_calls_for_pop_stats)
#                 last_snp = snp
#                 yield snp
#         except Exception:
#             from traceback import print_exception
#             exc_type, exc_value, exc_traceback = sys.exc_info()
# 
#             print_exception(exc_type, exc_value, exc_traceback,
#                             limit=20, file=sys.stderr)
# 
#             if last_snp is not None:
#                 chrom = str(last_snp.chrom)
#                 pos = last_snp.pos
#                 msg = 'Last parsed SNP was: {} {}\n'.format(chrom, pos + 1)
#                 sys.stderr.write(msg)
#                 raise
# 
#     def fetch_snvs(self, chrom, start, end=None):
#         min_calls_for_pop_stats = self.min_calls_for_pop_stats
#         try:
#             snvs = self.pyvcf_reader.fetch(chrom, start + 1, end=end)
#         except KeyError:
#             snvs = []
#         if snvs is None:
#             snvs = []
# 
#         for snp in snvs:
#             snp = SNV(snp, reader=self,
#                       min_calls_for_pop_stats=min_calls_for_pop_stats)
#             yield snp
# 
#     def sliding_windows(self, size, step=None, ref_fhand=None,
#                         min_num_snps=DEF_MIN_NUM_SNPS_IN_WIN):
#         random_snp_reader = VCFReader(open(self.fhand.name))
#         sliding_window = _SNPSlidingWindow(snp_reader=random_snp_reader,
#                                            win_size=size, win_step=step,
#                                            min_num_snps=min_num_snps,
#                                            ref_fhand=ref_fhand)
#         for window in sliding_window.windows():
#             yield window
# 
#     @property
#     def snpcaller(self):
#         if self._snpcaller is not None:
#             return self._snpcaller
# 
#         metadata = self.pyvcf_reader.metadata
#         if 'source' in metadata:
#             if 'VarScan2' in metadata['source']:
#                 snpcaller = VARSCAN
#             elif 'freebayes' in metadata['source'][0].lower():
#                 snpcaller = FREEBAYES
#             else:
#                 snpcaller = GENERIC
#         elif 'UnifiedGenotyper' in metadata:
#             snpcaller = GATK
#         else:
#             snpcaller = GENERIC
#         self._snpcaller = snpcaller
#         return snpcaller
# 
#     @property
#     def samples(self):
#         return self.pyvcf_reader.samples
# 
#     @property
#     def filters(self):
#         return self.pyvcf_reader.filters
# 
#     @property
#     def infos(self):
#         return self.pyvcf_reader.infos
# 
#     @property
#     def header(self):
#         header = '\n'.join(self.pyvcf_reader._header_lines)
#         header += '\n#' + '\t'.join(self.pyvcf_reader._column_headers)
#         header += '\t' + '\t'.join(self.pyvcf_reader.samples)
#         return header


# Path: crumbs/vcf/plot.py
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg
from crumbs.plot import get_fig_and_canvas
from crumbs.vcf.snv import VCFReader

    for item in iterator:
        chrom = item['chrom']
        start = item['start']
        end = item['end']
        item['values']

        if prev_chrom is None:
            x_vals = []
            y_vals = {}
        elif prev_chrom != chrom:
            yield prev_chrom, x_vals, y_vals
            x_vals = []
            y_vals = {}

        x_vals.append((start + end) / 2)
        for key, value in item['values'].items():
            if key not in y_vals:
                y_vals[key] = []
            y_vals[key].append(value)
        prev_chrom = chrom
    else:
        if x_vals:
            yield chrom, x_vals, y_vals


def plot_in_genome(iterator, out_base, labels):
    xsize = 100
    ysize = 10

    for chrom, x_vals, y_vals in window_data_by_chrom(iterator):

        fig, canvas = get_fig_and_canvas(figsize=(xsize, ysize))