-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motif_Finding.Rmd
163 lines (121 loc) · 3.98 KB
/
Motif_Finding.Rmd
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
---
title: "Motif-Finding"
author: "Okiemute Omoru"
date: '2022-07-10'
output: html_document
---
```{r}
###HTP-Homework4
#Relative Path
setwd("/Users/kiteomoru/Desktop/Motif-Finding/Assignment4-High-throughput")
getwd()
#Required libraries
library(dplyr)
library(tidyverse)
library(reshape)
library(stringr)
require(data.table)
library(bioseq)
##Q1
#load data
pwmfile<- suppressWarnings(read.table('argR-counts-matrix.txt', header = F, sep = "\t", row.names =1))
pwmfile<- pwmfile[,2:19]
pwmfile<-as.matrix(pwmfile)
view(pwmfile)
#Create PWN function for working out the position weight matrix value
pwm<- function(matrix){
#Frequency with pseudocounts
p<-matrix + 1
freq<-list()
for (i in 1:nrow(p)){
for (j in 1:ncol(p)){
freq<- c(freq, p[i,j]/ sum(p[,j]))
}
}
#Weight matrix
weight<-list()
for (i in freq){
weight<-c(weight,log(i/.25))
}
weightmat<-matrix(unlist(weight), ncol=18, nrow =4,byrow=TRUE)
rownames(weightmat)<-c('a','c','g','t')
View(weightmat)
return(weightmat)
}
scoringmatrix<-pwm(pwmfile)
scoringmatrix
#write table
write.table(scoringmatrix, file = 'scoringmatrix.txt', quote = FALSE,sep='\t', col.names = NA)
##Q2
#Load the data
seq<- read.table('E_coli_K12_MG1655.400_50 3', as.is = T)
seq <- subset(seq, select = -c(V2,V4))
head(seq)
#####Generate sequences of length 18 (This code will take a while. Please be patient)
kmer18=dna(seq$V3)
kmer18=seq_split_kmer(kmer18, k = 18)
kmer18=as.data.frame(kmer18)
head(kmer18)
seqkmers<- kmer18 %>%
mutate_all(as.character)
# Change sequences from upper case to lower case
seqkmers<-data.frame(lapply(seqkmers, function(v) {
if (is.character(v)) return(tolower(v))
else return(v)
}))
colnames(seqkmers)= seq$V1 #add the gene ids as column names
head(seqkmers)
# Create a Function that scans the weights in the scoring matrix(Logo-odds Matrix) from q1 to identify binding sites in the data-frame
weight = function(kmerstr){
x <- strsplit(x=kmerstr,split='')
seq_score <- vector()
for (i in 1:18){
seq_score[i] <- scoringmatrix[x[[1]][i],i]
}
return(print(seq_score))
}
# apply function
#Reshape data
newcol= c(1:nrow(seqkmers))
seqkmers$newcol=newcol
reshaped_seqkmers<- melt(seqkmers,
id.vars = c('newcol'),
variable_name = c('Seq_ID'),
value.name= c('Sequences'))
head(reshaped_seqkmers, n=50)
# apply function to add scores to the data frame
reshaped_seqkmers$scores<- lapply(reshaped_seqkmers$Sequences, weight) #Takes a while
head(reshaped_seqkmers, n=50)
####
#Create a new column(totalscore) to the dataframe which is the sum of all the scores
reshaped_seqkmers$totalscores<- lapply(reshaped_seqkmers$scores, function(a) sum(as.numeric(a)))
head(reshaped_seqkmers, n=50)
###
#remove the newcol colume from the dataframe
reshaped_seqkmers <- subset(reshaped_seqkmers, select = -c(newcol))
head(reshaped_seqkmers, n=50)
#Obtain the highest score for each gene id
topscoresforallgeneids<-setDT(reshaped_seqkmers)[, .SD[which.max(totalscores)], by= variable]
topscoresforallgeneids$totalscores<-as.numeric(topscoresforallgeneids$totalscores)
head(topscoresforallgeneids, n=50)
#####
# Another way to view top scores per gene-ID
reshaped_seqkmers$totalscores<-as.numeric(reshaped_seqkmers$totalscores)
Z<- reshaped_seqkmers %>%
group_by(variable) %>%
summarize(max.totalscores = max(totalscores))
head(Z, n=20)
#Top30
topscoresforallgeneids_arranged <- topscoresforallgeneids %>% arrange(desc(totalscores))
head(topscoresforallgeneids_arranged, n=20)
##top30
top30motifs<- head(topscoresforallgeneids_arranged, n=30)
view(top30motifs)
##Write table
top30motifs2<- subset(top30motifs, select = -c(scores))
write.table(top30motifs2, file = 'top30motifs.txt', quote = FALSE,sep='\t', col.names = NA)
####Reference:
##The results in this assignment was made with guidance from codes found in the website:
##https://davetang.org/muse/2013/10/01/position-weight-matrix/
## and various references to stackoverflow
```