-
Notifications
You must be signed in to change notification settings - Fork 0
/
automated_program.R
157 lines (108 loc) · 4.65 KB
/
automated_program.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
library(tidyverse)
library(meanShiftR)
predict_SV = function(data) {
#Clean the data
data_tidy <<- data %>%
rename("Reads" = 1, "FLAG" = 2, "POS" = 4, "CIGAR" = 6, "PNEXT" = 8, "TLEN" = 9) %>% #Name columns
select(Reads, FLAG, POS, CIGAR, PNEXT, TLEN) %>%
filter(!is.na(POS) & !is.na(PNEXT)) %>% #Clean
#Include only reads that have one mapping
group_by(Reads) %>%
mutate(count = n()) %>%
filter(count == 2) %>%
filter(TLEN > 0)
print("Clean")
data_marked <<- data_tidy %>%
#Mark the data
mutate(direction = ifelse(FLAG %in% c(161, 97, 163, 99) & TLEN > 0, "I", NA)) %>%
mutate(direction = ifelse(FLAG %in% c(161, 97, 163, 99) & TLEN < 0, "O", direction)) %>%
mutate(direction = ifelse(FLAG %in% c(81, 145, 83, 147) & TLEN < 0, "I", direction)) %>%
mutate(direction = ifelse(FLAG %in% c(81, 145, 83, 147) & TLEN > 0, "O", direction)) %>%
mutate(direction = ifelse(FLAG %in% c(65,129,113,177), "S", direction))
print("Marked")
match_length <- max(as.numeric(str_extract(data_marked$CIGAR, "[:digit:]+")))
print(as.character(match_length))
data_lengths <<- data_marked %>%
mutate(PNEXT = ifelse(direction == "I" & TLEN > 0, PNEXT + match_length, PNEXT)) %>%
mutate(POS = ifelse(direction == "I" & TLEN < 0, POS + match_length, POS)) %>%
mutate(PNEXT = ifelse(direction == "O" & TLEN < 0, PNEXT + match_length, PNEXT)) %>%
mutate(POS = ifelse(direction == "O" & TLEN > 0, POS + match_length, POS)) %>%
mutate(Length = abs(PNEXT - POS))
print("Length Corrected")
max_Length <- median(data_lengths$Length) + 3.22*mad(data_lengths$Length)
print(as.character(max_Length))
data_selected <- data_lengths %>%
filter(Length > match_length*2) %>% #2*Read length
filter(Length < max_Length) #Select empirical max distance
coverage <- simulate_coverage(data_selected)
min_coverage <- min(coverage)
print("Coveraged Simulated")
data_off_diagonal <- data_lengths %>%
filter(Length > max_Length)
points <- cbind(data_off_diagonal$POS, data_off_diagonal$PNEXT)
candidates <- meanShift(points)
candidates_points <- as.data.frame(candidates$value)
candidates_points <- candidates_points %>%
rename("POS" = 1, "PNEXT" = 2)
data_off_diagonal$POS_center <- candidates_points$POS
data_off_diagonal$PNEXT_center <- candidates_points$PNEXT
print("Mean Shift Clustered")
counts <<- data_off_diagonal %>%
mutate(near = ifelse(abs(POS - POS_center) < max_Length & abs(PNEXT - PNEXT_center) < max_Length, TRUE, FALSE)) %>%
filter(near) %>%
group_by(POS_center, PNEXT_center) %>%
mutate(count = sum(near)) %>%
ungroup() %>%
ungroup()
median <- data_off_diagonal %>%
mutate(near = ifelse(abs(POS - POS_center) < max_Length & abs(PNEXT - PNEXT_center) < max_Length, TRUE, FALSE)) %>%
filter(near) %>%
group_by(POS_center, PNEXT_center) %>%
summarize(count = n(), POS_center, PNEXT_center) %>%
ungroup() %>%
ungroup() %>%
summarize(median = median(count)) %>%
pull
mad <- data_off_diagonal %>%
mutate(near = ifelse(abs(POS - POS_center) < max_Length & abs(PNEXT - PNEXT_center) < max_Length, TRUE, FALSE)) %>%
filter(near) %>%
group_by(POS_center, PNEXT_center) %>%
summarize(count = n(), POS_center, PNEXT_center) %>%
ungroup() %>%
ungroup() %>%
summarize(mad = mad(count)) %>%
pull
sv_predictions <<- counts %>%
mutate(median = median, mad = mad, relative_signal = count / mean(coverage)) %>%
mutate(SV = ifelse(direction == "I", "Deletion", "None")) %>%
mutate(SV = ifelse(direction == "O", "Duplication", SV )) %>%
mutate(SV = ifelse(direction == "S", "Inversion", SV))
print("SV Predicted")
return(sv_predictions)
}
simulate_coverage = function(data) {
data <- data %>%
ungroup()
n <- data %>%
summarize(n()) %>%
pull
#Max d
max_d <- data %>%
summarise(max(Length)) %>%
pull
#Length of genome studied
L <- data %>%
summarise(max(PNEXT) - min(POS)) %>%
pull
#Run as a block to find empirical distribution
simulationSums <- {} #Create an empty vector
for (i in 1:10000) { #Run 1000 times to get a distribution
vector <- {} #Create a holding vector
for (d in 1:max_d) { #Iterate from 1 to max d
poson <- rpois(1, (n / L)) #Poisson distribution with average n/L
vector <- append(vector, sample(data$Length, size = poson, replace = TRUE) > d) #Sample a random amount of lengths and see if they are greater than current d
}
simulationSums[i] <- sum(vector) #Sum gives count of reads, append to sums
}
return(simulationSums)
}