2015-03-03 80 views
1

我有一个包含序列的大文件;我只想分析最后一组字符,它们的长度是可变的。在每一行中,我希望将每个集合的第一个字符和最后一个字符放在文本文件中,并计算这些字符的总实例。如何计算python中分区字符的出现次数?

这里是文件中的数据的一个示例:

-1iqd_BA_0_CDRH3.pdb Kabat的H3 P DPDAFD V

-1iqw_HL_0_CDRH3.pdb Kabat的H3 Ñ RDYSNNWYFD V

我想取“H3”和最后一个字符后面的第一个字符(在例子中都用粗体表示)。 为这两条线的输出应为:

第一计数器({ 'N':1, 'P':1})

最后计数器({ 'V':2})

这是我迄今所做的:

f = open("C:/CDRH3.txt", "r") 
from collections import Counter 
grab = 1 
for line in f: 
    line=line.rstrip() 
    left,sep,right=line.partition(" H3 ") 
    if sep: 
     AminoAcidsFirst = right[:grab] 
     AminoAcidsLast = right[-grab:] 
print ("first ",Counter(line[:] for line in AminoAcidsFirst)) 
print ("last ",Counter(line[:] for line in AminoAcidsLast)) 
f.close() 

这仅打印数据的最后一行看起来的数,如:

first Counter({'N': 1}) 
last Counter({'V': 1}) 

如何计算文件中所有行中的所有这些字符? 备注: 打印(AminoAcidsFirst)或(AminoAcidsLast)给出了所有垂直行的列表,但我无法对其进行计数或将其输出到文件。写入新文件只会写入原始文件最后一行的字符。 谢谢!

+0

你需要保持第一个和最后一个字符的计数是分开还是可以在同一个计数器中? – wwii 2015-03-03 18:03:21

回答

0

创建2名空列表并在每个循环追加像这样:

f = open("C:/CDRH3.txt", "r") 
from collections import Counter 
grab = 1 
AminoAcidsFirst = [] 
AminoAcidsLast = [] 
for line in f: 
    line=line.rstrip() 
    left,sep,right=line.partition(" H3 ") 
    if sep: 
     AminoAcidsFirst.append(right[:grab]) 
     AminoAcidsLast.append(right[-grab:]) 
print ("first ",Counter(line[:] for line in AminoAcidsFirst)) 
print ("last ",Counter(line[:] for line in AminoAcidsLast)) 
f.close() 

这里:

  1. 创建空的列表:

    AminoAcidsFirst = [] AminoAcidsLast = []

  2. 在每个追加循环:

    AminoAcidsFirst.append(right[:grab]) AminoAcidsLast.append(right[-grab:])

+0

这对我使用Py3非常有效,我可以输出到一个文件。非常感谢! – 2015-03-03 18:21:45

2

无需计数器:只是抢到最后一个令牌后split ING和计数第一个和最后一个字符:

first_counter = {} 
last_counter = {} 
for line in f: 
    line=line.split()[-1] # grab the last token 
    first_counter[line[0]] = first_counter.get(line[0], 0) + 1 
    last_counter[line[-1]] = last_counter.get(line[-1], 0) + 1  

print("first ", first_counter) 
print("last ", last_counter) 

输出

first {'P': 1, 'N': 1} 
last {'V': 2} 
+0

这会返回一个错误:'第4行,在0. builtins.IndexError:列表索引超出范围'。使用Py3。更改了print()格式。我不确定是否还有其他遗漏。不过谢谢。 – 2015-03-03 18:45:38

+0

@ BioEng-Mike是的,'print'已经从内建移到了Python3中的一个函数中(所以你需要添加括号)。我会更新答案。 – alfasin 2015-03-03 18:46:51

0

两个我想指出的重要事情

  1. 绝不泄露您的计算机上的文件路径,如果你是来自科学界,这一点尤其适用

  2. 你的代码可以使用with...as方法

,现在的计划是更Python

from collections import Counter 

filePath = "C:/CDRH3.txt" 
AminoAcidsFirst, AminoAcidsLast = [], [] # important! these should be lists 

with open(filePath, 'rt') as f: # rt not r. Explicit is better than implicit 
    for line in f: 
     line = line.rstrip() 
     left, sep, right = line.partition(" H3 ") 
     if sep: 
      AminoAcidsFirst.append(right[0]) # really no need of extra grab=1 variable 
      AminoAcidsLast.append(right[-1]) # better than right[-grab:] 
print ("first ",Counter(AminoAcidsFirst)) 
print ("last ",Counter(AminoAcidsLast)) 

不要做line.strip()[-1]因为sep验证是很重要的

输出

first {'P': 1, 'N': 1} 
last {'V': 2} 

注:数据文件能得到真正的大,你可能会遇到内存问题或计算机挂起。那么,我可以建议懒读吗? Folloing更强有力的项目

from collections import Counter 

filePath = "C:/CDRH3.txt" 
AminoAcidsFirst, AminoAcidsLast = [], [] # important! these should be lists 

def chunk_read(fileObj, linesCount = 100): 
    lines = fileObj.readlines(linesCount) 
    yield lines 

with open(filePath, 'rt') as f: # rt not r. Explicit is better than implicit 
    for aChunk in chunk_read(f): 
     for line in aChunk: 
      line = line.rstrip() 
      left, sep, right = line.partition(" H3 ") 
      if sep: 
       AminoAcidsFirst.append(right[0]) # really no need of extra grab=1 variable 
       AminoAcidsLast.append(right[-1]) # better than right[-grab:] 
print ("first ",Counter(AminoAcidsFirst)) 
print ("last ",Counter(AminoAcidsLast)) 
+0

感谢您提示不透露文件位置的提示。第一套代码对我的目的来说工作得很好。第二个文件在我将“linesCount”调整为等于文件中字符总数的数量后起作用。我正在查看最多1000行的大小为36 kb的文件,因此第一个版本就足够了。谢谢!非常感谢。 – 2015-03-03 18:38:49

+0

欢迎伴侣:) – aim100k 2015-03-04 01:41:21

0

如果你把在的底部或之后的语句你的for循环打印AminoAcidsFirstAminoAcidsLast,你会看到,在每次迭代你只是分配一个新的值。您的意图应该是收集,包含或累积这些值,然后再将它们送到collections.Counter

s = ['-1iqd_BA_0_CDRH3.pdb kabat H3 PDPDAFDV', '-1iqw_HL_0_CDRH3.pdb kabat H3 NRDYSNNWYFDV'] 

为代码立即解决此问题是累积的人物:

grab = 1 
AminoAcidsFirst = '' 
AminoAcidsLast = '' 
for line in s: 
    line=line.rstrip() 
    left,sep,right=line.partition(" H3 ") 
    if sep: 
     AminoAcidsFirst += right[:grab] 
     AminoAcidsLast += right[-grab:] 
print ("first ",collections.Counter(AminoAcidsFirst)) 
print ("last ",collections.Counter(AminoAcidsLast)) 

另一种方法是生产对需求的字符。定义一个发电机的功能,将产生要算

def f(iterable): 
    for thing in iterable: 
     left, sep, right = thing.partition(' H3 ') 
     if sep: 
      yield right[0] 
      yield right[-1] 

然后使用一个文件作为数据源,饲料,为collections.Counter

z = collections.Counter(f(s)) 

或者事情:

with open('myfile.txt') as f1: 
    # lines is a generator expression 
    # that produces stripped lines 
    lines = (line.strip() for line in f1) 
    z = collections.Counter(f(lines)) 
相关问题