Rishi
Rishi

Reputation: 39

Edit a certain part of a list inside a list

I'm relatively new to python and I've been working on an algorithm that involves genetic code. I started by associating all 4 genetic bases, A, C, T, G with a list. A is 1,0,0,0. C is 0,1,0,0. T is 0,0,1,0 and G is 0,0,0,1. There are two different genetic codes, one being the original one and the other being one that was genetically mutated. The algorithm is going to come to conclusions of the data given based on the difference between the two genetic codes. But first, I need to sort of preprocess the data before I can work on the algorithm making conclusions.

A = [1, 0, 0, 0]
C = [0, 1, 0, 0]
T = [0, 0, 1, 0]
G = [0, 0, 0, 1]
original = [A, T, T, G, C]
copy = [C, T, T, A, T]

final = original[:] 
for i, v in enumerate(original):
    if v == copy[i]: 
       print(v)
    elif v != copy[i]: 
        v.insert(0, ("add"))  
        print(v)

This is the output:

enter image description here

I'm trying to make the algorithm so that the algorithm compares the original and copy genetic code, item by item. It checks if each letter is equal. For the first one, A and C are not equal. If it's not equal I want the algorithm to output 1,1,0,0. Essentially overlap the two letters. IF the algorithm sees the code is the same, for example, the second one, T & T, it should remain the same which it does.

In the image above, where it says to add, those are the lines where the algorithm overlaps and should change. My question is how can I make it so that when the algorithm detects two different letters, it essentially overlaps the two separate lists to merge together?

Upvotes: 1

Views: 39

Answers (2)

busybear
busybear

Reputation: 10580

You should look at logical operators. What you are doing is an "or" operation on your base pairs. If you use numpy, you can vectorize this and drastically speed up processing:

original_array = np.array(original)
copy_array = np.array(copy)
oringal_array | copy_array

Result:

array([[1, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 1, 0],
       [1, 0, 0, 1],
       [0, 1, 1, 0]])

If you don't want to use numpy, you can still use logical or | with your lists:

final = []
for o, c in zip(original, copy):
    final.append([x | y for x, y in zip(o, c)])

Or

final = [[x | y for x, y in zip(o, c)] for o, c in zip(original, copy)]

Upvotes: 3

leopardxpreload
leopardxpreload

Reputation: 768

Try this:

import numpy as np

A = [1, 0, 0, 0]
C = [0, 1, 0, 0]
T = [0, 0, 1, 0]
G = [0, 0, 0, 1]
original = [A, T, T, G, C]
copy = [C, T, T, A, T]

x = [1 if 1 in j else 0 for i, v in enumerate(original) for j in zip(v, copy[i])]

new = np.array_split(x, len(original))
print(new)

Upvotes: 1

Related Questions