(Bio.SeqUtils.molecular_weight 函数) 无法计算明确核苷酸序列的分子量
Inability of (Bio.SeqUtils.molecular_weight function) to calculate molecular weight of unambiguous nucleotide sequence
我正在尝试在 python 中创建一个函数,从 fasta 文件中读取明确和模糊的核苷酸序列以及 returns 序列 ID 和分子量。
我尝试使用以下代码:
import Bio
from Bio import SeqUtils, SeqIO
def function(filename):
nucleotides ={'A','T', 'C', 'G'}
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
for nucl in record:
if nucl in nucleotides:
continue
else:
print(str(record.id)+": is ambiguous")
break
else:
mol_weight= Bio.SeqUtils.molecular_weight(record)
print(str(record.id)+": is unambiguous & molecular weight = "+str(mol_weight))
function("random.fasta")
如果我在不明确的序列上使用这段代码,绝对没有问题,我得到了我想要的结果。但是,如果我包含明确的序列,我会得到 "ValueError: 'I' is not a valid unambiguous letter for DNA" 由 'Bio.SeqUtils.molecular_weight(record)' 函数引起,在我看来这不是' 是有道理的,因为如果我理解正确的话,字母 'I' 通常会导致第一个 else 语句中断。
我还使用了一个自定义函数来手动计算分子量(基于固定值),在这种情况下没有错误,并且该函数对于模糊和明确的序列都工作得很好。但是有人指出我的手动计算不如内置函数准确。
我是 Python 的新手,但我希望有人知道为什么仍然会出现 ValueError 以及如何解决它。
我认为错误发生在表达式 mol_weight= Bio.SeqUtils.molecular_weight(record)
.
记录被隐式转换为其字符串表示 ID: SEQUENCE_1 ...
。所以 ValueError 中的 'I'
根本不对应核苷酸,而是对应一个相当随机的字符串值。
试试这一行:mol_weight = Bio.SeqUtils.molecular_weight(record.seq)
对我来说,它返回了我想要的结果:
SEQUENCE_1: is unambiguous & molecular weight = 331.2218
对于会引发您描述的错误的微不足道的序列(只是字母 A)。
>SEQUENCE_1
A
更新:
完整的解决方案如下所示:
from Bio import SeqIO, SeqUtils
def function(filename):
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
try:
mol_weight = SeqUtils.molecular_weight(record.seq)
print(f"{record.id}: is unambiguous & molecular weight = {mol_weight}")
except ValueError as exception:
print(f"{record.id}: {exception}")
if __name__ == '__main__':
function("random.fasta")
我包括了@pippo1980 的答案中的执行处理,因为它确实更简单、更快,而且可以说更像 pythonic。
更短:
from Bio import SeqIO, SeqUtils
def function(filename):
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
try:
mol_weight= SeqUtils.molecular_weight(record.seq, 'DNA')
print(str(record.id)+": is unambiguous & molecular weight = "+str(mol_weight))
except Exception as exception:
print(record.id+':',str(type(exception).__name__), str(exception))
function("random.fasta")
function("random.fasta")
输入random.fasta文件:
>first
AAAAAAAAAAAAATTTTTTTTTTTTTTGGGGGGGGGGGGGGCCCCCCCCCCCCCCGTA
>second
AAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTICCCCCCCCCCCCCCCCCCCCCCCCCCC
>third
AAAAAAAAAQQQQQQQQQQQQQQQQQQTTTTTTTTTT
>fourth
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTTGGGGGGGGGGGGGGGGGCGCGCGCGCGC
输出:
first: is unambiguous & molecular weight = 17952.438
second: ValueError 'I' is not a valid unambiguous letter for DNA
third: ValueError 'Q' is not a valid unambiguous letter for DNA
fourth: is unambiguous & molecular weight = 25755.56080000001
我正在尝试在 python 中创建一个函数,从 fasta 文件中读取明确和模糊的核苷酸序列以及 returns 序列 ID 和分子量。
我尝试使用以下代码:
import Bio
from Bio import SeqUtils, SeqIO
def function(filename):
nucleotides ={'A','T', 'C', 'G'}
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
for nucl in record:
if nucl in nucleotides:
continue
else:
print(str(record.id)+": is ambiguous")
break
else:
mol_weight= Bio.SeqUtils.molecular_weight(record)
print(str(record.id)+": is unambiguous & molecular weight = "+str(mol_weight))
function("random.fasta")
如果我在不明确的序列上使用这段代码,绝对没有问题,我得到了我想要的结果。但是,如果我包含明确的序列,我会得到 "ValueError: 'I' is not a valid unambiguous letter for DNA" 由 'Bio.SeqUtils.molecular_weight(record)' 函数引起,在我看来这不是' 是有道理的,因为如果我理解正确的话,字母 'I' 通常会导致第一个 else 语句中断。
我还使用了一个自定义函数来手动计算分子量(基于固定值),在这种情况下没有错误,并且该函数对于模糊和明确的序列都工作得很好。但是有人指出我的手动计算不如内置函数准确。
我是 Python 的新手,但我希望有人知道为什么仍然会出现 ValueError 以及如何解决它。
我认为错误发生在表达式 mol_weight= Bio.SeqUtils.molecular_weight(record)
.
记录被隐式转换为其字符串表示 ID: SEQUENCE_1 ...
。所以 ValueError 中的 'I'
根本不对应核苷酸,而是对应一个相当随机的字符串值。
试试这一行:mol_weight = Bio.SeqUtils.molecular_weight(record.seq)
对我来说,它返回了我想要的结果:
SEQUENCE_1: is unambiguous & molecular weight = 331.2218
对于会引发您描述的错误的微不足道的序列(只是字母 A)。
>SEQUENCE_1
A
更新:
完整的解决方案如下所示:
from Bio import SeqIO, SeqUtils
def function(filename):
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
try:
mol_weight = SeqUtils.molecular_weight(record.seq)
print(f"{record.id}: is unambiguous & molecular weight = {mol_weight}")
except ValueError as exception:
print(f"{record.id}: {exception}")
if __name__ == '__main__':
function("random.fasta")
我包括了@pippo1980 的答案中的执行处理,因为它确实更简单、更快,而且可以说更像 pythonic。
更短:
from Bio import SeqIO, SeqUtils
def function(filename):
with open(filename) as file:
for record in SeqIO.parse(file, "fasta"):
try:
mol_weight= SeqUtils.molecular_weight(record.seq, 'DNA')
print(str(record.id)+": is unambiguous & molecular weight = "+str(mol_weight))
except Exception as exception:
print(record.id+':',str(type(exception).__name__), str(exception))
function("random.fasta")
function("random.fasta")
输入random.fasta文件:
>first
AAAAAAAAAAAAATTTTTTTTTTTTTTGGGGGGGGGGGGGGCCCCCCCCCCCCCCGTA
>second
AAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTICCCCCCCCCCCCCCCCCCCCCCCCCCC
>third
AAAAAAAAAQQQQQQQQQQQQQQQQQQTTTTTTTTTT
>fourth
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATTTTTTTTTTTTTTTTTGGGGGGGGGGGGGGGGGCGCGCGCGCGC
输出:
first: is unambiguous & molecular weight = 17952.438
second: ValueError 'I' is not a valid unambiguous letter for DNA
third: ValueError 'Q' is not a valid unambiguous letter for DNA
fourth: is unambiguous & molecular weight = 25755.56080000001