# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 09:30:47 2015

@author: martin
"""
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SubsMat.MatrixInfo import blosum62

#remarque : Ceci n'est pas une maélioration de modifDoublon,
#c'est une version alternative qui ne s'applique pas aux même doublons.
def score_match(a,b, matrix):
    if
def modifDoublon2(x,y,z,ldna,offset,doublon,i,q,patternX,patternY):
    ATCG = ["A","T","C","G"]
    # on commence par déterminer quels acides aminés de x et y sont "ciblés"
    aaX = Seq("", IUPAC.protein)
    aaY = Seq("", IUPAC.protein)
    q_bis = 0.
    q_bis += q
    
    if(z<=0):
        aaX += x[i]
        q_bis /= patternX[i]
    else:
        aaX += x[i+1+z//3]
        q_bis /=patternX[i+1+z//3]
    if(z>0):
        aaY += y[-i]
        q_bis*=patternY[-i]
    else:
        aaY +=y[-i + z//3]
        q_bis*=patternY[-i + z//3]
        
    scores = []
    for a in ATCG:
        for b in ATCG:
            currentDNAx = Seq("", IUPAC.unambiguous_dna)
            currentDNAy = Seq("", IUPAC.unambiguous_dna)
            currentDNAx += a + b + ldna[doublon+2]
            currentaaX = currentDNAx.translate()
            currentDNAy += ldna[doublon-1] +a + b
            currentaaY = currentDNAy.reverse_complement().translate()
            score = score_match(aaX[0].upper(),currentaaX[0].upper(),blosum62)
            score += q_bis*score_match(aaY[0].upper(),currentaaY[0].upper(),blosum62)
            scores.append(score)
    
    result = scores.index(max(scores))
    ldna[doublon] = ATCG[result//4]
    ldna[doublon+1]= ATCG[result%4]
    
            
 b == '*' :
        return -500000000000000000000000000
    elif a == '-' :
        return 0
    elif (a,b) not in matrix:
        return matrix[(tuple(reversed((a,b))))]
    else:
        return matrix[(a,b)]

