-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consensus_Script.R
199 lines (178 loc) · 8.06 KB
/
Consensus_Script.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/Rscript
################################################
##########load required libraries###############
###############################################
library(data.table)
library(plyr)
library(dplyr)
library(stringr)
library(tidyr)
######################################
#########Set working directory########
######################################
setwd('/projects/dumont-lab/uma/k31/mapped.sam/')
######################################
#########read mapped kmers file########
######################################
k31file <- read.delim(file = '$1.fastq.k31.kmerscore.txt.satellitekmers.mapped.sam', header = FALSE, sep = '\t')
######################################
#########subset and split columns########
######################################
k31file <- select(k31file, V1,V2,V3,V4,V6,V13)
k31file <- separate(k31file,col=V1,into=c("KMER","COUNT"),sep = '_')
k31file <- separate(k31file,col=V13,into=c("tag","i","HDIST"),sep = ':')
k31file <- select(k31file, KMER,COUNT,V2,V3,V4,V6,HDIST)
k31file[,2] <- as.integer(k31file[,2])
##########################################
#########split major and minor sat########
##########################################
k31major <- filter(k31file, V3 == 'MajorSatelliteConsensusWongandRattner1988x3')
k31major_r <- filter(k31major, V2 == 16)
k31major_f <- filter(k31major, V2 == 0)
rm(k31major)
k31minor <- filter(k31file, V3 == 'MinorSatelliteConsensusWongandRattner1988x3')
k31minor_r <- filter(k31minor, V2 == 16)
k31minor_f <- filter(k31minor, V2 == 0)
rm(k31minor)
rm(k31file)
#########################################################################
##########create a dataframe for minor and major sequences###############
#########################################################################
#Minor satellite
probscoreminsat_f <-data.frame(matrix(nrow = 360,ncol = 4))
colnames(probscoreminsat_f) <- c('A','T','G','C')
probscoreminsat_f[is.na(probscoreminsat_f)]<-0
probscoreminsat_r <-data.frame(matrix(nrow = 360,ncol = 4))
colnames(probscoreminsat_r) <- c('A','T','G','C')
probscoreminsat_r[is.na(probscoreminsat_r)]<-0
#Major satellite
probscoremajsat_f <-data.frame(matrix(nrow = 702,ncol = 4))
colnames(probscoremajsat_f) <- c('A','T','G','C')
probscoremajsat_f[is.na(probscoremajsat_f)]<-0
probscoremajsat_r <-data.frame(matrix(nrow = 702,ncol = 4))
colnames(probscoremajsat_r) <- c('A','T','G','C')
probscoremajsat_r[is.na(probscoremajsat_r)]<-0
#########################################
########Minor satellite forward#########
#########################################
df <- k31minor_f
for (i in 1:nrow(df)) {
kmer <- as.vector(df[i,1])
kmer <- strsplit(kmer,split="")
kmercount <- df[i,2]
rownumber <- df[i,5]
for (j in 1:31) {
letter <- kmer[[1]][j]
if (letter == "A"){
probscoreminsat_f[rownumber,1]=probscoreminsat_f[rownumber,1]+kmercount
} else if (letter == "T") {
probscoreminsat_f[rownumber,2]=probscoreminsat_f[rownumber,2]+kmercount
} else if (letter == "G") {
probscoreminsat_f[rownumber,3]=probscoreminsat_f[rownumber,3]+kmercount
} else if (letter == "C") {
probscoreminsat_f[rownumber,4]=probscoreminsat_f[rownumber,4]+kmercount
}
rownumber = rownumber + 1
}
}
probscoreminsat_f[,5] <- probscoreminsat_f[,1] + probscoreminsat_f[,2] + probscoreminsat_f[,3] + probscoreminsat_f[,4]
# probscoreminsat_f[,1] <- probscoreminsat_f[,1]/probscoreminsat_f[,5]
# probscoreminsat_f[,2] <- probscoreminsat_f[,2]/probscoreminsat_f[,5]
# probscoreminsat_f[,3] <- probscoreminsat_f[,3]/probscoreminsat_f[,5]
# probscoreminsat_f[,4] <- probscoreminsat_f[,4]/probscoreminsat_f[,5]
#write out table
setwd('/projects/dumont-lab/uma/k31/consensus_separated')
write.table(probscoreminsat_f,file = "$1.consensus.minor.forward.txt", sep = '\t', quote = FALSE, row.names = FALSE)
########################################
########Minor satellite reverse#########
#########################################
df <- k31minor_r
for (i in 1:nrow(df)) {
kmer <- as.vector(df[i,1])
kmer <- strsplit(kmer,split="")
kmercount <- df[i,2]
rownumber <- df[i,5] + 30
for (j in 1:31) {
letter <- kmer[[1]][j]
if (letter == "A"){
probscoreminsat_r[rownumber,1]=probscoreminsat_r[rownumber,1]+kmercount
} else if (letter == "T") {
probscoreminsat_r[rownumber,2]=probscoreminsat_r[rownumber,2]+kmercount
} else if (letter == "G") {
probscoreminsat_r[rownumber,3]=probscoreminsat_r[rownumber,3]+kmercount
} else if (letter == "C") {
probscoreminsat_r[rownumber,4]=probscoreminsat_r[rownumber,4]+kmercount
}
rownumber = rownumber - 1
}
}
probscoreminsat_r[,5] <- probscoreminsat_r[,1] + probscoreminsat_r[,2] + probscoreminsat_r[,3] + probscoreminsat_r[,4]
# probscoreminsat_r[,1] <- probscoreminsat_r[,1]/probscoreminsat_r[,5]
# probscoreminsat_r[,2] <- probscoreminsat_r[,2]/probscoreminsat_r[,5]
# probscoreminsat_r[,3] <- probscoreminsat_r[,3]/probscoreminsat_r[,5]
# probscoreminsat_r[,4] <- probscoreminsat_r[,4]/probscoreminsat_r[,5]
#write out table
setwd('/projects/dumont-lab/uma/k31/consensus_separated')
write.table(probscoreminsat_r,file = "$1.consensus.minor.reverse.txt", sep = '\t', quote = FALSE, row.names = FALSE)
#########################################
########Major satellite forward#########
#########################################
df <- k31major_f
for (i in 1:nrow(df)) {
kmer <- as.vector(df[i,1])
kmer <- strsplit(kmer,split="")
kmercount <- df[i,2]
rownumber <- df[i,5]
for (j in 1:31) {
letter <- kmer[[1]][j]
if (letter == "A"){
probscoremajsat_f[rownumber,1]=probscoremajsat_f[rownumber,1]+kmercount
} else if (letter == "T") {
probscoremajsat_f[rownumber,2]=probscoremajsat_f[rownumber,2]+kmercount
} else if (letter == "G") {
probscoremajsat_f[rownumber,3]=probscoremajsat_f[rownumber,3]+kmercount
} else if (letter == "C") {
probscoremajsat_f[rownumber,4]=probscoremajsat_f[rownumber,4]+kmercount
}
rownumber = rownumber + 1
}
}
probscoremajsat_f[,5] <- probscoremajsat_f[,1] + probscoremajsat_f[,2] + probscoremajsat_f[,3] + probscoremajsat_f[,4]
# probscoremajsat_f[,1] <- probscoremajsat_f[,1]/probscoremajsat_f[,5]
# probscoremajsat_f[,2] <- probscoremajsat_f[,2]/probscoremajsat_f[,5]
# probscoremajsat_f[,3] <- probscoremajsat_f[,3]/probscoremajsat_f[,5]
# probscoremajsat_f[,4] <- probscoremajsat_f[,4]/probscoremajsat_f[,5]
#write out table
setwd('/projects/dumont-lab/uma/k31/consensus_separated')
write.table(probscoremajsat_f,file = "$1.consensus.major.forward.txt", sep = '\t', quote = FALSE, row.names = FALSE)
########################################
########major satellite reverse#########
#########################################
df <- k31major_r
for (i in 1:nrow(df)) {
kmer <- as.vector(df[i,1])
kmer <- strsplit(kmer,split="")
kmercount <- df[i,2]
rownumber <- df[i,5] + 30
for (j in 1:31) {
letter <- kmer[[1]][j]
if (letter == "A"){
probscoremajsat_r[rownumber,1]=probscoremajsat_r[rownumber,1]+kmercount
} else if (letter == "T") {
probscoremajsat_r[rownumber,2]=probscoremajsat_r[rownumber,2]+kmercount
} else if (letter == "G") {
probscoremajsat_r[rownumber,3]=probscoremajsat_r[rownumber,3]+kmercount
} else if (letter == "C") {
probscoremajsat_r[rownumber,4]=probscoremajsat_r[rownumber,4]+kmercount
}
rownumber = rownumber - 1
}
}
probscoremajsat_r[,5] <- probscoremajsat_r[,1] + probscoremajsat_r[,2] + probscoremajsat_r[,3] + probscoremajsat_r[,4]
# probscoremajsat_r[,1] <- probscoremajsat_r[,1]/probscoremajsat_r[,5]
# probscoremajsat_r[,2] <- probscoremajsat_r[,2]/probscoremajsat_r[,5]
# probscoremajsat_r[,3] <- probscoremajsat_r[,3]/probscoremajsat_r[,5]
# probscoremajsat_r[,4] <- probscoremajsat_r[,4]/probscoremajsat_r[,5]
#write out table
setwd('/projects/dumont-lab/uma/k31/consensus_separated')
write.table(probscoremajsat_r,file = "$1.consensus.major.reverse.txt", sep = '\t', quote = FALSE, row.names = FALSE)