forked from CPMurray/PythonForBiologists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPfB_ch5_exercises.py
78 lines (64 loc) · 2.7 KB
/
PfB_ch5_exercises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Created on Sun May 24 10:12:18 2015
@author: colin
"""
# (1) percentage of amino acid residues, part 1
# write a function that takes two arguments, a protein sequence
# and an amino acid residue code
# this function returns percentage content of specified amino
# acid residue in specified peptide
#def get_aa_percentage(peptide,aa_residue):
# # homogenize case
# peptide = peptide.upper()
# aa_residue = aa_residue.upper()
#
# # total number of residues in peptide
# len_peptide = len(peptide)
#
# # number of specified amino acid residues in peptide
# num_aa_residues = peptide.count(aa_residue)
#
# # return percentage content of specified aa residue
# return 100.0*num_aa_residues/len_peptide
## end function get_aa_percentage
## begin main program
#assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", "M") == 5
#assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", "r") == 10
#assert get_aa_percentage("msrslllrfllfllllpplp", "L") == 50
#assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", "Y") == 0
#print("All assertions passed")
#########################################################
# (2) percentage of amino acid residues, part 2
# modify functio from part 1 so that it accepts a list of
# residues. if no list is given, the function should return
# the percentage of hydrophobic amino acid residues:
# A,I,L,M,F,W,Y,V
# this function returns percentage content of specified amino
# acid residues in specified peptide
def get_aa_percentage(peptide,
aa_residue=['A','I','L','M','F','W','Y','V']):
# homogenize case
peptide = peptide.upper() # peptide is character
for index in range(len(aa_residue)): # aa_residue is list
aa = aa_residue[index] # aa is character
#print(type(aa) is str) # diagnostic: check aa type
aa_residue[index] = aa.upper()
#print(aa_residue) # diagnostic
# total number of residues in peptide
len_peptide = len(peptide)
# number of specified amino acid residues in peptide
num_aa_residues = 0 # initialize count
for index in range(len(aa_residue)):
aa = aa_residue[index]
num_aa_residues = num_aa_residues + peptide.count(aa)
#print("num_aa_residues ",num_aa_residues,"index ", index)
# return percentage content of specified aa residue
return 100.0*num_aa_residues/len_peptide
# end function get_aa_percentage
# begin main program
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ["M"]) == 5
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ['M', 'L']) == 55
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP", ['F', 'S', 'L']) == 70
assert get_aa_percentage("MSRSLLLRFLLFLLLLPPLP") == 65
print("All assertions passed")