-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotif_ic.py
executable file
·209 lines (194 loc) · 7.92 KB
/
motif_ic.py
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
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
# Author: Philip Machanick
# Modified by Caleb Kibet
from __future__ import print_function
import sys
from math import log
import os
# TODO: Give this function a sensible name
def get_motif_summary_tfid(motif_file, out_file, meme_path, reults_folder):
test = motif_file
motif_file2 = test
found = 0
row = 0
n_rows = 0
tn_rows = 0
entropy = 0
total_entropy = 0
motifs = 0
name = ""
n = 0
raw_dict = {}
#job_no = reults_folder.split("/")[-1]
with open(out_file, "w") as write_out:
# with open(raw_file) as raw_in:
# for line in raw_in:
# raw_dict[line.split()[0]] = line.split()[-1]
out = "Motif_name\tMotif_IC\tAverage_IC\tMotif_length\tMotif_logo\n"
write_out.write(out)
with open(motif_file, "r") as motif_file:
for line in motif_file:
words = line.split()
if found == 0:
if line.startswith("MOTIF"):
# allow for motifs without an alternative name
if len(words) < 3:
words.append("")
name = (words[1])
found = 1
motifs += motifs
entropy = 0
continue
if found == 1:
if line.startswith("letter-probability"):
n_rows = int((line.split("w="))[1].split()[0])
found = 2
continue
if found == 2:
if line == "\n":
continue
else:
check = 0
for val in words:
if float(val) > 0:
check += float(val) * log(float(val))/log(2.0)
entropy += float(val) * log(float(val))/log(2.0)
row += 1
if row >= n_rows:
v = 2*n_rows+entropy
out = '%s\t%f\t%f\t%i\t<img src="/static/files/temp/%s.png" alt="My image" class="img-responsive"/>\n'\
% (name, v, (v/n_rows), n_rows, name)
write_out.write(out)
found = 0
row = 0
total_entropy += (v/n_rows)
mot_list = []
#os.system("mkdir -p %s/temp" % (reults_folder, tf_name))
with open(test) as meme_in:
for line in meme_in:
if line.startswith("MOTIF"):
mot_list.append(line.split()[1])
fold = "%s" % reults_folder
for i in mot_list:
# TODO: Check at this point if data is available in the database, to ensure no repeat discovery
os.system("%s/ceqlogo -i%s %s -Y -f PNG -h3 -w4 -o %s/%s.png" % (meme_path, i, motif_file2, fold, i))
def get_motif_summary(motif_file, raw_file, tf_name, out_file, meme_path, reults_folder):
test = motif_file
motif_file2 = test
found = 0
row = 0
n_rows = 0
tn_rows = 0
entropy = 0
total_entropy = 0
motifs = 0
name = ""
n = 0
raw_dict = {}
job_no = reults_folder.split("/")[-1]
with open(out_file, "w") as write_out:
with open(raw_file) as raw_in:
for line in raw_in:
raw_dict[line.split()[0]] = line.split()[-1]
out = "Motif_name\tMotif_IC\tAverage_IC\tMotif_length\tMotif_score\tMotif_logo\n"
write_out.write(out)
with open(motif_file, "r") as motif_file:
for line in motif_file:
words = line.split()
if found == 0:
if line.startswith("MOTIF"):
# allow for motifs without an alternative name
if len(words) < 3:
words.append("")
name = (words[1])
found = 1
motifs += motifs
entropy = 0
continue
if found == 1:
if line.startswith("letter-probability"):
n_rows = int((line.split("w="))[1].split()[0])
found = 2
continue
if found == 2:
if line == "\n":
continue
else:
check = 0
for val in words:
if float(val) > 0:
check += float(val) * log(float(val))/log(2.0)
entropy += float(val) * log(float(val))/log(2.0)
row += 1
if row >= n_rows:
v = 2*n_rows+entropy
out = '%s\t%f\t%f\t%i\t%f\t<img src="/static/files/compare/%s/%s/motifs/%s.png" alt="My image" class="img-responsive"/>\n'\
% (name, v, (v/n_rows), n_rows, float(raw_dict[name]), job_no, tf_name, name)
write_out.write(out)
found = 0
row = 0
total_entropy += (v/n_rows)
mot_list = []
os.system("mkdir -p %s/%s/motifs" % (reults_folder, tf_name))
with open(test) as meme_in:
for line in meme_in:
if line.startswith("MOTIF"):
mot_list.append(line.split()[1])
fold = "%s/%s/motifs" % (reults_folder, tf_name)
for i in mot_list:
os.system("%s/ceqlogo -i%s %s -Y -f PNG -h3 -w4 -o %s/%s.png" % (meme_path, i, motif_file2, fold, i))
def motif_summary(motif_file, raw_file, out_file):
found = 0
row = 0
n_rows = 0
entropy = 0
total_entropy = 0
motifs = 0
name = ""
raw_dict = {}
with open(out_file, "w") as write_out:
with open(raw_file) as raw_in:
for line in raw_in:
raw_dict[line.split()[0]] = line.split()[1:5]
out = "Motif_name\tMotif_IC\tAverage_IC\tMotif_length\t%s\t%s\t%s\t%s\n" % \
(raw_dict["Motif"][0], raw_dict["Motif"][1], raw_dict["Motif"][2], raw_dict["Motif"][3])
write_out.write(out)
with open(motif_file, "r") as motif_file:
for line in motif_file:
words = line.split()
if found == 0:
if line.startswith("MOTIF"):
# allow for motifs without an alternative name
if len(words) < 3:
words.append("")
name = (words[1])
found = 1
motifs += motifs
entropy = 0
continue
if found == 1:
if line.startswith("letter-probability"):
n_rows = int((line.split("w="))[1].split()[0])
found = 2
continue
if found == 2:
if line == "\n":
continue
else:
check = 0
for val in words:
if float(val) > 0:
check += float(val) * log(float(val))/log(2.0)
entropy += float(val) * log(float(val))/log(2.0)
row += 1
if row >= n_rows:
v = 2*n_rows+entropy
out = '%s\t%f\t%f\t%i\t%f\t%f\t%f\t%f\n'\
% (name, v, (v/n_rows), n_rows, float(raw_dict[name][0]), float(raw_dict[name][1]),
float(raw_dict[name][2]), float(raw_dict[name][3]))
write_out.write(out)
#n+= 1
#print(n)
found = 0
row = 0
total_entropy += (v/n_rows)