-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathep5 ProfileMostProbableKmer.py
More file actions
60 lines (48 loc) · 1.86 KB
/
Copy pathep5 ProfileMostProbableKmer.py
File metadata and controls
60 lines (48 loc) · 1.86 KB
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
def Pr(Text, Profile):
"""
Calculate the probability of a given k-mer Text based on a profile matrix.
Args:
Text (str): A k-mer (substring of DNA sequence).
Profile (dict): A profile matrix where keys are nucleotides and values
are lists of probabilities for each position.
Returns:
float: Probability of Text being generated by Profile.
"""
p = 1 # Initialize probability
for i in range(len(Text)):
p *= Profile[Text[i]][i] # Multiply probabilities for each nucleotide
return p
def ProfileMostProbableKmer(Text, k, Profile):
"""
Find the most probable k-mer in Text based on a given profile matrix.
Args:
Text (str): DNA sequence.
k (int): Length of the k-mer.
Profile (dict): A profile matrix where keys are nucleotides and values
are lists of probabilities for each position.
Returns:
str: The most probable k-mer in Text based on Profile.
"""
max_probability = -1 # Initialize max probability as a very low value
most_probable_kmer = '' # Initialize the most probable k-mer
# Slide through the Text to consider all possible k-mers
for i in range(len(Text) - k + 1):
kmer = Text[i:i + k] # Extract k-mer
probability = Pr(kmer, Profile) # Calculate its probability
# Update the most probable k-mer if a higher probability is found
if probability > max_probability:
max_probability = probability
most_probable_kmer = kmer
return most_probable_kmer
# Example Input
Text = "ACGTACGTGACG"
k = 3
Profile = {
'A': [0.2, 0.4, 0.3],
'C': [0.4, 0.3, 0.1],
'G': [0.3, 0.1, 0.5],
'T': [0.1, 0.2, 0.1]
}
# Find and display the most probable k-mer
result = ProfileMostProbableKmer(Text, k, Profile)
print("Most Probable k-mer:", result)