【问题标题】:regex match not working on simple string with Pyteomics parser正则表达式匹配不适用于 Pyteomics 解析器的简单字符串
【发布时间】:2021-06-03 14:06:27
【问题描述】:

我正在对人类蛋白质组进行计算机消化,这意味着我正在尝试在特定位置切割每种蛋白质的氨基酸序列。我在我创建的一个更大的函数中使用 Pyteomics 解析器函数 Pyteomics Parser

我收到此错误: PyteomicsError:Pyteomics 错误,消息:“不是有效的 modX 序列:{'sequence': 'AKDEVQKN'}”

但是,我不确定 AKDEVQKN 如何与 modX_reqquence 编译器不匹配:

_modX_sequence = re.compile(r'^([^-]+-)?((?:[^A-Z-]*[A-Z])+)(-[^-]+)?$')

根据我对这个正则表达式的理解,它应该找到任何不以 (-) 开头且后跟一系列字母字符的字符串。

这是我尝试使用的功能。

import re
import pyteomics
from pyteomics import fasta, parser
def ButcherShop(df, target, rule,min_length=7,exception=None,max_legnth=100, pH=2.0):
>     raw = df[target]
>     unique_peptides = set()
>     for peptide in raw:
>         new_peptides = parser.cleave(peptide, rule=rule,min_length=min_length,exception=exception)
>         unique_peptides.update(new_peptides)
>     print(f'Done,{len(unique_peptides)} sequences of >= 7 amino acids!')
>     pep_dic = [{'sequence': i} for i in unique_peptides]
>     for peptides in pep_dic:
>         pep_dic['parsed_sequence'] = parser.parse(peptides,show_unmodified_termini=False)
>         pep_dic['xlength'] = len(peptides)
>         pep_dic['charge'] = int(round(electrochem.charge(peptides, pH=pH)))
>         pep_dic['mass']=int(round(Peptide_mass(peptides)))
>     pep_dic = [peptide for peptide in pep_dic if peptide['length'] <= int(max_length)]
>     pep_df = pd.DataFrame.from_dict(pep_dic)
>     return unique_peptides,pep_dic,pep_df

感谢您提供有关如何解决此问题的任何见解。

** 更新:如果我在不同的集合上运行,我会得到同样的错误,这可能表明它是库本身。

错误截图:

【问题讨论】:

  • ([^-]+-)? -- 这是一系列非破折号后跟破折号,但最后一个 ?说“这整个序列是可选的。”
  • 谢谢。我希望它匹配,我只是不确定它为什么会抛出这个错误。

标签: python regex string dataframe match


【解决方案1】:

这里是 Pyteomics 维护者。

错误信息其实告诉你问题的根源:PyteomicsError: Pyteomics error, message: "Not a valid modX sequence: {'sequence': 'AKDEVQKN'}"

这意味着您传递的不是字符串'AKDEVQKN',而是字典{'sequence': 'AKDEVQKN'}。这实际上发生在这里:

pep_dic = [{'sequence': i} for i in unique_peptides]
for peptides in pep_dic:
    pep_dic['parsed_sequence'] = parser.parse(peptides,show_unmodified_termini=False)
    ...

您应该将序列本身传递给parse,而不是字典:

pep_dic['parsed_sequence'] = parser.parse(peptides['sequence'], show_unmodified_termini=False)

【讨论】:

【解决方案2】:

在我运行解析器之前尝试使用它们的有效函数来测试所有肽段。我在我的字符串中找不到任何错误。我现在正在查看他们的功能或我自己的功能。

> for peptide in menu["Peptide"]:
>     x=parser.valid(peptide)
>     if x == False:
>         print(peptide)
>         break
>     else:
>         print(x)

【讨论】:

    【解决方案3】:

    不是解决方案,而是一些分析...

    在下面的简单案例示例代码中,“AKDEVQKN”使用帖子中的正则表达式匹配。

    import re
    
    line = 'AKDEVQKN'
    
    pat = re.compile(r'^([^-]+-)?((?:[^A-Z-]*[A-Z])+)(-[^-]+)?$')
    
    x = re.match(pat, line)
    
    if x:
        print(x)
        print(x.group())
        print(x.groups())
    

    输出:

    <re.Match object; span=(0, 8), match='AKDEVQKN'>
    AKDEVQKN
    (None, 'AKDEVQKN', None)
    

    这表明问题出在代码的其他地方。

    • 'AKDEVQKN' 是完整的系列还是还有更多?
    • _modX_sequence 是否可能在使用序列“AKDEVQKN”调用 re.match 时已更改? 要检查,暂时更改
      ~\Anaconda\envs\SciFly\lib\site-packages\pyteomics\parser.py
      在第 312 行,来自:
    try:
      n, body, c = re.match(_modX_sequence, sequence).groups()
    except AttributeError: 
    

    try:
      if sequence == 'AKDEVQKN':
        print("DEBUG: ", sequence, _modX_sequence)
        # or drop into a debugger, pdb or iPython's 
        # import pdb; pdb.set_trace()
        # dir() 
      n, body, c = re.match(_modX_sequence, sequence).groups()
    except AttributeError:
    

    【讨论】:

    • 我刚刚更新了这个问题。我已经尝试过使用不同的酶,基本上我需要 20,000 个字符串并生成 >500,000 个字符串,这些字符串最终形成一组。我开始怀疑库代码中存在错误。我将效仿他们的做法,看看我最终会得到什么。
    • 它似乎在随机字符串上抛出错误。
    • 现在我看到你的帖子,我开始认为这是他们的代码。 ``` 312 try: --> 313 n, body, c = re.match(_modX_sequence, sequence).groups() 314 除了AttributeError: ``
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2019-10-12
    • 1970-01-01
    相关资源
    最近更新 更多