Skip to content

Commit

Permalink
use with for opening filehandles where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
susannasiebert committed Dec 1, 2016
1 parent 781db86 commit 935788e
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions pvacseq/lib/input_file_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ def __init__(self, **kwargs):
self.trna_indels_coverage_file = kwargs['trna_indels_coverage_file']

def parse_bam_readcount_file(self, bam_readcount_file):
reader = open(bam_readcount_file, 'r')
coverage_tsv_reader = csv.reader(reader, delimiter='\t')
coverage = {}
for row in coverage_tsv_reader:
chromosome = row[0]
position = row[1]
reference_base = row[2].upper()
depth = row[3]
brct = row[4:]
if chromosome not in coverage:
coverage[chromosome] = {}
if position not in coverage[chromosome]:
coverage[chromosome][position] = {}
coverage[chromosome][position][reference_base] = brct
reader.close()
with open(bam_readcount_file, 'r') as reader:
coverage_tsv_reader = csv.reader(reader, delimiter='\t')
coverage = {}
for row in coverage_tsv_reader:
chromosome = row[0]
position = row[1]
reference_base = row[2].upper()
depth = row[3]
brct = row[4:]
if chromosome not in coverage:
coverage[chromosome] = {}
if position not in coverage[chromosome]:
coverage[chromosome][position] = {}
coverage[chromosome][position][reference_base] = brct
return coverage

def parse_brct_field(self, brct_entry):
Expand Down Expand Up @@ -151,21 +150,19 @@ def output_headers(self):
def execute(self):
gene_expns = {}
if self.gene_expn_file is not None:
reader = open(self.gene_expn_file, 'r')
genes_tsv_reader = csv.DictReader(reader, delimiter='\t')
for row in genes_tsv_reader:
if row['tracking_id'] not in gene_expns.keys():
gene_expns[row['tracking_id']] = {}
gene_expns[row['tracking_id']][row['locus']] = row
reader.close()
with open(self.gene_expn_file, 'r') as reader:
genes_tsv_reader = csv.DictReader(reader, delimiter='\t')
for row in genes_tsv_reader:
if row['tracking_id'] not in gene_expns.keys():
gene_expns[row['tracking_id']] = {}
gene_expns[row['tracking_id']][row['locus']] = row

transcript_expns = {}
if self.transcript_expn_file is not None:
reader = open(self.transcript_expn_file, 'r')
isoforms_tsv_reader = csv.DictReader(reader, delimiter='\t')
for row in isoforms_tsv_reader:
transcript_expns[row['tracking_id']] = row
reader.close()
with open(self.transcript_expn_file, 'r') as reader:
isoforms_tsv_reader = csv.DictReader(reader, delimiter='\t')
for row in isoforms_tsv_reader:
transcript_expns[row['tracking_id']] = row

coverage = {}
for variant_type in ['snvs', 'indels']:
Expand All @@ -179,11 +176,12 @@ def execute(self):

reader = open(self.input_file, 'r')
vcf_reader = vcf.Reader(reader)
writer = open(self.output_file, 'w')
if len(vcf_reader.samples) > 1:
sys.exit('ERROR: VCF file contains more than one sample')
writer = open(self.output_file, 'w')
tsv_writer = csv.DictWriter(writer, delimiter='\t', fieldnames=self.output_headers())
tsv_writer.writeheader()

csq_format = self.parse_csq_format(vcf_reader)
transcript_count = {}
for entry in vcf_reader:
Expand Down

0 comments on commit 935788e

Please sign in to comment.