-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_3_multicore.py
50 lines (44 loc) · 1.56 KB
/
sim_3_multicore.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
import Levenshtein as L
import random as R
from multiprocessing import Pool
def mutate(seq):
# a = alphabet
alphabet = ['A', 'C', 'G', 'T']
# choose a character to mutate
pos = R.randrange(len(seq))
if seq[pos] in alphabet:
alphabet.remove(seq[pos])
ch = R.choice(alphabet)
return seq[:pos] + ch + seq[pos+1:]
def padding(x):
alphabet = ['A', 'C', 'G', 'T']
return ''.join([R.choice(alphabet) for _ in range(x)])
def run_me(arg):
(covid_seq, ratg13_seq) = arg
num_padding = len(covid_seq) - len(ratg13_seq)
ratg13_seq += padding(num_padding)
dist_now = L.distance(covid_seq, ratg13_seq)
current_seq = ratg13_seq
worsening = 0
for iter in range(9000):
if iter % 100 == 0:
print("Iteration %d" % iter)
mutated = mutate(current_seq)
mutated_dist = L.distance(mutated, covid_seq)
if mutated_dist <= dist_now:
dist_now = mutated_dist
current_seq = mutated
else:
worsening += 1
print("The distance is now %d" % dist_now)
print("The final distance is %d units away from COVID 19." % dist_now)
print("In %d iterations, the distance tried to get worse." % worsening)
if __name__ == "__main__":
f = open("genomes/sars_cov_2/sars_cov_2_ref_NC_045512.fasta", "r")
covid_seq = ''.join([x[:-1] for x in f.readlines()[1:]])
f.close()
f = open("genomes/RaTG13_MN996532.fasta", "r")
ratg13_seq = ''.join([x[:-1] for x in f.readlines()[1:]])
f.close()
p = Pool(4)
p.map(run_me, [(covid_seq, ratg13_seq)]*4)