-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmotif2meme.R
95 lines (91 loc) · 3.75 KB
/
motif2meme.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
# convert homer.motif to meme.motif format
# how to run:
#source("/path/to/motif2meme.R")
#motif2meme("/path/to/homerMotifs.all.motifs")
#motif2meme("/path/to/knownResults.all.motif")
# output: homerMotifs.all.motifs.meme & knownResults.all.motif.meme
#converts a Homer .motif PWM/PFM into MEME format
motif2meme <- function(inFile) {
library(tools)
#establishing the number of distinct motifs in the file and parsing it accordingly
stopifnot(is.character(inFile))
outFile <- paste(inFile,"meme",sep=".")
thisFile <- file(outFile)
fileName <- file_path_sans_ext(inFile)
#reading the input file
motif.file <- scan(file=inFile,character(0), sep="\n",quote=NULL)
motif.index <- grep(pattern="^>",motif.file)
n.motifs <- length(motif.index)
total.len <- length(motif.file)
#print(n.motifs)
sink(thisFile,append=TRUE)
cat("MEME version 4\n\n",file=thisFile,append=TRUE)
cat("ALPHABET= ACGT\n\n",file=thisFile,append=TRUE)
cat("strands: + -\n\n")
nameSplit <- strsplit(fileName,"_")
nameNum <- nameSplit[[1]][1]
motifNum <- sub("^.....(..).*", "\\1", nameNum) # fifth
for (i in 1:(n.motifs-1)) {
#print(i)
#print(" ")
(motif.index[i]+1) -> index.start
((motif.index[i+1])-1) -> index.end
motif.file[index.start:index.end] -> this_motif
strsplit(this_motif,split="\t") -> motif_split
#print(head(motif_split))
length(this_motif) -> motif_row
array(NA,c(motif_row,4)) -> motif_array
for (n in 1:motif_row) {
as.numeric(motif_split[[n]]) -> motif_array[n,]
}
motif_array <- as.data.frame(motif_array)
motif.file[motif.index[i]] -> header.string
strsplit(header.string,split="[\t]") -> prob.string
prob.string[[1]][6] -> prob.string2
strsplit(prob.string2,"[:]") -> prob.string3
prob.string3[[1]][4] -> this.p.val
prob.string[[1]][2] -> name.string
strsplit(name.string,split=",") -> name.string2
name.string2[[1]][1] -> name.string3
motif_name <- paste("motif",motifNum,i,sep="_")
cat("MOTIF",motif_name,name.string3,"\n",file=thisFile, append=TRUE)
cat("letter-probability matrix: ",file=thisFile, append=TRUE)
cat("alength= 4 w=", motif_row, file=thisFile, append=TRUE)
cat(" nsites= 20 ",file=thisFile, append=TRUE)
cat("E= ",file=thisFile, append=TRUE)
cat(this.p.val,"\n",file=thisFile, append=TRUE)
write.table(motif_array,file=thisFile,append=TRUE,col.names=FALSE,row.names=FALSE,sep="\t")
cat("\n",file=thisFile, append=TRUE)
}
(motif.index[n.motifs]+1) -> index.start
total.len -> index.end
motif.file[index.start:index.end] -> this_motif
strsplit(this_motif,split="\t") -> motif_split
#print(head(motif_split))
length(this_motif) -> motif_row
array(NA,c(motif_row,4)) -> motif_array
for (n in 1:motif_row) {
as.numeric(motif_split[[n]]) -> motif_array[n,]
}
motif_array <- as.data.frame(motif_array)
motif.file[motif.index[i]] -> header.string
strsplit(header.string,split="[\t]") -> prob.string
prob.string[[1]][6] -> prob.string2
strsplit(prob.string2,"[:]") -> prob.string3
prob.string3[[1]][4] -> this.p.val
prob.string[[1]][2] -> name.string
strsplit(name.string,split=",") -> name.string2
name.string2[[1]][1] -> name.string3
motif_name <- paste("motif",motifNum,i,sep="_")
cat("MOTIF",motif_name,name.string3,"\n",file=thisFile, append=TRUE)
cat("letter-probability matrix: ",file=thisFile, append=TRUE)
cat("alength= 4 w=", motif_row, file=thisFile, append=TRUE)
cat(" nsites= 20 ",file=thisFile, append=TRUE)
cat("E= ",file=thisFile, append=TRUE)
cat(this.p.val,"\n",file=thisFile, append=TRUE)
write.table(motif_array,file=thisFile,append=TRUE,col.names=FALSE,row.names=FALSE,sep="\t")
cat("\n",file=thisFile, append=TRUE)
sink()
close(thisFile)
print("matrix has been converted to MEME")
}