本文整理汇总了Python中vcf.Writer方法的典型用法代码示例。如果您正苦于以下问题:Python vcf.Writer方法的具体用法?Python vcf.Writer怎么用?Python vcf.Writer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vcf
的用法示例。
在下文中一共展示了vcf.Writer方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _write_bad_variants
# 需要导入模块: import vcf [as 别名]
# 或者: from vcf import Writer [as 别名]
def _write_bad_variants(self, vcf_out):
"""**PRIVATE:** Write only those records that **haven't** passed."""
written_variants = 0
# Check if the output file ends with .gz, then compress data.
open_func = gzip.open if vcf_out.endswith(".gz") else open
with open_func(vcf_out, "w") as out_vcf:
writer = vcf.Writer(out_vcf, self.out_template)
for record in self._variants:
if record.FILTER != "PASS" and record.FILTER is not None:
writer.write_record(record)
written_variants += 1
return written_variants
示例2: convert_svtool_to_vcf
# 需要导入模块: import vcf [as 别名]
# 或者: from vcf import Writer [as 别名]
def convert_svtool_to_vcf(file_name, sample, out_vcf, toolname, reference, sort=False, index=False):
vcf_template_reader = get_template()
vcf_template_reader.samples = [sample]
vcf_fd = open(out_vcf, "w") if out_vcf is not None else sys.stdout
vcf_writer = vcf.Writer(vcf_fd, vcf_template_reader)
reference_handle = pysam.Fastafile(reference) if reference else None
reference_contigs = get_contigs(reference)
if sort:
if not reference_contigs:
logger.warn("Chromosomes will be sorted in lexicographic order since reference is missing")
else:
logger.info("Chromosomes will be sorted by the reference order")
vcf_records = []
for tool_record in tool_to_reader[toolname](file_name, reference_handle=reference_handle):
vcf_record = tool_record.to_vcf_record(sample)
if vcf_record is None:
continue
if sort:
vcf_records.append(vcf_record)
else:
vcf_writer.write_record(vcf_record)
if sort:
if reference_contigs:
contigs_order_dict = {contig.name: index for (index, contig) in enumerate(reference_contigs)}
vcf_records.sort(
cmp=lambda x, y: cmp((contigs_order_dict[x.CHROM], x.POS), (contigs_order_dict[y.CHROM], y.POS)))
else:
vcf_records.sort(cmp=lambda x, y: cmp((x.CHROM, x.POS), (y.CHROM, y.POS)))
for vcf_record in vcf_records:
vcf_writer.write_record(vcf_record)
vcf_writer.close()
if out_vcf and index:
pysam.tabix_index(out_vcf, force=True, preset='vcf')
示例3: print_vcf_header
# 需要导入模块: import vcf [as 别名]
# 或者: from vcf import Writer [as 别名]
def print_vcf_header():
"""
Creates vcf header by setting vcf format and calling functions to create each header section.
"""
global vcf_writer, vcf
vcf = py_vcf.Reader(io.StringIO("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t"+ str(bam.sample_name)))
set_metadata_header(vcf)
set_contig_header(vcf)
set_info_header(vcf)
set_alt_header(vcf)
set_format_header(vcf)
set_filter_header(vcf)
vcf_writer = py_vcf.Writer(NanoSV.opts_output, vcf)
示例4: write_variants
# 需要导入模块: import vcf [as 别名]
# 或者: from vcf import Writer [as 别名]
def write_variants(self, vcf_out, only_snps=False, only_good=False):
"""Write _variants to a VCF file.
Parameters
----------
vcf_out: str
Path to the file where VCF data is written.
only_snps: bool, optional
True is *only* SNP are to be written to the output (default: False).
only_good: bool, optional
True if only those records that PASS all filters should be written
(default: False).
Returns:
int:
Number of records written.
"""
written_variants = 0
# Check if the output file ends with .gz, then compress data.
open_func = gzip.open if vcf_out.endswith(".gz") else open
with open_func(vcf_out, "w") as out_vcf:
writer = vcf.Writer(out_vcf, self.out_template)
# Output internal _variants (if exist) otherwise, output data from reader.
variants = self._variants if self._variants else self._reader
for record in variants:
if only_snps and not record.is_snp:
continue
if only_good and record.FILTER:
continue
writer.write_record(record)
written_variants += 1
return written_variants
# --------------------------------------------------------------------------------------------------