nucleotideMap = { 'a':0, 'c':1, 'g':2, 't':3 } reverseMap = { 0:'a', 1:'c', 2:'g', 3:'t' } ScoreCount = 0 def getAndClearScoreCount(): global ScoreCount rval = ScoreCount ScoreCount = 0 return rval def Score(s, DNA, l): # s = list of starting indices, 1-based, 0 means ignore # DNA = list of nucleotide strings # l = Target Motif length global ScoreCount ScoreCount += 1 score = 0 for i in xrange(l): # loop over string positions cnt = [0 for x in xrange(4)] for j in xrange(len(s)): # loop over DNA strands sval = s[j] if (sval != 0): cnt[nucleotideMap[DNA[j][sval-1+i]]] += 1 score += max(cnt) return score def Consensus(s, DNA, l): # s = list of starting indices, 1-based, 0 means ignore # DNA = list of nucleotide strings # l = Target Motif length cstring = '' for i in xrange(l): # loop over string positions cnt = [0 for x in xrange(4)] for j in xrange(len(s)): # loop over DNA strands sval = s[j] if (sval != 0): cnt[nucleotideMap[DNA[j][sval-1+i]]] += 1 cstring += reverseMap[cnt.index(max(cnt))] return cstring if __name__ == "__main__": DNA = ["acatatag", "acatatag", "acatatag", "acatatag", "acatatag"] for s in DNA: print s print Score([1,1,1,1,1], DNA, 8), Consensus([1,1,1,1,1], DNA, 8) print Score([2,2,2,2,2], DNA, 6), Consensus([2,2,2,2,2], DNA, 6) print Score([2,2,0,2,2], DNA, 6), Consensus([2,2,0,2,2], DNA, 6) print Score([1,3,1,3,1], DNA, 4), Consensus([1,3,1,3,1], DNA, 4) print Score([1,2,3,4,5], DNA, 4), Consensus([1,2,3,4,5], DNA, 4) raw_input("Press [Enter] to quit")