-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataPreparation.R
100 lines (67 loc) · 3.46 KB
/
dataPreparation.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
rm(list=ls())
library(data.table)
library(ggplot2)
library(reshape2)
library(tidyr)
library(dplyr)
library(gplots)
library(RColorBrewer)
############# DATA PREPARATION ######
# uploda metadata
metadata = read.csv("metadata.csv")
colnames(metadata)[1] = "ID"
metadata$ID = as.character(metadata$ID)
# path to the folder with tsv files
filePath = "results_with_taxa_names/"
# get the tax IDs in all of the TSV files - starts with virus, ends with .tsv
tsvFiles = list.files(path = filePath)
# get the ID numbers from the TSV files
ID = regmatches(tsvFiles, gregexpr("[[:digit:]]+", tsvFiles))
ID = unlist(ID)
# filter out only the used metadata
metadata = metadata[metadata$ID %in% ID, ]
# upload files and make the tables
tsvFiles = list.files(path = filePath, full.names = T)
for (i in 2:length(tsvFiles)){
keepCols = c("Genome", "Final Guess", "Final Best Hit", "Final Best Hit Read Numbers", "Final High Confidence Hits", "Organism")
if(i == 2){
#upload table - select interesting columns - give them ID - merge by Genome and Organism
firstFile = fread(tsvFiles[i-1], skip = 1, sep = "\t", header = T) %>%
select(keepCols)
firstID = unlist(regmatches(tsvFiles[i-1], gregexpr("[[:digit:]]+", tsvFiles[i-1])))
secondFile = fread(tsvFiles[i], skip = 1, sep = "\t", header = T)%>%
select(keepCols)
secondID = unlist(regmatches(tsvFiles[i], gregexpr("[[:digit:]]+", tsvFiles[i])))
setkey(firstFile, "Genome", "Organism")
setkey(secondFile, "Genome", "Organism")
origFirstColnames = colnames(firstFile)[!colnames(firstFile) %in% c("Genome", "Organism")]
colnames(firstFile)[!colnames(firstFile) %in% c("Genome", "Organism")] = paste(origFirstColnames, firstID, sep = ".")
origSecondColnames = colnames(secondFile)[!colnames(secondFile) %in% c("Genome", "Organism")]
colnames(secondFile)[!colnames(secondFile) %in% c("Genome", "Organism")] = paste(origSecondColnames, secondID, sep = ".")
myTable = merge(firstFile, secondFile, all = TRUE)
} else {
newFile = fread(tsvFiles[i], skip = 1, sep = "\t", header = T)%>%
select(keepCols)
newID = unlist(regmatches(tsvFiles[i], gregexpr("[[:digit:]]+", tsvFiles[i])))
setkey(newFile, "Genome", "Organism")
origNewColnames = colnames(newFile)[!colnames(newFile) %in% c("Genome", "Organism")]
colnames(newFile)[!colnames(newFile) %in% c("Genome", "Organism")] = paste(origNewColnames, newID, sep = ".")
myTable = merge(myTable, newFile, all = TRUE)
}
}
myTable[is.na(myTable)] = 0
columnOptions = origNewColnames
# separate the table into separate tables and give the columns the kids' IDs
#--final guess table
finalGuess = myTable %>%
select(Genome, Organism, starts_with("Final Guess"))
colnames(finalGuess)[-c(1,2)] = c(unlist(regmatches(colnames(finalGuess), gregexpr("[[:digit:]]+", colnames(finalGuess)))))
#--final best hit
finalBestHit = myTable %>%
select(Genome, Organism, starts_with("Final Best Hit."))
colnames(finalBestHit)[-c(1,2)] = c(unlist(regmatches(colnames(finalBestHit), gregexpr("[[:digit:]]+", colnames(finalBestHit)))))
#-final best hit read number table
finalBestHitReads = myTable %>%
select(Genome, Organism, starts_with("Final Best Hit Read Numbers"))
colnames(finalBestHitReads)[-c(1,2)] = c(unlist(regmatches(colnames(finalBestHitReads), gregexpr("[[:digit:]]+", colnames(finalBestHitReads)))))
rm(list=setdiff(ls(), c("metadata", "finalGuess", "finalBestHit", "finalBestHitReads")))