-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_significant_results.R
67 lines (59 loc) · 2.64 KB
/
parse_significant_results.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
#install.packages("jsonlite")
require(jsonlite)
# R version 3.5.1 (2018-07-02) -- "Feather Spray"
# parses multiple json files and returns list of species and branch under selection
# must be used in directory with all the json files
# remember to replace Inf values with "Inf" using
# sed -i 's/inf/"inf"/'
<<<<<<< HEAD
# run using <Rscript parse_results.R>
#setwd("C:/Users/path/to/json/files")
=======
setwd("C:/Users/path/to/json/files")
>>>>>>> eb9498e6f6fb1967fb11bff82076be6e46ae536a
files <- list.files(full.names = TRUE, pattern = '*.json$')
print(files)
string <- "p2n.ABSREL.json"
for(file in files){
global_results <- data.frame(name = character(0), branch = character(0), omega1 = character(0), percent1 = character(0), omega2 = character(0),
percent2 = character(0), omega3 = character(0), percent3 = character(0), stringsAsFactors = FALSE)
name <- strsplit(paste(file), split = string)
simplejson <- fromJSON(file)
index <- 1
branchnames <- list()
branchnames <- simplejson$`branch attributes`$`0`
for(i in branchnames){
branch <- names(branchnames[index])
local_results <- data.frame(name = character(0), branch = character(0), omega1 = character(0), percent1 = character(0), omega2 = character(0),
percent2 = character(0), omega3 = character(0), percent3 = character(0), stringsAsFactors = FALSE)
if(i$`Corrected P-value` > 0.05){
index <- index + 1
next
} else if(i$`Corrected P-value` <= 0.05){
local_results[1, 1] <- name
local_results[1, 2] <- branch
if (i$`Rate classes` == 2){
local_results[1, 3] <- i$`Rate Distributions`[1,1]
local_results[1, 4] <- i$`Rate Distributions`[1,2]
local_results[1, 5] <- i$`Rate Distributions`[2,1]
local_results[1, 6] <- i$`Rate Distributions`[2,2]
local_results[1, 7] <- "NA"
local_results[1, 8] <- "NA"
} else if(i$`Rate classes` == 3){
local_results[1, 3] <- i$`Rate Distributions`[1,1]
local_results[1, 4] <- i$`Rate Distributions`[1,2]
local_results[1, 5] <- i$`Rate Distributions`[2,1]
local_results[1, 6] <- i$`Rate Distributions`[2,2]
local_results[1, 7] <- i$`Rate Distributions`[3,1]
local_results[1, 8] <- i$`Rate Distributions`[3,2]
}
}
global_results <- dplyr::bind_rows(global_results, local_results)
rm(local_results)
index <- index + 1
}
global_results <- na.omit(global_results)
if (nrow(global_results) > 0) {
write.table(global_results, file = "parsed_absrel_json.txt", sep = "\t", row.names = FALSE, col.names = FALSE, append = TRUE, quote = FALSE)
}
}