
name = valueaaseq1 = 'MNKMDLVADVAEKTDLSKAKATEVIDAVFA'
aaseq2 = 'AARHQGRGAPCGESFWHWALGADGGHGHAQPPFRSSRLIGAERQPTSDCRQSLQ'
aaseq1
len(aaseq2)
aaseq3
aaseq3 = aaseq1
aaseq3
a = b = c = 0a = a + 1a += 1def name(parametr_list):
body
return statement is used to return a value from a function. return is usually followed by an expression whose value is returned as the value of the function. None . None is the simple statement pass .def fn():
pass
fn()
# A simple function for recognizing a binding site
def recognition_site(base_seq, recognition_seq):
return base_seq.find(recognition_seq)
recognition_site("ATGCATGCATGC","GCAT")
Compare the length of the sequence string to the sum of the number of Ts, Cs, As, and Gs in the sequence string. If the length is equal to that sum, the function returns True ; otherwise, there is a character in the parameter sequence that isn’t a valid base character, and the result will be False .
def validate_base_sequence(base_sequence):
"""Return True if string base_seqeunce contains only
upper-or lovercase T,C,A and G characters, otherwise False"""
seq = base_sequence.upper()
return len(seq) == (seq.count('T') + seq.count('C') +
seq.count('A') + seq.count('G'))
help(validate_base_sequence)
validate_base_sequence('tattattat')
validate_base_sequence('atgcwrqatgc')
Compute the GC content of a given DNA sequence represented as a string.
def gc_content(base_seq):
"""Returns the percentage of G and C chracters in base_seq"""
seq = base_seq.upper()
return (seq.count('G') + seq.count('C'))/ len(seq)
seq50 = 'AACCTTGG'
seq75 = 'ATCCCGGG'
seq40 = 'ATATTTCGCG'
gc_content(seq50)
gc_content(seq75)
gc_content(seq40)
assert expressionassert expression1, expression2assert 1 == 2
assert 1 == 2, 'invalid arguments'
def gc_content(base_seq):
"""Returns the precentage of G and C characters in base_seq"""
assert validate_base_sequence(base_seq), \
'argumnet has invalid arguments'
seq = base_seq.upper()
return ((base_seq.count('G') + base_seq.count('C'))/
len(base_seq))
validate_base_sequence more flexible by giving it the ability to handle RNA sequences too.def validate_base_sequence(base_sequence, RNAflag):
"""Returns True if the string base_sequence contains only
upper or lowercase T (or U, if RNAflag), C, A, G characters
otherwise False"""
seq = base_sequence.upper()
return len(seq) == (seq.count('U' if RNAflag else 'T') +
seq.count('C') + seq.count('A') +
seq.count('G'))
validate_base_sequence('ATGC', False)
validate_base_sequence('ATGC', True)
validate_base_sequence('AUCG', True)
def validate_base_sequence(base_sequence, RNAflag=False):
"""Returns True if the string base_sequence contains only
upper or lowercase T (or U, if RNAflag), C, A, G characters
otherwise False"""
seq = base_sequence.upper()
return len(seq) == (seq.count('U' if RNAflag else 'T') +
seq.count('C') + seq.count('A') +
seq.count('G'))
validate_base_sequence('ATCG')
validate_base_sequence('AUGC', True)
import nameos.pathimport os # provides an interface to the computer’s operating system.
os
os.getcwd()
import allows import of specific names from the module from modulename import name1, name2, ...from modulename import actualname as yournamefrom modulename import *from sys import version
version
random.randint takes two integer arguments and returns a random integer in the range of the first to the second inclusive.from random import randint
def random_base(RNAflag = False):
return ('UCAG' if RNAflag else 'TCAG')[randint(0,3)]
def random_codon(RNAflag = False):
return random_base(RNAflag) + random_base(RNAflag) + \
random_base(RNAflag)
random_codon()
def replace_base_randomly(base_seq):
"""Return a sequence with the base at a randomly selected position
of base_seq replaced by a base chosen randomly from the three bases
that are not at that position"""
print(base_seq)
position = randint(0, len(base_seq) - 1)
print(position)
bases = 'TCAG'.replace(base_seq[position],'')
print(bases)
return (base_seq[0:position])+ bases[randint(0,2)] \
+ base_seq[position+1:]
replace_base_randomly('ATGCATGCATGC')