2015-03-03 77 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)) 
相關問題