-
Notifications
You must be signed in to change notification settings - Fork 1
/
practica-05.R
136 lines (103 loc) · 4.15 KB
/
practica-05.R
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
#' zona de notas
#'
# practica 05 -------------------------------------------------------------
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(broom)
library(qvalue)
library(purrr)
# sección para generar la base y guardarla --------------------------------
# fuente
# Robinson D. (2014).
# Modeling gene expression with broom: a case study in tidy analysis.
# [Variance explained blog](http://varianceexplained.org/r/tidy-genomics-broom/)
url <- "http://varianceexplained.org/files/Brauer2008_DataSet1.tds"
nutrient_names <- c(G = "Glucose", L = "Leucine", P = "Phosphate",
S = "Sulfate", N = "Ammonia", U = "Uracil")
cleaned_data <- read_delim(url, delim = "\t") %>%
separate(NAME, c("name", "BP", "MF", "systematic_name", "number"), sep = "\\|\\|") %>%
mutate_each(funs(trimws), name:systematic_name) %>%
select(-number, -GID, -YORF, -GWEIGHT) %>%
gather(sample, expression, G0.05:U0.3) %>%
separate(sample, c("nutrient", "rate"), sep = 1, convert = TRUE) %>%
mutate(nutrient = plyr::revalue(nutrient, nutrient_names)) %>%
filter(!is.na(expression), systematic_name != "")
write_rds(cleaned_data,"data/microarraydata.rds")
# importar datos ----------------------------------------------------------
cleaned_data <- read_rds("data-raw/microarraydata.rds")
plot_expression_data <- function(expression_data) {
ggplot(expression_data, aes(rate, expression, color = nutrient)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~name + systematic_name, scales = "free_y")
}
# visualization -----------------------------------------------------------
cleaned_data %>%
filter(BP == "leucine biosynthesis") %>%
plot_expression_data()
cleaned_data %>%
filter(BP == "cell wall organization and biogenesis") %>%
plot_expression_data()
# single regression -------------------------------------------------------
cleaned_data %>%
#elegimos 01 gen y 01 nutriente
filter(name == "LEU1", nutrient == "Leucine") %>%
#graficamos la relación Y: expresión ~ X: rate
ggplot(aes(rate, expression)) +
#empleamos la geometría punto
geom_point()
cleaned_data %>%
#elegimos 01 gen y 01 nutriente
filter(name == "LEU1", nutrient == "Leucine") %>%
#graficamos la relación Y: expresión ~ X: rate
ggplot(aes(rate, expression)) +
#empleamos la geometría punto
geom_point() + geom_smooth(method = "lm")
cleaned_data %>%
#elegimos 01 gen y 01 nutriente
filter(name == "LEU1", nutrient == "Leucine") %>%
#ajustamos una regresión lineal
#dado que data no es el primer argumento
#necesitamos especificarlo en data con "."
lm(expression ~ rate, data = .) %>%
#visualizamos tabla con estimados
tidy()
# to all combination of gene and nutrient ---------------------------------
cleaned_data %>% count(nutrient)
linear_models <- cleaned_data %>%
filter(nutrient=="Ammonia") %>% #filtramos por nutriente #<<
group_by(name, systematic_name, nutrient) %>% #agrupamos por gen
nest() %>% #anidamos los datos en una columna lista de df #<<
# ajustamos un modelo lineal a cada fila -ver paquete purrr::map-
mutate(model = map(data, ~ lm(expression ~ rate, data = .x)), #<<
tidym = map(model,tidy)) #<<
# conserva pendientes -----------------------------------------------------
slope_terms <- linear_models %>%
unnest(cols = c(tidym)) %>%
ungroup() %>%
filter(term=="rate" & !is.na(p.value))
slope_terms %>%
ggplot(aes(p.value)) +
geom_histogram(binwidth = .05) +
facet_wrap(~nutrient)
# corrige valoes p --------------------------------------------------------
slope_terms_adj <- slope_terms %>%
mutate(q.value = qvalue(p.value)$qvalues,
q.value_pass=if_else(q.value < .01,"TRUE","FALSE"))
slope_terms_adj %>%
ggplot(aes(p.value,fill=q.value_pass)) +
geom_histogram(binwidth = .05) +
facet_wrap(~nutrient)
# genera lista de valores p -----------------------------------------------
slope_terms_adj %>%
filter(q.value_pass=="TRUE") %>%
arrange(q.value)
# explora paquetes alternativos -------------------------------------------
library(qvalue)
#datos microarray
qmicro <- qvalue(p = slope_terms$p.value)
summary(qmicro)
hist(qmicro)
plot(qmicro)