-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_many_clip.py
1610 lines (1301 loc) · 96.8 KB
/
plot_many_clip.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
##### IMPORT #####
import numpy as np
import pandas as pd
import sys
import os
import argparse
import itertools
import subprocess
import matplotlib.pyplot as plt
import seaborn as sns
import functools
import scipy.stats as st
from scikits.bootstrap import bootstrap
from fittinglibs import plotting, seqfun
from tectolibs import tectplots
import scipy.cluster.hierarchy as sch
from puflibs import processing, predictions, seqmodel, variables
# function definitions
def get_log_enrichment_counts(counts1, counts2):
return np.log2(counts1/counts1.sum() / (counts2/counts2.sum()))
# import args
parser = argparse.ArgumentParser()
parser.add_argument('--mode', help='which analysis to run')
parser.add_argument('--filename_table', help='table of filenames')
args = parser.parse_args()
def find_enrichment(data, num_reads_total=None, method='input'):
"""Determined the fold enrichment by different methods."""
if method=='input':
weights = 1./num_reads_total*num_reads_total.mean()
fe = ((data.rep1*weights.rep1 + data.rep2*weights.rep2)/(2*data.input*weights.input)).replace(np.inf, np.nan)
elif method=='tpm':
pass
return re
def process_combined_data(data):
"""Process the data by different methods"""
data = (data.reset_index().rename(columns={'index':'name'}).groupby(['chrm', 'start', 'stop']).first().reset_index()).copy()
data = data.groupby(['chrm', 'start', 'stop']).first().reset_index()
data_expressed = data.loc[data.tpm > 0].copy()
data_no_neighbors = data.groupby('cluster').applyloc[(data.downstream_bases_to_TGTA > 60)&(data.upstream_bases_toTGTA > 60)].copy()
data
if __name__ == '__main__':
# filenames
#data = pd.read_table('analysis/output/combined_data.gz', index_col=0, compression='gzip')
data_0 = pd.read_table('analysis/output/all_unprocessed_st_merged.00.hPUM2_all.random_5e+06.input.ENCFF786ZZB.R2.500.rep2.ENCFF732EQX.rep1.ENCFF231WHF.temp0.combined_data.01.02.03.04.05.06.07.08.09.ENCFF372VPV.ENCFF141SVY.combined_data.gz', compression='gzip')
data_37 = pd.read_table('analysis/output/all_unprocessed_st_merged.00.hPUM2_all.random_5e+06.input.ENCFF786ZZB.R2.500.rep2.ENCFF732EQX.rep1.ENCFF231WHF.temp37.combined_data.01.02.03.04.05.06.07.08.09.ENCFF372VPV.ENCFF141SVY.combined_data.gz', compression='gzip')
temperature = 37
data = data_37
data.loc[:, 'min_bases_to_TGTA'] = data.loc[:, ['upstream_bases_to_TGTA', 'downstream_bases_to_TGTA']].min(axis=1)
data.loc[:, 'in_transcript'] = (data.gene_type=='protein-coding')&( # added 2018-07-03 SKD
(data.annotation == "5' UTR")|(data.annotation == "exon")|(data.annotation == "3' UTR"))
# remove duplicates
data = data.groupby(['chrm', 'start', 'stop']).first().reset_index().copy()
if args.mode == 'combine_data_all':
"""output of pipeline is split into 10 files. load and combine."""
filenames = subprocess.check_output('find analysis/output/ -mindepth 2 -name "*combined_data.gz" | sort', shell=True).strip().split()
data = pd.concat({i:pd.read_table(filename, compression='gzip', index_col=0)
for i, filename in enumerate(filenames)})
elif args.mode == 'compare_rna_seq':
"""Make sure the spike in controls of the RNA seq values are quantitative measurements of expression."""
# note col 'concentration_15' is the concentration of molecules in the version spiked into ENCODE dataset
nist_ercc_pool15 = pd.read_csv('annotations/nist_ercc_pool15.csv')
nist_ercc_pool15.index = ['tSpikein_%s'%control for control in nist_ercc_pool15.control]
rep1 = pd.read_table('analysis/expression/rna_seq_rep1.dat')
rep2 = pd.read_table('analysis/expression/rna_seq_rep2.dat')
sub_rep1 = rep1.loc[(rep1.transcript_id.str.find('tSpikein_ERCC')==0)].set_index('transcript_id').copy()
sub_rep1.loc[:, 'actual_concentration'] = nist_ercc_pool15.concentration_15
xlim = np.array([1E-11, 1E-4] )# nmol/ul
for col in ['expected_count', 'posterior_mean_count', 'TPM', 'FPKM', ]:
min_val = sub_rep1[col].replace(0, np.nan).min()/10.
plt.figure(figsize=(3,3))
plt.scatter(sub_rep1.actual_concentration, sub_rep1[col] + min_val, marker='.')
plt.xscale('log'); plt.yscale('log')
plt.xlim(xlim)
plt.ylim(min_val/2, sub_rep1[col].max()*2)
plt.xlabel('actual concentration (nmol/ul)', fontsize=10)
plt.ylabel(col, fontsize=10)
factor = sub_rep1[col].median()/sub_rep1.actual_concentration.median()
plt.plot(xlim, xlim*factor)
plt.tight_layout()
# make sure mapping is correct
tpm_data = pd.concat([rep1.set_index('transcript_id').TPM.rename('rep1'), rep2.set_index('transcript_id').TPM.rename('rep2')], axis=1).reset_index().rename(columns={'transcript_id':'transcript_idx'})
tpm_data.index = [s.split('.')[0] for s in tpm_data.transcript_idx]
tpm_combined = np.exp(np.log(tpm_data.loc[:, ['rep1', 'rep2']]).mean(axis=1))
biomart_file = 'annotations/ensemble_gene_converter_biomart.txt'
biomart_data = pd.read_table(biomart_file, names=['gene_id', 'transcript_id', 'gene_name', 'refseq_id', 'refseq_nc'], header=0)
biomart_data.loc[:, 'refseq_comb'] = [refseq_id if not str(refseq_id)=='nan' else refseq_nc for idx, refseq_id, refseq_nc in biomart_data.loc[:, ['refseq_id', 'refseq_nc']].itertuples()]
# annotate tpm data with refseq id
biomart_data.loc[:, 'tpm'] = tpm_combined.loc[biomart_data.transcript_id].values
# get NORAD
tpm_data.loc['ENST00000565493']
elif args.mode == 'find_consensus_sites_for_ss_structure':
"""To evaluate secondary structure effects, we want consensus sites in one register"""
filenames = subprocess.check_output('find analysis/effects/temp_%d -mindepth 2 -name "*affinity.gz" | sort'%temperature, shell=True).strip().split()
kT = seqmodel.get_ddG_conversion(temperature)
data_subset = {}
for i, filename in enumerate(filenames):
split_id = int(filename.split('/')[3].split('_')[-1])
if split_id in range(10): # exclude split_10 which is peaks
seqeffect = pd.read_table(filename, compression='gzip', index_col=0)
name_list = seqeffect.loc[seqeffect.noflip_0 < 0.5].index.tolist()
data_subset[split_id] = (data.groupby('split_id').get_group(split_id).drop('split_id', axis=1).set_index('name').
loc[name_list].copy())
data_subset = pd.concat(data_subset, names=['split_id', 'name2']).dropna(subset=['chrm', 'start', 'stop']).reset_index()
data_subset.loc[:, 'name'] = data_subset.split_id.astype(str) + ',' + data_subset.name2
for col in ['start', 'stop']:
data_subset.loc[:, col] = data_subset.loc[:, col].astype(int)
#data_subset.loc[:, variables.bed_fields].to_csv('analysis/beds/hPUM2_all.first_register_consensus.bed',
# sep='\t', index=False, header=False, float_format='%.2f')
elif args.mode == 'compare_consensus_sites_for_ss_structure':
"""Evaluate secondary structure effects for 8nt constraint and 11nt constraint"""
ss_ddG_table = pd.concat([pd.read_table(filename,
compression='gzip', index_col=0).iloc[:, 6:]
for filename in ['analysis/sec_structure/temp_37/hPUM2_all.first_register_consensus.40.dG.80.10.160.20.combined_data.gz',
'analysis/sec_structure/temp_37/hPUM2_all.first_register_consensus_8bp.8.dG.2.4.10.6.combined_data.gz']], axis=1)
ss_data = pd.read_table(filename, compression='gzip', index_col=0)
# change index
split_id, name = [ss_data.name.str.split(',', expand=True)[i] for i in [0,1]]
index = pd.MultiIndex.from_tuples([(int(i), s) for i, s in zip(split_id, name)])
ss_ddG_table.index = index
# find clip signal
data_subset = data.set_index(['split_id', 'name']).loc[index].copy()
med_background_val = data.loc[(data.tpm>0)&(data.ddG > 4.5)].clip_signal_per_tpm.median()
clip_signal_per_tpm_fold = data_subset.clip_signal_per_tpm/med_background_val
# bin the ss_ddGs
binedges = ss_ddG_table.stack().quantile(np.linspace(0, 1, 15))
ss_ddG_binned = pd.cut(ss_ddG_table.stack(), bins=binedges, precision=1, include_lowest=True).rename('ss_ddG')
order = pd.DataFrame(ss_ddG_binned).groupby('ss_ddG').first().index.tolist()
# find entries in the first bin in at least one energy category
first_bin = ss_ddG_binned.loc[ss_ddG_binned == order[0]].unstack().index.tolist()
ymax = clip_signal_per_tpm_fold.loc[first_bin].median()
x_order = (binedges.values[1:] + binedges.values[:-1])*0.5
# plotting function
func = functools.partial(sns.factorplot, x='ss_ddG', y='clip_signal_per_tpm', estimator=np.median,
errwidth=0.5,capsize=1, linestyles='', marker='.')
ylim = [0.5, 500]
for window_size in [4, 6, 8, 10, 12, 20, 40, 80, 160]:
data_toplot = (pd.concat([clip_signal_per_tpm_fold,
ss_ddG_binned.unstack().loc[:, 'ss_ddG_%d'%window_size].rename('ss_ddG')], axis=1)
.loc[(data_subset.tpm>0)&(data_subset.in_transcript)])
# plot
g = func(data=data_toplot)
plt.yscale('log')
plt.xticks(rotation=90)
plt.axhline(1, color='0.5', linestyle=':')
# plot expectation line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y, 'k--')
plt.ylim(ylim)
# labe
plt.title('%d nt'%window_size)
plt.tight_layout()
plt.savefig('scatterplot.clip_signal_vs_ss_ddG.window_size_%d.pdf'%window_size)
# also plot histogram
plt.figure(figsize=(4.5,3));
(data_toplot.groupby('ss_ddG').size().loc[order]).plot(kind='bar')
plt.xticks(rotation=90)
plt.ylabel('count')
plt.tight_layout()
sns.despine()
plt.title('%d nt'%window_size)
plt.savefig('histogram.clip_signal_vs_ss_ddG.window_size_%d.pdf'%window_size)
# plot with equally spaced bins
binedges = np.hstack([np.arange(0, 5, 0.5), np.logspace(np.log10(4.5), np.log10(ss_ddG_table.stack().max()), 3)])
ss_ddG_binned = pd.cut(ss_ddG_table.stack(), bins=binedges, precision=1, include_lowest=True).rename('ss_ddG')
order = pd.DataFrame(ss_ddG_binned).groupby('ss_ddG').first().index.tolist()
x_order = (binedges[1:] + binedges[:-1])*0.5
count_cutoff = 20
all_data = {}
for i, window_size in enumerate([10, 20, 40, 80, 160]):
data_toplot = (pd.concat([clip_signal_per_tpm_fold,
ss_ddG_binned.unstack().loc[:, 'ss_ddG_%d'%window_size].rename('ss_ddG')], axis=1)
.loc[(data_subset.tpm>0)&(data_subset.in_transcript)])
to_plot_categories = [category for category, count in data_toplot.ss_ddG.value_counts().iteritems() if count > count_cutoff]
all_data[window_size] = data_toplot.loc[np.in1d(data_toplot.ss_ddG, to_plot_categories)]
#all_data[window_size] = data_toplot
all_data = pd.concat(all_data, names=['window_size', 'split_id', 'name']).reset_index()
g = func(data=all_data, hue='window_size', hue_order=[20, 40, 160])
plt.yscale('log')
plt.xticks(rotation=90)
plt.axhline(1, color='0.5', linestyle=':')
# plot expectation line
y = ymax*np.exp((x_order-x_order[0])/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y, 'k--')
plt.ylim(ylim)
elif args.mode == 'plot_clip_footprint':
"""Load the clip signal counts and plot the aggregate footprint around consensus sites"""
filenames_rep1 = subprocess.check_output('find analysis/clip -mindepth 2 -maxdepth 2 -name "*tracks.txt.gz" | grep split | grep hPUM2_all | grep -v input | grep -v rep2 | sort', shell=True).strip().split()
filenames_rep2 = subprocess.check_output('find analysis/clip -mindepth 2 -maxdepth 2 -name "*tracks.txt.gz" | grep split | grep hPUM2_all | grep -v input | grep -v rep1 | sort', shell=True).strip().split()
filenames_input = subprocess.check_output('find analysis/clip -mindepth 2 -maxdepth 2 -name "*tracks.txt.gz" | grep split | grep hPUM2_all | grep -v rep2 | grep -v rep1 | sort', shell=True).strip().split()
# load counts
footprints = {}
for i, filename in enumerate(filenames_rep1):
split_id = int(filename.split('/')[2].split('_')[-1])
print split_id
footprint = pd.read_csv(filename, compression='gzip', index_col=0)
subset_consensus = data.loc[
(data.split_id == split_id)&
(data.ddG < 0.5)&
(data.tpm > 0)&
(data.in_transcript)].name
footprints[split_id] = footprint.loc[subset_consensus]
footprints = pd.concat(footprints, names=['split_id', 'name'])
# plot
interval_radius=40
offset=15
xvalues = np.arange(-250, 251)
plt.figure(figsize=(3,3))
plt.plot(xvalues, footprints.mean())
plt.axvline(-interval_radius-offset, color='k', linestyle=':')
plt.axvline(+interval_radius-offset, color='k', linestyle=':')
plt.ylim(0, 0.6)
plt.xlim(-225, 225)
plt.savefig('lineplot.footprint.pdf')
elif args.mode == 'compare_window_size':
"""Load the 500 bp window, and calculate enrichment above background for different window sizes"""
data = pd.read_pickle('analysis/output/all_unprocessed_st_merged.hPUM2.1.random_1e+06.input.ENCFF786ZZB.R2.500.rep2.ENCFF732EQX.rep1.ENCFF231WHF.combined_data.pkl')
index_bg = (data.score==0)&(data.tpm > 0.01)
index_consensus = (data.seq==consensus_seq)&(data.tpm > 0.01)
index_consensusC = (data.seq==consensus_seqC)&(data.tpm > 0.01)
index_consensusA = (data.seq==consensus_seqA)&(data.tpm > 0.01)
consensus_fes = {}
background_fes = {}
consensus_counts = {}
background_counts = {}
for interval_size in [10, 20, 40, 80, 160, 320]:
background_fes[interval_size] = data.loc[index_bg, ['%s_%d'%(key, interval_size) for key in ['rep1', 'rep2']]].sum(axis=1)/data.loc[index_bg].tpm/interval_size
consensus_fes[interval_size] = data.loc[index_consensus, ['%s_%d'%(key, interval_size) for key in ['rep1', 'rep2']]].sum(axis=1)/data.loc[index_consensus].tpm/interval_size
background_counts[interval_size] = (data.loc[index_bg, ['%s_%d'%(key, interval_size) for key in ['rep1', 'rep2']]].sum(axis=1)/interval_size).mean()
consensus_counts[interval_size] = (data.loc[index_consensus, ['%s_%d'%(key, interval_size) for key in ['rep1', 'rep2']]].sum(axis=1)/interval_size).mean()
consensus_fes = pd.concat(consensus_fes, axis=1)
consensus_counts = pd.Series(consensus_counts)
background_counts = pd.Series(background_counts)
reads_per_bp_80 = consensus_fes.quantile(0.8)
mean_w_reads = (consensus_fes>0).mean()
g = sns.FacetGrid(pd.concat([reads_per_bp_80.rename('reads_per_bp_80th'), mean_w_reads.rename('fraction_usable')], axis=1).reset_index(), hue='index', palette='viridis');
g.map(plt.scatter, 'reads_per_bp_80th', 'fraction_usable')
g = sns.FacetGrid(pd.concat([mean_w_reads.rename('fraction_usable'), (consensus_counts/background_counts).rename('fold_enrichment')], axis=1).reset_index(), hue='index', palette='viridis');
g.map(plt.scatter, 'fraction_usable', 'fold_enrichment')
clip_signal = (pd.read_csv('analysis/clip/all_unprocessed_st_merged.hPUM2.1.random_1e+06.ann.filt.rep1.ENCFF231WHF.R2.bedGraph.500.tracks.txt.gz', compression='gzip', index_col=0) +
pd.read_csv('analysis/clip/all_unprocessed_st_merged.hPUM2.1.random_1e+06.ann.filt.rep2.ENCFF732EQX.R2.bedGraph.500.tracks.txt.gz', compression='gzip', index_col=0))
xvalues = np.arange(-250, 251)
num_up_bases = 40
offset = 15
plt.figure(); plt.plot(xvalues, clip_signal.loc[index_bg].mean());
plt.plot(xvalues, clip_signal.loc[index_consensus].mean())
plt.axvline(-num_up_bases-offset)
plt.axvline(-num_up_bases-offset+offset+2*num_up_bases)
plt.errorbar(xvalues, clip_signal.loc[index_consensus].mean(), yerr=clip_signal.loc[index_consensus].std()/np.sqrt(index_consensus.sum())*1.96)
plt.errorbar(xvalues, clip_signal.loc[index_consensusA].mean(), yerr=clip_signal.loc[index_consensusA].std()/np.sqrt(index_consensusA.sum())*1.96)
plt.errorbar(xvalues, clip_signal.loc[index_consensusC].mean(), yerr=clip_signal.loc[index_consensusC].std()/np.sqrt(index_consensusC.sum())*1.96)
elif args.mode == 'apply_model_to_seqs':
""" """
basename = 'annotations/RNAmap/qMotif_table_05_012318_1_'
xlim = np.array([0, 5])
base_params = pd.read_csv(basename + 'term1.csv', index_col=0) #.stack().values
flip_params = pd.read_csv(basename + 'term2_single.csv', index_col=0) #.stack().values
dflip_params = pd.read_csv(basename + 'term2_double.csv', index_col=0, squeeze=True) #.stack().values, 10])
coupling_params= pd.read_csv(basename + 'term3.csv', index_col=0, squeeze=True) #.stack().values
passed_sequence = data.iloc[0].seq_rna
seqmodel2.additive_PUF_flip_model(passed_sequence, flip_params, base_penalties, coupling_params, double_flip_params, temperature)
elif (args.mode == 'plot_flip_annot' or args.mode == 'plot_clip_vs_ddG' or
args.mode == 'plot_annotation_vs_ddG' or
args.mode == 'plot_overestimation_with_noflip_model' or
args.mode == 'plot_intron_clip_subset' or
args.mode == 'plot_clip_vs_ddG_per_seq' or
args.mode=='plot_ss_ddG_versus_clip' or args.mode =='plot_versus_proximity'):
"""Using the New model to find effects, find ddG"""
pass
# annotatie flipped/not flipped
data.loc[:, 'flip_annot'] = 'noflip'
data.loc[data.ddG_noflip - data.ddG > 0.5, 'flip_annot'] = 'flip'
# find fold enrichment above expected bacground
med_background_val = data.loc[(data.tpm>0)&(data.ddG > 4.5)].clip_signal_per_tpm.median()
med_background_val_input = data.loc[(data.tpm>0)&(data.ddG > 4.5)].clip_input_per_tpm.median()
data.loc[:, 'clip_signal_per_tpm_fold'] = data.clip_signal_per_tpm/med_background_val
data.loc[:, 'clip_input_per_tpm_fold'] = data.clip_input_per_tpm/med_background_val_input
# determine the subset of data with expression and within a transcript
subdata = data.loc[(data.tpm > 0)&data.in_transcript]
if args.mode == 'plot_flip_annot':
"""plot the sites that have flip annotation or not."""
data.loc[:, 'is_random'] = [s.find('hPUM2')!=0 for s in data.name]
index_subset = np.random.choice((data.loc[(data.tpm>0)&(~data.is_random)].index.tolist()), size=5000, replace=False)
g = sns.FacetGrid(data=data.loc[index_subset], hue='flip_annot'); g.map(tectplots.scatter, 'ddG_noflip', 'ddG', marker='.', s=10)
xlim = np.array([-0.5, 8])
plt.plot(xlim, xlim, 'k--')
plt.plot(xlim, xlim-0.5, ':', color='0.5')
plt.xlim(xlim)
plt.ylim(xlim)
data.loc[:, 'dddG_noflip'] = data.ddG_noflip - data.ddG
bins = np.linspace(0, 5)
plt.figure(figsize=(3,3)); sns.distplot(data.loc[(data.tpm>0)&(~data.is_random)].dddG_noflip, color=sns.color_palette()[0], bins=bins, kde=False)
elif args.mode=='plot_clip_vs_ddG_per_seq':
"""Plot per sequences"""
count_cutoff = 50
represented_seqs = [seq for seq, count in subdata.seq.value_counts().iteritems() if count >=count_cutoff]
subsubdata = subdata.loc[np.in1d(subdata.seq.tolist(), represented_seqs)].copy()
quantitative_cols = ['ddG', 'ddG_noflip_noens', 'ddG_noflip', 'ddG_flip', 'clip_signal_per_tpm_fold', 'clip_input_per_tpm_fold']
nonquantitative_cols = ['flip_annot']
subdata_grouped = pd.concat([subsubdata.groupby('seq')[nonquantitative_cols].first(),
subsubdata.groupby('seq')[quantitative_cols].median()], axis=1)
elif args.mode == 'plot_clip_vs_ddG':
"""bin by ddG and plot"""
ddG_binedges = np.hstack([np.linspace(data.ddG.min(), 4.5, 25), data.ddG.max()])
subdata.loc[:, 'binned_ddG'] = pd.cut(subdata.ddG, ddG_binedges,
include_lowest=True, precision=2)
subdata.loc[:, 'binned_logtpm'] = pd.cut(np.log10(subdata.tpm), (np.log10(subdata.tpm).replace(-np.inf, np.nan).dropna()).quantile(np.linspace(0, 1, 4)), include_lowest=True)
subdata.loc[:, 'binned_min_dist'] = pd.cut(subdata.min_bases_to_TGTA, [0, 50, 100, 300], include_lowest=True, precision=0)
order = subdata.groupby('binned_ddG').first().index.tolist()
# plot
ylim = [0.5, 100]
ymax = subdata.groupby('binned_ddG').get_group(order[0])['clip_signal_per_tpm_fold'].median()
x_order = pd.Series({name:0.5*(group.ddG.max() + group.ddG.min()) for name, group in subdata.groupby('binned_ddG')})
func = functools.partial(sns.factorplot, data=subdata, estimator=np.median,
errwidth=0.5,capsize=1, linestyles='', marker='.')
# plot signal, colored by flip/noflip
for yval in ['clip_input_per_tpm_fold', 'clip_signal_per_tpm_fold']:
g = func(x='binned_ddG', y=yval, hue='flip_annot', hue_order=['noflip', 'flip']);
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
plt.savefig('scatterplot.%s.vs.binned_ddG.pdf'%yval)
# plot the numbers of flipped sites per bin
subdata.groupby(['binned_ddG', 'flip_annot']).size().unstack().loc[:, ['noflip', 'flip']].plot(kind='bar', stacked=True, width=0.8, figsize=(3,3));
plt.savefig('barplot.num_flipped.binned_ddG.pdf')
# plot the ddG between flipped and non flipped sites
subdata_stable_flipped = subdata.loc[(subdata.ddG < 2)&(subdata.flip_annot=='flip')]
plt.figure(figsize=(3,3));
sns.distplot(subdata_stable_flipped.ddG_noflip - subdata_stable_flipped.ddG, kde=False, bins=np.linspace(0, 5, 20));
plt.axvline((subdata_stable_flipped.ddG_noflip - subdata_stable_flipped.ddG).median())
plt.tight_layout()
plt.xticks(np.arange(6))
plt.yticks([0, 500, 1000, 1500])
plt.savefig('histogram.ddG_diffs.flipped_stable_sites.pdf')
# plot signal, colored by annotation
annotation_order = ["3' UTR", "exon"]
annotation_colors = ['#f7931d', '0.7']
for yval in ['clip_input_per_tpm_fold', 'clip_signal_per_tpm_fold']:
g = func(x='binned_ddG', y=yval, hue='annotation', hue_order=annotation_order, palette=annotation_colors);
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
plt.savefig('scatterplot.%s.vs.binned_ddG.by_annotation.pdf'%yval)
# plot by annotation again but only plot sites at least 100 nt away
annotation_order = ["3' UTR", "exon"]
annotation_colors = ['#f7931d', '0.7']
func2 = functools.partial(sns.factorplot, data=subdata.loc[subdata.binned_min_dist=='(100, 300]'], estimator=np.median,
errwidth=0.5,capsize=1, linestyles='', marker='.')
for yval in ['clip_input_per_tpm_fold', 'clip_signal_per_tpm_fold']:
g = func2(x='binned_ddG', y=yval, hue='annotation', hue_order=annotation_order, palette=annotation_colors);
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
plt.savefig('scatterplot.%s.vs.binned_ddG.by_annotation.greater_than_100nt.pdf'%yval)
# plot signal, colored by distance to nearest site
min_dist_order = ['[0, 50]', '(50, 100]', '(100, 300]']
min_dist_colors = ['#f6935a', '#ab665c', '#603e4a']
for yval in ['clip_input_per_tpm_fold', 'clip_signal_per_tpm_fold']:
g = func(x='binned_ddG', y=yval, hue='binned_min_dist', hue_order=min_dist_order,
palette=min_dist_colors)
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
plt.savefig('scatterplot.%s.vs.binned_ddG.by_mindist.pdf'%yval)
# plot fraction of annotations in distance bins
subdata_stable = subdata.loc[(subdata.ddG < 2)]
num_in_annotations = subdata_stable.groupby(['annotation', 'binned_min_dist']).size().unstack()
(num_in_annotations.transpose()/num_in_annotations.sum(axis=1)).loc[min_dist_order,annotation_order].transpose().plot(kind='bar', stacked=True, colors=min_dist_colors)
plt.savefig('barplot.num_annotations.binned_min_dist.pdf')
# plot the effect of expression
# plot signal, colored by flip/noflip
tpm_order = subdata.groupby('binned_logtpm').first().index.tolist()
for yval in ['clip_input_per_tpm_fold', 'clip_signal_per_tpm_fold']:
g = func2(x='binned_ddG', y=yval, hue='binned_logtpm', );
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp(x_order/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
plt.savefig('scatterplot.%s.vs.binned_ddG.pdf'%yval)
"""
# plot
subdata = data.loc[(data.tpm>0)]
func = functools.partial(sns.factorplot, data=subdata, hue='annotation', hue_order=["5' UTR", "exon", "3' UTR"], y='clip_signal_per_tpm', estimator=np.median,
errwidth=0.5,capsize=1, linestyles='', marker='.')
g = func(x='binned_ddG'); plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(med_background_val, color='0.5', linestyle='--'); plt.yscale('log')
ymax = subdata.groupby('binned_ddG').get_group(order[0]).clip_signal_per_tpm.median()
x = pd.Series({name:0.5*(group.ddG.max() + group.ddG.min()) for name, group in subdata.groupby('binned_ddG')})
y = ymax*np.exp(x/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
ylim = [0.01, 20]
plt.ylim(ylim)
# plot
subdata = data.loc[(data.tpm>0)]
func = functools.partial(sns.factorplot, data=subdata, hue='binned_logtpm', y='clip_signal_per_tpm', estimator=np.median,
errwidth=0.5,capsize=1, linestyles='', marker='.', palette='viridis')
g = func(x='binned_ddG'); plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(med_background_val, color='0.5', linestyle='--'); plt.yscale('log')
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
ylim = [0.01, 20]
plt.ylim(ylim)
"""
# plot by type
elif args.mode == 'plot_rna_expression_vs_ddG':
"""Compare to RNA expression."""
subdata = data.loc[(data.tpm>0)&(data.binned_ddG==order[0])].copy()
subdata.loc[:, 'binned_tpm'] = np.digitize(subdata.tpm, np.logspace(-2, 3, 10))
pass
elif args.mode == 'plot_overestimation_with_noflip_model':
"""Try to see if clip signal is overestimated when using ddG_no flip model"""
subdata = data.loc[(data.tpm>0)&data.in_transcript&(subdata.min_bases_to_TGTA>=100)]
binedges = np.hstack([np.arange(-0.5, 5, 0.5), subdata.ddG.max()])
binedges = np.hstack([subdata.loc[(subdata.ddG < 4.5)&(subdata.ddG_noflip < 4.5), ['ddG', 'ddG_noflip']].stack().quantile(np.linspace(0, 1, 20)).values,
max(subdata.ddG.max(), subdata.ddG_noflip.max())])
binedges = np.hstack([np.linspace(data.ddG.min(), 4.5, 25), data.ddG.max()])
subdata.loc[:, 'binned_ddG'] = pd.cut(subdata.ddG, binedges,
precision=1, include_lowest=True)
subdata.loc[:, 'binned_ddGnoflip'] = pd.cut(subdata.ddG_noflip, binedges,
precision=1, include_lowest=True)
order = subdata.groupby('binned_ddG').first().index.tolist()
x_order = (binedges[1:] + binedges[:-1])*0.5
ymax = subdata.groupby('binned_ddG').get_group(order[0]).clip_signal_per_tpm_fold.median()
ylim = [0.1, 100]
func = functools.partial(sns.factorplot, estimator=np.median,
errwidth=0.5,capsize=0.5, linestyles='', marker='.')
#plot
yval = 'clip_signal_per_tpm_fold'
count_cutoff = 25
for xval in ['binned_ddG', 'binned_ddGnoflip']:
subsubdata = pd.concat([group for name, group in subdata.groupby([xval, 'flip_annot']) if len(group) > count_cutoff])
g = func(data=pd.concat([subsubdata.loc[:,[xval, 'flip_annot']], subsubdata[yval] + 0.1], axis=1), x=xval, y=yval, hue='flip_annot', hue_order=['noflip', 'flip'], order=order);
plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
plt.axhline(1, color='0.5', linestyle='--');
plt.yscale('log')
plt.ylim(ylim)
# plot expected line
y = ymax*np.exp((x_order-x_order[0])/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y, 'k--')
plt.savefig('scatterplot.clip_signal_vs_%s.big_bins.greaterthan_100nt.pdf'%xval)
elif args.mode == 'plot_intron_clip_subset':
"""Using a small subset of sites for which the clip signal was determined for intronic sites, find the enrichment"""
data_intron = pd.read_table('analysis/output/split_0/all_unprocessed_st_merged.09.hPUM2_odds9.intron_filt.input.ENCFF786ZZB.R2.500.rep2.ENCFF732EQX.rep1.ENCFF231WHF.temp0.combined_data.gz')
data_intron.loc[:, 'min_bases_to_TGTA'] = data_intron.loc[:, ['upstream_bases_to_TGTA', 'downstream_bases_to_TGTA']].min(axis=1)
subdata_intron = data_intron.loc[(data_intron.tpm>0)&(data_intron.min_bases_to_TGTA>=100)]
subdata_intron.loc[:, 'clip_signal_per_tpm_fold'] = subdata_intron.clip_signal_per_tpm/med_background_val
subdata = data.loc[data.in_transcript&(data.tpm>0)&(data.min_bases_to_TGTA>=100)]
# plot
annotation_order = ["3' UTR", "exon"]
annotation_colors = ['#f7931d', '0.7']
g = sns.FacetGrid(data=subdata.loc[subdata.ddG<0.5], hue='annotation',
hue_order=annotation_order, palette=annotation_colors);
g.map(tectplots.plot_cdf, 'clip_signal_per_tpm_fold');
tectplots.plot_cdf(subdata_intron['clip_signal_per_tpm_fold'], color='#494979'); plt.xscale('log')
sys.exit()
# find subset of sites in data_intron with similar tpm and sequence
subsubdata = []
weights_seq = []
n_expected = len(subdata_intron)/float(len(subdata_intron.seq.value_counts()))
for seq, n in subdata_intron.seq.value_counts().iteritems():
index = subdata.loc[subdata.seq == seq].index.tolist()
subsubdata.append(subdata.loc[index])
weights_seq.append(pd.Series(float(n)/len(index), index=index))
subsubdata = pd.concat(subsubdata)
weights_seq = pd.concat(weights_seq)
# subset subdata to get approximately equal tpm
binedges = pd.concat([subsubdata.tpm, subdata_intron.tpm]).quantile(np.linspace(0, 1, 20))
counts_per_bin_target = pd.DataFrame(pd.cut(subdata_intron.tpm, binedges.values, precision=1, include_lowest=True).rename('binned_val')).groupby('binned_val').size()
counts_per_bin_current = pd.DataFrame(pd.cut(subsubdata.tpm, binedges.values, precision=1, include_lowest=True).rename('binned_val')).groupby('binned_val').size()
weights_per_bin = counts_per_bin_target/counts_per_bin_current
weights_tpm = pd.Series(weights_per_bin.loc[pd.cut(subsubdata.tpm, binedges.values, precision=1, include_lowest=True)].values, index=subsubdata.index)
# resample with weights
index_sub = np.random.choice(subsubdata.index.tolist(), p=(weights_seq*weights_tpm)/(weights_seq*weights_tpm).sum(), size=len(subsubdata))
subsubdata_resampled = subsubdata.loc[index_sub]
# plot each
plt.figure(); sns.distplot(np.log10(subdata_intron.tpm)); sns.distplot(np.log10(subsubdata_resampled.tpm))
plt.figure(); sns.distplot(subdata_intron.ddG, bins=bins, kde=False, norm_hist=True); sns.distplot(subsubdata.loc[index_sub].ddG, bins=bins, kde=False, norm_hist=True)
elif args.mode == 'plot_annotation_vs_ddG':
"""Using New model to find effects, se how enrichment for UTR changes"""
annotation_order = ["3' UTR", "exon", "5' UTR"]
annotation_colors = ['#f7931d', '0.7', '#be1e2d']
# examine only sites with some hPUM annotation
subdata = data.loc[data.in_transcript&(data.name.str.find('hPUM') == 0)]
# find fraction per ddG bin
subdata.loc[:, 'binned_ddG'] = pd.cut(subdata.ddG, np.hstack([np.arange(-0.5, 5, 0.5), subdata.ddG.max()]),
precision=1, include_lowest=True)
order = subdata.groupby('binned_ddG').first().index.tolist()
num_annotations = pd.concat({name:group.annotation.value_counts() for name, group in subdata.groupby('binned_ddG')}).unstack().loc[order].fillna(0)
fraction_annotation = (num_annotations.transpose()/num_annotations.sum(axis=1)).transpose()
# add background expections
num_background_annotations = data.loc[data.name.str.find('hPUM') == -1].annotation.value_counts().loc[annotation_order]
expected_fractions = num_background_annotations/num_background_annotations.sum()
fraction_annotation.loc['expected'] = expected_fractions
# plot
fraction_annotation.loc[:, annotation_order].plot(kind='bar', stacked=True, colors=annotation_colors, figsize=(3,3), width=0.6)
plt.ylim(0, 1)
plt.tight_layout()
# plot enrichment relative to high ddG bin
np.log2((fraction_annotation/fraction_annotation.loc[order[-1]]).loc[order, annotation_order]).plot(kind='bar', colors=annotation_colors, figsize=(3,3), width=0.8)
sys.exit()
# also find expected fractions given the refgene table
three_UTR_len_list = subprocess.check_output('cat annotations/refseq/hg38_refGene.txt | awk \'{OFS="\\t"}{if ($4=="-") {l=$7-$5} else if ($4=="+") {l=$6-$8} else {l="3UTR_length"}; print $13, l}\'', shell=True).strip().split('\n')
three_UTR_len = pd.DataFrame([[s.split('\t')[0], int(s.split('\t')[1])] for s in three_UTR_len_list[1:]], columns=three_UTR_len_list[0].split()).groupby('name2')['3UTR_length'].median()
# get CDS len
# get 5' UTR len
five_UTR_len_list = subprocess.check_output('cat annotations/refseq/hg38_refGene.txt | awk \'{OFS="\\t"}{if ($4=="-") {l=$6-$8} else if ($4=="+") {l=$7-$5} else {l="5UTR_length"}; print $13, l}\'', shell=True).strip().split('\n')
five_UTR_len_df = pd.DataFrame([[s.split('\t')[0], int(s.split('\t')[1])] for s in five_UTR_len_list[1:]], columns=five_UTR_len_list[0].split())
five_UTR_len = five_UTR_len_df.groupby('name2')['5UTR_length'].median()
enrichment = {}
fraction = {}
for name, group in data.loc[data.tpm>0].groupby('binned_ddG'):
group1 = group.loc[group.flip_annot == 'noflip']
group2 = group.loc[group.flip_annot != 'noflip']
#fracmat[name] = group.annotation.value_counts()/float(len(group))
fraction[(name, 'noflip')] = (group1.annotation=="3' UTR").mean()
fraction[(name, 'flip')] = (group2.annotation=="3' UTR").mean()
enrichment[(name, 'noflip')] = np.log2((group1.annotation=="3' UTR").mean()/expected_fractions.loc["3' UTR"])
enrichment[(name, 'flip')] = np.log2((group2.annotation=="3' UTR").mean()/expected_fractions.loc["3' UTR"])
fraction = pd.Series(fraction).unstack().loc[order]
fraction.plot(marker='o', linestyle='none', figsize=(3,3))
plt.xticks(np.arange(len(order)), order, rotation=90)
plt.axhline(expected_fractions.loc["3' UTR"], linestyle='--', color='k')
plt.xlim(-1, len(order))
plt.ylim(0, 1)
elif args.mode == 'plot_versus_proximity':
"""Plot how the distribution of clip enrichment changes if there are nearby UGUA sites"""
data.loc[:, 'min_distance_to_TGTA'] = data.loc[:, ['upstream_bases_to_TGTA', 'downstream_bases_to_TGTA']].min(axis=1)
binedges = [0, 60, 150, 251]
data.loc[:, 'binned_min_dist'] = pd.cut(data.min_distance_to_TGTA, binedges, include_lowest=True)
subdata = data.loc[(data.tpm>0)&(data.annotation=="3' UTR")]
g = sns.factorplot(data=subdata, x='binned_ddG', hue='binned_min_dist', y='clip_input_per_tpm', estimator=np.median,
errwidth=1,capsize=1, linestyles='', marker='.', palette='viridis')
plt.yscale('log'); plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
ymax = subdata.groupby('binned_ddG').get_group(order[0]).clip_signal_per_tpm.median()
x = pd.Series({name:0.5*(group.ddG.max() + group.ddG.min()) for name, group in subdata.groupby('binned_ddG')})
y = ymax*np.exp(x/seqmodel.get_ddG_conversion(temperature))
plt.plot(np.arange(len(order)), y.loc[order], 'k--')
ylim = [0.01, 20]
plt.ylim(ylim)
elif args.mode == 'plot_ss_ddG_versus_clip':
subdata = data.loc[(data.binned_ddG==order[0])&(data.tpm>0)]
subdata.loc[:, 'binned_ddG_ss'] = pd.cut(data.ss_ddG,
#data.ddG.quantile(np.linspace(0, 1, 100)),
np.hstack([np.linspace(subdata.ss_ddG.min(), 20, 25),
subdata.ss_ddG.max()]),
include_lowest=True, precision=2)
order_ss = subdata.groupby('binned_ddG_ss').first().index.tolist()
g = sns.factorplot(data=subdata, x='binned_ddG_ss', y='clip_signal_per_tpm', estimator=np.median,
errwidth=1,capsize=2, linestyles='', marker='.', palette=['k'])
plt.yscale('log'); plt.xticks(rotation=90); plt.subplots_adjust(bottom=0.35)
ylim = plt.gca().get_ylim()
# plot line
ymax = subdata.groupby('binned_ddG_ss').get_group(order_ss[0]).clip_signal_per_tpm.median()
x = np.linspace(subdata.ss_ddG.min(), 20)
y = ymax*np.exp(x/seqmodel.get_ddG_conversion(temperature))
plt.plot(x, y, 'k--')
plt.ylim(ylim)
elif args.mode == 'plot_indiv_sites':
"""Rather than aggregate behavior, examine individual sites"""
norad_refseqid = 'NR_027451'
counts = {}
for i in np.arange(11):
print i
directory = 'analysis/clip/split_%d/'%i
filenames = [os.path.join(directory, filename) for filename in os.listdir(directory) if filename[-6:]=="txt.gz"]
keys = [filename.split('.')[-8] for filename in filenames]
for key, filename in zip(keys, filenames):
data_table = pd.read_csv(filename, compression='gzip', index_col=0)
counts[(i, '%s'%(key))] = processing.get_counts_from_counts_table(data_table, )
counts = pd.concat(counts).unstack(level=1)
clip_signal_counts = counts.rep1 + counts.rep2
data = pd.concat([data.set_index(['split_id', 'name']), clip_signal_counts.rename('clip_signal_counts')], axis=1).reset_index()
norad_subset = data.loc[data.refseq_id==norad_refseqid].sort_values('start').copy()
dist_to_next_site = pd.Series({idx:next_start - stop for idx, stop, next_start in zip(norad_subset.index.tolist()[:-1], norad_subset.stop.iloc[:-1], norad_subset.start.iloc[1:])})
# for all sites within 40 bp, take the max
for idx, dist in dist_to_next_site.iteritems():
pass
elif args.mode == 'compare_to_pum12_kd' or args.mode == 'compare_to_pum2_oe':
"""Load supp data from NAR paper and compare sites."""
# load the biomart ref
biomart_data = pd.read_table('annotations/ensemble_gene_converter_biomart.txt', names=['gene_id', 'transcript_id', 'gene_name', 'refseq_id', 'refseq_nc'], header=0)
biomart_data.loc[:, 'refseq_comb'] = [refseq_id if not str(refseq_id)=='nan' else refseq_nc for idx, refseq_id, refseq_nc in biomart_data.loc[:, ['refseq_id', 'refseq_nc']].itertuples()]
if args.mode == 'compare_to_pum12_kd':
col_name = 'gene_name'
else:
col_name = 'gene_id'
data.loc[:, 'gene_name'] = pd.Series(biomart_data.groupby('refseq_comb').first().loc[data.refseq_id.dropna()][col_name].values, index=data.refseq_id.dropna().index)
# group by the gene and find occupancy and other metrics
RT = -seqmodel.get_ddG_conversion(temperature=37)
ss_ddG_threshold = 10 # kcal/mol
occupancy_data = {}
for name, group in data.groupby('gene_name'):
# filter all for ss structure
group_3UTR = group.loc[(group.annotation=="3' UTR")&(group.ss_ddG < ss_ddG_threshold)]
occupancy_3UTR = np.exp(-group_3UTR.ddG/RT).sum()
occupancy_noflip_3UTR = np.exp(-group_3UTR.ddG_noflip/RT).sum()
num_consensus_3UTR = (group_3UTR.ddG < 0.5).sum()
num_consensus_CDS = (group.loc[(group.annotation=="exon")].ddG < 0.5).sum()
num_sites_2kc_3UTR = (group_3UTR.ddG < 2).sum()
num_sites_between1and4kc_3UTR = ((group_3UTR.ddG < 4)&(group_3UTR.ddG >= 1)).sum()
min_dG_3UTR = group_3UTR.ddG.min()
occupancy_not3UTR = np.exp(-group.loc[group.annotation!="3' UTR"].ddG/RT).sum()
occupancy_data[name] = pd.Series({'occupancy_3UTR':occupancy_3UTR,
'occupancy_noflip_3UTR':occupancy_noflip_3UTR,
'occupancy_not3UTR':occupancy_not3UTR,
'min_ddG':min_dG_3UTR,
'num_consensus_3UTR':num_consensus_3UTR,
'num_consensus_CDS':num_consensus_CDS,
'num_sites_2kc_3UTR':num_sites_2kc_3UTR,
'num_sites_between1and4kc_3UTR':num_sites_between1and4kc_3UTR})
occupancy_data = pd.concat(occupancy_data).unstack()
occupancy_data.loc[occupancy_data.min_ddG.isnull(), 'min_ddG'] = 10 #kcal.mol
# load expression data
if args.mode == 'compare_to_pum12_kd':
expression_data = pd.read_csv('annotations/nar-01280/supp_table4.csv')
expression_data.loc[:, 'lfc'] = expression_data.lfc.replace('#NAME?', np.nan).astype(float).replace(np.inf, np.nan)
expression_data.loc[:, 'log_fpkm_cntrl'] = np.log10(expression_data.FPKM_NTC)
expression_fpkm_bins = [-np.inf, 1, 1.5]
elif args.mode == 'compare_to_pum2_oe':
expression_data = pd.read_table('annotations/GSE75440/GSe75440_PUM2edgeR.txt.gz')
expression_data.rename(columns={col:col.lower().replace(' ', '_').replace('.', '_') for col in expression_data}, inplace=True)
expression_data.loc[:, 'lfc'] = expression_data['logfc_pum2/gfp']
expression_data.loc[:, 'log_fpkm_cntrl'] = np.log10(expression_data.loc[:, 'gfp_#1_fpkm':'gfp_#3_fpkm']+0.01).mean(axis=1)
#expression_data.loc[:, 'gene_id'] = expression_data.gene
#expression_data.loc[:, 'gene'] = expression_data.genename
expression_data.loc[:, 'sig_up'] = (expression_data.adj_pval_tgw < 1E-2)&(expression_data.lfc > 0)
expression_data.loc[:, 'sig_down'] = (expression_data.adj_pval_tgw < 1E-2)&(expression_data.lfc < 0)
#expression_data.set_index('gene', inplace=True)
expression_fpkm_bins = [-np.inf, 1, 3]
sys.exit()
# choose randomly from a subset of similarly expressed genes in the control
"""
row_subsets = {}
length_factor = 5
for name, subset in zip(['up_matched', 'down_matched'], [expression_data.sig_up, expression_data.sig_down]):
kernel = st.gaussian_kde(expression_data.loc[subset].log_fpkm_cntrl)
prob_density = kernel(expression_data.loc[~subset].log_fpkm_cntrl)
kernel_start = st.gaussian_kde(np.random.choice(expression_data.loc[~subset].log_fpkm_cntrl.replace([-np.inf], np.nan).dropna(), 1000))
prob_density_orig = kernel_start(expression_data.loc[~subset].log_fpkm_cntrl)
weights = (pd.Series(prob_density/prob_density_orig/
np.nansum((prob_density/prob_density_orig)), index=expression_data.loc[~subset].index).
replace([-np.inf, np.inf], np.nan).fillna(0))
row_subsets[name] = np.random.choice(expression_data.loc[~subset].index.tolist(),
p=weights,
size=int(subset.sum()*length_factor),
replace=False)
row_subsets[name.split('_')[0]] = expression_data.loc[subset].index.tolist()
"""
# find roc
roc_curves = {}
for name in occupancy_data:
expression_data.loc[:, 'occupancy'] = occupancy_data.loc[expression_data.gene, name].values
expression_data_sub = expression_data.dropna(subset=['lfc', 'occupancy']).copy()
if name=='min_ddG':
predicted_up=False
else:
predicted_up = True
roc_curves[('any', name)] = processing.find_roc_data(expression_data_sub, 'occupancy', 'sig_down')
#for expression_threshold in expression_fpkm_bins:
# roc_curves[(expression_threshold, name)] = processing.find_roc_data(expression_data_sub.loc[expression_data_sub.log_fpkm_cntrl >= expression_threshold], 'occupancy', 'sig_up', predicted_up=predicted_up, )
roc_curves = pd.concat(roc_curves, names=['cntrl_expression', 'occ_def', 'occ_val'])
g = sns.FacetGrid(data=roc_curves.reset_index(), hue='occ_def', col='cntrl_expression', hue_order=['occupancy_3UTR', 'occupancy_noflip_3UTR', 'occupancy_not3UTR'], palette=['b', 'r', '0.5'], ); g.map(plt.plot, 'fpr', 'tpr', )
g = sns.FacetGrid(data=roc_curves.reset_index(), hue='occ_def', col='cntrl_expression', hue_order=['occupancy_3UTR', 'min_ddG', 'num_sites_2kc_3UTR', 'num_sites_between1and4kc_3UTR'], palette=['b', 'g', 'm', 'c'], ); g.map(plt.plot, 'fpr', 'tpr', )
# find ROC curve for genes with NO consensus sites
expression_data.loc[:, 'occupancy'] = occupancy_data.loc[expression_data.gene, 'occupancy_3UTR'].values
expression_data.loc[:, 'has_consensus'] = occupancy_data.loc[expression_data.gene, 'num_consensus_3UTR'].values > 0
expression_data_sub = expression_data.dropna(subset=['lfc', 'occupancy'])
roc_noconsesus = processing.find_roc_data(expression_data_sub.groupby('has_consensus').get_group(False), 'occupancy', 'sig_up')
roc_wconsesus = processing.find_roc_data(expression_data_sub.groupby('has_consensus').get_group(True), 'occupancy', 'sig_up')
g = sns.FacetGrid(roc_curves.loc[-np.inf].reset_index(), hue='occ_def', hue_order=['occupancy_3UTR', 'occupancy_not3UTR'], palette=['b', '0.5']); g.map(plt.plot, 'fpr', 'tpr')
plt.plot(roc_noconsesus.fpr, roc_noconsesus.tpr, 'b--')
idx_noconsensus = np.abs(roc_noconsesus.reset_index().loc[:, 'index'] - 1).sort_values().index[0]
plt.scatter(roc_noconsesus.reset_index().loc[idx_noconsensus].fpr, roc_noconsesus.reset_index().loc[idx_noconsensus].tpr, c='b')
#plt.plot(roc_wconsesus.fpr, roc_wconsesus.tpr, 'b:')
idx_gene_body = np.abs(roc_curves.loc[-np.inf].loc['occupancy_not3UTR'].reset_index().occ_val-1).sort_values().index[0]
plt.scatter(roc_curves.loc[-np.inf].loc['occupancy_not3UTR'].reset_index().loc[idx_gene_body].fpr, roc_curves.loc[-np.inf].loc['occupancy_not3UTR'].reset_index().loc[idx_gene_body].tpr, c='0.5')
plt.plot([0, 1], [0, 1], 'k--')
# plot the roc plot for the other model
expression_data.loc[:, 'occupancy'] = occupancy_data.loc[expression_data.gene, 'occupancy_noflip_3UTR'].values
expression_data_sub = expression_data.dropna(subset=['lfc', 'occupancy'])
roc_noconsesus_noflip = processing.find_roc_data(expression_data_sub.groupby('has_consensus').get_group(False), 'occupancy', 'sig_up')
plt.plot(roc_noconsesus_noflip.fpr, roc_noconsesus_noflip.tpr, 'r--')
print processing.get_tpr_fpr(expression_data_sub.has_consensus, expression_data_sub.sig_up)
print processing.get_tpr_fpr(expression_data_sub.loc[~expression_data_sub.has_consensus].occupancy >= 1, expression_data_sub.loc[~expression_data_sub.has_consensus].sig_up)
roc = processing.find_roc_data(expression_data_sub, 'occupancy', 'sig_up')
# laod 3' UTR length from refseq database
bed_data = pd.read_table('annotations/refseq/hg38_refGene.3UTR.bed', names=variables.bed_fields + ['gene_name'], header=0)
bed_data.loc[:, 'utr_length'] = bed_data.stop - bed_data.start
expression_data.loc[:, "utr_length_min"] = bed_data.groupby('gene_name')['utr_length'].min().loc[expression_data.gene].values
expression_data.loc[:, "utr_length_max"] = bed_data.groupby('gene_name')['utr_length'].max().loc[expression_data.gene].values
expression_data.loc[:, "utr_length_median"] = bed_data.groupby('gene_name')['utr_length'].median().loc[expression_data.gene].values
roc_curves_length = {}
for name in ['utr_length_min', 'utr_length_max', 'utr_length_median']:
roc_curves_length[name] = processing.find_roc_data(expression_data.dropna(subset=['lfc', name]).copy(), name, 'sig_up')
roc_curves_length = pd.concat(roc_curves_length, names=['length_def', 'length_val'])
sys.exit()
# for each gene in the expression data, find the sites that are associated with it
data_subsets = {}
for key, idxs in row_subsets.items():
print key
for i, (idx, gene) in enumerate(expression_data.loc[idxs].gene.iteritems()):
if i%10 == 0:
print i
possible_refseq_ids = biomart_data.loc[(biomart_data.gene_name==gene)].refseq_comb.dropna().unique().tolist()
data_subset = data.loc[pd.Series(np.in1d(data.refseq_id, possible_refseq_ids), index=data.index)&
(data.annotation=="3' UTR")].copy()
data_to_save = pd.Series({'min_ddG':data_subset.ddG.min(), 'median_ddG':data_subset.ddG.median(),
'num_sites':len(data_subset), 'num_sites_2kc':(data_subset.ddG < 2).sum(),
'sites_index':data_subset.index.tolist()})
data_subsets[(key, gene)] = data_to_save
if args.mode == "compare_to_clip_peaks":
"""Compare the motifs found to the clip peaks analysis"""
clip_peaks = processing.load_bed('analysis/beds/ENCFF372VPV.rep1.ENCFF141SVY.rep2.bed')
gc_content = pd.read_table('analysis/beds/ENCFF372VPV.rep1.ENCFF141SVY.rep2.bed.nuc', usecols=['4_usercol', '8_pct_gc'], index_col='4_usercol', squeeze=True)
peak_count = pd.read_table('analysis/beds/ENCFF372VPV.rep1.ENCFF141SVY.rep2.bed.bam_counts', index_col=0)
total_counts = {'rep2':np.loadtxt('analysis/bams/rep2.ENCFF732EQX.R2.bamcount')}
which_motifs = pd.read_table('analysis/beds/ENCFF372VPV.rep1.ENCFF141SVY.rep2.bed.peak_id', skiprows=1, header=None, names=['clip_peak', 'motif_site']).replace('.', np.nan)
peak_score = (peak_count.rep1 + peak_count.rep2)/(peak_count.input + 1)
num_sites = (which_motifs.dropna().groupby('clip_peak').size()).loc[clip_peaks.name].fillna(0)
site_info = data.loc[data.split_id==10].set_index('name')
####### OLD 2/2/18 #######
if args.mode == 'compare_consensus_ss_energies':
"""Make plots for the comparing the clip enrichment to the ss energy difference."""
order = ['ss_50', 'ss_75', 'ss_100']
data = pd.concat([motif_data.loc[(motif_data.score>11), ['fold_enrichment']+order]], axis=1).dropna()
data_melt = pd.melt(data, id_vars=['fold_enrichment'])
xlim = np.array([0, 20])
g = sns.FacetGrid(data=data_melt, col='fold_enrichment', xlim=xlim, palette=['0.5']); g.map(plt.scatter, 'value', 'fold_enrichment', marker='.', edgecolor='k')
for i, ax in enumerate(g.axes.flat):
slope, intercept, rvalue, pvalue, stderr = st.linregress(data.loc[:, order[i]], data.loc[:, 'clip_enrichment'])
ax.plot(xlim, xlim*slope+intercept, c='k')
ax.annotate('rsq = %4.3f'%rvalue**2, xy=(0.95, 0.95), xycoords='axes fraction',
horizontalalignment='right',
verticalalignment='top',)
# plot mean.
ddG_binned = pd.Series(np.digitize(motif_data.loc[:, order].mean(axis=1), [2, 4, 6, 8, 10]), index=motif_data.index)
motif_data.loc[:, 'ss_binned'] = ddG_binned
motif_data.loc[:, 'ss_ddG'] = ddG_binned
# plot
subset_index = motif_data.ddG < 0.5
sub_data = motif_data.loc[subset_index].copy()
ddG_ss = sub_data.loc[subset_index].groupby('ss_binned')[order].median().mean(axis=1)
# take subset with low ddG (affinity)
cols = ['input', 'rep1', 'rep2']
# do it differently
num_reads = random_data.loc[:, ['input', 'rep1', 'rep2']].mean()
#num_reads = pd.Series(1, index=cols)
counts = sub_data.groupby('ss_binned')[cols].mean()/num_reads
counts_bounds = pd.concat({(name, col):pd.Series(np.abs(bootstrap.ci(group.loc[:, col]/num_reads.loc[col], np.mean) - counts.loc[name, col]),
index=['eminus', 'eplus'])
for col in cols for name, group in sub_data.groupby('ss_binned')})
counts_err = pd.concat({name:group.unstack().loc[:, name].unstack()
for name, group in counts_bounds.groupby(level=2)})
mat = pd.concat([ddG_ss.rename('ddG'), counts, ], axis=1)
# plot
plt.figure(figsize=(4,3))
for col in cols:
plt.scatter(mat.ddG, mat.loc[:, col], label=col)
plt.errorbar(mat.ddG, mat.loc[:, col], yerr=[counts_err.loc[key].loc[mat.index.tolist(), col] for key in ['eminus', 'eplus']], fmt=',')
plt.yscale('log')
plt.xlabel('$\Delta \Delta G$ (kcal/mol)')
plt.ylabel('num. reads per site')
plt.tight_layout()
# plot the
g = sns.FacetGrid(sub_data_melt, hue='ss_binned', col='variable'); g.map(tectplots.plot_cdf, 'value'); plt.xscale('log')
if args.mode=='find_num_in_annotations':
"""Find the enrichmnet for various annotations"""
order = ["5' UTR", "exon", "3' UTR"]
sub_data = motif_data.loc[ motif_data.ddG < 0.5].copy()
fraction_consensus = sub_data.annotation.value_counts()/len(sub_data)
expected_fraction = random_data.annotation.value_counts()/len(random_data)
# plot fraction
tectplots.figure(); sub_data.annotation.value_counts().loc[order].plot(kind='bar'); plt.ylabel('number of sites'); plt.tight_layout()
# plot enrichment
tectplots.figure(); np.log2(fraction_consensus/expected_fraction).loc[order].plot(kind='bar'); plt.ylabel('log2 obs/expected');plt.tight_layout()
# plot signal in each annotation
sub_data_melt = pd.melt(sub_data.loc[:, ['annotation'] + cols], id_vars=['annotation'])
g = sns.FacetGrid(sub_data_melt, hue='annotation', col='variable'); g.map(tectplots.plot_cdf, 'value'); plt.xscale('log')
g = sns.FacetGrid(sub_data, hue='annotation'); g.map(tectplots.plot_cdf, 'fold_enrichment'); plt.xscale('log')