I am very new to python so bear with me, please. I have been asked to create a program. I need to look for occurrences of a pattern (which has to be provided by the user using the keyboard) string inside each of the sequences and if there are occurrences, register the id and the number (count) of occurrences in the sequence.
The data looks like this:
id sequence
1 MVLSEGEWAAVLHVWAKVEADVAAGHGQDILIRLFKS
2 MNIFEMLRIAAGLRLKIYKDTEAAGYYTIGIGHLLTKSPSL
3 MVLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSH
4 MNIFEMLRAAEGAALRLKIYKAADTEGYYTIGIGHLLTKS
5 MVLSAAEGEWQLVLHVWAKVEADVAGHGQDILIRLFK
where the ids are the numbers and the sequence are the sequences below each number. The file is a matrix of (100437 x 2)
This is the code that I have so far:
import re
def proteins_pattern_count(pattern):
with open("proteins.csv", 'r') as proteins:
proteins = proteins.read()
items = re.findall(pattern, proteins)
return len(items)
# Reading the pattern to look for and forcing the input to change the pattern to capital letters.
pattern = input("Please type in the pattern you would like to look for: ").upper()
count = proteins_pattern_count(pattern)
print('The pattern {} appears {} times within the proteins file'.format(pattern, count))
Output that I get:
Please type in the pattern you would like to look for: AA
The pattern AA appears 173372 times within the proteins file
But what I really want is: for instance, if the pattern I am looking for is "AA" then I would like to see a table only with the ids of the sequences that actually have such pattern inside and the number (count) of occurrences in the sequence, like this:
id count
1 2
2 2
4 3
5 1
I think this is very easy to do, but I'm completely new to Python.
Thanks for your support!!