Python Bioinformatics

Computer Science | Artificial Intelligence | Computational Biology | Chess | Blender

Python Bioinformatics

This is a simple program in Python that analyzes DNA strands. The program reads a DNA strand and provides insights into the genetic code of organisms. The program is used to demonstrate the syntax and structure of a Python program.

This also shows that Bioinformatics can be done too in Python, with the power of vast Python libraries in acknowledgement I am able to make that happen.

Python Bioinformatics

import random
Nucleotides = ["A","T","C","G"]

def validate_code(code):
    """check if our code is valid to be DNA"""

    correct = []
    for i in code:
        if i in Nucleotides:
            correct.append(i)
        else:
            continue
    return correct

def count_code(code):
    """count how many times each Nucleotide appears"""
    DNA = {"A":0,"T":0,"C":0,"G":0}
    for i in code:
        if i in DNA:
            DNA[i] += 1
        else:
            continue
    return DNA

def RNA_generator(code):
    """generate RNA to be translated"""
    RNA = {"T": "U"}
    RNA_strand = []
    for i in code:
        if i == "T":
            RNA_strand.append(RNA.get(i))
        else:
            RNA_strand.append(i)
    return RNA_strand

def reverse(code):
    DNA = {"A":"T","T":"A",
           "G":"C","C":"G"}
    template = []
    for i in code:
        template.append(DNA.get(i))
    return template[::-1]

def GC_percent(code):
    GC_counter = {"G":0,"C":0}
    for i in code:
        if i in GC_counter.keys():
            GC_counter[i] += 1
        else:
            continue
    result = (GC_counter.get("G") + GC_counter.get("C")) / len(code) * 100
    return round(result)

def check_genes(code):
    codex = ""
    for i in code:
        codex += i
    if "GTG" in codex:
        print("Valid")
    else:
        print("Invalid")

def DNA_mapping(code):
    dna = [random.choice(Nucleotides) for i in range(50)]
    error2 = []
    error = []
    index = 0
    for i in code:
        if i != dna[index]:
            error.append(dna[index])
            error2.append(i)
        index += 1
    print("".join(dna))
    print("".join(code))
    print()
    print(' '.join(error))
    print(' '.join(error2))
    print()
    percent = float(len(error2) / len(code) * 100)
    print(f"Mutation: {percent}%")

def GC_content_per_seq(code,segment=5):
    DNA_segment = []
    for i in range(0,len(code),segment):
        DNA = code[i:i+segment]
        DNA_segment.append(GC_percent(DNA))
    return DNA_segment