Hello,
I'm new to the world of python and im trying to solve a question which I am given a few dna sequences, for example:
sequences = ["GAGGTAAACTCTG", "TCCGTAAGTTTTC", "CAGGTTGGAACTC", "ACAGTCAGTTCAC",
"TAGGTCATTACAG", "TAGGTACTGATGC"]
I want to know how many times the nucleotide "A" is in the first position [0] of all of those sequences (the answer should be 1 in that case).
Im trying to use for loop but don't really know how to move forward.
Ill appreciate any help, Thank you!
Hi, It is not a norm but it is a good practice to post an attempted resolution first and then other members try to correct it or suggest another answer. You said you are a python beginner so don't worry about judgments you can post any attempt no matter how bad it went. ;)
Here's a little python function you can work off of:
def count_quer_at_pos_in_seq(seqs, quer = "A"):
#Initialize a list of zeroes, and make it
#as long as the longest input sequence.
#This will be used to store the counts for the
#character counts at each position (along the
#length of the sequences).
out = [0]*max([len(seq) for seq in seqs])
#For each sequence in the list sequences:
for seq in seqs:
#For each position in the current sequence:
for pos in range(len(seq)):
#Check if the character at the current position
#is identical to the query character supplied by
#the user.
if seq[pos] == quer:
#If it is, increment the count in the list "out"
#by one.
out[pos] += 1
#Return out to the calling environment.
return(out)
#----
#Test run.
sequences = ["GAGGTAAACTCTG", "TCCGTAAGTTTTC", "CAGGTTGGAACTC", "ACAGTCAGTTCAC", "TAGGTCATTACAG", "TAGGTACTGATGC"]
count_quer_at_pos_in_seq(sequences, quer = 'A')
#[1, 4, 1, 0, 0, 3, 4, 1, 1, 3, 0, 2, 0]
Hi, It is not a norm but it is a good practice to post an attempted resolution first and then other members try to correct it or suggest another answer. You said you are a python beginner so don't worry about judgments you can post any attempt no matter how bad it went. ;)
cross posted: https://stackoverflow.com/questions/67060118/
[0, 0, 0, 1, 0, 0]
1