-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLX3pk_Enh_TSS2Kb.py
195 lines (132 loc) · 5.54 KB
/
TLX3pk_Enh_TSS2Kb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python2
import pybedtools as pb
import numpy as np
import yaml
from os.path import join
import pandas as pd
import gseapy as gp
import seaborn as sns
import matplotlib.pyplot as plt
from metaseq.results_table import ResultsTable, DESeq2Results
def log2p1(x):
return np.log2(x + 1)
def write_gmt(st, name, path=''):
gmt = [name, name] + list(st)
with open(join(path,name+'.gmt'), 'w') as fp:
fp.write("\t".join(gmt))
# Save or not
SV = False
## === Bed manipulation ======================
## ===========================================
#~ tlx_peak = pb.BedTool('tracks/TLX3_TLX3_peaks_100.bed')
#~ enh_fantm = pb.BedTool('tracks/annot_tracks/enhancers_FANTOM.bed')
#~ enh_chrhmm = pb.BedTool('tracks/TLX3_6_FE_E4_sorted.bed')
#~ tss_3kb = pb.BedTool('tracks/annot_tracks/references/mm9/mm9_tsses-3kb.gtf')
#~ tlx_tss_3kb = (tlx_peak+tss_3kb).sort()
#~ tlx_chrhmm = (tlx_peak+enh_chrhmm - tlx_tss_3kb).sort()
#~ tlx_tss_3kb.saveas('tracks/TLX3_TSS3Kb5.bed')
#~ tlx_chrhmm.saveas('tracks/TLX3_chrhmm5.bed')
## === Gene lists manipulation (tables comes from GREAT)
## =====================================================
tlx_tss_3kb_gt = pd.read_table('gene_lists/tlx_enh_tss3k/TLX3_TSS3Kb-gene.txt',
header=1,
names=['Genes','Regions'])
tlx_chrhmm_gt = pd.read_table('gene_lists/tlx_enh_tss3k/TLX3_enh_hmm6-gene.txt',
header=1,
names=['Genes','Regions'])
tlx_tss_3kb_gn = list(tlx_tss_3kb_gt['Genes'].str.upper())
tlx_chrhmm_gn = list(tlx_chrhmm_gt['Genes'].str.upper())
tss = set(tlx_tss_3kb_gn)
enh = set(tlx_chrhmm_gn)
from matplotlib_venn import venn2
venn2([tss,enh], set_labels = ('Tlx3 in Enhancer', 'Tlx3 in Promoter'))
tss_and_enh = tss & enh
tss_or_enh = tss | enh
tss_notin_enh = tss - enh
enh_notin_tss = enh - tss
diff_enh_tss = enh ^ tss
if SV:
write_gmt(tss_and_enh, 'tss_and_enh', 'gene_lists/tlx_enh_tss3k')
write_gmt(tss_or_enh, 'tss_or_enh', 'gene_lists/tlx_enh_tss3k')
write_gmt(tss_notin_enh, 'tss_notin_enh','gene_lists/tlx_enh_tss3k')
write_gmt(enh_notin_tss, 'enh_notin_tss','gene_lists/tlx_enh_tss3k')
write_gmt(diff_enh_tss, 'diff_enh_tss', 'gene_lists/tlx_enh_tss3k')
# === Load expression table
tbl = pd.read_table(join('tracks', 'TLX3vsRAGvsTAP_DESeq2-results.txt'), index_col=0)
# Filter genes (Note: this filter remove microRNA expression)
tbl = tbl[(tbl.padj < 0.05)].dropna()
# === Load gene names
names = pd.read_table("tracks/annot_tracks/references/mm9/mm9_EnsemblTransc_GeneNames.txt",
index_col=1,
header=0,
names=['GeneID', 'TransID', 'Gene_name'])
names = names.loc[tbl.index]
assert names.shape[0] == tbl.shape[0]
tbl=names.join(tbl, how ='right')
## === Expresion analysis
import RNA_expression_processing as rn
tbn = tbl[['Gene_name', 'R2.RAG1W.RAG1','RAGS.RAGZ','RAGZ','TLX3.1_1','TLX3.1_5','TLX3.1_P', 'padj']]
classes = ['RAG','RAG','RAG','TLX3','TLX3','TLX3']
groups ={
'TSS and Enh':tss_and_enh,
'Only TSS':tss_notin_enh,
'ALL: TSS or Enh':tss_or_enh,
'Only Enh':enh_notin_tss,
'In Enh or TSS but not common':diff_enh_tss
}
if SV:
rn.write_dic2gmt(groups,
name='TLX3pk_Enh_TSS2Kb',
path='gene_lists/tlx_enh_tss3k')
for nm, gns in groups.iteritems():
gl = list(gns)
rn.scatter(tbn,'TLX3', 'RAG', classes=classes,
n_top=5, geneList=gl, ttl=nm)
rn.express(tbn,'TLX3', 'RAG', classes=classes,
n_top=30, geneList=gl, ttl=nm)
plt.show()
# === Run GSEA
#~ tbl_c = tbl_n.copy()
#~ tbl_c.index=tbl_n['NAME']
#gnc = list(gen_tb['Genes'].str.upper())
#tbl_c = tbl_c.loc[gen_tb['Genes'].str.upper()].dropna()
#tbl_cc=tbl_cc.dropna()
#~ tbl_c = tbl_c.groupby(tbl_c.index).agg({'NAME': 'first',
#~ 'R2.RAG1W.RAG1':sum,
#~ 'RAGS.RAGZ':sum,
#~ 'RAGZ':sum,
#~ 'TLX3.1_1':sum,
#~ 'TLX3.1_5':sum,
#~ 'TLX3.1_P':sum,
#~ 'TAP':sum,
#~ 'TAP1B':sum,
#~ 'TAP2B':sum})
#~ tbl_c = tbl_c[['NAME',
#~ 'R2.RAG1W.RAG1',
#~ 'RAGS.RAGZ',
#~ 'RAGZ',
#~ 'TLX3.1_1',
#~ 'TLX3.1_5',
#~ 'TLX3.1_P']]
#~ 'TAP',
#~ 'TAP1B',
#~ 'TAP2B']]
#~ classi = ['RAG','RAG','RAG','TLX3','TLX3','TLX3'] #,'TLX3','TLX3','TLX3']
#~ gs_res = gp.gsea.call(data=tbl_c,
#~ gene_sets= 'tracks/TLX_TSS3Kb_ChHMM.gmt', #gene_sets='KEGG_2016',
#~ cls=classi,
#~ max_size = 2000,
#~ permutation_type='gene_set',#~ permutation_type='phenotype',
#~ outdir='gsea_TLX_TSS3Kb_ChHMM')
# === Pictures
#~ gsea_results = gs_res.reset_index().sort_values('fdr',axis=0,ascending=True)
#~ with plt.style.context('ggplot'):
#~ gsea_results.head(40).plot.bar(y='fdr',x='Term', figsize=(12, 6),fontsize=12)
#~ plt.show()
#------------------------------------------------------------------------
#~ genes = test1.closest('tracks/genes.bed')
#~ genes.saveas('tracks/TLX3_peaks_RUNX_ETS_genes.bed')
#~ gs = genes.to_dataframe()
#~ nm = list(gs['thickStart'].str.upper())
#~ nm = list(set(nm))
#~ gmt = {'TLX-RUNX-ETS':nm}