def read_wig(filename, strand, libs): wig_parser = WigParser() wigs = {} if filename is not False: wig_fh = open(filename) for entry in wig_parser.parser(wig_fh, strand): if entry.strain not in wigs.keys(): wigs[entry.strain] = {} for lib in libs: if lib["cond"] not in wigs[entry.strain]: wigs[entry.strain][lib["cond"]] = {} for lib in libs: if (lib["name"] == entry.track) and ( lib["strand"] == entry.strand): lib_name = "|".join([ entry.track, entry.strand, lib["type"]]) if lib_name not in wigs[entry.strain][lib["cond"]].keys(): wigs[entry.strain][lib["cond"]][lib_name] = [] wigs[entry.strain][lib["cond"]][lib_name].append(entry.coverage) wig_fh.close() for strain, conds in wigs.items(): for cond, lib_names in conds.items(): for lib_name, cover_list in lib_names.items(): wigs[strain][cond][lib_name] = np.array( wigs[strain][cond][lib_name]) return wigs
def read_wig(filename, strand, libs): wig_parser = WigParser() wigs = {} if filename is not False: wig_fh = open(filename) for entry in wig_parser.parser(wig_fh, strand): if entry.strain not in wigs.keys(): strain = entry.strain wigs[strain] = {} for lib in libs: if lib["cond"] not in wigs[strain]: wigs[strain][lib["cond"]] = {} for lib in libs: if (lib["name"] == entry.track) and (lib["strand"] == entry.strand): lib_name = "|".join( [entry.track, entry.strand, lib["type"]]) if lib_name not in wigs[strain][lib["cond"]].keys(): wigs[strain][lib["cond"]][lib_name] = [] wigs[strain][lib["cond"]][lib_name].append(entry.coverage) wig_fh.close() for strain, conds in wigs.items(): for cond, lib_names in conds.items(): for lib_name, cover_list in lib_names.items(): wigs[strain][cond][lib_name] = np.array( wigs[strain][cond][lib_name]) return wigs
class TestParserWig(unittest.TestCase): def setUp(self): self.example = Example() self.wig_parser = WigParser() self.test_folder = "test_folder" if (not os.path.exists(self.test_folder)): os.mkdir(self.test_folder) def tearDown(self): if os.path.exists(self.test_folder): shutil.rmtree(self.test_folder) def test_parser(self): wigs = [] wig_f_fh = StringIO(self.example.wig_forward_file) for entry in self.wig_parser.parser(wig_f_fh, "+"): self.assertEqual(entry.strain, "aaa") self.assertEqual(entry.track, "TSB_t0_TEX_forward") wigs.append(entry) self.assertEqual(wigs[2].pos, 3) self.assertEqual(wigs[2].coverage, 1.4041251228308191) wigs = [] wig_r_fh = StringIO(self.example.wig_reverse_file) for entry in self.wig_parser.parser(wig_r_fh, "-"): self.assertEqual(entry.strain, "aaa") self.assertEqual(entry.track, "TSB_t0_TEX_reverse") wigs.append(entry) self.assertEqual(wigs[2].pos, 3) self.assertEqual(wigs[2].coverage, 1.4041251228308191)
def import_wig(lib, wigs, strand): wig_parser = WigParser() for wig in lib: wigs[wig] = {} strain = "" wig_fh = open(wig) for entry in wig_parser.parser(wig_fh, strand): if strain != entry.strain: wigs[wig][entry.strain] = [] strain = entry.strain wigs[wig][strain].append(entry) wig_fh.close()
def read_wig(filename, strand): wigs = {} wig_parser = WigParser() if filename: wig_fh = open(filename) for entry in wig_parser.parser(wig_fh, strand): if entry.strain not in wigs.keys(): strain = entry.strain wigs[strain] = {} if entry.track not in wigs[strain].keys(): wigs[strain][entry.track] = [] wigs[strain][entry.track].append({ "pos": entry.pos, "coverage": entry.coverage, "strand": entry.strand}) wig_fh.close() return wigs
def read_wig(filename, strand, libs): wig_parser = WigParser() wigs = {} if filename is not False: wig_fh = open(filename) for entry in wig_parser.parser(wig_fh, strand): if entry.strain not in wigs.keys(): strain = entry.strain wigs[strain] = {} for lib in libs: if lib["cond"] not in wigs[strain]: wigs[strain][lib["cond"]] = {} for lib in libs: if (lib["name"] == entry.track) and ( lib["strand"] == entry.strand): if entry.track not in wigs[strain][lib["cond"]].keys(): wigs[strain][lib["cond"]][entry.track] = [] wigs[strain][lib["cond"]][entry.track].append({ "pos": entry.pos, "coverage": entry.coverage, "strand": entry.strand, "type": lib["type"]}) wig_fh.close() return wigs
def read_wig(filename, libs, strand): wigs = {} tracks = [] wig_parser = WigParser() wig_fh = open(filename) for entry in wig_parser.parser(wig_fh, strand): if entry.track not in tracks: tracks.append(entry.track) if entry.strain not in wigs.keys(): strain = entry.strain wigs[entry.strain] = [] for lib in libs: if strand == lib["strand"]: for key, value in lib.items(): if (key == "name") and (value == entry.track): cond = lib["cond"] track = value wigs[strain].append({ "pos": entry.pos, "coverage": entry.coverage, "strand": entry.strand, "track": track, "cond": cond}) break wig_fh.close() return wigs
def setUp(self): self.example = Example() self.wig_parser = WigParser() self.test_folder = "test_folder" if (not os.path.exists(self.test_folder)): os.mkdir(self.test_folder)