开发者

python condition and calculate the sum?

开发者 https://www.devze.com 2023-03-31 12:34 出处:网络
Write a function that compares two DNA sequences based on the following scoring scheme: +1 for a match, +3 for each consecutive match and -1 for each mismatch.

Write a function that compares two DNA sequences based on the following scoring scheme: +1 for a match, +3 for each consecutive match and -1 for each mismatch.

MY CODE:

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:'+ 开发者_StackOverflow社区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

Could anyone give me the tips?

thanks


if signed=='||':

This code only matches if signed is exactly equal to '||'.

You want to look at the last two chars, so try:

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

I think you also have a calculation error in the score calculation.

Here's a fixed and cleaned up version for your function:

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)


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

精彩评论

暂无评论...
验证码 换一张
取 消