2011-08-26 111 views
0

根據以下評分方案編寫一個比較兩個DNA序列的函數:匹配+1,每個連續匹配+3,每個不匹配-1。python條件並計算總和?

我的代碼:

def pairwiseScore(seqA, seqB): 
    '''1+1+3-1-1+1+3+1-1-1+1-1-1-1''' 
    print 
    signed = '' 
    score = 0 
    for i in range(len(seqA)): 
    if(seqA[i] == seqB[i]):   
     signed += '|' 
     score += 1 
     if signed=='||': 
      score=0 
      score += 3 
     else: 
      score += 1 

    else: 
     signed += ' ' 
     score -=1 
    return seqA+"\n"+signed+"\n"+seqB+"\n"+'score:'+ str(score) 

print pairwiseScore("ATTCGT", "ATCTAT") 
print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA") 
print pairwiseScore('ATCG', 'ATCG') 
print pairwiseScore('CATTCATCATGCAA', 'GATAAATCTGGTCT') 

OUPUT:

ATTCGT 
|| | 
ATCTAT 
score:2 

GATAAATCTGGTCT 
|| ||| | 
CATTCATCATGCAA 
score:4 

ATCG 
|||| 
ATCG 
score:7 // this should be 10 because +3 for each consecutive match` 1 + 3 +3 +3 =10 

CATTCATCATGCAA 
|| ||| | 
GATAAATCTGGTCT 
score:4 

誰能給我提示嗎?

感謝

+0

提示:使用一致的縮進。除此之外,你有什麼特別的問題? – geoffspear

+0

看看分數:7我應該得分:10因爲每個連續比賽都會得到+3的分數' – kn3l

+0

看起來像功課。請關注你的問題。如問題所述,目前還不清楚要問什麼,它將被關閉。 –

回答

1
if signed=='||': 

這個代碼僅簽署匹配正好等於'||'

你想看看最後兩個字符,所以嘗試:

if signed[-2:] == '||': 

我想你也有得分計算的計算錯誤。

下面是你的功能的固定和清理版本:

def pairwiseScore(seqA, seqB): 
    signed = '' 
    score = 0 
    for i in range(len(seqA)): 
     if seqA[i] == seqB[i]: 
      signed += '|' 
      if signed[-2:] == '||': 
       score += 3 
      else: 
       score += 1 
     else: 
      signed += ' ' 
      score -= 1 
    return '%s\n%s\n%s\nscore:%d' % (seqA, signed, seqB, score) 
+0

舉例:ATCG |||| ATCG分數應該是:1 + 3 + 3 + 3 = 10如果你把這個分數放在3 – kn3l

+0

是的,因爲你有一個計算錯誤。 FTFY。 –

+0

感謝Yuval Adam – kn3l

1

Look here

def pairwiseScore(seqA, seqB): 
    '''1+1+3-1-1+1+3+1-1-1+1-1-1-1''' 
    print 
    signed = '' 
    score = 0 
    for i in range(len(seqA)): 
    if(seqA[i] == seqB[i]):   
     signed += '|' 
     if i > 0 and signed[len(signed)-2]=='|': 
      score += 3 
     else: 
      score += 1 

    else: 
     signed += ' ' 
     score -=1 
    return seqA+"\n"+signed+"\n"+seqB+"\n"+'score:'+ str(score) 

print pairwiseScore("ATTCGT", "ATCTAT") 
print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA") 
print pairwiseScore('ATCG', 'ATCG') 
print pairwiseScore('CATTCATCATGCAA', 'GATAAATCTGGTCT') 
+0

其他方法,謝謝Fedor Skrynnikov – kn3l