-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.r
More file actions
111 lines (94 loc) · 3.84 KB
/
server.r
File metadata and controls
111 lines (94 loc) · 3.84 KB
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
require(XML)
library(plyr)
library(dplyr)
library(DT)
server <- function(input, output, session){
custom_db <- c("LvTx")
custom_db_path <- c("/LV_transcriptome/LvTx")
blastresults <- eventReactive(input$blast, {
#gather input and set up temp file
query <- input$query
tmp <- tempfile(fileext = ".fa")
#if else chooses the right database
if (input$db == custom_db){
db <- custom_db_path
remote <- c("")
} else {
db <- c("nr")
#add remote option for nr since we don't have a local copy
remote <- c("-remote")
}
#this makes sure the fasta is formatted properly
if (startsWith(query, ">")){
writeLines(query, tmp)
} else {
writeLines(paste0(">Query\n",query), tmp)
}
#calls the blast
data <- system(paste0(input$program," -query ",tmp," -db ",db," -evalue ",input$eval," -outfmt 5 -max_hsps 1 -max_target_seqs 10 ",remote), intern = T)
xmlParse(data)
}, ignoreNULL= T)
#Now to parse the results...
parsedresults <- reactive({
if (is.null(blastresults())){}
else {
xmltop = xmlRoot(blastresults())
#the first chunk is for multi-fastas
results <- xpathApply(blastresults(), '//Iteration',function(row){
query_ID <- getNodeSet(row, 'Iteration_query-def') %>% sapply(., xmlValue)
hit_IDs <- getNodeSet(row, 'Iteration_hits//Hit//Hit_id') %>% sapply(., xmlValue)
hit_length <- getNodeSet(row, 'Iteration_hits//Hit//Hit_len') %>% sapply(., xmlValue)
bitscore <- getNodeSet(row, 'Iteration_hits//Hit//Hit_hsps//Hsp//Hsp_bit-score') %>% sapply(., xmlValue)
eval <- getNodeSet(row, 'Iteration_hits//Hit//Hit_hsps//Hsp//Hsp_evalue') %>% sapply(., xmlValue)
cbind(query_ID,hit_IDs,hit_length,bitscore,eval)
})
#this ensures that NAs get added for no hits
results <- rbind.fill(lapply(results,function(y){as.data.frame((y),stringsAsFactors=FALSE)}))
}
})
#makes the datatable
output$blastResults <- renderDataTable({
if (is.null(blastresults())){
} else {
parsedresults()
}
}, selection="single")
#this chunk gets the alignemnt information from a clicked row
output$clicked <- renderTable({
if(is.null(input$blastResults_rows_selected)){}
else{
xmltop = xmlRoot(blastresults())
clicked = input$blastResults_rows_selected
tableout<- data.frame(parsedresults()[clicked,])
tableout <- t(tableout)
names(tableout) <- c("")
rownames(tableout) <- c("Query ID","Hit ID", "Length", "Bit Score", "e-value")
colnames(tableout) <- NULL
data.frame(tableout)
}
},rownames =T,colnames =F)
#this chunk makes the alignments for clicked rows
output$alignment <- renderText({
if(is.null(input$blastResults_rows_selected)){}
else{
xmltop = xmlRoot(blastresults())
clicked = input$blastResults_rows_selected
#loop over the xml to get the alignments
align <- xpathApply(blastresults(), '//Iteration',function(row){
top <- getNodeSet(row, 'Iteration_hits//Hit//Hit_hsps//Hsp//Hsp_qseq') %>% sapply(., xmlValue)
mid <- getNodeSet(row, 'Iteration_hits//Hit//Hit_hsps//Hsp//Hsp_midline') %>% sapply(., xmlValue)
bottom <- getNodeSet(row, 'Iteration_hits//Hit//Hit_hsps//Hsp//Hsp_hseq') %>% sapply(., xmlValue)
rbind(top,mid,bottom)
})
#split the alignments every 40 carachters to get a "wrapped look"
alignx <- do.call("cbind", align)
splits <- strsplit(gsub("(.{40})", "\\1,", alignx[1:3,clicked]),",")
#paste them together with returns '\n' on the breaks
split_out <- lapply(1:length(splits[[1]]),function(i){
rbind(paste0("Q-",splits[[1]][i],"\n"),paste0("M-",splits[[2]][i],"\n"),paste0("H-",splits[[3]][i],"\n"))
})
unlist(split_out)
}
})
}
#phew...