I wrote an algorithm that analyzes protein-RNA interactions and I found that the following function is the bottleneck that causes performance issues:
import numpy as np
#len(protein_sequence)~500, len(rna_sequence)~1500
def affinity_matrix(protein_sequence, rna_sequence):
python_matrix = [[] for _ in range(len(protein_sequence))]
for i, AA in enumerate(protein_sequence):
for base in rna_sequence:
python_matrix[i].append(scales[base][AA])
#(Where "scales" is a small dict() with the structure:
#scales[base][AA] = float(), with 4 bases and 20 AA, so 80 values total.)
return(np.array(python_matrix))
I suspect 2 problems in this code, but I don't know how to solve them:
- I am retrieving values from this
scales
dict
millions of times, and it's said calling values from "non-static" data structures likedict()
is slow. So how can I make this small dictionary "static" instead? (This dictionary is only created once, while therna_sequence
andprotein_sequence
will be different each time I call the function.) - I am building the matrix at first with pythons tools, and later I convert it into the (faster) NumPy array. Possibly it is faster to directly create it with NumPy, but I am not sure if this is possible.
I am thankful for any tips how to improve this code or references to guides that would help in this case.
Here is example data, in case you want to try out the algorithm:
rna_sequence = 'ACAGGAGGAGCCGCUCGCUGGCGGCUGAUCCAGCGUCUCCGUGACAGGCACCCUGCUCCGCCGCCACCGCCACCGCCACCGCCACCGUCGCCUUUUCUUCUUCGUCCCGGGCGGUGCGUUCCACUGCUCUGGGGCCGGCGCCGCGCCCAGUCCCGCUUCGGGCCGCAAGCCCCACCGCUCCCCUCCCCGGGCAGGGGCGCCGCGCAGCCCGCUCCCGCCGCCACCUCCUCCCCUGCCGCCCUCCUAGCCGGCAGGAAUUGCGCGACCACAGCGCCGCUCGCGUCGCCCGCAUCAGCUCAGCCCGCUGCCGCUCGGCCCUCGGCACCGCUCCGGGUCCGGCCGCCGCGCGGCCAGGGCUCCCCCUGCCCAGCGCUCCCAGGCCCCGCCACGCGUCGCCGCGCCCAGCUCCAGUCUCCCCUCCCCGGGGUCUCGCCAGCCCCUUCCUGCAGCCGCCGCCUCCGAAGGAGCGGGUCCGCCGCGGGUAACCAUGCCUAGCAAAACCAAGUACAACCUUGUGGACGAUGGGCACGACCUGCGGAUCCCCUU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UUCCUUUUAUUUACUCUUUGCUUGACUGCUUCCUCCUAACCCUCUACCCACUAGCACUCUACUUCCUAAAGCUGUUGUGUCAUUAACUCUGUUGGAUCAACUCUCUGGGAAAAGAUUCUGUUAAUGUAAGUGCACUUACUCCCUGGAUGUUGUCACUAGUCUAGUGGCUUUUGCUAAAUAAACCUUUCUUAUUUCUA'
protein_sequence = 'MPSKTKYNLVDDGHDLRIPLHNEDAFQHGICFEAKYVGSLDVPRPNSRVEIVAAMRRIRYEFKAKNIKKKKVSIMVSVDGVKVILKKKKKLLLLQKKEWTWDESKMLVMQDPIYRIFYVSHDSQDLKIFSYIARDGASNIFRCNVFKSKKKSQAMRIVRTVGQAFEVCHKLSLQHTQQNADGQEDGESERNSNSSGDPGRQLTGAERASTATAEETDIDAVEVPLPGNDVLEFSRGVTDLDAVGKEGGSHTGSKVSHPQEPMLTASPRMLLPSSSSKPPGLGTETPLSTHHQMQLLQQLLQQQQQQTQVAVAQVHLLKDQLAAEAAARLEAQARVHQLLLQNKDMLQHISLLVKQVQELELKLSGQNAMGSQDSLLEITFRSGALPVLCDPTTPKPEDLHSPPLGAGLADFAHPAGSPLGRRDCLVKLECFRFLPPEDTPPPAQGEALLGGLELIKFRESGIASEYESNTDESEERDSWSQEELPRLLNVLQRQELGDGLDDEIAV'
scales = {'A': {'A': -0.103534,
'C': -0.141027,
'D': -0.057364,
'E': 0.025673,
'F': -0.160025,
'G': 0.155926,
'H': 0.114486,
'I': -0.064125,
'K': 0.058532,
'L': -0.093059,
'M': -0.093239,
'N': 0.211717,
'P': -0.187549,
'Q': 0.286417,
'R': 0.045458,
'S': -0.068299,
'T': -0.256675,
'V': -0.126037,
'W': -0.352338,
'Y': -0.059715},
'C': {'A': -0.170978,
'C': 0.460587,
'D': 0.011352,
'E': 0.456847,
'F': -0.157944,
'G': 0.209027,
'H': -0.166851,
'I': -0.016438,
'K': 0.093534,
'L': 0.112175,
'M': -0.08656,
'N': 0.439522,
'P': -0.189412,
'Q': -0.031332,
'R': 0.009869,
'S': -0.357084,
'T': -0.360561,
'V': 0.248514,
'W': 0.417484,
'Y': 0.059507},
'G': {'A': 0.157098,
'C': 0.006956,
'D': 0.047448,
'E': -0.193184,
'F': 0.480436,
'G': -0.279106,
'H': 0.144117,
'I': 0.170305,
'K': -0.186721,
'L': 0.177462,
'M': -0.139056,
'N': -0.225754,
'P': 0.097558,
'Q': -0.085275,
'R': -0.055897,
'S': 0.273463,
'T': 0.166763,
'V': 0.108108,
'W': 0.093968,
'Y': 0.263202},
'U': {'A': 0.130008,
'C': -0.012462,
'D': 0.005557,
'E': 0.043517,
'F': -0.269202,
'G': -0.031819,
'H': -0.198936,
'I': -0.084679,
'K': 0.096389,
'L': -0.153234,
'M': 0.351179,
'N': -0.215165,
'P': 0.240871,
'Q': -0.065384,
'R': 0.005856,
'S': 0.31803,
'T': 0.264754,
'V': -0.078486,
'W': 0.094917,
'Y': -0.118544}}
for
loop? \$\endgroup\$