-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_design_exploration_EJ_edit_random_1.27.22.Rmd
1521 lines (1187 loc) · 61.4 KB
/
screen_design_exploration_EJ_edit_random_1.27.22.Rmd
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
---
editor_options:
chunk_output_type: console
---
title: "Exploration ENCODE CRISPRi screen design"
author: "Andreas Gschwind"
date: "10/06/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Goal
Explore candidate loci for ENCODE enhancer screen.
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(here)
library(rtracklayer)
library(BiocParallel)
library(cowplot)
```
### Candidate genes
Transcript-per-million (TPM) data computed from the Gasperini et al., 2019 data is used to select
potential loci with genes above 50 TPM.
```{r tmpData, fig.height=4, fig.width=5}
# load TPM data
#tpm <- read_csv(here("results", "gasperini", "tpm.csv"),
#col_types = cols(.default = col_character(),tpm = col_double()))
##ej
tpm = read_csv("/Users/ejagoda/Documents/HGRM/EP_benchmarking/Encode_Crispri_Screen/tpm.csv",col_types = cols(.default = col_character(),tpm = col_double()))
# number and percentage of genes above 50 TPM
n_above_50 <- sum(tpm$tpm >= 50)
# plot TPM distribution
ggplot(tpm, aes(tpm)) +
geom_histogram(bins = 50) +
geom_vline(xintercept = 50, color = "red") +
labs(x = "Transcripts-per-million (TPM)", title = paste0("Genes above 50 TPM: ", n_above_50)) +
scale_x_log10() +
theme_bw()
```
### Genome annotations
GENCODE v26lift37 genome annotations were used in the original analysis by Gasperini et al., but to
be consistent with ENCODE it would be best to design the screen in hg38. ENCODE 4 will use GENCODE
v29 annotations. Let's see how many genes can be uniquely identified in genome annotations.
```{r genomeAnnot}
# URLs to GENCODE annotations
v26lift37_url <- "http://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_26/gencode.v26.annotation.gtf.gz"
v26_url <- "http://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_26/GRCh37_mapping/gencode.v26lift37.annotation.gtf.gz"
v29_url <- "http://ftp.ebi.ac.uk/pub/databases/gencode/Gencode_human/release_29/gencode.v29.annotation.gtf.gz"
# import annotations
v26lift37 <- import(v26lift37_url, format = "gtf")
v26 <- import(v26_url, format = "gtf")
v29 <- import(v29_url, format = "gtf")
#EJ
#v26 = read.table("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/just_genes_gencode.v26lift37.annotation.gtf",header =F)
v26 = rtracklayer::import("/Users/ejagoda/Documents/HGRM/EP_benchmarking/Encode_Crispri_Screen/gencode.v26lift37.annotation.gtf.gz")
v29 = rtracklayer::import("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/wct11/gencode.v29.annotation.gtf.gz")
# get Ensembl gene ids without version
#v26lift37$gene_base_id <- sub("\\..*", "", v26lift37$gene_id)
v26$gene_base_id <- sub("\\..*", "", v26$gene_id)
#v29$gene_base_id <- sub("\\..*", "", v29$gene_id)
# get number of genes in TPM data missing from annotations
#in_v26lift37 <- table(tpm$gene %in% v26lift37$gene_base_id)
in_v26 <- table(tpm$gene %in% v26$gene_base_id)
#in_v29 <- table(tpm$gene %in% v29$gene_base_id)
# combine into one table
#in_annot <- list(v26lift37 = in_v26lift37, v26 = in_v26, v29 = in_v29) %>%
# bind_rows(.id = "genome_annotation") %>%
# dplyr::rename(missing = `FALSE`, found = `TRUE`)
# print table
#knitr::kable(in_annot)
```
All annotations seem to miss some of the genes where expression data is present. Surprisingly v26
(hg38) seems to contain most of the genes, which is weird as the paper states that v26lift37 was
used for their analyses. Let's use hg38 v26 for now until we figure out what's going on. #EJ - i think you flipped them, in this version
### Candidate enhancers
K562 DNase-seq peaks from ENCODE (Experiment: ENCSR000EKS, File: ENCFF274YGF (hg38)) are used as
candidate enhancers.
```{r dhsData, fig.height=4, fig.width=5}
# column names in a narrowPeaks bed fil
peak_colnames <- c("chrom", "chromStart", "chromEnd", "name", "score", "strand", "signalValue",
"pValue", "qValue", "peak", "reads")
# column types in a narrowPeaks bed file with added read counts
peak_cols <- cols(
chrom = col_character(),
chromStart = col_double(),
chromEnd = col_double(),
name = col_character(),
score = col_double(),
strand = col_character(),
signalValue = col_double(),
pValue = col_double(),
qValue = col_double(),
peak = col_double(),
reads = col_double()
)
# import ENCODE DNase-seq peaks
#dhs_file <- here("resources/DNase/DNase_counts/ENCFF274YGF_ENCFF257HEE_read_counts.bed.gz")
##Evvie addition, redo when get access to sherlock##
#dhs_file = "/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/ENCFF274YGF_ENCFF257HEE_read_counts.bed.gz"
dhs_file = "/Users/ejagoda/Documents/HGRM/EP_benchmarking/Encode_Crispri_Screen/final_round_design/ENCFF185XRG_w_ENCFF325RTP_q30_sorted.txt"
#raw_dhs_file = read.table("ENCFF274YGF.bed")
#####
dhs <- read_tsv(dhs_file, col_names = peak_colnames, col_types = peak_cols)
# normalize reads for peak width by calculating RPKM
dhs <- dhs %>%
mutate(peak_width = chromEnd - chromStart,
reads_rpkm = reads / (sum(reads) / 1e6) / (peak_width / 1000))
dhs$id = paste0(dhs$chrom,":",dhs$chromStart,"_",dhs$chromEnd)
q_thresholds = seq(from = 0.0, to = 1, by = 0.25)
quants = c()
for (q in q_thresholds){
quant = quantile(as.numeric(paste0(dhs$reads)),q)
quants = c(quants, quant)
}
dhs$quant = "x"
for (i in 1:nrow(dhs)){
read = as.numeric(paste0(dhs[i,"reads"]))
if (read < quants[2]){
dhs[i,"quant"] = "0_25"
}
else if (read >= quants[2] & read < quants[3]){
dhs[i,"quant"] = "25_50"
}
else if (read >= quants[3] & read < quants[4]){
dhs[i,"quant"] = "50_75"
}
else if (read >= quants[4] & read < quants[5]){
dhs[i,"quant"] = "75_1"
}
}
###add for each dhs the quantile and the tss and then you can just report that in the locus stats - quant should be easish
##then can sample 100 samples of 25 and see which ones you could do the thresholds for
# plot DNase reads distribution
ggplot(dhs, aes(x = reads)) +
geom_histogram(bins = 50, fill = hsv(h = 0.1, s = 0.8, alpha = 1)) +
labs(title = paste(nrow(dhs), "K562 DHS"), x = "DNase-seq reads in DHS") +
theme_bw()
##EJ plot DHS
ggplot(dhs, aes(x = reads)) +
stat_ecdf(geom = "step") +
labs(title = "K562 DHS", x = "DNase-seq reads in DHS", y = "cumulative fraction")
#not to self, export this, find inflection point, show median/mean
ggplot(dhs, aes(x = reads_rpkm)) +
stat_ecdf(geom = "step") +
labs(title = "rpkm normalized K562 DHS", x = "rpkm normalized DNase-seq reads in DHS", y = "cumulative fraction")
#not to self, export this, find inflection point, show median/mean
# create GRanges object
dhs_gr <- makeGRangesFromDataFrame(dhs, keep.extra.columns = TRUE)
```
### Candidate loci
For every protein-coding gene above 50TPM the number of other genes, other genes above 50 TPM and
DHS within 1Mb are counted.
###EJ - need to add the gene names and the dhs_locations
```{r locusStatsFunction}
# function to count all genes and peaks within a certain distance of a target gene
compute_locus_stats <- function(gene, genes_annot, peaks, tpm_data, tpm_threshold = 50,
locus_width = 2e6) {
# create locus window, centered on TSS of gene
locus <- resize(gene, width = locus_width, fix = "start")
locusstart = as.numeric(paste0(start(locus)))
locusend = as.numeric(paste0(end(locus)))
# get all genes and peaks within that locus window
locus_genes <- subsetByOverlaps(genes_annot, locus, ignore.strand = TRUE)
locus_dhs <- subsetByOverlaps(peaks, locus, ignore.strand = TRUE)
#locus_nearest_gene_to_peak <- locus_genes[precede(locus_dhs,locus_genes, ignore.strand=FALSE)]
#locus_distance_nearest_gene_to_peak <- distanceToNearest(locus_dhs,locus_genes, ignore.strand=FALSE)
#locus_distance_nearest_gene_to_peak <- mcols(distanceToNearest(ranges(dhs_gr),genes$tss))[,1]
x = distanceToNearest(ranges(locus_dhs),locus_genes$tss)
locus_distance_nearest_tss_to_peak <- mcols(x)[,1]
locus_nearest_tss = locus_genes$gene_base_id[subjectHits(x)]
# get genes with tpm data
locus_gene_start <- start(locus_genes)
locus_gene_ids <- locus_genes$gene_name
locus_genes <- locus_genes$gene_base_id
genes_with_tpm_data <- intersect(locus_genes, tpm$gene)
genes_without_tmp_data <- setdiff(locus_genes, tpm$gene)
# get tpm values for genes if available
locus_tpm <- filter(tpm_data, gene %in% genes_with_tpm_data)
tpm_values <- pull(locus_tpm, tpm)
# get genes above tpm threshold
genes_above_tpm <- locus_tpm %>%
filter(tpm >= tpm_threshold) %>%
pull(gene)
# create output data frame
'
output <- tibble(
locus = gene$gene_base_id,
dhs = length(locus_dhs),
dhs_reads = list(locus_dhs$reads),
genes = length(locus_genes),
genes_above_tpm = length(genes_above_tpm),
genes_with_tpm_data = length(genes_with_tpm_data),
tpm_values = list(tpm_values),
genes_without_tmp_data = length(genes_without_tmp_data)
)
'
#EJ additions
output <- tibble(
locus = gene$gene_base_id,
locus_start = locusstart,
locus_end = locusend,
dhs = length(locus_dhs),
locus_dhs_ids = list(locus_dhs$id),
dhs_reads = list(locus_dhs$reads),
dhs_quants = list(locus_dhs$quant),
#dhs_nearest_gene = list(locus_nearest_gene_to_peak),
dhs_nearest_tss = list(locus_nearest_tss),
dhs_nearest_tss_distance = list(locus_distance_nearest_tss_to_peak),
genes = length(locus_genes),
gene_ids = list(locus_genes),
gene_names = list(locus_gene_ids),
gene_start = list(locus_gene_start),
genes_above_tpms = length(genes_above_tpm),
#genes_above_tpm = length(genes_above_tpm),
genes_above_tpm_ids = list(genes_above_tpm),
genes_with_tpm_data = length(genes_with_tpm_data),
tpm_values = list(tpm_values),
genes_without_tmp_data = length(genes_without_tmp_data)
)
return(output)
}
```
```{r computeLocusStats}
# get all genes above 50 TPM
tpm_genes <- filter(tpm, tpm >= 50)
# only retain protein-coding gene locus annotations on autosomes and chromosome X
genes <- v26[v26$type == "gene" &
seqnames(v26) %in% paste0("chr", c(1:22, "X")) &
v26$gene_type == "protein_coding"]
#add the strand oriented start position as tss
genes$tss= IRanges(start(resize(genes,1)),start(resize(genes,1)) + 1)
#EJ
'''
genes <- v26[v26$V3 == "gene" &
v26$V1 %in% paste0("chr", c(1:22, "X")) &
v26$V13 == "protein_coding",]
genes$gene_base_id = "x"
for (i in 1:nrow(genes)){
gene_name = genes[i,"V10"]
gene_name_split = str_split(gene_name,"[.]")[[1]][1]
genes[i,"gene_base_id"] = gene_name_split
}
'''
# extract annotations for tpm filtered genes
#EJ
tpm_genes_annot <- genes[genes$gene_base_id %in% tpm_genes$gene]
tpm_genes <- filter(tpm_genes, gene %in% tpm_genes_annot$gene_base_id)
# split tpm filtered gene annotations into GRangesList, one gene per element
tpm_genes_annot <- split(tpm_genes_annot, f = tpm_genes_annot$gene_base_id)
# register backend for parallel computing
register(MulticoreParam(workers = 5))
# compute locus statistics for each potential target gene
locus_stats <- bplapply(tpm_genes_annot, FUN = compute_locus_stats, genes_annot = genes,
peaks = dhs_gr, tpm_data = tpm, tpm_threshold = 50, locus_width = 2e6)
# combine into one data frame
locus_stats <- bind_rows(locus_stats)
x = data.frame(locus_stats) #got dhs working work on gene ids, see if you can add quant data to the computation and distance threshold
```
Each of the loci will have a number of genes without TPM data. With the correct annotations, these
should be non-expressed genes or genes that did not pass a filter to end up on the scRNA-seq data.
```{r plotLocusStats, fig.height=3.5, fig.width=9}
# colors for locus stats
colors <- c("DHS" = hsv(h = 0.1, s = 0.8, alpha = 1),
"Genes" = hsv(h = 0.62, s = 0.3, alpha = 1),
"Genes above 50 TPM" = hsv(h = 0.62, s = 0.8, alpha = 1))
# plot locus stats for all candidate loci
locus_stats %>%
dplyr::select(locus, DHS = dhs, Genes = genes, `Genes above 50 TPM` = genes_above_tpm) %>%
pivot_longer(cols = -locus, names_to = "stat", values_to = "value") %>%
ggplot(., aes(x = value, fill = stat)) +
facet_wrap(~stat, scale = "free") +
geom_histogram(bins = 20) +
labs(title = paste0("Locus stats all candidate loci (", nrow(locus_stats), ")"),
x = "Number of DHS/Genes/Genes above 50 TPM",
y = "Number of loci") +
scale_fill_manual(values = colors) +
theme_bw()
```
```{r, fig.height=4, fig.width=5}
# extract DNase-seq reads in DHS
dnase_reads <- locus_stats %>%
dplyr::select(locus, dhs_reads) %>%
unnest(cols = dhs_reads)
# plot DNase-seq reads distribution across all DHS within loci
ggplot(dnase_reads, aes(x = dhs_reads)) +
geom_histogram(bins = 50, fill = hsv(h = 0.1, s = 0.8, alpha = 1)) +
labs(title = paste(nrow(dnase_reads), "K562 DHS within", nrow(locus_stats), "loci"),
x = "DNase-seq reads in DHS") +
theme_bw()
```
### Randomly select sets of loci
To perform a CRISPRi screen we will randomly select 25 loci from all the candidate loci around genes
above 50 TPM. This is done repeatedly to check how variable the above locus stats are, i.e. how
representative 25 loci will be.
##EJ - do random samples of 100 loci - and then get the yes/no on if they could do the % tss and the % dhs [maybe] -
###so here's where you get the different sets and could add in the various additional filters from the sets
```{r sampledLocusStats, fig.height=15, fig.width=7}
#Add dhs and tss distance information then do just random samples of 40 for each locus
#compare the end result
locus_stats_df = data.frame(locus_stats)
locus_stats_df$q75counts = "x"
locus_stats_df$bottom75 = "x"
locus_stats_df$q75counts_p = "x"
locus_stats_df$bottom75_p = "x"
for (i in 1:nrow(locus_stats_df)){
quant_list = locus_stats_df[i,"dhs_quants"][[1]]
if (is.na( as.numeric(table(quant_list)["75_1"]))){
locus_stats_df[i,"q75counts"] = 0
}
else{
locus_stats_df[i,"q75counts"] = as.numeric(table(quant_list)["75_1"])
locus_stats_df[i,"q75counts_p"] = as.numeric(table(quant_list)["75_1"])/length(quant_list)
}
locus_stats_df[i,"bottom75"] = sum(table(quant_list)) - as.numeric(table(quant_list)["75_1"])
locus_stats_df[i,"bottom75_p"] = as.numeric(paste0(locus_stats_df[i,"bottom75"]))/length(quant_list)
}
locus_stats_df$g100kb = "x"
locus_stats_df$l100kb = "x"
locus_stats_df$g100kb_p = "x"
locus_stats_df$l100kb_p = "x"
for (i in 1:nrow(locus_stats_df)){
distances = locus_stats_df$dhs_nearest_tss_distance[[i]]
if (length(distances) == 0){
locus_stats_df[i,"g100kb"] = 0
locus_stats_df[i,"l100kb"] = 0
}
else{
g100kb_count = length(which(distances > 100000))
l100kb_count = length(which(distances <= 100000 & distances > 1000)) #want at least 1kb from promoter
locus_stats_df[i,"g100kb"] = g100kb_count
locus_stats_df[i,"l100kb"] = l100kb_count
}
locus_stats_df[i,"g100kb_p"] = as.numeric(paste0(locus_stats_df[i,"g100kb"]))/length(distances)
locus_stats_df[i,"l100kb_p"] = as.numeric(paste0(locus_stats_df[i,"l100kb"]))/length(distances)
}
#get the tpm genes for rep 6
rep_6_loci = read.table("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/loci_only_bed_version_rep6.bed",header = F,sep = '\t')
colnames(rep_6_loci) = c("chr","start","end","seqnames")
locus_stats_rep6 = locus_stats_df[locus_stats_df$locus %in% rep_6_loci$seqnames,]
#write.table(locus_stats_rep6,"/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/locus_stats_full_rep6.txt",quote = F,sep = '\t',row.names = F)
locus_stats_rep6_small = locus_stats_rep6[,c("locus","genes_above_tpms","genes_above_tpm_ids")]
sum(locus_stats_rep6_small$genes_above_tpms) #156
locus_stats_rep6_small$genes_above_tpms_list = paste(locus_stats_rep6_small$genes_above_tpm_ids,sep = ";")
locus_stats_rep6_small$genes_above_tpm_ids = NULL
write.table(locus_stats_rep6_small,"/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/rep_6_locus_info_genes_>50tpm.txt", quote = F,sep = '\t' ,row.names = F)
rep_tab_small = rep_tab[,c("seqnames","chr","start","end","genes_above_tpms" ,"genes_above_tpm_ids")]
write.table(rep_tab_small,paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/wct11/Potential_Loci/Fixed_TPM/locus_info_w_genes_>50tpm",rep,".txt"),sep = '\t',row.names = F, col.names = T,quote = F)
# repeatedly sample 25 loci move this down, add the quantile stuff
set.seed(211005)
#sample_reps <- replicate(sample_n(locus_stats, size = 25), n = 25, simplify = FALSE)
#try sampling 100 loci - need 40 total where you could get 50% DHS top quant and max 25% more than 1kb away
sample_reps <- replicate(sample_n(locus_stats, size = 25), n = 100, simplify = FALSE)
sample_reps <- sample_reps %>%
bind_rows(.id = "rep") %>%
mutate(rep = fct_inorder(rep))
sample
sample_reps$chr = "x"
for (i in 1:nrow(sample_reps)){
chr = str_split( sample_reps[i,"locus_dhs_ids"][[1]][1][[1]][1],":")[[1]][1]
sample_reps[i,"chr"] = chr
}
sample_reps_gr <- makeGRangesFromDataFrame(sample_reps, keep.extra.columns = TRUE,start.field = "locus_start",end.field = "locus_end",seqnames.field = "locus")
rep_overlaps = c()
for (rep in unique(sample_reps_gr$rep)){
rep_loci = sample_reps_gr[sample_reps_gr$rep == rep,]
cat(paste0("\nrep",rep))
overlaps = 0
for (i in 1:(length(rep_loci))){
for (j in 1:(length(rep_loci))){
if (i != j & i < j){
#cat(paste0(" locus",i,"vs",j))
intersect = GenomicRanges::intersect(x = rep_loci[i,],y = rep_loci[j,])
if (length(intersect) > 0){
overlaps = overlaps + 1
cat(paste0("overlap locus",i,"vs",j))
}
}
}
}
rep_overlaps = c(rep_overlaps,overlaps)
}
#x = sample_reps_gr[sample_reps_gr$rep == 1,]
#ranges(x[x$chr == "chr4",])
#1-64 no overlaps
#GenomicRanges::intersect(x = sample_reps_gr[1,],y = sample_reps_gr[2,])
sample_reps_gr$locus_dhs_sample = NULL
sample_reps_gr$sample_dhs_length = NULL
sample_reps_gr$quant_sample = NULL
sample_reps_gr$q75_count = NULL
sample_reps_gr$g100kb_count = NULL
for (i in 1:length(sample_reps_gr)){
peak_list = sample_reps_gr$locus_dhs_ids[[i]]
distances = sample_reps_gr$dhs_nearest_tss_distance[[i]]
quant_list = sample_reps_gr$dhs_quants[[i]]
if (length(peak_list) >= 40){
peak_sample_index = sample(1:length(peak_list),size = 40)
}
else{
peak_sample_index = 1:length(peak_list)
}
peak_sample = peak_list[peak_sample_index]
distance_sample = distances[peak_sample_index]
quant_sample = quant_list[peak_sample_index]
sample_reps_gr$locus_dhs_sample[[i]] = peak_sample
sample_reps_gr$sample_dhs_length[[i]] = length(peak_sample)
sample_reps_gr$distance_sample[[i]] = distance_sample
sample_reps_gr$quant_sample[[i]] = quant_sample
sample_reps_gr$q75_count[[i]] = sum(quant_sample=="75_1")
sample_reps_gr$g100kb_count[[i]] = length(distance_sample[which( distance_sample > 100000)])
}
#rep summary
locus_stats_df$q75counts = "x"
locus_stats_df$bottom75 = "x"
locus_stats_df$q75counts_p = "x"
locus_stats_df$bottom75_p = "x"
dhs_counts = c()
q75counts = c()
q75counts_p = c()
g100kb_counts = c()
g100kbcounts_p = c()
for (i in 1:length(unique(sample_reps_gr$rep))){
rep = unique(sample_reps_gr$rep)[i]
rep_tab = data.frame(sample_reps_gr[sample_reps_gr$rep == rep,])
dhs_count = sum(as.numeric(paste0(rep_tab$sample_dhs_length)))
q75_count = sum(as.numeric(paste0(rep_tab$q75_count)))
q75_p = q75_count/dhs_count
g100kb_count = sum(as.numeric(paste0(rep_tab$g100kb)))
g100kb_p = g100kb_count/dhs_count
dhs_counts = c(dhs_counts,dhs_count)
q75counts = c(q75counts,q75_count)
q75counts_p = c(q75counts_p,q75_p)
g100kb_counts = c(g100kb_counts, g100kb_count)
g100kbcounts_p = c(g100kbcounts_p,g100kb_p)
}
rep_summary_tab = data.frame(cbind(unique(sample_reps_gr$rep),dhs_counts,q75counts,q75counts_p,g100kb_counts,g100kbcounts_p))
write.table(rep_summary_tab,"rep_summary_tab.txt",quote = F,sep = '\t',row.names = F)
##this should be good
for (rep in unique(sample_reps_gr$rep)){
rep_tab = data.frame(sample_reps_gr[sample_reps_gr$rep == rep,])
bed_version = rep_tab[,c("chr","start","end","seqnames")]
write.table(bed_version ,paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/loci_only_bed_version_rep",rep,".bed"),sep = '\t',row.names = F, col.names = F,quote = F)
outfile_name = paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/sampled_peaks_rep",rep,".txt")
outfile = file(outfile_name)
outfile_name2 = paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/potential_loci/sampled_peaks_rep_bed_only",rep,".bed")
outfile2 = file(outfile_name2)
#write(paste("locus","peak","peak_reads","peak_quant","peak_nearest_tss_distance","locus_genes_id", "locus_gene_names",sep = "\t"),outfile_name)
rep_tab$gene_ids_print = "x"
rep_tab$gene_names_print = "x"
for (i in 1:nrow(rep_tab)){
gene_ids = rep_tab[i,"gene_ids"][[1]]
rep_tab[i,"gene_ids_print"] = paste0(gene_ids,collapse = ";")
gene_names = rep_tab[i,"gene_names"][[1]]
rep_tab[i,"gene_names_print"] = paste0(gene_names,collapse = ";")
peaks = rep_tab[i,"locus_dhs_sample"][[1]]
sampled_quants = rep_tab[i,"quant_sample"][[1]]
for (j in 1:length(peaks)){
#write(paste(rep_tab[i,"locus"],peaks[j],sampled_quants[j],rep_tab[i,"gene_ids_print"],rep_tab[i,"gene_names_print"],sep = "\t"),outfile_name,append = T)
peak_split1= str_split(peaks[j],":")[[1]]
peak_split = str_split(peak_split1[2],"_")[[1]]
write(paste(peak_split1[1],peak_split[1],peak_split[2],peaks[j],sep = "\t"),outfile_name2,append =T)
}
}
}
rep_summary_tab = read.table("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/totally_random_design/rep_summary_tab.txt",header = T,sep = '\t')
ggplot(rep_summary_tab,aes(y = dhs_counts,x = "reps")) +
geom_boxplot()+
geom_jitter(width = 0.2)
dim(rep_summary_tab[rep_summary_tab$dhs_counts == 1000,]) #29
#rep 8 0.298 0.253
###good enough probs
write.table(bed_version[bed_version$locus %in% best_loci_sample_1$locus,],paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/loci_only_bed_version_rep",k,".bed"),sep = '\t',row.names = F, col.names = F,quote = F)
best_loci_sample_1$gene_ids_print = "x"
best_loci_sample_1$gene_names_print = "x"
outfile_name = paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/sampled_peaks_rep",k,".txt")
outfile = file(outfile_name)
write(paste("locus","peak","peak_reads","peak_quant","peak_nearest_tss_distance","locus_genes_id", "locus_gene_names",sep = "\t"),outfile_name)
for (i in 1:nrow(best_loci_sample_1)){
gene_ids = best_loci_sample_1[i,"gene_ids"][[1]]
best_loci_sample_1[i,"gene_ids_print"] = paste0(gene_ids,collapse = ";")
gene_names = best_loci_sample_1[i,"gene_names"][[1]]
best_loci_sample_1[i,"gene_names_print"] = paste0(gene_names,collapse = ";")
peaks = str_split(best_loci_sample_1[i,"peak_sample"],pattern = ";")[[1]]
sampled_reads = str_split(best_loci_sample_1[i,"sampled_reads"],pattern = ";")[[1]]
sampled_quants = str_split(best_loci_sample_1[i,"sampled_quants"],pattern = ";")[[1]]
sampled_nearest_tss_distance = str_split(best_loci_sample_1[i,"sampled_nearest_tss_distance"],pattern = ";")[[1]]
for (j in 1:length(peaks)){
write(paste(best_loci_sample_1[i,"locus"],peaks[j],sampled_reads[j],sampled_quants[j],sampled_nearest_tss_distance[j],best_loci_sample_1[i,"gene_ids_print"],best_loci_sample_1[i,"gene_names_print"],sep = "\t"),outfile_name,append = T)
}
}
#make 1 file with 1 row per loucs and another file with 1 row per peak with a locus id
best_loci_sample_1_printv1 = data.frame(cbind(best_loci_sample_1$locus,best_loci_sample_1$dhs,best_loci_sample_1$gene_ids_print, best_loci_sample_1$gene_names_print,best_loci_sample_1$peak_sample, best_loci_sample_1$sampled_reads,best_loci_sample_1$sampled_quants,best_loci_sample_1$sampled_nearest_tss_distance ))
colnames(best_loci_sample_1_printv1) = c("locus_central_gene","total_dhs","locus_gene_ids","locus_gene_names","sampled_peaks","sampled_peak_DHSreads","sampled_peak_quants","sampled_peaks_distance_to_nearest_tss")
write.table(best_loci_sample_1_printv1,paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/sampled_loci_rep",k,".txt"),quote=F ,sep = "\t",row.names = F)
}
distances = usable_loci$dhs_nearest_tss_distance[[i]]
quant_list = usable_loci$dhs_quants[[i]]
peak_list = usable_loci$locus_dhs_ids[[i]]
g100_kb_peak_indexs = which(distances > 100000)
g100_kb_sampled_indexs = sample(g100_kb_peak_indexs,size = 10)
g100kb_sampled_peaks = peak_list[g100_kb_sampled_indexs]
q75_sampled_already = length(which(quant_list[g100_kb_sampled_indexs] == "75_1"))
l100_kb_peak_indexs = which(distances <= 100000 & distances > 100)
l100kb_peaks = peak_list[l100_kb_peak_indexs]
quants_l100kb_peaks = quant_list[l100_kb_peak_indexs]
top75_l100kb = l100kb_peaks[which(quants_l100kb_peaks == "75_1")]
bottom75_l100kb = l100kb_peaks[which(quants_l100kb_peaks != "75_1")]
#make the bedfile with the intersection across loci so that you can pull those out
dhs_sums = c()
sample_reps_df = data.frame(sample_reps)
for (rep in unique(sample_reps_df$rep)){
total_dhs = sum((sample_reps_df[sample_reps_df$rep == rep, "dhs"]))
dhs_sums = c(dhs_sums,total_dhs)
}
plot(unique(sample_reps_df$rep),dhs_sums) #okay so too many, so pick 40 from each
#for each sample check total number of peaks, check intersection fraction etc
g100_kb_peak_indexs = which(distances > 100000)
g100_kb_sampled_indexs = sample(g100_kb_peak_indexs,size = 10)
g100kb_sampled_peaks = peak_list[g100_kb_sampled_indexs]
dhs_threshold = 20
kb100_threshold = 10
locus_stats_df = data.frame(locus_stats)
locus_stats_df$q75counts = "x"
locus_stats_df$bottom75 = "x"
locus_stats_df$q75counts_p = "x"
locus_stats_df$bottom75_p = "x"
for (i in 1:nrow(locus_stats_df)){
quant_list = locus_stats_df[i,"dhs_quants"][[1]]
if (is.na( as.numeric(table(quant_list)["75_1"]))){
locus_stats_df[i,"q75counts"] = 0
}
else{
locus_stats_df[i,"q75counts"] = as.numeric(table(quant_list)["75_1"])
locus_stats_df[i,"q75counts_p"] = as.numeric(table(quant_list)["75_1"])/length(quant_list)
}
locus_stats_df[i,"bottom75"] = sum(table(quant_list)) - as.numeric(table(quant_list)["75_1"])
locus_stats_df[i,"bottom75_p"] = as.numeric(paste0(locus_stats_df[i,"bottom75"]))/length(quant_list)
}
locus_stats_df$g100kb = "x"
locus_stats_df$l100kb = "x"
locus_stats_df$g100kb_p = "x"
locus_stats_df$l100kb_p = "x"
for (i in 1:nrow(locus_stats_df)){
distances = locus_stats_df$dhs_nearest_tss_distance[[i]]
if (length(distances) == 0){
locus_stats_df[i,"g100kb"] = 0
locus_stats_df[i,"l100kb"] = 0
}
else{
g100kb_count = length(which(distances > 100000))
l100kb_count = length(which(distances <= 100000 & distances > 1000)) #want at least 1kb from promoter
locus_stats_df[i,"g100kb"] = g100kb_count
locus_stats_df[i,"l100kb"] = l100kb_count
}
locus_stats_df[i,"g100kb_p"] = as.numeric(paste0(locus_stats_df[i,"g100kb"]))/length(distances)
locus_stats_df[i,"l100kb_p"] = as.numeric(paste0(locus_stats_df[i,"l100kb"]))/length(distances)
}
#get the potentially usable
usable_loci = locus_stats_df[as.numeric(paste0(locus_stats_df$q75counts)) >= 20 & as.numeric(paste0(locus_stats_df$bottom75)) >= 20 & as.numeric(paste0(locus_stats_df$g100kb)) >= 10 & as.numeric(paste0(locus_stats_df$l100kb)) >= 30, ] #1005/2207 --> 45.5%
usable_loci$peak_sample = "x"
usable_loci$peak_sample_length = "x"
for (i in 1:nrow(usable_loci)){
set.seed(100)
distances = usable_loci$dhs_nearest_tss_distance[[i]]
quant_list = usable_loci$dhs_quants[[i]]
peak_list = usable_loci$locus_dhs_ids[[i]]
g100_kb_peak_indexs = which(distances > 100000)
g100_kb_sampled_indexs = sample(g100_kb_peak_indexs,size = 10)
g100kb_sampled_peaks = peak_list[g100_kb_sampled_indexs]
q75_sampled_already = length(which(quant_list[g100_kb_sampled_indexs] == "75_1"))
l100_kb_peak_indexs = which(distances <= 100000 & distances > 100)
l100kb_peaks = peak_list[l100_kb_peak_indexs]
quants_l100kb_peaks = quant_list[l100_kb_peak_indexs]
top75_l100kb = l100kb_peaks[which(quants_l100kb_peaks == "75_1")]
bottom75_l100kb = l100kb_peaks[which(quants_l100kb_peaks != "75_1")]
if (length(top75_l100kb) >= (20 - q75_sampled_already)){
top75_l100kb_sampled = sample(top75_l100kb,size = 20 - q75_sampled_already)
}
else{
top75_l100kb_sampled = top75_l100kb
}
if (length(bottom75_l100kb) >= (40 - (length(top75_l100kb_sampled) + q75_sampled_already) - (10-q75_sampled_already))){
bottom75_l100kb_sampled = sample(bottom75_l100kb,size = (40 - (length(top75_l100kb_sampled) + q75_sampled_already) - (10-q75_sampled_already)))
}
else{
bottom75_l100kb_sampled #now add back in and extra top 75 if there are and get the total length
}
full_sample = unique(c(g100kb_sampled_peaks,top75_l100kb_sampled,bottom75_l100kb_sampled))
usable_loci[i,"peak_sample"] = paste0(full_sample,collapse = ";")
usable_loci[i,"peak_sample_length"] = length(full_sample)
}
##just go back now and double check in case you're low on top peaks
#make a final sample report
usable_loci$sampled_stats = "x"
usable_loci$sampled_quants = "x"
usable_loci$sampled_nearest_tss_distance = "x"
usable_loci$sampled_reads = "x"
for (i in 1:nrow(usable_loci)){
distances = usable_loci$dhs_nearest_tss_distance[[i]]
quant_list = usable_loci$dhs_quants[[i]]
reads_list = usable_loci$dhs_reads[[i]]
all_locus_peak_list = usable_loci$locus_dhs_ids[[i]]
sampled_peak_list = str_split(usable_loci$peak_sample[[i]],pattern = ";")[[1]]
sampled_peak_indexs = c()
for (j in 1:length(sampled_peak_list)){
peak = sampled_peak_list[j]
peak_index = match(peak,all_locus_peak_list)
sampled_peak_indexs = c(sampled_peak_indexs,peak_index)
}
sampled_distances = distances[sampled_peak_indexs]
g100 = length(which(sampled_distances > 100000))
l100 = length(which(sampled_distances <= 100000 & sampled_distances > 1000))
sampled_quants = quant_list[sampled_peak_indexs]
sampled_reads = reads_list[sampled_peak_indexs]
q75 = length(which(sampled_quants == "75_1"))
b75 = length(which(sampled_quants != "75_1"))
usable_loci[i,"sampled_stats"] = paste0(c(g100,l100,q75,b75),collapse = ";")
usable_loci[i,"sampled_quants"] = paste0(sampled_quants, collapse = ";")
usable_loci[i,"sampled_reads"] = paste0(sampled_reads, collapse = ";")
usable_loci[i,"sampled_nearest_tss_distance"] = paste0(sampled_distances, collapse = ";")
}
best_loci = usable_loci[usable_loci$sampled_stats == "10;30;20;20",] #426
#check genome distribution - one way to avoid any overlaps would be to do bedtools intersect with any loci, maybe do like 30 loci per rep and then do any removal
#just need to check that within a given sample there's no overlap
#retrying this with the actual locus stats printing, seem to find peaks outside of what i think the locus should be, need to redo the self intersection thing with this
#central_genes = data.frame(genes[genes$gene_base_id %in% best_loci$locus,])
#barplot(table(central_genes$seqnames),las = 2,ylab = "n usable loci")
##central_genes_chr1 = central_genes[central_genes$seqnames == "chr1",]
#plot(central_genes_chr1$start,1:nrow(central_genes_chr1),pch = 16, cex = 0.5)
#barplot(table(central_genes$seqnames),ylab = "n best loci")
#central_genes$start_minus1MB = as.numeric(paste0(central_genes$start)) - 1000000
#central_genes$end_plus1MB = as.numeric(paste0(central_genes$end)) + 1000000
best_loci$chr = "x"
for (i in 1:nrow(best_loci)){
chr = str_split(best_loci[i,"locus_dhs_ids"][[1]][1],":")[[1]][1]
best_loci[i,"chr"] = chr
}
#write this as a bedfile to see if any of these loci intersect eachother
bed_version = best_loci[,c("chr","locus_start","locus_end","locus")]
write.table(bed_version,"/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/best_loci_loci_only_for_bedtools_intersect.bed",row.names =F, col.names = F,sep ='\t',quote = F)
intersected_best_version = read.table("best_loci_loci_only_for_bedtools_intersect_self_intersect_self.bed",header=F,sep ='\t')
intersected_best_version_non_self = intersected_best_version[intersected_best_version$V4 != intersected_best_version$V8,]
number = runif(1,1,100000) #69167
set.seed(69167)
for (k in 0:100){
set.seed(69167 + k)
sample_best_loci_indexes = sample(1:nrow(best_loci),size = 25)
best_loci_sample_1 = best_loci[sample_best_loci_indexes,]
#then check if multiple in the same intersected and remove
sample_loci_intersect_tab = intersected_best_version_non_self[intersected_best_version_non_self$V4 %in% best_loci_sample_1$locus, ]
write.table(bed_version[bed_version$locus %in% best_loci_sample_1$locus,],paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/loci_only_bed_version_rep",k,".bed"),sep = '\t',row.names = F, col.names = F,quote = F)
best_loci_sample_1$gene_ids_print = "x"
best_loci_sample_1$gene_names_print = "x"
outfile_name = paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/sampled_peaks_rep",k,".txt")
outfile = file(outfile_name)
write(paste("locus","peak","peak_reads","peak_quant","peak_nearest_tss_distance","locus_genes_id", "locus_gene_names",sep = "\t"),outfile_name)
for (i in 1:nrow(best_loci_sample_1)){
gene_ids = best_loci_sample_1[i,"gene_ids"][[1]]
best_loci_sample_1[i,"gene_ids_print"] = paste0(gene_ids,collapse = ";")
gene_names = best_loci_sample_1[i,"gene_names"][[1]]
best_loci_sample_1[i,"gene_names_print"] = paste0(gene_names,collapse = ";")
peaks = str_split(best_loci_sample_1[i,"peak_sample"],pattern = ";")[[1]]
sampled_reads = str_split(best_loci_sample_1[i,"sampled_reads"],pattern = ";")[[1]]
sampled_quants = str_split(best_loci_sample_1[i,"sampled_quants"],pattern = ";")[[1]]
sampled_nearest_tss_distance = str_split(best_loci_sample_1[i,"sampled_nearest_tss_distance"],pattern = ";")[[1]]
for (j in 1:length(peaks)){
write(paste(best_loci_sample_1[i,"locus"],peaks[j],sampled_reads[j],sampled_quants[j],sampled_nearest_tss_distance[j],best_loci_sample_1[i,"gene_ids_print"],best_loci_sample_1[i,"gene_names_print"],sep = "\t"),outfile_name,append = T)
}
}
#make 1 file with 1 row per loucs and another file with 1 row per peak with a locus id
best_loci_sample_1_printv1 = data.frame(cbind(best_loci_sample_1$locus,best_loci_sample_1$dhs,best_loci_sample_1$gene_ids_print, best_loci_sample_1$gene_names_print,best_loci_sample_1$peak_sample, best_loci_sample_1$sampled_reads,best_loci_sample_1$sampled_quants,best_loci_sample_1$sampled_nearest_tss_distance ))
colnames(best_loci_sample_1_printv1) = c("locus_central_gene","total_dhs","locus_gene_ids","locus_gene_names","sampled_peaks","sampled_peak_DHSreads","sampled_peak_quants","sampled_peaks_distance_to_nearest_tss")
write.table(best_loci_sample_1_printv1,paste0("/Users/ejagoda/Documents/HGRM/Encode_Crispri_Screen/final_round_design/Potential_loci/sampled_loci_rep",k,".txt"),quote=F ,sep = "\t",row.names = F)
}
#of 725, 302 have the perfect 10;30;20;20
#P1 classes
##test if the best loci are unusual - plot the proportial hds, the faction tss, the total number, color the usable
locus_stats_df$isbest = 0
for (i in 1:nrow(locus_stats_df)){
if (paste0(locus_stats_df[i,"locus"]) %in% paste0(best_loci$locus)) {
locus_stats_df[i,"isbest"] = 1
}
}
locus_stats_df_odhs = locus_stats_df[order(as.numeric(paste0(locus_stats_df$q75counts_p)),decreasing = F),]
locus_stats_df_odhs$dhs_rank = 1:nrow(locus_stats_df_odhs)
plot(locus_stats_df_odhs$dhs_rank,as.numeric(paste0(locus_stats_df_odhs$q75counts_p)),pch = 16, cex = 0.5, main = "All TPM > 50 loci",ylab = "% q75 peaks",xlab = "rank")
legend("topleft",fill = c("black","red"),legend = c("all","usable"))
best = locus_stats_df_odhs[locus_stats_df_odhs$isbest == 1,]
points(best$dhs_rank,as.numeric(paste0(best$q75counts_p)),pch =16, cex = 0.5, col = "red")
locus_stats_df_otss = locus_stats_df[order(as.numeric(paste0(locus_stats_df$g100kb_p)),decreasing = F),]
locus_stats_df_otss$dhs_rank = 1:nrow(locus_stats_df_otss)
plot(locus_stats_df_otss$dhs_rank,as.numeric(paste0(locus_stats_df_otss$g100kb_p)),pch = 16, cex = 0.5, main = "All TPM > 50 loci",ylab = "% >100kb nearest Tss",xlab = "rank")
legend("topleft",fill = c("black","red"),legend = c("all","usable"))
best = locus_stats_df_otss[locus_stats_df_otss$isbest == 1,]
points(best$dhs_rank,as.numeric(paste0(best$g100kb_p)),pch =16, cex = 0.5, col = "red")
locus_stats_df_npeaks = locus_stats_df[order(as.numeric(paste0(locus_stats_df$dhs)),decreasing = F),]
locus_stats_df_npeaks$dhs_rank = 1:nrow(locus_stats_df_npeaks)
plot(locus_stats_df_npeaks$dhs_rank,as.numeric(paste0(locus_stats_df_npeaks$dhs)),pch = 16, cex = 0.5, main = "All TPM > 50 loci",ylab = "npeaks",xlab = "rank")
legend("topleft",fill = c("black","red"),legend = c("all","usable"))
best = locus_stats_df_npeaks[locus_stats_df_npeaks$isbest == 1,]
points(best$dhs_rank,as.numeric(paste0(best$dhs)),pch =16, cex = 0.5, col = "red")
#this all looks good:
boxplot(as.numeric(paste0(locus_stats_df$q75counts_p)) ~ locus_stats_df$isbest,ylab = "% DHS q75",xlab = "usable")
boxplot(as.numeric(paste0(locus_stats_df$g100kb_p)) ~ locus_stats_df$isbest,ylab = "% >100kb tss",xlab = "usable")
boxplot(as.numeric(paste0(locus_stats_df$dhs)) ~ locus_stats_df$isbest,ylab = "n_peaks",xlab = "usable")
boxplot(as.numeric(paste0(locus_stats_df$genes)) ~ locus_stats_df$isbest,ylab = "n_genes",xlab = "usable")
boxplot(as.numeric(paste0(locus_stats_df$genes_above_tpm)) ~ locus_stats_df$isbest,ylab = "n_genes_above50tpm",xlab = "usable")
promoter_classifications = read.table("prom_gw_prediction.csv",header=T,sep = ",")
locus_stats_df$perc_p2 = "x"
locus_stats_df$perc_p1 = "x"
locus_stats_df$perc_p0 = "x"
for (i in 1:nrow(locus_stats_df)){
cat(i," ")
locus_genes = locus_stats_df$gene_names[[i]]
p2_count = 0
p1_count = 0
p0_count = 0
for (gene in locus_genes){
promoter_tab = promoter_classifications[paste0(promoter_classifications$name) == gene,]
if (nrow(promoter_tab) == 1){
class = promoter_tab[1,"predicted"]
if (class == 2){
p2_count = p2_count + 1
}
else if (class == 1){
p1_count = p1_count + 1
}
else if (class == 0){
p0_count = p0_count + 1
}
}
}
locus_stats_df[i,"perc_p2"] = p2_count/length(locus_genes)
locus_stats_df[i,"perc_p1"] = p1_count/length(locus_genes)
locus_stats_df[i,"perc_p0"]= p0_count/length(locus_genes)
}
boxplot(as.numeric(paste0(locus_stats_df$perc_p2)) ~ locus_stats_df$isbest,xlab = "usable",ylab = "% p2 predicted promoters in locus")
boxplot(as.numeric(paste0(locus_stats_df$perc_p1)) ~ locus_stats_df$isbest,xlab = "usable",ylab = "% p1 predicted promoters in locus")
boxplot(as.numeric(paste0(locus_stats_df$perc_p0)) ~ locus_stats_df$isbest,xlab = "usable",ylab = "% p0 predicted promoters in locus")
#mean(as.numeric(paste0(locus_stats_df[locus_stats_df$isbest == 0,"perc_p2"])))
#0. 0.3777979
#mean(as.numeric(paste0(locus_stats_df[locus_stats_df$isbest == 1,"perc_p2"])))
#0.415975
mean(as.numeric(paste0(locus_stats_df[locus_stats_df$isbest == 0,"perc_p1"])))
#0. 0.31
mean(as.numeric(paste0(locus_stats_df[locus_stats_df$isbest == 1,"perc_p1"])))
#0.32
#get all the stats you want in one table then set a random seed at get 25, make maybe 3 sets of 25 send to dulguun or something
#one version with 1 row per each locus
#one version with 1 row per peak with locus numbers
#also check the p1 p2 thing
#old and extra
'
if (length(top75_l100kb) >= (20 - q75_sampled_already) & length(bottom75_l100kb) >= 20 - (10-q75_sampled_already)){
top75_l100kb_sampled = sample(top75_l100kb,size = 20 - q75_sampled_already)
bottom75_l100kb_sampled = sample(bottom75_l100kb,size = 20 - (10-q75_sampled_already))
full_sample = unique(c(g100kb_sampled_peaks,top75_l100kb_sampled,bottom75_l100kb_sampled))
usable_loci[i,"peak_sample"] = paste0(full_sample,collapse = ";")
}
else{
usable_loci$peak_sample = "x"
}
}
dim(usable_loci[usable_loci$peak_sample != "x",]) #can do +/- 2 and see what that gets you
distances = rep_tab$dhs_nearest_tss_distance[[i]]
sampled_distances = distances[full_sample]
g100kb_sampled = which(sampled_distances > 100000)
'
#get_threshold_counts = function(sample_reps,dhs_threshold,kb100_threshold){
'sample_rep_df = data.frame(sample_reps)
sample_rep_df$q75counts = "x"
sample_rep_df$bottom75 = "x"
for (i in 1:nrow(sample_rep_df)){
quant_list = sample_rep_df[i,"dhs_quants"][[1]]
if (is.na( as.numeric(table(quant_list)["75_1"]))){
sample_rep_df[i,"q75counts"] = 0
}
else{
sample_rep_df[i,"q75counts"] = as.numeric(table(quant_list)["75_1"])
}
sample_rep_df[i,"bottom75"] = sum(table(quant_list)) - as.numeric(table(quant_list)["75_1"])
}
#okay start don even random sample, just divide all the loci up into the 4 metrics, and
'good_loci_tss = c()