annotate chromeister/bin/compute_score.R @ 0:7fdf47a0bae8 draft

Uploaded
author alvarofaure
date Wed, 12 Dec 2018 07:18:40 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
2 #!/usr/bin/env Rscript
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
3 args = commandArgs(trailingOnly=TRUE)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
4
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
5 if(length(args) < 2){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
6 stop("USE: Rscript --vanilla plot.R <matrix> <matsize>")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
7 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
8
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
9
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
10
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
11
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
12
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
13
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
14 growing_regions <- function(mat, reward = 6, penalty = 5, sidePenalty = 3, MAXHSPS = 500, TH = 10, WSIZE = 7){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
15
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
16 #write(mat, file ="data.txt", ncolumns = 200)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
17
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
18 len <- min(length(mat[1,]), length(mat[,1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
19 HSPS <- matrix(0, nrow=MAXHSPS, ncol=5)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
20 idx <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
21 lH <- round(WSIZE/2) - 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
22 rH <- round(WSIZE/2) + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
23
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
24
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
25 #print(paste("lH: ", lH, "rH: ", rH))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
26
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
27 if(WSIZE %% 2 == 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
28 print("WSIZE MUST BE ODD")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
29 stop()
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
30 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
31
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
32
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
33
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
34 i <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
35 #readline(prompt="Press [enter] to continue")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
36 while(i < len){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
37
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
38 value <- max(mat[i,]) * reward
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
39 if(value == 0) i <- i + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
40 pos <- which.max(mat[i,])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
41 # these two hold ending frag
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
42 endfrag <- pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
43 j <- i
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
44 count_penalties <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
45
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
46 #print("-----------------")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
47
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
48 while(value > 0 && j < len){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
49
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
50 #print(paste("took values ", j, endfrag, "which have score of ", mat[j, endfrag], "current value is: ", value))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
51
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
52
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
53
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
54
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
55 # Reset position used
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
56
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
57 mat[max(1,j-1), max(1,endfrag-2)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
58 mat[max(1,j-1), max(1,endfrag-1)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
59 mat[max(1,j-1), endfrag] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
60 mat[max(1,j-1), min(len, endfrag+1)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
61 mat[max(1,j-1), min(len, endfrag+2)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
62
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
63 mat[j, max(1,endfrag-2)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
64 mat[j, max(1,endfrag-1)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
65 mat[j, endfrag] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
66 mat[j, min(len, endfrag+1)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
67 mat[j, min(len, endfrag+2)] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
68
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
69 #print(paste("Erasing: (",max(1,j-1), max(1,endfrag-2),")(",max(1,j-1), max(1,endfrag-1),")(",max(1,j-1), endfrag,
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
70 # ")(",max(1,j-1), min(len, endfrag+1),")(",max(1,j-1), min(len, endfrag+2),")(",j, max(1,endfrag-2),
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
71 # ")(",j, max(1,endfrag-1),")(",j, endfrag,")(",j, min(len, endfrag+1),")(",j, min(len, endfrag+2),")"))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
72
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
73
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
74 # Go for next
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
75 j <- j + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
76 # Check next, could be reverse or forward
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
77 mleft <- max(1, endfrag-lH)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
78 mright <- min(len, endfrag+lH)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
79 window <- mat[j, mleft:mright]
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
80
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
81
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
82 #print(paste("mleft: ", mleft, "mright", mright, "j is: ", j))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
83 #print(window)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
84
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
85 v <- max(window)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
86 selected <- which.max(window)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
87 # Make it rather go diagonally
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
88 #print(paste("WIndow len: ", length(window)))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
89 chose_diagonal <- FALSE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
90 if(length(window) == WSIZE && v == window[lH]){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
91 selected <- lH
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
92 chose_diagonal <- TRUE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
93 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
94
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
95 if(length(window) == WSIZE && v == window[rH]){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
96 selected <- rH
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
97 chose_diagonal <- TRUE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
98 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
99
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
100 #print(paste("Selected value be like ", selected))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
101
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
102
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
103 if(v != 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
104
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
105
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
106
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
107 endfrag <- (mleft + selected ) # To make the indexing
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
108 if(length(window) == WSIZE) endfrag <- endfrag - 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
109 endfrag <- max(1, min(len, endfrag))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
110 #print(paste("\t", " endfragnew = ", endfrag, "max of window: ", max(window), "on position", which.max(window)))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
111 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
112
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
113 #print(paste("Chose diagonal is ", chose_diagonal))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
114
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
115 # If no similarity is found
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
116 if(v == 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
117 value <- value - count_penalties * penalty
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
118 count_penalties <- count_penalties + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
119 #print("Got penalty #########")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
120
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
121 }else{
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
122
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
123 # Similarity is found
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
124
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
125 if(!chose_diagonal){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
126 value <- value + count_penalties * (-sidePenalty)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
127 count_penalties <- count_penalties + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
128 #print("Got SIDE penalty @@@ #########")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
129 }else{
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
130 count_penalties <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
131 value <- value + reward
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
132 #print("Got reward thou #########")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
133 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
134
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
135 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
136 #readline(prompt="Press [enter] to continue")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
137
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
138 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
139 # Check len of frag
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
140 if(j-i > TH){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
141 # HSPS[idx, 1] <- i
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
142 # HSPS[idx, 2] <- pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
143 # HSPS[idx, 3] <- j
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
144 # HSPS[idx, 4] <- endfrag
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
145 # HSPS[idx, 5] <- abs(i-j)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
146
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
147 HSPS[idx, 1] <- pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
148 HSPS[idx, 2] <- i
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
149 HSPS[idx, 3] <- endfrag
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
150 HSPS[idx, 4] <- j
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
151 HSPS[idx, 5] <- abs(i-j)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
152
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
153
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
154
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
155 #print(paste("ACCEPT", i, pos, j, endfrag, "x-y", j-i, abs(endfrag-pos), sep = " "))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
156 idx <- idx + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
157 }else{
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
158 #print("REJECT")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
159 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
160 # This will prevent overlappign lines, I think
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
161 #i <- j
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
162
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
163 if(idx == MAXHSPS) break
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
164 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
165
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
166
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
167
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
168
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
169 return (HSPS)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
170 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
171
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
172 detect_events <- function(HSPS, sampling){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
173
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
174 DIAG_SEPARATION <- 10
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
175 # same as HSPS but adding the event
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
176 output <- matrix(0, nrow=length(HSPS[,1]), ncol=1+length(HSPS[1,]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
177 colnames(output) <- c("x1", "y1", "x2", "y2", "len", "event")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
178 j <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
179 for(i in 1:(length(HSPS[,1]))){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
180
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
181 if(sum(HSPS[i,]) > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
182 j <- j + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
183 is_inverted = FALSE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
184 is_diagonal = TRUE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
185
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
186 if(HSPS[i,1] > HSPS[i,3]) is_inverted = TRUE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
187 if(abs(HSPS[i,1] - HSPS[i,2]) > DIAG_SEPARATION && abs(HSPS[i,3] - HSPS[i,4]) > DIAG_SEPARATION) is_diagonal = FALSE
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
188
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
189 output[i,1] <- HSPS[i,1] * sampling
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
190 output[i,2] <- HSPS[i,2] * sampling
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
191 output[i,3] <- HSPS[i,3] * sampling
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
192 output[i,4] <- HSPS[i,4] * sampling
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
193 output[i,5] <- HSPS[i,5] * sampling
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
194
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
195 if(is_diagonal) output[i,6] <- "synteny block"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
196 if(is_diagonal && is_inverted) output[i,6] <- "inversion"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
197 if(!is_diagonal && !is_inverted) output[i,6] <- "transposition"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
198 if(!is_diagonal && is_inverted) output[i,6] <-"inverted transposition"
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
199 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
200
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
201
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
202 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
203
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
204 return (output[1:j,])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
205 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
206
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
207
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
208
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
209 paint_frags <- function(HSPS, l, sampling){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
210
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
211 plot(c(HSPS[1,1]*sampling, HSPS[1,3]*sampling), c(HSPS[1,2]*sampling, HSPS[1,4]*sampling), xlim = c(1,l*sampling), ylim = c(1,l*sampling), type="l", xlab="X-genome",ylab="Y-genome")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
212 for(i in 2:length(HSPS[,1])){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
213 if(sum(HSPS[i,]) > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
214 lines(c(HSPS[i,1]*sampling, HSPS[i,3]*sampling), c(HSPS[i,2]*sampling, HSPS[i,4]*sampling))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
215 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
216 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
217 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
218
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
219 supersample <- function(mat, upscale){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
220 l <- min(length(mat[1,]), length(mat[,1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
221 size <- round(l*upscale)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
222 m <- matrix(0, nrow=size, ncol=size)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
223
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
224 for(i in 1:size){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
225 for(j in 1:size){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
226 mleft <- max(1, i-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
227 mright <- min(l, i+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
228 mup <- max(1, j-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
229 mdown <- min(l, j+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
230
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
231 ri <- max(1, i/upscale)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
232 rj <- max(1, j/upscale)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
233
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
234 if(mat[ri, rj] > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
235 m[i, j] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
236 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
237 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
238 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
239 return (m)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
240 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
241
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
242
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
243 downsample <- function(mat, downscale){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
244 l <- min(length(mat[1,]), length(mat[,1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
245 size <- round(l/downscale)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
246 m <- matrix(0, nrow=size, ncol=size)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
247
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
248 for(i in 1:l){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
249 for(j in 1:l){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
250 mup <- max(1, i-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
251 mdown <- min(l, i+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
252 mleft <- max(1, j-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
253 mright <- min(l, j+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
254
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
255 #print(paste("Matrix at ", i, j))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
256 #print(mat[mup:mdown, mleft:mright])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
257
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
258 if(sum(mat[mup:mdown, mleft:mright]) > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
259 #print(paste("goes to ", round(i/downscale), round(j/downscale)))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
260 m[round(i/downscale), round(j/downscale)] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
261 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
262 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
263 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
264
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
265 return (m)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
266 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
267
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
268
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
269
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
270
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
271
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
272
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
273
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
274 path_mat = args[1]
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
275
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
276
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
277 fancy_name <- strsplit(path_mat, "/")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
278 fancy_name <- (fancy_name[[1]])[length(fancy_name[[1]])]
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
279
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
280 # Read sequence lenghts
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
281 con <- file(path_mat,"r")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
282 seq_lengths <- readLines(con, n=2)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
283 seq_x_len <- as.numeric(seq_lengths[1])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
284 seq_y_len <- as.numeric(seq_lengths[2])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
285 close(con)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
286
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
287
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
288 data <- as.matrix(read.csv(path_mat, sep = " ", skip=2))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
289
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
290 # Get sequence names
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
291 name_x <- strsplit(fancy_name, "-")[[1]][1]
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
292 name_y <- strsplit(fancy_name, "-")[[1]][2]
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
293
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
294 # Max of columns
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
295
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
296 len_i <- as.numeric(args[2])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
297 len_j <- as.numeric(args[2])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
298
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
299
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
300 score_density <- data
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
301 aux_density <- data
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
302
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
303
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
304 pmax_pos <- which.max(aux_density[,1])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
305 for(i in 1:len_i){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
306
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
307 cmax_pos <- which.max(aux_density[i,]) # get max of row
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
308
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
309 if((aux_density[i,cmax_pos]) > 0){ # if it has value
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
310 #row_from_col <- which.max(aux_density[,cmax_pos]) # get max of the column pointed by maximum of row
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
311 score_density[i,] <- 0 # put all others in row to 0 i.e. only use this max, EXCEPT for the maximum in the column
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
312 #score_density[row_from_col, cmax_pos] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
313 score_density[i,cmax_pos] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
314 pmax_pos <- cmax_pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
315 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
316 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
317
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
318
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
319 pmax_pos <- which.max(aux_density[1,])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
320
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
321
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
322 for(i in 1:len_i){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
323
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
324 cmax_pos <- which.max(aux_density[,i]) # get max of column
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
325
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
326 if((aux_density[cmax_pos,i]) > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
327 score_density[,i] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
328 score_density[cmax_pos,i] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
329 pmax_pos <- cmax_pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
330 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
331 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
332
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
333
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
334
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
335
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
336
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
337
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
338
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
339
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
340
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
341 score_copy <- score_density
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
342 diag_len <- 4
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
343
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
344 for(i in 6:(len_i-5)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
345 for(j in 6:(len_j-5)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
346
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
347 value <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
348 for(w in (-diag_len/2):(diag_len/2)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
349 if(score_density[i+w,j+w] > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
350 value <- value + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
351 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
352 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
353
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
354 if(value >= diag_len){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
355 for(k in 1:5){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
356 score_copy[i+k,j+k] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
357 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
358 for(k in 1:5){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
359 score_copy[i-k,j-k] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
360 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
361 }else if(score_copy[i,j]==0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
362 score_copy[i,j] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
363 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
364 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
365 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
366
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
367 for(i in 6:(len_i-5)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
368 for(j in 6:(len_j-5)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
369
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
370 value <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
371 for(w in (-diag_len/2):(diag_len/2)){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
372 if(score_density[i-w,j+w] > 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
373 value <- value + 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
374 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
375 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
376
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
377 if(value >= diag_len){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
378 for(k in 1:5){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
379 score_copy[i-k,j+k] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
380 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
381 for(k in 1:5){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
382 score_copy[i+k,j-k] <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
383 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
384 }else if(score_copy[i,j]==0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
385 score_copy[i,j] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
386 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
387 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
388 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
389
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
390
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
391 # Kernel to remove single points
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
392
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
393 for(i in 1:(length(score_copy[,1]))){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
394 for(j in 1:(length(score_copy[1,]))){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
395
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
396 value <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
397
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
398 min_i <- max(1, i-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
399 max_i <- min(len_i, i+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
400 min_j <- max(1, j-1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
401 max_j <- min(len_j, j+1)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
402
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
403 value <- sum(score_copy[min_i:max_i, min_j:max_j])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
404
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
405 if(value < 2) score_copy[i,j] <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
406 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
407 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
408
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
409 # To compute the score
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
410 score <- 0
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
411 pmax_pos <- which.max(score_copy[,1])
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
412 dist_th <- 1.5
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
413 besti <- 1
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
414 bestj <- pmax_pos
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
415 dvec1 <- abs(which.max(score_copy[,2]) - which.max(score_copy[,1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
416 dvec2 <- abs(which.max(score_copy[,3]) - which.max(score_copy[,2]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
417 dvec3 <- abs(which.max(score_copy[,4]) - which.max(score_copy[,3]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
418 for(i in 5:len_i){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
419
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
420 #print(paste(paste(paste(dvec1, dvec2), dvec3), mean(c(dvec1, dvec2, dvec3))))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
421 distance <- mean(c(dvec1, dvec2, dvec3))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
422
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
423
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
424
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
425 dvec1 <- dvec2
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
426 dvec2 <- dvec3
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
427 dvec3 <- abs(which.max(score_copy[,i]) - which.max(score_copy[,i-1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
428
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
429 # If there is a 0 or we are too far away just add max distance!
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
430 if(distance > dist_th || distance == 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
431 score <- score + len_i
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
432 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
433
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
434 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
435
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
436
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
437 score <- (score/(len_i^2))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
438 print(score)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
439
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
440
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
441 sampling_value <- 5
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
442 submat <- downsample(score_copy, sampling_value)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
443 m <- growing_regions((submat), WSIZE = 7, TH = 5, penalty = 15)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
444 events <- detect_events(m, sampling_value)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
445 events <- rbind(events, c(0,0,0,0,0,"Null event"))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
446 write(as.character(c(seq_x_len, seq_y_len)), file=paste(path_mat,".events.txt", sep=""), append = TRUE, sep =",", ncolumns=2)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
447 write(as.character(c("x1", "y1", "x2", "y2", "len", "event")), file=paste(path_mat,".events.txt", sep=""), append = TRUE, sep =",", ncolumns=6)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
448 for(i in 1:length(events[,1])){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
449 write(as.character(events[i,]), file=paste(path_mat,".events.txt", sep=""), append = TRUE, sep =",", ncolumns=6)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
450 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
451
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
452
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
453
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
454 coords1 <- round(seq(from=0, to=1, by=0.2)*seq_x_len)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
455 coords2 <- round(seq(from=0, to=1, by=0.2)*seq_y_len)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
456
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
457 final_image <- apply((t(score_copy)), 2, rev)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
458
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
459 png(paste(path_mat, ".filt.png", sep=""), width = length(data[,1]), height = length(data[,1]))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
460 image(t(final_image), col = grey(seq(1, 0, length = 256)), xaxt='n', yaxt='n', main = paste(fancy_name, paste("filt. score=", score)), xlab = name_x, ylab = name_y, axes = FALSE)
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
461 axis(1, tick = TRUE, labels = (coords1), at = c(0.0, 0.2, 0.4, 0.6, 0.8, 1))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
462 axis(2, tick = TRUE, labels = rev(coords2), at = c(0.0, 0.2, 0.4, 0.6, 0.8, 1))
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
463 dev.off()
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
464
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
465
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
466 # Output pixel coordinates of highly conserved signals
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
467
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
468 # To clear it in case it existed
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
469 write(c(paste("X", "Y")), file = paste("hits-XY-", paste(fancy_name, ".hits", sep=""), sep=""), append = FALSE, sep = "\n")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
470
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
471
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
472 for(i in 1:(length(score_copy[,1]))){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
473 for(j in 1:(length(score_copy[1,]))){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
474 if(score_copy[i,j] != 0){
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
475 write(c(paste(i, j)), file = paste("hits-XY-", paste(fancy_name, ".hits", sep=""), sep=""), append = TRUE, sep = "\n")
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
476 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
477 }
7fdf47a0bae8 Uploaded
alvarofaure
parents:
diff changeset
478 }