"""

################################
ENTER CONTROL S TO SAVE THE FILE
################################

Akshat Santhana Gopalan, Abhinav Senthilkumar, Eshaan Bhandar, Rohan Kulkarni, Shridhar 
Mutation Tech HackJPS 2023 Project 
A DNA Mutation Simulation that allows the user to choose from Preset Mutations, or choose their own mutation. 
6/4/23
"""

import os
import time
import random

codon_chart = {
  "TTT": "Phenylalanine (Phe)",
  "TTC": "Phenylalanine (Phe)",
  "TTA": "Leucine (Leu)",
  "TTG": "Leucine (Leu)",
  "TCT": "Serine (Ser)",
  "TCC": "Serine (Ser)",
  "TCA": "Serine (Ser)",
  "TCG": "Serine (Ser)",
  "TAT": "Tyrosine (Tyr)",
  "TAC": "Tyrosine (Tyr)",
  "TAA": "Stop codon",
  "TAG": "Stop codon",
  "TGT": "Cysteine (Cys)",
  "TGC": "Cysteine (Cys)",
  "TGA": "Stop codon",
  "TGG": "Tryptophan (Trp)",
  "CTT": "Leucine (Leu)",
  "CTC": "Leucine (Leu)",
  "CTA": "Leucine (Leu)",
  "CTG": "Leucine (Leu)",
  "CCT": "Proline (Pro)",
  "CCC": "Proline (Pro)",
  "CCA": "Proline (Pro)",
  "CCG": "Proline (Pro)",
  "CAT": "Histidine (His)",
  "CAC": "Histidine (His)",
  "CAA": "Glutamine (Gln)",
  "CAG": "Glutamine (Gln)",
  "CGT": "Arginine (Arg)",
  "CGC": "Arginine (Arg)",
  "CGA": "Arginine (Arg)",
  "CGG": "Arginine (Arg)",
  "ATT": "Isoleucine (Ile)",
  "ATC": "Isoleucine (Ile)",
  "ATA": "Isoleucine (Ile)",
  "ATG": "Methionine (Met) or Start codon",
  "ACT": "Threonine (Thr)",
  "ACC": "Threonine (Thr)",
  "ACA": "Threonine (Thr)",
  "ACG": "Threonine (Thr)",
  "AAT": "Asparagine (Asn)",
  "AAC": "Asparagine (Asn)",
  "AAA": "Lysine (Lys)",
  "AAG": "Lysine (Lys)",
  "AGT": "Serine (Ser)",
  "AGC": "Serine (Ser)",
  "AGA": "Arginine (Arg)",
  "AGG": "Arginine (Arg)",
  "GTT": "Valine (Val)",
  "GTC": "Valine (Val)",
  "GTA": "Valine (Val)",
  "GTG": "Valine (Val)",
  "GCT": "Alanine (Ala)",
  "GCC": "Alanine (Ala)",
  "GCA": "Alanine (Ala)",
  "GCG": "Alanine (Ala)",
  "GAT": "Aspartic Acid (Asp)",
  "GAC": "Aspartic Acid (Asp)",
  "GAA": "Glutamic Acid (Glu)",
  "GAG": "Glutamic Acid (Glu)",
  "GGT": "Glycine (Gly)",
  "GGC": "Glycine (Gly)",
  "GGA": "Glycine (Gly)",
  "GGG": "Glycine (Gly)"
}
replay = 1
while replay == 1:
  print()
  print()
  print("Copyright @ Mutation Tech")
  print("All rights reserved © Mutation Tech")
  print("A HACKJPS 2023 Project")
  print("**************************************************")
  print("**************************************************")
  print("*Welcome to Mutation Tech DNA Simulation Services*")
  print("**************************************************")
  print("**************************************************\n")


  print("DNA Sequence Example - 'ATCGAT'\n")



  
  print("Enter 1 for custom mutation testing: ")
  print("Enter 2 for preset mutation testing: \n")
  option_1 = input()
  if option_1 != '1' and option_1 != '2':
    print("Enter a valid number")
    try:
      option_1 = int(option_1)
    except ValueError:
      option_1 = input()
    else:
      option_1 = int(option_1)
  if int(option_1) == 1:
    print("Please enter your DNA sequence below: ")
    sequence = input().strip().upper().replace('U', 'T').replace(" ", "")
    while any(base not in "ACTG" for base in sequence):
      print("Invalid characters found in the sequence.")
      print(
        "Please enter a valid DNA sequence (containing only A, C, G, or T): ")
      sequence = input().strip().upper().replace('U', 'T').replace(" ", "")
    print("Processing . . .")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("Sequence received and validated successfully.\n")
    time.sleep(1)

    print("Enter a type of mutation:")
    print("Enter 1 for Substitution")
    print("Enter 2 for Deletion")
    print("Enter 3 for Insertion")
    print("Enter 4 for Inversion")
    option_1_1 = int(input())

    if option_1_1 == 1:
      print("Enter the base number you would like to substitute: ")
      base = int(input())
      if isinstance(base, int):
        pass
      else:
        print("Enter a valid number")
        base = int(input())

      print(
        "Enter the preferred nucleotide (just the first letter in CAPS (A, T, C, G): "
      )
      nucleotide = input()

      print("Start of simulation...\n")
      time.sleep(3)

      print("Original Sequence:")
      print(sequence)

      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]

      print("Codons correspond to the given DNA strand")
      print(start_amino_acids)

      sequence = sequence.split()
      joinedSequence = "".join(sequence)
      givenNucleotide = joinedSequence[base - 1]

      print("\nNucleotide to be substituted is *" + givenNucleotide + "*")
      print("Going to be substituted with *" + nucleotide + "*")
      print("Result:")
      print("********")

      endingSequence = joinedSequence[0:base -
                                      1] + nucleotide + joinedSequence[base:]
      endingSequence = endingSequence.upper()
      print(endingSequence + "\n")

      codons = [
        endingSequence[i:i + 3] for i in range(0, len(endingSequence), 3)
      ]
      amino_acids = [codon_chart.get(codon, "N/A") for codon in codons]
      print("Amino Acids:")
      print(amino_acids)

      if start_amino_acids == amino_acids:
        print("Type of mutation: ")
        print("/nSilent Mutation")
        print("Severity Analytics:")
        print(
          "Silent mutations are not considered severe. They do not change the amino acid or affect the sequence, thus known           as 'invisible' or silent mutations."
        )
        print("Probability of this mutation: ")
        print("1 - 10% chance of gene receiving a silent mutation.")
      if start_amino_acids != amino_acids and "Stop codon" in amino_acids:
        print("Type of Mutation:\n")
        print(
          "Nonsense Mutation - Premature stop codon detected\n Probability of this mutation:\n The probability of a nonsense mutation occurring in a specific gene can be in the range of 0.001% to 0.1%."
        )
        print("Severity analytics:")
        print("Critically severe")
      else:
        print("Type of mutation:")
        print("Missense Mutation - Singular Amino Acid Altered\n Probability of this mutation: \n  Estimates suggest that missense mutations can occur with a probability ranging from 0.1% to 1% in coding regions.")

    elif option_1_1 == 2:
      print("Enter the base number you would like to delete: ")
      base = int(input())
      if isinstance(base, int):
        pass
      else:
        print("Enter a valid number")
        base = int(input())

      print("Start of simulation...\n")
      time.sleep(3)

      print("Original Sequence:")
      print(sequence)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]

      print("Codons correspond to the given DNA strand")
      print(start_amino_acids)
      sequence = sequence.split()
      joinedSequence = "".join(sequence)
      givenNucleotide = joinedSequence[base - 1]

      print("\nNucleotide to be deleted is *" + givenNucleotide + "*")
      print("Result:")
      print("********")

      endingSequence = joinedSequence[0:base - 1] + joinedSequence[base:]
      endingSequence = endingSequence.upper()
      print(endingSequence + "\n")

      codons = [
        endingSequence[i:i + 3] for i in range(0, len(endingSequence), 3)
      ]
      amino_acids = [codon_chart.get(codon, "N/A") for codon in codons]
      print("Amino Acids:")
      print(amino_acids)

      if start_amino_acids == amino_acids:
        print("Type of mutation: ")
        print("/nSilent Mutation")
        print("Severity Analytics:")
        print(
          "Silent mutations are not considered severe. They do not change the amino acid or affect the sequence, thus known           as 'invisible' or silent mutations."
        )
        print("Probability of this mutation: ")
        print("1 - 10% chance of gene receiving a silent mutation.")
      if start_amino_acids != amino_acids and "Stop codon" in amino_acids:
        print("Type of Mutation:\n")
        print(
          "Nonsense Mutation - Premature stop codon detected\n Probability of this mutation:\n The probability of a nonsense mutation occurring in a specific gene can be in the range of 0.001% to 0.1%."
        )
        print("Severity analytics:")
        print("Critically severe")
      else:
        print("Type of mutation:")
        print(
          "Missense Mutation - Singular Amino Acid Altered\n Probability of this mutation: \n  Estimates suggest that missense mutations can occur with a probability ranging from 0.1% to 1% in coding regions."
        )

    elif option_1_1 == 3:
      print("Enter the base number you would like to insert at: ")
      base_position = int(input())
      if isinstance(base_position, int):
        pass
      else:
        print("Enter a valid number")
        base_position = int(input())

      print("Enter the nucleotide you would like to insert (A, T, C, G): ")
      nucleotide = input()

      print("Start of simulation...\n")
      time.sleep(3)

      print("Original Sequence:")
      print(sequence)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]

      print("Codons correspond to the given DNA strand")
      print(start_amino_acids)

      print(f"\nNucleotide to be inserted is: {nucleotide}")
      print(f"Inserting in position: {base_position}")

      strand = [*sequence]
      strand.insert(base_position - 1, nucleotide)
      output = "".join(strand)
      output = output.upper()
      print(output + "\n")
      codons = [output[i:i + 3] for i in range(0, len(output), 3)]
      amino_acids = [codon_chart.get(codon, "N/A") for codon in codons]
      print("Amino Acids:")
      print(amino_acids)

      if start_amino_acids == amino_acids:
        print("Type of mutation: ")
        print("/nSilent Mutation")
        print("Severity Analytics:")
        print(
          "Silent mutations are not considered severe. They do not change the amino acid or affect the sequence, thus known           as 'invisible' or silent mutations."
        )
        print("Probability of this mutation: ")
        print("1 - 10% chance of gene receiving a silent mutation.")
      if start_amino_acids != amino_acids and "Stop codon" in amino_acids:
        print("Type of Mutation:\n")
        print(
          "Nonsense Mutation - Premature stop codon detected\n Probability of this mutation:\n The probability of a nonsense mutation occurring in a specific gene can be in the range of 0.001% to 0.1%."
        )
        print("Severity analytics:")
        print("Critically severe")
      else:
        print("Type of mutation:")
        print(
          "Missense Mutation - Singular Amino Acid Altered\n Probability of this mutation: \n  Estimates suggest that missense mutations can occur with a probability ranging from 0.1% to 1% in coding regions."
        )

    elif option_1_1 == 4:
      print("Original Sequence:")
      print(sequence + "\n")
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]

      print("Codons correspond to the given DNA strand")
      print(start_amino_acids)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]

      print("Codons correspond to the given DNA strand")
      print(start_amino_acids)
      time.sleep(2)

      inverse = sequence[::-1]
      print("Inverted DNA Sequence:")
      print(inverse + "\n")

      codons = [inverse[i:i + 3] for i in range(0, len(inverse), 3)]
      amino_acids = [codon_chart.get(codon, "N/A") for codon in codons]
      print("Amino Acids:")
      print(amino_acids)

      print("Inverse Mutation - Order of bases reversed")
    replay = int(input("\nEnter 1 to run the program again: "))

  if int(option_1) == 2:
    print("Please enter your DNA sequence below: ")
    sequence = input().strip().upper().replace('U', 'T').replace(" ", "")
    while any(base not in "ACTG" for base in sequence):
      print("Invalid characters found in the sequence.")
      print(
        "Please enter a valid DNA sequence (containing only A, C, G, or T): ")
    print("Processing . . .")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("***")
    time.sleep(1)
    print("Sequence received and validated successfully.\n")
    time.sleep(1)

    print("Preset Mutation Options")
    time.sleep(1)
    print("Choose a disorder to see how this mutation alters your sample DNA ")
    time.sleep(1)
    print("Enter the number corresponding to the disorder of choice: ")
    time.sleep(1)
    print("Enter 1 to simulate Sickle Cell Anemia")
    print("Enter 2 to simulate Cystic Fibrosis")  #deletion
    print("Enter 3 to simulate Beta Thalessemia")
    option_1_2 = int(input())

    if option_1_2 == 1:
      print("Sickle Cell Anemia")
      time.sleep(1)
      print("Sickle cell anemia is a genetic disorder caused by a mutation in the hemoglobin gene, resulting in abnormal red blood cells that can cause pain and organ damage.")
      time.sleep(3)
      print("Original DNA Sequence: " + sequence)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]
      print("Original Amino Acid Sequence: ")
      print(start_amino_acids)
      print("Sickle Cell Anemia is caused by a SUBSTITION in the base")
      random_base = random.randint(0, (len(sequence) - 1))
      print("Auto-generated testing base pair: " + str(random_base))
      
      sequence = ''.join(sequence)
      endingSequence = sequence[0:random_base - 1] + 'T' + sequence[random_base:]
      

      sequence = ''.join(endingSequence)
      end_codons = [endingSequence[i:i + 3] for i in range(0, len(endingSequence), 3)]
      end_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in end_codons
      ]
      print(f"Mutated DNA Sequence: {endingSequence}")
      print(f"Mutated Amino Acids: {end_amino_acids}")
    
      print("Severity Analytics: ")
      print("Criticially Fatal - Possesses different variations and degrees of symptoms related to the blood, but can very negatively impact health.")
      print("Probability: ")
      print("Rare - Less than 1% of the people in America get it every year.")
      
    
      time.sleep(3)
    elif option_1_2 == 2:
      print("Cystic Fibrosis")
      time.sleep(1)
      print("Cystic fibrosis is a genetic disorder caused by a mutation in the CFTR gene, leading to the production of thick, sticky mucus that affects the lungs, digestive system, and other organs.")
      time.sleep(3)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]
      print("Original Sequence: "+sequence)
      start_amino_acids = ''.join(start_amino_acids)
      print("Original Amino Acids: "+start_amino_acids)
      time.sleep(1)
      print("This is caused by a DELETION in a base pair")
      sequence = list(sequence)
      randomDeletion = random.randint(0, (len(sequence) - 1))
      print("The base to be deleted is: "+ str(sequence[randomDeletion])+", the location is "+str(randomDeletion+1))
      sequence.pop(randomDeletion)
      time.sleep(1)
      sequence = ''.join(sequence)
      print("Mutated sequence is :"+sequence)
      end_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      end_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in end_codons
      ]
      end_amino_acids = ''.join(end_amino_acids)
      print("Mutated Amino Acids: "+ end_amino_acids) 
      time.sleep(1)
      print("Severity of Cystic Fibrosis is EXTREMELY CRITICAL\n Approximately, 1 in 30 in the US are carriers")
    elif option_1_2 == 3:
      print("Beta Thalassemia")
      time.sleep(1)
      print("Beta thalassemia is a genetic disorder characterized by reduced production of beta globin chains, leading to abnormal red blood cells and varying degrees of anemia.")
      time.sleep(3)
      start_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      start_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in start_codons
      ]
      print("Original Sequence: "+sequence)
      start_amino_acids = ''.join(start_amino_acids)
      print("Original Amino Acids: "+start_amino_acids)
      time.sleep(1)
      print("This is caused by a DELETION in a base pair")
      sequence = list(sequence)
      randomDeletion = random.randint(0, (len(sequence) - 1))
      print("The base to be deleted is: "+ str(sequence[randomDeletion])+", the location is "+str(randomDeletion+1))
      sequence.pop(randomDeletion)
      time.sleep(1)
      sequence = ''.join(sequence)
      print("Mutated sequence is :"+sequence)
      end_codons = [sequence[i:i + 3] for i in range(0, len(sequence), 3)]
      end_amino_acids = [
        codon_chart.get(codon, "N/A") for codon in end_codons
      ]
      end_amino_acids = ''.join(end_amino_acids)
      print("Mutated Amino Acids: "+ end_amino_acids) 
      time.sleep(1)
      print("Severity of Beta Thalassemia is CRITICAL\n In the US, an estimated 1 in 100,000 get this disease each year")
else:
  print("Exit Program")
  pass
