softwareDevelopment commited on
Commit
489dc5d
·
verified ·
1 Parent(s): 783aa2c

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +91 -170
app.R CHANGED
@@ -5,174 +5,146 @@ library(dplyr)
5
  library(ggplot2)
6
  library(DT)
7
 
8
- # ---------------- UI ---------------- #
9
  ui <- fluidPage(
10
 
11
- tags$head(tags$style(HTML("
12
- body { background:#f4f6f9; font-family:Arial; }
13
- .box { background:white; padding:15px; border-radius:10px; }
14
- "))),
15
-
16
- titlePanel("🌾 Advanced QTL Mapping Pipeline"),
17
 
18
  sidebarLayout(
19
- sidebarPanel(class="box",
20
 
21
- fileInput("geno","Upload Genotype CSV"),
22
- fileInput("pheno","Upload Phenotype CSV"),
23
 
24
- selectInput("trait","Select Trait", choices=NULL),
25
 
26
  selectInput("model","Model",
27
- c("EM","HK","IMP","CIM","STEPWISE")),
28
-
29
- numericInput("perm","Permutation",100),
30
 
31
- actionButton("run","Run Analysis"),
32
  br(), br(),
33
- downloadButton("download","Download Results")
34
  ),
35
 
36
  mainPanel(
37
  tabsetPanel(
38
-
39
- tabPanel("LOD Plot", plotOutput("lod")),
40
- tabPanel("Manhattan Plot", plotOutput("manhattan")),
41
- tabPanel("QTL Map", plotOutput("qtlMap", height="600px")),
42
- tabPanel("Peaks", DTOutput("peaks"))
43
  )
44
  )
45
  )
46
  )
47
 
48
- # ---------------- SERVER ---------------- #
49
  server <- function(input, output, session) {
50
 
51
- # -------- Load Genotype -------- #
52
- geno_clean <- reactive({
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  req(input$geno)
54
 
55
- g <- read_csv(input$geno$datapath)
56
 
57
- # Remove position row if exists
58
- if(any(!g[1,-1] %in% c(0,1,2,-1))) {
59
  g <- g[-1,]
60
  }
61
 
62
- # Replace -1 with NA
63
- g[,-1] <- lapply(g[,-1], function(x){
64
- x[x == -1] <- NA
65
- as.numeric(x)
66
- })
67
-
68
  g
69
  })
70
 
71
- # -------- Load Phenotype -------- #
72
- pheno <- reactive({
73
- req(input$pheno)
74
- read_csv(input$pheno$datapath)
75
- })
76
 
77
- # Update trait dropdown
78
- observe({
79
- df <- pheno()
80
- updateSelectInput(session,"trait",
81
- choices=colnames(df)[-1])
82
- })
83
 
84
- # -------- Create Cross -------- #
85
- cross_obj <- reactive({
86
 
87
- g <- geno_clean()
88
- p <- pheno()
89
 
90
- data <- inner_join(p, g, by="id")
 
91
 
92
- write.csv(data, "temp.csv", row.names=FALSE)
 
 
 
 
 
93
 
94
- cross <- read.cross(
95
- format="csv",
96
- file="temp.csv",
97
- genotypes=c("0","1","2"),
98
- na.strings="NA"
99
- )
100
 
101
- cross <- calc.genoprob(cross, step=1)
102
- cross <- fill.geno(cross, method="argmax")
103
 
104
- cross
 
 
 
105
  })
106
 
107
- # -------- Segregation Filter -------- #
108
- seg_filter <- function(cross){
109
 
110
- geno <- pull.geno(cross)
111
 
112
- keep <- apply(geno, 1, function(x){
113
- tab <- table(x)
114
- if(length(tab) < 2) return(FALSE)
115
- chisq.test(tab)$p.value > 0.05
116
- })
117
 
118
- cross[keep,]
119
- }
 
 
 
 
 
120
 
121
- # -------- QTL Analysis -------- #
122
- result <- eventReactive(input$run, {
 
 
123
 
124
- cross <- cross_obj()
125
- cross <- seg_filter(cross)
126
-
127
- method <- switch(input$model,
128
- "EM"="em",
129
- "HK"="hk",
130
- "IMP"="imp",
131
- "CIM"="hk",
132
- "STEPWISE"="hk")
133
-
134
- if(input$model == "CIM"){
135
- res <- cim(cross, pheno.col=input$trait)
136
- } else if(input$model == "STEPWISE"){
137
- res <- stepwiseqtl(cross, pheno.col=input$trait)
138
- } else {
139
- res <- scanone(cross, pheno.col=input$trait, method=method)
140
- }
141
 
142
- perm <- scanone(cross, pheno.col=input$trait,
143
- method=method, n.perm=input$perm)
144
 
145
- list(res=res, perm=perm)
 
 
 
146
  })
147
 
148
- # -------- LOD Plot -------- #
149
  output$lod <- renderPlot({
150
 
151
- res <- result()$res
152
- perm <- result()$perm
153
-
154
- if(!("scanone" %in% class(res))) return(NULL)
155
 
156
- thr <- summary(perm, alpha=0.05)
157
-
158
- df <- as.data.frame(res)
159
 
160
  ggplot(df, aes(pos, lod, color=factor(chr))) +
161
- geom_line(size=1) +
162
- geom_hline(yintercept=thr,
163
- linetype="dashed", color="red") +
164
- labs(title="LOD Plot",
165
- x="Position", y="LOD") +
166
- theme_minimal()
167
  })
168
 
169
- # -------- Manhattan Plot -------- #
170
  output$manhattan <- renderPlot({
171
 
172
- res <- result()$res
173
- if(!("scanone" %in% class(res))) return(NULL)
174
 
175
- df <- as.data.frame(res)
176
  df$chr <- as.factor(df$chr)
177
 
178
  df <- df %>%
@@ -182,76 +154,25 @@ server <- function(input, output, session) {
182
  mutate(tot=cumsum(chr_len)-chr_len) %>%
183
  mutate(BPcum=pos+tot)
184
 
185
- axis_set <- df %>%
186
  group_by(chr) %>%
187
  summarise(center=mean(BPcum))
188
 
189
  ggplot(df, aes(BPcum, lod, color=chr)) +
190
- geom_point(size=2) +
191
- scale_x_continuous(label=axis_set$chr,
192
- breaks=axis_set$center) +
193
- labs(title="Manhattan Plot",
194
- x="Chromosome", y="LOD") +
195
- theme_minimal() +
196
- theme(legend.position="none")
197
- })
198
-
199
- # -------- QTL Chromosome Map -------- #
200
- output$qtlMap <- renderPlot({
201
-
202
- res <- result()$res
203
- if(!("scanone" %in% class(res))) return(NULL)
204
-
205
- df <- as.data.frame(res)
206
-
207
- df <- df %>%
208
- group_by(chr) %>%
209
- mutate(pos_norm = pos/max(pos)) %>%
210
- ungroup()
211
-
212
- ggplot(df, aes(x=factor(chr), y=pos_norm)) +
213
-
214
- geom_segment(aes(x=factor(chr),
215
- xend=factor(chr),
216
- y=0, yend=1),
217
- size=5, color="cyan3") +
218
-
219
- geom_point(aes(color=lod),
220
- size=3) +
221
-
222
- coord_flip() +
223
-
224
- scale_color_gradient(low="blue", high="red") +
225
-
226
- labs(title="Chromosome QTL Map",
227
- x="Chromosome",
228
- y="Relative Position") +
229
-
230
  theme_minimal() +
231
- theme(panel.grid=element_blank())
 
232
  })
233
 
234
- # -------- Peaks -------- #
235
- output$peaks <- renderDT({
236
-
237
- res <- result()$res
238
-
239
- if("scanone" %in% class(res)){
240
- datatable(summary(res))
241
- } else {
242
- datatable(data.frame(Result=capture.output(print(res))))
243
- }
244
  })
245
 
246
- # -------- Download -------- #
247
- output$download <- downloadHandler(
248
- filename=function() "QTL_results.csv",
249
- content=function(file){
250
- res <- result()$res
251
- write.csv(as.data.frame(res), file)
252
- }
253
- )
254
  }
255
 
256
- # ---------------- RUN ---------------- #
257
  shinyApp(ui, server)
 
5
  library(ggplot2)
6
  library(DT)
7
 
 
8
  ui <- fluidPage(
9
 
10
+ titlePanel("QTL Mapping Pipeline"),
 
 
 
 
 
11
 
12
  sidebarLayout(
13
+ sidebarPanel(
14
 
15
+ fileInput("geno","Genotype CSV"),
16
+ fileInput("pheno","Phenotype CSV"),
17
 
18
+ selectInput("trait","Trait", choices=NULL),
19
 
20
  selectInput("model","Model",
21
+ c("EM","HK","IMP","CIM")),
 
 
22
 
23
+ actionButton("run","Run"),
24
  br(), br(),
25
+ verbatimTextOutput("status")
26
  ),
27
 
28
  mainPanel(
29
  tabsetPanel(
30
+ tabPanel("LOD", plotOutput("lod")),
31
+ tabPanel("Manhattan", plotOutput("manhattan")),
32
+ tabPanel("Table", DTOutput("table"))
 
 
33
  )
34
  )
35
  )
36
  )
37
 
 
38
  server <- function(input, output, session) {
39
 
40
+ output$status <- renderText("Waiting for input...")
41
+
42
+ # -------- Load phenotype -------- #
43
+ pheno <- reactive({
44
+ req(input$pheno)
45
+ read.csv(input$pheno$datapath)
46
+ })
47
+
48
+ observe({
49
+ req(pheno())
50
+ updateSelectInput(session,"trait",
51
+ choices=colnames(pheno())[-1])
52
+ })
53
+
54
+ # -------- Load genotype safely -------- #
55
+ geno <- reactive({
56
  req(input$geno)
57
 
58
+ g <- read.csv(input$geno$datapath)
59
 
60
+ # remove non-genotype row
61
+ if(any(!g[1,-1] %in% c(0,1,2,-1))){
62
  g <- g[-1,]
63
  }
64
 
 
 
 
 
 
 
65
  g
66
  })
67
 
68
+ # -------- Create cross safely -------- #
69
+ cross <- reactive({
 
 
 
70
 
71
+ req(geno(), pheno())
 
 
 
 
 
72
 
73
+ tryCatch({
 
74
 
75
+ merged <- merge(pheno(), geno(), by="id")
 
76
 
77
+ tmp <- tempfile(fileext = ".csv")
78
+ write.csv(merged, tmp, row.names=FALSE)
79
 
80
+ cross <- read.cross(
81
+ format="csv",
82
+ file=tmp,
83
+ genotypes=c("0","1","2"),
84
+ na.strings=c("-1","NA")
85
+ )
86
 
87
+ cross <- calc.genoprob(cross, step=1)
88
+
89
+ output$status <- renderText("Cross object created successfully")
 
 
 
90
 
91
+ cross
 
92
 
93
+ }, error=function(e){
94
+ output$status <- renderText(paste("Error:", e$message))
95
+ return(NULL)
96
+ })
97
  })
98
 
99
+ # -------- Run analysis -------- #
100
+ result <- eventReactive(input$run, {
101
 
102
+ req(cross(), input$trait)
103
 
104
+ tryCatch({
 
 
 
 
105
 
106
+ if(input$model=="CIM"){
107
+ res <- cim(cross(), pheno.col=input$trait)
108
+ } else {
109
+ method <- switch(input$model,
110
+ "EM"="em",
111
+ "HK"="hk",
112
+ "IMP"="imp")
113
 
114
+ res <- scanone(cross(),
115
+ pheno.col=input$trait,
116
+ method=method)
117
+ }
118
 
119
+ output$status <- renderText("Analysis completed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
+ res
 
122
 
123
+ }, error=function(e){
124
+ output$status <- renderText(paste("Analysis error:", e$message))
125
+ return(NULL)
126
+ })
127
  })
128
 
129
+ # -------- LOD plot -------- #
130
  output$lod <- renderPlot({
131
 
132
+ req(result())
 
 
 
133
 
134
+ df <- as.data.frame(result())
 
 
135
 
136
  ggplot(df, aes(pos, lod, color=factor(chr))) +
137
+ geom_line() +
138
+ theme_minimal() +
139
+ labs(title="LOD Plot")
 
 
 
140
  })
141
 
142
+ # -------- Manhattan -------- #
143
  output$manhattan <- renderPlot({
144
 
145
+ req(result())
 
146
 
147
+ df <- as.data.frame(result())
148
  df$chr <- as.factor(df$chr)
149
 
150
  df <- df %>%
 
154
  mutate(tot=cumsum(chr_len)-chr_len) %>%
155
  mutate(BPcum=pos+tot)
156
 
157
+ axis <- df %>%
158
  group_by(chr) %>%
159
  summarise(center=mean(BPcum))
160
 
161
  ggplot(df, aes(BPcum, lod, color=chr)) +
162
+ geom_point() +
163
+ scale_x_continuous(breaks=axis$center,
164
+ labels=axis$chr) +
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  theme_minimal() +
166
+ theme(legend.position="none") +
167
+ labs(title="Manhattan Plot")
168
  })
169
 
170
+ # -------- Table -------- #
171
+ output$table <- renderDT({
172
+ req(result())
173
+ datatable(as.data.frame(result()))
 
 
 
 
 
 
174
  })
175
 
 
 
 
 
 
 
 
 
176
  }
177
 
 
178
  shinyApp(ui, server)