2
votes

Problem:

Hi I'm new to Python so looking for some help. I have multiple input lines. I'm looking for a way to take every single word from multiple list and then add them up to get the total count on each instance for that word in multiple List. I will really appreciate any guidance.

3
Add your code as well. - H. U.
I don't understand your expected output - Two-Bit Alchemist
@Two-BitAlchemist basically counting the indexes of the read sequences in an output (e.g. GGGA shows up at index 0 in read 1, and index 3 in read 2)? - Nick T
Can a sequence show up multiple times in a read? Your read 2 shows ATTA twice. Given that, I'm not sure about the output again. - RobertB
Your output is still unclear. Give a real example with real input and the expected output. You don't need 50 columns, just 5, but make it exactly correct. - RobertB

3 Answers

1
votes

Edit:

"""I need a count of the word given by key(s) in multiple lists at the given index. So what I need is
>>> count(["a", "b", "c"], ["c", "b", "a"])
{"a": [1,0,1], "b": [0,2,0], "c": [1,0,1]}":
"""

I hope this will be right:

def count (*args):
    l = len(args[0])
    end = {}
    for lst in args:
        y = 0
        for x in lst:

            end.setdefault(x, l*[0])
            end[x][y] += 1
            y += 1
    return end

If you need a way to manipulate genome material from RNA/DNA you can search for libraries on pypi.python.org. There are plenty good ones.

0
votes

Take a look at the dict.setdefault() method and the enumerate() function:

def count_items(data):
    count = {}
    for datum in data:
        count[datum] = count.setdefault(datum, 0) + 1
    return count


def collate(*data):
    collated = {}
    for datum in data:
        for k, v in datum.items():
            collated[k] = datum.setdefault(k, 0) + 1
    return collated


def key_position(sequence, key):
    sequence_map = [0 for _ in sequence]
    for i, item in enumerate(sequence):
        if key == item:
            sequence_map[i] += 1
    return sequence_map


data1 = ['a', 'b', 'c', 'd', 'a']
data2 = ['a', 'b', 'c', 'd', 'a']

counted1 = count_items(data1)
counted2 = count_items(data2)

collated = collate(counted1, counted2)

a_positions = key_position(data1, 'a')
0
votes

First, zip your reads:

Read_1 = ['GGGA', 'ATTA']
Read_2 = ['GATT', 'ATTA']
reads = zip(Read_1, Read_2)
# ['GGGA', 'GATT'], ['ATTA', 'ATTA']

Then, count stuff:

from collections import Counter
counters = [Counter(read) for read in reads]

Then ask for the frequence of a given sequence:

print(list(cnt['ATTA'] for cnt in counters)
# [0, 2]
print(list(cnt['GGGA'] for cnt in counters)
# [1, 0]