Search code examples
pythonfunctiondictionaryreturnbiopython

convert single letter code to 3 letter code along with the chain numbering


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

Solution

  • This also can be achieved by using zip function, and 'for loop'.

    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, sep='')
    
    
        
    A= 'GPHMA'
    B= range(250,254)
    for A,B in zip(A,B):
        triple_aa(A,B)
    

    Output will be:

    GLY250
    PRO251
    HIS252
    MET253