# Path: crumbs/vcf/statistics.py
# def choose_samples(record, sample_names):
#     if sample_names is None:
#         chosen_samples = record.samples
#     else:
#         filter_by_name = lambda x: True if x.sample in sample_names else False
#         chosen_samples = filter(filter_by_name, record.samples, )
#     return chosen_samples
# 
# Path: crumbs/iterutils.py
# class RandomAccessIterator(object):
#     def __init__(self, iterable, rnd_access_win):
#         self._stream = iterable
#         if not rnd_access_win % 2:
#             msg = 'rnd_access_win should be odd'
#             raise ValueError(msg)
#         self._rnd_access_win = rnd_access_win
#         self._buff = []
#         self._curr_item_in_buff = None
#         self._buffer_pos = 0
#         self._stream_consumed = False
#         self._fill_buffer()
# 
#     def _fill_buffer(self):
#         half_win = (self._rnd_access_win - 1) // 2
#         self._buff = list(islice(self._stream, half_win))
#         if len(self._buff) < half_win:
#             self._stream_consumed = True
# 
#     def __iter__(self):
#         return self
# 
#     def next(self):
#         in_buffer = self._buff
#         try:
#             stream_next = self._stream.next()
#         except StopIteration:
#             self._stream_consumed = True
# 
#         if not self._stream_consumed:
#             in_buffer.append(stream_next)
# 
#         if self._curr_item_in_buff is None:
#             self._curr_item_in_buff = 0
#         else:
#             self._curr_item_in_buff += 1
# 
#         try:
#             item_to_yield = in_buffer[self._curr_item_in_buff]
#         except IndexError:
#             raise StopIteration
# 
#         if len(in_buffer) > self._rnd_access_win:
#             in_buffer.pop(0)
#             self._curr_item_in_buff -= 1
#             self._buffer_pos += 1
# 
#         return item_to_yield
# 
#     def _getitem(self, start):
#         start -= self._buffer_pos
#         if start < 0:
#             raise IndexError('Negative indexes not supported')
#         return self._buff[start]
# 
#     def __getitem__(self, index):
#         if isinstance(index, int):
#             start = index
#             return self._getitem(start)
#         else:
#             start = index.start
#             stop = index.stop
#             step = index.step
#         if start is None:
#             start = 0
# 
#         if start < 0 or stop < 0:
#             raise IndexError('Negative indexes not supported')
# 
#         buff_pos = self._buffer_pos
#         start -= buff_pos
#         stop -= buff_pos
# 
#         buff = self._buff
#         if start < 0:
#             raise IndexError('Index outside buffered window')
#         if (not self._stream_consumed and
#            (stop is None or stop > len(buff))):
#             raise IndexError('Index outside buffered window')
# 
#         return buff[start:stop:step]


from itertools import chain
from collections import Counter, namedtuple
from scipy.stats import fisher_exact as scipy_fisher
from vcf import Reader as pyvcfReader
from crumbs.vcf.statistics import choose_samples
from crumbs.iterutils import RandomAccessIterator
    from crumbs.plot import build_histogram, draw_density_plot
    from os.path import join as pjoin
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from scipy.stats import gaussian_kde
import numpy
            cache_indx1 = {}
            self.cache[index[0]] = cache_indx1
        cache_indx1[index[1]] = value

    def get_stat(self, snv1, snv2):
        index = self._sort_index(snv1, snv2)
        cache_indx1 = self.cache[index[0]]
        return cache_indx1[index[1]]

    def del_lower_than(self, min_index0):
        cache = self.cache
        to_del = []
        for key in cache.keys():
            if key < min_index0:
                to_del.append(key)
        for key in to_del:
            del cache[key]


def filter_snvs_by_ld(snvs, samples=None, r_sqr=DEF_R_SQR_THRESHOLD,
                      p_val=DEF_P_VAL, bonferroni=True, snv_win=DEF_SNV_WIN,
                      min_phys_dist=MIN_PHYS_DIST, log_fhand=None):
    if not snv_win % 2:
        msg = 'The window should have an odd number of snvs'
        raise ValueError(msg)
    half_win = (snv_win - 1) // 2

    if bonferroni:
        p_val /= (snv_win - 1)


# Path: crumbs/vcf/statistics.py
# def choose_samples(record, sample_names):
#     if sample_names is None:
#         chosen_samples = record.samples
#     else:
#         filter_by_name = lambda x: True if x.sample in sample_names else False
#         chosen_samples = filter(filter_by_name, record.samples, )
#     return chosen_samples
# 
# Path: crumbs/iterutils.py
# class RandomAccessIterator(object):
#     def __init__(self, iterable, rnd_access_win):
#         self._stream = iterable
#         if not rnd_access_win % 2:
#             msg = 'rnd_access_win should be odd'
#             raise ValueError(msg)
#         self._rnd_access_win = rnd_access_win
#         self._buff = []
#         self._curr_item_in_buff = None
#         self._buffer_pos = 0
#         self._stream_consumed = False
#         self._fill_buffer()
# 
#     def _fill_buffer(self):
#         half_win = (self._rnd_access_win - 1) // 2
#         self._buff = list(islice(self._stream, half_win))
#         if len(self._buff) < half_win:
#             self._stream_consumed = True
# 
#     def __iter__(self):
#         return self
# 
#     def next(self):
#         in_buffer = self._buff
#         try:
#             stream_next = self._stream.next()
#         except StopIteration:
#             self._stream_consumed = True
# 
#         if not self._stream_consumed:
#             in_buffer.append(stream_next)
# 
#         if self._curr_item_in_buff is None:
#             self._curr_item_in_buff = 0
#         else:
#             self._curr_item_in_buff += 1
# 
#         try:
#             item_to_yield = in_buffer[self._curr_item_in_buff]
#         except IndexError:
#             raise StopIteration
# 
#         if len(in_buffer) > self._rnd_access_win:
#             in_buffer.pop(0)
#             self._curr_item_in_buff -= 1
#             self._buffer_pos += 1
# 
#         return item_to_yield
# 
#     def _getitem(self, start):
#         start -= self._buffer_pos
#         if start < 0:
#             raise IndexError('Negative indexes not supported')
#         return self._buff[start]
# 
#     def __getitem__(self, index):
#         if isinstance(index, int):
#             start = index
#             return self._getitem(start)
#         else:
#             start = index.start
#             stop = index.stop
#             step = index.step
#         if start is None:
#             start = 0
# 
#         if start < 0 or stop < 0:
#             raise IndexError('Negative indexes not supported')
# 
#         buff_pos = self._buffer_pos
#         start -= buff_pos
#         stop -= buff_pos
# 
#         buff = self._buff
#         if start < 0:
#             raise IndexError('Index outside buffered window')
#         if (not self._stream_consumed and
#            (stop is None or stop > len(buff))):
#             raise IndexError('Index outside buffered window')
# 
#         return buff[start:stop:step]


# Path: crumbs/vcf/ld.py
from itertools import chain
from collections import Counter, namedtuple
from scipy.stats import fisher_exact as scipy_fisher
from vcf import Reader as pyvcfReader
from crumbs.vcf.statistics import choose_samples
from crumbs.iterutils import RandomAccessIterator
    from crumbs.plot import build_histogram, draw_density_plot
    from os.path import join as pjoin
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from scipy.stats import gaussian_kde
import numpy

            cache_indx1 = {}
            self.cache[index[0]] = cache_indx1
        cache_indx1[index[1]] = value

    def get_stat(self, snv1, snv2):
        index = self._sort_index(snv1, snv2)
        cache_indx1 = self.cache[index[0]]
        return cache_indx1[index[1]]

    def del_lower_than(self, min_index0):
        cache = self.cache
        to_del = []
        for key in cache.keys():
            if key < min_index0:
                to_del.append(key)
        for key in to_del:
            del cache[key]


def filter_snvs_by_ld(snvs, samples=None, r_sqr=DEF_R_SQR_THRESHOLD,
                      p_val=DEF_P_VAL, bonferroni=True, snv_win=DEF_SNV_WIN,
                      min_phys_dist=MIN_PHYS_DIST, log_fhand=None):
    if not snv_win % 2:
        msg = 'The window should have an odd number of snvs'
        raise ValueError(msg)
    half_win = (snv_win - 1) // 2

    if bonferroni:
        p_val /= (snv_win - 1)


    snvs = RandomAccessIterator(snvs, rnd_access_win=snv_win)