我要计算DNA序列中的碱基数,返回序列中每种碱基的计数,还打印出两列表,其中第一列是碱基,第二列是关联的基本数量。我可以获得返回基数的函数,但是我不确定如何打印表格。我想使用基本的python函数进行此分析,尽管我认为使用某些python模块会更容易。
代码:
def base_counter(DNA):
A = 0
T = 0
G = 0
C = 0
for base in DNA:
if base == "A":
A = A + 1
elif base == "T":
T = T + 1
elif base == "G":
G = G + 1
elif base == "C":
C = C + 1
else:
pass
return A,T,G,C
参数输入:
dna="AAGCTACGTGGGTGACTTT"
函数调用:
counts=base_counter(dna)
print(counts)
输出:
(4, 6, 6, 3)
所需的输出:
print(counts)
A 4
T 6
G 6
C 3
和
counts
(4, 6, 6, 3)
答案 0 :(得分:2)
您可以使用curl -X GET -u "apikey:{apikey}" "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&text=Team,%20I%20know%20that%20times%20are%20tough!%20Product%20sales%20have%20been%20disappointing%20for%20the%20past%20three%20quarters.%20We%20have%20a%20competitive%20product,%20but%20we%20need%20to%20do%20a%20better%20job%20of%20selling%20it!"
来计算基数,并使用collections.Counter
以列方式设置数据。这是一个例子
pandas
答案 1 :(得分:1)
1)您有一个错误-您的return
向右缩进了一个额外的标签
2)使用dict
:
def base_counter(DNA):
dna_dict = {
"A": 0,
"T": 0,
"G": 0,
"C": 0,
}
for base in DNA:
if base == "A":
dna_dict["A"] += 1
elif base == "T":
dna_dict["T"] += 1
elif base == "G":
dna_dict["G"] += 1
elif base == "C":
dna_dict["C"] += 1
return dna_dict
dna = "AAGCTACGTGGGTGACTTT"
counts = base_counter(dna)
for base, count in counts.items():
print(base, count)
但是如果您必须按原样保留该功能:
def base_counter(DNA):
A = 0
T = 0
G = 0
C = 0
for base in DNA:
if base == "A":
A = A + 1
elif base == "T":
T = T + 1
elif base == "G":
G = G + 1
elif base == "C":
C = C + 1
return A,T,G,C
dna = "AAGCTACGTGGGTGACTTT"
counts = base_counter(dna)
for base, count in zip("ATGC", counts):
print(base, count)
答案 2 :(得分:0)
从功能内:
out_str="A "+str(A)+"\n"+\
"T "+str(T)+"\n"+\
"G "+str(G)+"\n"+\
"C "+str(C)
return out_str
现在您可以调用并打印它,它将以您想要的格式打印:
result=base_counter(DNA)
print(result)
用于OP的请求完整代码:
def base_counter(DNA):
A = 0
T = 0
G = 0
C = 0
for base in DNA:
if base == "A":
A = A + 1
elif base == "T":
T = T + 1
elif base == "G":
G = G + 1
elif base == "C":
C = C + 1
out_str = "A " + str(A) + "\n"+\
"T " + str(T) + "\n"+\
"G " + str(G) + "\n"+\
"C " + str(C)
return out_str
base=base_counter("AAGCTACGTGGGTGACTTT")
print(base)
输出:
A 4
T 6
G 6
C 3
答案 3 :(得分:0)
您可以使用另一个功能来打印结果:
def print_bases(bases):
print("A "+str(bases[0])+"\n"
"T "+str(bases[1])+"\n"
"G "+str(bases[2])+"\n"
"C "+str(bases[3]))
print_bases(counts)