contig N50---小脚本

1. contig N50 的定义

基因组的统计信息包含GC含量,N50等等,这里我们计算N50的算法:
N50是指一个基因组所有的contig,按照长度从大到小排列,一直长度加和,一直到某条contig达到该基因组总共长度的1/2时,那么这条contig的长度,就是N50的值。N50可以作为评价一个基因组拼接好坏的条件之一。

2. 脚本实现

1. N50 计算:

# 以sequence.fasta 为列,来计算N50的值
# 基因组拼接时,contig是按照长度从大到小排列的
f = open('sequence.fasta', 'r').readlines()
total_length = 0
for i in f:
	if not i.startswith('>'):
		seq_length = len(i.strip())
		total_length += seq_length


variable_length = 0
for i in f:
	if not i.startswith('>'):
		seq_length = len(i.strip())
		variable_length += seq_length
		if variable_length >= total_length / 2:
			print('N50 : %s'%seq_length)          #输出N50的值
			break


2. GC 含量计算

# 以sequence.fasta 为列,来计算N50的值

f = open('sequence.fasta', 'r').readlines()
total_length = 0
total_seq = ''
for i in f:
	if not i.startswith('>'):
		seq_length = len(i.strip())
		total_length += seq_length
		total_seq += i.strip()


GC_number = 0
for j in total_seq:
	if j == 'C' or j == 'G':
		GC_number += 1

GC_percent = GC_number / total_length     # 输出为GC含量
print(f'GC percent : {GC_percent}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值