forked from jmoggridge/bioinfo-notebooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BA1_N - d-neighborhood of a string.py
83 lines (63 loc) · 1.52 KB
/
BA1_N - d-neighborhood of a string.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 20 04:35:37 2019
@author: jasonmoggridge
Generate the d-Neighborhood of a String :
The d-neighborhood Neighbors(Pattern, d)
is the set of all k-mers whose Hamming
distance from Pattern does not exceed d.
Generate the d-Neighborhood of a String:
Find all the neighbors of a pattern.
Given: A DNA string Pattern and an integer d.
Return: The collection of strings Neighbors(Pattern, d).
Sample Dataset
ACG
1
Sample Output
CCG
TCG
GCG
AAG
ATG
AGG
ACA
ACC
ACT
ACG
"""
###
def HammondD(p,q):
if (p,q) in memo_H.keys():
return memo_H[(p,q)]
elif (q,p) in memo_H.keys():
return memo_H[(q,p)]
else:
H=0
for i in range(len(p)):
if p[i]!=q[i]:
H +=1
memo_H[(p,q)] = H
memo_H[(q,p)] = H
return H
def generate_all_kmers(k):
import itertools
alpha = "ACGT"
kmers = [''.join(x) for x in itertools.product(alpha, repeat=k)]
return kmers
###
#
#f = open('//Users/jasonmoggridge/Desktop/rosalind_ba1n.txt', 'r')
#pattern = str(f.readline().strip())
#d = int(f.readline().strip())
#pattern ='CCAGTCAATG'
#d=1
memo_H ={}
d_neighbors=[]
for kmer in generate_all_kmers(len(pattern)):
if HammondD(kmer, pattern) <= d:
d_neighbors.append(kmer)
f = open('//Users/jasonmoggridge/Desktop/rosalind_op.txt', 'w')
for d in d_neighbors:
f.write(d +'\n')
f.close()