def test_read_write(self): zip_path = './data/test.bgzf' with BlockZip(zip_path, 'w') as f: f.write('chr1', 100, False, 'yes') f.write('chr1', 101, False, 'yes') f.write('chr2', 2101, True, 'yes') f.write('chrX', 0, False, 'X') with BlockZip(zip_path, 'r') as f: self.assertEqual(f[('chr1', 100, False)], 'yes') self.assertEqual(f[('chr1', 0, False)], None) self.assertEqual(f[('chrX', 0, False)], 'X') self.assertEqual(f[('chr2', 2101, True)], 'yes') self.assertEqual(f[('chr1', 0, True)], None) # Test verification: with BlockZip(zip_path, 'r') as f: f.verify() with BlockZip(zip_path, 'w') as f: f.write('chr1', 101, False, 'yes') f.write('chr1', 100, False, 'yes') f.write('chr2', 2101, True, 'yes') f.write('chrX', 0, False, 'X') with BlockZip(zip_path, 'r') as bz: self.assertRaises(ValueError, bz.verify) # Test for missing index: os.remove(zip_path + '.idx') self.assertRaises(ValueError, BlockZip, './non_existing.bgzf', 'r') os.remove(zip_path) self.assertRaises(ValueError, BlockZip, './non_existing.bgzf', 'r')
def __init__(self, mapability_safe_file_path, read_all=False, dont_open=True): self.args = locals().copy() del self.args['self'] self.mapability_safe_file_path = mapability_safe_file_path if not dont_open: self.handle = BlockZip(mapability_safe_file_path, 'r')
def site_is_mapable(self, contig, ds, strand): if self.handle is None: self.handle = BlockZip(self.mapability_safe_file_path, 'r') """ Obtain if a restriction site is mapable or not Args: contig (str) : contig of site to look up ds (int) : zero based coordinate of site to look up strand (bool) : strand of site to look up (False: FWD, True: REV) Returns: site_is_mapable (bool) : True when the site is uniquely mapable, False otherwise """ if self.handle[contig, ds, strand] == 'ok': return True return False
if read_site == site_pos and site_chrom == read_contig and read.is_reverse == strand: # Correct assignment sites[key]['correct'] += 1 else: # Assign a missing count to the origin site: sites[key]['lost'] += 1 # Assing a wrong annotation to the target site: if key_mapped not in sites: sites[key_mapped] = {'correct': 0, 'wrong_gain': 0, 'lost': 0} sites[key_mapped]['wrong_gain'] += 1 print("Writing site statistics ...") outtab = f'simulated_{args.digest_sequence}_single_{r1_read_length}.mappability.stats.bgzf' outtabsafe = f'simulated_{args.digest_sequence}_single_{r1_read_length}.mappability.safe.bgzf' keys = sorted([(contig, pos, strand) for (contig, pos, strand) in sites.keys() if contig is not None]) with BlockZip(outtab, 'w') as stats, BlockZip(outtabsafe, 'w') as safe: for (contig, pos, strand) in keys: measured = sites[(contig, pos, strand)] stats.write( contig, pos, strand, f'{measured["correct"]}\t{measured["lost"]}\t{measured["wrong_gain"]}' ) if measured['wrong_gain'] == 0 and measured[ 'lost'] == 0 and measured['correct'] == 1: safe.write(contig, pos, strand, 'ok')
def __getitem__(self, contig_ds_strand): if self.handle is None: self.handle = BlockZip(self.mapability_safe_file_path, 'r') contig, ds, strand = contig_ds_strand return self.handle[contig, ds, strand]
def __init__(self, mapability_safe_file_path): self.mapability_safe_file_path = mapability_safe_file_path self.handle = BlockZip(mapability_safe_file_path, 'r')