-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB01_PatternCount.py
More file actions
executable file
·38 lines (35 loc) · 1.16 KB
/
B01_PatternCount.py
File metadata and controls
executable file
·38 lines (35 loc) · 1.16 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
#
# PatternCount
# For "Finding Hidden Messages in DNA (Bioinformatics I)" by UC San Diego
# Copyright (c) 2024 Mariusz Matusiak
#
import sys
def PatternCountAndPosition(Text, Pattern):
#return Text.count(Pattern) #<- doesn't work as it does not count overlaps
count = 0
pos = 0
positions = []
while pos < len(Text):
nextOccurence = Text.find(Pattern, pos)
if nextOccurence != -1:
positions.append(str(nextOccurence))
count = count+1
pos = nextOccurence + 1
else:
break
return (count, positions)
def main():
file = sys.argv[1] if len(sys.argv) > 1 else "TestData/PatternMatching/inputs/Vibrio_cholerae.txt"
with open (file, "r") as inputFile:
fileContent = inputFile.readlines()
Text = fileContent[0].strip()
# Text = "ATGACTTCGCTGTTACGCGC"
print(Text)
Pattern = fileContent[1].strip()
# Pattern = "CGC"
print(Pattern)
count, positions = PatternCountAndPosition(Text, Pattern)
print("Occurences: {}".format(count))
print("Positions: {}".format(" ".join(positions)))
if __name__ == "__main__":
main()