-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.rmd
1528 lines (1326 loc) · 59.9 KB
/
README.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
---
title: "L. major UTR Analysis"
output:
knitrBootstrap::bootstrap_document:
theme: flatly
clean_supporting: false
---
```{r knitr_settings, include=FALSE}
library(knitr)
opts_chunk$set(fig.width=1280/96,
fig.height=720/96,
dpi=96)
```
L. major UTR Length and Alternative Trans-splicing / Poly-adenylation Analysis
==============================================================================
Overview
--------
The goal of this analysis is to parse the output from our [UTR analysis
pipeline](https://github.com/khughitt/utr_analysis), perform some basic
smoothing of the spliced leader and polya acceptor site counts and determine
the most likely primary UTR boundaries for each gene where information is
available. 5'- and 3'-UTR boundaries, along with some other useful information
such as UTR GC- and CT-richness will be outputted in a format that is
convenient for downstream analysis.
Finally, we will look for evidence of either alternative trans-splicing or
poly-adenylation and attempt to visualize the prevelance and magnitude of
these events across the different developmental stages.
TODO
----
- Include Trey's uORFs
- Removal RNAs (SLRNA, etc)
```{r run_date, results='asis', echo=FALSE}
email = "<a href='mailto:[email protected]'>Keith Hughitt</a>"
last_update = format(Sys.time(), "(<time>%Y-%m-%d</time>)")
cat(paste(email, last_update))
# Clean up any existing variables
rm(list=ls())
```
Settings
--------
```{r settings}
# Number of individual UTR plots to display for each developmental stage and
# UTR side. The purpose of these plots are to provide a sense of the data
# distribution and to visualize the effect of primary # site selection with and
# without smoothing.
max_plots = 10
```
[view source](README.rmd)
Methods
-------
### Load data
```{r load_annotations, warning=FALSE}
library(GenomicRanges)
library(Biostrings)
library(rtracklayer)
library(ggplot2)
library(ggvis)
library(reshape2)
library(dplyr)
# Genome sequence and annotations from TriTrypDB (8.0)
gff = import.gff(file.path('/cbcb/lab/nelsayed/ref_data/lmajor_friedlin',
'/annotation/TriTrypDB-8.0_LmajorFriedlin.gff'),
version='3')
chromosomes = gff[gff$type == 'chromosome']
genes = gff[gff$type == 'gene']
# Load unannotated ORFs detected from ribosome profiling data
uorfs = import.gff(gzfile('input/lmajor_orfs_manual_2014-09-08_clean.gff.gz'), version='3')
uorfs$description = 'Unannotated ORF'
# Drop GFF columns not shared betwene TriTrypDB GFF and uORF GFF
keep_cols = intersect(colnames(mcols(genes)), colnames(mcols(uorfs)))
genes = genes[,keep_cols]
uorfs = uorfs[,keep_cols]
genes = append(genes, uorfs)
input_fasta = file.path(Sys.getenv("REF"),
"lmajor_friedlin/genome/TriTrypDB-8.0_LmajorFriedlin_Genome.fasta")
fasta = readDNAStringSet(input_fasta)
# Fix names (L. major chromosome identifiers)
names(fasta) = substring(names(fasta), 0, 7)
# Number of bases flanking CDS to scan for motifs when actual UTR length is not
# known; numbers are based on median 5' and 3' UTR lengths for L. major
default_5utr_width = 250
default_3utr_width = 575
# Taken from L. major UTR analysis output from 2014/11/14
procyclic_polya = import.gff(gzfile('input/lmajor_procyclic_ncrnas_removed_polya.gff.gz'), version='3')
metacyclic_polya = import.gff(gzfile('input/lmajor_metacyclic_ncrnas_removed_polya.gff.gz'), version='3')
amastigote_polya = import.gff(gzfile('input/lmajor_amastigote_ncrnas_removed_polya.gff.gz'), version='3')
procyclic_sl = import.gff(gzfile('input/lmajor_procyclic_ncrnas_removed_sl.gff.gz'), version='3')
metacyclic_sl = import.gff(gzfile('input/lmajor_metacyclic_ncrnas_removed_sl.gff.gz'), version='3')
amastigote_sl = import.gff(gzfile('input/lmajor_amastigote_ncrnas_removed_sl.gff.gz'), version='3')
# Filter noncoding RNAs
id_filter_string = 'rRNA|snRNA|snoRNA|SLRNA|TRNA|SRP'
noncoding_ids = genes$ID[grepl(id_filter_string, genes$ID)]
genes = genes[!genes$ID %in% noncoding_ids]
procyclic_polya = procyclic_polya[!procyclic_polya$Name %in% noncoding_ids,]
metacyclic_polya = metacyclic_polya[!metacyclic_polya$Name %in% noncoding_ids,]
amastigote_polya = amastigote_polya[!amastigote_polya$Name %in% noncoding_ids,]
procyclic_sl = procyclic_sl[!procyclic_sl$Name %in% noncoding_ids,]
metacyclic_sl = metacyclic_sl[!metacyclic_sl$Name %in% noncoding_ids,]
amastigote_sl = amastigote_sl[!amastigote_sl$Name %in% noncoding_ids,]
# Create output and build directories if needed
for (x in c('build', 'output')) {
if (!file.exists(x)) {
dir.create(x)
}
}
```
```{r helper_functions}
#
# find_peak
#
find_peak = function(gene, acceptor_sites, gene_strand, feature_type,
smoothed=FALSE, include_plot=FALSE, secondary=FALSE) {
# Determine orientation of feature relative to CDS
if ((feature_type == 'sl' && gene_strand == '+') ||
(feature_type == 'polya' && gene_strand == '-')) {
feature_side = 'left'
} else {
feature_side = 'right'
}
# Create a vector from the furthest SL site to the CDS boundary
if (feature_side == 'left') {
cds_boundary = start(gene)
raw_scores = rep(0, cds_boundary - min(start(acceptor_sites)))
rel_start = cds_boundary - start(acceptor_sites) + 1
} else {
cds_boundary = end(gene)
raw_scores = rep(0, max(start(acceptor_sites)) - cds_boundary)
rel_start = start(acceptor_sites) - cds_boundary + 1
}
# add scores at indices where acceptor sites were detected
raw_scores[rel_start] = score(acceptor_sites)
x = 1:length(raw_scores)
# create smooted version of scores (optional)
if (smoothed) {
input_scores = ksmooth(x, raw_scores, kernel="normal", bandwidth=6,
n.points=length(x))$y
} else {
input_scores = raw_scores
}
# find acceptor site peak
if (secondary && (length(acceptor_sites) >= 2)) {
# secondary site
j = 2
} else {
# primary site
j = 1
}
sorted_scores = sort(input_scores, decreasing=TRUE)
raw_idx = which(input_scores == sorted_scores[j])
if (length(raw_idx) > 1) {
if (feature_side == 'left') {
raw_idx = head(raw_idx, 1)
} else {
raw_idx = tail(raw_idx, 1)
}
}
# find raw score (number of reads) for the desired primary or secondary
# site peak
raw_score = raw_scores[raw_idx]
# If smoothing was used, the actual location of the smoothed peak may not
# have any support. In this case we will find the nearest site to the peak
# with a non-zero score in the raw scores vector
while(raw_score == 0) {
# find next highest peak
j = j + 1
raw_idx = which(input_scores == sorted_scores[j])
# if more than one peak, choose the furthest one from CDS
if (length(raw_idx) > 1) {
if (feature_side == 'left') {
raw_idx = head(raw_idx, 1)
} else {
raw_idx = tail(raw_idx, 1)
}
}
raw_score = raw_scores[raw_idx]
}
# plot raw and smoothed peaks
if (include_plot) {
if (smoothed) {
df = melt(data.frame(x, raw=raw_scores, smoothed=input_scores),
id=c("x"), variable.name='type')
print(qplot(x, value, data=df, color=type, geom='line')
+ ggtitle(gene$ID))
} else {
df = melt(data.frame(x, raw=raw_scores), id=c("x"),
variable.name='type')
print(qplot(x, value, data=df, geom='line')
+ ggtitle(gene$ID))
}
}
# find index of the site with the highest score (or second highest score,
# in the case of secondary site selection)
idx = which(score(acceptor_sites) == raw_score)
# if there is a tie, use the furthest site from CDS
if (length(idx) > 1) {
if (feature_side == 'left') {
idx = head(idx, 1)
} else {
idx = tail(idx, 1)
}
}
return(idx)
}
#
# find_primary_site
#
# Determines the primary site among a list of acceptor sites and their
# associated score (number of reads mapped).
#
# Returns a list containing the site and score for the primary site.
#
find_primary_site = function(acceptor_sites, feature, gene, gene_strand, smoothed=FALSE) {
if (length(acceptor_sites) == 0) {
return(list(location=NA, num_reads=NA))
} else if (length(acceptor_sites) == 1) {
# if only one site found, use it
return(list(location=start(acceptor_sites)[1],
num_reads=score(acceptor_sites)[1]))
} else {
# two or more sites
# find highest smoothed peak which has non-zero coverage in the raw
# data as well
max_index = find_peak(gene, acceptor_sites, gene_strand, feature,
smoothed=smoothed)
max_index_smoothed = find_peak(gene, acceptor_sites, gene_strand,
feature, smoothed=TRUE)
# if the smoothing would result in a different primary UTR choice,
# plot the raw and smoothed versions of the data
if (abs(max_index - max_index_smoothed) > 15) {
num_diff = num_diff + 1
if (plot_num <= max_plots) {
sprintf("PLOTTING %d/%d", plot_num, max_plots)
#tmp = find_peak(gene, acceptor_sites, gene_strand, feature,
# smoothed=TRUE, include_plot=TRUE)
plot_num = plot_num + 1
}
}
return(list(location=start(acceptor_sites)[max_index],
num_reads=score(acceptor_sites)[max_index]))
}
}
#
# find_secondary_site
#
# Determines the secondary site among a list of acceptor sites and their
# associated score (number of reads mapped).
#
# Returns a list containing the site and score for the secondary site.
#
find_secondary_site = function(acceptor_sites, feature, gene, gene_strand,
smoothed=FALSE) {
if (length(acceptor_sites) == 0) {
return(list(location=NA, num_reads=NA))
} else if (length(acceptor_sites) == 1) {
# if only one site found, use it
return(list(location=start(acceptor_sites)[1],
num_reads=score(acceptor_sites)[1]))
} else {
# two or more sites
# find highest smoothed peak which has non-zero coverage in the raw
# data as well
secondary_site_index = find_peak(gene, acceptor_sites, gene_strand,
feature, smoothed=smoothed,
secondary=TRUE)
secondary_site_index_smoothed = find_peak(gene, acceptor_sites,
gene_strand, feature,
smoothed=TRUE,
secondary=TRUE)
# if the smoothing would result in a different secondary UTR choice,
# plot the raw and smoothed versions of the data
#if (abs(secondary_site_index - secondary_site_index_smoothed) > 15) {
# num_diff = num_diff + 1
# if (plot_num <= max_plots) {
# tmp = find_peak(gene, acceptor_sites, gene_strand, feature,
# smoothed=TRUE, include_plot=TRUE,
# secondary=TRUE)
# plot_num = plot_num + 1
# }
#}
return(list(location=start(acceptor_sites)[secondary_site_index],
num_reads=score(acceptor_sites)[secondary_site_index]))
}
}
#
# get_utr_sequences
#
# Returns a vector of Biostrings instances containing the UTR sequence for each
# input gene.
#
get_utr_sequences = function(genes, fasta, utr_lengths, default_width,
utr5=TRUE) {
# retrieve utr length if known
widths = data.frame(
id=genes$ID,
width=NA)
widths$width = utr_lengths[match(genes$ID, utr_lengths$name),]$length
widths$width[is.na(widths$width)] = default_width
# get positive and negative strand genes
if (utr5) {
utr = flank(genes, widths$width)
} else {
utr = flank(genes, widths$width, start=FALSE)
}
pos_strand = utr[as.character(strand(utr)) == "+"]
neg_strand = utr[as.character(strand(utr)) == "-"]
# for genes that were assigned the default UTR size, make sure that the
# assigned boundaries fall within the chromosome
start(pos_strand) = pmax(1, start(pos_strand))
start(neg_strand) = pmax(1, start(neg_strand))
end(pos_strand) = pmin(end(pos_strand), width(fasta[seqnames(pos_strand)]))
end(neg_strand) = pmin(end(neg_strand), width(fasta[seqnames(neg_strand)]))
seqs = fasta[pos_strand]
seqs = append(seqs, reverseComplement(fasta[neg_strand]))
names(seqs) = c(pos_strand$ID, neg_strand$ID)
return(seqs)
}
#
# get_num_reads
#
# Returns the number of reads mapped to a given position for the specified
# stage, or 0 if none are found
#
get_num_reads = function(utr_reads, site) {
if (is.na(site)) {
return(NA)
}
num_reads = score(utr_reads[start(utr_reads) == site])
return(ifelse(length(num_reads) == 0, 0, num_reads))
}
```
### Compute UTR coordinates and features
#### 5'UTR
##### 5'UTR Length
```{r compute_5utr_lengths, message=FALSE, cache=TRUE, autodep=TRUE}
# Output columns
procyclic_lengths = c()
procyclic_num_reads = c()
procyclic_num_reads_primary = c()
procyclic_num_reads_secondary = c()
metacyclic_lengths = c()
metacyclic_num_reads = c()
metacyclic_num_reads_primary = c()
metacyclic_num_reads_secondary = c()
amastigote_lengths = c()
amastigote_num_reads = c()
amastigote_num_reads_primary = c()
amastigote_num_reads_secondary = c()
combined_lengths = c()
combined_num_reads = c()
combined_num_reads_primary = c()
combined_num_reads_secondary = c()
# Vectors to keep track of stage-specific primary site coverage across
# different stages (e.g. amastigote primary site reads in procyclic samples)
# This will be useful later on when looking for evidence of alternative trans-
# splicing and polyadenylation.
#
# For example, "pro_meta_primary_sl_reads" will contain the number of
# metacyclic reads mapped to the metacyclic primary site.
#
# Note 2015/01/15 -- for now, we will use the simpler "primary to secondary"
# ratios *within* each condition to give a sense of the degree of "dominance"
# for a given primary site.
#
pro_meta_primary_sl_reads = c()
pro_amast_primary_sl_reads = c()
meta_pro_primary_sl_reads = c()
meta_amast_primary_sl_reads = c()
amast_pro_primary_sl_reads = c()
amast_meta_primary_sl_reads = c()
# Start GFF output for combined set of acceptor sites
gff_lines = c("##gff-version\t3",
"##feature-ontology\tsofa.obo",
"##attribute-ontology\tgff3_attributes.obo")
# Add chromosome entries
for (i in 1:length(chromosomes)) {
ch = chromosomes[i]
gff_lines = append(gff_lines, paste("##sequence-region", ch$Name, 1,
ch$size, sep='\t'))
}
# GFF chromosome entries
#for (i in 1:length(chromosomes)) {
# ch = chromosomes[i]
# # ID=LmjF.01;Name=LmjF.01;description=LmjF.01;size=268988;web_id=LmjF.01;
# # molecule_type=dsDNA;organism_name=Leishmania
# # major;translation_table=1;topology=linear;localization=nuclear;
# # Dbxref=ApiDB:LmjF.01,taxon:347515
# descr_template = paste0(
# "ID=%s;Name=%s;description=%s;size=%s;web_id=%s;",
# "molecule_type=%s;organism_name=%s;translation_table=%s",
# "topology=%s;localization=%s;Dbxref=%s")
# descr = sprintf(descr_template,
# ch$ID, ch$Name, ch$description, ch$size, ch$web_id,
# ch$molecule_type, ch$organism_name, ch$translation_table,
# ch$topology, ch$localization,
# paste(ch$Dbxref[[1]][1], ch$Dbxref[[1]][2], sep=','))
# gff_lines = append(gff_lines, paste(ch$Name, "TriTrypDB", "chromosome", 1,
# ch$size, ".", "+", ".", descr,
# sep='\t'))
#}
# keep track of the number of plots created
num_diff = 0
plot_num = 0
# Add gene entries
i = 1
for (gene_id in genes$ID) {
message(sprintf("Processing SL sites for gene %d/%d", i, length(genes)))
gene = genes[genes$ID == gene_id]
gene_strand = as.character(strand(gene))
# get all of the SL acceptor sites as a GRanges object
metacyclic_utr5 = metacyclic_sl[metacyclic_sl$Name == gene_id]
procyclic_utr5 = procyclic_sl[procyclic_sl$Name == gene_id]
amastigote_utr5 = amastigote_sl[amastigote_sl$Name == gene_id]
combined_utr5 = metacyclic_sl[metacyclic_sl$Name == gene_id]
# total number of reads found which contain an acceptor site
metacyclic_num_reads = append(metacyclic_num_reads,
sum(metacyclic_utr5$score))
procyclic_num_reads = append(procyclic_num_reads,
sum(procyclic_utr5$score))
amastigote_num_reads = append(amastigote_num_reads,
sum(amastigote_utr5$score))
combined_num_reads = append(combined_num_reads,
sum(procyclic_utr5$score) +
sum(metacyclic_utr5$score) +
sum(amastigote_utr5$score))
# Combined output
# Add procyclic reads
if (length(procyclic_utr5) > 0) {
for (j in 1:length(procyclic_utr5)) {
# if new site, add a new entry
entry = procyclic_utr5[j]
if(!start(entry) %in% start(combined_utr5)) {
combined_utr5 = c(combined_utr5, entry)
}
# otherwise add procyclic score to existing metacyclic score
else {
score(combined_utr5[start(combined_utr5) == start(entry)]) = (
score(combined_utr5[start(combined_utr5) == start(entry)]) +
score(entry))
}
}
}
# Add amastigote reads
if (length(amastigote_utr5) > 0) {
for (j in 1:length(amastigote_utr5)) {
# if new site, add a new entry
entry = amastigote_utr5[j]
if(!start(entry) %in% start(combined_utr5)) {
combined_utr5 = c(combined_utr5, entry)
}
# otherwise add amastigote score to existing metacyclic score
else {
score(combined_utr5[start(combined_utr5) == start(entry)]) = (
score(combined_utr5[start(combined_utr5) == start(entry)]) +
score(entry))
}
}
}
i = i + 1
# Determine length and scores for procyclic, metacyclic, amastigote,
# and combined outputs
pro_primary_site = find_primary_site(procyclic_utr5, 'sl', gene, gene_strand)
meta_primary_site = find_primary_site(metacyclic_utr5, 'sl', gene, gene_strand)
amast_primary_site = find_primary_site(amastigote_utr5, 'sl', gene, gene_strand)
combined_primary_site = find_primary_site(combined_utr5, 'sl', gene, gene_strand)
# secondary site
pro_secondary_site = find_secondary_site(procyclic_utr5, 'sl', gene, gene_strand)
meta_secondary_site = find_secondary_site(metacyclic_utr5, 'sl', gene, gene_strand)
amast_secondary_site = find_secondary_site(amastigote_utr5, 'sl', gene, gene_strand)
combined_secondary_site = find_secondary_site(combined_utr5, 'sl', gene, gene_strand)
# compute 5'utr length and coordinates
if (gene_strand == '+') {
# procylic + strand
procyclic_utr5_length = start(gene) - pro_primary_site$location
# metacyclic + strand
metacyclic_utr5_length = start(gene) - meta_primary_site$location
# amastigote + strand
amastigote_utr5_length = start(gene) - amast_primary_site$location
# combined + strand
combined_utr5_start = combined_primary_site$location + 1
combined_utr5_end = start(gene) - 1
combined_utr5_length = start(gene) - combined_primary_site$location
} else {
# procyclic - strand
procyclic_utr5_length = pro_primary_site$location - end(gene)
# metacyclic - strand
metacyclic_utr5_length = meta_primary_site$location - end(gene)
# procyclic - strand
amastigote_utr5_length = amast_primary_site$location - end(gene)
# combined - strand
combined_utr5_start = end(gene) + 1
combined_utr5_end = combined_primary_site$location - 1
combined_utr5_length = combined_primary_site$location - end(gene)
}
# Add primary site read count and UTR length
procyclic_lengths = append(procyclic_lengths, procyclic_utr5_length)
metacyclic_lengths = append(metacyclic_lengths, metacyclic_utr5_length)
amastigote_lengths = append(amastigote_lengths, amastigote_utr5_length)
combined_lengths = append(combined_lengths, combined_utr5_length)
procyclic_num_reads_primary = append(procyclic_num_reads_primary,
pro_primary_site$num_reads)
metacyclic_num_reads_primary = append(metacyclic_num_reads_primary,
meta_primary_site$num_reads)
amastigote_num_reads_primary = append(amastigote_num_reads_primary,
amast_primary_site$num_reads)
combined_num_reads_primary = append(combined_num_reads_primary,
combined_primary_site$num_reads)
procyclic_num_reads_secondary = append(procyclic_num_reads_secondary,
pro_secondary_site$num_reads)
metacyclic_num_reads_secondary = append(metacyclic_num_reads_secondary,
meta_secondary_site$num_reads)
amastigote_num_reads_secondary = append(amastigote_num_reads_secondary,
amast_secondary_site$num_reads)
combined_num_reads_secondary = append(combined_num_reads_secondary,
combined_secondary_site$num_reads)
# Update counts for cross-stage primary sites
pro_meta_primary_sl_reads = append(pro_meta_primary_sl_reads,
get_num_reads(procyclic_utr5, meta_primary_site$location))
pro_amast_primary_sl_reads = append(pro_amast_primary_sl_reads,
get_num_reads(procyclic_utr5, amast_primary_site$location))
meta_pro_primary_sl_reads = append(meta_pro_primary_sl_reads,
get_num_reads(metacyclic_utr5, pro_primary_site$location))
meta_amast_primary_sl_reads = append(meta_amast_primary_sl_reads,
get_num_reads(metacyclic_utr5, amast_primary_site$location))
amast_pro_primary_sl_reads = append(amast_pro_primary_sl_reads,
get_num_reads(amastigote_utr5, pro_primary_site$location))
amast_meta_primary_sl_reads = append(amast_meta_primary_sl_reads,
get_num_reads(amastigote_utr5, meta_primary_site$location))
# Add GFF entry
descr = sprintf("ID=%s_5utr;Name=%s;description=%s", gene$ID, gene$ID,
gene$description)
gff_entry = paste(
seqnames(gene),
"El-Sayed",
"five_prime_UTR",
combined_utr5_start,
combined_utr5_end,
combined_primary_site$num_reads,
strand(gene),
'.',
descr, sep='\t')
gff_lines = append(gff_lines, gff_entry)
}
# metacyclic
metacyclic_utr5_df = data.frame(
name=genes$ID,
length=metacyclic_lengths,
num_reads=metacyclic_num_reads,
num_reads_primary=metacyclic_num_reads_primary,
num_reads_secondary=metacyclic_num_reads_secondary
)
# procyclic
procyclic_utr5_df = data.frame(
name=genes$ID,
length=procyclic_lengths,
num_reads=procyclic_num_reads,
num_reads_primary=procyclic_num_reads_primary,
num_reads_secondary=procyclic_num_reads_secondary
)
# amastigote
amastigote_utr5_df = data.frame(
name=genes$ID,
length=amastigote_lengths,
num_reads=amastigote_num_reads,
num_reads_primary=amastigote_num_reads_primary,
num_reads_secondary=amastigote_num_reads_secondary
)
# combined
combined_utr5_df = data.frame(
name=genes$ID,
length=combined_lengths,
num_reads=combined_num_reads,
num_reads_primary=combined_num_reads_primary,
num_reads_secondary=combined_num_reads_secondary
)
# Create cross-stage site usage dataframes
# For example, "pro_other_sl_sites" lists the number of procyclic reads
# which mapped to the primary sites detected for other stages (amast and meta).
pro_other_sl_sites = tbl_df(data.frame(
gene=genes$ID,
metacyclic=pro_meta_primary_sl_reads,
amastigote=pro_amast_primary_sl_reads
))
meta_other_sl_sites = tbl_df(data.frame(
gene=genes$ID,
procyclic=meta_pro_primary_sl_reads,
amastigote=meta_amast_primary_sl_reads
))
amast_other_sl_sites = tbl_df(data.frame(
gene=genes$ID,
procyclic=amast_pro_primary_sl_reads,
metacyclic=amast_meta_primary_sl_reads
))
```
##### 5'UTR Composition
```{r utr5_composition}
# Metacyclic
metacyclic_utr5_sequences = get_utr_sequences(genes, fasta, metacyclic_utr5_df,
default_5utr_width, utr5=TRUE)
freqs = alphabetFrequency(metacyclic_utr5_sequences)[,1:4]
metacyclic_utr5_features_df = data.frame(
name=names(metacyclic_utr5_sequences),
gc=(freqs[,'G'] + freqs[,'C']) / rowSums(freqs),
ct=(freqs[,'C'] + freqs[,'T']) / rowSums(freqs)
)
metacyclic_utr5_df = merge(metacyclic_utr5_df, metacyclic_utr5_features_df,
by='name')
# Procyclic
procyclic_utr5_sequences = get_utr_sequences(genes, fasta, procyclic_utr5_df,
default_5utr_width, utr5=TRUE)
freqs = alphabetFrequency(procyclic_utr5_sequences)[,1:4]
procyclic_utr5_features_df = data.frame(
name=names(procyclic_utr5_sequences),
gc=(freqs[,'G'] + freqs[,'C']) / rowSums(freqs),
ct=(freqs[,'C'] + freqs[,'T']) / rowSums(freqs)
)
procyclic_utr5_df = merge(procyclic_utr5_df, procyclic_utr5_features_df,
by='name')
# amastigote
amastigote_utr5_sequences = get_utr_sequences(genes, fasta, amastigote_utr5_df,
default_5utr_width, utr5=TRUE)
freqs = alphabetFrequency(amastigote_utr5_sequences)[,1:4]
amastigote_utr5_features_df = data.frame(
name=names(amastigote_utr5_sequences),
gc=(freqs[,'G'] + freqs[,'C']) / rowSums(freqs),
ct=(freqs[,'C'] + freqs[,'T']) / rowSums(freqs)
)
amastigote_utr5_df = merge(amastigote_utr5_df, amastigote_utr5_features_df,
by='name')
# Combined
combined_utr5_sequences = get_utr_sequences(genes, fasta, combined_utr5_df,
default_5utr_width, utr5=TRUE)
freqs = alphabetFrequency(combined_utr5_sequences)[,1:4]
combined_utr5_features_df = data.frame(
name=names(combined_utr5_sequences),
gc=(freqs[,'G'] + freqs[,'C']) / rowSums(freqs),
ct=(freqs[,'C'] + freqs[,'T']) / rowSums(freqs)
)
combined_utr5_df = merge(combined_utr5_df, combined_utr5_features_df,
by='name')
```
```{r utr5_output}
# Write results
write.csv(metacyclic_utr5_df, file='output/lmajor_metacyclic_5utr_lengths.csv',
quote=FALSE, row.names=FALSE)
write.csv(procyclic_utr5_df, file='output/lmajor_procyclic_5utr_lengths.csv',
quote=FALSE, row.names=FALSE)
write.csv(amastigote_utr5_df, file='output/lmajor_amastigote_5utr_lengths.csv',
quote=FALSE, row.names=FALSE)
write.csv(combined_utr5_df, file='output/lmajor_combined_5utr_lengths.csv',
quote=FALSE, row.names=FALSE)
# Write result to GFF
fp = file("output/lmajor_5utr.gff")
writeLines(gff_lines, fp)
close(fp)
```
#### 3'UTR
```{r compute_3utr_lengths, message=FALSE, cache=TRUE, autodep=TRUE}
# Output columns
procyclic_lengths = c()
procyclic_num_reads = c()
procyclic_num_reads_primary = c()
procyclic_num_reads_secondary = c()
metacyclic_lengths = c()
metacyclic_num_reads = c()
metacyclic_num_reads_primary = c()
metacyclic_num_reads_secondary = c()
amastigote_lengths = c()
amastigote_num_reads = c()
amastigote_num_reads_primary = c()
amastigote_num_reads_secondary = c()
combined_lengths = c()
combined_num_reads = c()
combined_num_reads_primary = c()
combined_num_reads_secondary = c()
# Vectors to keep track of stage-specific primary site coverage across
# different stages (e.g. amastigote primary site reads in procyclic samples)
pro_meta_primary_polya_reads = c()
pro_amast_primary_polya_reads = c()
meta_pro_primary_polya_reads = c()
meta_amast_primary_polya_reads = c()
amast_pro_primary_polya_reads = c()
amast_meta_primary_polya_reads = c()
# Start GFF output for combined set of acceptor sites
gff_lines = c("##gff-version\t3",
"##feature-ontology\tsofa.obo",
"##attribute-ontology\tgff3_attributes.obo")
# Add chromosome entries
for (i in 1:length(chromosomes)) {
ch = chromosomes[i]
gff_lines = append(gff_lines, paste("##sequence-region", ch$Name, 1,
ch$size, sep='\t'))
}
# GFF chromosome entries
#for (i in 1:length(chromosomes)) {
# ch = chromosomes[i]
# # ID=LmjF.01;Name=LmjF.01;description=LmjF.01;size=268988;web_id=LmjF.01;
# # molecule_type=dsDNA;organism_name=Leishmania
# # major;translation_table=1;topology=linear;localization=nuclear;
# # Dbxref=ApiDB:LmjF.01,taxon:347515
# descr_template = paste0(
# "ID=%s;Name=%s;description=%s;size=%s;web_id=%s;",
# "molecule_type=%s;organism_name=%s;translation_table=%s",
# "topology=%s;localization=%s;Dbxref=%s")
# descr = sprintf(descr_template,
# ch$ID, ch$Name, ch$description, ch$size, ch$web_id,
# ch$molecule_type, ch$organism_name, ch$translation_table,
# ch$topology, ch$localization,
# paste(ch$Dbxref[[1]][1], ch$Dbxref[[1]][2], sep=','))
# gff_lines = append(gff_lines, paste(ch$Name, "TriTrypDB", "chromosome", 1,
# ch$size, ".", "+", ".", descr,
# sep='\t'))
#}
# keep track of the number of plots created
num_diff = 0
plot_num = 0
# Add gene entries
i = 1
for (gene_id in genes$ID) {
message(sprintf("Processing Poly(A) sites for gene %d/%d", i, length(genes)))
gene = genes[genes$ID == gene_id]
gene_strand = as.character(strand(gene))
# get all of the Poly(A) acceptor sites as a GRanges object
metacyclic_utr3 = metacyclic_polya[metacyclic_polya$Name == gene_id]
procyclic_utr3 = procyclic_polya[procyclic_polya$Name == gene_id]
amastigote_utr3 = amastigote_polya[amastigote_polya$Name == gene_id]
combined_utr3 = metacyclic_polya[metacyclic_polya$Name == gene_id]
# total number of reads found which contain an acceptor site
metacyclic_num_reads = append(metacyclic_num_reads,
sum(metacyclic_utr3$score))
procyclic_num_reads = append(procyclic_num_reads,
sum(procyclic_utr3$score))
amastigote_num_reads = append(amastigote_num_reads,
sum(amastigote_utr3$score))
combined_num_reads = append(combined_num_reads,
sum(procyclic_utr3$score) +
sum(metacyclic_utr3$score) +
sum(amastigote_utr3$score))
# Combined output
if (length(procyclic_utr3) > 0) {
for (j in 1:length(procyclic_utr3)) {
# if new site, add a new entry
entry = procyclic_utr3[j]
if(!start(entry) %in% start(combined_utr3)) {
combined_utr3 = c(combined_utr3, entry)
}
# otherwise add procyclic score to existing metacyclic score
else {
score(combined_utr3[start(combined_utr3) == start(entry)]) = (
score(combined_utr3[start(combined_utr3) == start(entry)]) +
score(entry))
}
}
}
if (length(amastigote_utr3) > 0) {
for (j in 1:length(amastigote_utr3)) {
# if new site, add a new entry
entry = amastigote_utr3[j]
if(!start(entry) %in% start(combined_utr3)) {
combined_utr3 = c(combined_utr3, entry)
}
# otherwise add amastigote score to existing metacyclic score
else {
score(combined_utr3[start(combined_utr3) == start(entry)]) = (
score(combined_utr3[start(combined_utr3) == start(entry)]) +
score(entry))
}
}
}
i = i + 1
# Determine length and scores for procyclic, metacyclic, and combined
# outputs
pro_primary_site = find_primary_site(procyclic_utr3, 'polya', gene, gene_strand)
meta_primary_site = find_primary_site(metacyclic_utr3, 'polya', gene, gene_strand)
amast_primary_site = find_primary_site(amastigote_utr3, 'polya', gene, gene_strand)
combined_primary_site = find_primary_site(combined_utr3, 'polya', gene, gene_strand)
# secondary sites
pro_secondary_site = find_secondary_site(procyclic_utr3, 'polya', gene, gene_strand)
meta_secondary_site = find_secondary_site(metacyclic_utr3, 'polya', gene, gene_strand)
amast_secondary_site = find_secondary_site(amastigote_utr3, 'polya', gene, gene_strand)
combined_secondary_site = find_secondary_site(combined_utr3, 'polya', gene, gene_strand)
# compute 3'utr length and coordinates
if (gene_strand == '+') {
# procylic + strand
procyclic_utr3_length = pro_primary_site$location - end(gene)
# metacyclic + strand
metacyclic_utr3_length = meta_primary_site$location - end(gene)
# amastigote + strand
amastigote_utr3_length = amast_primary_site$location - end(gene)
# combined + strand
combined_utr3_start = end(gene) + 1
combined_utr3_end = combined_primary_site$location - 1
combined_utr3_length = combined_primary_site$location - end(gene)
} else {
# procyclic - strand
procyclic_utr3_length = start(gene) - pro_primary_site$location
# metacyclic - strand
metacyclic_utr3_length = start(gene) - meta_primary_site$location
# amastigote - strand
amastigote_utr3_length = start(gene) - amast_primary_site$location
# combined - strand
combined_utr3_start = combined_primary_site$location + 1
combined_utr3_end = start(gene) - 1
combined_utr3_length = start(gene) - combined_primary_site$location
}
# Add primary site read count and UTR length
procyclic_lengths = append(procyclic_lengths, procyclic_utr3_length)
metacyclic_lengths = append(metacyclic_lengths, metacyclic_utr3_length)
amastigote_lengths = append(amastigote_lengths, amastigote_utr3_length)
combined_lengths = append(combined_lengths, combined_utr3_length)
procyclic_num_reads_primary = append(procyclic_num_reads_primary,
pro_primary_site$num_reads)
metacyclic_num_reads_primary = append(metacyclic_num_reads_primary,
meta_primary_site$num_reads)
amastigote_num_reads_primary = append(amastigote_num_reads_primary,
amast_primary_site$num_reads)
combined_num_reads_primary = append(combined_num_reads_primary,
combined_primary_site$num_reads)
procyclic_num_reads_secondary = append(procyclic_num_reads_secondary,
pro_secondary_site$num_reads)
metacyclic_num_reads_secondary = append(metacyclic_num_reads_secondary,
meta_secondary_site$num_reads)
amastigote_num_reads_secondary = append(amastigote_num_reads_secondary,
amast_secondary_site$num_reads)
combined_num_reads_secondary = append(combined_num_reads_secondary,
combined_secondary_site$num_reads)
# Update counts for cross-stage primary sites
pro_meta_primary_polya_reads = append(pro_meta_primary_polya_reads,
get_num_reads(procyclic_utr5, meta_primary_site$location))
pro_amast_primary_polya_reads = append(pro_amast_primary_polya_reads,
get_num_reads(procyclic_utr5, amast_primary_site$location))
meta_pro_primary_polya_reads = append(meta_pro_primary_polya_reads,
get_num_reads(metacyclic_utr5, pro_primary_site$location))
meta_amast_primary_polya_reads = append(meta_amast_primary_polya_reads,
get_num_reads(metacyclic_utr5, amast_primary_site$location))
amast_pro_primary_polya_reads = append(amast_pro_primary_polya_reads,
get_num_reads(amastigote_utr5, pro_primary_site$location))
amast_meta_primary_polya_reads = append(amast_meta_primary_polya_reads,
get_num_reads(amastigote_utr5, meta_primary_site$location))
# Add GFF entry
descr = sprintf("ID=%s_3utr;Name=%s;description=%s", gene$ID, gene$ID,
gene$description)
gff_entry = paste(
seqnames(gene),
"El-Sayed",
"three_prime_UTR",
combined_utr3_start,
combined_utr3_end,
combined_primary_site$num_reads,
strand(gene),
'.',
descr, sep='\t')
gff_lines = append(gff_lines, gff_entry)
}
# metacyclic
metacyclic_utr3_df = data.frame(
name=genes$ID,
length=metacyclic_lengths,
num_reads=metacyclic_num_reads,
num_reads_primary=metacyclic_num_reads_primary,
num_reads_secondary=metacyclic_num_reads_secondary