繁体   English   中英

正则表达式以查找序列中的某些碱基

[英]regular expression to find certain bases in a sequence

在我的代码中,我想做的是通过仅在输出字符串中包含字母A,C,T,G,N和U来清理FastA文件。 我正试图通过一个正则表达式来做到这一点,它看起来像这样:

newFastA = (re.findall(r'A,T,G,C,U,N',self.fastAsequence)) #trying to extract all of the listed bases from my fastAsequence.
        print (newFastA)

但是,我并没有按顺序获得所有碱基的出现。 我认为我的正则表达式格式不正确,所以如果您可以让我知道我犯了什么错误,那将是很好的。

我会完全避免使用正则表达式。 您可以使用str.translate删除不需要的字符。

from string import ascii_letters

removechars = ''.join(set(ascii_letters) - set('ACTGNU'))

newFastA = self.fastAsequence.translate(None, removechars)

演示:

dna = 'ACTAGAGAUACCACG this will be removed GNUGNUGNU'

dna.translate(None, removechars)
Out[6]: 'ACTAGAGAUACCACG     GNUGNUGNU'

如果您也想删除空格,则可以将string.whitespace放入removechars

旁注,以上内容仅适用于python 2,在python 3中还有一个附加步骤:

from string import ascii_letters, punctuation, whitespace

#showing how to remove whitespace and punctuation too in this example
removechars = ''.join(set(ascii_letters + punctuation + whitespace) - set('ACTGNU'))

trans = str.maketrans('', '', removechars)

dna.translate(trans)
Out[11]: 'ACTAGAGAUACCACGGNUGNUGNU'
print re.sub("[^ACTGNU]","",fastA_string)

与百万其他答案一起得到

还是没有

print "".join(filter(lambda character:character in set("ACTGUN"),fastA_string)

您需要使用一个字符集。

re.findall(r"[ATGCUN]", self.fastAsequence)

您的代码将查找文字"A,T,G,C,U,N" ,并输出所有出现的内容。 正则表达式中的字符集允许搜索以下类型:“以下任意一项: ATGCUN ”,而不是“以下内容: A,T,G,C,U,N

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM