python文本处理---计算fasta文件中不同氨基酸的数目

Posted 爬行的乌龟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python文本处理---计算fasta文件中不同氨基酸的数目相关的知识,希望对你有一定的参考价值。

#::!/usr/bin/python3
#-*- coding:utf-8 -*-
#计算fasta文件中各个氨基酸的含量

import sys
args=sys.argv

f=open(args[1], r)
fw=open(out.txt, w)

line=f.read()
txt=‘‘.join(line.split(\n)[1:])       #可以得到氨基酸序列


#构建了各个氨基酸和含量的字典
##注意collections模块中Counter的用法
from collections import Counter
dict=Counter(txt)       #Counter直接返回字典


#文件的write方法只能写入字符串,
#因此要先将字典转变为字符串模式;json模块
import json
strdict=json.dumps(dict)
fw.writelines(strdict)
fw.writelines(\n)
fw.writelines(str(sum(dict.values())))
fw.writelines(\n)


#计算指定氨基酸的百分比
‘‘‘
for index  in dict.keys():
    if index==args[2]:
        rate=str(dict[index]/sum(dict.values()))
        fw.writelines(‘\n‘)
        fw.writelines(rate)
        fw.writelines(‘\n‘)
‘‘‘

f.close()
fw.close()

 




以上是关于python文本处理---计算fasta文件中不同氨基酸的数目的主要内容,如果未能解决你的问题,请参考以下文章

python文本处理--定位motif在fasta文件上的位置,并给出位置信息

如何在 fasta 文件中并行化计算,其中每个处理器采用一个序列

python处理fasta文件,ID和序列放在一行

python 计算任何给定fasta文件的GC内容

python:计算fasta的GC含量

利用Python将GB格式序列文件 转换成 Fasta格式文件