CAVS CAVS - 1 month ago 34
Python Question

mutating dna in parallel lists python

def mutations (list_a,string1,name,list_b):
""" (list of str, str, list of str, list of str) -> NoneType
"""


dna=list_a
for i in range(len(list_b)):
strand=dna[:dna.index(list_b[i])]
string1=string1[string1.index(list_b[i]):]
dna[strand+string1]





>>>dna=['TGCAGAATTCGGTT','ACGTCCCGGGTTGC']
>>>mutations(dna,'CCCGGGGAATTCTCGC',['EcoRI','SmaI'],['GAATTC','CCCGGG'])
>>>mutated
>>>['TGCAGAATTCTCGC','ACGTCCCGGGGAATTCTCGC']


It's suppose to modify the first parameter. So basically im trying to modify list_a and making it change to
['TGCAGAATTCTCGC','ACGTCCCGGGGAATTCTCGC']
however, i get an error saying

strand=dna[:dna.index(string1[i])].

ValueError: 'GAATTC' is not in list


Also, is there a way if the sequence does not exist, it doesn't modify the function?

Answer

Well if I understand you correctly, you want to check each element in list_a if it contains its corresponding element from list_b. If so, you want to modify the element from list_a by replacing the rest of the string (including the list_b element) with part of a control string that does also contain the element from list_b, right?!

Ideally you would put this in your question!!

A way of doing this would be as follow:

def mut(list_a, control, list_b):
    for i in range(len(list_a)):  # run through list_a (use xrange in python 2.x)
        if list_b[i] in list_a[i]:  # if the element from list_b is in it
            # correct the element
            list_a[i] = list_a[i][:list_a[i].index(list_b[i])] +\
                control[control.index(list_b[i]):]