Python Programs for Beginner Bioinformatics — Part 5

Input: 
AAGCC
TTTCT
Output:
{'A': [1,1,0,0,0], 'C': [0,0,0,2,1], 'G': [0,0,1,0,0], 'T': [0,0,0,1,0]}
Input: 
AACGTA
CCCGTT
CACCTT
GGATTA
TTCCGG
Output:
{'A': [1, 2, 1, 0, 0, 2], 'C': [2, 1, 4, 2, 0, 0], 'G': [1, 1, 0, 2, 1, 1], 'T': [1, 1, 0, 1, 4, 2]}
def Count(Motifs):
return {symbol: count_array(symbol, Motifs) for symbol in "ACGT"}

def count_array(symbol, motifs):
individual_count_arrays = [symbol_count_array_for_single_motif(symbol, motif) for motif in motifs]
return [sum(elements) for elements in zip(*individual_count_arrays)]

def symbol_count_array_for_single_motif(symbol, motif):
return [1 if current_symbol == symbol else 0 for current_symbol in motif]
Input: 
AGA
TCA
TCA
The Count Matrix:
{'A':[1,0,1], 'T':[2,0,0], 'C':[0,2,0], 'G':[0,1,0]}
The Profile Matrix:
{'A':[1/3,0,1], 'T':[2/3,0,0], 'C':[0,2/3,0], 'G':[0,1/3,0]}
def Profile(Motifs):
count = Count(Motifs)
number_of_motifs = len(Motifs)
return {symbol: motif_profile_for_symbol(count[symbol], number_of_motifs) for symbol in "AGCT"}


def motif_profile_for_symbol(count_array_for_symbol, number_of_motifs):
return list(map(lambda x: x / number_of_motifs, count_array_for_symbol))
Input: 
AGA
TCA
TCA
Profile Matrix:
{'A':[1/3,0,1], 'T':[2/3,0,0], 'C':[0,2/3,0], 'G':[0,1/3,0]}
def Consensus(Motifs):
count_matrix = Count(Motifs)
symbols = "ACGT"
string_length = len(Motifs[0])
consensus_list = [consensus_symbol_at_index(count_matrix, i, symbols) for i in range(string_length)]
return ''.join(consensus_list)

def consensus_symbol_at_index(count_matrix, index, symbols):
count_to_symbols_tuple = {count_matrix[symbol][index]: symbol for symbol in symbols}
max_count = max(count_to_symbols_tuple.keys())
return count_to_symbols_tuple[max_count]
def HammingDistance(p, q):
return sum(list(map(lambda x, y: int(x != y), list(p), list(q))))

def Score(Motifs):
return sum(map(lambda motif: HammingDistance(motif, Consensus(Motifs)), Motifs))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store