Home > Software engineering >  convert single letter code to 3 letter code along with the chain numbering
convert single letter code to 3 letter code along with the chain numbering

Time:06-13

I have a string having single letter AA code ('GPHMAQGTLI'). I want to convert into its respective 3 letter code and give the numbering in a range (300-309) in the same order.

I have written the following but because of 'return' function it is converting only first letter of string

d = {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
    'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N', 
     'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W', 
     'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M'}

def triple_aa(val, num):
    for key, value in d.items():
         if val == value:
             print(key, num)
     
    
def single_aa(x):
    for i in x:
        #print(i)
        return i
        
        

def chain_no(a,b):
    for i in range(a,b 1,1):
        #print(i)
        return i
        
    
    
A= single_aa('GPHMA')
B= chain_no(250,254)
triple_aa(A,B) 

expected output is as below but I am getting only 'GLY250'

print((triple_aa(A,B)) 
GLY250
PRO251
HIS252
MET253

CodePudding user response:

Here is what you could do. First flip key, value of the dict to search easier, then loop through your letter AA code.

EDIT

As function with input arguments as you have it:

def triple_aa(seq, dic, rng_start):   
    d_flip = {v:k for k,v in dic.items()}
    res = []
    for idx, char in enumerate(seq, rng_start):
        tmp = f"{d_flip.get(char, 'XXX')}{idx}"
        #print(tmp)
        res.append(tmp)
    return res

res = triple_aa('GPHMAQGTLI', d, 300)
print(res)

without function:

d = {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
    'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N', 
     'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W', 
     'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M'}
d_flip = {v:k for k,v in d.items()}

s = 'GPHMAQGTLI'

#res = [] # res if you want to access the combinations later
for idx, char in enumerate(s, 300):
    tmp = f"{d_flip.get(char, 'XXX')}{idx}" #if key can't be found 'XXX' as default value
    print(tmp)
    #res.append(tmp)
    
#print(res)

GLY300
PRO301
HIS302
MET303
ALA304
GLN305
GLY306
THR307
LEU308
ILE309

Let me know if that works for you

  • Related