Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Berselli authored and Michele Berselli committed Jan 12, 2021
1 parent 80144f8 commit a47a9e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
38 changes: 7 additions & 31 deletions granite/lib/vcf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ def get_genotype_value(self, ID_genotype, tag_to_get, sep=':'):

@staticmethod
def read_vcf(inputfile):
''' generator that reads vcf file, gzipped or ungzipped '''
''' read vcf file, gzipped or ungzipped,
return a generator '''
if inputfile.endswith('.gz'):
with gzip.open(inputfile, 'rb') as fz:
for byteline in fz:
Expand Down Expand Up @@ -352,49 +353,24 @@ def parse_variants(self): # generator
#end def

def write_definitions(self, outputfile_obj):
''' write header definitions to file.
::
with open(somefilename, 'w') as fo:
vcf_obj.write_definitions(fo)
'''
''' write header definitions to outputfile_obj buffer '''
outputfile_obj.write(self.header.definitions)
#end def

def write_columns(self, outputfile_obj):
''' write header columns to file,
#CHROM ...
::
with open(somefilename, 'w') as fo:
vcf_obj.write_columns(fo)
'''
''' write header columns to outputfile_obj buffer,
#CHROM ... '''
outputfile_obj.write(self.header.columns)
#end def

def write_header(self, outputfile_obj):
''' write header definitions and columns to file.
::
with open(somefilename, 'w') as fo:
vcf_obj.write_header(fo)
'''
''' write header definitions and columns to outputfile_obj buffer '''
self.write_definitions(outputfile_obj)
self.write_columns(outputfile_obj)
#end def

def write_variant(self, outputfile_obj, parsed_variant):
''' write parsed_variant (Variant object generated by parse_variants) to file.
::
with open(somefilename, 'w') as fo:
for vnt_obj in vcf_obj.parse_variants():
vcf_obj.write_variant(fo, vnt_obj)
'''
''' write parsed_variant (Variant object generated by parse_variants) to outputfile_obj buffer '''
outputfile_obj.write(parsed_variant.to_string())
#end def

Expand Down
20 changes: 15 additions & 5 deletions tests/test_vcf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_read_vcf():
vcflines = vcf_parser.Vcf.read_vcf('tests/files/input_vcf_parser.vcf')
assert next(vcflines) == '##fileformat=VCFv4.1\n'
assert next(vcflines) == '##ALT=<ID=NON_REF,Description="Represents any possible alternative allele at this location">\n'
#end def

def test_read_vcf_gz():
''' '''
Expand All @@ -84,21 +85,25 @@ def test_read_vcf_gz():
assert next(vcflines) == next(vcflines_gz)
assert next(vcflines) == next(vcflines_gz)
assert next(vcflines) == next(vcflines_gz)
#end def

def test_write_header():
''' '''
# Creating Vcf object
vcf_obj = vcf_parser.Vcf('tests/files/input_vcf_parser.vcf')
# write
# Write
with open('tests/files/test_write_header.out', 'w') as fo:
vcf_obj.write_definitions(fo)
# test
#end with
# Test
with open('tests/files/test_write_header.out', 'r') as f:
output_headers = f.read()
#end with
input_headers = vcf_obj.header.definitions
assert output_headers == input_headers
# clean up
# Clean up
os.remove('tests/files/test_write_header.out')
#end def

def test_write_variant():
''' '''
Expand All @@ -108,10 +113,15 @@ def test_write_variant():
with open('tests/files/test_write_variants.out', 'w') as fo:
for rec in vcf_obj.parse_variants():
vcf_obj.write_variant(fo, rec)
# test
#end for
#end with
# Test
recs = vcf_obj.parse_variants()
with open('tests/files/test_write_variants.out', 'r') as f:
for line in f:
assert line == next(recs).to_string()
# clean up
#end for
#end with
# Clean up
os.remove('tests/files/test_write_variants.out')
#end def

0 comments on commit a47a9e5

Please sign in to comment.