-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
151 lines (131 loc) · 2.96 KB
/
tools.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
import gzip
import io
import math
import sys
####################
# Global Constants #
####################
NT2RE = {
'A': 'A',
'C': 'C',
'G': 'G',
'T': 'T',
'R': '[AG]',
'Y': '[CT]',
'M': '[AC]',
'K': '[GT]',
'W': '[AT]',
'S': '[CG]',
'B': '[CGT]',
'D': '[AGT]',
'H': '[ACT]',
'V': '[ACG]',
'N': '[ACGT]',
}
##################
# File Utilities #
##################
def get_filepointer(thing):
if (type(thing) == str):
if thing == '-': return sys.stdin
elif thing.endswith('.gz'): return gzip.open(thing, 'rt')
else: return open(thing)
elif (type(thing)) == io.StringIO: return thing
elif (type(thing)) == io.TextIOWrapper: return thing
else: raise ValueError('unknown thing')
def read_fasta(input):
fp = get_filepointer(input)
name = None
seqs = []
for line in fp:
line = line.rstrip()
if line.startswith('>'):
if len(seqs) > 0:
yield(name, ''.join(seqs))
name = line[1:]
seqs = []
else:
name = line[1:]
else:
seqs.append(line)
yield(name, ''.join(seqs))
fp.close()
##################
# Math Utilities #
##################
def check_probs(vals, tol=1e-3):
for val in vals: assert(val >= 0 and val <= 1)
assert(math.isclose(1.0, sum(vals), abs_tol=tol))
def entropy(vals, check=True):
if check: check_probs(vals)
h = 0
for val in vals:
if val != 0: h -= val * math.log2(val)
return h
def dl1(ps, qs, check=True):
# Manhattan, Taxicab, City Block
if check:
check_probs(ps)
check_probs(qs)
d = 0
for p, q, in zip(ps, qs):
d += abs(p - q)
return d
def dl2(ps, qs, check=True):
# Euclidean
if check:
check_probs(ps)
check_probs(qs)
d = 0
for p, q in zip(ps, qs):
d += (p - q) ** 2
return d ** 0.5
def dkl(ps, qs, check=True):
# Kullback-Leibler - not recommended
if check:
check_probs(ps)
check_probs(qs)
for p in ps: assert(p != 0)
for q in qs: assert(q != 0)
d = 0
for p, q in zip(ps, qs):
d += p * math.log2(p/q)
return d
######################
# Sequence Utilities #
######################
def anti(seq):
anti = ''
for nt in seq[::-1]:
if nt == 'A': anti += 'T'
elif nt == 'C': anti += 'G'
elif nt == 'G': anti += 'C'
elif nt == 'T': anti += 'A'
elif nt == 'R': anti += 'Y'
elif nt == 'Y': anti += 'R'
elif nt == 'M': anti += 'K'
elif nt == 'K': anti += 'M'
elif nt == 'W': anti += 'W'
elif nt == 'S': anti += 'S'
elif nt == 'B': anti += 'V'
elif nt == 'D': anti += 'H'
elif nt == 'H': anti += 'D'
elif nt == 'V': anti += 'B'
elif nt == 'N': anti += 'N'
elif nt == 'a': anti += 't'
elif nt == 'c': anti += 'g'
elif nt == 'g': anti += 'c'
elif nt == 't': anti += 'a'
elif nt == 'r': anti += 'y'
elif nt == 'y': anti += 'r'
elif nt == 'm': anti += 'k'
elif nt == 'k': anti += 'm'
elif nt == 'w': anti += 'w'
elif nt == 's': anti += 's'
elif nt == 'b': anti += 'v'
elif nt == 'd': anti += 'h'
elif nt == 'h': anti += 'd'
elif nt == 'v': anti += 'b'
elif nt == 'n': anti += 'n'
else: raise
return anti