-
Notifications
You must be signed in to change notification settings - Fork 11
/
map_kmer.py
executable file
·171 lines (139 loc) · 5.37 KB
/
map_kmer.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
#!/usr/bin/env python
from optparse import OptionParser
import os, subprocess, tempfile
################################################################################
# map_kmer.py
#
# Map a short sequence using possum.
################################################################################
################################################################################
# main
################################################################################
def main():
usage = 'usage: %prog [options] <kmer>'
parser = OptionParser(usage)
parser.add_option('-g', dest='genome_index', default='%s/indexes/possum/hg19' % os.environ['HG19'])
parser.add_option('-o', dest='possum_out', help='Keep the possum output file as this file name')
(options,args) = parser.parse_args()
if len(args) != 1:
parser.error('Must provide kmer')
else:
kmer = args[0]
# convert the regular expressions to uniform PWMs
motif_kmer = []
for i in range(len(kmer)):
motif_kmer.append({'A':0, 'C':0, 'G':0, 'T':0})
motif_kmer[-1][kmer[i].upper()] += 1
motifs_pwm = {kmer:motif_kmer}
# run possum
possum_fd, possum_file = run_possum(motifs_pwm, options.genome_index, options.possum_out)
# convert output to gff
possum2gff(possum_file)
if options.possum_out == None:
os.close(possum_fd)
os.remove(possum_file)
################################################################################
# read_motif_pwms
#
# Read in the motifs from FIRE output as regular expression motifs and convert
# them to PWMs.
################################################################################
def read_motif_pwms(fire_signif, min_robust):
motifs_re = []
for line in open(fire_signif):
a = line.split()
robust = int(a[4])
if robust >= min_robust:
motifs_re.append(a[0])
motifs_pwm = {}
for mre in motifs_re:
# strip outside '.'s. idk why FIRE adds those.
mre_strip = mre.strip('.')
mpwm = []
i = 0
while i < len(mre_strip):
if mre_strip[i] == '.':
mpwm.append({'A':1, 'C':1, 'G':1, 'T':1})
else:
mpwm.append({'A':0, 'C':0, 'G':0, 'T':0})
if mre_strip[i] == '[':
i += 1
while mre_strip[i] != ']':
mpwm[-1][mre_strip[i]] += 1
i += 1
else:
mpwm[-1][mre_strip[i]] += 1
i += 1
motifs_pwm[mre_strip] = mpwm
return motifs_pwm
################################################################################
# run_possum
################################################################################
def run_possum(motifs_pwm, possum_index, possum_out):
############################################
# print pwm's for possum
pwm_fd, pwm_file = tempfile.mkstemp(dir='%s/research/scratch/temp' % os.environ['HOME'])
pwm_out = open(pwm_file, 'w')
print >> pwm_out, 'BEGIN GROUP'
for motif_re in motifs_pwm:
motif_pwm = motifs_pwm[motif_re]
print >> pwm_out, 'BEGIN INT'
print >> pwm_out, 'ID %s' % motif_re
print >> pwm_out, 'AP DNA'
print >> pwm_out, 'LE %d' % len(motif_pwm)
for i in range(len(motif_pwm)):
line = 'MA'
for nt in ['A','C','G','T']:
line += ' %d' % motif_pwm[i][nt]
print >> pwm_out, line
print >> pwm_out, 'END'
print >> pwm_out, 'END'
pwm_out.close()
############################################
# run possum
if possum_out == None:
possum_fd, possum_file = tempfile.mkstemp(dir='%s/research/scratch/temp' % os.environ['HOME'])
else:
possum_file = possum_out
possum_fd = None
#subprocess.call('possumsearch -pr %s -db %s -freq %s_freqs.txt -lazy -esa -all -pval %f -fn -rc -format tabs > %s' % (pwm_file,possum_index,possum_index,pval,possum_file), shell=True)
subprocess.call('possumsearch -pr %s -db %s -freq %s_freqs.txt -esa -mssth 1 -fn -rc -format tabs > %s' % (pwm_file,possum_index,possum_index,possum_file), shell=True)
# clean
os.close(pwm_fd)
os.remove(pwm_file)
return possum_fd, possum_file
################################################################################
# possum2gff
#
# Convert possum output to gff.
################################################################################
def possum2gff(possum_file):
for line in open(possum_file):
a = line.split('\t')
a[-1] = a[-1].rstrip()
motif_re = a[0]
try:
start = int(a[5])+1
except:
print 'ERROR: cant extract start'
print a
exit(1)
end = start+int(a[6])-1
fnrc = a[7]
try:
seq_id = a[16][:a[16].find('.')]
except:
print 'ERROR: cant extract seq_id'
print a
exit(1)
if fnrc == 'fn':
strand = '+'
else:
strand = '-'
out_a = [seq_id, 'possum', 'motif', str(start), str(end), '.', strand, '.', motif_re]
print '\t'.join(out_a)
################################################################################
# __main__
################################################################################
if __name__ == '__main__':
main()