def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Prepare paths # orig_bigwig_path = info['bigwig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bigwig_path = temporary_path('.bigwig') # From bigwig to SQL # try: track.convert(orig_bigwig_path, test_sql_path, assembly='sacCer2') except MissingExecutableError as err: sys.stderr.write("skipping: {0} ".format(err.message)) break else: self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to bigwig # try: track.convert(test_sql_path, test_bigwig_path) except MissingExecutableError as err: sys.stderr.write("skipping: {0} ".format(err.message)) break else: self.assertTrue(assert_file_equal(orig_bigwig_path, test_bigwig_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bigwig_path)
def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Prepare paths # orig_bigwig_path = info['bigwig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bigwig_path = temporary_path('.bigwig') # From bigwig to SQL # try: track.convert(orig_bigwig_path, test_sql_path, assembly='sacCer2') except MissingExecutableError as err: sys.stderr.write("skipping: {0} ".format(err.message)) break else: self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to bigwig # try: track.convert(test_sql_path, test_bigwig_path) except MissingExecutableError as err: sys.stderr.write("skipping: {0} ".format(err.message)) break else: self.assertTrue( assert_file_equal(orig_bigwig_path, test_bigwig_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bigwig_path)
def serialize_chr_file(chrmeta, path=None, seperator='\t'): """Read a dictionary and create a plain text file""" if not path: path = temporary_path() def lines(): for k,v in chrmeta.items(): yield k + seperator + str(v['length']) + '\n' with open(path, 'w') as f: f.writelines(lines()) return path
def runTest(self): out_path = temporary_path('.sql') with track.new(out_path) as t: for chrom in range(5): t.write(str(chrom), [(0,10,'A',0.0,-1)]) cur = t.cursor() cur.execute("CREATE table tmp (koopa text,troopa text)") cur.execute("INSERT into tmp values (?,?)", (1,2))
def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Prepare paths # orig_bigwig_path = info['bigwig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bigwig_path = temporary_path('.bigwig') # From bigwig to SQL # track.convert(orig_bigwig_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to bigwig # track.convert(test_sql_path, test_bigwig_path) self.assertTrue(assert_file_equal(orig_bigwig_path, test_bigwig_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bigwig_path)
def runTest(self): out_path = temporary_path('.sql') with track.new(out_path) as t: for chrom in range(5): t.write(str(chrom), [(0, 10, 'A', 0.0, -1)]) cur = t.cursor() cur.execute("CREATE table tmp (koopa text,troopa text)") cur.execute("INSERT into tmp values (?,?)", (1, 2))
def runTest(self): for num, info in sorted(samples['small_features'].items()): # Prepare paths # orig_bed_path = info['bed'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bed_path = temporary_path('.bed') # From BED to SQL # track.convert(orig_bed_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to BED # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_bed_path) self.assertTrue(assert_file_equal(orig_bed_path, test_bed_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bed_path)
def runTest(self): for num, info in sorted(samples['gtf_tracks'].items()): if num == 'GenRep': continue orig_gtf_path = info['gtf'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_gtf_path = temporary_path('.gtf') # From GTF to SQL # track.convert(orig_gtf_path, test_sql_path) self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to GTF # # Use Track() instead. # track.convert(test_sql_path, test_gtf_path) self.assertTrue(assert_file_equal(orig_gtf_path, test_gtf_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_gtf_path)
def runTest(self): in_path = samples['small_signals'][7]['sql'] out_path = temporary_path('.sql') with track.load(in_path) as i: with track.new(out_path) as o: for chrom in i: o.write(chrom, i.read(chrom)) self.assertEqual(list(o.read('chrI')), list(i.read('chrI'))) os.remove(out_path)
def runTest(self): for num, info in sorted(samples['gtf_tracks'].items()): if num == 'GenRep': continue orig_gtf_path = info['gtf'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_gtf_path = temporary_path('.gtf') # From GTF to SQL # track.convert(orig_gtf_path, test_sql_path) self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to GTF # # Use Track() instead. # track.convert(test_sql_path, test_gtf_path) self.assertTrue( assert_file_equal(orig_gtf_path, test_gtf_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_gtf_path)
def runTest(self): for num, info in sorted(samples["gff_tracks"].items()): # Prepare paths # orig_gff_path = info["gff"] orig_sql_path = info["sql"] test_sql_path = temporary_path(".sql") test_gff_path = temporary_path(".gff") # From GFF to SQL # track.convert(orig_gff_path, test_sql_path, assembly="sacCer2") self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to GFF # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_gff_path) self.assertTrue(assert_file_equal(orig_gff_path, test_gff_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_gff_path)
def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Some files cannot be roundtriped # if num == 3 or num == 7: continue # Prepare paths # orig_wig_path = info['wig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_wig_path = temporary_path('.wig') # From WIG to SQL # track.convert(orig_wig_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to WIG # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_wig_path) self.assertTrue(assert_file_equal(orig_wig_path, test_wig_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_wig_path)
def runTest(self): for num, info in sorted(samples['small_features'].items()): # Prepare paths # orig_bed_path = info['bed'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bed_path = temporary_path('.bed') # From BED to SQL # track.convert(orig_bed_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to BED # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_bed_path) self.assertTrue( assert_file_equal(orig_bed_path, test_bed_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bed_path)
def runTest(self): # Prepare paths # orig_gzip_path = samples['gzip_tracks'][1]['gzip'] orig_sql_path = samples['small_features'][1]['sql'] test_sql_path = temporary_path('.sql') # From BED to SQL # track.convert(orig_gzip_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # Clean up # os.remove(test_sql_path)
def runTest(self): for num, info in sorted(samples['rand_signals'].items()): # Prepare paths # orig_wig_path = info['wig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') # From WIG to SQL # track.convert(orig_wig_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # Clean up # os.remove(test_sql_path)
def runTest(self): for num, info in sorted(samples['yeast_features'].items()): # Prepare paths # orig_bed_path = info['bed'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') # From BED to SQL # track.convert(orig_bed_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # Clean up # os.remove(test_sql_path)
def runTest(self): in_path = samples['small_signals'][4]['sql'] out_path = temporary_path('.sql') t = threshold(in_path, 8000.0) t.export(out_path) with track.load(out_path) as t: data = t.read('chrI') got = map(tuple, data) expected = [(120, 122, 9000.0)] self.assertEqual(got, expected) os.remove(out_path)
def runTest(self): for num, info in sorted(samples['sga_tracks'].items()): # Prepare paths # orig_sga_path = info['sga'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') # From SGA to SQL # track.convert(orig_sga_path, test_sql_path, assembly='hg19') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # Clean up # os.remove(test_sql_path)
def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Prepare paths # orig_bigwig_path = info['bigwig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_bigwig_path = temporary_path('.bigwig') # From bigwig to SQL # track.convert(orig_bigwig_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to bigwig # track.convert(test_sql_path, test_bigwig_path) self.assertTrue( assert_file_equal(orig_bigwig_path, test_bigwig_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_bigwig_path)
def runTest(self): info = samples['small_signals'][1] # Prepare paths # orig_sql_path = info['sql'] orig_sga_path = info['sga'] test_sga_path = temporary_path('.sga') # From SGA to SQL # track.convert(orig_sql_path, test_sga_path, assembly='hg19') self.assertTrue(assert_file_equal(orig_sga_path, test_sga_path)) # Clean up # os.remove(test_sga_path)
def serialize_chr_file(chrmeta, path=None, seperator='\t'): """Read a dictionary and create a plain text file""" if not path: path = temporary_path() def lines(): for k, v in chrmeta.items(): yield k + seperator + str(v['length']) + '\n' with open(path, 'w') as f: f.writelines(lines()) return path
def runTest(self): in_path = samples['small_features'][1]['sql'] out_path = temporary_path('.sql') chrom = 'chrI' with track.load(in_path) as i: with track.new(out_path) as o: o.fields = track.default_fields o.write(chrom, i.read(chrom, ('start','end'))) got = tuple(o.read(chrom).next()) expected = (0, 10, None, None, None) self.assertEqual(got, expected) os.remove(out_path)
def runTest(self): in_path = temporary_path('.sql') out_path = temporary_path('.sql') with track.new(in_path) as t: t.fields = ('start','end','score') t.assembly = 'sacCer2' t.write('chrI',[(0,2,10),(2,4,20),(6,8,10)]) result = window_smoothing(t, 2) result.export(out_path) with track.load(out_path) as t: data = t.read('chrI') got = map(tuple, data) expected = [(0, 1, 8.0), (1, 3, 12.0), (3, 5, 10.0), (5, 6, 8.0), (6, 9, 4.0), (9, 10, 2.0)] self.assertEqual(got, expected) os.remove(in_path) os.remove(out_path)
def runTest(self): for num, info in sorted(samples['small_signals'].items()): # Some files cannot be roundtriped # if num == 3 or num == 7: continue # Prepare paths # orig_wig_path = info['wig'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_wig_path = temporary_path('.wig') # From WIG to SQL # track.convert(orig_wig_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to WIG # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_wig_path) self.assertTrue( assert_file_equal(orig_wig_path, test_wig_path, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_wig_path)
def runTest(self): in_path = samples['small_features'][1]['sql'] out_path = temporary_path('.sql') chrom = 'chrI' with track.load(in_path) as i: with track.new(out_path) as o: o.fields = track.default_fields o.write(chrom, i.read(chrom, ('start', 'end'))) got = tuple(o.read(chrom).next()) expected = (0, 10, None, None, None) self.assertEqual(got, expected) os.remove(out_path)
def runTest(self): x_path = samples['small_signals'][4]['sql'] y_path = samples['small_features'][4]['sql'] out_path = temporary_path('.sql') t = mean_score_by_feature(x_path,y_path) t.export(out_path) with track.load(out_path) as t: data = t.read('chrI') got = map(tuple, data) expected = [(10, 20, 15.0, u'Lorem', 1), (30, 40, 50.0, u'Ipsum', 1)] self.assertEqual(got, expected) os.remove(out_path)
def runTest(self): for num, info in sorted(samples['gtf_tracks'].items()): # This one is too large # if num == 'GenRep': continue # Prepare paths # orig_gtf_path = info['gtf'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') # From GTF to SQL # track.convert(orig_gtf_path, test_sql_path) self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # Clean up # os.remove(test_sql_path)
def runTest(self): for num, info in sorted(samples['gff_tracks'].items()): # Prepare paths # orig_gff_path = info['gff'] orig_sql_path = info['sql'] test_sql_path = temporary_path('.sql') test_gff_path = temporary_path('.gff') # From GFF to SQL # track.convert(orig_gff_path, test_sql_path, assembly='sacCer2') self.assertTrue(assert_file_equal(orig_sql_path, test_sql_path)) # From SQL to GFF # with track.load(test_sql_path) as t: t.roman_to_integer() track.convert(test_sql_path, test_gff_path) self.assertTrue( assert_file_equal(orig_gff_path, test_gff_path, start_a=1, start_b=1)) # Clean up # os.remove(test_sql_path) os.remove(test_gff_path)
def parse(self): # Special case # if self.handler.format == 'bedGraph': return # The two paths # bigwig_path = self.path bedgraph_path = temporary_path('.bedgraph') # Run the tool # bigwig_to_bedgraph(bigwig_path, bedgraph_path) # Now just parse the bedgraph # parser = track.parse.get_parser(bedgraph_path, 'bedgraph') parser.name = self.name parser(self.handler) # Erase the temporary file # os.remove(bedgraph_path)
def runTest(self): orig_path = samples['small_features'][2]['sql'] test_path = temporary_path('.sql') shutil.copy(orig_path, test_path) with track.load(test_path) as t: t.delete_fields(['name', 'strand']) got = list(t.read()) expected = [('chrI', 10, 20, 0.1), ('chrI', 30, 40, 0.2), ('chrI', 50, 60, 0.1), ('chrI', 70, 80, 0.2), ('chrI', 90, 100, 0.0), ('chrI', 110, 120, 0.4), ('chrI', 130, 150, 0.4), ('chrI', 180, 190, 0.1), ('chrI', 180, 200, 0.1), ('chrI', 210, 220, 0.2), ('chrI', 230, 240, 0.1), ('chrI', 250, 260, 0.2), ('chrI', 270, 280, 0.0), ('chrI', 290, 300, 0.7)] self.assertEqual(got, expected) # Clean up # os.remove(test_path)
def runTest(self): in_path = samples['small_features'][1]['sql'] out_path = temporary_path('.bed') with track.load(in_path) as t: result = complement(t) result.export(out_path) with track.load(out_path) as t: data = t.read('chrI') got = map(tuple, data) expected = [( 10, 20), ( 30, 40), ( 50, 60), ( 80, 90), (110, 120), (135, 230208)] self.assertEqual(got, expected) os.remove(out_path)
def bedgraph_to_bigwig(bedgraph_path, chrmeta, bigwig_path): """ Converts a bedgraph file to a bigwig file. :param bedgraph_path: The path to the bedgraph file to read. :type bedgraph_path: string :param chrmeta: The chromosome metadata. :type chrmeta: dict :param bigwig_path: The path to the bedbig file to create. :type bigwig_path: string :returns: None """ # Check the binary tool exists # check_executable('bedGraphToBigWig') # Make the chr file # chrfile_path = temporary_path('.chr') serialize_chr_file(chrmeta, chrfile_path) # Run the tool # run_tool('bedGraphToBigWig', [bedgraph_path, chrfile_path, bigwig_path]) # Remove the chr file # os.remove(chrfile_path)
def runTest(self): in_paths = [samples['small_signals'][1]['sql'], samples['small_signals'][2]['sql'], samples['small_signals'][3]['sql']] out_path = temporary_path('.sql') t = merge_scores(in_paths) t.export(out_path) with track.load(out_path) as t: data = t.read('chrI') got = map(tuple, data) expected = [( 0, 5, 2.0 + 0.6666666666666666), ( 5, 10, 4.0), ( 20, 30, 10.0), ( 30, 40, 30.0), ( 40, 50, 26.0 + 0.666666666666666), ( 50, 60, 120.0), ( 60, 68, 100.0), ( 68, 70, 200.0), ( 70, 80, 100.0), ( 90, 110, 3.0), (120, 130, 10.0)] self.assertEqual(got, expected) os.remove(out_path)
def runTest(self): orig_path = samples['small_features'][2]['sql'] test_path = temporary_path('.sql') shutil.copy(orig_path, test_path) with track.load(test_path) as t: t.delete_fields(['name','strand']) got = list(t.read()) expected = [('chrI', 10, 20, 0.1), ('chrI', 30, 40, 0.2), ('chrI', 50, 60, 0.1), ('chrI', 70, 80, 0.2), ('chrI', 90, 100, 0.0), ('chrI', 110, 120, 0.4), ('chrI', 130, 150, 0.4), ('chrI', 180, 190, 0.1), ('chrI', 180, 200, 0.1), ('chrI', 210, 220, 0.2), ('chrI', 230, 240, 0.1), ('chrI', 250, 260, 0.2), ('chrI', 270, 280, 0.0), ('chrI', 290, 300, 0.7)] self.assertEqual(got, expected) # Clean up # os.remove(test_path)
def __enter__(self): # Just serialize it as a bedgraph first # self.tmp_path = temporary_path('.bedgraph') self.file = open(self.tmp_path, 'w') return self
def runTest(self): in_path = samples['small_features'][1]['sql'] out_path = temporary_path('.bed') track.convert(in_path, out_path) #assert_file_equal(out_path, samples['small_features'][1]['bed']) os.remove(out_path)