Chapter 12 Functions in Python
A function is a block of code which only run when it is called. Data can be passed as parameters into a function.
A function can return data as a result.
12.1 Create a function to determine the GC-content of DNA sequence
def cg_content(dna):
= dna.count('C') + dna.count('G')
cg_number = len(dna)
length return float(cg_number/length)
= "CGCTAGTCAGT"
DNA_seq = cg_content(DNA_seq)
cg_content_val print("CG content is: " + str(cg_content_val))
## CG content is: 0.5454545454545454
= "CGCTAGTCAGTAAAAAAAAAAAAAAAAAAAAAAAAAATTT"
DNA_seq2 = cg_content(DNA_seq2)
cg_content_val print("CG content is: " + str(cg_content_val))
## CG content is: 0.15
12.2 Reverse complement
def reverse_complement(seq):
= {"A": "T", "T":"A" ,"C":"G","G":"C"}
complement = ""
rc_seq for base in seq:
= rc_seq+complement[base]
rc_seq return rc_seq
= "ACGT"
DNA_seq print("After reverse complement, the sequence is: " + reverse_complement(DNA_seq))
## After reverse complement, the sequence is: TGCA