2
$\begingroup$

I have a string, a list of substrings, and a list of positions where substrings are found in string.

string = 'ATGGTACACGCTACGAGCTAG'
substrings = ['TACA', 'CTAGAAAAG']
positions = ['11, 4', '']

Substring 'TACA' appears at positions 11 and 4 in string (i.e. '11, 4').

Substring 'CTAGAAAAG' does not appear in string (i.e. '').

I need to remove 'CTAGAAAAG' from substrings and I need to remove '' from positions, while creating a new list with 'CTAGAAAAG'. I need your help here to create code which will give the following results:

new_substrings = ['TACA']
new_positions = ['11, 4']
new_list = ['CTAGAAAAG']
string = 'ATGGTACACGCTACGAGCTAG' (obtained from above)

Once this is complete, the given code:

new_positions = [map(int, x.split(',')) for x in new_positions]
seq_pos = dict(zip(new_substrings,new_positions))
seq_ref = {k: [string[start:start+length] for start in starts] for k, starts, length in zip(new_substrings,new_positions,[len(x) for x in new_substrings])}

Gives:

seq_pos = {'TACA': [11, 4]}
seq_ref = {'TACA': ['TACG', 'TACA']}

Then, from seq_ref, I need to create a list of integers which represents the number of mismatches between the key: 'TACA' and both of its values: 'TACG' and 'TACA'.

mismatches = [[1, 0]]

The code to find the # of mismatches between two strings is provided:

def HammingDistance(p, q):
    dist = 0
    for i in range(len(p)):
        if p[i] != q[i]:
            dist += 1
    return dist

Finally, I need to save my data in a CSV file (i.e. Excel or Numbers).

new_positions = ['11, 4'] (obtained above)
new_substrings = ['TACA'] (obtained above)
actual_substrings = [['TACG', 'TACA']] (values of seq_ref)
mismatches = [[1, 0]] (obtained above)

Info needs to be sorted based on # of mismatches.

enter image description here

I understand this is a multi-step problem but any help would be appreciated.

$\endgroup$
  • 2
    $\begingroup$ You posted a lot of given code, but what have you tried so far? To be honest this reads a lot like a homework assignment and while most users here are happy to help this is not a site to get your work done for you. $\endgroup$ – voiDnyx Oct 15 '18 at 12:37
  • $\begingroup$ Where did the blocks of "given" code come from? I agree this seems like homework and many of the questions you are trying to answer have similar concepts in the "given" code. Have you tried using or understand some of the functions in the "given" code to see how they apply to the missing steps you have? $\endgroup$ – Bioathlete Oct 15 '18 at 14:21
  • $\begingroup$ This is not part of a homework assignment. I've never taken a coding or bioinformatics class before and I just need some assistance. This is a unique problem that I have created from scratch. The given code is my own work and I uploaded it so that it may provide a better understanding of the problem at hand. I need help removing a blank string element from a list and I need help iterating the HammingDistance() function for elements in my list. Also, I am unfamiliar with csv files, so I am just reaching out to see if anyone can help. $\endgroup$ – Thomas Ribeiro Oct 15 '18 at 14:26
  • $\begingroup$ Ok I posted a solution. It is confusing because most of what you are stuck on short of the CSV are concepts that you are using in the "given" code, including zip, dictionaries, list comprehension / iteration. $\endgroup$ – Bioathlete Oct 15 '18 at 14:46
1
$\begingroup$

For the first question try something like:

new_substrings = []
new_positions = []
new_list = []
for pos, sub in zip(positions, substrings): 
   if not pos:
       new_list.append(sub)
   else:
       new_substrings.append(sub)
       new_positions.append(pos)

this gives:

>>> new_substrings
['TACA']
>>> new_positions
['11, 4']
>>> new_list
['CTAGAAAAG']

For the hamming distance you just need to iterate over the values in seq_ref and append the results of the hamming distance function in mismatches. Based on the given code and the example above you should be able to create that block. Below is an untested quick example to get you on track.

mismatches = []
for seq in seq_ref.keys():
   for value in seq_ref[seq]:
       mismatches.append(HammingDistance(seq, value))

As far as the CSV and sorting is concerned I suggest that you look into pandas or numpy. Both of those packages provided functions to help with this.

$\endgroup$
  • $\begingroup$ Just a note to say that your hamming distance calculation is pretty slow. When we've had to do this calculation before on many millions of sequences we've ended up cythonising the code. But friends who are better coders have even better algorithms for calculating these things. $\endgroup$ – Ian Sudbery Oct 17 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.