【Rosalind】Consensus and Profile

该博客介绍了如何处理一组等长DNA序列,通过统计每个位置上不同碱基(A、C、G、T)出现的频率,生成一个共识字符串和对应的碱基频率矩阵。给出的参考代码使用Python的Bio库解析FASTA格式的DNA序列,并输出最常见的碱基及其在每个位置的计数。

问题描述

Problem

A matrix is a rectangular table of values divided into rows and columns. An m×nm \times nm×n matrix has mmm rows and nnn columns. Given a matrix AAA, we write Ai,jA_{i, j}Ai,j to indicate the value found at the intersection of row iii and column jjj.

Say that we have a collection of DNA strings, all having the same length nnn. Their profile matrix is a 4×n4 \times n4×n matrix PPP in which P1,jP_{1,j}P1,j represents the number of times that ‘A’ occurs in the jjjth position of one of the strings, P2,jP_{2,j}P2,j represents the number of times that C occurs in the jjjth position, and so on (see below).

A consensus string ccc is a string of length nnn formed from our collection by taking the most common symbol at each position; the jjjth symbol of ccc therefore corresponds to the symbol having the maximum value in the jjj-th column of the profile matrix. Of course, there may be more than one most common symbol, leading to multiple possible consensus strings.

DNA Strings
A T C C A G C T
G G G C A A C T
A T G G A T C T
A A G C A A C C
T T G G A A C T
A T G C C A T T
A T G G C A C T
Profile
A 5 1 0 0 5 5 0 0
C 0 0 1 4 2 0 6 1
G 1 1 6 3 0 1 0 0
T 1 5 0 0 0 1 1 6
Consensus
A T G C A A C T

Given: A collection of at most 10 DNA strings of equal length (at most 1 kbp) in FASTA format.

Return: A consensus string and profile matrix for the collection. (If several possible consensus strings exist, then you may return any one of them.)

Sample Dataset

>Rosalind_1
ATCCAGCT
>Rosalind_2
GGGCAACT
>Rosalind_3
ATGGATCT
>Rosalind_4
AAGCAACC
>Rosalind_5
TTGGAACT
>Rosalind_6
ATGCCATT
>Rosalind_7
ATGGCACT

Sample Output

ATGCAACT
A: 5 1 0 0 5 5 0 0
C: 0 0 1 4 2 0 6 1
G: 1 1 6 3 0 1 0 0
T: 1 5 0 0 0 1 1 6

题目大意

给多串等长DNA序列,统计各位置不同碱基出现的次数,第一行输出各位置上最常见的碱基(如有多个,输出任意一个),接下来4行输出各位置各碱基出现的次数

题解

生成一个链表,每一项为一个字典,统计各位置各碱基出现的次数,最后输出即可
注意输入数据满足fasta格式,可能存在一个序列碱基有多行的情况,注意处理

参考代码

from Bio import SeqIO

l = 0
ans = []

for seq_record in SeqIO.parse("rosalind_cons.txt", "fasta"):
	if (l == 0):
		l = len(seq_record.seq)
		for i in range(l):
			ans.append({'A': 0, 'G': 0, 'C': 0, 'T':0})
	for i in range(l):
		ans[i][seq_record.seq[i]] += 1

with open("out.txt", "w") as fo:
	for i in range(l):
		fo.write(max(ans[i], key = ans[i].get))
	fo.write('\n')
	for j in ['A', 'C', 'G', 'T']:
		fo.write("%s: " % j)
		for i in range(l):
			fo.write("%d " % ans[i][j])
		fo.write("\n")
	fo.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值