I'm using a function where I need the start and end indices of the matched uppercase letters. But one thing to keep in mind is that if there is any '-' in any of the strings we need to skip the index from counting. For example:
str1 = 'ATCGATCGATCG------ATCGATCG'
str2 = 'CGcgCGCGCGCG---CGCCGCGcgCG'
Here, actually, the length of the string should be counted:
str1: 20 (instead of 26)
and str2: 23 (instead of 26).
Here the start and end indices should be:
matched_str: 'CG', 'CG', 'CG', 'CG'
start = [6, 10, 14, 18]
end= [7, 11, 18, 22]
Now I have got the following function which doesn't skip the indices while encountering '-' in any of the two strings. How can I modify the following code to do that?
str1 = 'ggtacTGAGGTCCCCTGGGTACTGAGATCTCCTCGGTACTGAAGTCTCCTCGGTGCTGAGGTCGCCTCGGTGCTGAGACCTCCTAGGTATTGAGGTCGCCTCGGTACTGAGGTTGCCTC----------------------------GGTGCTGAGGT-----CGCCACGGTGCTGAGACCTCCTAGATACTGAGG----TCTCCTAGGCACGGAGATCTCCTATGTACAGAGACCTCGTCGGTACTGAGGTCGCCTAGGTACTGAGACCTTCTAGGTCCTGAGGT--------CTAGGTACTGAGG-CCTTCTCC\n'
str2 = 'GATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTT-CCTCTCCCGGGATGCTGAGGTTCCTCTCCCGGGATGCTGAGGTTCCTCTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCTCTCCCAG---------------------------------GATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCTCTCCCAGGATGCTGAGGTTCCTCTCC\n'
matches = []
for i,(letter1, letter2) in enumerate(zip(str1,str2)):#i=index, letter1=str1, letter2=str2
# print(letter1, letter2)
if ((letter1 == letter2) and
letter1 in ['A','T','C','G'] and letter2 in ['A','T','C','G']):
if (not matches or matches[-1][1] != i-1):
matches.append([i,i])
else:
matches[-1][1] = 1
start = [k[0] for k in matches]
end = [k[1] for k in matches]
print(start, end)
CodePudding user response:
I would actually suggest just doing some preprocessing of the string
str1 = 'ggtacTGAGGTCCCCTGGGTACTGAGATCTCCTCGGTACTGAAGTCTCCTCGGTGCTGAGGTCGCCTCGGTGCTGAGACCTCCTAGGTATTGAGGTCGCCTCGGTACTGAGGTTGCCTC----------------------------GGTGCTGAGGT-----CGCCACGGTGCTGAGACCTCCTAGATACTGAGG----TCTCCTAGGCACGGAGATCTCCTATGTACAGAGACCTCGTCGGTACTGAGGTCGCCTAGGTACTGAGACCTTCTAGGTCCTGAGGT--------CTAGGTACTGAGG-CCTTCTCC'
str2 = 'GATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTT-CCTCTCCCGGGATGCTGAGGTTCCTCTCCCGGGATGCTGAGGTTCCTCTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCTCTCCCAG---------------------------------GATGCTGAGGTTCCCAGGATGCTGAGGTTCCCAGGATGCTGAGGTTCCTCTCCCAGGATGCTGAGGTTCCTCTCC'
str1 = list(str1)
str2 = list(str2)
shortest_len = min(len(str1), len(str2))
for i in range(shortest_len):
if str1[i] == '-' or str2[i] == '-':
str1[i] = '-'
str2[i] = '-'
str1 = '-'.join(str1).replace('-','')
str2 = '-'.join(str2).replace('-','')
This simply makes sures that both strings will have '-' at the exact same indices, you can then just remove them from the string.
CodePudding user response:
Rather than Modifying your code I would instead just add few lines to prepare your strings before getting into the code you've written.
let's say we have these two strings:
str1 = GCGC----ATGC
str2 = GC----AgcTGG
In this case if there's '-' in one item of the two strings, just delete the '-' and the letter that has the same index as the deleted '-' In that case you would have:
str1_preprocessed = GCATGC (GCGC----ATGC)
str2_preprocessed = GCcTGG (GC----AgcTGG)
Then you would have your strings ready for your function.
CodePudding user response:
The answer will be the following function. I used exclusive1 and exclusive2 variables to count the indices of the str1 and str2 respectively by skipping '-'. But I kept the length of the 2 strings the same. Then I used the 3rd 'for loop' for counting the start and end indices (considering the original string including '-') of the matched strings. Then I used these start and end indices as the indices of the exclusive1 and exclusive2 variables.
def matched_indices():
str1 = 'GGTGGCGGGGACGGACTCGTGGTGACATGTCCGCGTGTGTGCTTTGCTCTCAGGGAGCCACGACACGATGACGTACTGCCTGAACAAGAAGTCCCCCATTTCGCACGAGGAGTCCCGGCTGCTGCAGCTGCTGAACAAGGCCTTGCCCTGCATCACGCGCCCTGTCGTGCTGAAATGGTCCGTCACCCAGGTACGG\n'
str2 = 'GGCGTCAGGGGCGG---------GATATGACCG-GTGACTTGCGTGTCCCCAGGGAGCCACGACACGATGACATACTGCCTGAACCGTAAGTCTCGGATCTCCCGTGCAAGTTCGTGGCTACTGCACCTGTTGGGCCGGGTCGTGCCATTCATCACCGGGCCCGTGGTGATGAAATGGTCGGTCACACAGGTGAGG\n'
exclusive1 = [] #new indices skipping indels for 1st sequence
exclusive2 = [] #new indices skipping indels for 2nd sequence
count=0
for idx, val in enumerate(str1):
if (val not in ['-', '\n', ' ', '-', '.']) and (idx!=len(str1)-1) and (str1[idx 1] not in ['-', '\n', ' ', '-', '.']): #str is not '-' and not the last idx and next is not '-'
exclusive1.append(count)
count =1
elif (val not in ['-', '\n', ' ', '-', '.']) and (idx==len(str1)-1): #str is not '-' and the last idx
exclusive1.append(count)
elif (val in ['-', '\n', ' ', '-', '.']) and (idx!=len(str1)-1) and (str1[idx 1] not in ['-', '\n', ' ', '-', '.']): #str is '-' and not the last idx and next is not '-'
exclusive1.append(count)
count =1
else: #str is '-'
exclusive1.append(count)
count=0
for idx, val in enumerate(str2):
if (val not in ['-', '\n', ' ', '-', '.']) and (idx!=len(str2)-1) and (str2[idx 1] not in ['-', '\n', ' ', '-', '.']):
exclusive2.append(count)
count =1
elif (val not in ['-', '\n', ' ', '-', '.']) and (idx==len(str2)-1):
exclusive2.append(count)
elif (val in ['-', '\n', ' ', '-', '.']) and (idx!=len(str2)-1) and (str2[idx 1] not in ['-', '\n', ' ', '-', '.']):
exclusive2.append(count)
count =1
else:
exclusive2.append(count)
matches = []
for i,(letter1, letter2) in enumerate(zip(str1,str2)):#i=index, letter1=str1, letter2=str2
if ((letter1 == letter2) and (letter1 in ['A','T','C','G']) and (letter2 in ['A','T','C','G'])):
if not matches or matches[-1][1] != i-1:
matches.append([i,i])
else:
matches[-1][1] = 1
start = [k[0] for k in matches]
end = [k[1] for k in matches]
first_start = [exclusive1[i] for i in start]
second_end = [exclusive2[i] for i in end]
first_end = [exclusive1[i] for i in end]
second_start = [exclusive2[i] for i in start]
return first_start, first_end, second_start, second_end
first_start = []
first_end = []
second_start = []
second_end = []
for _ in starmap(list.extend, zip([first_start, first_end, second_start, second_end], matched_indices())):
pass
print(first_start, first_end, second_start, second_end)
first_start [0, 3, 5, 7, 11, 23, 26, 30, 34, 39, 44, 48, 50, 73, 88, 94, 97, 100, 103, 106, 112, 116, 121, 127, 131, 135, 138, 141, 143, 148, 150, 158, 160, 163, 166, 170, 181, 187, 194]
first_end [1, 3, 5, 9, 13, 24, 28, 32, 36, 39, 45, 48, 71, 84, 92, 94, 98, 101, 103, 106, 113, 119, 125, 129, 132, 135, 139, 141, 146, 148, 155, 158, 161, 164, 168, 179, 185, 191, 195]
second_start [0, 3, 5, 7, 11, 14, 17, 21, 24, 29, 34, 38, 40, 63, 78, 84, 87, 90, 93, 96, 102, 106, 111, 117, 121, 125, 128, 131, 133, 138, 140, 148, 150, 153, 156, 160, 171, 177, 184]
second_end [1, 3, 5, 9, 13, 15, 19, 23, 26, 29, 35, 38, 61, 74, 82, 84, 88, 91, 93, 96, 103, 109, 115, 119, 122, 125, 129, 131, 136, 138, 145, 148, 151, 154, 158, 169, 175, 181, 185]
You can see the difference of the above 4 indices in both strings despite being part of the same matched strings. That's what I was trying to solve and asked for help today but it seems that nobody understood my problem correctly. So, I'm attaching the solution for any future assistance.