# Path: crumbs/utils/file_utils.py
# class TemporaryDir(object):
#     '''This class creates temporary directories '''
#     def __init__(self, suffix='', prefix='', directory=None):
#         '''It initiates the class.'''
#         self._name = tempfile.mkdtemp(suffix=suffix, prefix=prefix,
#                                       dir=directory)
# 
#     @property
#     def name(self):
#         'Returns path to the dict'
#         return self._name
# 
#     def close(self):
#         '''It removes the temp dir'''
#         if os.path.exists(self._name):
#             shutil.rmtree(self._name)
# 
# Path: crumbs/vcf/plot.py
# 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))
# 
#         num_axes = len(y_vals)
#         for index, plotname in enumerate(labels.keys()):
#             plot_label = labels[plotname]
#             axes = fig.add_subplot(num_axes, 1, index + 1)
#             axes.plot(x_vals, y_vals[plotname])
#             title = plot_label['title'] + ' for chromosome {}'.format(chrom)
#             axes.set_title(title)
#             axes.set_ylabel(plot_label['ylabel'])
# 
#         axes.set_xlabel('Position in chromosome')
# 
#         fhand = open(out_base + '.' + str(chrom) + '.png', 'w')
# 
#         canvas.print_figure(fhand, dpi=300)
#         fhand.flush()
# 
# def plot_haplotypes(vcf_fhand, plot_fhand, genotype_mode=REFERENCE,
#                     filter_alleles_gt=FILTER_ALLELES_GT):
#     reader = VCFReader(vcf_fhand)
# 
#     # collect data
#     genotypes = None
#     samples = []
#     for snv in reader.parse_snvs():
#         if genotypes is None:
#             genotypes = {}
#             for call in snv.calls:
#                 sample = call.sample
#                 genotypes[sample] = []
#                 samples.append(sample)
# 
#         for call in snv.calls:
#             alleles = _get_alleles(call, filter_alleles_gt=filter_alleles_gt)
#             genotypes[call.sample].append(alleles)
# 
#     # draw
#     n_samples = len(samples)
#     xsize = len(genotypes[sample]) / 100
#     if xsize >= 100:
#         xsize = 100
#     if xsize <= 8:
#         xsize = 8
#     ysize = n_samples * 2
#     if ysize >= 100:
#         ysize = 100
#     # print xsize, ysize
#     figure_size = (xsize, ysize)
# 
#     fig = Figure(figsize=figure_size)
# 
#     for index, sample in enumerate(samples):
#         axes = fig.add_subplot(n_samples, 1, index)
#         axes.set_title(sample)
#         y_data = genotypes[sample]
#         x_data = [i + 1 for i in range(len(y_data))]
#         x_data, y_data = _flatten_data(x_data, y_data)
# 
#         axes.plot(x_data, y_data, marker='o',
#                   linestyle='None', markersize=3.0, markeredgewidth=0,
#                   markerfacecolor='red')
#         ylim = axes.get_ylim()
#         ylim = ylim[0] - 0.1, ylim[1] + 0.1
#         axes.set_ylim(ylim)
#         axes.tick_params(axis='x', bottom='off', top='off', which='both',
#                          labelbottom='off')
#         axes.tick_params(axis='y', left='on', right='off', labelleft='off')
#         axes.set_ylabel(sample)
# 
#     canvas = FigureCanvasAgg(fig)
#     canvas.print_figure(plot_fhand, dpi=300)
#     plot_fhand.flush()
# 
# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))


from os.path import join
from collections import OrderedDict
from crumbs.utils.file_utils import TemporaryDir
from crumbs.vcf.plot import plot_in_genome, plot_haplotypes
from crumbs.utils.test_utils import TEST_DATA_DIR
from tempfile import NamedTemporaryFile
import itertools
import random
import unittest
'''
Created on 2014 uzt 24

@author: peio
'''



class GenomePlot(unittest.TestCase):
    def gen_windows(self, chrom='ch1'):
        step = 10
        for x in range(0, 1000, step):
            start = x
            end = x + step
            value = random.random()
            value2 = random.random()
            yield {'start': start, 'end': end, 'chrom': chrom,
                   'values': {'val1': value, 'val2': value2}}

    def test_plot_window(self):
        iterator = itertools.chain(self.gen_windows(),
                                   self.gen_windows('ch2'))

# Path: crumbs/utils/file_utils.py
# class TemporaryDir(object):
#     '''This class creates temporary directories '''
#     def __init__(self, suffix='', prefix='', directory=None):
#         '''It initiates the class.'''
#         self._name = tempfile.mkdtemp(suffix=suffix, prefix=prefix,
#                                       dir=directory)
# 
#     @property
#     def name(self):
#         'Returns path to the dict'
#         return self._name
# 
#     def close(self):
#         '''It removes the temp dir'''
#         if os.path.exists(self._name):
#             shutil.rmtree(self._name)
# 
# Path: crumbs/vcf/plot.py
# 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))
# 
#         num_axes = len(y_vals)
#         for index, plotname in enumerate(labels.keys()):
#             plot_label = labels[plotname]
#             axes = fig.add_subplot(num_axes, 1, index + 1)
#             axes.plot(x_vals, y_vals[plotname])
#             title = plot_label['title'] + ' for chromosome {}'.format(chrom)
#             axes.set_title(title)
#             axes.set_ylabel(plot_label['ylabel'])
# 
#         axes.set_xlabel('Position in chromosome')
# 
#         fhand = open(out_base + '.' + str(chrom) + '.png', 'w')
# 
#         canvas.print_figure(fhand, dpi=300)
#         fhand.flush()
# 
# def plot_haplotypes(vcf_fhand, plot_fhand, genotype_mode=REFERENCE,
#                     filter_alleles_gt=FILTER_ALLELES_GT):
#     reader = VCFReader(vcf_fhand)
# 
#     # collect data
#     genotypes = None
#     samples = []
#     for snv in reader.parse_snvs():
#         if genotypes is None:
#             genotypes = {}
#             for call in snv.calls:
#                 sample = call.sample
#                 genotypes[sample] = []
#                 samples.append(sample)
# 
#         for call in snv.calls:
#             alleles = _get_alleles(call, filter_alleles_gt=filter_alleles_gt)
#             genotypes[call.sample].append(alleles)
# 
#     # draw
#     n_samples = len(samples)
#     xsize = len(genotypes[sample]) / 100
#     if xsize >= 100:
#         xsize = 100
#     if xsize <= 8:
#         xsize = 8
#     ysize = n_samples * 2
#     if ysize >= 100:
#         ysize = 100
#     # print xsize, ysize
#     figure_size = (xsize, ysize)
# 
#     fig = Figure(figsize=figure_size)
# 
#     for index, sample in enumerate(samples):
#         axes = fig.add_subplot(n_samples, 1, index)
#         axes.set_title(sample)
#         y_data = genotypes[sample]
#         x_data = [i + 1 for i in range(len(y_data))]
#         x_data, y_data = _flatten_data(x_data, y_data)
# 
#         axes.plot(x_data, y_data, marker='o',
#                   linestyle='None', markersize=3.0, markeredgewidth=0,
#                   markerfacecolor='red')
#         ylim = axes.get_ylim()
#         ylim = ylim[0] - 0.1, ylim[1] + 0.1
#         axes.set_ylim(ylim)
#         axes.tick_params(axis='x', bottom='off', top='off', which='both',
#                          labelbottom='off')
#         axes.tick_params(axis='y', left='on', right='off', labelleft='off')
#         axes.set_ylabel(sample)
# 
#     canvas = FigureCanvasAgg(fig)
#     canvas.print_figure(plot_fhand, dpi=300)
#     plot_fhand.flush()
# 
# Path: crumbs/utils/test_utils.py
# TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
#                                              '..', 'test', 'test_data'))


# Path: test/vcf/test_plot.py
from os.path import join
from collections import OrderedDict
from crumbs.utils.file_utils import TemporaryDir
from crumbs.vcf.plot import plot_in_genome, plot_haplotypes
from crumbs.utils.test_utils import TEST_DATA_DIR
from tempfile import NamedTemporaryFile
import itertools
import random
import unittest

'''
Created on 2014 uzt 24

@author: peio
'''



class GenomePlot(unittest.TestCase):
    def gen_windows(self, chrom='ch1'):
        step = 10
        for x in range(0, 1000, step):
            start = x
            end = x + step
            value = random.random()
            value2 = random.random()
            yield {'start': start, 'end': end, 'chrom': chrom,
                   'values': {'val1': value, 'val2': value2}}

    def test_plot_window(self):
        iterator = itertools.chain(self.gen_windows(),
                                   self.gen_windows('ch2'))

        tempdir = TemporaryDir()