Mikołaj Szymon Halber Mikołaj Szymon Halber - 2 months ago 27
Python Question

Pyschools Topic 3 Q 11 - DNA parity check

I have no idea how to get around this, it is DNA parity check in pyschools

Pairwise comparision of DNA sequences is a popular technique used in Bioinformatics. It usually involves some scoring scheme to express the degree of similarity. 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.

Examples:

>>> print pairwiseScore("ATTCGT", "ATCTAT")
ATTCGT
|| |
ATCTAT
Score: 2
>>> print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")
GATAAATCTGGTCT
|| ||| |
CATTCATCATGCAA
Score: 4

Answer Source

fun python challenges!

basically you want to run through the strings at the same time

Sudo code :

previous = false;
for index in stringA: 
   if stringA[index] == stringB[index]: 
      +1 or +3 based on previous
   else 
      -1
   previous = stringA[index] == stringB[index]

print formatted string. 

python 2 with comments.

zip acts differently in python 3

def pairwiseScore(seqA, seqB): 
   # final score to keep track of the matches in DNA
   score = 0 
   # String helper this will tell us if a match happened previously
   #  and it will be used to show the relationships between the seqA and seqB
   prevMatch = ""   
   # https://docs.python.org/2/library/functions.html#zip
   # zip will transpose the two lists (char lists) into tuples based on their indices
   #  the we can use A,B to determine likeness of the two strings.
   for (A,B) in zip (seqA,seqB): # sync run through the lists (char lists) 
      if  A == B :              
         # if the string prevMatch has at least one character 
         #  and the last one is a '|' +3 otherwise +1
         score += 3 if len(prevMatch) > 0 and prevMatch[-1] == '|' else 1         
      else :         
         #strings don't match -1
         score -= 1
      # add the bar for the equal chars in strings. to output and 
      #  test for previous Match '| || |' etc... 
      prevMatch += '|' if ( A == B ) else ' '

   # basically asking for that string seqA, matches, seqB, score : #
   #  return a string 
   # GATAT    // seqA
   # | ||     // prevMatch
   # GCTAC    // seqB
   # Score: 3 // score
   return "{}\n{}\n{}\nScore: {}".format(seqA,prevMatch,seqB, score)

but seriously dont ask questions that you dont show any work or links to pages that require logins or other redirects.

Read the FAQs about asking questions. https://stackoverflow.com/help/asking