In [1]:
import numpy as np
In [2]:
class aaMutationCostsDict():
    def __init__(self, name):
        self.costDict={}
        self.name=name
    def setCostDict(self, costDict):
        """
        Get dictionary of costs where key are two chars string denoted from which to which aa we mutate
        and thsi keys assigne cost values.
        """
        self.costDict=costDict
    def getMutationCost(self, orginalAA, mutatedAA):
        """
        Arguments should be one characters string denoted aa.
        """
        return self.costDict[orginalAA+mutatedAA]
    
    @staticmethod
    def createCostDictFromLists(aaValuesList, aaLabelsList):
        values=np.array(aaValuesList)
        matrix=(values-values.reshape((-1,1)))**2
        maxValue=matrix.max()
        matrix/=maxValue
        costDict={}
        for i in range(len(aaLabelsList)):
            for j in range(len(aaLabelsList)):
                costDict[aaLabelsList[i]+aaLabelsList[j]]=matrix[i,j]
        return costDict
    
    @staticmethod
    def createCostDictFromMatrix(aaMatrix, aaLabelsList):
        matrix=aaMatrix
        maxValue=matrix.max()
        matrix/=maxValue
        costDict={}
        for i in range(len(aaLabelsList)):
            for j in range(len(aaLabelsList)):
                costDict[aaLabelsList[i]+aaLabelsList[j]]=matrix[i,j]
        return costDict
In [5]:
def loadMutationCostDicts(pathCSV="./scales/skale_aa_8_wys.csv"):
    listOfScales=[]
    with open(pathCSV, 'r') as file:
        aaLabels=[]
        aaValues=[]
        for line in file:
            line=line[:-1]
            if line=="":
                continue
            listedLine=line.split('\t')
            if listedLine[0]=="":
                if listedLine[1]=="":
                    continue
                for i in range(1, len(listedLine)):
                    aaLabels.append(listedLine[i])
            else:
                for i in range(1, len(listedLine)):
                    aaValues.append(float(listedLine[i]))
                actualCreatedCostsDict=aaMutationCostsDict(listedLine[0])
                print(aaValues)
                print(aaLabels)
                costDict=actualCreatedCostsDict.createCostDictFromLists(aaValues, aaLabels)
                actualCreatedCostsDict.setCostDict(costDict)
                aaLabels=[]
                aaValues=[]
                listOfScales.append(actualCreatedCostsDict)
    return listOfScales
In [37]:
def loadMutationCostDictsFromMatrix(pathCSV="./scales/grantham.csv"):
    listOfScales=[]
    with open(pathCSV, 'r') as file:
        aaLabels=[]
        line=file.readline()
        line=line[:-1]
        if line=="":
            raise RuntimeError("Wrong format of:"+pathCSV)
        listedLine=line.split('\t')
        for i in range(0, len(listedLine)):
            aaLabels.append(listedLine[i])
        aaMatrix=np.loadtxt(file, max_rows=20)
        actualCreatedCostsDict=aaMutationCostsDict(listedLine[0])
        costDict=actualCreatedCostsDict.createCostDictFromMatrix(aaMatrix, aaLabels)
        actualCreatedCostsDict.setCostDict(costDict)
        aaLabels=[]
        aaValues=[]
        listOfScales.append(actualCreatedCostsDict)
    return listOfScales