根据提供的fastq文件列表,针对单个fastq文件并行计算reads的长度,全部完成后计算N50

#!/usr/bin/env python
#-*- encoding=UTF-8 -*-
import multiprocessing
import gzip
import sys
## this function cal the sequence length per file
def cal_length(f):
	seq = []
	reads = 0
	tt = gzip.open(f).readlines()
	tlen = len(tt)
	plen = tlen/4
	for i in range(0,plen):
		start,end = (i*4,(i+1)*4)
		pdna = tt[start:end][1]
		reads += 1
		seq.append(len(pdna))
	return (f,reads,seq)

if __name__ == '__main__':
	fasta_files = sys.argv[1]## fasta files input list
	flist = [  f.strip() for f in open(fasta_files).readlines() if f.strip()] ## read the list file
	pool = multiprocessing.Pool(10) ## multi processing cal the sequence length
	results = pool.map(cal_length,flist) ## get the calulate results

	lengths = [] ### the seqences list
	pool.close() ## close the processing
	pool.join()
	for res in results: ## read results
		#print ('fasta file',res[0],'reads number:',res[1])
		for r in res[2]:
			lengths.append(int(r))

	## the N50 calculate
	N50_pos = sum(lengths)/2.0    
	lengths.sort()  
	lengths.reverse() 
	ValueSum,N50 = 0,0
	for value in lengths: 
		ValueSum += value
		if N50_pos <= ValueSum:
			N50 = value
			break
	print ("fasta files N50:",N50)

1.使用示例:

python n50_cal_batch.py f.list

f.list是fastq文件的绝对路径的列表。

本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值