Friday, August 7, 2015

Bioinformatics in Python: amino acid residues

Abstract: hunt amino acid residues and calculate its percentage

The result:
In: MSRSLLLRFLLFLCLLLPPLP
({'P': 3}, {'P': 14.29})
({}, {})
({'P': 3, 'C': 1, 'M': 1, 'h': 0}, {'P': 14.29, 'C': 4.76, 'M': 4.76, 'h': 0.0})
({'LP': 2}, {'LP': 9.52})


The script:
# -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 18:33:11 2015

@author: yuan
"""
import re

class amino_acid:
    def __init__(self, aa_seq):
        self.aa_seq=aa_seq.upper()
        self.seq_len=len(self.aa_seq)
        print 'In:', self.aa_seq
       
    def residue_percentage(self,query_aa):
        aa_counts={}
        aa_perc={}
        for aa in query_aa:
            aa.upper()
            target=re.findall(aa, self.aa_seq)
            aa_counts[aa]=0 if target==[] else len(target)
            aa_perc[aa]=aa_counts[aa]*100/float(self.seq_len)
            aa_perc[aa]=round(aa_perc[aa], 2)
        return aa_counts, aa_perc
           
               

if __name__=="__main__":
    aa="MSRSLLLRFLLFLCLLLPPLP"
    a=amino_acid(aa)
    print a.residue_percentage(['P'])
    print a.residue_percentage([])
    print a.residue_percentage(['P', 'h', 'C','M'])
    print a.residue_percentage(['LP'])

No comments:

Post a Comment