def test_select(self): # by fields stream = fstream([(10,12,0.5), (14,15,1.2)], fields=['start','end','score']) res = list(select(stream,['score','end'])) expected = [(0.5,12),(1.2,15)] self.assertListEqual(res,expected) # by selection stream = fstream([('a',10,12), ('a',14,15), ('b',16,19)], fields=['name','start','end']) res = list(select(stream,None,{'name':['a','c']})) expected = [('a',10,12),('a',14,15)] self.assertListEqual(res,expected)
def test_select(self): # by fields stream = fstream([(10, 12, 0.5), (14, 15, 1.2)], fields=['start', 'end', 'score']) res = list(select(stream, ['score', 'end'])) expected = [(0.5, 12), (1.2, 15)] self.assertListEqual(res, expected) # by selection stream = fstream([('a', 10, 12), ('a', 14, 15), ('b', 16, 19)], fields=['name', 'start', 'end']) res = list(select(stream, None, {'name': ['a', 'c']})) expected = [('a', 10, 12), ('a', 14, 15)] self.assertListEqual(res, expected)
def fimo(motifs,fasta,qval=True): # Run Fimo if qval: options = "--max-stored-scores 1000000 --verbosity 1 --thresh 0.01 --qv-thresh" else: options = "--max-stored-scores 1000000 --verbosity 1 --thresh 0.000001" cmd = "fimo " + options + " %s %s" % (motifs, fasta) print "Running >>",cmd os.system(cmd) os.system("sort -k2,2n -k3,3n -k4,4n fimo_out/fimo.txt > fimo.txt") # Bed output t = track('fimo.txt', fields=["name","chr","start","end","strand","score","p-value","q-value","sequence"]) t.fields = ["name","chr","start","end","strand","a","score","q","sequence"] s = t.read() s = select(s,['chr','start','end','name','score','strand']) s = apply(s,'chr',lambda x:x.split('|')[1]) s = sorted_stream(s) s = cobble(s) s = apply(s,'name',lambda x:'|'.join(list(set(x.split('|'))))) outname = 'fimo.bed' bed = track(outname,fields=s.fields) bed.make_header(name="TSS_motifs", description="Motifs +-XKb around TSS", mode='overwrite') bed.write(s) if os.path.exists("fimo_out"): shutil.rmtree("fimo_out")
def fimo(motifs, fasta, qval=True): # Run Fimo if qval: options = "--max-stored-scores 1000000 --verbosity 1 --thresh 0.01 --qv-thresh" else: options = "--max-stored-scores 1000000 --verbosity 1 --thresh 0.000001" cmd = "fimo " + options + " %s %s" % (motifs, fasta) print "Running >>", cmd os.system(cmd) os.system("sort -k2,2n -k3,3n -k4,4n fimo_out/fimo.txt > fimo.txt") # Bed output t = track('fimo.txt', fields=[ "name", "chr", "start", "end", "strand", "score", "p-value", "q-value", "sequence" ]) t.fields = [ "name", "chr", "start", "end", "strand", "a", "score", "q", "sequence" ] s = t.read() s = select(s, ['chr', 'start', 'end', 'name', 'score', 'strand']) s = apply(s, 'chr', lambda x: x.split('|')[1]) s = sorted_stream(s) s = cobble(s) s = apply(s, 'name', lambda x: '|'.join(list(set(x.split('|'))))) outname = 'fimo.bed' bed = track(outname, fields=s.fields) bed.make_header(name="TSS_motifs", description="Motifs +-XKb around TSS", mode='overwrite') bed.write(s) if os.path.exists("fimo_out"): shutil.rmtree("fimo_out")