这是我的第一个脚本,我试图比较两个基因组文件,其中一个比另一个具有更多的数据点。正则表达式搜索使用列表元素在大文档中查找匹配
文件的内容是这样的:
rs3094315 1 742429 AA
rs12562034 1 758311 GG
rs3934834 1 995669 CC
有每个字段之间的制表符。每个文件中大约有500,000行。
为了方便比较,我只想保留两个文件都包含的数据点,并放弃其中任何一个都独有的任何数据点。为此,我创建了所有独特的DNA位置列表,现在我试图搜索原始数据文件的每一行,并将不包含这些独特DNA位置的所有行打印到新文件中。
我的代码中的所有东西都已经运行起来,直到我尝试使用正则表达式来搜索基因组文件以打印所有非独特的DNA位置。我能拿到剧本打印for
循环内的LaurelSNP_left
列表中的所有项目,但是当我尝试使用re.match
每个项目,我收到此错误信息:
Traceback (most recent call last):
File "/Users/laurelhochstetler/scripts/identify_SNPs.py", line 57, in <module>
if re.match(item,"(.*)", Line):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 242, in _compile
p = sre_compile.compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_compile.py", line 500, in compile
p = sre_parse.parse(p, flags)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 673, in parse
p = _parse_sub(source, pattern, 0)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 308, in _parse_sub
itemsappend(_parse(source, state))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sre_parse.py", line 401, in _parse
if state.flags & SRE_FLAG_VERBOSE:
TypeError: unsupported operand type(s) for &: 'str' and 'int'
我的问题是双重的:
- 如何在正则表达式中使用我的列表?
- 有没有更好的方法来完成我在这里要做的事情?
这里是我的代码:
#!/usr/bin/env python
import re #this imports regular expression module
import collections
MomGenome=open('/Users/laurelhochstetler/Documents/genetics fun/genome_Mary_Maloney_Full_20110514145353.txt', 'r')
LaurelGenome=open('/Users/laurelhochstetler/Documents/genetics fun/genome_Laurel_Hochstetler_Full_20100411230740.txt', 'r')
LineNumber = 0
momSNP = []
LaurelSNP = []
f = open("mom_edit.txt","w")
for Line in MomGenome:
if LineNumber > 0:
Line=Line.strip('\n')
ElementList=Line.split('\t')
momSNP.append(ElementList[0])
LineNumber = LineNumber + 1
MomGenome.close()
for Line in LaurelGenome:
if LineNumber > 0:
Line=Line.strip('\n')
ElementList=Line.split('\t')
LaurelSNP.append(ElementList[0])
LineNumber = LineNumber + 1
momSNP_multiset = collections.Counter(momSNP)
LaurelSNP_multiset = collections.Counter(LaurelSNP)
overlap = list((momSNP_multiset and LaurelSNP_multiset).elements())
momSNP_left = list((momSNP_multiset - LaurelSNP_multiset).elements())
LaurelSNP_left = list((LaurelSNP_multiset - momSNP_multiset).elements())
LaurelGenome=open('/Users/laurelhochstetler/Documents/genetics fun/genome_Laurel_Hochstetler_Full_20100411230740.txt', 'r')
i = 0
for Line in LaurelGenome:
for item in LaurelSNP_left:
if i < 1961:
if re.match(item, Line):
pass
else:
print Line
i = i + 1
LineNumber = LineNumber + 1
你能提供一个输入文件的例子吗? – Thomas 2012-02-18 22:26:26
酷,看到人们使用他们的23和我的原始数据! – Stedy 2012-02-18 22:27:41
好的,在文件中添加了一些行的例子!是的,Stedy,我为了家谱目的而疯狂地使用等位基因数据! – blizpix 2012-02-18 22:41:12