I have a fasta file as shown below. I would like to convert the three letter codes to one letter code. How can I do this with python or R?
>2ppo
ARGHISLEULEULYS
>3oot
METHISARGARGMET
desired output
>2ppo
RHLLK
>3oot
MHRRM
your suggestions would be appreciated!!
BioPython already has built-in dictionaries to help with such translations. Following commands will show you a whole list of available dictionaries:
import Bio
help(Bio.SeqUtils.IUPACData)
The predefined dictionary you are looking for:
Bio.SeqUtils.IUPACData.protein_letters_3to1['Ala']
Use a dictionary to look up the one letter codes:
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'}
And a simple function to match the three letter codes with one letter codes for the entire string:
def shorten(x):
if len(x) % 3 != 0:
raise ValueError('Input length should be a multiple of three')
y = ''
for i in range(len(x) // 3):
y += d[x[3 * i : 3 * i + 3]]
return y
Testing your example:
>>> shorten('ARGHISLEULEULYS')
'RHLLK'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With