# 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
# 
# class VCFWriter(pyvcfWriter):
# 
#     def __init__(self, stream, template_reader, lineterminator="\n"):
#         template = template_reader.pyvcf_reader
#         super(VCFWriter, self).__init__(stream, template,
#                                         lineterminator=lineterminator)
# 
#     def write_snv(self, snv):
#         super(VCFWriter, self).write_record(snv.record)
# 
#     def write_snvs(self, snvs):
#         for snv in snvs:
#             try:
#                 self.write_snv(snv)
#             except IOError, error:
#                 # The pipe could be already closed
#                 if 'Broken pipe' in str(error):
#                     break
#                 else:
#                     raise
# 
#     def flush(self):
#         flush_fhand(self.stream)


from collections import Counter
from StringIO import StringIO
from crumbs.vcf.snv import VCFReader, VCFWriter
from __future__ import division


# Missing docstring
# pylint: disable=C0111
# Too few public methods
# pylint: disable=R0903

DEF_PROB_AA_THRESHOLD = 0.9999
HW = 'hw'
RIL_SELF = 'ril_self'


def run_genotype_filters(in_fhand, out_fhand, gt_filters, reader_kwargs=None):
    if reader_kwargs is None:
        reader_kwargs = {}

    reader_kwargs['filename'] = 'pyvcf_bug_workaround'
    reader_kwargs['compressed'] = False

# 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
# 
# class VCFWriter(pyvcfWriter):
# 
#     def __init__(self, stream, template_reader, lineterminator="\n"):
#         template = template_reader.pyvcf_reader
#         super(VCFWriter, self).__init__(stream, template,
#                                         lineterminator=lineterminator)
# 
#     def write_snv(self, snv):
#         super(VCFWriter, self).write_record(snv.record)
# 
#     def write_snvs(self, snvs):
#         for snv in snvs:
#             try:
#                 self.write_snv(snv)
#             except IOError, error:
#                 # The pipe could be already closed
#                 if 'Broken pipe' in str(error):
#                     break
#                 else:
#                     raise
# 
#     def flush(self):
#         flush_fhand(self.stream)


# Path: crumbs/vcf/genotype_filters.py
from collections import Counter
from StringIO import StringIO
from crumbs.vcf.snv import VCFReader, VCFWriter

from __future__ import division


# Missing docstring
# pylint: disable=C0111
# Too few public methods
# pylint: disable=R0903

DEF_PROB_AA_THRESHOLD = 0.9999
HW = 'hw'
RIL_SELF = 'ril_self'


def run_genotype_filters(in_fhand, out_fhand, gt_filters, reader_kwargs=None):
    if reader_kwargs is None:
        reader_kwargs = {}

    reader_kwargs['filename'] = 'pyvcf_bug_workaround'
    reader_kwargs['compressed'] = False

    reader = VCFReader(in_fhand, **reader_kwargs)