Rosalind is a platform for learning bioinformatics and programming through problem solving.
Rosalind 是一个通过解决实际生物学问题来学习生物信息和练习编程的平台,类似于 IT 行业的力扣(LeetCode)。工作学习之余,咱们也来刷一波吧。
字符串是 Python 中的一种基本数据类型,一个字符串由一组有序的字符组成。由于 DNA 的四种碱基通常用 A、T、C、G 四个字母表示,因此一段 DNA 序列可以通过一个字符串表示,如:ATGCTTCAGAAAGGTCTTACG
给定:一条长度至多 1000bp 的 DNA 字符序列。
应得:4 个以空格隔开的整数,分别表示 4 种碱基(A, C, G, T)在字符串中出现的次数。
AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC
20 12 17 21
Counting_DNA_Nucleotides.py
from collections import defaultdict;
def count_dna_nucleotides(dna):
d = defaultdict(int)
for c in dna.upper():
d[c] += 1
return "%d %d %d %d" % (d['A'], d['C'], d['G'], d['T'])
def test():
dna = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
ret = count_dna_nucleotides(dna)
return ret == "20 12 17 21"
if __name__ == '__main__':
if not test():
print("count_dna_nucleotides: Failed")
with open('rosalind_dna.txt') as fh:
dna = fh.read()
ret = count_dna_nucleotides(dna)
print(ret)
说明:
怎么样,很简单吧,趁车速还不快,赶快上车吧。
A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.
An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."
Given: A DNA string of length at most 1000 nt.
Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in .
AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC
20 12 17 21