# Path: crumbs/iterutils.py
# def generate_windows(size, step=None, start=0, end=None):
#     if step is None:
#         step = size
# 
#     win_start = None
#     while True:
#         if win_start is None:
#             win_start = start
#         else:
#             win_start += step
#         win_end = win_start + size
#         if end:
#             if win_end > end:
#                 break
#         yield Window(win_start, win_end)
# 
# Path: crumbs/seq/seqio.py
# def read_seqs(fhands, out_format=None, prefered_seq_classes=None):
#     'It returns a stream of seqs in different codings: seqrecords, seqitems...'
# 
#     if not prefered_seq_classes:
#         prefered_seq_classes = [SEQITEM, SEQRECORD]
#     try:
#         in_format = get_format(fhands[0])
#     except FileIsEmptyError:
#         return []
#     # seqitems is incompatible with different input and output formats
#     # or when in_format != a fasta or fastq
#     if ((out_format not in (None, GUESS_FORMAT) and in_format != out_format
#          and SEQITEM in prefered_seq_classes) or
#         (in_format not in ('fasta',) + SANGER_FASTQ_FORMATS +
#          ILLUMINA_FASTQ_FORMATS)):
#         prefered_seq_classes.pop(prefered_seq_classes.index(SEQITEM))
# 
#     if not prefered_seq_classes:
#         msg = 'No valid seq class left or prefered'
#         raise ValueError(msg)
# 
#     for seq_class in prefered_seq_classes:
#         if seq_class == SEQITEM:
#             try:
#                 return _read_seqitems(fhands)
#             except NotImplementedError:
#                 continue
#         elif seq_class == SEQRECORD:
#             try:
#                 seqs = _read_seqrecords(fhands)
#                 return assing_kind_to_seqs(SEQRECORD, seqs, None)
#             except NotImplementedError:
#                 continue
#         else:
#             raise ValueError('Unknown class for seq: ' + seq_class)
#     raise RuntimeError('We should not be here, fixme')
# 
# Path: crumbs/seq/seq.py
# def get_name(seq):
#     if 'SeqRecord' in seq.__class__.__name__:
#         seq_class = SEQRECORD
#     else:
#         seq_class = seq.kind
#         seq = seq.object
#     if seq_class == SEQITEM:
#         name = seq.name
#     elif seq_class == SEQRECORD:
#         name = seq.id
#     return name
# 
# def get_length(seq):
#     return len(get_str_seq(seq))
# 
# Path: crumbs/utils/file_utils.py
# def flush_fhand(fhand):
#     try:
#         fhand.flush()
#     except IOError, error:
#         # The pipe could be already closed
#         if 'Broken pipe' not in str(error):
#             raise


import sys
import gzip
from collections import Counter, OrderedDict, namedtuple
from operator import itemgetter
from vcf import Reader as pyvcfReader
from vcf import Writer as pyvcfWriter
from vcf.model import make_calldata_tuple
from vcf.model import _Call as pyvcfCall
from vcf.model import _Record as pyvcfRecord
from crumbs.iterutils import generate_windows
from crumbs.seq.seqio import read_seqs
from crumbs.seq.seq import get_name, get_length
from crumbs.utils.file_utils import flush_fhand
            from traceback import print_exception
    @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):

# Path: crumbs/iterutils.py
# def generate_windows(size, step=None, start=0, end=None):
#     if step is None:
#         step = size
# 
#     win_start = None
#     while True:
#         if win_start is None:
#             win_start = start
#         else:
#             win_start += step
#         win_end = win_start + size
#         if end:
#             if win_end > end:
#                 break
#         yield Window(win_start, win_end)
# 
# Path: crumbs/seq/seqio.py
# def read_seqs(fhands, out_format=None, prefered_seq_classes=None):
#     'It returns a stream of seqs in different codings: seqrecords, seqitems...'
# 
#     if not prefered_seq_classes:
#         prefered_seq_classes = [SEQITEM, SEQRECORD]
#     try:
#         in_format = get_format(fhands[0])
#     except FileIsEmptyError:
#         return []
#     # seqitems is incompatible with different input and output formats
#     # or when in_format != a fasta or fastq
#     if ((out_format not in (None, GUESS_FORMAT) and in_format != out_format
#          and SEQITEM in prefered_seq_classes) or
#         (in_format not in ('fasta',) + SANGER_FASTQ_FORMATS +
#          ILLUMINA_FASTQ_FORMATS)):
#         prefered_seq_classes.pop(prefered_seq_classes.index(SEQITEM))
# 
#     if not prefered_seq_classes:
#         msg = 'No valid seq class left or prefered'
#         raise ValueError(msg)
# 
#     for seq_class in prefered_seq_classes:
#         if seq_class == SEQITEM:
#             try:
#                 return _read_seqitems(fhands)
#             except NotImplementedError:
#                 continue
#         elif seq_class == SEQRECORD:
#             try:
#                 seqs = _read_seqrecords(fhands)
#                 return assing_kind_to_seqs(SEQRECORD, seqs, None)
#             except NotImplementedError:
#                 continue
#         else:
#             raise ValueError('Unknown class for seq: ' + seq_class)
#     raise RuntimeError('We should not be here, fixme')
# 
# Path: crumbs/seq/seq.py
# def get_name(seq):
#     if 'SeqRecord' in seq.__class__.__name__:
#         seq_class = SEQRECORD
#     else:
#         seq_class = seq.kind
#         seq = seq.object
#     if seq_class == SEQITEM:
#         name = seq.name
#     elif seq_class == SEQRECORD:
#         name = seq.id
#     return name
# 
# def get_length(seq):
#     return len(get_str_seq(seq))
# 
# Path: crumbs/utils/file_utils.py
# def flush_fhand(fhand):
#     try:
#         fhand.flush()
#     except IOError, error:
#         # The pipe could be already closed
#         if 'Broken pipe' not in str(error):
#             raise


# Path: crumbs/vcf/snv.py
import sys
import gzip
from collections import Counter, OrderedDict, namedtuple
from operator import itemgetter
from vcf import Reader as pyvcfReader
from vcf import Writer as pyvcfWriter
from vcf.model import make_calldata_tuple
from vcf.model import _Call as pyvcfCall
from vcf.model import _Record as pyvcfRecord
from crumbs.iterutils import generate_windows
from crumbs.seq.seqio import read_seqs
from crumbs.seq.seq import get_name, get_length
from crumbs.utils.file_utils import flush_fhand
            from traceback import print_exception

    @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)