-
Notifications
You must be signed in to change notification settings - Fork 3
/
BA2G.py
154 lines (139 loc) · 9.42 KB
/
BA2G.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
import random
def patternToNumber(pattern):
if len(pattern) == 0:
return 0
return 4 * patternToNumber(pattern[0:-1]) + symbolToNumber(pattern[-1:])
def symbolToNumber(symbol):
if symbol == "A":
return 0
if symbol == "C":
return 1
if symbol == "G":
return 2
if symbol == "T":
return 3
def numberToPattern(x, k):
if k == 1:
return numberToSymbol(x)
return numberToPattern(x // 4, k-1) + numberToSymbol(x % 4)
def numberToSymbol(x):
if x == 0:
return "A"
if x == 1:
return "C"
if x == 2:
return "G"
if x == 3:
return "T"
def profileProbable(text, k, profile):
maxprob = 0
kmer = text[0:k]
for i in range(0,len(text) - k +1):
prob = 1
pattern = text[i:i+k]
for j in range(k):
l = symbolToNumber(pattern[j])
prob *= profile[l][j]
if prob > maxprob:
maxprob = prob
kmer = pattern
return kmer
def profileRandom(k, profile, text):
probs = []
for i in range(0,len(text) - k +1):
prob = 1.0
pattern = text[i:i+k]
for j in range(k):
l = symbolToNumber(pattern[j])
prob *= profile[l][j]
probs.append(prob)
r = myRandom(probs)
return r
def hammingDistance(p, q):
ham = 0
for x, y in zip(p, q):
if x != y:
ham += 1
return ham
def distanceBetweenPatternAndString(pattern, dna):
k = len(pattern)
distance = 0
for x in dna:
hamming = k+1
for i in range(len(x) - k + 1):
z = hammingDistance(pattern, x[i:i+k])
if hamming > z:
hamming = z
distance += hamming
return distance
def profileForm(motifs):
k = len(motifs[0])
profile = [[1 for i in range(k)] for j in range(4)]
for x in motifs:
for i in range(len(x)):
j = symbolToNumber(x[i])
profile[j][i] += 1
for x in profile:
for i in range(len(x)):
x[i] = x[i]/len(motifs)
return profile
def consensus(profile):
str = ""
for i in range(len(profile[0])):
max = 0
loc = 0
for j in range(4):
if profile[j][i] > max:
loc = j
max = profile[j][i]
str+=numberToSymbol(loc)
return str
def score(motifs):
profile = profileForm(motifs)
cons = consensus(profile)
score = 0
for x in motifs:
for i in range(len(x)):
if cons[i] != x[i]:
score += 1
return score
def myRandom(dist):
s = 0.0
for x in dist:
s+= x
i = random.random()
partial = 0.0
for x in range(len(dist)):
partial += dist[x]
if partial/s >= i:
return x
def gibbsSampler(dna, k, t, n):
bestMotifs = []
motifs = []
for x in range(t):
i = random.randint(0, len(dna[x])-k)
motifs.append(dna[x][i:i+k])
bestMotifs = motifs[:]
for i in range(n):
j = random.randint(0,t-1)
profile = profileForm(motifs[:j] + motifs[j+1:])
r = profileRandom(k, profile, dna[j])
motifs[j] = dna[j][r:r+k]
if score(motifs) < score(bestMotifs):
bestMotifs = motifs[:]
return bestMotifs
k = 15
t = 20
n = 2000
dna = ["CATCATGTGTGAGGTCTGAACCCGCACCTAAGATTTAGCGAAGATCGCATTACCGCTGCGAGGGCAACACTTTACGCAGAAACACTCACGACTGGCGATCACCCTGGTATTAAAGAGTGAATCTCCTGGCTGCAATATGTCTTGGCCTTGTCGCGGGCTATCAAGCGCAAATCCAGGCCAGTCGTTTGATTCCAGGGAGCTACCGCTGTCACTAACCGTAACTCCTCTCGGGCTCCAAGTAATATGAACGTGCGCCGAGGGGATTCCTAATTGGTTAATCGTTAATATTCGGCACGGAGGTAAGCATCATGTGTGAGGT", "CTGAACCCGCACCTAAGATTTAGCGAAGATCGCATTACCGCTGCGAGGGCAACACTTTACGCAGAAACACTCACGACTGGCGATCACCCTGGTATTAAAGAGTGAATCTCCTGGCTGCAATATGTCTTGGCCTTGTCGCGGGCTATCAAGCGCAAATCCAGGCCAGTCGTTTGATTCCAGGGAGCTACCGCTGTCACTAACCGTAACTCCTCTCGGGCTCCAAGTAATATGAACGTGCGCCGAGGGGATTCCTAATTGGTTAATCGTTAATATTCGGACACAGTGTATTACGCACGGAGGTAAGCATCATGTGTGAGGT", "GCCATATTTGTACTTAGGGTACATATGCAGTAGCTACCAGCTCCGCTGCCTAACCGCTACTACAAGGCGCTGCAGTCGATTACGGTAGACCGATGAGGCAGCCCTCTCTCGTGAGTCGCAATAAAGACCTGCAAGTTAGAAACATTCGCGGCCCCCGGAGGGTTGGTAGACCCTAGAACAACTTTCCAAATGATCCGAATGCATGGCAAACATTGTCACATCTATACTGTTTCAGAGTAAACGTAGTTATAGTACCCAGAATTTAGCGAGGGTGGGTCTCGGTACCACAGAGGGTATTACGGGAAATACGTTCTAGATG", "TGTTACGAGACGCACTACTAGGCCGTACCAGGGGACGCCTAGTAAAGACGAGAGTTCAGATTTAGGAACGATTCAGGTAGCGTAATGTTCCACGTTCATGCATACTTCCCTAGAACGATGACTCTGTAGCGAGTTTACACGACGGCTTATCCGGAGCGGCCCCGAAGTCAGCCGTCAGGCTGTATTAGTCGGCCTGCTAACCGGGCATTCCTACCGGCGGTTTCATATGAATTTCGGCGATTAGGTTCCTTAGCCAGATCGGCTTGCACCTAATTATCCGAAAGGACGATATACAGGACAGCTTGTATGTATACCATGT", "GAAGAATACCTGCACTCAGCCCACGAGAAATTCACCGCCGCTACTAGCTGTTGTTCCTACCTCGGCACTAAAACCTTTCGATCACTTGCCACTGCCTTTGATTCGCAGGGGACTGGAGCACGTGCCTTAAGGGAACGCGTGTCAACAGGGATATCCCAAGCTCCGAGATGACACTTTTTCACCGCTTGTTTAGGGGTCGGGGCCTTGTCCACGCCTATCTAGTATTGTTAAAGCTGATAAGCACTATCCAACTCTCGGCGCCAGCATGACCTGGTATCGTCTCGGTGACAGGCTGTGCAACGCTATCCTGTTTGTGACA", "ACTTTACCTCGTAAAGAGCATGCGCGCACTGGGTTACGTGCTTACGTGGTAGTCACGTGTTGGTACAGTTGTATGCACCGCGTAGAGTGATTACGTCGGTCCTTGTGTTGAAGGGATACGCCTGGCTTGACCAGACCTAATACAGGCTGGTGTACGACACCCACCTTAAGTCCAGTAACCCGTGTTTGCCAACTGCCCTATGTTTGTGCATCGACCGGGTGAGTAAAATAAGACTGCGTCAGTCACCACGAAGACATTCGTGATGAGGGCTATGGACAAAGGGCCAGCTAAGCAGGGATCCGTCCTCCACAGTTGGAAG", "AGTAGCAACACTGATACGGGATAGTTACGCAGCAAGGCCGAAAACGCATGGCTTACGTTGGAATGGGAGGTTGGTGTACTTGACCCACGGGCAATAAGTTGCGAATCTTGCTTTTTGAACGTACTGTATTACGAAGCAATTACTCATGCAGCGCGTGTGCGAGTTACTCACGAACCCTAGTCTCAAAGCTAGGGCCAGCTGGGCTAAACCCGGTATGGAGAACCCCTGCATTAAGCTGGCTGTTGCGATAGAGCGTCCGCTAGACCGGTTTTTCCACTTGCTCCCCCGTCGGCTGCTATGAATAAACTACTTGCGTGGG", "TGCACTCACTGGGCCCGTCTTCTGTTAGGGTGACAACCCCTCGAATTGTTGCAATTGAGCTTTCGTTTCTTGCAAGAGCCCGCTGGCTTTGAGGGTCTAGTTACTATCAACCCACAGGCAATGCCCGCGCGTGGCTGTATTACGAATGGCTCGCACATTCGGCTCCACGCTTGTTTGGAGCTCCTGGTAATTTCGCTTCAACGGTTCGTCGGCCCTCAGCTTTGATTCGCAGAGCCCTGCGCATCTGCCCAATTACCGAACTCGTGCACTTGAACTAGATCGGACAAAGCAAGTCTTGACCAGCTAACGGAGTACAGGT", "CTACAGATACGATCAAAGCCGACTTTGGATATTGCTTCGATGACCCAAAAAACTCGCAGTTGGCGATTAGGCATGGCATCTGAACGATTGTAGACCTAGACACTGGTCCCACGACCTGCTATGCACCGCGGACCGCCGTCGAGAACTGCTGTAACGGGGGAAAGCGAATCCGAAAGGACTCATGCCAAACTGGACGGGGCTAGTATCATACTCCTACGAATCCATGACCTCCAGAACAACAGAGGGTATTACGCATTTGTTAAAAACACGTCACAGATGGTGGGCAATACAGCTTGATAGCAACCTGGTCAGTGAGCCT", "GTAAGTTATAACATACCGGTACCGTAGTGTGTATTTATCATTGAGTATTCGTTGTCAACGATTCCTCCCTTGTGCTCTTTTGTTACGTTAGTGCAGTGTAACCCAGAGATTTATCGGACACGCTTTAAAGAATACCCTGGCATATGGCGATAGTTATCTTAACTGAACACATAAACGTAGCGTAGCCGGAAAACACTAGAGACTGACCGGCTAACATAGCAATGCACACATATCTAATCCGGAGTGAACAGGAGTTATTACGTGTGATTGCTGCCAGACAGTGTAAGATTATCCGACTGTAACACACAGCCGTTCGCCC", "GTCCTTCGGGTGGCTGTACGCGCCGAACCCTACTGCACGCATGGCTTAGTGTAAATCCCCTAGTGGCGTGTGCGTGACACTATTGGATACCACAACGACAGGTCCTATTACGCACGAAACAATTGGGAGACAGTGCACGCGTCACGGCCATCCAATTTTCTGCAGGCCCTCCTGAGGACATCTAATACGAACCCTCAGGAGCCCACTTCCTTGGTAAGGAACACTCTACTAATACAGCTCTTTGCATGTCGCACTATCTAGATACAAAGCCTCGATAAACGCCTCGGAAGATGACACCGCGCAGGTTCCCCTCTCCCCA", "TTGTCTCTCAGAGTTTATTCTTGGGCTACCCTCTTGCCACAGTTCCTTCGTTGTTGTGAGCTGGGGTTCAAGTGACGATCCGACGCTAGATTGGACGCGTTTCACCCGTTCACTAGCAGCGCAGAGTTAGTGCAAATTATGGTACCGCTAGATATGCTCGCCGGTGGGATACTTGACAGTGAGGACCCCGGCACTCGATAGCCATAAGACACATCGTGTATTACGGGCCGGAGAGTCTATTCCCGAGGCGTTTTCCTGATTAAGGATGTATATTGAGACGCGCCAGGAGCTCGCACTCGAAGCTGCCGGACCAGGTCCC", "GTCACGTCAATTTGAATGTCTCTCTGGCCGCCCGACAAAGCAAGAAGTATAGTAGTGGTGTGCCGTAGACCGCCTCCACATTCGGCACGACTTACTTAACTGCTACCATAATGAGCCACACACATGGAGGTCAGACGCGCATTTGTTAGACCAAGTTTTGATCTGATTGTCTTTATCTTCCCCTCCGACGCCACATTCTATGCCTTGAGCGCCTCACTGCAAGTTCCAAGTGAAAATTGGTCACTTCGTCGAAGTTAAATTGAGACCTCCATGCGGCGAAACCTAACAGGCACGATTACGTCGCGCGTTGTCTATGCCG", "CCACGGATCTCAATCCTGTGTGTATGCGATGAGACGAGCAGCGAGTTCGCGTATACAGGTGGTGTGAGCGCGGTAAAGGAGTCTCCTTCATGGCTTACTGTGGGGAACGCATTATCTCCTTTGGGACCGAATTCGTAAGTAGGTCTGCACTGCGTTAACGGAGGCTACTTGCAATTACTTGGTTACAGGCTACGTTACGAGTTACATTACTCGTCCCTGAGGCTTTGGGCTGACTTACGACGCACCAAGGGGGTAAGAAAAGCGAGTTATTGCCATGCGCGGGATCCAATTCGGCTGGCTTGCGGATATGTTTGGAATG", "AACGCAACTAATTGCTCCGTTTATCTACAGCGGCGGGCCCCTAGTATACTGATTATGACATAAGCCATAGCCGCCCGGCAGATCCATAACTAGCATTTCACCCCATTTTCGAAGCCCGACGTGGCCTGAAAACCTCTACAACAAGCTTGTGGTCTACAGGCGCAATTACGTTTGTGGGTATACGTAGGACCGTCGGAACCCGGCTTTAACCACGGCCGTCGGATGTCAGCATATCAGTGCTTATGTGGATACCGCTGTCGGATGATGGTGGAACCTATGCATTGCTAAAAACTCGTCCGCGGATATCCCATCCTGCGCG", "TGGGAAGGTGATTCTAACCTTCGGGGCTCTGTTAAAAACTACCCTATGGCCTGCTCGAAGAGTTCGGCGGTCATCATTTGATACCTCGCGTATAGTTAGCGATTTTGGCAATGTATCCCTTGAGGTGTATAAGGCTGTATTACCGTCCGATCCCACACTTTGCACGAGCCCCGACATCACGACACTACGCAAGTTCAAATTATTGAGGATCGATGGATTAGAATGTCGTGACCTTTTACTAGCATAACCACGATCCCACTAGGTCGAGGGGGAACGTCCGCCCGACGACACTGCGGGTCAATAAAGTTGATGTGTATCG", "TGCAGGGCCGCCGCACCTTTACGGCAGGTCTTGAATTGACTTAGATACTAACATGGCTTGTAGTTCTGGTTCATGGGCAATACTTAAGGGTTTATGTTATTATCACTATAGCATACAGGTGCAGACTTCCACATAAAAAATTAGTCCGGGTAAGATACCAGGTAGGTTTAGGCCCTCCTCTGCGCCGATTGACCCGCCAATTTGGTGTACAGGTGACTTTTACCGTCATAAGTTCCTGGAACGTTTGGGAAGTGTCTAAGGCACTTCCACGCGTGGCGCCATTAGATTCATTAACAGGCTGTATTGTCTCAATCTTCTG", "AATGACAAATGATAGTTCCTGCCAATGTGGAGGGTAGTTCAGCAGAAAGAAACAAGGGTACATTCTATCGACCTGGTGACGCTATACATGCCTCATGCTGGTAAGCAAGGCGCACGCTGCGATGTCCCTTTACAGTGCATCACGGCTCTCTCTCGTCTCGATATAGCCACAGGCTGTAGGGCGCGGATTCACTGTATTTTGCGAACCGCAAATCCGACAGGTAAGAAACCAACTGGCTCTCCCCTCGTCAACCTAACCGGGGAGTTGCGATATGCTTTACGCCCGGCTTAGACGGAGGACGTCCGGGTGTAGGGGACAC", "GCAATTGCATGCCAGAAGTGTACAACGCCAAACAATTAGACCAGTGGTTCAGCCGGGTAACACGCTCGTCGAGCCCATTCGTTTCGTCATGAGAGACCCTATGCTACATACTTTGGAGGAATCGAGTTACAGGCTGTATAGTGTAGGTCTGCGTCCAGTTAGCTGCAAGAAAACTTGCACATGCACCCCCCGTCAGCCCAACCGGCTTGGCCGCCCGTGGTGCACGGAAAGTGGACTGTCTCCTACCGTCAGCGTAAACCATTTCCTCCGCTCACCAGTGCATTTCTGTCGTTGAGTCGTACTCAAGTGGCAATAAAAG", "TCTTGTGTGTTCGGGTACTGCATATGGACCATATAGAGCCCGATGGCGCGTCAACTTCAGGAGTCACCGGATGGGGGCACTCCACCACTCTGCAGTTTTGTGCGTCTGACATAAGTTGGTTAGGCAGACACCTCCAAAGAGGTCTCACCACCCAGCACGCGACCGTGAATTATACAAGAAGTCCCGACTGGCGCGATGTGCTGTATTACGTTCGGCCATACTCTCCGTATGGTACCGATGATCTAAGGTTACGGCAGTGATGCTACGAATACGTGCACACGGAACCTGCACACTGGAGCGTTTTGCTTTCGGTTACGAA"]
best = gibbsSampler(dna, k, t, n)
s = score(best)
print(s)
for x in range(20):
sample = gibbsSampler(dna, k, t, n)
print(score(sample))
if score(sample) < s:
s = score(sample)
best = sample[:]
for b in best:
print(b)