使用Python正则表达式查找和编辑文本文件的内容

时间:2018-11-01 19:24:18

标签: regex bioinformatics python-3.7

我有一个充满氨基酸的文本文件(CA-Final.txt)以及其他一些数据。这是文本文件的一个片段

ATOM    109  CA ASER A  48      10.832  19.066  -2.324  0.50 61.96           C  
ATOM    121  CA AALA A  49      12.327  22.569  -2.163  0.50 60.22           C  
ATOM    131  CA AGLN A  50       8.976  24.342  -1.742  0.50 56.71           C  
ATOM    145  CA APRO A  51       7.689  25.565   1.689  0.50 51.89           C  
ATOM    158  CA  GLN A  52       5.174  23.336   3.467  1.00 43.45           C  
ATOM    167  CA  HIS A  53       2.339  24.135   5.889  1.00 38.39           C  
ATOM    177  CA  PHE A  54       0.900  22.203   8.827  1.00 33.79           C  
ATOM    188  CA  TYR A  55      -1.217  22.065  11.975  1.00 34.89           C  
ATOM    200  CA  ALA A  56       0.334  20.465  15.090  1.00 31.84           C  
ATOM    205  CA  VAL A  57       0.000  20.066  18.885  1.00 30.46           C  
ATOM    212  CA  VAL A  58       2.738  21.762  20.915  1.00 27.28           C 

从本质上讲,我的问题是,有些氨基酸在它们前面不应该存在的字母前带有字母A。氨基酸缩写应为3个字母长。我尝试使用正则表达式在氨基酸缩写前面的每个A实例处删除A。到目前为止,这是我的代码

def Trimmer(txtFileName):
    i = open('CA-final.txt', 'w')
    j = open(txtFileName, 'r')
    for record in j:
        with open(txtFileName, 'r') as j:
            content= j.read()
            content_new = re.sub('^ATOM\s+\d+\s+CA\s+A[ADTSEPGCVMILYFHKRWQN]', r'^ATOM\s+\d+\s+CA\s+[ADTSEPGCVMILYFHKRWQN]', content, flags = re.M)

运行该函数时,它会返回错误

 File "C:\Users\UserName\AppData\Local\conda\conda\envs\biopython\lib\sre_parse.py", line 1024, in parse_template
    raise s.error('bad escape %s' % this, len(this)) 

error: bad escape \s

我的想法是,此函数将在3个字符的字符串前找到A的每个实例,并将其替换为其他3个字符。为什么我会收到此错误?

2 个答案:

答案 0 :(得分:1)

据我所知,目前实现目标的最简单方法是使用 biopython (因为它是PDB文件)进行解析。

让我们分析以下脚本:

#!/usr/bin/env python3
import Bio
print("Biopython v" + Bio.__version__)

from Bio.PDB import PDBParser
from Bio.PDB import PDBIO

# Parse and get basic information
parser=PDBParser()
protein_1p49 = parser.get_structure('STS', '1p49.pdb')
protein_1p49_resolution = protein_1p49.header["resolution"]
protein_1p49_keywords = protein_1p49.header["keywords"]

print("Sample name: " + str(protein_1p49))
print("Resolution: " + str(protein_1p49_resolution))
print("Keywords: " + str(protein_1p49_keywords))
print("Model: " + str(protein_1p49[0]))

#initialize IO 
io=PDBIO()

#custom select
class Select():
    def accept_model(self, model):
        return True
    def accept_chain(self, chain):
        return True
    def accept_residue(self, residue):
        # print("residue id:" + str(residue.get_id()))
        print("residue name:" + str(residue.get_resname()))
        if len(str(residue.get_resname()))>3:
            print("Alert! abbr longer that 3 letters" + residue.get_resname())
            exit(1)
        return True       
    def accept_atom(self, atom):
        # print("atom id:" + atom.get_id())
        # print("atom name:" + atom.get_name())
        if atom.get_name() == 'CA':  
            return True
        else:
            return False

#write to output file
io.set_structure(protein_1p49)
io.save("1p49_out.pdb", Select())

exit(0)

它解析PDB结构,并使用内置的 biopython PDBIO 保存蛋白质结构的自定义部分。请注意,您可以将自定义逻辑放在 Select 子类中。

在此示例中,我使用了 accept_residue 方法来获取有关蛋白质结构中异常命名的残基的信息。您可以轻松地对此进行扩展,并在此函数内执行简单的字符串修剪。

答案 1 :(得分:0)

如果三个字母中的第一个字母是'A',则您的正则表达式将失败。尝试以下方法:

(^ATOM\s+\d+\s+CA\s+)A(\w\w\w)

它创建2个群组,其中包含多余的'A'

之前和之后的内容

然后替换为2个组:

\1\2
相关问题