<jupyter_start><jupyter_text>Case study: Bioinformatics - comparing genomes**Objectives*** In this case study, we will compare two DNA sequences: (1) human; vs. (2) bacteria**Dataset features**Escherichia coli strain U 5/41 16S ribosomal RNA gene, partial sequence* Rows: 37* Columns: 2* File format: fasta* Source: https://www.ncbi.nlm.nih.gov/nuccore/M10098.1?report=fasta Data Science: Visualização de Dados com Python Udemy's course:'DNA is a molecule present in all living beings, which is responsible for storing hereditary characteristics. It is composed of nucleotide sequences, which can be of four types: adenine, thymine, cytosine or guanine."Computerly" speaking we can represent them through 4 letters: A, T, C or G.In this case study, we want to assess whether structures with similar functions (we are using ribosomal RNA sequences) from different organisms have differences. For this we will evaluate the number of nucleotide pairs.' ![2018-11-18_22-54-37-e5c469c34cfe0984380af3738ed13a95.svg](attachment:2018-11-18_22-54-37-e5c469c34cfe0984380af3738ed13a95.svg)  Opening and reading files<jupyter_code># Reading the file as a list skiping the first line
human = open("human.fasta").readlines()[1:]
bacteria = open("bacteria.fasta").readlines()[1:]

# Concating the list elements
human = ''.join(human)
bacteria = ''.join(bacteria)

# For breaklines
human = human.replace("\n","")
bacteria = bacteria.replace("\n","")

# Creating and writing a new html file
comparison = open("human_bacteria_comparison.html", "w")

print(bacteria)<jupyter_output>AGTTTGATCATGGCTCAGATTGAACGCTGGCGGCAGGCCTAACACATGCAAGTCGAACGGTAACAGGAAGCAGCTTGCTGCTTTGCTGACGAGTGGCGGACGGGTGAGTAATGTCTGGGAAACTGCCTGATGGAGGGGGATAACTACTGGAAACGGTAGCTAATACCGCATAACGTCGCAAGCACAAAGAGGGGGACCTTAGGGCCTCTTGCCATCGGATGTGCCCAGATGGGATTAGCTAGTAGGTGGGGTAACGGCTCACCTAGGCGACGATCCCTAGCTGGTCTGAGAGGATGACCAGCAACACTGGAACTGAGACACGGTCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCACAATGGGCGCAAGCCTGATGCAGCCATGCGCGTGTATGAAGAAGGCCTTCGGGTTGTAAAGTACTTTCAGCGGGGAGGAAGGGAGTAAAGTTAATACCTTTGCTCATTGACGTTACCCGCAGAAGAAGCACCGGCTAACTCCGTGCCAGCAGCCGCGGTAATACGGAGGGTGCAAGCGTTAATCGGAATTACTGGGCGTAAAGCGCACGCAGGCGGTTTGTTAAGTCAGATGTGAAATCCCCGGGCTCAACCTGGGAACTGCATCTGATACTGGCAAGCTTGAGTCTCGTAGAGGGGGGTAGAATTCCAGGTGTAGCGGTGAAATGCGTAGAGATCTGGAGGAATACCGGTGGCGAAGGCGGCCCCCTGGACGAAGACTGACGCTCAGGTGCGAAAGCGTGGGGAGCAAACAGGATTAGATACCCTGGTAGTCCACGCCGTAAACGATGTCGACTTGGAGGTTGTGCCCTTGAGGCGTGGCTTCCGGATAACGCGTTAAGTCGACCGCCTGGGGAGTACGGCCGCAAGGTTAAAACTCAAATGAATTGACGGGGGCCGCACAAGCGGTGGAGCATGTGGTTTAATTCGATGCAACGCGAAGAACCTTACCTGGTCTTGACATCCACGGAAGTTTTCAG[...]<jupyter_text>Creating a dictionary<jupyter_code>count = {}
# Creating a Simple Arrangement of 4 elements taken 2 to 2.
for i in ['A', 'T', 'C', 'G']:
    for j in ['A', 'T', 'C', 'G']:
        count[i+j] = 0
        
count<jupyter_output><empty_output><jupyter_text>Counting human and bacteria nucleotide pairs<jupyter_code># Distributing
human_count = count
bacteria_count = count

# Counting how many combinations are there in the human file
for k in range(len(human)-1):
        human_count[human[k]+human[k+1]] += 1

print(human_count)

# Counting how many combinations are there in the bacterian file
for k in range(len(bacteria)-1):
        bacteria_count[bacteria[k]+bacteria[k+1]] += 1

print(bacteria_count)<jupyter_output>{'AA': 116, 'AT': 95, 'AC': 97, 'AG': 113, 'TA': 85, 'TT': 113, 'TC': 109, 'TG': 111, 'CA': 87, 'CT': 105, 'CC': 184, 'CG': 170, 'GA': 134, 'GT': 105, 'GC': 155, 'GG': 184}
{'AA': 221, 'AT': 159, 'AC': 183, 'AG': 224, 'TA': 149, 'TT': 170, 'TC': 169, 'TG': 219, 'CA': 170, 'CT': 180, 'CC': 258, 'CG': 267, 'GA': 247, 'GT': 198, 'GC': 264, 'GG': 331}<jupyter_text>Printing on a HTML page<jupyter_code># HTML part
i = 1
for k in count:
    transparency = human_count[k]/max(human_count.values())
    comparison.write("<div style='width:100px; border:1px solid #111; color:#fff; height:100px; float:left; background-color:rgba(0, 0, 0, "+str(transparency)+")'>"+k+"</div>\n")
    if i % 4 == 0:
        comparison.write("<div style='clear:both'></div>")
    i += 1
    
comparison.close()
import matplotlib.pyplot as plt
import numpy as np

''' Example
chess = np.array([[1,0,1,0,1,0,1,0],
                  [0,1,0,1,0,1,0,1],
                  [1,0,1,0,1,0,1,0],
                  [0,1,0,1,0,1,0,1],
                  [1,0,1,0,1,0,1,0],
                  [0,1,0,1,0,1,0,1],
                  [1,0,1,0,1,0,1,0],
                  [0,1,0,1,0,1,0,1]])
'''
human_transparency = np.array([human_count[k]/max(human_count.values()) for k in count]).reshape(4,4)
print(human_transparency)

plt.figure(figsize=(8,8))
plt.imshow(human_transparency, cmap='gray_r') # gray_r reverse scale
plt.axis(False)
plt.show()<jupyter_output>[[0.66767372 0.48036254 0.55287009 0.67673716]
 [0.45015106 0.51359517 0.51057402 0.66163142]
 [0.51359517 0.54380665 0.77945619 0.80664653]
 [0.74622356 0.59818731 0.79758308 1.        ]]
StarcoderdataJupyterStructuredCleanDedup