-
Notifications
You must be signed in to change notification settings - Fork 0
/
tissuesExp_pipeline_v2.2.Rmd
1771 lines (1546 loc) · 76.9 KB
/
tissuesExp_pipeline_v2.2.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: "tissuesExp pipeline"
author: "Lissur Orsine"
date: "27/02/2021"
output: html_document
---
<!-- Be aware that overwrite = TRUE, which means that every time you run the pipeline, the tables are updated in the database -->
## 1. Chunks global options
```{r, setup, include = FALSE}
knitr::opts_chunk$set(include = TRUE, echo = TRUE, message = TRUE, warning = TRUE)
```
## 2. Libraries
```{r, libraries}
library(RMySQL)
library(e1071) # For kurtosis and skewness calculation
```
## 3. Pipeline variables
```{r, pipeline_variables}
# R
rnaseqExpDir = "/home/lissur/cellsExp_db/"
rnaseqExpName = "breast_cancer"
rnaseqExpCode = "E-MTAB-4801"
pipelineDir = "/home/lissur/cellsExp_pipeline/"
database = "cellsExp"
# bash
Sys.setenv(rnaseqExpDir = rnaseqExpDir, rnaseqExpName = rnaseqExpName, rnaseqExpCode = rnaseqExpCode)
```
## 4. Chunks variables
```{r, chunks_variables}
consideringOutliers = "yes"
fence = "inner"
# For chunk HERD
o_score_ts_score <- list(c(1,25),
c(2,25),
c(3,25),
c(4,25),
c(5,25),
c(1,50),
c(2,50),
c(3,50),
c(4,50),
c(5,50))
```
## 5. Download data
```{bash, download_data}
# tsv
echo -e "Downloading tsv for $rnaseqExpName...\n"
urltsv="https://www.ebi.ac.uk/gxa/experiments-content/$rnaseqExpCode/resources/ExperimentDownloadSupplier.RnaSeqBaseline/tpms.tsv"
outputFiletsv="${rnaseqExpDir}${rnaseqExpName}.tsv"
wget --no-check-certificate $urltsv -O $outputFiletsv
echo "Download completed for $rnaseqExpName!"
```
## 6. Download metadata
```{bash, download_metadata}
echo -e "Downloading metadata for $rnaseqExpName...\n"
urlmetadata="https://www.ebi.ac.uk/gxa/experiments-content/$rnaseqExpCode/resources/ExperimentDesignFile.Baseline/experiment-design"
outputFilemetadata="${rnaseqExpDir}${rnaseqExpName}_metadata.tsv"
wget --no-check-certificate $urlmetadata -O $outputFilemetadata
echo "Download completed for $rnaseqExpName!"
```
## 7. Create database
```{r, create_database}
# Login into mysql as root user and grant all privileges on the new database to your user before creating the database itself (e.g.: GRANT ALL PRIVILEGES ON cellsExp.* TO 'hep'@'localhost';)
# Definition for group clientother (i.e. user and password) in file /etc/my.cnf
# Create database
con <- dbConnect(MySQL(), group = "clientother")
dbGetQuery(con, paste0("create database if not exists ", database,";"))
dbDisconnect(con)
# Delete local variables
rm(con)
```
## 8. TPM
```{r, tpm}
# tsv
# Import data
tpm <- read.table(paste0(rnaseqExpDir, rnaseqExpName, ".tsv"),
header = TRUE,
sep = "\t",
quote = "")
# Adjust columns'names to SQL pattern (i.e. replace dot by underscore)
tissues <- colnames(tpm)
tissues <- gsub('\\.', '_', tissues)
colnames(tpm) <- tissues
# Put columns into alphabetical order and remove Gene Name column
tissues <- tissues[3:length(tissues)]
tpm <- tpm[,c("Gene_ID", sort(tissues))]
# Replace NA/NULL by ZERO
tpm[is.na(tpm)] <- 0
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con,
paste0(rnaseqExpName, "_tpm_gxa"),
tpm,
row.names = FALSE,
overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_tpm_gxa change Gene_ID ensembl varchar(15) primary key"))
# Copy tpm_gxa to tpm
dbGetQuery(con, paste0("DROP TABLE IF EXISTS `", rnaseqExpName, "_tpm`"))
dbGetQuery(con, paste0("CREATE TABLE ", rnaseqExpName, "_tpm LIKE ", rnaseqExpName, "_tpm_gxa"))
dbGetQuery(con, paste0("INSERT INTO ", rnaseqExpName, "_tpm SELECT * FROM ", rnaseqExpName, "_tpm_gxa"))
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tissues)
```
## 9. TPM max
```{r, tpm_max}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
dbDisconnect(con)
# Prepare output
tpm_max <- data.frame(ensembl = tpm$ensembl,
tpm_max = rep(0, nrow(tpm)),
tpm_max_tissue = rep(NA, nrow(tpm)),
stringsAsFactors = FALSE)
# Maximum TPM per gene
for(i in 1:nrow(tpm)){
tpm_gene <- tpm[i,2:ncol(tpm)]
tpm_max[i,"tpm_max"] <- max(tpm_gene)
tpm_max[i,"tpm_max_tissue"] <- names(sort(tpm_gene)[length(tpm_gene)])
}
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_tpmMax_per_gene"), tpm_max, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", tpm_max = "float", tpm_max_tissue = "varchar(34)"), overwrite = TRUE)
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_max, tpm_gene, i)
```
## 10. Metadata
```{r, metadata}
# tsv
# Import data
metadata <- read.table(paste0(rnaseqExpDir, rnaseqExpName, "_metadata.tsv"),
header = TRUE,
sep = "\t",
quote = "",
stringsAsFactors = FALSE)
# Adjust columns'names to SQL pattern (i.e. replace dot by underscore)
metadata_colnames <- colnames(metadata)
metadata_colnames <- gsub("[.]$", "", metadata_colnames)
metadata_colnames <- gsub("[.]", "_", metadata_colnames)
colnames(metadata) <- metadata_colnames
# Replace missing information by NA/NULL
missing_metadata <- which(metadata=="", arr.ind = TRUE)
for(i in 1:nrow(missing_metadata)){
missing_row <- missing_metadata[i,1]
missing_col <- missing_metadata[i,2]
metadata[missing_row,missing_col] <- NA
}
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(
con,
paste0(rnaseqExpName, "_metadata"),
metadata,
row.names = FALSE,
overwrite = TRUE
)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_metadata change Run Run varchar(", max(nchar(metadata[,1])),") primary key"))
dbDisconnect(con)
# Delete local variables
rm(metadata, metadata_colnames, i, missing_metadata, missing_row, missing_col, con)
```
## 11. Boxplot
```{r, boxplot}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
# Prepare outputs
boxplot_stats <- data.frame("ensembl" = tpm[,1],
"lower_whisker"= rep(0, nrow(tpm)),
"q1" = rep(0, nrow(tpm)),
"q2" = rep(0, nrow(tpm)),
"q3" = rep(0, nrow(tpm)),
"upper_whisker" = rep(0, nrow(tpm)))
outliers <- tpm_values
outliers[,] <- 0
# Boxplot
for(i in 1:nrow(tpm)){
if(fence == "inner"){
result <- boxplot.stats(tpm_values[i,])
}else if(fence == "outer"){
result <- boxplot.stats(tpm_values[i,], coef = 3.0)
}else{
stop("Please provide fence option: inner or outer.")
}
boxplot_stats[i,2:6] <- result$stats
}
# Flag outliers
outliers_low <- tpm_values < boxplot_stats[,2]
outliers_up <- tpm_values > boxplot_stats[,6]
outliers[which(outliers_low)] <- -1
outliers[which(outliers_up)] <- 1
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_boxplot_stats_", fence, "_fence"), boxplot_stats, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", lower_whisker = "float", q1 = "float", q2 = "float", q3 = "float", upper_whisker = "float"), overwrite = TRUE)
dbWriteTable(con, paste0(rnaseqExpName, "_outliers_", fence, "_fence"), as.data.frame(outliers), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_outliers_", fence, "_fence change row_names ensembl varchar(15) primary key"))
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, boxplot_stats, result, i, outliers_up, outliers_low, outliers)
```
## 12. Outliers summary
```{r, outliers_summary}
# Import tables
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
outIF <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_inner_fence"))
outOF <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_outer_fence"))
dbDisconnect(con)
# Tables parser
outIF_values <- as.matrix(outIF[,2:ncol(outIF)])
rownames(outIF_values) <- outIF[,1]
outOF_values <- as.matrix(outOF[,2:ncol(outOF)])
rownames(outOF_values) <- outOF[,1]
# Prepare output
outIFOF <- matrix(data = NA, nrow = nrow(outIF_values), ncol = ncol(outIF_values), byrow = FALSE, dimnames = list(rownames(outIF_values), colnames(outIF_values)))
# Combine tables information
# Ultra UP
outIFOF[outIF_values == 1 & outOF_values == 1] <- 2
# Ultra LOW
outIFOF[outIF_values == -1 & outOF_values == -1] <- -2
# Non-outliers
outIFOF[outIF_values == 0 & outOF_values == 0] <- 0
# Mild UP
outIFOF[outIF_values == 1 & outOF_values == 0] <- 1
# Mild LOW
outIFOF[outIF_values == -1 & outOF_values == 0] <- -1
# Write combined table into database
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_outliers_inner_outer_fences"), as.data.frame(outIFOF), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_outliers_inner_outer_fences change row_names ensembl varchar(15) primary key"))
dbDisconnect(con)
# Delete local variables
rm(con, outIF, outOF, outIF_values, outOF_values, outIFOF)
```
## 13. All non-outliers list
```{r, all_non_outliers_list, eval = FALSE, purl=FALSE}
# -- encode
# create table tmp_encode_nonOutliers
# select ensembl
# from encode_outliers_inner_outer_fences
# where
# adipose_tissue=0 and
# adrenal_gland=0 and
# brain=0 and
# heart=0 and
# kidney=0 and
# liver=0 and
# lung=0 and
# ovary=0 and
# pancreas=0 and
# sigmoid_colon=0 and
# small_intestine=0 and
# spleen=0 and
# testis=0;
#
# -- fantom
# create table tmp_fantom_nonOutliers
# select ensembl
# from fantom_outliers_inner_outer_fences
# where
# amygdala=0 and
# artery=0 and
# bone_marrow=0 and
# brain=0 and
# brain_meninx=0 and
# breast=0 and
# caudate_nucleus=0 and
# cerebellum=0 and
# colon=0 and
# diencephalon=0 and
# dorsal_thalamus=0 and
# dura_mater=0 and
# epididymis=0 and
# gall_bladder=0 and
# globus_pallidus=0 and
# heart=0 and
# heart_left_ventricle=0 and
# hippocampal_formation=0 and
# kidney=0 and
# left_cardiac_atrium=0 and
# locus_ceruleus=0 and
# lung=0 and
# lymph_node=0 and
# medulla_oblongata=0 and
# middle_frontal_gyrus=0 and
# middle_temporal_gyrus=0 and
# mitral_valve=0 and
# occipital_cortex=0 and
# occipital_lobe=0 and
# olfactory_apparatus=0 and
# ovary=0 and
# pancreas=0 and
# parietal_lobe=0 and
# parotid_gland=0 and
# penis=0 and
# pineal_body=0 and
# pituitary_gland=0 and
# placenta=0 and
# prostate_gland=0 and
# pulmonary_valve=0 and
# putamen=0 and
# seminal_vesicle=0 and
# smooth_muscle_tissue=0 and
# spinal_cord=0 and
# spleen=0 and
# submandibular_gland=0 and
# substantia_nigra=0 and
# testis=0 and
# tongue=0 and
# tricuspid_valve=0 and
# uterine_cervix=0 and
# uterus=0 and
# vagina=0 and
# vas_deferens=0 and
# vermiform_appendix=0 and
# zone_of_skin=0;
#
# -- gtex
# create table tmp_gtex_nonOutliers
# select ensembl
# from gtex_outliers_inner_outer_fences
# where
# adrenal_gland=0 and
# amygdala=0 and
# aorta=0 and
# atrium_auricular_region=0 and
# blood=0 and
# breast=0 and
# Brodmann_1909_area_24=0 and
# Brodmann_1909_area_9=0 and
# C1_segment_of_cervical_spinal_cord=0 and
# caudate_nucleus=0 and
# cerebellar_hemisphere=0 and
# cerebellum=0 and
# cerebral_cortex=0 and
# coronary_artery=0 and
# cortex_of_kidney=0 and
# EBV_transformed_lymphocyte=0 and
# ectocervix=0 and
# endocervix=0 and
# esophagogastric_junction=0 and
# esophagus_mucosa=0 and
# esophagus_muscularis_mucosa=0 and
# fallopian_tube=0 and
# greater_omentum=0 and
# heart_left_ventricle=0 and
# hippocampus_proper=0 and
# hypothalamus=0 and
# liver=0 and
# lower_leg_skin=0 and
# lung=0 and
# minor_salivary_gland=0 and
# nucleus_accumbens=0 and
# ovary=0 and
# pancreas=0 and
# pituitary_gland=0 and
# prostate_gland=0 and
# putamen=0 and
# sigmoid_colon=0 and
# skeletal_muscle_tissue=0 and
# small_intestine_Peyers_patch=0 and
# spleen=0 and
# stomach=0 and
# subcutaneous_adipose_tissue=0 and
# substantia_nigra=0 and
# suprapubic_skin=0 and
# testis=0 and
# thyroid_gland=0 and
# tibial_artery=0 and
# tibial_nerve=0 and
# transformed_skin_fibroblast=0 and
# transverse_colon=0 and
# urinary_bladder=0 and
# uterus=0 and
# vagina=0;
#
# -- hpa
# create table tmp_hpa_nonOutliers
# select ensembl
# from hpa_outliers_inner_outer_fences
# where
# adipose_tissue=0 and
# adrenal_gland=0 and
# bone_marrow=0 and
# cerebral_cortex=0 and
# colon=0 and
# duodenum=0 and
# endometrium=0 and
# esophagus=0 and
# fallopian_tube=0 and
# gall_bladder=0 and
# heart=0 and
# kidney=0 and
# liver=0 and
# lung=0 and
# lymph_node=0 and
# ovary=0 and
# pancreas=0 and
# placenta=0 and
# prostate_gland=0 and
# rectum=0 and
# saliva_secreting_gland=0 and
# skeletal_muscle_tissue=0 and
# small_intestine=0 and
# smooth_muscle_tissue=0 and
# spleen=0 and
# stomach=0 and
# testis=0 and
# thyroid_gland=0 and
# tonsil=0 and
# urinary_bladder=0 and
# vermiform_appendix=0 and
# zone_of_skin=0;
#
# -- ibm
# create table tmp_ibm_nonOutliers
# select ensembl
# from ibm_outliers_inner_outer_fences
# where
# adipose_tissue=0 and
# adrenal_gland=0 and
# brain=0 and
# breast=0 and
# colon=0 and
# heart=0 and
# kidney=0 and
# leukocyte=0 and
# liver=0 and
# lung=0 and
# lymph_node=0 and
# ovary=0 and
# prostate_gland=0 and
# skeletal_muscle_tissue=0 and
# testis=0 and
# thyroid_gland=0;
#
# -- all_non_outliers table
# create table all_non_outliers
# select g.ensembl
# from
# (select e.ensembl
# from
# (select c.ensembl
# from
# (select a.ensembl
# from
# (select ensembl from tmp_encode_nonOutliers) a
# join
# (select ensembl from tmp_fantom_nonOutliers) b
# on a.ensembl=b.ensembl) c
# join
# (select ensembl from tmp_gtex_nonOutliers) d
# on c.ensembl=d.ensembl) e
# join
# (select ensembl from tmp_hpa_nonOutliers) f
# on e.ensembl=f.ensembl) g
# join
# (select ensembl from tmp_ibm_nonOutliers) h
# on g.ensembl=h.ensembl;
#
# drop table tmp_encode_nonOutliers, tmp_fantom_nonOutliers, tmp_gtex_nonOutliers, tmp_hpa_nonOutliers, tmp_ibm_nonOutliers;
```
## 14. Outliers count (per gene)
```{r, outliers_count}
# In this script, we are dealing only with upper inner outliers; that means, outliers table (outliers_values object) contains only upper inner outliers (to check, use "apply(outliers_values, 2, unique)")
# Import outliers and outliers_IFOF tables from database
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
outliers <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_inner_fence"))
outliers_IFOF <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_inner_outer_fences"))
dbDisconnect(con)
# Parse outliers and outliers_IFOF tables to matrix
outliers_values <- as.matrix(outliers[,2:ncol(outliers)])
rownames(outliers_values) <- outliers[,1]
outliers_IFOF_values <- as.matrix(outliers_IFOF[,2:ncol(outliers_IFOF)])
rownames(outliers_IFOF_values) <- outliers_IFOF[,1]
# Remove lower inner outliers (i.e. replace by zero)
outliers_values[which(outliers_values==-1)] <- 0
outliers_IFOF_values[which(outliers_IFOF_values==-1)] <- 0
outliers_IFOF_values[which(outliers_IFOF_values==-2)] <- 0
# Prepare output
outliersCount <- data.frame("ensembl" = outliers[,"ensembl"],
"OutIF" = rep(0, nrow(outliers)),
"OutOF" = rep(0, nrow(outliers)),
"OutTotal" = rep(0, nrow(outliers)),
"tissues_OutIF" = rep(NA, nrow(outliers)),
"tissues_OutOF" = rep(NA, nrow(outliers)),
"tissues_OutTotal" = rep(NA, nrow(outliers)))
# Count total upper inner outliers per gene
for(i in 1:nrow(outliers_IFOF_values)){
outliersCount[i,"OutTotal"] <- length(which(outliers_values[i,]==1))
outliersCount[i,"OutIF"] <- length(which(outliers_IFOF_values[i,]==1))
outliersCount[i,"OutOF"] <- length(which(outliers_IFOF_values[i,]==2))
if(outliersCount[i,"OutTotal"]!=0){
outliersCount[i,"tissues_OutTotal"] <- paste(colnames(outliers_values)[which(outliers_values[i,]==1)], collapse = ", ")
}else{
outliersCount[i,"tissues_OutTotal"] <- "None"
}
if(outliersCount[i,"OutIF"]!=0){
outliersCount[i,"tissues_OutIF"] <- paste(colnames(outliers_IFOF_values)[which(outliers_IFOF_values[i,]==1)], collapse = ", ")
}else{
outliersCount[i,"tissues_OutIF"] <- "None"
}
if(outliersCount[i,"OutOF"]!=0){
outliersCount[i,"tissues_OutOF"] <- paste(colnames(outliers_IFOF_values)[which(outliers_IFOF_values[i,]==2)], collapse = ", ")
}else{
outliersCount[i,"tissues_OutOF"] <- "None"
}
}
# Export table outliersCount
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_outliers_count_per_gene"), outliersCount,
row.names = FALSE,
field.types = c(ensembl = "varchar(15) primary key",
OutIF = "tinyint unsigned",
OutOF = "tinyint unsigned",
OutTotal = "tinyint unsigned",
tissues_OutIF = "varchar(1000)",
tissues_OutOF = "varchar(1000)",
tissues_OutTotal = "varchar(1000)"),
overwrite = TRUE)
dbDisconnect(con)
# Delete local variables
rm(con, outliers, outliers_values, outliers_IFOF, outliers_IFOF_values, outliersCount, i)
```
## 15. Average and Standard deviation
```{r, avg_std}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
if(consideringOutliers == "no"){
outliers <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_", fence, "_fence"), row.names = "ensembl")
}
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
if(consideringOutliers == "no"){
outliers <- as.matrix(outliers)
tpm_values[which(outliers != 0)] <- NA
}
# Average
average <- function(x){
mean(x, na.rm = TRUE)
}
avg <- apply(tpm_values, 1, average)
avg <- as.data.frame(avg)
# Standard deviation
standard_deviation <- function(x){
sd(x, na.rm = TRUE)
}
std <- apply(tpm_values, 1, standard_deviation)
std <- as.data.frame(std)
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
if(consideringOutliers == "yes"){
dbWriteTable(con, paste0(rnaseqExpName, "_avg"), avg, row.names = TRUE, field.types = c(row_names = "varchar(15)", avg = "float"), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_avg change row_names ensembl varchar(15) primary key"))
dbWriteTable(con, paste0(rnaseqExpName, "_std"), std, row.names = TRUE, field.types = c(row_names = "varchar(15)", std = "float"), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_std change row_names ensembl varchar(15) primary key"))
}
if(consideringOutliers == "no"){
dbWriteTable(con, paste0(rnaseqExpName, "_avg_without_outliers_", fence, "_fence"), avg, row.names = TRUE, field.types = c(row_names = "varchar(15)", avg = "float"), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_avg_without_outliers_", fence, "_fence change row_names ensembl varchar(15) primary key"))
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_avg_without_outliers_", fence, "_fence change avg avg_without_outliers float"))
dbWriteTable(con, paste0(rnaseqExpName, "_std_without_outliers_", fence, "_fence"), std, row.names = TRUE, field.types = c(row_names = "varchar(15)", std = "float"), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_std_without_outliers_", fence, "_fence change row_names ensembl varchar(15) primary key"))
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_std_without_outliers_", fence, "_fence change std std_without_outliers float"))
}
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, outliers, average, avg, standard_deviation, std)
```
## 16. Normality test
```{r, normality_test}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
if(consideringOutliers == "no"){
outliers <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_", fence, "_fence"), row.names = "ensembl")
}
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
if(consideringOutliers == "no"){
outliers <- as.matrix(outliers)
tpm_values[which(outliers != 0)] <- NA
}
# Prepare output
normality_test <- data.frame("ensembl" = tpm[,1],
"sw_stat" = rep(0, nrow(tpm)),
"sw_pvalue" = rep(0, nrow(tpm)),
"support" = rep(0, nrow(tpm)),
"sw_result" = rep(0, nrow(tpm)))
# Normality test
# Conditions for the Shapiro-Wilk test of normality:
# (Look at the shapiro.test function source code to check the conditions)
# The number of non-missing values must be between 3 and 5000 (3 <= n <= 5000; only the first limit is checked in this script as the second limit is unlikely to occur)
# The non-missing values must not be identical (rng != 0)
for(i in 1:nrow(tpm)){
x <- tpm_values[i,]
x <- sort(x[complete.cases(x)])
n <- length(x)
rng <- x[n] - x[1L]
if(n >= 3L && rng != 0){
result <- shapiro.test(x)
normality_test[i,2] <- result$statistic
normality_test[i,3] <- result$p.value
normality_test[i,4] <- n
if(result$p.value>0.05){normality_test[i,5] <- 1}
}else{
normality_test[i,2:5] <- NA
}
}
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
if(consideringOutliers == "yes"){
dbWriteTable(con, paste0(rnaseqExpName, "_normality_test"), normality_test, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", sw_stat = "float", sw_pvalue = "float", support = "int", sw_result = "tinyint"), overwrite = TRUE)
}
if(consideringOutliers == "no"){
dbWriteTable(con, paste0(rnaseqExpName, "_normality_test_without_outliers_", fence, "_fence"), normality_test, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", sw_stat = "float", sw_pvalue = "float", support = "int", sw_result = "tinyint"), overwrite = TRUE)
}
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, outliers, normality_test, i, x, n, rng, result)
```
## 17. Uniformity test
```{r, uniformity_test}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
if(consideringOutliers == "no"){
outliers <- dbReadTable(con, paste0(rnaseqExpName, "_outliers_", fence, "_fence"), row.names = "ensembl")
}
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
if(consideringOutliers == "no"){
outliers <- as.matrix(outliers)
tpm_values[which(outliers != 0)] <- NA
}
# Prepare output
uniformity_test <- data.frame("ensembl" = tpm[,1],
"ks_stat"= rep(0, nrow(tpm)),
"ks_pvalue" = rep(0, nrow(tpm)),
"support" = rep(0, nrow(tpm)),
"ks_result" = rep(0, nrow(tpm)))
# Uniformity test
# Conditions for the Kolmogorov-Smirnov test of uniformity:
# (See "Details" in the ks.test help documentation)
# When ties (i.e. repeated values) are present, both statistic and p.value are NA
for(i in 1:nrow(tpm)){
expression <- tpm_values[i,]
n <- length(which(!is.na(expression)))
out <- which(is.na(expression))
if(consideringOutliers == "no" && length(out) != 0){
expression <- expression[-out]
}
result <- ks.test(expression, "punif", min(expression), max(expression))
if(!is.na(result$p.value)){
uniformity_test[i,2] <- result$statistic
uniformity_test[i,3] <- result$p.value
uniformity_test[i,4] <- n
if(result$p.value>0.05){uniformity_test[i,5] <- 1}
}else{
uniformity_test[i,2:5] <- NA
}
}
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
if(consideringOutliers == "yes"){
dbWriteTable(con, paste0(rnaseqExpName, "_uniformity_test"), uniformity_test, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", ks_stat = "float", ks_pvalue = "float", support = "int", ks_result = "tinyint"), overwrite = TRUE)
}
if(consideringOutliers == "no"){
dbWriteTable(con, paste0(rnaseqExpName, "_uniformity_test_without_outliers_", fence, "_fence"), uniformity_test, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", ks_stat = "float", ks_pvalue = "float", support = "int", ks_result = "tinyint"), overwrite = TRUE)
}
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, outliers, uniformity_test, i, n, expression, out, result)
```
## 18. "Zeros" counting
```{r, zeros_counting}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
# Prepare output
zeros_counting <- data.frame("ensembl" = tpm[,1],
"zeros_counting"= rep(0, nrow(tpm)),
"zeros_counting_result" = rep(0, nrow(tpm)))
# "Zeros" counting
for(i in 1:nrow(tpm)){
zeros_counting[i,2] <- length(which(tpm_values[i,]==0))
if(NCOL(tpm_values) %% 2 != 0 & zeros_counting[i,2]>=round(0.75*NCOL(tpm_values))){zeros_counting[i,3] <- 1} # Odd number of tissues in the database
if(NCOL(tpm_values) %% 2 == 0 & zeros_counting[i,2]>=(round(0.75*NCOL(tpm_values))+1)){zeros_counting[i,3] <- 1} # Even number of tissues in the database
}
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_zeros_counting"), zeros_counting, row.names = FALSE, field.types = c(ensembl = "varchar(15) primary key", zeros_counting = "int", zeros_counting_result = "tinyint"), overwrite = TRUE)
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, zeros_counting, i)
```
## 19. Tests summary
```{r, tests_summary}
# This script is considering only inner fence
# sw_stat, sw_pvalue and support to be considered are those in the normality_test_summary table (and not those in normality_test or normality_test_without_outliers_inner_fence tables)
# sw_stat, sw_pvalue and support came from the first application of the normality test (data from normality_test table) only for pG; for all the other, they came from the second application of the normality test (data from normality_test_without_outliers_inner_fence table)
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
normality_test <- dbReadTable(con, paste0(rnaseqExpName, "_normality_test"))
normality_test_without_outliers_inner_fence <- dbReadTable(con, paste0(rnaseqExpName, "_normality_test_without_outliers_inner_fence"))
uniformity_test <- dbReadTable(con, paste0(rnaseqExpName, "_uniformity_test"))
uniformity_test_without_outliers_inner_fence <- dbReadTable(con, paste0(rnaseqExpName, "_uniformity_test_without_outliers_inner_fence"))
zeros_counting <- dbReadTable(con, paste0(rnaseqExpName, "_zeros_counting"))
dbDisconnect(con)
# normality_test and normality_test_without_outliers_inner_fence
normality_test_summary <- normality_test
normality_test_summary[which(normality_test$sw_result==1),] <- normality_test[which(normality_test$sw_result==1),] # Não é necessário (tabela normality_test_summary é igual à tabela normality_test neste ponto), mas melhor manter por segurança
normality_test_summary[which(normality_test$sw_result==1),"sw_result"] <- "pG"
normality_test_summary[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==1),] <- normality_test_without_outliers_inner_fence[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==1),]
normality_test_summary[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==1),"sw_result"] <- "cG"
normality_test_summary[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==0),] <- normality_test_without_outliers_inner_fence[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==0),]
normality_test_summary[which(normality_test$sw_result==0 & normality_test_without_outliers_inner_fence$sw_result==0),"sw_result"] <- "nonG"
normality_test_summary[which(normality_test$sw_result==0 & is.na(normality_test_without_outliers_inner_fence$sw_result)),] <- normality_test_without_outliers_inner_fence[which(normality_test$sw_result==0 & is.na(normality_test_without_outliers_inner_fence$sw_result)),]
normality_test_summary[which(normality_test$sw_result==0 & is.na(normality_test_without_outliers_inner_fence$sw_result)),"sw_result"] <- NA # Não é necessário (linha inteira é NULL), mas melhor manter por segurança
# uniformity_test and uniformity_test_without_outliers_inner_fence
uniformity_test_summary <- uniformity_test
uniformity_test_summary[which(uniformity_test$ks_result==1),] <- uniformity_test[which(uniformity_test$ks_result==1),] # Não é necessário (tabela uniformity_test_summary é igual à tabela uniformity_test neste ponto), mas melhor manter por segurança
uniformity_test_summary[which(uniformity_test$ks_result==1),"ks_result"] <- "pU"
uniformity_test_summary[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==1),] <- uniformity_test_without_outliers_inner_fence[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==1),]
uniformity_test_summary[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==1),"ks_result"] <- "cU"
uniformity_test_summary[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==0),] <- uniformity_test_without_outliers_inner_fence[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==0),]
uniformity_test_summary[which(uniformity_test$ks_result==0 & uniformity_test_without_outliers_inner_fence$ks_result==0),"ks_result"] <- "nonU"
# uniformity_test_summary[which(uniformity_test$ks_result==0 & is.na(uniformity_test_without_outliers_inner_fence$ks_result)),] <- uniformity_test_without_outliers_inner_fence[which(uniformity_test$ks_result==0 & is.na(uniformity_test_without_outliers_inner_fence$ks_result)),]
# uniformity_test_summary[which(uniformity_test$ks_result==0 & is.na(uniformity_test_without_outliers_inner_fence$ks_result)),"ks_result"] <- NA # Não é necessário (linha inteira é NULL), mas melhor manter por segurança
# zeros_counting
zeros_counting_summary <- zeros_counting
zeros_counting_summary[which(zeros_counting_summary$zeros_counting_result==1),"zeros_counting_result"] <- "cZ"
zeros_counting_summary[which(zeros_counting_summary$zeros_counting_result==0),"zeros_counting_result"] <- "nonZ"
# Tests summary table
stats_tests <- merge(normality_test_summary, uniformity_test_summary, by = "ensembl", all = TRUE)
tests_summary <- merge(stats_tests, zeros_counting_summary, by = "ensembl", all = TRUE)
tests_summary <- tests_summary[,c("ensembl", "sw_result", "ks_result", "zeros_counting_result")]
tests_summary[,"tests_result"] <- paste(tests_summary$sw_result, tests_summary$ks_result, tests_summary$zeros_counting_result, sep = "_")
# Export tables
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_normality_test_summary"), normality_test_summary,
row.names = FALSE,
field.types = c(ensembl = "varchar(15) primary key", sw_stat = "float", sw_pvalue = "float", support = "int", sw_result = "varchar(5)"),
overwrite = TRUE)
dbWriteTable(con, paste0(rnaseqExpName, "_uniformity_test_summary"), uniformity_test_summary,
row.names = FALSE,
field.types = c(ensembl = "varchar(15) primary key", ks_stat = "float", ks_pvalue = "float", support = "int", ks_result = "varchar(5)"),
overwrite = TRUE)
dbWriteTable(con, paste0(rnaseqExpName, "_zeros_counting_summary"), zeros_counting_summary,
row.names = FALSE,
field.types = c(ensembl = "varchar(15) primary key", zeros_counting = "int", zeros_counting_result = "varchar(5)"),
overwrite = TRUE)
dbWriteTable(con, paste0(rnaseqExpName, "_tests_summary"), tests_summary,
row.names = FALSE,
field.types = c(ensembl = "varchar(15) primary key", sw_result = "varchar(5)", ks_result = "varchar(5)", zeros_counting_result = "varchar(5)", tests_result = "varchar(18)"),
overwrite = TRUE)
dbDisconnect(con)
# Delete local variables
rm(con, normality_test, normality_test_without_outliers_inner_fence, uniformity_test, uniformity_test_without_outliers_inner_fence, zeros_counting, normality_test_summary, uniformity_test_summary, zeros_counting_summary, stats_tests, tests_summary)
```
## 20. o-score
```{r, oScore}
# Rounding to 4 decimal places
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
tpm <- dbReadTable(con, paste0(rnaseqExpName, "_tpm"))
boxplot_stats <- dbReadTable(con, paste0(rnaseqExpName, "_boxplot_stats_inner_fence"))
dbDisconnect(con)
# Data parser
tpm_values <- as.matrix(tpm[,2:ncol(tpm)])
rownames(tpm_values) <- tpm[,1]
# Prepare outputs
q3_values <- tpm_values
iqr_values <- tpm_values
oScore_values <- tpm_values
# Calculate o-score
for (i in 1:nrow(boxplot_stats)) {
q1 <- boxplot_stats$q1[i]
q3 <- boxplot_stats$q3[i]
iqr <- q3 - q1
q3_values[i,] <- q3
iqr_values[i,] <- iqr
}
oScore_values <- round((tpm_values - q3_values) / (1.5 * iqr_values), digits = 4)
# Flag cases for whose iqr = 0 (or q1 = q3)
oScore_values[which(rowSums(iqr_values)==0),] <- NA
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(con, paste0(rnaseqExpName, "_oScore"), as.data.frame(oScore_values), overwrite = TRUE)
dbGetQuery(con, paste0("alter table ", rnaseqExpName, "_oScore change row_names ensembl varchar(15) primary key"))
dbDisconnect(con)
# Delete local variables
rm(con, tpm, tpm_values, boxplot_stats, i, q1, q3, iqr, q3_values, iqr_values, oScore_values)
```
## 21. o-score max
```{r, oScore_max}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
oScore <- dbReadTable(con, paste0(rnaseqExpName, "_oScore"))
dbDisconnect(con)
# Prepare output
oScore_max <- data.frame(ensembl = oScore$ensembl,
oScore_max = rep(0, nrow(oScore)),
stringsAsFactors = FALSE)
# Calculate o-score max
oScore_max$oScore_max <- apply(oScore[,2:ncol(oScore)], 1, max)
# Export data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
dbWriteTable(
con,
paste0(rnaseqExpName, "_oScoreMax_per_gene"),
oScore_max,
field.types = c(ensembl = "varchar(15) primary key", oScore_max = "float"),
row.names = FALSE,
overwrite = TRUE
)
dbDisconnect(con)
# Delete local variables
rm(con, oScore, oScore_max)
```
## 22. Fences
```{r, fences}
# Import data
con <- dbConnect(MySQL(), group = "clientother", dbname = database)
boxplot_stats <- dbGetQuery(con, paste0("select ensembl,q1,q3 from ", rnaseqExpName, "_boxplot_stats_inner_fence;"))
dbDisconnect(con)
# Prepare outputs
boxplot_upper_fences <- matrix(data = 0, nrow = nrow(boxplot_stats), ncol = 103)
rownames(boxplot_upper_fences) <- boxplot_stats$ensembl
colnames(boxplot_upper_fences) <- c("q1", "q3", "iqr", "fence_1", "fence_2", "fence_3", "fence_4", "fence_5", "fence_6", "fence_7", "fence_8", "fence_9", "fence_10", "fence_11", "fence_12", "fence_13", "fence_14", "fence_15", "fence_16", "fence_17", "fence_18", "fence_19", "fence_20", "fence_21", "fence_22", "fence_23", "fence_24", "fence_25", "fence_26", "fence_27", "fence_28", "fence_29", "fence_30", "fence_31", "fence_32", "fence_33", "fence_34", "fence_35", "fence_36", "fence_37", "fence_38", "fence_39", "fence_40", "fence_41", "fence_42", "fence_43", "fence_44", "fence_45", "fence_46", "fence_47", "fence_48", "fence_49", "fence_50", "fence_51", "fence_52", "fence_53", "fence_54", "fence_55", "fence_56", "fence_57", "fence_58", "fence_59", "fence_60", "fence_61", "fence_62", "fence_63", "fence_64", "fence_65", "fence_66", "fence_67", "fence_68", "fence_69", "fence_70", "fence_71", "fence_72", "fence_73", "fence_74", "fence_75", "fence_76", "fence_77", "fence_78", "fence_79", "fence_80", "fence_81", "fence_82", "fence_83", "fence_84", "fence_85", "fence_86", "fence_87", "fence_88", "fence_89", "fence_90", "fence_91", "fence_92", "fence_93", "fence_94", "fence_95", "fence_96", "fence_97", "fence_98", "fence_99", "fence_100")
boxplot_lower_fences <- matrix(data = 0, nrow = nrow(boxplot_stats), ncol = 103)
rownames(boxplot_lower_fences) <- boxplot_stats$ensembl
colnames(boxplot_lower_fences) <- c("q1", "q3", "iqr", "fence_1", "fence_2", "fence_3", "fence_4", "fence_5", "fence_6", "fence_7", "fence_8", "fence_9", "fence_10", "fence_11", "fence_12", "fence_13", "fence_14", "fence_15", "fence_16", "fence_17", "fence_18", "fence_19", "fence_20", "fence_21", "fence_22", "fence_23", "fence_24", "fence_25", "fence_26", "fence_27", "fence_28", "fence_29", "fence_30", "fence_31", "fence_32", "fence_33", "fence_34", "fence_35", "fence_36", "fence_37", "fence_38", "fence_39", "fence_40", "fence_41", "fence_42", "fence_43", "fence_44", "fence_45", "fence_46", "fence_47", "fence_48", "fence_49", "fence_50", "fence_51", "fence_52", "fence_53", "fence_54", "fence_55", "fence_56", "fence_57", "fence_58", "fence_59", "fence_60", "fence_61", "fence_62", "fence_63", "fence_64", "fence_65", "fence_66", "fence_67", "fence_68", "fence_69", "fence_70", "fence_71", "fence_72", "fence_73", "fence_74", "fence_75", "fence_76", "fence_77", "fence_78", "fence_79", "fence_80", "fence_81", "fence_82", "fence_83", "fence_84", "fence_85", "fence_86", "fence_87", "fence_88", "fence_89", "fence_90", "fence_91", "fence_92", "fence_93", "fence_94", "fence_95", "fence_96", "fence_97", "fence_98", "fence_99", "fence_100")
# Calculate fences
for (i in 1:nrow(boxplot_stats)) {
q1 <- boxplot_stats$q1[i]
q3 <- boxplot_stats$q3[i]
iqr <- (q3 - q1)
boxplot_upper_fences[i, "q1"] <- q1
boxplot_upper_fences[i, "q3"] <- q3
boxplot_upper_fences[i, "iqr"] <- iqr
boxplot_upper_fences[i, "fence_1"] <- q3 + (1.5*iqr)
boxplot_upper_fences[i, "fence_2"] <- q3 + (3*iqr)
boxplot_upper_fences[i, "fence_3"] <- q3 + (4.5*iqr)
boxplot_upper_fences[i, "fence_4"] <- q3 + (6*iqr)
boxplot_upper_fences[i, "fence_5"] <- q3 + (7.5*iqr)
boxplot_upper_fences[i, "fence_6"] <- q3 + (9*iqr)
boxplot_upper_fences[i, "fence_7"] <- q3 + (10.5*iqr)
boxplot_upper_fences[i, "fence_8"] <- q3 + (12*iqr)
boxplot_upper_fences[i, "fence_9"] <- q3 + (13.5*iqr)
boxplot_upper_fences[i, "fence_10"] <- q3 + (15*iqr)